
    &`iNS                     P   d dl Z d dlZd dlmZmZmZmZmZmZm	Z	m
Z
mZmZmZ d dlZ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lmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z% d d	l&m'Z' erd d
l(m)Z) d dl*m+Z+  ed          Z, ede'          Z- G d de          Z. G d de          Z/dS )    N)TYPE_CHECKINGAnyDictIteratorListMappingOptionalSequenceTupleTypeVarUnion)env_integer)TENSOR_COLUMN_NAME)BlockBuilder)SizeEstimator)NULL_SENTINELfind_partition_indexis_nan
keys_equal)BlockBlockAccessorBlockColumnAccessorBlockExecStatsBlockMetadataWithSchema	BlockTypeKeyTypeU)DEFAULT_TARGET_MAX_BLOCK_SIZE)SortKey)AggregateFnT#RAY_DATA_MAX_UNCOMPACTED_SIZE_BYTESc                   (   e Zd Zd Zdeeeej        f         ddfdZ	de
ddfdZedeeee
         f         defd	            Zed
ee         defd            Zede
fd            Zedefd            ZdefdZdefdZdefdZdefdZddZdS )TableBlockBuilderc                     t          j        t                    | _        g | _        d| _        d| _        t                      | _        d| _	        d| _
        d| _        || _        d S Nr   )collectionsdefaultdictlist_columns_tables_tables_size_cursor_tables_size_bytesr   _uncompacted_size	_num_rows_num_uncompacted_rows_num_compactions_block_type)self
block_types     r/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/data/_internal/table_block.py__init__zTableBlockBuilder.__init__7   s`    #/55"$ $%  #$!.%&" !%    itemreturnNc                    t          |d          r|                                }n#t          |t          j                  r	t
          |i}t          |t          j        j                  s0t          d
                    |t          |                              |D ]}|| j        vrd g| j        z  | j        |<   | j        D ]7}|                    |          }| j        |                             |           8| xj        dz  c_        | xj        dz  c_        |                                  | j                            |           d S )N	as_pydictzLReturned elements of an TableBlock must be of type `dict`, got {} (type {}).   )hasattrr;   
isinstancenpndarrayr   r'   abcr   
ValueErrorformattyper*   r0   getappendr/   _compact_if_neededr.   add)r3   r8   column_namevalues       r5   rH   zTableBlockBuilder.addL   sP   4%% 	.>>##DDbj)) 	.&-D$ 788 	$$*F4d$<$<     	Q 	QK$-//.2Vd6P-Pk*= 	5 	5KHH[))EM+&--e4444!""a'""!!!""4(((((r7   blockc                 $   t          || j                  s*t          dt          |           d| j         d|           t	          j        |          }| j                            |           | xj        |	                                z  c_        d S )NzGot a block of type z, expected z[.If you are mapping a function, ensure it returns an object with the expected type. Block:
)
r>   r2   	TypeErrorrD   r   	for_blockr+   rF   r/   num_rows)r3   rK   accessors      r5   	add_blockzTableBlockBuilder.add_blocke   s    %!122 	tE{{  t?O       !*511E"""(++---r7   columnsc                     t           NNotImplementedError)rR   s    r5   _table_from_pydictz$TableBlockBuilder._table_from_pydictq       !!r7   tablesc                     t           rT   rU   )rY   s    r5   _combine_tablesz!TableBlockBuilder._combine_tablesu   rX   r7   c                      t           rT   rU    r7   r5   _empty_tablezTableBlockBuilder._empty_tabley   rX   r7   c                      t           rT   rU   r]   r7   r5   _concat_would_copyz$TableBlockBuilder._concat_would_copy}   rX   r7   c                 l    | j         rdS |                                 ot          | j                  dk    S )NTr<   )r*   r`   lenr+   r3   s    r5   will_build_yield_copyz'TableBlockBuilder.will_build_yield_copy   s7    = 	4&&((BS->->-BBr7   c                     | j         r|                     | j                   g}ng }|                    | j                   t	          |          dk    r|                                 S |                     |          S r&   )r*   rW   extendr+   rb   r^   r[   )r3   rY   s     r5   buildzTableBlockBuilder.build   sv    = 	--dm<<=FFFdl###v;;!$$&&&''///r7   c                     | j         S rT   )r/   rc   s    r5   rO   zTableBlockBuilder.num_rows   s
    ~r7   c                 &   | j         dk    rdS | j        | j        d          D ]6}| xj        t	          j        |                                          z  c_        7t          | j                  | _        | j        | j                                        z   S r&   )	r/   r+   r,   r-   r   rN   
size_bytesrb   r.   r3   tables     r5   get_estimated_memory_usagez,TableBlockBuilder.get_estimated_memory_usage   s    >Q1\$":"<"<= 	S 	SE##}'>u'E'E'P'P'R'RR####&t|#4#4 &)?)J)J)L)LLLr7   c                 D   | j         sJ | j                                        t          k     rd S |                     | j                   }|                     |           t                      | _        | j                                          | xj        dz  c_        d| _	        d S )Nr<   r   )
r*   r.   rj   MAX_UNCOMPACTED_SIZE_BYTESrW   rQ   r   clearr1   r0   )r3   rK   s     r5   rG   z$TableBlockBuilder._compact_if_needed   s    }}!,,..1KKKF''66u!."%&"""r7   )r9   N)__name__
__module____qualname__r6   r   dictr   r?   r@   rH   r   rQ   staticmethodr   strr   r   rW   r[   r^   boolr`   rd   rg   intrO   rm   rG   r]   r7   r5   r$   r$   6   s       & & &*)dGRZ78 )T ) ) ) )2
.s 
.t 
. 
. 
. 
. "Dd3i$8 "U " " " \" "U " " " " \" "# " " " \" " " " " \"Ct C C C C0u 0 0 0 0#    MC M M M M	' 	' 	' 	' 	' 	'r7   r$   c                      e Zd ZdefdZed             Zededede	j
        fd            ZdefdZdee         fd	Zd
ededefdZdefdZdeddfdZd7dZedefd            ZdedddefdZdedddefdZd8dededee         fdZdededee         fdZdededee         fdZdededee         fdZdededee         fd Z 	 d9deded"ee         dee         fd#Z!d$efd%Z"ddd&e#d'         defd(Z$e%	 d:d*ee         ddd&e#d'         d+ede#ed,f         f
d-            Z&d.ee#e                  ddfd/Z'e%	 d9d*ee         d0ee(         dee         fd1            Z)e%d2ed3e(fd4            Z*d5edefd6Z+d!S );TableBlockAccessorrl   c                     || _         d S rT   _tablerk   s     r5   r6   zTableBlockAccessor.__init__   s    r7   c                     |  d|dz    S )N_r<   r]   )namecounts     r5   _munge_conflictz"TableBlockAccessor._munge_conflict   s    $$$$$r7   rowrow_idxr9   c                     t           rT   rU   )r   r   s     r5   _build_tensor_rowz$TableBlockAccessor._build_tensor_row   rX   r7   c                 .    |                                  }|S rT   )	to_pandas)r3   defaults     r5   
to_defaultzTableBlockAccessor.to_default   s     ..""r7   c                     t           rT   rU   rc   s    r5   column_nameszTableBlockAccessor.column_names       !!r7   r   rJ   c                     t           rT   rU   )r3   r   rJ   s      r5   fill_columnzTableBlockAccessor.fill_column   r   r7   c                     | j         S rT   r|   rc   s    r5   to_blockzTableBlockAccessor.to_block   s
    {r7   accr   c                     t           rT   rU   )r3   r   s     r5   _zipzTableBlockAccessor._zip   r   r7   otherc                    t          j        |          }t          |t          |                     st          | t                    r`t          |t                    rKt                              | j        |g          \  }}t          j        |                              |          S t          d	                    t          |           t          |                              |
                                | 
                                k    rGt          d	                    | 
                                |
                                                    |                     |          S )Nz#Cannot zip {} with block of type {}z3Cannot zip self (length {}) with block of length {})r   rN   r>   rD   rz   normalize_block_typesr}   ziprB   rC   rO   r   )r3   r   r   	self_norm
other_norms        r5   r   zTableBlockAccessor.zip   s:   %e,,#tDzz** 	$ 233 
'9 9 
 );(P(P[%() )%	: %.y99==jIII 9@@T

DKK   
 <<>>T]]__,,ELLMMOOS\\^^   
 yy~~r7   c                      t           rT   rU   r]   r7   r5   r^   zTableBlockAccessor._empty_table   rX   r7   	n_samplessort_keyr   c                     t           rT   rU   )r3   r   r   s      r5   _samplezTableBlockAccessor._sample   r   r7   c                    |t          |          rt          d|           |                                 dk    r|                                 S t	          ||                                           }|                     ||          S )Nz+Table sort key must be a column name, was: r   )callablerV   rO   r^   minr   )r3   r   r   ks       r5   samplezTableBlockAccessor.sample   s    x11%HhHH   ==??a $$&&&	4==??++||Ax(((r7   Fonignore_nullsc                 l    t          j        | j        |                   }|                    |          S N)r   )r   
for_columnr}   r   r3   r   r   rP   s       r5   r   zTableBlockAccessor.count   s,    &1$+b/BB~~<~888r7   c                     |                      |           t          j        | j        |                   }|                    |          S r   )_validate_columnr   r   r}   sumr   s       r5   r   zTableBlockAccessor.sum   @    b!!!&1$+b/BB|||666r7   c                     |                      |           t          j        | j        |                   }|                    |          S r   )r   r   r   r}   r   r   s       r5   r   zTableBlockAccessor.min  r   r7   c                     |                      |           t          j        | j        |                   }|                    |          S r   )r   r   r   r}   maxr   s       r5   r   zTableBlockAccessor.max  r   r7   c                     |                      |           t          j        | j        |                   }|                    |          S r   )r   r   r   r}   meanr   s       r5   r   zTableBlockAccessor.mean  s@    b!!!&1$+b/BB}},}777r7   Nr   c                     |                      |           t          j        | j        |                   }|                    |          S r   )r   r   r   r}   sum_of_squared_diffs_from_mean)r3   r   r   r   rP   s        r5   r   z1TableBlockAccessor.sum_of_squared_diffs_from_mean  sE     	b!!!&1$+b/BB66L6QQQr7   colc                     |t          d| d          ||                                 vr't          d| d|                                            d S )Nz-Provided `on` value has to be non-null (got 'z')zReferencing column 'z' not present in the schema: )rB   r   schema)r3   r   s     r5   r   z#TableBlockAccessor._validate_column  sm    ;TSTTTUUU))++++XsXXXX   ,+r7   aggsr    c                     |                                 dt          t          t          t                   t
          f                  f fd}                                 } |            D ]H\  }}|t          |          dk    r|d         fd|D             }t          t          |                    D ]S}t          j
        |          }	|	                                dk    r%||                             ||         |          ||<   Ti }
rt          |          D ]
\  }}||
|<   t          j        t                     }t          ||          D ]I\  }}|j        }||         dk    r                     |||                   }||xx         dz  cc<   ||
|<   J|                    |
           J|                                S )aY  Applies provided aggregations to groups of rows with the same key.

        This assumes the block is already sorted by key in ascending order.

        Args:
            sort_key: A column name or list of column names.
               If this is ``None``, place all rows in a single group.

            aggs: The aggregations to do.

        Returns:
            A sorted block of [k, v_1, ..., v_n] columns where k is the groupby
            key and v_i is the partially combined accumulator for the ith given
            aggregation.
            If key is None then the k column is omitted.
        r9   c               3     K   s&t                                                      fV  dS dx} }                    d          }d}	 	 |t          |          }|         }t	          |         |          r>|dz  }	 t          |          }n# t
          $ r d}Y nw xY wt	          |         |          >|                    | |          fV  |} n# t
          $ r Y dS w xY w)z/Creates an iterator over zero-copy group views.Nr   Fpublic_row_formatTr<   )tupler   	iter_rowsnextr   StopIterationslice)startenditernext_row	next_keyskeysr3   s        r5   iter_groupsz2TableBlockAccessor._aggregate.<locals>.iter_groups9  s3      ggt}}....OEC>>E>::DH'#':: (I$Xd^Y?? "q"'+DzzHH, " " "'+H!E"	 %Xd^Y?? " $TZZs%;%;;;;;EE$   EEs6   
4C ?B C BC B5C 
C"!C"r<   r   c                 :    g | ]}|                               S r]   )init).0agg	init_valss     r5   
<listcomp>z1TableBlockAccessor._aggregate.<locals>.<listcomp>[  s%    @@@CCHHY//@@@r7   )get_columnsr   r   r
   r   r   builderrb   ranger   rN   rO   accumulate_blockr   r'   r(   rx   r   r   rH   rg   )r3   r   r   r   r   
group_keys
group_viewaccumulatorsirP   r   r   gkr   r   accumulatorr   r   r   s   `                @@r5   
_aggregatezTableBlockAccessor._aggregate&  s   " #..00	XeHW,=u,D&EF 	 	 	 	 	 	 	6 ,,..&1kmm 	 	"J
"I:!##&qM	@@@@4@@@L3t99%%  (2:>>$$&&**&*1g&>&>$Q' 'LO
 C   z22    EArCFF+C00E$'l$;$; ( ( [x;??//eDkBBDdq 'D		KK}}r7   Tblocksfinalizer   c                 6   t                               |          }t          j                    }|                                fdfd}t          j        d |D             d|idt          j        |d                                                   }	 	 t                               }fd}	d}
dgt          |          z  }dgt          |          z  } |	            D ]}|
rt          j        t                    }t          t          |                    D ]\}||         j        }||         dk    r!t                               |||                   }||xx         d	z  cc<   |||<   ||         ||<   ]d
}
t          t          |                    D ]3}||                             ||         |||                            ||<   4i }rt#          |          D ]
\  }}|||<   t#          |||          D ]&\  }}}|r|                    |          ||<   !|||<   '|                    |           n# t(          $ r Y nw xY w|                                }|t-          j        ||                                          fS )aX  Combine previously aggregated blocks.

        This assumes blocks are already sorted by key in ascending order,
        so we can do merge sort to get all the rows with the same key.

        Args:
            blocks: A list of partially combined and sorted blocks.
            sort_key: The column name of key or None for global aggregation.
            aggs: The aggregations to do.
            finalize: Whether to finalize the aggregation. This is used as an
                optimization for cases where we repeatedly combine partially
                aggregated groups.

        Returns:
            A block of [k, v_1, ..., v_n] columns and its metadata where k is
            the groupby key and v_i is the corresponding aggregation result for
            the ith given aggregation.
            If key is None then the k column is omitted.
        c                 6    rt          |                    S dS )N)r   r   )rr   s    r5   _key_fnz>TableBlockAccessor._combine_aggregated_blocks.<locals>._key_fn  s      QtW~~%tr7   c                 L     |           }t          d |D                       S )Nc                 B    g | ]}|t          |          rt          n|S rT   )r   r   )r   vs     r5   r   zVTableBlockAccessor._combine_aggregated_blocks.<locals>.safe_key_fn.<locals>.<listcomp>  s*    PPPA!)vayy)aPPPr7   r   )r   valuesr   s     r5   safe_key_fnzBTableBlockAccessor._combine_aggregated_blocks.<locals>.safe_key_fn  s3    WQZZFPPPPP  r7   c                 ^    g | ]*}t          j        |                              d           +S )Fr   )r   rN   r   )r   rK   s     r5   r   zATableBlockAccessor._combine_aggregated_blocks.<locals>.<listcomp>  sD        '..8858QQ  r7   keyNr   Tc               3      K   t                                rCV  	 t                    n# t          $ r d Y d S w xY wt                                Ad S d S rT   )r   r   r   )r   r   r   r   s   r5   genz:TableBlockAccessor._combine_aggregated_blocks.<locals>.gen  s       %WWX%6%6	BB "&"'+DzzHH, " " "'+H!EE"	 %WWX%6%6	BB " " " " "s   2 AAr<   F)stats)rz   r   r   r   r   heapqmerger   rN   r   rb   r'   r(   rx   r   r   r   r   r   rH   r   rg   r   
from_block)clsr   r   r   r   r   r   r   next_key_columnsr   firstr   resolved_agg_namesr   r   r   r   r   col_namenext_keyr   agg_namer   retr   r   r   r   r   s                           @@@@@r5   _combine_aggregated_blocksz-TableBlockAccessor._combine_aggregated_blocksw  s[   : $99&AA&((##%%	 	 	 	 		 	 	 	 	 { #  

 
 
 )&)44<<>>;	:##DzzH#GH--	#' 	" 	" 	" 	" 	" 	" 	" 	"  $vD		1&*Vc$ii%7"  A  + 7 < <!&s4yy!1!1 
6 
6A#'7<D  %T{Q'9'I'I$(%+(" (" "$KKK1,KKK48.q1./gLOO %!&s4yy!1!1  A.21gmm ,Q3Ea3H1I/ /LOO
  1.12BI.N.N 1 1*((0H25,l3 3 4 4.C;   4(+[(A(AH(3HC        u;	z mmoo+6s%++--PPPPPs   F.I 
II
boundariesc                      g } fd|D             }d}|D ]&}|                      j        ||                    |}'|                      j        |d                     |S )Nc                 <    g | ]}t          j        |          S r]   )r   r}   )r   boundaryr3   r   s     r5   r   z>TableBlockAccessor._find_partitions_sorted.<locals>.<listcomp>  s7     
 
 
 !hAA
 
 
r7   r   )rF   r}   )r3   r   r   
partitionsboundslast_idxidxs   ` `    r5   _find_partitions_sortedz*TableBlockAccessor._find_partitions_sorted  s    
 

 
 
 
 
&
 
 

  	 	Cdk(3,7888HH$+hii0111r7   target_block_typec                     t          j        t                    }|D ]m}t          j        |          }t          |t                    s t          dt          |           d          ||	                                xx         dz  cc<   nt          |          dk    r*&gt          |                                          k    r|S .t          |                                d d          d         \  } fd	|D             t          fd
D                       rt          dd D              d          S )a  Normalize input blocks to the specified `normalize_type`. If the blocks
        are already all of the same type, returns original blocks.

        Args:
            blocks: A list of TableBlocks to be normalized.
            target_block_type: The type to normalize the blocks to. If None,
               Ray Data chooses a type to minimize the amount of data conversions.

        Returns:
            A list of blocks of the same type.
        zWBlock type normalization is only supported for TableBlock, but received block of type: .r<   Nc                     | d         S )Nr<   r]   )xs    r5   <lambda>z:TableBlockAccessor.normalize_block_types.<locals>.<lambda>2  s
    ad r7   T)r   reverser   c                 <    g | ]}                     |          S r]   )try_convert_block_type)r   rK   r   r  s     r5   r   z<TableBlockAccessor.normalize_block_types.<locals>.<listcomp>6  s7     
 
 
EJC&&u.?@@
 
 
r7   c              3   `   K   | ](}t          |t          d                               V  )dS )r   N)r>   rD   )r   rK   resultss     r5   	<genexpr>z;TableBlockAccessor.normalize_block_types.<locals>.<genexpr>:  s<      LL5:eT'!*%5%5666LLLLLLr7   zYExpected all blocks to be of the same type after normalization, but got different types: c                 ,    g | ]}t          |          S r]   )rD   )r   bs     r5   r   z<TableBlockAccessor.normalize_block_types.<locals>.<listcomp>=  s    (B(B(BQa(B(B(Br7   zP. Try using blocks of the same type to avoid the issue with block normalization.)r'   r(   rx   r   rN   r>   rz   rB   rD   r4   rb   r)   r   sorteditemsany)r   r   r  
seen_typesrK   block_accessorr   r  s   ` `    @r5   r   z(TableBlockAccessor.normalize_block_types  s   " ,7+B3+G+G
 	9 	9E*4U;;Nn.@AA  B37;;B B B  
 ~0022333q83333 z??a%*;)<Z__EVEV@W@W)W)WM $#)  """N$ $ $ 	$ A 
 
 
 
 
NT
 
 
 LLLLGLLLLL 	,(B(B'(B(B(B, , ,   r7   rK   r4   c                 &   |t           j        k    r&t          j        |                                          S |t           j        k    r&t          j        |                                          S t          j        |                                          S rT   )r   ARROWr   rN   to_arrowPANDASr   r   )r   rK   r4   s      r5   r	  z)TableBlockAccessor.try_convert_block_typeC  st    (( *511::<<<9+++ *511;;=== *511<<>>>r7   other_blockc                     t           )a  Combine this table with another table horizontally (column-wise).
        This will append the columns.

        Args:
            other_block: The table to hstack side-by-side with.

        Returns:
            A new table with columns from both tables combined.
        rU   )r3   r  s     r5   hstackzTableBlockAccessor.hstackL  s
     "!r7   )r   r   r9   r   )FrT   )T),rq   rr   rs   r   r6   ru   r   r   rx   r?   r@   r   r   r   r   rv   r   r   r   r   r   r   r^   r   r   rw   r	   r   r   r   r   r   r   r   r   r   r   classmethodr   r   r   r   r	  r  r]   r7   r5   rz   rz      s       c     % % \% "w " " " " " \"E    "d3i " " " "" "C "E " " " "%    " "' " " " "   4 "# " " " \"" "	 "c " " " "
) 
)y 
)S 
) 
) 
) 
)9 9 94 9HQK 9 9 9 97c 7 7(1+ 7 7 7 77c 7 7(1+ 7 7 7 77c 7 7(1+ 7 7 7 78s 8$ 88A; 8 8 8 8 !		R 	R	R 	R qk		R
 
!	R 	R 	R 	RC    O9 OE-4H OU O O O Ob  xQ xQUxQ xQ M"	xQ
 xQ 
u//	0xQ xQ xQ [xQts$    2  265 5U5 $I.5 
e	5 5 5 [5n ?5 ?i ? ? ? [?
"% 
"E 
" 
" 
" 
" 
" 
"r7   rz   )0r'   r   typingr   r   r   r   r   r   r	   r
   r   r   r   numpyr?   ray._private.ray_constantsr   ray.air.constantsr    ray.data._internal.block_builderr   !ray.data._internal.size_estimatorr   ray.data._internal.utilr   r   r   r   ray.data.blockr   r   r   r   r   r   r   r   ray.data.contextr   2ray.data._internal.planner.exchange.sort_task_specr   ray.data.aggregater    r!   ro   r$   rz   r]   r7   r5   <module>r'     sj                                     2 2 2 2 2 2 0 0 0 0 0 0 9 9 9 9 9 9 ; ; ; ; ; ;           	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ; : : : : : /JJJJJJ......GCLL )[)+H  
r' r' r' r' r' r' r' r'jk" k" k" k" k" k" k" k" k" k"r7   