
    AHjo                       U d dl mZ d dlmZmZ d dlmZ d dlmZm	Z	m
Z
mZ d dlmZ d dlmZ erQd dlZd dlmZ d d	lmZ  ej*                  e      5  d d
lmZ ddd       d dlmZ d dlmZ d dlmZmZ d dlmZ d dl m!Z!  G d d      Z" G d d      Z# ed       G d d             Z$ ed       G d d             Z% ed       G d d             Z&ee&gdf   Z'de(d<    ed       G d d             Z)	 	 	 	 d!d Z*y# 1 sw Y   xY w)"    )annotations)CallableMapping)	dataclass)TYPE_CHECKINGClassVarLiteral	TypeAlias)parse_into_list_of_expressions)issue_unstable_warningN)Path)	DataFrame)PyExpr)Sequence)IO)StorageOptionsDictSyncOnCloseMethod)Expr)CredentialProviderBuilderc                      e Zd ZU ded<   y)_InternalPlPathProviderConfigzClassVar[str]pl_path_provider_idN)__name__
__module____qualname____annotations__     K/root/tools/cai/cai_env/lib/python3.12/site-packages/polars/io/partition.pyr   r      s    &&r   r   c                  @    e Zd ZdZdddddd	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)PartitionBya  
    Configuration for writing to multiple output files.

    .. warning::
        This functionality is currently considered **unstable**. It may be
        changed at any point without it being considered a breaking change.

    Parameters
    ----------
    base_path
        Base path to write to.
    file_path_provider
        Callable for custom file output paths.
    key
        Expressions to partition by.
    include_key
        Include the partition key expression outputs in the output files.
    max_rows_per_file
        Maximum number of rows to write for each file. Note that files may have
        less than this amount of rows.
    approximate_bytes_per_file
        Approximate number of bytes to write to each file. This is measured as
        the estimated size of the DataFrame in memory.

    Examples
    --------
    Split to multiple files partitioned by year:

    >>> pl.LazyFrame({"year": [2026, 2027, 1970], "month": [0, 0, 0]}).sink_parquet(
    ...     pl.PartitionBy("data/", key="year")
    ... )  # doctest: +SKIP

    Split to multiple files based on size:

    >>> pl.LazyFrame({"year": [2026, 2027, 1970], "month": [0, 0, 0]}).sink_parquet(
    ...     pl.PartitionBy(
    ...         "data/", max_rows_per_file=1000, approximate_bytes_per_file=100_000_000
    ...     )
    ... )  # doctest: +SKIP

    Split to multiple files partitioned by year, with limits on individual file sizes:

    >>> pl.LazyFrame({"year": [2026, 2027, 1970], "month": [0, 0, 0]}).sink_parquet(
    ...     pl.PartitionBy(
    ...         "data/",
    ...         key="year",
    ...         max_rows_per_file=1000,
    ...         approximate_bytes_per_file=100_000_000,
    ...     )
    ... )  # doctest: +SKIP
    Nauto)file_path_providerkeyinclude_keymax_rows_per_fileapproximate_bytes_per_filec                   d}t        |       |||dk(  rd}t        |      ||d}t        |      t        |      }|dk(  r|dnd }|d}t        |||t	        |      nd |||      | _        y )Nz2`PartitionBy` functionality is considered unstabler"   zlat least one of ('key', 'max_rows_per_file', 'approximate_bytes_per_file') must be specified for PartitionByz1cannot use 'include_key' without specifying 'key'l    l    )	base_pathr#   r$   r%   r&   r'   )r   
ValueErrorstr_PartitionByInner_parse_to_pyexpr_list_pl_partition_by)selfr)   r#   r$   r%   r&   r'   msgs           r   __init__zPartitionBy.__init__T   s     Cs# K!)*f44 
 S/!;;2ECS/!	N	%/!2!: ' &-)6& 11.1o%c*4#/'A!
r   )r)   z
str | Pathr#   eCallable[[FileProviderArgs], str | Path | IO[bytes] | IO[str]] | _InternalPlPathProviderConfig | Noner$   z=str | Expr | Sequence[str | Expr] | Mapping[str, Expr] | Noner%   bool | Noner&   
int | Noner'   zint | Literal['auto'] | NonereturnNone)r   r   r   __doc__r1   r   r   r   r!   r!      sd    2x MQ#'(,CI2
2
	2
 K2
 !2
 &2
 %A2
 
2
r   r!   T)kw_onlyc                  &    e Zd ZU dZded<   ded<   y)FileProviderArgsz
    Holds information on the file being sinked to.

    .. warning::
        This functionality is currently considered **unstable**. It may be
        changed at any point without it being considered a breaking change.
    intindex_in_partitionr   partition_keysNr   r   r   r7   r   r   r   r   r:   r:      s     r   r:   c                  N    e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   ded<   y)r,   zG
    Holds parsed partitioned sink options.

    For internal use.
    r+   r)   r2   r#   zlist[PyExpr] | Noner$   r3   r%   r4   r&   r;   r'   Nr>   r   r   r   r,   r,      s2     N	
 
!! ##r   r,   c                      e Zd ZU dZded<   y)SinkedPathsCallbackArgszInformation on sinked paths.z	list[str]pathsNr>   r   r   r   rA   rA      s    &r   rA   r
   SinkedPathsCallbackc                  ^    e Zd ZU dZded<   ded<   dZded<   dZded	<   dZd
ed<   dZded<   y)_SinkOptionsz
    Holds sink options that are generic over file / target type.

    For internal use. Most of the options will parse into `UnifiedSinkArgs`.
    boolmkdirmaintain_orderNzSyncOnCloseMethod | Nonesync_on_closezStorageOptionsDict | Nonestorage_optionsz CredentialProviderBuilder | Nonecredential_providerzSinkedPathsCallback | Nonesinked_paths_callback)	r   r   r   r7   r   rI   rJ   rK   rL   r   r   r   rE   rE      sD     K.2M+2 26O.5<@9@8<5<r   rE   c                    t        | t              r<| j                         D cg c]   \  }}|j                  |      j                  " c}}S t        |       S c c}}w )N)
isinstancer   itemsalias_pyexprr   )exprs_or_columnskes      r   r-   r-      sM     "G,/?/E/E/GHtq!
""HH)*:;; Is   %A)rR   z6str | Expr | Sequence[str | Expr] | Mapping[str, Expr]r5   zlist[PyExpr])+
__future__r   collections.abcr   r   dataclassesr   typingr   r   r	   r
   polars._utils.parse.exprr   polars._utils.unstabler   
contextlibpathlibr   polarsr   suppressImportErrorpolars._plrr   r   r   polars._typingr   r   polars.exprr   ,polars.io.cloud.credential_provider._builderr   r   r!   r:   r,   rA   rC   r   rE   r-   r   r   r   <module>rd      s   " - ! > > C 9 			[	) '&' )D V' 'g
 g
T 4
 
 
 4$ $ $& 4   "*+B*CT*I!J Y J 4= = ="<L<<o' 's   DD