U
    d/e                     @   sX   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	 G dd	 d	eZ
d
d ZdS )    )chain)Struct   )Column)UInt64Column   )pairwisec                       sp   e Zd ZdZeefZ fddZdd Zdd Z	dd	 Z
d
d Zdd Zdd Zdd Zdd Zdd Z  ZS )ArrayColumna  
    Nested arrays written in flatten form after information about their
    sizes (offsets really).
    One element of array of arrays can be represented as tree:
    (0 depth)          [[3, 4], [5, 6]]
                      |               |
    (1 depth)      [3, 4]           [5, 6]
                   |    |           |    |
    (leaf)        3     4          5     6

    Offsets (sizes) written in breadth-first search order. In example above
    following sequence of offset will be written: 4 -> 2 -> 4
    1) size of whole array: 4
    2) size of array 1 in depth=1: 2
    3) size of array 2 plus size of all array before in depth=1: 2 + 2 = 4

    After sizes info comes flatten data: 3 -> 4 -> 5 -> 6
    c                    s4   t  | _|| _d| _tt| jf | |jg| _d S )NT)r   Zsize_columnnested_column_write_depth_0_sizesuperr	   __init__Z
null_value)selfr
   kwargs	__class__ I/tmp/pip-unpacked-wheel-mds_o03w/clickhouse_driver/columns/arraycolumn.pyr      s
    zArrayColumn.__init__c                 C   s2   t | j| _| j| j_d| _d| _| || d S )NF)r	   r
   nullabler   _write)r   databufr   r   r   
write_data%   s
    
zArrayColumn.write_datac                 C   s,   t | j| _| j| j_d| _| ||d S )NFr   )r	   r
   r   _read)r   Zrowsr   r   r   r   	read_data.   s    
zArrayColumn.read_datac                 C   s   g }| }| j rt|gng }|j}t|tsB|jr~dd |D }q~d}g }|D ]$}	|t|	7 }|| ||	 qN|}|}q|r| || t	d
t|}
||
j|  d S )Nc                 S   s   g | ]}|d kqS Nr   ).0xr   r   r   
<listcomp>>   s     z,ArrayColumn._write_sizes.<locals>.<listcomp>r   <{}Q)r   lenr
   
isinstancer	   r   appendextend_write_nulls_mapr   formatwritepack)r   valuer   	nulls_mapcolumnZsizesr
   offset	new_valuer   nsr   r   r   _write_sizes4   s(    

zArrayColumn._write_sizesc                 C   s>   | j r|pg }t| jtr(tt|}|r:| j|| d S r   )r   r!   r
   r	   listr   from_iterable_write_datar   r(   r   r   r   r   r1   Q   s    zArrayColumn._write_datac                 C   sR   | j r|pg }t| jtr8tt|}| j|| n| jj rN| j|| d S r   )	r   r!   r
   r	   r/   r   r0   _write_nulls_datar$   r2   r   r   r   r3   [   s    zArrayColumn._write_nulls_datac                 C   s2   |  |}| || | || | || d S r   )Zprepare_itemsr.   r3   r1   r2   r   r   r   r   f   s    
zArrayColumn._writec                 C   s   | j |S r   )r
   read_state_prefixr   r   r   r   r   r4   l   s    zArrayColumn.read_state_prefixc                 C   s   | j | d S r   )r
   write_state_prefixr5   r   r   r   r6   o   s    zArrayColumn.write_state_prefixc                    s   d|gg}| j }|}d }t|tr|d kr0dg}td|}|||j}|| |	| d }t
|dkr||d nd}|j }q|dkr|nd}	d }
|jr| |	|}
g  |	rt|j|	||
d t|D ]} fddt|D  qt S )Nr   r   )r)   c                    s   g | ]\}} || qS r   r   )r   beginendr   r   r   r      s     z%ArrayColumn._read.<locals>.<listcomp>)r
   r!   r	   r   r%   unpackreadsizer#   r"   r    r   Z_read_nulls_mapr/   Z
_read_datareversedr   tuple)r   r=   r   Zslices_seriesr
   Zcur_level_slice_sizeZcur_level_slicer-   Znested_sizesZn_itemsr)   Zslicesr   r:   r   r   r   s<    



  zArrayColumn._read)__name__
__module____qualname____doc__r/   r?   Zpy_typesr   r   r   r.   r1   r3   r   r4   r6   r   __classcell__r   r   r   r   r	   	   s   	
r	   c                 C   s   | dd }t ||f|S )N   r7   )r	   )specZcolumn_by_spec_getterZcolumn_optionsinnerr   r   r   create_array_column   s    rH   N)	itertoolsr   structr   baser   Z	intcolumnr   Zutil.helpersr   r	   rH   r   r   r   r   <module>   s    