
    ɯei8                        d Z ddlmZmZmZmZmZmZ ddlm	c m
c mc mc mZ ddlmZmZ ddlmZmZmZmZ ddlmZ ddlmZmZmZ ddlmZ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(m)Z)  G d d      Z* G d de*      Z+dee,ef   defdZ-dee,ee,   e*f   dededefdZ.dede#de#deeeef   fdZ/de+de#dee%   deeef   fdZ0y) z(Contains table function related classes.    )DictIterableListOptionalTupleUnionN)	Ascending	SortOrder)NamedArgumentsTableFunctionPosArgumentsTableFunctionTableFunctionExpression$TableFunctionPartitionSpecDefinition)ColumnOrName)	publicapi
quote_namevalidate_object_name)Column_to_col_if_str)	ArrayTypeMapType   )SnowflakePlan)SelectStatement)build_expr_from_python_val+build_expr_from_snowpark_column_or_col_namewith_src_positionc                       e Zd ZdZeddddeeee   f   dede	e
j                     ded	ed
dfd       Zd Zeddddde	eeee   f      de	eeee   f      ded
d fd       Zedddeded
d fd       ZeZy)TableFunctionCalla  Represents a table function call.
    A table function call has the function names, positional arguments, named arguments and the partitioning information.

    The constructor of this class is not supposed to be called directly.
    Instead, use :func:`~snowflake.snowpark.function.call_table_function`, which will create an instance of this class.
    Or use :func:`~snowflake.snowpark.function.table_function` to create a ``Callable`` object and call it to create an
    instance of this class.
    NT)_ast	_emit_ast	func_namefunc_argumentsr   r    func_named_argumentsreturnc                    |r|rt        d      || _        || _        || _        || _        d| _        d | _        d | _        d | _        d | _	        || _
        y )Nz8A table function shouldn't have both args and named argsF)
ValueErrornameuser_visible_name	argumentsnamed_arguments_over_partition_by	_order_by_aliases_api_call_sourcer   )selfr!   r   r    r"   r#   s         c/var/www/html/glpi_dashboard/venv/lib/python3.12/site-packages/snowflake/snowpark/table_function.py__init__zTableFunctionCall.__init__,   sl     2WXX"	&/  	
 ! 	 
!15 $	    c                     || _         y N)r/   )r0   api_call_sources     r1   _set_api_call_sourcez&TableFunctionCall._set_api_call_sourceF   s
     /r3   )partition_byorder_byr    r8   r9   c                   t        |t        t        f      r|f}n|t        |      }nd}|r t	        |      dkD  r|D ]  }t        |        d}|r| j                  t        j                         }t        |j                        }|j                  j                  | j                         |t        |t        t        f      r@d|j                  _        t        |j                  j                   j#                         |       nFd|j                  _        |D ]0  }t        |j                  j                   j#                         |       2 |t        |t        t        f      r@d|j$                  _        t        |j$                  j                   j#                         |       nFd|j$                  _        |D ]0  }	t        |j$                  j                   j#                         |	       2 t'        | j(                  g| j*                  d|i| j,                  }
d|
_        t        |t        t        f      r|f}n|t        |      }nd}|r?|D cg c]3  }t        |t              r|j0                  nt        |      j0                  5 c}nd}||
_        t        |t        t        f      r|f}n|t        |      }nd}|r8g }t	        |      dkD  r!|D ]  }|j5                  t        |              ||
_        |
S c c}w )a  Specify the partitioning plan for this table function call when you lateral join this table function.

        When a query does a lateral join on a table function, the query feeds data to the table function row by row.
        Before rows are passed to table functions, the rows can be grouped into partitions. Partitioning has two main benefits:

          - Partitioning allows Snowflake to divide up the workload to improve parallelization and thus performance.
          - Partitioning allows Snowflake to process all rows with a common characteristic as a group. You can return results that are based on all rows in the group, not just on individual rows.

        Refer to `table functions and partitions <https://docs.snowflake.com/en/developer-guide/udf/python/udf-python-tabular-functions.html#processing-partitions>`__ for more information.

        Args:
            partition_by: Specify the partitioning column(s). It tells the table function to partition by these columns.
            order_by: Specify the ``order by`` column(s). It tells the table function to process input rows with this order within a partition.

        Note that if this function is called but both ``partition_by`` and ``order_by`` are ``None``, the table function call will put all input rows into a single partition.
        If this function isn't called at all, the Snowflake database will use implicit partitioning.
        Nr   TFr   )
isinstancestrr   tuplelen_create_order_by_expressionr   protoExprr   table_fn_call_overlhsCopyFromr8   variadicr   argsaddr9   r   r'   r)   r*   r+   _expressionr,   appendr-   )r0   r8   r9   r    order_by_tupleeastexprpartition_clauseorder_clausenew_table_functionpartition_by_tuplepartition_spec
order_specs                 r1   overzTableFunctionCall.overI   s   6 hf.&[N!"8_N!N>"Q&' 3A/23 .**,C$S%;%;<DHHdii('lS&M:15D%%.?))..224l 27D%%.,8 (C --22668:J #hf6-1DMM*?**..0( .3DMM*(0 C MM..224l
 /II

-0
484H4H
 $( lS&M2".%!&|!4!% " , ",Av!6F1I<Q<QQ
  	 ,:(hf.&[N!"8_N!NJ>"Q&' FA%%&A!&DEF+5(!!+s   8K9)r    aliasesc                   d}|rt        j                         }t        |j                        }|j                  j                  | j                         d|j                  _        |D ]0  }t        |j                  j                  j                         |       2 |D cg c]  }t        |       }}t        t        |            t        |      k7  rt        d      || _        || _        | S c c}w )aL  Alias the output columns from the output of this table function call.

        Args:
            aliases: An iterable of unique column names that do not collide with column names after join with the main table.

        Raises:
            ValueError: Raises error when the aliases are not unique after being canonicalized.
        NTz6All output column names after aliasing must be unique.)r@   rA   r   table_fn_call_aliasrC   rD   r   rU   rE   r   rF   rG   r   r>   setr&   r.   )r0   r    rU   rL   rM   argcolcanon_aliasess           r1   aliaszTableFunctionCall.alias   s     **,C$S%<%<=DHHdii($(DLL! I*4<<+<+<+@+@+BCHI 5<<SC<<s=!"c'l2UVV%	 =s   C,)__name__
__module____qualname____doc__r   r   r<   r   r   r   r@   rA   boolr2   r7   rT   r\   as_ r3   r1   r   r   "   s%    
 &*hsm+, & uzz"	
  !- 
 20  OSJNe" u\8L3I%IJKe" 5x/E!EFG	e"
 e" 
e" e"N 59 c d >Q  4 Cr3   r   c                   0     e Zd ZdZdededdf fdZ xZS )_ExplodeFunctionCallz[Internal class to identify explode function call as a special instance of TableFunctionCallrZ   outerr$   Nc                 F    t         |   d||       || _        d| _        y )Nflatten)inputrf   explode)superr2   rZ   r(   )r0   rZ   rf   	__class__s      r1   r2   z_ExplodeFunctionCall.__init__   s&    #U;&/r3   )r]   r^   r_   r`   r   r2   __classcell__)rl   s   @r1   re   re      s%    e0F 06 0d 0 0r3   re   rK   r$   c                 .   t        | t              r't        t        |       j                  t                     S t        | t              rDt        | j                  t              r| j                  S t        | j                  t                     S t        d      )NzDOrder By columns must be of column names in str, or a Column object.)r;   r<   r
   r   rH   r	   	TypeError)rK   s    r1   r?   r?      sj    !S..	<<	Av	ammY/== Q]]IK88R
 	
r3   funcrF   
named_argsc                     d }d }d }d }d }|r|rt        d      t        | t              r| }nt        | t              r$| D ]  }	t	        |	        dj                  |       }nt        | t              rp|s|rt        d      | j                  }| j                  }| j                  }| j                  }| j                  }| j                  }| j                  }| j                  }nt        d      |r/t!        ||D 
cg c]  }
t#        |
d      j$                   c}
      }nBt'        ||j)                         D 
ci c]  \  }}
|t#        |
d      j$                   c}
}      }|rt+        |||      nd |_        ||_        ||_        |S c c}
w c c}
}w )Nz9A table function shouldn't have both args and named args..zN'args' and 'named_args' shouldn't be used if a TableFunction instance is used.z'func' should be a function name in str, a list of strs that have all or a part of the fully qualified name, or a TableFunctionCall instance.ztable function)r&   r;   r<   listr   joinr   r'   r)   r*   r+   r,   r-   r.   r/   ro   r   r   rH   r   itemsr   rR   rU   r6   )rp   rF   rq   rT   r8   r9   rU   r6   fqdnnrY   table_function_expressionarg_names                r1   !_create_table_function_expressionr{      s   
 DLHGO
TUU$	D$	 	$A #	$xx~	D+	,:`  yy~~))
zz))>>--// \
 	
 $=JNO3^C!12>>O%
!
 %@ &0%5%5%7!Hc ..>?KKK%
!  	-T<J ,
 )0%0?-$$# P
s   8F
6!F
	func_exprcurrent_plan	join_planc                    dt         dt        t           fd} ||      } ||      }| j                  }|t	        |      d  }|D cg c]  }t        |      j                          }	}|r'|D cg c]  }t        |      j                          c}ng }
|rmt	        |      t	        |      k7  rt        d| d|       t        ||      D cg c]-  \  }}t        |      j                  |      j                         / }}}n&|D cg c]  }t        |      j                          }}|	||
fS c c}w c c}w c c}}w c c}w )Nplanr$   c                 T    | j                   D cg c]  }|j                   c}S c c}w r5   )outputr'   )r   attrs     r1   get_column_names_from_planz>_get_cols_after_join_table.<locals>.get_column_names_from_plan%  s    &*kk2d		222s   %zxThe number of aliases should be same as the number of cols added by table function. Columns added by table function are z and aliases given are )
r   r   r<   rU   r>   r   _namedr&   zipr\   )r|   r}   r~   r   cols_before_joincols_after_joinrU   new_cols_namesrZ   old_cols
alias_colsr'   
alias_namenew_colss                 r1   _get_cols_after_join_tabler      sP   
3 349 3 2,?0;OG$S)9%:%<=N0@As""$AHA>E':3&+$$&:2J~#g,.77E6FF]^e]fh  %($@
 j 4Lz*113
 

 5CCSF3K&&(CCXz))! B:

 Ds    D-. D22D7 D=r   select_statementc                    |rN|j                  | j                  j                         g      j                  }|j                  d   j
                  }n@| j                  j                  j                  }|j                  j                  |d g      d   }g }| j                  }|r'|D cg c]  }t        |      j                          c}ng }t        |t              r|rct        |      dk7  rt        dt        |             |j!                  t        d      j#                  |d         j                                ||fS |j!                  t        d      j                                ||fS t        |t$              r|rt        |      dk7  rt        dt        |             |j'                  t        d      j)                  |d         j                         t        d      j)                  |d         j                         g       ||fS |j'                  t        d      j                         t        d      j                         g       ||fS t        d| j                   d	|       c c}w )
Nr   r   z>Invalid number of aliases given for explode. Expecting 1, got VALUE   z>Invalid number of aliases given for explode. Expecting 2, got KEYz Invalid column type for explode(z*). Expected ArrayType() or MapType(), got )selectrZ   r   snowflake_planr   datatyperH   r'   output_dictgetr.   r   r;   r   r>   r&   rI   r\   r   extendrb   )	rp   r   r   explode_col_typecol_namecolsrU   rZ   r   s	            r1   _get_cols_after_explode_joinr   A  s9   
 &&(9':;JJ;;q>2288'',,++//4&A!DDmmG>E':3&+$$&:2J"I.7|q  TUXY`UaTbc  KKw--gaj9@@BC* ' KKw..01& % 
$g	.7|q  TUXY`UaTbc  KK5M%%gaj188:7O''
3::<  KK--/1G1G1IJK
  .txxj8bcsbtu
 	
3 ;s   % I+)1r`   typingr   r   r   r   r   r   4snowflake.snowpark._internal.proto.generated.ast_pb2snowpark	_internalr@   	generatedast_pb25snowflake.snowpark._internal.analyzer.sort_expressionr	   r
   4snowflake.snowpark._internal.analyzer.table_functionr   r   r   r   'snowflake.snowpark._internal.type_utilsr   "snowflake.snowpark._internal.utilsr   r   r   snowflake.snowpark.columnr   r   snowflake.snowpark.typesr   r   !_internal.analyzer.snowflake_planr   #_internal.analyzer.select_statementr   _internal.ast.utilsr   r   r   r   re   r<   r?   r{   r   r   rc   r3   r1   <module>r      s,  
 / ? ? D D D V  A 
 = 7 < @ j jZ0, 0
5f#5 
) 
7%
T#Y 11
27%7% 7% 	7%t*&** * 4t	*B*
*
* /* 4:	*r3   