
    >iei                       d dl mZ d dlZd dlmZmZ d dlmZ d dlZd dl	Z
d dlmZ d dlmZ 	 d dlmZ n# e$ r dZY nw xY wd Zd Zdd	Zd
 Zd Zd Z	 	 	 	 ddZe G d de                      Ze G d de                      ZdS )    )annotationsN)Protocolruntime_checkable)uuid4)LocalFileSystem)parsec                N    | o#t          | t                    pt          |           S )z'Check if an fsspec file-system is local)
isinstancer   _is_local_fs_pyarrowfss    7lib/python3.11/site-packages/dask/dataframe/io/utils.py_is_local_fsr      s,     2'' 	$##    c                    | r?t          | d          rt          | j                  S t          | d          r| j        dk    S dS )z-Check if a pyarrow-based file-system is localr   	type_namelocalF)hasattrr   r   r   r   s    r   r   r      sP    	 +2t 	+'...R%% 	+<7**5r   c                   |dk    rddl m} |j        }nd }| j        duod| j        v }|rst	          j        | j        d                             d                    }d |                    d	g           D             }d
 |                    d	g           D             }ni }i }	t          t          |                     D ]}
| |
         }|j	        |v rV|j	        |v r?t          j        g d          j                            ||j	                           j        }n0||j	                 }n"	  ||j                  }n# t           $ r Y w xY w||	|j	        <   |r
|D ]}d|	|<   |	S )z4Convert a pyarrow.Schema object to pandas dtype dictnumpy_nullabler   )PYARROW_NULLABLE_DTYPE_MAPPINGc                *    |                                  S N)to_pandas_dtype)ts    r   <lambda>z%_get_pyarrow_dtypes.<locals>.<lambda>.   s     1 1 3 3 r   Ns   pandasutf8c           	     p    i | ]3}|                     d |                     dd                    |d         4S )
field_namenameN
numpy_typeget.0cs     r   
<dictcomp>z'_get_pyarrow_dtypes.<locals>.<dictcomp>4   sJ     "
 "
 "
 EE,fd 3 344ao"
 "
 "
r   columnsc           	         i | ]Y}|d          dv |d         |                     d|                     dd                    |d                              dd          ZS )pandas_type)datetime
datetimetzmetadatar   r    Ntimezoner"   r$   s     r   r'   z'_get_pyarrow_dtypes.<locals>.<dictcomp>8   sw     
 
 
 #===!J-=	 EE,fd 3 344a
m6G6GD7 7 >==r   zM8[ns]dtypecategory)dask.dataframe.io.parquet.arrowr   r#   r-   jsonloadsdecoderangelenr    pdSeriesdttz_localizer0   typeNotImplementedError)schema
categoriesdtype_backendr   type_mapperhas_pandas_metadatapandas_metadatapandas_metadata_dtypestzdtypesifieldnumpy_dtypecats                 r   _get_pyarrow_dtypesrK   '   s   (((RRRRRR4833 !/5V)v:V $*V_Y%?%F%Fv%N%NOO"
 "
$((B77"
 "
 "

 
 %((B77	
 
 
 "$F3v;; ) )q	 :///zRIb1114@@EJPPV  5UZ@)k%*55&    )uz % 	% 	%C$F3KKMs   )D::
EEc                f   fd| D             fd|pg D             }t          |          dk    rd}nVt          |          dk    r"|d         }|d         dk    r|d         |_        n!t          j                            ||          }t          j        |          }|r||j        _        |S )	aN  Get the final metadata for the dask.dataframe

    Parameters
    ----------
    to_read_columns : list
        All the columns to end up with, including index names
    file_dtypes : dict
        Mapping from column name to dtype for every element
        of ``to_read_columns``
    index_cols : list
        Subset of ``to_read_columns`` that should move to the
        index
    column_index_names : list
        The values for df.columns.name for a MultiIndex in the
        columns, or df.index.name for a regular Index in the columns

    Returns
    -------
    meta : DataFrame
    c           
     f    i | ]-}|t          j        g                     |d                     .S )int64r/   )r8   r9   r#   )r%   r&   file_dtypess     r   r'   z%_meta_from_dtypes.<locals>.<dictcomp>r   sE       @A29R{q'::;;;  r   c                :    g | ]}                     |          S  )pop)r%   r&   datas     r   
<listcomp>z%_meta_from_dtypes.<locals>.<listcomp>u   s#    555qtxx{{555r   r   N   __index_level_0__)names)index)r7   r    r8   
MultiIndexfrom_arrays	DataFramer(   rW   )to_read_columnsrO   
index_colscolumn_index_namesindexesrX   dfrS   s    `     @r   _meta_from_dtypesra   ]   s    *   ET  D 6555J$4"555G
7||q	ZA		
 a=///#AEJ))')DD	d%	(	(	(B .-
Ir   c                 (    t                      j        S )z0Simple utility function to get random hex string)r   hexrQ   r   r   _guidrd      s    77;r   c                4    || S |                     |           S )z5Helper function to place an object on a context stack)enter_context)objstacks     r   _set_contextri      s     }
s###r   c                `   fd| D             S |pi                                  }|                    dd          }|dk    rƉt                    st          t          j                  t          d          k    r                    |                               dd          pdgt          |           z  }                    dd          }|dk    rt          d	| d
          fdt          | |          D             S fd| D             S fd| D             S )a  Return a list of open-file objects given
    a list of input-file paths.

    WARNING: This utility is experimental, and is meant
    for internal ``dask.dataframe`` use only.

    Parameters
    ----------
    paths : list(str)
        Remote or local path of the parquet file
    fs : fsspec object, optional
        File-system instance to use for file handling
    context_stack : contextlib.ExitStack, Optional
        Context manager to use for open files.
    open_file_func : callable, optional
        Callable function to use for file opening. If this argument
        is specified, ``open_file_func(path, **kwargs)`` will be used
        to open each file in ``paths``. Default is ``fs.open``.
    precache_options : dict, optional
        Dictionary of key-word arguments to use for precaching.
        If ``precache_options`` contains ``{"method": "parquet"}``,
        ``fsspec.parquet.open_parquet_file`` will be used for remote
        storage.
    **kwargs :
        Key-word arguments to pass to the appropriate open function
    Nc           	     >    g | ]}t           |fi           S rQ   )ri   )r%   pathcontext_stackkwargsopen_file_funcs     r   rT   z%_open_input_files.<locals>.<listcomp>   sE     
 
 
 7777GG
 
 
r   methodparquetz	2021.11.0
row_groups
cache_typepartsz<'parts' `cache_type` required for 'parquet' precaching, got .c           
     \    g | ](\  }}t          t          j        |f|d           )S ))r   rr   )ri   fsspec_parquetopen_parquet_file)r%   rl   rgsrm   r   rn   s      r   rT   z%_open_input_files.<locals>.<listcomp>   sk     
 
 
 c 0"  	   
 
 
r   c           	     H    g | ]}t           j        |fi           S rQ   ri   open)r%   rl   rm   r   rn   s     r   rT   z%_open_input_files.<locals>.<listcomp>   s7    WWWWRWT44V44mDDWWWr   c           	     F    g | ]}t          t          |fi           S rQ   r{   )r%   rl   rm   rn   s     r   rT   z%_open_input_files.<locals>.<listcomp>   s3    PPP$Ld--f--}==PPPr   )
copyrR   r   parse_versionfsspec__version__updater7   
ValueErrorzip)	pathsr   rm   ro   precache_optionsrn   precacherr   rs   s	    ``` `   r   _open_input_filesr      s   F !
 
 
 
 
 

 
 
 	
 ).B4466##Hd33HINR   &,--k0J0JJJ&'''ZZd33LU8K
ZZg66
  &"& & &  
 
 
 
 
 
 !
33
 
 
 	
 
WWWWWWQVWWWWPPPPP%PPPPr   c                  4    e Zd ZdZed             Zd Zd ZdS )DataFrameIOFunctionzkDataFrame IO function with projectable columns

    Enables column projection in ``DataFrameIOLayer``.
    c                    t           )z$Return the current column projectionr=   selfs    r   r(   zDataFrameIOFunction.columns   s
     "!r   c                    t           )zUReturn a new DataFrameIOFunction object
        with a new column projection
        r   )r   r(   s     r   project_columnsz#DataFrameIOFunction.project_columns   s
     "!r   c                    t           )z Return a new DataFrame partitionr   )r   argsrn   s      r   __call__zDataFrameIOFunction.__call__   s    !!r   N)__name__
__module____qualname____doc__propertyr(   r   r   rQ   r   r   r   r      sW         
 " " X"" " "" " " " "r   r   c                      e Zd ZddZddZdS )SupportsLockreturnobjectc                    d S r   rQ   r   s    r   acquirezSupportsLock.acquire       r   c                    d S r   rQ   r   s    r   releasezSupportsLock.release   r   r   N)r   r   )r   r   r   r   r   rQ   r   r   r   r      s<                r   r   r   )NNNN)
__future__r   r3   typingr   r   uuidr   r   pandasr8   fsspec.implementations.localr   packaging.versionr   r   fsspec.parquetrq   rw   ImportErrorr   r   rK   ra   rd   ri   r   r   r   rQ   r   r   <module>r      s   " " " " " "  . . . . . . . .            8 8 8 8 8 8 4 4 4 4 4 4+++++++   NNN  	 	 	3 3 3 3l' ' 'T  
$ $ $ KQ KQ KQ KQ\ " " " " "( " " ",     8     s   5 ??