o
    KDiZ*                     @   s   d dl mZ d dlZddlmZmZ edZG dd deZ	G dd	 d	e	Z
G d
d de	ZG dd de	ZG dd de	ZG dd deZG dd deZG dd deZG dd de	ZG dd de	ZG dd de	Zee e	ZdS )    )unicode_literalsN   )
comma_joinget_subclass_namesclickhouse_ormc                   @      e Zd Zdd ZdS )Enginec                 C   s   t  N)NotImplementedErrorselfdb r   T/var/www/Datamplify/venv/lib/python3.10/site-packages/infi/clickhouse_orm/engines.pycreate_table_sql   s   zEngine.create_table_sqlN__name__
__module____qualname__r   r   r   r   r   r   
       r   c                   @   r   )TinyLogc                 C      dS )Nr   r   r   r   r   r   r         zTinyLog.create_table_sqlNr   r   r   r   r   r      r   r   c                   @   r   )Logc                 C   r   )Nr   r   r   r   r   r   r      r   zLog.create_table_sqlNr   r   r   r   r   r      r   r   c                   @   r   )Memoryc                 C   r   )Nr   r   r   r   r   r   r      r   zMemory.create_table_sqlNr   r   r   r   r   r      r   r   c                   @   sF   e Zd Z			dddZedd Zejdd Zd	d
 Zdd ZdS )	MergeTreeNr       c	           	      C   s   t |ttfv sJ d|d u st|tsJ d|d u s)t |ttfv s)J d|d u s9t |ttfv s9J d|d u |d u ksEJ d|sM|sMJ d|| _|rT|nd| f| _|| _|| _|| _	|| _
|| _|| _d S )Nz order_by must be a list or tuplez"date_col must be string if presentz#primary_key must be a list or tuplez.partition_key must be tuple or list if presentz:both replica_table_path and replica_name must be specifiedz-You must set either date_col or partition_keyztoYYYYMM(`%s`))typelisttuple
isinstancestrdate_colpartition_keyprimary_keyorder_bysampling_exprindex_granularityreplica_table_pathreplica_name)	r   r"   r%   r&   r'   r(   r)   r#   r$   r   r   r   __init__$   s$    
zMergeTree.__init__c                 C   s   t d | jS Nza`key_cols` attribute is deprecated and may be removed in future. Use `order_by` attribute insteadloggerwarningr%   )r   r   r   r   key_cols=   s   
zMergeTree.key_colsc                 C   s   t d || _d S r+   r,   )r   valuer   r   r   r/   B   s   

c                 C   s   | j j}| jrd| }|jdkr@dt| jddt| jddf }| jr.|dt| jdd 7 }| jr8|d| j 7 }|d| j	 7 }n| j
sMd	d
lm} |dd}| |}d|t||f S )N
Replicatedr   r   i&  zPARTITION BY (%s) ORDER BY (%s)T	stringifyz PRIMARY KEY (%s)z SAMPLE BY %sz SETTINGS index_granularity=%dr   )DatabaseExceptionzCustom partitioning is not supported before ClickHouse 1.1.54310. Please update your server or use date_col syntax.https://clickhouse.tech/docs/en/table_engines/custom_partitioning_key/ z	%s(%s) %s)	__class__r   r)   server_versionr   r#   r%   r$   r&   r'   r"   infi.clickhouse_orm.databaser5   _build_sql_params)r   r   namepartition_sqlr5   paramsr   r   r   r   G   s(   

zMergeTree.create_table_sqlc                 C   sv   g }| j r|d| j d| j  g7 }|jdk r9|| j | jr%|| j |dt| jdd  |t| j	 |S )Nz'%s'r2   (%s)Tr3   )
r)   r(   r8   appendr"   r&   r   r%   r!   r'   r   r   r=   r   r   r   r:   h   s   
zMergeTree._build_sql_params)Nr   Nr   NNNN)	r   r   r   r*   propertyr/   setterr   r:   r   r   r   r   r   "   s    


!r   c                       s0   e Zd Z			d	 fdd	Z fddZ  ZS )
CollapsingMergeTreeNr   signr   c
           
   
      (   t t| ||||||||	 || _d S r	   )superrC   r*   sign_col)
r   r"   r%   rG   r&   r'   r(   r)   r#   r$   r7   r   r   r*   }      
zCollapsingMergeTree.__init__c                    s    t t| |}|| j |S r	   )rF   rC   r:   r?   rG   r@   rH   r   r   r:      s   z%CollapsingMergeTree._build_sql_params)	Nr   rD   Nr   NNNNr   r   r   r*   r:   __classcell__r   r   rH   r   rC   {       rC   c                       0   e Zd Z			d fdd	Z fddZ  ZS )	SummingMergeTreeNr   r   c
           
   
      sH   t t| ||||||||	 td u st|ttfv sJ d|| _d S )Nz$summing_cols must be a list or tuple)rF   rN   r*   r   r   r   summing_cols)
r   r"   r%   rO   r&   r'   r(   r)   r#   r$   rH   r   r   r*      s
    
zSummingMergeTree.__init__c                    s.   t t| |}| jr|dt| j  |S )Nr>   )rF   rN   r:   rO   r?   r   r@   rH   r   r   r:      s   z"SummingMergeTree._build_sql_params	Nr   NNr   NNNNrJ   r   r   rH   r   rN      s    rN   c                       rM   )	ReplacingMergeTreeNr   r   c
           
   
      rE   r	   )rF   rQ   r*   ver_col)
r   r"   r%   rR   r&   r'   r(   r)   r#   r$   rH   r   r   r*      rI   zReplacingMergeTree.__init__c                    s&   t t| |}| jr|| j |S r	   )rF   rQ   r:   rR   r?   r@   rH   r   r   r:      s   z$ReplacingMergeTree._build_sql_paramsrP   rJ   r   r   rH   r   rQ      rL   rQ   c                   @   s&   e Zd ZdZ		dd	d
Zdd ZdS )Bufferz
    Buffers the data to write in RAM, periodically flushing it to another table.
    Must be used in conjuction with a `BufferModel`.
    Read more [here](https://clickhouse.tech/docs/en/engines/table-engines/special/buffer/).
       
   d   '  @B 逖  c	           	      C   s4   || _ || _|| _|| _|| _|| _|| _|| _d S r	   )
main_model
num_layersmin_timemax_timemin_rowsmax_rows	min_bytes	max_bytes)	r   r[   r\   r]   r^   r_   r`   ra   rb   r   r   r   r*      s   
zBuffer.__init__c              
   C   s4   d|j | j | j| j| j| j| j| j| j	f	 }|S )Nz7ENGINE = Buffer(`%s`, `%s`, %d, %d, %d, %d, %d, %d, %d))
db_namer[   
table_namer\   r]   r^   r_   r`   ra   rb   )r   r   sqlr   r   r   r      s   zBuffer.create_table_sqlN)rT   rU   rV   rW   rX   rY   rZ   r   r   r   __doc__r*   r   r   r   r   r   rS      s    
rS   c                   @   s    e Zd ZdZdd Zdd ZdS )Mergea  
    The Merge engine (not to be confused with MergeTree) does not store data itself,
    but allows reading from any number of other tables simultaneously.
    Writing to a table is not supported
    https://clickhouse.tech/docs/en/engines/table-engines/special/merge/
    c                 C   s   t |ts	J d|| _d S )Nz&'table_regex' parameter must be string)r    r!   table_regex)r   ri   r   r   r   r*      s   
zMerge.__init__c                 C   s   d|j | jf S )NzMerge(`%s`, '%s'))rc   ri   r   r   r   r   r      s   zMerge.create_table_sqlNrf   r   r   r   r   rh      s    rh   c                   @   s6   e Zd ZdZdddZedd Zdd Zd	d
 ZdS )Distributedan  
    The Distributed engine by itself does not store data,
    but allows distributed query processing on multiple servers.
    Reading is automatically parallelized.
    During a read, the table indexes on remote servers are used, if there are any.

    See full documentation here
    https://clickhouse.tech/docs/en/engines/table-engines/special/distributed/
    Nc                 C   s   || _ || _|| _dS )a  
        - `cluster`: what cluster to access data from
        - `table`: underlying table that actually stores data.
        If you are not specifying any table here, ensure that it can be inferred
        from your model's superclass (see models.DistributedModel.fix_engine_table)
        - `sharding_key`: how to distribute data among shards when inserting
        straightly into Distributed table, optional
        N)clustertablesharding_key)r   rk   rl   rm   r   r   r   r*      s   	
zDistributed.__init__c                 C   s(   ddl m} | j}t||r| S |S )Nr   )	ModelBase)modelsrn   rl   r    rd   )r   rn   rl   r   r   r   rd      s
   
zDistributed.table_namec                 C   s$   | j j}| |}d|d|f S )Nz%s(%s)z, )r7   r   r:   join)r   r   r;   r=   r   r   r   r      s   
zDistributed.create_table_sqlc                 C   sL   | j d u rtd| jjdd | j|j| j fD }| jr$|| j |S )Nz4Cannot create {} engine: specify an underlying tablec                 S   s   g | ]}d | qS )z`%s`r   ).0pr   r   r   
<listcomp>  s    z1Distributed._build_sql_params.<locals>.<listcomp>)	rd   
ValueErrorformatr7   r   rk   rc   rm   r?   r@   r   r   r   r:     s   
zDistributed._build_sql_params)NN)	r   r   r   rg   r*   rA   rd   r   r:   r   r   r   r   rj      s    
	
rj   )
__future__r   loggingutilsr   r   	getLoggerr-   objectr   r   r   r   r   rC   rN   rQ   rS   rh   rj   locals__all__r   r   r   r   <module>   s     
Y4