
    .h;                       d dl mZ d dlZd dlmZ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 d dlmZ  ej(                  e      5  d dlmZ ddd       erd dlmZ d d	lmZ d d
lmZmZmZ ddddd	 	 	 	 	 	 	 	 	 	 	 ddZddZ dd	 	 	 	 	 	 	 ddZ!dd	 	 	 	 	 d dZ"dd	 	 	 	 	 d!dZ#dd	 	 	 	 	 d"dZ$	 	 	 	 d#dZ%d$dZ&dd	 	 	 	 	 d%dZ'	 	 	 	 	 	 d&dZ(d'dZ)d(dZ*y# 1 sw Y   xY w))    )annotationsN)
CollectionIterableMapping)TYPE_CHECKINGAny)	functions)qualified_type_name)ComputeError)Expr)PyExpr)ColumnNameOrSelectorIntoExprPolarsDataTypeF)
str_as_litlist_as_series	structifydtypec                  t        | t        j                        r| }|rt        |      }|j                  S t        | t              r#|s!t        j                  |       }|j                  S t        | t              r8|r6t        j                  t        j                  |       |      }|j                  S t        j                  | |      }|j                  S )a  
    Parse a single input into an expression.

    Parameters
    ----------
    input
        The input to be parsed as an expression.
    str_as_lit
        Interpret string input as a string literal. If set to `False` (default),
        strings are parsed as column names.
    list_as_series
        Interpret list input as a Series literal. If set to `False` (default),
        lists are parsed as list literals.
    structify
        Convert multi-column expressions to a single struct expression.
    dtype
        If the input is expected to resolve to a literal with a known dtype, pass
        this to the `lit` constructor.

    Returns
    -------
    PyExpr
    )r   )
isinstanceplr   _structify_expressionstrFcollistlitSeries_pyexpr)inputr   r   r   r   exprs         V/var/www/html/ai-service/venv/lib/python3.12/site-packages/polars/_utils/parse/expr.pyparse_into_expressionr#      s    > %!(.D << 
E3	
uuU| << 
E4	 ^uuRYYu%U3 << uuU%(<<    c                :   | j                   j                         }|j                   j                         rA	 | j                   j                         }t	        j
                  |      j                  |      } | S | S # t        $ r t	        j
                  |       } Y | S w xY wN)metaundo_aliaseshas_multiple_outputsoutput_namer   structaliasr   )r!   unaliased_expr	expr_names      r"   r   r   C   s    YY++-N//1	=		--/I 88N+11)<DK4K	  	"88D>D K		"s   A8 8BB)__structifyc                `    t        ||       }|rt        ||       }|j                  |       |S )a  
    Parse multiple inputs into a list of expressions.

    Parameters
    ----------
    *inputs
        Inputs to be parsed as expressions, specified as positional arguments.
    **named_inputs
        Additional inputs to be parsed as expressions, specified as keyword arguments.
        The expressions will be renamed to the keyword used.
    __structify
        Convert multi-column expressions to a single struct expression.

    Returns
    -------
    list of PyExpr
    r   )_parse_positional_inputs_parse_named_inputsextend)r/   inputsnamed_inputsexprsnamed_exprss        r"   parse_into_list_of_expressionsr9   O   s0    , %V{CE),+N[!Lr$   Tstrictc               *   t        | t              rdd lm} |j	                  | g|      S t        | t
        j                        r| S t        | t
        j                        r| j                  j                         S dt        |       d}t        |      )Nr   require_allzcannot turn z into selector)r   r   polars.selectors	selectorsby_namer   Selectorr   r'   as_selectorr
   	TypeError)ir;   csmsgs       r"   parse_into_selectorrH   m   sy    
 !S%zz1#6z22	Ar{{	#	Arww	vv!!##036nEnr$   c               Z   t        | t              rt        | t              sdd lm} t        t        d |             }|j                  ||      }t        |      t        |       k(  r|S t        |      dk(  r|j                         }| D ]  }|t        ||      z  } |S t        | |      S )Nr   c                "    t        | t              S r&   )r   r   )rE   s    r"   <lambda>z*parse_list_into_selector.<locals>.<lambda>   s    
1c(: r$   r=   r:   )r   r   r   r?   r@   r   filterrA   lenemptyrH   )r5   r;   rF   columnsselectorrE   s         r"   parse_list_into_selectorrQ      s    
 &*%j.E%v:FCD::g6::w<3v;&O w<1xxzH 	>A+Af==H	>"6&99r$   r1   c               X    t        |       }|D cg c]  }t        ||       c}S c c}w Nr1   )_parse_inputs_as_iterabler#   )r5   r   inputs_iteres       r"   r2   r2      s*    
 ,F3KCNOa!!y9OOOs   'c                    | sg S t        |       dk(  r t        | d   t              rd}t        |      t        |       dk(  rt	        | d         r| d   S | S )N   r   u8  Cannot pass a dictionary as a single positional argument.
If you merely want the *keys*, use:
  • df.method(*your_dict.keys())
If you need the key value pairs, use one of:
  • unpack as keywords:    df.method(**your_dict)
  • build expressions:     df.method(expr.alias(k) for k, expr in your_dict.items()))rM   r   r   rD   _is_iterable)r5   rG   s     r"   rT   rT      sf     	 6{aJvay':e 	 n 6{aL3ayMr$   c                r    t        | t              xr& t        | t        t        t        j
                  f       S r&   )r   r   r   bytesr   r   )r    s    r"   rY   rY      s2    eX& zUBII&0 , r$   c             #  x   K   | j                         D ]#  \  }}t        ||      j                  |       % y wrS   )itemsr#   r,   )r6   r   namer    s       r"   r3   r3      s>      $))+ Le#EY?EEdKKLs   8:c                 j    t        |       }|rt        |      }|j                  |       t        |      S )a  
    Parse predicates and constraints into a single expression.

    The result is an AND-reduction of all inputs.

    Parameters
    ----------
    *predicates
        Predicates to be parsed, specified as positional arguments.
    **constraints
        Constraints to be parsed, specified as keyword arguments.
        These will be converted to predicates of the form "keyword equals input value".

    Returns
    -------
    PyExpr
    )r2   _parse_constraintsr4   _combine_predicates)
predicatesconstraintsall_predicatesconstraint_predicatess       r"   ,parse_predicates_constraints_into_expressionrf      s6    * .j9N 2; ?34~..r$   c              #     K   | j                         D ]5  \  }}t        j                  |      j                  |      j                   7 y wr&   )r]   r   r   eqr   )rc   r^   values      r"   r`   r`      s?     "((* ,eeeDknnU#+++,s   A
Ac                p    | sd}t        |      t        |       dk(  r| d   S t        j                  |       S )Nz5at least one predicate or constraint must be providedrX   r   )rD   rM   plrall_horizontal)rb   rG   s     r"   ra   ra      s;    En
:!!}j))r$   )r    r   r   boolr   rm   r   rm   r   zPolarsDataType | Nonereturnr   )r!   r   rn   r   )r5   IntoExpr | Iterable[IntoExpr]r/   rm   r6   r   rn   list[PyExpr])rE   r   r;   rm   rn   pl.Selector)r5   z7ColumnNameOrSelector | Collection[ColumnNameOrSelector]r;   rm   rn   rq   )r5   z0tuple[IntoExpr, ...] | tuple[Iterable[IntoExpr]]r   rm   rn   rp   )r5   z&tuple[Any, ...] | tuple[Iterable[Any]]rn   zIterable[Any])r    zAny | Iterable[Any]rn   rm   )r6   dict[str, IntoExpr]r   rm   rn   Iterable[PyExpr])rb   ro   rc   r   rn   r   )rc   rr   rn   rs   )rb   rp   rn   r   )+
__future__r   
contextlibcollections.abcr   r   r   typingr   r   polars._reexport	_reexportr   polarsr	   r   polars._utils.variousr
   polars.exceptionsr   suppressImportErrorpolars._plr_plrrk   r   r   polars._typingr   r   r   r#   r   r9   rH   rQ   r2   rT   rY   r3   rf   r`   ra    r$   r"   <module>r      s   "  9 9 %  ! 5 *Z%  "MM  #'++ + 	+
 + !+ +\	 *  	B   	* :C: : 	:8 P<P P 	P22 =BL%L59LL/.// /<,
*} s   C$$C-