
    Lg;                        d dl mZmZ d dlmZmZ 	 d dlmZmZm	Z	m
Z
mZmZmZmZmZ 	 d dlmZ d dlmZ ddZddd
defdZd
efdZd ZefdZddZy# e$ rZ ed ee       d      ddZ[ww xY w# e$ r  G d d	      ZeZY Sw xY w)    )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                   4    e Zd Z G d d      Z G d d      Zy)DatasetModuleStubc                       e Zd Zy)DatasetModuleStub.DatasetN__name__
__module____qualname__     -  /croot/pyarrow_1738229418212/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/lib/python3.12/site-packages/pyarrow/acero.pyDatasetr   3       r   r   c                       e Zd Zy)!DatasetModuleStub.InMemoryDatasetNr   r   r   r   InMemoryDatasetr    6   r   r   r!   N)r   r   r   r   r!   r   r   r   r   r   2   s    	 		 	r   r   TFc           	      z   t        dt        | ||            }| j                  j                  D cg c]  }t	        |       }}t        j
                  |t        dt        |            g      }| j                  j                  d      }|*t        j
                  |t        dt        |            g      }|S c c}w )Nscanuse_threadsrequire_sequenced_outputprojectfilter)
r   r   schemanamesr   from_sequencer   _scan_optionsgetr
   )datasetr%   r&   declfprojectionsfilter_exprs          r   _dataset_to_declr3   ;   s    v[!9 ; <D &-^^%9%9:%958%9K:$$	{9&8&EFGD ''++H5K ((;x):;)GHI
 K ;s   B8c
                    t        |t        t        j                  f      st	        dt        |             t        |t        t        j                  f      st	        dt        |             i }
t        |t        t        f      s|g}t        |      D ]
  \  }}||
|<    i }t        |t        t        f      s|g}t        |      D ]
  \  }}|||<    |j                  j                  }|j                  j                  }| dk(  s| dk(  rg }nE| dk(  s| dk(  rg }n8| dk(  s| dk(  r|D cg c]	  }||vs| }}n| dk(  r|D cg c]	  }||
vs| }}i }t        |      D ]  \  }}||v s|||<    i }t        |      D ]  \  }}||v s|||<    t        |t        j                        rt        ||	      }nt        d
t        |            }t        |t        j                        rt        ||	      }nt        d
t        |            }|rt        | |||||xs d|xs d      }nt        | |||xs d|xs d      }t        d|||g      }|rL| dk(  rFt        |      }t        |      }t!        |      }g }g }t        ||z         D ]  \  }}|t!        |      k  rq||v rm|j#                  |       |||
|         }|j#                  t%        j&                  dt%        j(                  |      t%        j(                  ||z         g             ||k\  r||v r|r||k  r	||v r||z  }|r||k\  r	||v r||z  }|j#                  |       |j#                  t%        j(                  |              t        dt+        ||            }t        j,                  ||g      }|j/                  |	      }|	t        k(  r|S |	t        j0                  k(  rt        j0                  |      S t	        d      c c}w c c}w )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 outerzright outerr%   table_source )output_suffix_for_leftoutput_suffix_for_righthashjoinoptionsinputsz
full outercoalescer'   Unsupported output type)
isinstancer   dsr   	TypeErrortypetuplelist	enumerater)   r*   r3   r   r	   r   setlenappendr   _call_fieldr   r+   to_tabler!   ) 	join_typeleft_operand	left_keysright_operand
right_keysleft_suffixright_suffixr%   coalesce_keysoutput_typeleft_keys_orderidxkeyright_keys_orderleft_columnsright_columnscolleft_column_keys_indicescolnameright_column_keys_indicesleft_sourceright_source	join_optsr/   left_columns_setright_columns_setright_operand_indexprojected_col_namesr1   right_key_index
projectionresult_tables                                    r   _perform_joinrl   R   sx   P lUBJJ$789$|:L9MNOOmeRZZ%899$}:M9NOPP Oi%/K	i(S" ) j4-0 \
j)S # *  &&,,L!((..M K9#;	l	"i<&?	g	l!:(
(CC7G,GC= 	 
 
m	#'
'C3o+EC< 	 
  "!,/Wi03$W- 0 !#!-0Wj 14%g. 1
 ,

+&|M!.2H2VW-,';O"2=A
 'y*lM#.#4"$0$6B
	 (y*#.#4"$0$6B
	
 I{L.ID l2 |,.!,/ !,">?HCS&&32J+J#**3/
 #<s34#6""$$Z"))#.
0A0A/?1A2  ++7P0P  C*=$=00;&C S,?%?//<'C#**3/""%%c*E @J !)+7JK

 (($
);<==[=9Le	**	*!!,//122U

s   )	O/3O/	O4O4c	                    t        | t        t        j                  f      st	        dt        |              t        |t        t        j                  f      st	        dt        |             t        |t        t        f      s|g}t        |t        t        f      s|g}|j                  j                  D 	cg c]  }	|	|g|z   vr|	 }
}	t        | j                  j                        t        |
      z  }|rt        dj                  |            t        | t        j                        rt        | |d      }nt        dt        |             }t        |t        j                        rt        ||d      }nt        dt        |            }t!        |||||      }t        d|||g      }|j#                  |      }|t        k(  r|S |t        j$                  k(  rt        j$                  |      S t	        d	      c c}	w )
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
    r5   zOColumns {} present in both tables. AsofJoin does not support column collisions.Tr$   r8   asofjoinr=   r7   rA   )rB   r   rC   r   rD   rE   rF   rG   r)   r*   rI   
ValueErrorformatr3   r   r	   r   rN   r!   )rP   left_onleft_byrR   right_onright_by	tolerancer%   rW   r^   r]   columns_collisionsrb   rc   rd   r/   rk   s                    r   _perform_join_asofrw     s   B lUBJJ$789$|:L9MNOOmeRZZ%899$}:M9NOPPgt}-)h.: %++111xj8++ 	1   \00667#m:LL!!'(:!;
 	
 ,

+&#%)+
 "2<@
 -,'{%)+ #2=A
 $(HiI I{L.ID ==[=9Le	**	*!!,//122Ys   9G>c           	      B   d}t        | t              rt        j                  | g      } d}t	        j
                  t	        dt        |             t	        dt        |            g      }|j                  d      }|r!|j                         j                         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
    FTr8   )r>   r(   r7   r   )rB   r   r   from_batchesr   r+   r	   r
   rN   combine_chunks
to_batches)table
expressionis_batchr/   results        r   _filter_tabler   ^  s    " H%%""E7+$$N,B5,IJH&7
&CD& D ]]t],F&&(335a8Mr   c                 v   t        | t        j                        rt        | d      }nt	        dt        |             }t	        dt        |fi |      }t	        j                  ||g      }|j                  d      }|t        k(  r|S |t        j                  k(  rt        j                  |      S t        d      )NTr7   r8   order_byrA   )rB   rC   r   r3   r   r	   r   r+   rN   r   r!   rD   )table_or_dataset	sort_keysrW   kwargsdata_sourcer   r/   rk   s           r   _sort_sourcer   ~  s    "BJJ/&'7TJ!23CD
 :'9)'Nv'NOH$$k8%<=D==T=2Le	**	*!!,//122r   c           
          t        j                  t        dt        |             t        dt        ||            g      }|j	                  |      S )Nr8   	aggregate)keysr7   )r   r+   r	   r   rN   )r|   
aggregatesr   r%   r/   s        r   	_group_byr     sL    $$N$:5$ABK!5jt!LM& D ==[=11r   )TF)T)pyarrow.libr   r   pyarrow.computer   r   pyarrow._aceror   r   r	   r
   r   r   r   r   r   ImportErrorexcstrpyarrow.datasetr.   rC   pyarrow._datasetr   r   r3   rl   rw   r   r   r   r   r   r   <module>r      s   2 + -
 
 
"
 02 #"%#	n3f /3#(X3v@ ;@ 3,2Y  

J3s8*TUV    
Bs(   A A5 A2A--A25B	B	