Ë
    Þš§j�6  ã                   óä   — 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
 ddlmZ dd	lmZ erdd
lmZ ddlmZ ddgZdddedede
fd„Zdede
f   dede
f   fd„Z G d„ d«      Z G d„ d«      Zy)é    )ÚCallable)ÚTYPE_CHECKINGÚoverloadé   )ÚContributionsAcceptedErroré   )ÚColumn)Ú	DataFrame)Ú_to_column_expr)ÚNumericType)ÚColumnOrName)ÚSparkSessionÚGroupedDataÚGroupingÚselfÚnameÚcolsÚreturnc                 ó.  — dj                  t        |«      «      }| j                  rt        | j                  «      nd}| j                  j	                  «       }| j
                  j                  j                  ||||¬«      }t        || j                  «      S )NÚ,Ú )Úfunction_nameÚfunction_aggrÚ
group_exprÚprojected_columns)
ÚjoinÚlistÚ	_groupingÚstrÚget_columnsÚ_dfÚrelationÚapplyr
   Úsession)r   r   r   ÚexpressionsÚgroup_byÚprojectionsÚjdfs          úfC:\Users\Luka\AppData\Local\Temp\pip-target-o84fipzb\lib\python\duckdb/experimental/spark/sql/group.pyÚ_api_internalr*   #   sz   € Ø—(‘(œ4 ›:Ó&€KØ&*§n¢nŒs�4—>‘>Ô"¸"€HØ—.‘.×,Ñ,Ó.€KØ
�(‰(×
Ñ
×
!Ñ
!ØØ!ØØ%ð	 "ó €Cô �S˜$Ÿ,™,Ó'Ð'ó    Úf.c                 ót   ‡ — dddt         dt        fˆ fd„}‰ j                  |_        ‰ j                  |_        |S )Nr   r   r   r   c                 ó6   •— ‰j                   }t        | |g|¢­Ž S ©N)Ú__name__r*   )r   r   r   r,   s      €r)   Ú_apizdf_varargs_api.<locals>._api1   s   ø€ Ø�z‰zˆÜ˜T 4Ð/¨$Ò/Ð/r+   )r   r
   r0   Ú__doc__)r,   r1   s   ` r)   Údf_varargs_apir3   0   s:   ø€ ð0�=ð 0¬ð 0´õ 0ð —J‘J€D„MØ—9‘9€D„LØ€Kr+   c                   ó,   — e Zd Zdd„Zdefd„Zdefd„Zy)r   r   Nc                 óŽ   — d| _         |D �cg c]  }t        |«      ‘Œ c}| _        d|v r|d   }ddg}||v sJ ‚|| _         y y c c}w )Nr   ÚspecialÚcubeÚrollup)Ú_typer   Ú_cols)r   r   ÚkwargsÚxr6   Úaccepted_specials         r)   Ú__init__zGrouping.__init__;   sa   € ØˆŒ
Ù26Ó7±$¨Q”o aÕ(°$Ñ7ˆŒ
Ø˜ÑØ˜YÑ'ˆGØ &¨Ð1ÐØÐ.Ñ.Ð.Ð.Ø ˆD�Jð	 ùò 8s   ŒAc                 ót   — dj                  | j                  D �cg c]  }t        |«      ‘Œ c}«      }|S c c}w )Nr   )r   r:   r   )r   r<   Úcolumnss      r)   r    zGrouping.get_columnsD   s1   € Ø—(‘(¨D¯JªJÓ7©J qœC �F¨JÑ7Ó8ˆØˆùò 8s   š5c                 óh   — | j                  «       }| j                  r| j                  dz   |z   dz   S |S )NÚ(Ú))r    r9   )r   r@   s     r)   Ú__str__zGrouping.__str__H   s5   € Ø×"Ñ"Ó$ˆØ�:Š:Ø—:‘: Ñ# gÑ-°Ñ3Ð3Øˆr+   )r   r   r   N)r0   Ú
__module__Ú__qualname__r>   r   r    rD   © r+   r)   r   r   :   s   „ ó!ð˜Só ð˜ô r+   c                   ó  — e Zd ZdZdededdfd„Zdefd„Zdefd„Z	e
d	edefd
„«       Zd	edefd„Ze
d	edefd„«       Ze
d	edefd„«       Ze
d	edefd„«       Zededefd„«       Zedeeef   defd„«       Zdeeeef   z  defd„Zy)r   zjA set of methods for aggregations on a :class:`DataFrame`,
    created by :func:`DataFrame.groupBy`.

    ÚgroupingÚdfr   Nc                 óB   — || _         || _        |j                  | _        y r/   )r   r!   r$   )r   rI   rJ   s      r)   r>   zGroupedData.__init__U   s   € Ø!ˆŒØˆŒØ%'§Z¡Zˆ�r+   c                 ó,   — t        | j                  «      S r/   )r   r!   ©r   s    r)   Ú__repr__zGroupedData.__repr__Z   s   € Ü�4—8‘8‹}Ðr+   c                 ó:   — t        | d«      j                  dd«      S )a‹  Counts the number of records for each group.

        Examples:
        --------
        >>> df = spark.createDataFrame(
        ...     [(2, "Alice"), (3, "Alice"), (5, "Bob"), (10, "Bob")], ["age", "name"]
        ... )
        >>> df.show()
        +---+-----+
        |age| name|
        +---+-----+
        |  2|Alice|
        |  3|Alice|
        |  5|  Bob|
        | 10|  Bob|
        +---+-----+

        Group-by name, and count each group.

        >>> df.groupBy(df.name).count().sort("name").show()
        +-----+-----+
        | name|count|
        +-----+-----+
        |Alice|    2|
        |  Bob|    2|
        +-----+-----+
        Úcountzcount_star())r*   ÚwithColumnRenamedrM   s    r)   rP   zGroupedData.count]   s   € ô8 ˜T 7Ó+×=Ñ=¸nÈgÓVÐVr+   r   c                  ó   — y)zñComputes average values for each numeric columns for each group.

        :func:`mean` is an alias for :func:`avg`.

        Parameters
        ----------
        cols : str
            column names. Non-numeric columns are ignored.
        NrG   ©r   r   s     r)   ÚmeanzGroupedData.mean{   ó   � r+   c                 ó   — t        |«      }t        |«      dk(  rT| j                  j                  }|j                  D �cg c])  }t        |j                  t        «      sŒ|j                  ‘Œ+ }}t        | dg|¢­Ž S c c}w )aì  Computes average values for each numeric columns for each group.

        :func:`mean` is an alias for :func:`avg`.

        Parameters
        ----------
        cols : str
            column names. Non-numeric columns are ignored.

        Examples:
        --------
        >>> df = spark.createDataFrame(
        ...     [(2, "Alice", 80), (3, "Alice", 100), (5, "Bob", 120), (10, "Bob", 140)],
        ...     ["age", "name", "height"],
        ... )
        >>> df.show()
        +---+-----+------+
        |age| name|height|
        +---+-----+------+
        |  2|Alice|    80|
        |  3|Alice|   100|
        |  5|  Bob|   120|
        | 10|  Bob|   140|
        +---+-----+------+

        Group-by name, and calculate the mean of the age in each group.

        >>> df.groupBy("name").avg("age").sort("name").show()
        +-----+--------+
        | name|avg(age)|
        +-----+--------+
        |Alice|     2.5|
        |  Bob|     7.5|
        +-----+--------+

        Calculate the mean of the age and height in all data.

        >>> df.groupBy().avg("age", "height").show()
        +--------+-----------+
        |avg(age)|avg(height)|
        +--------+-----------+
        |     5.0|      110.0|
        +--------+-----------+
        r   Úavg)
r   Úlenr!   ÚschemaÚfieldsÚ
isinstanceÚdataTyper   r   r*   )r   r   r@   rY   r<   s        r)   rW   zGroupedData.avg‡   si   € ôZ �t“*ˆÜˆw‹<˜1ÒØ—X‘X—_‘_ˆFà28·-²-Ó!g±-¨QÄ:ÈaÏjÉjÔZeÕCf !§&£&°-ˆGÐ!gÜ˜T 5Ð3¨7Ò3Ð3ùò "hs   ¾A;ÁA;c                  ó   — y)aA  Computes the max value for each numeric columns for each group.

        Examples:
        --------
        >>> df = spark.createDataFrame(
        ...     [(2, "Alice", 80), (3, "Alice", 100), (5, "Bob", 120), (10, "Bob", 140)],
        ...     ["age", "name", "height"],
        ... )
        >>> df.show()
        +---+-----+------+
        |age| name|height|
        +---+-----+------+
        |  2|Alice|    80|
        |  3|Alice|   100|
        |  5|  Bob|   120|
        | 10|  Bob|   140|
        +---+-----+------+

        Group-by name, and calculate the max of the age in each group.

        >>> df.groupBy("name").max("age").sort("name").show()
        +-----+--------+
        | name|max(age)|
        +-----+--------+
        |Alice|       3|
        |  Bob|      10|
        +-----+--------+

        Calculate the max of the age and height in all data.

        >>> df.groupBy().max("age", "height").show()
        +--------+-----------+
        |max(age)|max(height)|
        +--------+-----------+
        |      10|        140|
        +--------+-----------+
        NrG   rS   s     r)   ÚmaxzGroupedData.max»   rU   r+   c                  ó   — y)aµ  Computes the min value for each numeric column for each group.

        Parameters
        ----------
        cols : str
            column names. Non-numeric columns are ignored.

        Examples:
        --------
        >>> df = spark.createDataFrame(
        ...     [(2, "Alice", 80), (3, "Alice", 100), (5, "Bob", 120), (10, "Bob", 140)],
        ...     ["age", "name", "height"],
        ... )
        >>> df.show()
        +---+-----+------+
        |age| name|height|
        +---+-----+------+
        |  2|Alice|    80|
        |  3|Alice|   100|
        |  5|  Bob|   120|
        | 10|  Bob|   140|
        +---+-----+------+

        Group-by name, and calculate the min of the age in each group.

        >>> df.groupBy("name").min("age").sort("name").show()
        +-----+--------+
        | name|min(age)|
        +-----+--------+
        |Alice|       2|
        |  Bob|       5|
        +-----+--------+

        Calculate the min of the age and height in all data.

        >>> df.groupBy().min("age", "height").show()
        +--------+-----------+
        |min(age)|min(height)|
        +--------+-----------+
        |       2|         80|
        +--------+-----------+
        NrG   rS   s     r)   ÚminzGroupedData.minã   rU   r+   c                  ó   — y)a°  Computes the sum for each numeric columns for each group.

        Parameters
        ----------
        cols : str
            column names. Non-numeric columns are ignored.

        Examples:
        --------
        >>> df = spark.createDataFrame(
        ...     [(2, "Alice", 80), (3, "Alice", 100), (5, "Bob", 120), (10, "Bob", 140)],
        ...     ["age", "name", "height"],
        ... )
        >>> df.show()
        +---+-----+------+
        |age| name|height|
        +---+-----+------+
        |  2|Alice|    80|
        |  3|Alice|   100|
        |  5|  Bob|   120|
        | 10|  Bob|   140|
        +---+-----+------+

        Group-by name, and calculate the sum of the age in each group.

        >>> df.groupBy("name").sum("age").sort("name").show()
        +-----+--------+
        | name|sum(age)|
        +-----+--------+
        |Alice|       5|
        |  Bob|      15|
        +-----+--------+

        Calculate the sum of the age and height in all data.

        >>> df.groupBy().sum("age", "height").show()
        +--------+-----------+
        |sum(age)|sum(height)|
        +--------+-----------+
        |      20|        440|
        +--------+-----------+
        NrG   rS   s     r)   ÚsumzGroupedData.sum  rU   r+   Úexprsc                  ó   — y r/   rG   )r   rc   s     r)   ÚaggzGroupedData.agg=  s   € Ø03r+   Ú_GroupedData__exprsc                  ó   — y r/   rG   )r   rf   s     r)   re   zGroupedData.agg@  s   € Ø9<r+   c                 óÐ  — |sJ d«       ‚t        |«      dk(  rt        |d   t        «      rt        ‚t	        d„ |D «       «      sJ d«       ‚t        | j                  j                  «      }|j                  |D �cg c]  }|j                  ‘Œ c}«       t        | j                  «      } | j                  j                  j                  |d|iŽ}t        || j                  «      S c c}w )aæ  Compute aggregates and returns the result as a :class:`DataFrame`.

        The available aggregate functions can be:

        1. built-in aggregation functions, such as `avg`, `max`, `min`, `sum`, `count`

        2. group aggregate pandas UDFs, created with :func:`pyspark.sql.functions.pandas_udf`

           .. note:: There is no partial aggregation with group aggregate UDFs, i.e.,
               a full shuffle is required. Also, all the data of a group will be loaded into
               memory, so the user should be aware of the potential OOM risk if data is skewed
               and certain groups are too large to fit in memory.

           .. seealso:: :func:`pyspark.sql.functions.pandas_udf`

        If ``exprs`` is a single :class:`dict` mapping from string to string, then the key
        is the column to perform aggregation on, and the value is the aggregate function.

        Alternatively, ``exprs`` can also be a list of aggregate :class:`Column` expressions.

        .. versionadded:: 1.3.0

        .. versionchanged:: 3.4.0
            Supports Spark Connect.

        Parameters
        ----------
        exprs : dict
            a dict mapping from column name (string) to aggregate functions (string),
            or a list of :class:`Column`.

        Notes:
        -----
        Built-in aggregation functions and group aggregate pandas UDFs cannot be mixed
        in a single call to this function.

        Examples:
        --------
        >>> from pyspark.sql import functions as F
        >>> from pyspark.sql.functions import pandas_udf, PandasUDFType
        >>> df = spark.createDataFrame(
        ...     [(2, "Alice"), (3, "Alice"), (5, "Bob"), (10, "Bob")], ["age", "name"]
        ... )
        >>> df.show()
        +---+-----+
        |age| name|
        +---+-----+
        |  2|Alice|
        |  3|Alice|
        |  5|  Bob|
        | 10|  Bob|
        +---+-----+

        Group-by name, and count each group.

        >>> df.groupBy(df.name)
        GroupedData[grouping...: [name...], value: [age: bigint, name: string], type: GroupBy]

        >>> df.groupBy(df.name).agg({"*": "count"}).sort("name").show()
        +-----+--------+
        | name|count(1)|
        +-----+--------+
        |Alice|       2|
        |  Bob|       2|
        +-----+--------+

        Group-by name, and calculate the minimum age.

        >>> df.groupBy(df.name).agg(F.min(df.age)).sort("name").show()
        +-----+--------+
        | name|min(age)|
        +-----+--------+
        |Alice|       2|
        |  Bob|       5|
        +-----+--------+

        Same as above but uses pandas UDF.

        >>> @pandas_udf("int", PandasUDFType.GROUPED_AGG)  # doctest: +SKIP
        ... def min_udf(v):
        ...     return v.min()
        >>> df.groupBy(df.name).agg(min_udf(df.age)).sort("name").show()  # doctest: +SKIP
        +-----+------------+
        | name|min_udf(age)|
        +-----+------------+
        |Alice|           2|
        |  Bob|           5|
        +-----+------------+
        zexprs should not be emptyr   r   c              3   ó<   K  — | ]  }t        |t        «      –— Œ y ­wr/   )r[   r	   )Ú.0Úcs     r)   Ú	<genexpr>z"GroupedData.agg.<locals>.<genexpr>¢  s   è ø€ Ð<±e°”z !¤V×,±eùs   ‚zall exprs should be ColumnÚgroups)rX   r[   Údictr   Úallr   r   r:   ÚextendÚexprr   r!   r"   Úselectr
   r$   )r   rc   r%   r<   r&   Úrels         r)   re   zGroupedData.aggC  sÇ   € ñt Ð1Ð1Ó1ˆuÜˆu‹:˜Š?œz¨%°©(´DÔ9Ü,Ð,ô Ñ<±eÓ<Ô<ÐZÐ>ZÓZÐ<Ü˜tŸ~™~×3Ñ3Ó4ˆKØ×Ñ±Ó6±¨1 §£°Ñ6Ô7Ü˜4Ÿ>™>Ó*ˆHØ*�$—(‘(×#Ñ#×*Ñ*¨KÐIÀÑIˆCÜ˜˜dŸl™lÓ+Ð+ùò  7s   Á8C#)r0   rE   rF   r2   r   r
   r>   r   rN   rP   r3   rT   rW   r^   r`   rb   r   r	   re   rn   rG   r+   r)   r   r   O   s7  „ ñð
0 ð 0¨yð 0¸Tó 0ð
˜#ó ðW�yó Wð< ð	˜#ð 	 )ò 	ó ð	ð24˜ð 24 ó 24ðh ð%˜ð % ò %ó ð%ðN ð*˜ð * ò *ó ð*ðX ð*˜ð * ò *ó ð*ðX Ø3˜&Ð3 YÒ3ó Ø3àØ<˜4  S ™>Ð<¨iÒ<ó Ø<ðd,˜& 4¨¨S¨¡>Ñ1ð d,°iô d,r+   N)Úcollections.abcr   Útypingr   r   Ú	exceptionr   Úcolumnr	   Ú	dataframer
   Ú	functionsr   Útypesr   Ú_typingr   r$   r   Ú__all__r   r*   r3   r   r   rG   r+   r)   Ú<module>r}      s�   ðõ$ %ß *å 2Ý Ý  Ý &Ý ñ Ý%Ý%à˜*Ð
%€ð
(˜ð 
(¨Sð 
(¸ð 
(Àó 
(ð�h˜s I˜~Ñ.ð °8¸CÀ¸NÑ3Kó ÷ñ ÷*X,ò X,r+   