
    ˀh7                        d dl mZ d dlmZmZm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 er:d dlZd d	lmZ d d
lmZ d dlmZ d dlmZmZ ej4                  dk\  rd dlmZ nd dlmZ  G d d      Zy)    )annotations)TYPE_CHECKINGLiteraloverload)
deprecated)serialize_polars_object)display_dot_graph)	wrap_expr)ComputeErrorN)IOBase)Path)Expr)
SchemaDictSerializationFormat)      c                  6   e Zd ZdZdZd0dZd1dZd1dZd1dZd1dZ	d2dZ
d2d	Zd2d
Zddd3dZddd3dZeddd4d       Zed5d       Zddd6dZddd7dZd8dZd9dZd9dZd:dZd:dZd:dZd:dZe	 d;dd	 	 	 	 	 d<d        Zed;d=d!       Zedd	 	 	 	 	 d>d"       Z	 d?d#d	 	 	 	 	 d@d$Zed;dAd%       ZedBd&       Z ed'      d?dCd(       Zedd	 	 	 	 	 dDd)       Zedd	 	 	 	 	 dEd*       Zddd+	 	 	 	 	 dFd,Zdddd-dd.	 	 	 	 	 	 	 	 	 	 	 dGd/Zy)HExprMetaNameSpacez*Namespace for expressions on a meta level.metac                &    |j                   | _         y N)_pyexpr)selfexprs     l/var/www/html/wine-match-dev/backend/winematch-backend/venv/lib/python3.12/site-packages/polars/expr/meta.py__init__zExprMetaNameSpace.__init__   s    ||    c                L    | j                   j                  |j                         S r   r   meta_eqr   others     r   __eq__zExprMetaNameSpace.__eq__!   s    ||##EMM22r   c                    | |k(   S r    r!   s     r   __ne__zExprMetaNameSpace.__ne__$   s    5=  r   c                L    | j                   j                  |j                         S )aX  
        Indicate if this expression is the same as another expression.

        Examples
        --------
        >>> foo_bar = pl.col("foo").alias("bar")
        >>> foo = pl.col("foo")
        >>> foo_bar.meta.eq(foo)
        False
        >>> foo_bar2 = pl.col("foo").alias("bar")
        >>> foo_bar.meta.eq(foo_bar2)
        True
        r   r!   s     r   eqzExprMetaNameSpace.eq'   s     ||##EMM22r   c                &    | j                  |       S )a\  
        Indicate if this expression is NOT the same as another expression.

        Examples
        --------
        >>> foo_bar = pl.col("foo").alias("bar")
        >>> foo = pl.col("foo")
        >>> foo_bar.meta.ne(foo)
        True
        >>> foo_bar2 = pl.col("foo").alias("bar")
        >>> foo_bar.meta.ne(foo_bar2)
        False
        )r(   r!   s     r   nezExprMetaNameSpace.ne7   s     775>!!r   c                6    | j                   j                         S )z
        Indicate if this expression expands into multiple expressions.

        Examples
        --------
        >>> e = pl.col(["a", "b"]).name.suffix("_foo")
        >>> e.meta.has_multiple_outputs()
        True
        )r   meta_has_multiple_outputsr   s    r   has_multiple_outputsz&ExprMetaNameSpace.has_multiple_outputsG   s     ||5577r   c                6    | j                   j                         S )aq  
        Indicate if this expression is a basic (non-regex) unaliased column.

        Examples
        --------
        >>> e = pl.col("foo")
        >>> e.meta.is_column()
        True
        >>> e = pl.col("foo") * pl.col("bar")
        >>> e.meta.is_column()
        False
        >>> e = pl.col(r"^col.*\d+$")
        >>> e.meta.is_column()
        False
        )r   meta_is_columnr-   s    r   	is_columnzExprMetaNameSpace.is_columnS   s      ||**,,r   c                6    | j                   j                         S )z
        Indicate if this expression expands to columns that match a regex pattern.

        Examples
        --------
        >>> e = pl.col("^.*$").name.prefix("foo_")
        >>> e.meta.is_regex_projection()
        True
        )r   meta_is_regex_projectionr-   s    r   is_regex_projectionz%ExprMetaNameSpace.is_regex_projectione   s     ||4466r   F)allow_aliasingc               8    | j                   j                  |      S )az  
        Indicate if this expression only selects columns (optionally with aliasing).

        This can include bare columns, columns matched by regex or dtype, selectors
        and exclude ops, and (optionally) column/expression aliasing.

        .. versionadded:: 0.20.30

        Parameters
        ----------
        allow_aliasing
            If False (default), any aliasing is not considered to be column selection.
            Set True to allow for column selection that also includes aliasing.

        Examples
        --------
        >>> import polars.selectors as cs
        >>> e = pl.col("foo")
        >>> e.meta.is_column_selection()
        True
        >>> e = pl.col("foo").alias("bar")
        >>> e.meta.is_column_selection()
        False
        >>> e.meta.is_column_selection(allow_aliasing=True)
        True
        >>> e = pl.col("foo") * pl.col("bar")
        >>> e.meta.is_column_selection()
        False
        >>> e = cs.starts_with("foo")
        >>> e.meta.is_column_selection()
        True
        >>> e = cs.starts_with("foo").exclude("foo!")
        >>> e.meta.is_column_selection()
        True
        )r   meta_is_column_selectionr   r5   s     r   is_column_selectionz%ExprMetaNameSpace.is_column_selectionq   s    H ||44^DDr   c               8    | j                   j                  |      S )a  
        Indicate if this expression is a literal value (optionally aliased).

        .. versionadded:: 1.14

        Parameters
        ----------
        allow_aliasing
            If False (default), only a bare literal will match.
            Set True to also allow for aliased literals.

        Examples
        --------
        >>> from datetime import datetime
        >>> e = pl.lit(123)
        >>> e.meta.is_literal()
        True
        >>> e = pl.lit(987.654321).alias("foo")
        >>> e.meta.is_literal()
        False
        >>> e = pl.lit(datetime.now()).alias("bar")
        >>> e.meta.is_literal(allow_aliasing=True)
        True
        )r   meta_is_literalr8   s     r   
is_literalzExprMetaNameSpace.is_literal   s    2 ||++N;;r   T)raise_if_undeterminedc                    y r   r%   r   r=   s     r   output_namezExprMetaNameSpace.output_name   s    RUr   c                    y r   r%   r?   s     r   r@   zExprMetaNameSpace.output_name   s    SVr   c               \    	 | j                   j                         S # t        $ r |sY y w xY w)aa  
        Get the column name that this expression would produce.

        It may not always be possible to determine the output name as that can depend
        on the schema of the context; in that case this will raise `ComputeError` if
        `raise_if_undetermined` is True (the default), or `None` otherwise.

        Examples
        --------
        >>> e = pl.col("foo") * pl.col("bar")
        >>> e.meta.output_name()
        'foo'
        >>> e_filter = pl.col("foo").filter(pl.col("bar") == 13)
        >>> e_filter.meta.output_name()
        'foo'
        >>> e_sum_over = pl.sum("foo").over("groups")
        >>> e_sum_over.meta.output_name()
        'foo'
        >>> e_sum_slice = pl.sum("foo").slice(pl.len() - 10, pl.col("bar"))
        >>> e_sum_slice.meta.output_name()
        'foo'
        >>> pl.len().meta.output_name()
        'len'
        N)r   meta_output_namer   r?   s     r   r@   zExprMetaNameSpace.output_name   s2    2	<<0022 	(	s    ++N)schemac               p    | j                   j                  |      D cg c]  }t        |       c}S c c}w )a<  
        Pop the latest expression and return the input(s) of the popped expression.

        Returns
        -------
        list of Expr
            A list of expressions which in most cases will have a unit length.
            This is not the case when an expression has multiple inputs.
            For instance in a `fold` expression.

        Examples
        --------
        >>> e = pl.col("foo") + pl.col("bar")
        >>> first = e.meta.pop()[0]
        >>> first.meta == pl.col("bar")
        True
        >>> first.meta == pl.col("foo")
        False
        )r   meta_popr
   )r   rD   es      r   popzExprMetaNameSpace.pop   s+    ( '+ll&;&;F&CD	!DDDs   3c                6    | j                   j                         S )aD  
        Get a list with the root column name.

        Examples
        --------
        >>> e = pl.col("foo") * pl.col("bar")
        >>> e.meta.root_names()
        ['foo', 'bar']
        >>> e_filter = pl.col("foo").filter(pl.col("bar") == 13)
        >>> e_filter.meta.root_names()
        ['foo', 'bar']
        >>> e_sum_over = pl.sum("foo").over("groups")
        >>> e_sum_over.meta.root_names()
        ['foo', 'groups']
        >>> e_sum_slice = pl.sum("foo").slice(pl.len() - 10, pl.col("bar"))
        >>> e_sum_slice.meta.root_names()
        ['foo', 'bar']
        )r   meta_root_namesr-   s    r   
root_nameszExprMetaNameSpace.root_names   s    & ||++--r   c                H    t        | j                  j                               S )aR  
        Undo any renaming operation like `alias` or `name.keep`.

        Examples
        --------
        >>> e = pl.col("foo").alias("bar")
        >>> e.meta.undo_aliases().meta == pl.col("foo")
        True
        >>> e = pl.col("foo").sum().over("bar")
        >>> e.name.keep().meta.undo_aliases().meta == e
        True
        )r
   r   meta_undo_aliasesr-   s    r   undo_aliaseszExprMetaNameSpace.undo_aliases  s     779::r   c                H    t        | j                  j                               S )z#Turn this expression in a selector.)r
   r   _meta_as_selectorr-   s    r   _as_selectorzExprMetaNameSpace._as_selector  s    779::r   c                ^    t        | j                  j                  |j                              S )zAdd ('+') selectors.)r
   r   _meta_selector_addr!   s     r   _selector_addzExprMetaNameSpace._selector_add       88GHHr   c                ^    t        | j                  j                  |j                              S )zAnd ('&') selectors.)r
   r   _meta_selector_andr!   s     r   _selector_andzExprMetaNameSpace._selector_and  rU   r   c                ^    t        | j                  j                  |j                              S )zSubtract ('-') selectors.)r
   r   _meta_selector_subr!   s     r   _selector_subzExprMetaNameSpace._selector_sub  rU   r   c                ^    t        | j                  j                  |j                              S )zXor ('^') selectors.)r
   r   _meta_selector_xorr!   s     r   _selector_xorzExprMetaNameSpace._selector_xor"  rU   r   .formatc                    y r   r%   r   filer`   s      r   	serializezExprMetaNameSpace.serialize&  s     r   c                    y r   r%   rb   s      r   rd   zExprMetaNameSpace.serialize+  s    NQr   c                    y r   r%   rb   s      r   rd   zExprMetaNameSpace.serialize.       r   binaryc                   |dk(  r| j                   j                  }n,|dk(  r| j                   j                  }nd|}t        |      t	        |||      S )u  
        Serialize this expression to a file or string in JSON format.

        Parameters
        ----------
        file
            File path to which the result should be written. If set to `None`
            (default), the output is returned as a string instead.
        format
            The format in which to serialize. Options:

            - `"binary"`: Serialize to binary format (bytes). This is the default.
            - `"json"`: Serialize to JSON format (string).

        See Also
        --------
        Expr.deserialize

        Notes
        -----
        Serialization is not stable across Polars versions: a LazyFrame serialized
        in one Polars version may not be deserializable in another Polars version.

        Examples
        --------
        Serialize the expression into a binary representation.

        >>> expr = pl.col("foo").sum().over("bar")
        >>> bytes = expr.meta.serialize()
        >>> type(bytes)
        <class 'bytes'>

        The bytes can later be deserialized back into an `Expr` object.

        >>> import io
        >>> pl.Expr.deserialize(io.BytesIO(bytes))  # doctest: +ELLIPSIS
        <Expr ['col("foo").sum().over([col("ba…'] at ...>
        rh   jsonz0`format` must be one of {'binary', 'json'}, got )r   serialize_binaryserialize_json
ValueErrorr   )r   rc   r`   
serializermsgs        r   rd   zExprMetaNameSpace.serialize3  sX    X X66Jv44JFvjQCS/!&z4@@r   c                     y r   r%   r   rc   s     r   
write_jsonzExprMetaNameSpace.write_jsoni  s    36r   c                     y r   r%   rq   s     r   rr   zExprMetaNameSpace.write_jsonl  s    =@r   z;`meta.write_json` was renamed; use `meta.serialize` insteadc                (    | j                  |d      S )z
        Write expression to json.

        .. deprecated:: 0.20.11
            This method has been renamed to :meth:`serialize`.
        rj   r_   )rd   rq   s     r   rr   zExprMetaNameSpace.write_jsono  s     ~~d6~22r   c                    y r   r%   r   return_as_stringrD   s      r   tree_formatzExprMetaNameSpace.tree_formaty  rg   r   c                    y r   r%   rv   s      r   rx   zExprMetaNameSpace.tree_format~  s     r   )rw   rD   c               X    | j                   j                  |      }|r|S t        |       y)ar  
        Format the expression as a tree.

        Parameters
        ----------
        return_as_string:
            If True, return as string rather than printing to stdout.

        Examples
        --------
        >>> e = (pl.col("foo") * pl.col("bar")).sum().over(pl.col("ham")) / 2
        >>> e.meta.tree_format(return_as_string=True)  # doctest: +SKIP
        N)r   meta_tree_formatprint)r   rw   rD   ss       r   rx   zExprMetaNameSpace.tree_format  s*      LL))&1H!Hr   )g      0@g      (@)showoutput_path
raw_outputfigsizerD   c               X    | j                   j                  |      }t        |||||      S )a  
        Format the expression as a Graphviz graph.

        Note that Graphviz must be installed to render the visualization (if not
        already present, you can download it here: `<https://graphviz.org/download>`_).

        Parameters
        ----------
        show
            Show the figure.
        output_path
            Write the figure to disk.
        raw_output
            Return dot syntax. This cannot be combined with `show` and/or `output_path`.
        figsize
            Passed to matplotlib if `show == True`.

        Examples
        --------
        >>> e = (pl.col("foo") * pl.col("bar")).sum().over(pl.col("ham")) / 2
        >>> e.meta.show_graph()  # doctest: +SKIP
        )dotr~   r   r   r   )r   meta_show_graphr	   )r   r~   r   r   r   rD   r   s          r   
show_graphzExprMetaNameSpace.show_graph  s4    > ll**62 #!
 	
r   )r   r   returnNone)r"   zExprMetaNameSpace | Exprr   bool)r   r   )r5   r   r   r   )r=   Literal[True]r   str)r=   Literal[False]r   
str | None)r=   r   r   r   )rD   zSchemaDict | Noner   z
list[Expr])r   z	list[str])r   r   )r"   r   r   r   ).)rc   r   r`   zLiteral['binary']r   bytes)rc   r   r`   zLiteral['json']r   r   )rc   IOBase | str | Pathr`   r   r   r   r   )rc   IOBase | str | Path | Noner`   r   r   zbytes | str | None)rc   r   r   r   )rc   r   r   r   )rc   r   r   r   )rw   r   rD   None | SchemaDictr   r   )rw   r   rD   r   r   r   )rw   r   rD   r   r   r   )r~   r   r   zstr | Path | Noner   r   r   ztuple[float, float]rD   r   r   r   )__name__
__module____qualname____doc__	_accessorr   r#   r&   r(   r*   r.   r1   r4   r9   r<   r   r@   rH   rK   rN   rQ   rT   rX   r[   r^   rd   rr   r   rx   r   r%   r   r   r   r      sZ   4I$3!3 " 
8-$
7 =B $EL 49 <6 DHU UV V;? @ 26 E,.*;;IIII ?B+<	  Q QJM'4G	  ,04A '/	4A(4A $	4A
 
4Al 6 6@ @MN3 O3 OS#1;L	  NR#0:K	 
 +0T#'9J	4 )- '3$(&
 &
 '	&

 &
 %&
 "&
 
&
r   r   )
__future__r   typingr   r   r   polars._utils.deprecationr   polars._utils.serder   polars._utils.variousr	   polars._utils.wrapr
   polars.exceptionsr   sysior   pathlibr   polarsr   polars._typingr   r   version_infowarningstyping_extensionsr   r%   r   r   <module>r      sO    " 3 3 0 7 3 ( *>
7"'0g
 g
r   