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ZIeMHeded@eeeCf deBfdEdZIeMHe$de$d@eeCef deBfdFdZIeMHe!ded@eeCef deBfdGdZIeMHe#de#d@eeCef deBfdHdZIeMHeeMHedeeef d@eDdeDfdIdZIeMHeded@eBdeBfdJdZIeMHedKed3eFdeBfdLdZIeMHeded3eFdeBfdMdZIeMHeded@edeBfdNdZIeMHe&de&d@ejEdeBfdOdZIe	ded@edefdPdQZNeNHeded@eJdeJfdRdZIeNHeeNHedeeef d@eCdeCfdSdZIeNHeded@eeBeCef defdTdZIeNHe$de$d@eeBeCef defdUdZIeNHe!ded@eeBeCef defdVdZIeNHe#de#d@eeBeCef defdWdZIeNHeeNHedeeef d@eDdeDfdXdZIeNHeded@eBdeBfdYdZIeNHedKed@eeBeFf deFfdZdZIeNHeded@eeFeBf deFfd[dZIeNHeded@eBdefd\dZIeNHe&de&d@eeBeFejEf dejEfd]dZIdS )^a  Utility module for various conversions around PrimitiveType implementations.

This module enables:
    - Converting partition strings to built-in python objects.
    - Converting a value to a byte buffer.
    - Converting a byte buffer to a value.
    - Converting a json-single field serialized field

Note:
    Conversion logic varies based on the PrimitiveType implementation. Therefore conversion functions
    are defined here as generic functions using the @singledispatch decorator. For each PrimitiveType
    implementation, a concrete function is registered for each generic conversion function. For PrimitiveType
    implementations that share the same conversion logic, registrations can be stacked.
    N)datedatetimetimeDecimal)singledispatch)Struct)AnyCallableOptionalUnion)UTF8L)
BinaryTypeBooleanTypeDateTypeDecimalType
DoubleType	FixedType	FloatTypeIntegerTypeLongTypePrimitiveType
StringTypeTimestampNanoTypeTimestampTypeTimestamptzNanoTypeTimestamptzTypeTimeTypeUnknownTypeUUIDType	strtobool)date_str_to_daysdate_to_daysdatetime_to_microsdatetime_to_nanosdays_to_datemicros_to_timemicros_to_timestampmicros_to_timestamptztime_str_to_microstime_to_microstimestamp_to_microstimestamptz_to_microsto_human_dayto_human_timeto_human_timestampto_human_timestamptz)decimal_to_bytesunscaled_to_decimalz<?z<iz<qz<fz<dfuncreturnc                    s"   dt dtt dtf fdd}|S )zHandle cases where partition values are `None` or "__HIVE_DEFAULT_PARTITION__".

    Args:
        func (Callable): A function registered to the singledispatch function `partition_to_py`.
    primitive_type	value_strr5   c                    s"   |d u rd S |dkrd S  | |S )N__HIVE_DEFAULT_PARTITION__ r6   r7   r4   r9   X/home/ubuntu/maya3_transcribe/venv/lib/python3.10/site-packages/pyiceberg/conversions.pywrappere   s
   
zhandle_none.<locals>.wrapper)r   r   strr	   )r4   r=   r9   r;   r<   handle_none^   s   r?   r6   r7   c                 C   s   t d| d|  )zConvert a partition string to a python built-in.

    Args:
        primitive_type (PrimitiveType): An implementation of the PrimitiveType base class.
        value_str (str): A string representation of a partition value.
    zCannot convert 'z' to unsupported type: 	TypeErrorr:   r9   r9   r<   partition_to_pyo      rB   c                 C      t |S N)r!   r:   r9   r9   r<   _z      rF   c                 C   s6   t | \}}}|dkrtd|  dtt|S )zkConvert a string to an integer value.

    Raises:
        ValueError: If the scale/exponent is not 0.
    r   zHCannot convert partition value, value cannot have fractional digits for z
 partition)r   as_tuple
ValueErrorintfloat)r6   r7   rF   exponentr9   r9   r<   rF      s   c                 C   rD   rE   )rK   rF   r7   r9   r9   r<   rF      s   c                 C      |S rE   r9   rM   r9   r9   r<   rF         c                 C   
   t |S rE   )uuidUUIDrM   r9   r9   r<   rF      s   
c                 C   s
   t |tS rE   )bytesr   rM   r9   r9   r<   rF      s   
c                 C   rD   rE   r   rM   r9   r9   r<   rF      rG   type_c                 C      d S rE   r9   )rT   rF   r9   r9   r<   rF      rO   c                 C   s   t d|  )a.  Convert a built-in python value to bytes.

    This conversion follows the serialization scheme for storing single values as individual binary values defined in the Iceberg specification that
    can be found at https://iceberg.apache.org/spec/#appendix-d-single-value-serialization

    Args:
        primitive_type (PrimitiveType): An implementation of the PrimitiveType base class.
        _: The value to convert to bytes (The type of this value depends on which dispatched function is
            used--check dispatchable functions for type hints).
    zscale does not match r@   )r6   rF   r9   r9   r<   to_bytes   s   rV   valuec                 C   s   t |rdS dS )N   r   )_BOOL_STRUCTpackrF   rW   r9   r9   r<   rF      s   c                 C   rP   rE   )_INT_STRUCTrZ   r[   r9   r9   r<   rF         
c                 C   rP   rE   )_LONG_STRUCTrZ   r[   r9   r9   r<   rF      r]   c                 C      t |tr	t|}t|S rE   )
isinstancer   r$   r^   rZ   r[   r9   r9   r<   rF         

c                 C   r_   rE   )r`   r   r%   r^   rZ   r[   r9   r9   r<   rF      ra   c                 C   r_   rE   )r`   r   r#   r\   rZ   r[   r9   r9   r<   rF         

c                 C   r_   rE   )r`   r   r+   r^   rZ   r[   r9   r9   r<   rF      rb   c                 C   rP   )zConvert a float value into bytes.

    Note: float in python is implemented using a double in C. Therefore this involves a conversion of a 32-bit (single precision)
    float to a 64-bit (double precision) float which introduces some imprecision.
    )_FLOAT_STRUCTrZ   r[   r9   r9   r<   rF      s   
c                 C   rP   rE   )_DOUBLE_STRUCTrZ   r[   r9   r9   r<   rF     r]   c                 C   s
   | tS rE   )encoder   r[   r9   r9   r<   rF     r]   c                 C   s   t |tr|S |jS rE   )r`   rS   r[   r9   r9   r<   rF     s   
c                 C   rN   rE   r9   r[   r9   r9   r<   rF     rO   c                 C   sf   |  \}}}tt|}|| jkrtd|  d| t|| jkr/td|  dt| t|S )a  Convert a Decimal value to bytes given a DecimalType instance with defined precision and scale.

    Args:
        primitive_type (DecimalType): A DecimalType instance with precision and scale.
        value (Decimal): A Decimal instance.

    Raises:
        ValueError: If either the precision or scale of `value` does not match that defined in the DecimalType instance.


    Returns:
        bytes: The byte representation of `value`.
    z;Cannot serialize value, scale of value does not match type z: zMCannot serialize value, precision of value is greater than precision of type )rH   absrJ   scalerI   len	precisionr2   )r6   rW   rF   digitsrL   r9   r9   r<   rF     s   
bc                 C   s   t d|  d|)zConvert bytes to a built-in python value.

    Args:
        primitive_type (PrimitiveType): An implementation of the PrimitiveType base class.
        b (bytes): The bytes to convert.
    Cannot deserialize bytes, type  not supported: r@   )r6   rk   r9   r9   r<   
from_bytes5  rC   rn   c                 C   s   t |d dkS Nr   )rY   unpackrF   rk   r9   r9   r<   rF   @  s   c                 C      t |d S ro   )r\   rp   rq   r9   r9   r<   rF   E  s   c                 C   rr   ro   )r^   rp   rq   r9   r9   r<   rF   K  s   c                 C   rr   ro   )rc   rp   rq   r9   r9   r<   rF   U     c                 C   rr   ro   )rd   rp   rq   r9   r9   r<   rF   Z  rs   c                 C   s   t |tS rE   )rS   decoder   rq   r9   r9   r<   rF   _  rs   c                 C   rN   rE   r9   rq   r9   r9   r<   rF   d     bufc                 C   s   t j|ddd}t|| jS )NbigT)signed)rJ   rn   r3   rg   )r6   rv   unscaledr9   r9   r<   rF   k  s   c                 C   rU   rE   r9   )rT   rv   r9   r9   r<   rF   q  s   valc                 C   s   t d|  d| )a5  Convert built-in python values into JSON value types.

    https://iceberg.apache.org/spec/#json-single-value-serialization

    Args:
        primitive_type (PrimitiveType): An implementation of the PrimitiveType base class.
        val (Any): The arbitrary built-in value to convert into the right form
    rl   rm   r@   r6   rz   r9   r9   r<   to_jsonv  s   
r|   c                 C   rN   )z4Python bool automatically converts into a JSON bool.r9   rF   rz   r9   r9   r<   rF     rO   c                 C   rN   )z0Python int automatically converts to a JSON int.r9   r}   r9   r9   r<   rF     ru   c                 C      t |tr	t|}t|S zJSON date is string encoded.)r`   r   r#   r.   r}   r9   r9   r<   rF        
c                 C   r~   )zHPython time or microseconds since epoch serializes into an ISO8601 time.)r`   r   r+   r/   r}   r9   r9   r<   rF     r   c                 C   r~   )zdPython datetime (without timezone) or microseconds since epoch serializes into an ISO8601 timestamp.)r`   r   r$   r0   r}   r9   r9   r<   rF     s   
c                 C   r~   )zaPython datetime (with timezone) or microseconds since epoch serializes into an ISO8601 timestamp.)r`   r   r$   r1   r}   r9   r9   r<   rF     r   c                 C   rN   )z!Float serializes into JSON float.r9   r}   r9   r9   r<   rF     ru   c                 C   rN   )z*Python string serializes into JSON string.r9   r}   r9   r9   r<   rF     rO   tc                 C   s>   t | t |krtdt |  dt | t|dtS )8Python bytes serializes into hexadecimal encoded string.FixedType has length %, which is different from the value: hex)rh   rI   codecsre   rt   r   )r   rk   r9   r9   r<   rF     s   c                 C   s   t |dtS )r   r   )r   re   rt   r   rq   r9   r9   r<   rF     s   c                 C   rD   )af  Python decimal serializes into string.

    Stores the string representation of the decimal value, specifically, for
    values with a positive scale, the number of digits to the right of the
    decimal point is used to indicate scale, for values with a negative scale,
    the scientific notation is used and the exponent must equal the negated scale.
    r>   r}   r9   r9   r<   rF     s   	c                 C   rD   )zSerialize into a JSON string.r   r}   r9   r9   r<   rF     rG   c                 C   s   t d|  dt| )a1  Convert JSON value types into built-in python values.

    https://iceberg.apache.org/spec/#json-single-value-serialization

    Args:
        primitive_type (PrimitiveType): An implementation of the PrimitiveType base class.
        val (Any): The arbitrary JSON value to convert into the right form
    rl   rm   )rA   r>   r{   r9   r9   r<   	from_json  s   
r   c                 C   rN   )z4JSON bool automatically converts into a Python bool.r9   r}   r9   r9   r<   rF     rO   c                 C   rN   )z0JSON int automatically converts to a Python int.r9   r}   r9   r9   r<   rF     ru   c                 C   (   t |tr	t|}t |trt|S |S r   )r`   r>   r"   rJ   r&   r}   r9   r9   r<   rF     
   

c                 C   r   )z%JSON ISO8601 string into Python time.)r`   r>   r*   rJ   r'   r}   r9   r9   r<   rF     r   c                 C   r   z)JSON ISO8601 string into Python datetime.)r`   r>   r,   rJ   r(   r}   r9   r9   r<   rF     r   c                 C   r   r   )r`   r>   r-   rJ   r)   r}   r9   r9   r<   rF     r   c                 C   rN   )z,JSON float deserializes into a Python float.r9   r}   r9   r9   r<   rF   %  ru   c                 C   rN   )z,JSON string serializes into a Python string.r9   r}   r9   r9   r<   rF   ,  rO   c                 C   sL   t |trt|td}t| t|kr$tdt|  dt| |S )+JSON hexadecimal encoded string into bytes.r   r   r   )r`   r>   r   rt   re   r   rh   rI   )r   rz   r9   r9   r<   rF   2  s
   
c                 C   s    t |trt|tdS |S )r   r   )r`   r>   r   rt   re   r   r}   r9   r9   r<   rF   >  s   
c                 C   rD   )z*Convert JSON string into a Python Decimal.r   r}   r9   r9   r<   rF   G  rG   c                 C   s.   t |tr
t|S t |trtj|dS |S )z%Convert JSON string into Python UUID.)rS   )r`   r>   rQ   rR   rS   r}   r9   r9   r<   rF   M  s
   


)O__doc__r   rQ   r   r   r   decimalr   	functoolsr   structr   typingr	   r
   r   r   pyiceberg.typedefr   r   pyiceberg.typesr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   pyiceberg.utils.datetimer"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   pyiceberg.utils.decimalr2   r3   rY   r\   r^   rc   rd   r?   r>   rJ   rK   rR   rS   rB   registerrF   boolrV   rn   r|   r   r9   r9   r9   r<   <module>   s\  TH*
((""  	"
""    """
"
"
"
"  *