콘텐츠로 이동

Use Vector Types

이 예제에서는 vector, vecf16, svector, bvector 값을 표현하는 방법을 다룹니다.

DROP TABLE IF EXISTS akasicdb_example_vector_types;

CREATE TABLE akasicdb_example_vector_types (
  id integer PRIMARY KEY,
  dense vector(3) NOT NULL,
  half_precision vecf16(3) NOT NULL,
  sparse svector(4) NOT NULL,
  binary_code bvector(4) NOT NULL
);

-- From real[]
INSERT INTO akasicdb_example_vector_types
  (id, dense, half_precision, sparse, binary_code)
VALUES
  (
    1,
    ARRAY[1.0, 0.0, 0.0]::real[]::vector,
    ARRAY[1.0, 0.0, 0.0]::real[]::vector::vecf16,
    ARRAY[1.0, 0.0, 0.0, 0.0]::real[]::vector::svector,
    ARRAY[1.0, 0.0, 1.0, 0.0]::real[]::vector::bvector
  ),
  (
    2,
    ARRAY[0.8, 0.2, 0.0]::real[]::vector,
    ARRAY[0.8, 0.2, 0.0]::real[]::vector::vecf16,
    ARRAY[0.0, 1.0, 0.0, 0.0]::real[]::vector::svector,
    ARRAY[1.0, 0.0, 0.0, 0.0]::real[]::vector::bvector
  ),
  (
    3,
    ARRAY[0.0, 1.0, 0.0]::real[]::vector,
    ARRAY[0.0, 1.0, 0.0]::real[]::vector::vecf16,
    ARRAY[0.0, 0.0, 1.0, 0.0]::real[]::vector::svector,
    ARRAY[0.0, 1.0, 0.0, 1.0]::real[]::vector::bvector
  );

-- From vector literals (except sparse vector)
SELECT id
FROM akasicdb_example_vector_types
ORDER BY dense <-> '[1.0, 0.0, 0.0]'::vector
LIMIT 1;

SELECT id
FROM akasicdb_example_vector_types
ORDER BY half_precision <-> '[1.0, 0.0, 0.0]'::vecf16
LIMIT 1;

SELECT id
FROM akasicdb_example_vector_types
ORDER BY binary_code <-> '[1, 0, 1, 0]'::bvector
LIMIT 1;

-- Sparse vector literals use a special syntax with curly braces and position:value pairs
-- Position is 0-based, and unspecified positions are treated as 0.0
SELECT id
FROM akasicdb_example_vector_types
ORDER BY sparse <-> '{0:1.0}/4'::svector
LIMIT 1;

-- to_svector can also be used to construct sparse vectors from arrays of positions and values
-- to_svector(dimension, positions, values)
-- Positions and values arrays must have the same length
SELECT id
FROM akasicdb_example_vector_types
ORDER BY sparse <-> to_svector(4, ARRAY[0]::int[], ARRAY[1.0]::real[])
LIMIT 1;

Types

Type 값 생성/변환 방법
vector vector literal 또는 real[]로부터 변환
vecf16 vector literal 또는 vector로부터 변환
svector sparse vector literal 또는 real[]로부터 변환, 또는 to_svector() 사용
bvector vector literal 또는 real[]로부터 변환