
    hh!                        d dl mZ d dlZd dlmZmZmZ d dlZd dl	m
Z d dlmZmZ d dlmZ d dlmZ d dlmZmZ er,d dlmZmZmZ d d	lmZ d d
lmZ d dlmZm Z m!Z! d dl"m#Z# d dl$m%Z%  G d ded                   Z&dS )    )annotationsN)TYPE_CHECKINGAnyClassVar)cast_to_comparable_string_typesextract_py_scalar)EagerGroupBy)!evaluate_output_names_and_aliases)generate_temporary_column_namerequires)IteratorMappingSequence)ArrowDataFrame)	ArrowExpr)AggregateOptionsAggregation
Incomplete)NarwhalsAggregation)UniqueKeepStrategyc                     e Zd ZU ddddddddd	dd
ddddZded<   ddddZded<    ed          Zded<    ed          Zded<    ed          Z	ded<    ed          Z
ded<    ed          Zded<   d7d'Zd8d-Zd9d1Zd:d3Zd;d5Zd6S )<ArrowGroupBysummeanapproximate_medianmaxminstddevvariancecountcount_distinctallanyfirstlast)r   r   medianr   r   stdvarlenn_uniquer    r"   r#   r$   r%   z3ClassVar[Mapping[NarwhalsAggregation, Aggregation]]_REMAP_AGGS)r#   r$   r%   z2ClassVar[Mapping[UniqueKeepStrategy, Aggregation]]_REMAP_UNIQUE)r)   r*   z(ClassVar[frozenset[NarwhalsAggregation]]_OPTION_COUNT_ALL)r    _OPTION_COUNT_VALID)r$   r%   _OPTION_ORDERED)r'   r(   _OPTION_VARIANCE)r#   r"   _OPTION_SCALARdfr   keys#Sequence[ArrowExpr] | Sequence[str]drop_null_keysboolreturnNonec                 || _         |                     ||          \  }| _        | _        |r|                    | j                  n|| _        t          j        | j        j	        | j                  | _
        || _        d S )N)r3   )_df_parse_keys_keys_output_key_names
drop_nulls_compliant_framepaTableGroupBy	compliantnative_grouped_drop_null_keys)selfr2   r3   r5   frames        O/var/www/histauto/venv/lib/python3.11/site-packages/narwhals/_arrow/group_by.py__init__zArrowGroupBy.__init__<   sz     484D4DRd4D4S4S1tz41@N Y 0 0 < < <TY(=tzJJ-    groupedpa.TableGroupByexprr   <tuple[pa.TableGroupBy, Aggregation, AggregateOptions | None]c                  d }|                      |          }|| j        v r1|j                            dd          }t	          j        |          }n|| j        v rt	          j        d          }n`|| j        v rt	          j        d          }nA|| j	        v rt	          j
        d          }n"|| j        v r|                     ||          \  }}||                     |          |fS )	Nddof   )rP   r"   mode
only_validr   )	min_count)
_leaf_namer0   _scalar_kwargsgetpcVarianceOptionsr-   CountOptionsr.   r1   ScalarAggregateOptionsr/   _ordered_agg_remap_expr_name)rF   rK   rM   optionfunction_namerP   s         rH   _configure_aggzArrowGroupBy._configure_aggJ   s     +/--D111&**6155D'T222FFd444_%000FFd666_,777FFd111.;;;FFd222"//GGOGV--m<<fDDrJ   namer   (tuple[pa.TableGroupBy, AggregateOptions]c                  | j         j        }|dk    r0|j        r)| j         j        }t	          j        ||j        d          }n0|dk     r*d| dt          j        |          d}t          |          |t          j        d          fS )a  The default behavior of `pyarrow` raises when `first` or `last` are used.

        You'd see an error like:

            ArrowNotImplementedError: Using ordered aggregator in multiple threaded execution is not supported

        We need to **disable** multi-threading to use them, but the ability to do so
        wasn't possible before `14.0.0` ([pyarrow-36709])

        [pyarrow-36709]: https://github.com/apache/arrow/issues/36709
        )   r   F)use_threadszUsing `z]()` in a `group_by().agg(...)` context is only available in 'pyarrow>=14.0.0', found version z3.

See https://github.com/apache/arrow/issues/36709)
skip_nulls)rB   _backend_version_use_threadsrC   r@   rA   r3   r   _unparse_versionNotImplementedErrorrY   r\   )rF   rK   rb   backend_versionrC   msgs         rH   r]   zArrowGroupBy._ordered_agg\   s     .9g%%'*>%^*FofglNNNGGw&&D$ D D!)!:?!K!KD D D 
 &c***1UCCCCCrJ   exprsc                   |                      |           g }| j                                        }| j                                        g | j        | j        R }| j        }|D ]*}t          || j        |          \  }}|j        dk    r|j        dk    rd}	t          |	          
                    |d                    |
                    | j        d          d           |
                    | j        d         dt          j        d          f           |                     ||          \  }                    |           |                    fd|D                        |                    fd	|D                        ,|                    |          }
t!          j        t$                    t'          |          D ] \  }}|         
                    |           !t)          |
j                  t)          |          k    r%t-          |
j                  t-          |          k    sd
| d|
j         d}	t          |	          fd|
j        D             }fd|D             |
                              }
| j                            |
                              t5          t7          | j        | j                                      S )Nr   r)   z_Safety assertion failed, please report a bug to https://github.com/narwhals-dev/narwhals/issues_countr    r"   rR   c                    g | ]	}| d  
S )_ ).0output_namer`   s     rH   
<listcomp>z$ArrowGroupBy.agg.<locals>.<listcomp>   s'    RRRkK11-11RRRrJ   c                    g | ]}|fS rs   rs   )rt   ru   r`   r_   s     rH   rv   z$ArrowGroupBy.agg.<locals>.<listcomp>   s     VVV++}f5VVVrJ   z"Safety assertion failed, expected z got zH, please report a bug at https://github.com/narwhals-dev/narwhals/issuesc                F    g | ]}|                              d           S )r   )pop)rt   itemexpected_old_names_indicess     rH   rv   z$ArrowGroupBy.agg.<locals>.<listcomp>   s:      
  
  
8<&t,0033 
  
  
rJ   c                     g | ]
}|         S rs   rs   )rt   inew_column_namess     rH   rv   z$ArrowGroupBy.agg.<locals>.<listcomp>   s    CCCA,Q/CCCrJ   )_ensure_all_simpler<   copyr=   rD   r
   rB   _depth_function_nameAssertionErrorappendrY   r[   ra   extend	aggregatecollectionsdefaultdictlist	enumeratesetcolumn_namesr)   rename_columns_with_nativerenamedictzip)rF   rn   aggsexpected_pyarrow_column_namesexcluderK   rM   output_namesaliasesrm   result_simpleidxrz   	index_mapr{   r`   r~   r_   s                 @@@@rH   aggzArrowGroupBy.aggw   s=   &&&GI37:??3D3D%&*joo&7&78DJ8!788- 	 	D$Edng% %!L' {a&%//{C(--- ''
333-44
15M5M5MNNNTZ]GR_%5P5P5PQRRR-1-@-@$-O-O*G]F##G,,,)00RRRR\RRR   KKVVVVVVVV     ))$// <G;RSW;X;X""#@AA 	9 	9IC&t,33C8888*++s3P/Q/QQQM.//37T3U3UUUY5R Y Y$1Y Y Y 
 !%%% 
  
  
  
@M@Z 
  
  
	 DCCCCCC%445EFF~**=99@@TZ!78899
 
 	
rJ   $Iterator[tuple[Any, ArrowDataFrame]]c           	   #    
K   t          d| j        j                  }d}| j        j        
t	          
fd| j        D             ddi\  }}t          j        } |g ||R d|d}
                    d	||
          
t          j	        |          D ]}| j        
                    
                    t          j        
|         |                                        |g                    } |j        | j                             d	          }	t!          d |	D                        |j        | j        j         fV  d S )N   )n_bytescolumns__null_token_value__c              3  (   K   | ]}|         V  d S Nrs   )rt   keytables     rH   	<genexpr>z(ArrowGroupBy.__iter__.<locals>.<genexpr>   s'      //SeCj//////rJ   	separator replace)null_handlingnull_replacementr   )r}   field_columnc              3  4   K   | ]}t          |          V  d S r   )r   )rt   els     rH   r   z(ArrowGroupBy.__iter__.<locals>.<genexpr>   s+      ::'++::::::rJ   )r   rB   r   rC   r   r<   rY   binary_join_element_wise
add_columnuniquer   filterequaldropsimple_selectrowtupler:   )rF   	col_token
null_tokenitseparator_scalar
concat_str
key_valuesvtr   r   s             @rH   __iter__zArrowGroupBy.__iter__   s     2t~5
 
 
	 1
%>////DJ/// 
;= 
  

 "$!<
Z 

!
 
1:Z
 
 

   1Yz JJ:&& 	 	A++RXeI&6::;;@@)MM A "!/4:.22155C::c:::::!12    	 	rJ   N)r2   r   r3   r4   r5   r6   r7   r8   )rK   rL   rM   r   r7   rN   )rK   rL   rb   r   r7   rc   )rn   r   r7   r   )r7   r   )__name__
__module____qualname__r+   __annotations__r,   	frozensetr-   r.   r/   r0   r1   rI   ra   r]   r   r   rs   rJ   rH   r   r      s        &$H HK    " I IM    
 CL)C C     ENIjDYDYYYYY@I	A AO     BK>AZAZZZZZ?Hy?X?XNXXXX. . . .E E E E$D D D D68
 8
 8
 8
t     rJ   r   )r   r   r   )'
__future__r   r   typingr   r   r   pyarrowr@   pyarrow.computecomputerY   narwhals._arrow.utilsr   r   narwhals._compliantr	   narwhals._expression_parsingr
   narwhals._utilsr   r   collections.abcr   r   r   narwhals._arrow.dataframer   narwhals._arrow.exprr   narwhals._arrow.typingr   r   r   narwhals._compliant.typingr   narwhals.typingr   r   rs   rJ   rH   <module>r      s   " " " " " "     / / / / / / / / / /           T T T T T T T T , , , , , , J J J J J J D D D D D D D D 3;;;;;;;;;;888888......         
 ?>>>>>222222o o o o o< LM o o o o orJ   