
    iF-              	          d dl Z d dlZd dlZd dlZd dlZd dl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Zd dlZd dlmZ ddlmZ d Z e       Zd	ed
efdZd	ed
efdZd	ed
e
ej>                  ef   fdZ deeejB                  f   ded
efdZ"d3defdZ#d3dejH                  deee%e&f      fdZ'dejP                  d
efdZ)dejT                  d
e&fdZ+dejP                  dedee   dee   fdZ,ed        Z-e-j]                  e      defd        Z/e-j]                  e0      de0fd!       Z/e-j]                  e%      de%fd"       Z/e-j]                  e1      de1fd#       Z/e-j]                  e&      de&fd$       Z/e-j]                   e2d            d e2d      fd%       Z/e-j]                  e      defd&       Z/e-j]                  e      defd'       Z/e-j]                  e3      de3fd(       Z/e-j]                  ejh                        dejh                  fd)       Z/d* Z5d+efd,Zd-e6d.efd/Z7dejH                  fd0Z8d1ejr                  fd2Z:y)4    N)datedatetime)singledispatch)TupleUnionOptionalAny)urlparse   )validate_table_namec                  .    	 dd l } | S # t        $ r Y y w xY w)Nr   )adlfsImportError)r   s    H/mnt/e/genesis-system/.venv/lib/python3.12/site-packages/lancedb/util.pysafe_import_adlfsr      s!     s    	urireturnc                 p    t        |       }|j                  }|sd}|S |dv rd}|S t        |      dk(  rd}|S )z
    Get the scheme of a URI. If the URI does not have a scheme, assume it is a file URI.

    Parameters
    ----------
    uri : str
        The URI to parse.

    Returns
    -------
    str: The scheme of the URI.
    file)s3as3ns3r   )r
   schemelen)r   parsedr   s      r   get_uri_schemer   #   sU     c]F]]F M 
>	! M 
V	 M    c                     t        |       }t        |j                        dk(  r| S |j                  s|j                  S |j                  |j                  z   S )z
    Get the location of a URI. If the parameter is not a url, assumes it is just a path

    Parameters
    ----------
    uri : str
        The URI to parse.

    Returns
    -------
    str: Location part of the URL, without scheme
    r   )r
   r   r   netlocpath)r   r   s     r   get_uri_locationr!   >   sI     c]F
6==Q 
=={{}}v{{**r   c                    t        |       dk(  rDt        j                  t        j                  j                  d      dd      }t        |       }||fS t        |       dk(  rt        t        j                  t        j                  j                  d      t        j                  j                  d            }t        j                  t        j                  |            }t        |       }||fS t        j                  j                  |       S )	zT
    Get a PyArrow FileSystem from a URI, handling extra environment variables.
    r   AWS_ENDPOINT   )endpoint_overriderequest_timeoutconnect_timeoutazAZURE_STORAGE_ACCOUNT_NAMEAZURE_STORAGE_ACCOUNT_KEY)account_nameaccount_key)r   pa_fsS3FileSystemosenvirongetr!   r   AzureBlobFileSystemPyFileSystemFSSpecHandler
FileSystemfrom_uri)r   fsr    
az_blob_fss       r   fs_from_urir9   X   s     cd" jjnn^<

  $4x			$):..(DE

'BC / 


  3 3J ?@$4x$$S))r   basepartsc                    t        | t        j                        r | j                  | S t	        |       } t        |       dk(  rt	        t        j                  | g|       S t        |       }dj                  |j                  g|D cg c]  }|j                  d       c}      }|j                  |      j                         S c c}w )a  
    Join a URI with multiple parts, handles both local and remote paths

    Parameters
    ----------
    base : str
        The base URI
    parts : str
        The parts to join to the base URI, each separated by the
        appropriate path separator for the URI scheme and OS
    r   /)r    )
isinstancepathlibPathjoinpathstrr   r
   joinr    rstrip_replacegeturl)r:   r;   urlpnew_paths        r   join_urirJ   s   s     $%t}}e$$t9Ddv% 7<<-u-.. tn88SXX4F4FGqQXXc]GH|||*1133 Hs   	Cmodulec                 l    	 t        j                  |       S # t        $ r t        d|xs |        w xY w)ac  
    Import the specified module. If the module is not installed,
    raise an ImportError with a helpful message.

    Parameters
    ----------
    module : str
        The name of the module to import
    mitigation : Optional[str]
        The package(s) to install to mitigate the error.
        If not provided then the module name will be used.
    zPlease install )	importlibimport_moduler   )rK   
mitigations     r   attempt_import_or_raiserP      sB    D&&v.. DOJ,@&+ABCCDs    3tblflattenc                     |du r1	 | j                         } t        d | j                  D              r.	 | S t        |t              r0|dk  rt        d      |dkD  r| j                         } |dz  }|dkD  r| S )aJ  
    Flatten all struct columns in a table.

    Parameters
    ----------
    flatten: Optional[Union[int, bool]]
        If flatten is True, flatten all nested columns.
        If flatten is an integer, flatten the nested columns up to the
        specified depth.
        If unspecified, do not flatten the nested columns.
    Tc              3   n   K   | ]-  }t         j                  j                  |j                         / y wN)patypes	is_structtype).0cols     r   	<genexpr>z"flatten_columns.<locals>.<genexpr>   s#     FC288%%chh/Fs   35r   zIPlease specify a positive integer for flatten or the boolean value `True`r   )rR   anyschemar>   int
ValueError)rQ   rR   s     r   flatten_columnsra      s     $++-CF3::FF J 
GS	!a<  k++-CqLG k Jr   r^   c                     d}d}| j                   D ]F  }| j                  |      }t        |j                        s*|dz  }|dkD  rt	        d      |dk(  sE|}H |dk(  rt	        d      |S )z
    Get the vector column name

    Parameters
    ----------
    schema : pa.Schema
        The schema of the vector column.

    Returns
    -------
    str: the vector column name.
     r   r   z_Schema has more than one vector column. Please specify the vector column name for vector searchz^There is no vector column in the data. Please specify the vector column name for vector search)namesfieldis_vector_columnrY   r`   )r^   vector_col_namevector_col_count
field_namere   s        r   inf_vector_column_queryrj      s     Oll -
Z(EJJ'!!# ( 
 "Q&",- 1F
 	
 r   	data_typec                 P   t         j                  j                  |       rSt         j                  j                  | j                        s)t         j                  j                  | j                        ryt         j                  j                  |       rt        | j                        S y)z
    Check if the column is a vector column.

    Parameters
    ----------
    data_type : pa.DataType
        The data type of the column.

    Returns
    -------
    bool: True if the column is a vector column.
    TF)rV   rW   is_fixed_size_listis_floating
value_typeis_uint8is_listrf   )rk   s    r   rf   rf      so     
xx""9-
Y11288Y112			)	$	 4 455r   
query_typequeryvector_column_namec                 j    ||S |dk(  ry ||dk(  r	 t        |       }|S |S # t        $ r}|d }~ww xY w)Nftshybrid)rj   	Exception)r^   rr   rs   rt   es        r   infer_vector_column_namerz      s]     %!!UJ(2	!8!@   	G	s   " 	2-2c                     t        d      )Nz/SQL conversion is not implemented for this type)NotImplementedErrorvalues    r   value_to_sqlr     s    
O
PPr   r~   c                 2    | j                  dd      } d|  dS )N'z'')replacer}   s    r   _r     s     MM#t$EugQ<r   c                 P    dt        j                  |       j                          dS )z~Convert bytes to a hex string literal.

    See https://datafusion.apache.org/user-guide/sql/data_types.html#binary-types
    zX'r   )binasciihexlifydecoder}   s    r   r   r     s(       '..0133r   c                     t        |       S rU   rB   r}   s    r   r   r   $      u:r   c                     t        |       S rU   r   r}   s    r   r   r   )  r   r   c                 4    t        |       j                         S rU   )rB   upperr}   s    r   r   r   .  s    u:r   c                      y)NNULL r}   s    r   r   r   3  s    r   c                 *    d| j                          dS Nr   	isoformatr}   s    r   r   r   8      u !##r   c                 *    d| j                          dS r   r   r}   s    r   r   r   =  r   r   c                 L    ddj                  t        t        |             z   dz   S )N[z, ])rC   mapr   r}   s    r   r   r   B  s"    3|U344s::r   c                 4    t        | j                               S rU   )r   tolistr}   s    r   r   r   G  s    ''r   c                 B     t        j                          fd       }|S )zThis is a decorator which can be used to mark functions
    as deprecated. It will result in a warning being emitted
    when the function is used.c                      t        j                  dt               t        j                  dj                   dt        d       t        j                  dt                | i |S )Nalwaysz	Function z6 is deprecated and will be removed in a future version   )category
stackleveldefault)warningssimplefilterDeprecationWarningwarn__name__)argskwargsfuncs     r   new_funczdeprecated.<locals>.new_funcQ  s`    h(:;DMM? +. . (	
 	i);<T$V$$r   )	functoolswraps)r   r   s   ` r   
deprecatedr   L  s'    
 __T% % Or   namec                     t        |        y)zVerify the table name is valid.N)native_validate_table_name)r   s    r   r   r   b  s
    t$r   base_exceptionnotec                     t        | d      r| j                  |       y t        | j                  d   t              r,| j                  d   dz   |z   g| j                  dd  | _        y t        d      )Nadd_noter   
r   zCannot add note to exception)hasattrr   r>   r   rB   r`   )r   r   s     r   r   r   g  st    ~z*%	N''*C	0"T)D0
  $

 788r   c           
          t        dd      dt        j                  ffd}j                  t	        | j
                        D cg c]  } || j                  |             c}      S c c}w )a&  
    Convert a PyArrow Table to a PyTorch Tensor.

    Each column is converted to a tensor (using zero-copy via DLPack)
    and the columns are then stacked into a single tensor.

    Fails if torch is not installed.
    Fails if any column is more than one chunk.
    Fails if a column's data type is not supported by PyTorch.

    Parameters
    ----------
    tbl : pa.Table or pa.RecordBatch
        The table or record batch to convert to a tensor.

    Returns
    -------
    torch.Tensor: The tensor containing the columns of the table.
    torchr[   c                 x    | j                   dkD  rt        d      j                  | j                  d            S )Nr   z8Single batch was too large to fit into a one-chunk tabler   )
num_chunksrx   from_dlpackchunk)r[   r   s    r   	to_tensorz tbl_to_tensor.<locals>.to_tensor  s4    >>AVWW  1..r   )rP   rV   ChunkedArraystackrangenum_columnscolumn)rQ   r   ir   s      @r   tbl_to_tensorr   s  sV    ( $GW5E/r /
 ;;%:PQQ	#**Q-0QRRQs   A+batchc                     t        dd      }|j                  | j                  D cg c]  }|j                  |       c}      S c c}w )a  
    Convert a PyArrow RecordBatch to a PyTorch Tensor.

    Each column is converted to a tensor (using zero-copy via DLPack)
    and the columns are then stacked into a single tensor.

    Fails if torch is not installed.
    Fails if a column's data type is not supported by PyTorch.

    Parameters
    ----------
    batch : pa.RecordBatch
        The record batch to convert to a tensor.

    Returns
    -------
    torch.Tensor: The tensor containing the columns of the record batch.
    r   )rP   r   columnsr   )r   r   r[   s      r   batch_to_tensorr     s<    & $GW5E;;%--H3))#.HIIHs   ArU   );r   r   rM   r/   r?   r   r   r   r   typingr   r   r   r	   urllib.parser
   numpynppyarrowrV   
pyarrow.fsr7   r-   _lancedbr   r   r   r   rB   r   r!   r5   r9   r@   rJ   rP   Tabler_   boolra   Schemarj   DataTyperf   rz   r   registerr   bytesfloatrY   listndarrayr   BaseExceptionr   r   RecordBatchr   r   r   r   <module>r      s6  
    	   # $ . . !    G 	  6+# +# +4*S *U5#3#3S#89 *645gll*+ 4S 4S 44DC D& HU395E,F @ BII  #  F  .II C= !	, Q Q sS  
 u4U 4 4 sS   uU   tT   tDz"T$Z  # x $X $ !$ t$T $ $ t;T ; ; rzz"(RZZ ( #(,%c %
	9] 	9# 	9Srxx S<J2>> Jr   