fKddlZddlZddlZddlmZddlmZ ddZ ddZ ddZ dZ d Z d Z Gd d Z Gd dZy)N) url_to_fs)merge_offset_rangesc ~|t|fi|xsid}|!t|dk(r|j||St|}t |g|||||| | |  } | rt t | n|}| jdij}|j|f|di|| j|i|dd| S)a Return a file-like object for a single Parquet file. The specified parquet `engine` will be used to parse the footer metadata, and determine the required byte ranges from the file. The target path will then be opened with the "parts" (`KnownPartsOfAFile`) caching strategy. Note that this method is intended for usage with remote file systems, and is unlikely to improve parquet-read performance on local file systems. Parameters ---------- path: str Target file path. mode: str, optional Mode option to be passed through to `fs.open`. Default is "rb". metadata: Any, optional Parquet metadata object. Object type must be supported by the backend parquet engine. For now, only the "fastparquet" engine supports an explicit `ParquetFile` metadata object. If a metadata object is supplied, the remote footer metadata will not need to be transferred into local memory. fs: AbstractFileSystem, optional Filesystem object to use for opening the file. If nothing is specified, an `AbstractFileSystem` object will be inferred. engine : str, default "auto" Parquet engine to use for metadata parsing. Allowed options include "fastparquet", "pyarrow", and "auto". The specified engine must be installed in the current environment. If "auto" is specified, and both engines are installed, "fastparquet" will take precedence over "pyarrow". columns: list, optional List of all column names that may be read from the file. row_groups : list, optional List of all row-groups that may be read from the file. This may be a list of row-group indices (integers), or it may be a list of `RowGroup` metadata objects (if the "fastparquet" engine is used). storage_options : dict, optional Used to generate an `AbstractFileSystem` object if `fs` was not specified. strict : bool, optional Whether the resulting `KnownPartsOfAFile` cache should fetch reads that go beyond a known byte-range boundary. If `False` (the default), any read that ends outside a known part will be zero padded. Note that using `strict=True` may be useful for debugging. max_gap : int, optional Neighboring byte ranges will only be merged when their inter-range gap is <= `max_gap`. Default is 64KB. max_block : int, optional Neighboring byte ranges will only be merged when the size of the aggregated range is <= `max_block`. Default is 256MB. footer_sample_size : int, optional Number of bytes to read from the end of the path to look for the footer metadata. If the sampled bytes do not contain the footer, a second read request will be required, and performance will suffer. Default is 1MB. **kwargs : Optional key-word arguments to pass to `fs.open` r)mode)metadatacolumns row_groupsenginemax_gap max_blockfooter_sample_size cache_optionsparts)datastrict)r cache_typer) rlenopen _set_engine_get_parquet_byte_rangesnextiterpopcopyget)pathrfsrr r storage_optionsrr r r rkwargsrfnoptionss W/var/lib/jenkins/workspace/mettalog/venv/lib/python3.12/site-packages/fsspec/parquet.pyopen_parquet_filer$sb z t 7 52 7 :s7|q0wwt$w'' F $  - D"d4j tBjj"-224G 277     HHR$    c t|tr t|}|t|||||||S|j |} i} g} g} g} d}|v|tt |D]b\}}i| |<t d| ||D]E}| j|| j|| jt||z| |Gdd}ng}g}t |D]<\}}|j| |td| ||z }|j|>|j|||}|j}d}t |D]I\}}tj||ddd}| ||dzz }|||ks6|||<t||dz}K|rPtjd |d t |j|||D]\}}|||z||<||||<t |D]\}}| ||kr?||dkDr6| j|| jd| j||M|j!|||||| \}}| |gt#|zz } | |z } | |z } t%| | | ||d \} } } t |D]\}}||||f||i| |<t'|| | | | |r t)| | S) aGet a dictionary of the known byte ranges needed to read a specific column/row-group selection from a Parquet dataset. Each value in the output dictionary is intended for use as the `data` argument for the `KnownPartsOfAFile` caching strategy of a single path. )r r r r TrFilittlezYNot enough data was used to sample the parquet footer. Try setting footer_sample_size >= .)r footer footer_startr r sort) isinstancestrr&_get_parquet_byte_ranges_from_metadatasizes enumeraterangeappendminmax cat_rangesrint from_byteswarningswarn_parquet_byte_rangesrr_transfer_ranges_add_header_magic)pathsrrr r r r rr file_sizesresult data_paths data_starts data_endsadd_header_magicirb footer_starts footer_ends sample_sizefooter_samplesmissing_footer_starts large_footer footer_sizereal_footer_startblockpath_data_startspath_data_endss r#rrs&&#V$6   !  %JFJKI:- !'GAtF4L1jmY7!!$'""1%  Q]JqM!BC8( !   'GAt   z!} -aA1C!CDK   -(um[I!. 2 2 4  'GAt..):2b)A8LK *1 q A  =#33+<%a("<+/C (  MM55AN!E & )!5%*N1,=$=q!#8#; a !'GAt!}y( #a'%%d+&&q)$$]1%5606/J/J%%a(*1- 0K0 , n 4&3'7#88 8J + +K  'I/(4.A    . * K!'GAt*1-{1~>q@QRF4L(R[)D&! Mr%c|j|||\}}} t||| ||d\}}} tt|D cic]} | i} } t || ||| t | | Scc} w)zSimplified version of `_get_parquet_byte_ranges` for the case that an engine-specific `metadata` object is provided, and the remote footer metadata does not need to be transferred before calculating the required byte ranges. )r rFr-)r=rlistsetr>r?) rrr r r r r rCrDrEr!rBs r#r1r1s *0)D)D*E*&J Y*=  *&J Y $C O4 5b"f 5F 5R[)Df M 6s A,cj|||f}tg||j|D]\}}}} | ||||f<yN)zipr8) rblocksr@startsendsrangesrstartstoprs r#r>r>HsLVT "F#&#G#G  v0F#GeT4&*t eT]#$Hr%ct|jD]<}d}||jD]}|ddk(s |ddk\sd}n|s5d||d<>y)NTrrFsPAR1)rra)rUkeys)rr add_magicks r#r?r?OsaTYY[! d"AtqyQqTQY! # !(DJv "r%c|dk(rd}n0t|ts td|dvrt|d|g}|D]%} |dk(r tcS|dk(r t cS't d|d#t $rYBwxYw) Nauto) fastparquetpyarrowzMFailed to set parquet engine! Please pass 'fastparquet', 'pyarrow', or 'auto'z) engine not supported by `fsspec.parquet`rgrhzLThe following parquet engines are not installed in your python environment: zS.Please install 'fastparquert' or 'pyarrow' to utilize the `fsspec.parquet` module.)r/r0 ValueErrorFastparquetEngine PyarrowEngine ImportError) engine_str try_enginesr s r#rr[sV0  C ( >   5 5J<'PQRR!l  &(**9$$&%   ''2m4/ 0     sA2A22 A>=A>c(eZdZdZdZ ddZy)rjcddl}||_yNr)rgfp)selfrrs r#__init__zFastparquetEngine.__init__s  r%c$|j|SrX)row_group_filename)rs row_grouppfs r#_row_group_filenamez%FastparquetEngine._row_group_filenames$$Y//r%Nc |}ggg} }}|.|jjtj|}|dn t |} | St |drG|j jdgD cgc]} t| ts| } } | t | z} |rt|dtsd} n|} |j}t|D]\}}| || vs |j||}|jD]}|jj d}| || vs#|jj"}||jj$}|jj&}|||kso|j)||j)|| j)t+||z|xs||z|r||| fS|| fScc} w)Npandas_metadata index_columnsr)rr ParquetFileioBytesIOrVhasattrr{rr/dictr9r r3ryr meta_datapath_in_schemadictionary_page_offsetdata_page_offsettotal_compressed_sizer5r6)rsr r rr+r,rxrCrDrE column_setindmd_indexrow_group_indicesrrwr!columnname file_offset0 num_bytess r#r=z&FastparquetEngine._parquet_byte_rangess-/RK :$$RZZ%78B%_T#g,  !gb2C&D--11/2F!#t,H #h- 'J jA< $ !+ J&j1LAy!(A1B,B--i<'//F!++::1=D")TZ-?'-'7'7'N'N '/+1+;+;+L+LL$*$4$4$J$J '/<,3N&--b1'..|<%,, #$09$<$0$N\I5M!"024 {I5 5I%%cs4F>NNNN__name__ __module__ __qualname__rtryr=r%r#rjrj~s  0  C&r%rjc(eZdZdZdZ ddZy)rkcddlm}||_yrq)pyarrow.parquetparquetpq)rsrs r#rtzPyarrowEngine.__init__s $r%ctrX)NotImplementedError)rsrwrs r#ryz!PyarrowEngine._row_group_filenames!!r%Nc| tdgg}}|jjtj|j }|dn t |} | |jj} | j duxrd| j v} | rltj| j djdjdgD cgc]} t| ts| } } | t | z} t|j D]}|||vs |j#|}t|j$D]}|j'|}|j(}|j+dd}| || vs|| vs?|j,}| |j.}|j0}||ksk|j3||j3t5||z|||fScc} w)Nz.metadata input not supported for PyarrowEnginespandasutf8r|r*r)rirr}r~rrrVschemato_arrow_schemajsonloadsdecoderr/rr4num_row_groupsrw num_columnsrrsplitrrrr5r6)rsr r rr+r,rDrEmdrrhas_pandas_metadatarrrrwcrr split_namerrs r#r=z"PyarrowEngine._parquet_byte_rangess  MN N!#RY WW F!3 4 = =%_T#g,  !YY..0Ft+L V__0L # $zz 299&A c/2. &c40 c(m+ r(()A!Q*_LLO y445A&--a0F!00D"&C!3J"*:-%3'-'D'D '/+1+B+BL$*$@$@ ',6'..|<%,, #L9$r?rrjrkrr%r#rs &       y~   Nj  'T+ ) FR&R&jJ&J&r%