a
    •˜ëj?*  ã                   @  sÒ   d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 ddl
mZ dd	lmZ G d
d„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZeG dd„ dƒƒZG dd„ dƒZG dd„ dejƒZdS )é    )ÚannotationsN)Ú	dataclass)ÚEnum)ÚOptional)ÚUnioné   )Útypes)ÚFloatc                   @  s   e Zd ZdZdZdZdS )ÚVectorIndexTypez˜Enum representing different types of VECTOR index structures.

    See :ref:`oracle_vector_datatype` for background.

    .. versionadded:: 2.0.41

    ÚHNSWÚIVFN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   © r   r   ú`/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/sqlalchemy/dialects/oracle/vector.pyr
      s   r
   c                   @  s    e Zd ZdZdZdZdZdZdS )ÚVectorDistanceTypez˜Enum representing different types of vector distance metrics.

    See :ref:`oracle_vector_datatype` for background.

    .. versionadded:: 2.0.41

    Ú	EUCLIDEANÚDOTÚCOSINEÚ	MANHATTANN)r   r   r   r   r   r   r   r   r   r   r   r   r   )   s   r   c                   @  s    e Zd ZdZdZdZdZdZdS )ÚVectorStorageFormatz�Enum representing the data format used to store vector components.

    See :ref:`oracle_vector_datatype` for background.

    .. versionadded:: 2.0.41

    ÚINT8ÚBINARYÚFLOAT32ÚFLOAT64N)r   r   r   r   r   r   r   r   r   r   r   r   r   H   s   r   c                   @  s   e Zd ZdZdZdZdS )ÚVectorStorageTypez}Enum representing the vector type,

    See :ref:`oracle_vector_datatype` for background.

    .. versionadded:: 2.0.43

    ÚSPARSEÚDENSEN)r   r   r   r   r   r   r   r   r   r   r   c   s   r   c                   @  sˆ   e Zd ZU dZejZded< dZded< dZ	ded< dZ
ded	< dZded
< dZded< dZded< dZded< dZded< dd„ ZdS )ÚVectorIndexConfigaÆ  Define the configuration for Oracle VECTOR Index.

    See :ref:`oracle_vector_datatype` for background.

    .. versionadded:: 2.0.41

    :param index_type: Enum value from :class:`.VectorIndexType`
     Specifies the indexing method. For HNSW, this must be
     :attr:`.VectorIndexType.HNSW`.

    :param distance: Enum value from :class:`.VectorDistanceType`
     specifies the metric for calculating distance between VECTORS.

    :param accuracy: integer. Should be in the range 0 to 100
     Specifies the accuracy of the nearest neighbor search during
     query execution.

    :param parallel: integer. Specifies degree of parallelism.

    :param hnsw_neighbors: integer. Should be in the range 0 to
     2048. Specifies the number of nearest neighbors considered
     during the search. The attribute :attr:`.VectorIndexConfig.hnsw_neighbors`
     is HNSW index specific.

    :param hnsw_efconstruction: integer. Should be in the range 0
     to 65535. Controls the trade-off between indexing speed and
     recall quality during index construction. The attribute
     :attr:`.VectorIndexConfig.hnsw_efconstruction` is HNSW index
     specific.

    :param ivf_neighbor_partitions: integer. Should be in the range
     0 to 10,000,000. Specifies the number of partitions used to
     divide the dataset. The attribute
     :attr:`.VectorIndexConfig.ivf_neighbor_partitions` is IVF index
     specific.

    :param ivf_sample_per_partition: integer. Should be between 1
     and ``num_vectors / neighbor partitions``. Specifies the
     number of samples used per partition. The attribute
     :attr:`.VectorIndexConfig.ivf_sample_per_partition` is IVF index
     specific.

    :param ivf_min_vectors_per_partition: integer. From 0 (no trimming)
     to the total number of vectors (results in 1 partition). Specifies
     the minimum number of vectors per partition. The attribute
     :attr:`.VectorIndexConfig.ivf_min_vectors_per_partition`
     is IVF index specific.

    r
   Ú
index_typeNzOptional[VectorDistanceType]ÚdistancezOptional[int]ÚaccuracyÚhnsw_neighborsÚhnsw_efconstructionÚivf_neighbor_partitionsÚivf_sample_per_partitionÚivf_min_vectors_per_partitionÚparallelc                 C  sN   t | jƒ| _dD ]8}t| |ƒ}|d urt|tƒst|› dt|ƒj› �ƒ‚qd S )N)r$   r%   r&   r'   r(   r)   r#   z$ must be an integer ifprovided, got )r
   r!   ÚgetattrÚ
isinstanceÚintÚ	TypeErrorÚtyper   )ÚselfÚfieldÚvaluer   r   r   Ú__post_init__¶   s    	
ÿÿzVectorIndexConfig.__post_init__)r   r   r   r   r
   r   r!   Ú__annotations__r"   r#   r$   r%   r&   r'   r(   r)   r2   r   r   r   r   r    x   s   
2r    c                   @  s*   e Zd ZdZddddœdd„Zdd„ Zd	S )
ÚSparseVectorz„
    Lightweight SQLAlchemy-side version of SparseVector.
    This mimics oracledb.SparseVector.

    .. versionadded:: 2.0.43

    r,   zUnion[list, array.array]©Únum_dimensionsÚindicesÚvaluesc                 C  sh   t |tjƒr|jdkr"t d|¡}t |tjƒs:t d|¡}t|ƒt|ƒkrRtdƒ‚|| _|| _|| _d S )NÚIÚdz.indices and values must be of the same length!)r+   ÚarrayÚtypecodeÚlenr-   r6   r7   r8   )r/   r6   r7   r8   r   r   r   Ú__init__Ò   s    zSparseVector.__init__c                 C  s$   d| j › dt| jƒ› d| jj› d�S )NzSparseVector(num_dimensions=z, size=z, typecode=ú))r6   r=   r7   r8   r<   )r/   r   r   r   Ú__str__ã   s    
ÿÿÿzSparseVector.__str__N)r   r   r   r   r>   r@   r   r   r   r   r4   É   s   r4   c                   @  sj   e Zd ZdZdZd Zejdejdej	dej
diZddd	„Zd
d„ Zdd„ Zdd„ ZG dd„ dejjƒZdS )ÚVECTORz“Oracle VECTOR datatype.

    For complete background on using this type, see
    :ref:`oracle_vector_datatype`.

    .. versionadded:: 2.0.41

    TÚbÚBÚfr:   Nc                 C  sd   |durt |tƒstdƒ‚|dur4t |tƒs4tdƒ‚|durNt |tƒsNtdƒ‚|| _|| _|| _dS )aü  Construct a VECTOR.

        :param dim: integer. The dimension of the VECTOR datatype. This
         should be an integer value.

        :param storage_format: VectorStorageFormat. The VECTOR storage
         type format. This should be Enum values form
         :class:`.VectorStorageFormat` INT8, BINARY, FLOAT32, or FLOAT64.

        :param storage_type: VectorStorageType. The Vector storage type. This
         should be Enum values from :class:`.VectorStorageType` SPARSE or
         DENSE.

        Nzdim must be an integerz:storage_format must be an enum of type VectorStorageFormatz6storage_type must be an enum of type VectorStorageType)r+   r,   r-   r   r   ÚdimÚstorage_formatÚstorage_type)r/   rE   rF   rG   r   r   r   r>   ÿ   s"    
ÿÿ
ÿÿzVECTOR.__init__c                   s   ‡ ‡fdd„}|S )z¬
        Converts a Python-side SparseVector instance into an
        oracledb.SparseVectormor a compatible array format before
        binding it to the database.
        c                   sj   | d u st | tjƒr| S t | tƒr>ˆ ˆj¡}t || ¡} | S t | tƒr^ˆ j | j| j| j	¡S t
dƒ‚d S )Nz’
                    Invalid input for VECTOR: expected a list, an array.array,
                    or a SparseVector object.
                    )r+   r;   ÚlistÚ_array_typecoderF   r4   Údbapir6   r7   r8   r-   )r1   r<   ©Údialectr/   r   r   Úprocess)  s    

ýz.VECTOR._cached_bind_processor.<locals>.processr   )r/   rL   rM   r   rK   r   Ú_cached_bind_processor"  s    zVECTOR._cached_bind_processorc                   s   ‡ fdd„}|S )a  
        Converts database-returned values into Python-native representations.
        If the value is an oracledb.SparseVector, it is converted into the
        SQLAlchemy-side SparseVector class.
        If the value is a array.array, it is converted to a plain Python list.

        c                   sF   | d u rd S t | tjƒr t| ƒS t | ˆ jjƒrBt| j| j| jd�S d S )Nr5   )r+   r;   rH   rJ   r4   r6   r7   r8   )r1   ©rL   r   r   rM   L  s    ýz0VECTOR._cached_result_processor.<locals>.processr   )r/   rL   ZcoltyperM   r   rO   r   Ú_cached_result_processorC  s    	zVECTOR._cached_result_processorc                 C  s   | j  |d¡S )z7
        Map storage format to array typecode.
        r:   )Ú_typecode_mapÚget)r/   r<   r   r   r   rI   ]  s    zVECTOR._array_typecodec                   @  s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )zVECTOR.comparator_factoryc                 C  s   | j dtd�|ƒS )Nz<->©Úreturn_type©Úopr	   ©r/   Úotherr   r   r   Úl2_distanced  s    z%VECTOR.comparator_factory.l2_distancec                 C  s   | j dtd�|ƒS )Nz<#>rS   rU   rW   r   r   r   Úinner_productg  s    z'VECTOR.comparator_factory.inner_productc                 C  s   | j dtd�|ƒS )Nz<=>rS   rU   rW   r   r   r   Úcosine_distancej  s    z)VECTOR.comparator_factory.cosine_distanceN)r   r   r   rY   rZ   r[   r   r   r   r   Úcomparator_factoryc  s   r\   )NNN)r   r   r   r   Zcache_okZ__visit_name__r   r   r   r   r   rQ   r>   rN   rP   rI   r   Ú
TypeEngineZ
Comparatorr\   r   r   r   r   rA   ê   s   	ü
#!rA   )Ú
__future__r   r;   Zdataclassesr   Úenumr   Útypingr   r   Ú r   r	   r
   r   r   r   r    r4   r]   rA   r   r   r   r   Ú<module>
   s   P!