U
    )3g;                  
   @   s  d dl mZmZ d dlmZmZ z0d dlmZmZm	Z	m
Z
mZmZmZmZmZ W n8 ek
r Z zedee ddW 5 dZ[X Y nX zd dlmZ d dlmZ W n& ek
r   G dd	 d	ZeZY nX dddZddd
defddZd
efddZdd ZefddZdddZdS )    )TableRecordBatch)
Expressionfield)	DeclarationExecNodeOptionsTableSourceNodeOptionsFilterNodeOptionsProjectNodeOptionsAggregateNodeOptionsOrderByNodeOptionsHashJoinNodeOptionsAsofJoinNodeOptionsz@The pyarrow installation is not built with support for 'acero' ()N)ScanNodeOptionsc                   @   s(   e Zd ZG dd dZG dd dZdS )DatasetModuleStubc                   @   s   e Zd ZdS )zDatasetModuleStub.DatasetN__name__
__module____qualname__ r   r   1/tmp/pip-unpacked-wheel-0fuzq0j6/pyarrow/acero.pyDataset3   s   r   c                   @   s   e Zd ZdS )z!DatasetModuleStub.InMemoryDatasetNr   r   r   r   r   InMemoryDataset6   s   r   N)r   r   r   r   r   r   r   r   r   r   2   s   r   Tc                 C   sl   t dt| |d}dd | jjD }t |t dt|g}| jd}|d k	rht |t dt|g}|S )Nscanuse_threadsc                 S   s   g | ]}t |qS r   )r   ).0fr   r   r   
<listcomp>@   s     z$_dataset_to_decl.<locals>.<listcomp>projectfilter)	r   r   schemanamesfrom_sequencer
   Z_scan_optionsgetr	   )datasetr   declprojectionsZfilter_exprr   r   r   _dataset_to_decl;   s    r)   Fc
                    s  t |ttjfs"tdt| t |ttjfsDtdt| i  t |ttfs\|g}t|D ]\}
}|
 |< qdi t |ttfs|g}t|D ]\}
}|
|< q|j	j
}|j	j
}| dks| dkrg }nX| dks| dkrg }nB| dks| dkr
fdd	|D }n| d
kr& fdd	|D }i }t|D ]\}
}||kr2|
||< q2i }t|D ]\}
}||kr\|
||< q\t |tjrt||d}ntdt|}t |tjrt||d}ntdt|}|rt| |||||pd|pdd}nt| |||pd|pdd}td|||gd}|r`| dkr`t|}t|}t|}g }g }t|| D ]\}
}|
t|k r||kr|| || |   }|tdt|
t|| g nx|
|kr||krqdn^|r|
|k r||kr||7 }|r$|
|kr$||kr$||7 }|| |t|
 qdtdt||}t||g}|j|d}|	tkrz|S |	tjkrt|S tddS )a\  
    Perform join of two tables or datasets.

    The result will be an output table with the result of the join operation

    Parameters
    ----------
    join_type : str
        One of supported join types.
    left_operand : Table or Dataset
        The left operand for the join operation.
    left_keys : str or list[str]
        The left key (or keys) on which the join operation should be performed.
    right_operand : Table or Dataset
        The right operand for the join operation.
    right_keys : str or list[str]
        The right key (or keys) on which the join operation should be performed.
    left_suffix : str, default None
        Which suffix to add to left column names. This prevents confusion
        when the columns in left and right operands have colliding names.
    right_suffix : str, default None
        Which suffix to add to the right column names. This prevents confusion
        when the columns in left and right operands have colliding names.
    use_threads : bool, default True
        Whether to use multithreading or not.
    coalesce_keys : bool, default False
        If the duplicated keys should be omitted from one of the sides
        in the join result.
    output_type: Table or InMemoryDataset
        The output type for the exec plan result.

    Returns
    -------
    result_table : Table or InMemoryDataset
    Expected Table or Dataset, got z	left semiz	left antiz
right semiz
right antiinnerz
left outerc                    s   g | ]}| kr|qS r   r   r   col)right_keys_orderr   r   r      s     z!_perform_join.<locals>.<listcomp>zright outerc                    s   g | ]}| kr|qS r   r   r,   )left_keys_orderr   r   r      s     r   table_source )Zoutput_suffix_for_leftZoutput_suffix_for_rightZhashjoinoptionsinputsz
full outerZcoalescer    Unsupported output typeN)
isinstancer   dsr   	TypeErrortypetuplelist	enumerater"   r#   r)   r   r   r   setlenappendr   Z_callZ_fieldr
   r$   to_tabler   )Z	join_typeleft_operandZ	left_keysright_operandZ
right_keysZleft_suffixZright_suffixr   Zcoalesce_keysoutput_typeidxkeyZleft_columnsright_columnsZleft_column_keys_indicesZcolnameZright_column_keys_indicesleft_sourceright_source	join_optsr'   Zleft_columns_setZright_columns_setZright_operand_indexZprojected_col_namesr(   r-   Zright_key_indexZ
projectionresult_tabler   )r/   r.   r   _perform_joinP   s    (






         

 
 

rK   c	                    sX  t | ttjfs"tdt|  t |ttjfsDtdt| t |ttfsX|g}t  ttfsl g  fdd|jj	D }	t
| jj	t
|	@ }
|
rtd|
t | tjrt| |d}ntdt| }t |tjrt||d}ntdt|}t|| |}td|||gd}|j|d}|tkr6|S |tjkrLt|S td	d
S )a-  
    Perform asof join of two tables or datasets.

    The result will be an output table with the result of the join operation

    Parameters
    ----------
    left_operand : Table or Dataset
        The left operand for the join operation.
    left_on : str
        The left key (or keys) on which the join operation should be performed.
    left_by: str or list[str]
        The left key (or keys) on which the join operation should be performed.
    right_operand : Table or Dataset
        The right operand for the join operation.
    right_on : str or list[str]
        The right key (or keys) on which the join operation should be performed.
    right_by: str or list[str]
        The right key (or keys) on which the join operation should be performed.
    tolerance : int
        The tolerance to use for the asof join. The tolerance is interpreted in
        the same units as the "on" key.
    output_type: Table or InMemoryDataset
        The output type for the exec plan result.

    Returns
    -------
    result_table : Table or InMemoryDataset
    r*   c                    s   g | ]}|g  kr|qS r   r   r,   right_byright_onr   r   r   -  s   z&_perform_join_asof.<locals>.<listcomp>zOColumns {} present in both tables. AsofJoin does not support column collisions.r   r0   Zasofjoinr2   r5   N)r6   r   r7   r   r8   r9   r:   r;   r"   r#   r=   
ValueErrorformatr)   r   r   r   r@   r   )rA   Zleft_onZleft_byrB   rN   rM   Z	tolerancer   rC   rF   Zcolumns_collisionsrG   rH   rI   r'   rJ   r   rL   r   _perform_join_asof  s`    !        

rQ   c                 C   sh   d}t | trt| g} d}ttdt| dtdt|dg}|jdd}|rd|	 
 d }|S )a}  Filter rows of a table based on the provided expression.

    The result will be an output table with only the rows matching
    the provided expression.

    Parameters
    ----------
    table : Table or RecordBatch
        Table that should be filtered.
    expression : Expression
        The expression on which rows should be filtered.

    Returns
    -------
    Table
    FTr0   )r3   r!   r   r   )r6   r   r   Zfrom_batchesr   r$   r   r	   r@   Zcombine_chunksZ
to_batches)tableZ
expressionZis_batchr'   resultr   r   r   _filter_tableW  s    
rT   c                 K   s   t | tjrt| dd}ntdt| }tdt|f|}t||g}|jdd}|t	kr`|S |tj
krtt
|S tdd S )NTr   r0   order_byr5   )r6   r7   r   r)   r   r   r   r$   r@   r   r   r8   )Ztable_or_dataset	sort_keysrC   kwargsZdata_sourcerU   r'   rJ   r   r   r   _sort_sourcew  s     

rX   c              	   C   s2   t t dt| t dt||dg}|j|dS )Nr0   Z	aggregate)keysr   )r   r$   r   r   r@   )rR   Z
aggregatesrY   r   r'   r   r   r   	_group_by  s
    rZ   )T)T)Zpyarrow.libr   r   Zpyarrow.computer   r   Zpyarrow._aceror   r   r   r	   r
   r   r   r   r   ImportErrorexcstrZpyarrow.datasetr&   r7   Zpyarrow._datasetr   r   r)   rK   rQ   rT   rX   rZ   r   r   r   r   <module>   s:   0

  
 4
V 