o
    FDi                     @   sL   d dl mZ ddlmZ ddlmZmZmZmZ dd Z	G dd deZ
d	S )
    )log   )Column)UInt8ColumnUInt16ColumnUInt32ColumnUInt64Columnc                 C   s$   | dd }||}t |fi |S )N   )LowCardinalityColumn)speccolumn_by_spec_gettercolumn_optionsinnernested r   b/var/www/Datamplify/venv/lib/python3.10/site-packages/asynch/proto/columns/lowcardinalitycolumn.pycreate_low_cardinality_column   s   r   c                       s`   e Zd ZdZeeeedZdZ	dZ
e	e
B Z fddZdd Zd	d
 Zdd ZdddZ  ZS )r   z~
    Stores column as index (unique elements) and keys.
    Good for de-duplication of large values with low cardinality.
    )r   r         i   i   c                    s2   | t|j|jd || _t jdi | d S )Nreaderwriterr   )updatedictr   r   nested_columnsuper__init__)selfr   kwargs	__class__r   r   r      s   zLowCardinalityColumn.__init__c                    s   | j  I d H S N)r   read_uint64r   r   r   r   read_state_prefix$   s   z&LowCardinalityColumn.read_state_prefixc                    s   | j dI d H S )Nr   )r   write_int64r$   r   r   r   write_state_prefix)   s   z'LowCardinalityColumn.write_state_prefixc           
         sr  g g }}i }| j jrD|| j j d| j _|D ])}|d u r%|d q||}|d u r;t|}|||< || ||d  qn"|D ]}||}|d u r`t|}t|||< || || qFt|sld S ttt|dd }| j| | j	| j
d}| j|B }	| j
|	I d H  | j
t|I d H  | j |I d H  | j
t|I d H  ||I d H  d S )NFr   r   r      r   )r   nullableappend
null_valuegetlenintr   	int_typesr   r   serialization_typer&   
write_data)
r   itemsindexkeyskey_by_index_elementxkeyint_type
int_columnr0   r   r   r   _write_data/   sJ   






z LowCardinalityColumn._write_dataNc           	         s   |st  S | j I d H }|d@ }| j| | j| jd}| jj}d| j_| j I d H }| j|I d H  |r@d dd    | j I d H  ||I d H }t  fdd|D S )Nr	   r   Fr"   r   c                 3   s    | ]} | V  qd S r"   r   ).0r6   r3   r   r   	<genexpr>   s    z2LowCardinalityColumn._read_data.<locals>.<genexpr>)tupler   r#   r/   r   r   r)   	read_data)	r   n_items	nulls_mapr0   key_typekeys_columnr)   
index_sizer4   r   r<   r   
_read_datak   s&   

zLowCardinalityColumn._read_datar"   )__name__
__module____qualname____doc__r   r   r   r   r/   has_additional_keys_bitneed_update_dictionaryr0   r   r%   r'   r:   rE   __classcell__r   r   r    r   r      s    <r   N)mathr   baser   	intcolumnr   r   r   r   r   r   r   r   r   r   <module>   s
    