U
    dª/eÊ  ã                   @   sd   d dl mZ ddlmZ 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é   ©Úread_binary_uint64©Úwrite_binary_int64é   )ÚColumn)ÚUInt8ColumnÚUInt16ColumnÚUInt32ColumnÚUInt64Columnc                 C   s    | dd… }||ƒ}t |f|ŽS )Né   éÿÿÿÿ)ÚLowCardinalityColumn)ÚspecZcolumn_by_spec_getterZcolumn_optionsÚinnerÚnested© r   úR/tmp/pip-unpacked-wheel-mds_o03w/clickhouse_driver/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   r   é   i   i   c                    s   || _ tt| ƒjf |Ž d S ©N)Únested_columnÚsuperr   Ú__init__)Úselfr   Úkwargs©Ú	__class__r   r   r   %   s    zLowCardinalityColumn.__init__c                 C   s   t |ƒS r   r   ©r   Úbufr   r   r   Úread_state_prefix)   s    z&LowCardinalityColumn.read_state_prefixc                 C   s   t d|ƒ d S )Nr   r   r    r   r   r   Úwrite_state_prefix,   s    z'LowCardinalityColumn.write_state_prefixc                 C   sF  g g  }}i }| j jr†| | j j¡ d| j _|D ]R}|d krH| d¡ q0| |¡}|d krtt|ƒ}|||< | |¡ | |d ¡ q0nD|D ]>}| |¡}|d kr¾t|ƒ}t|ƒ||< | |¡ | |¡ qŠt|ƒsÖd S ttt|ƒdƒd ƒ}| j| ƒ }	| j	|B }
t
|
|ƒ t
t|ƒ|ƒ | j  ||¡ t
t|ƒ|ƒ |	 ||¡ d S )NFr   r   r   é   )r   ÚnullableÚappendZ
null_valueÚgetÚlenÚintr   Ú	int_typesÚserialization_typer   Ú
write_dataZwrite_items)r   Úitemsr!   ÚindexÚkeysZkey_by_index_elementÚxÚkeyZint_typeZ
int_columnr+   r   r   r   Ú_write_data0   s>    






z LowCardinalityColumn._write_dataNc           
         sŠ   |s
t ƒ S t|ƒ}|d@ }| j| ƒ }| jj}d| j_t|ƒ}| j ||¡‰ |r`dˆ dd …  ‰ t|ƒ | ||¡}	t ‡ fdd„|	D ƒƒS )Nr   Fr   r   c                 3   s   | ]}ˆ | V  qd S r   r   )Ú.0r0   ©r.   r   r   Ú	<genexpr>{   s     z2LowCardinalityColumn._read_data.<locals>.<genexpr>)Útupler   r*   r   r%   Ú	read_data)
r   Zn_itemsr!   Z	nulls_mapr+   Zkey_typeZkeys_columnr%   Z
index_sizer/   r   r4   r   Ú
_read_datae   s    zLowCardinalityColumn._read_data)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r   r   r*   Zhas_additional_keys_bitZneed_update_dictionaryr+   r   r"   r#   r2   r8   Ú__classcell__r   r   r   r   r      s   ü
5r   N)Úmathr   Úreaderr   Úwriterr   Úbaser	   Z	intcolumnr
   r   r   r   r   r   r   r   r   r   Ú<module>   s   