o
    hK                     @   s   d dl Z d dlZd dlZddlmZ ddlmZ 												
dddZ							
	dddZ					dddZ	dd Z
dd Zdd ZG dd dZG dd dZdS )    N   )	url_to_fs)merge_offset_rangesrbFauto    @B@B c                 K   s   |du rt | fi |pi d }|dur!t|dkr!|j| |dS t|}t| g||||||	|
|d	}|r;tt|n| }|di  }|j|f|di ||	|i |dd|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`
    Nr   )mode)metadatacolumns
row_groupsenginemax_gap	max_blockfooter_sample_sizecache_optionsparts)datastrict)r
   
cache_typer   )
r   lenopen_set_engine_get_parquet_byte_rangesnextiterpopcopyget)pathr
   fsr   r   r   storage_optionsr   r   r   r   r   kwargsr   fnoptions r&   b/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/fsspec/parquet.pyopen_parquet_file   s@   Q
	r(   c	              	   C   s  t |tr	t|}|durt|||||||dS || }	i }
g }g }g }d}|du r`|du r`t| D ])\}}i |
|< td|	| |D ]}|| || |t|| |	|  qCq3d}ng }g }t| D ]\}}||	|  t	d|	| | }|| qh|
| ||}| }d}t| D ])\}}t|| dd d}|	| |d	  }||| k r|||< t	||d	 }q|rtd
| d t|
| ||D ]\}}|||  ||< || ||< qt| D ]D\}}|	| |k r|| dkr|| |d |||  q|j|||| || d\}}||gt| 7 }||7 }||7 }qt|||||dd\}}}t| D ]\}}|| || f|| i|
|< q>t||
||| |rat|
 |
S )a  Get 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.
    N)r   r   r   r   Tr   Filittle   zYNot enough data was used to sample the parquet footer. Try setting footer_sample_size >= .)r   footerfooter_startr   r   sort)
isinstancestrr   &_get_parquet_byte_ranges_from_metadatasizes	enumeraterangeappendminmax
cat_rangesr   int
from_byteswarningswarn_parquet_byte_rangesr   r   _transfer_ranges_add_header_magic)pathsr!   r   r   r   r   r   r   r   
file_sizesresult
data_pathsdata_starts	data_endsadd_header_magicir    bfooter_startsfooter_endssample_sizefooter_samplesmissing_footer_startslarge_footerfooter_sizereal_footer_startblockpath_data_startspath_data_endsr&   r&   r'   r      s   








 r   c                 C   sb   |j ||| d\}}}	t|||	||dd\}}}	dd tt|D }
t||
|||	 t|
 |
S )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   r   Fr/   c                 S   s   i | ]}|i qS r&   r&   ).0r$   r&   r&   r'   
<dictcomp>?  s    z:_get_parquet_byte_ranges_from_metadata.<locals>.<dictcomp>)r?   r   listsetr@   rA   )r   r!   r   r   r   r   r   rE   rF   rG   rD   r&   r&   r'   r3     s"   
r3   c           
      C   sD   |||f}t g || j| R  D ]\}}}}	|	|| ||f< qd S N)zipr:   )
r!   blocksrB   startsendsrangesr    startstopr   r&   r&   r'   r@   H  s   
$r@   c                 C   sX   t |  D ]#}d}| | D ]}|d dkr |d dkr d} nq|r)d| | d< qd S )NTr   r      Fs   PAR1)r   rb   )rX   keys)r   r    	add_magickr&   r&   r'   rA   O  s   rA   c              	   C   s   | dkrd}nt | tstd| dvrt|  d| g}|D ]!}z|dkr-t W   S |dkr7t W   S W q  tyA   Y q w td| d)	Nr   )fastparquetpyarrowzMFailed to set parquet engine! Please pass 'fastparquet', 'pyarrow', or 'auto'z) engine not supported by `fsspec.parquet`rf   rg   zLThe following parquet engines are not installed in your python environment: zS.Please install 'fastparquert' or 'pyarrow' to utilize the `fsspec.parquet` module.)r1   r2   
ValueErrorFastparquetEnginePyarrowEngineImportError)
engine_strtry_enginesr   r&   r&   r'   r   [  s0   
r   c                   @   .   e Zd Zdd Zdd Z				dddZdS )	ri   c                 C   s   dd l }|| _d S Nr   )rf   fp)selfrp   r&   r&   r'   __init__  s   
zFastparquetEngine.__init__c                 C   s
   | |S rZ   )row_group_filename)rq   	row_grouppfr&   r&   r'   _row_group_filename  s   
z%FastparquetEngine._row_group_filenameNc                 C   sf  |}g g g }}}	|d u r| j t|}|d u rd nt|}
|
d ur<t|dr<dd |jdg D }|
t|O }
|rHt|d t	sHd }n|}|j
}t|D ]V\}}|d u s]||v r| ||}|jD ]@}|jjd }|
d u sv||
v r|jj}|d u r|jj}|jj}|d u s||k r|| || |	t|| |p||  qfqQ|r|||	fS ||	fS )Npandas_metadatac                 S      g | ]	}t |ts|qS r&   r1   dictrV   indr&   r&   r'   
<listcomp>  s    z:FastparquetEngine._parquet_byte_ranges.<locals>.<listcomp>index_columnsr   )rp   ParquetFileioBytesIOrY   hasattrrw   r   r1   r;   r   r5   rv   r   	meta_datapath_in_schemadictionary_page_offsetdata_page_offsettotal_compressed_sizer7   r8   )rq   r   r   r   r-   r.   ru   rE   rF   rG   
column_setmd_indexrow_group_indicesrrt   r$   columnnamefile_offset0	num_bytesr&   r&   r'   r?     sJ   	




z&FastparquetEngine._parquet_byte_rangesNNNN__name__
__module____qualname__rr   rv   r?   r&   r&   r&   r'   ri   ~      ri   c                   @   rn   )	rj   c                 C   s   dd l m} || _d S ro   )pyarrow.parquetparquetpq)rq   r   r&   r&   r'   rr     s   
zPyarrowEngine.__init__c                 C   s   t rZ   )NotImplementedError)rq   rt   r   r&   r&   r'   rv     s   z!PyarrowEngine._row_group_filenameNc                 C   sX  |d urt dg g }}| jt|j}|d u rd nt|}	|	d urP|j }
|
jd uo3d|
jv }|rPdd t	
|
jd ddg D }|	t|O }	t|jD ]R}|d u s_||v r||}t|jD ]=}||}|j}|dd }|	d u s||	v s||	v r|j}|d u r|j}|j}||k r|| |t|| | qiqU||fS )	Nz.metadata input not supported for PyarrowEngines   pandasc                 S   rx   r&   ry   r{   r&   r&   r'   r}     s    z6PyarrowEngine._parquet_byte_ranges.<locals>.<listcomp>utf8r~   r,   r   )rh   r   r   r   r   r   rY   schemato_arrow_schemajsonloadsdecoder   r6   num_row_groupsrt   num_columnsr   r   splitr   r   r   r7   r8   )rq   r   r   r   r-   r.   rF   rG   mdr   r   has_pandas_metadatar   r   rt   cr   r   
split_namer   r   r&   r&   r'   r?     sL   




z"PyarrowEngine._parquet_byte_rangesr   r   r&   r&   r&   r'   rj     r   rj   )r   NNNNNFr   r   r   r	   )NNNr   r   r	   r   )NNr   r   )r   r   r=   corer   utilsr   r(   r   r3   r@   rA   r   ri   rj   r&   r&   r&   r'   <module>   sH    

 
*#U