All pages
Powered by GitBook
1 of 6

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

VEC_DISTANCE_COSINE

Calculate Cosine distance. This function computes the Cosine distance between two vectors, measuring the cosine of the angle between them.

Syntax

VEC_DISTANCE_COSINE(v, s)

Description

VEC_Distance_Cosine is an SQL function that calculates the between two (not necessarily normalized) vectors.

Vectors must be of the same length. A distance between two vectors of different lengths is not defined, and VEC_Distance_Cosine will return NULL in such cases.

If the vector index was not built for the cosine function (see ), the index is not used — a full table scan is performed instead. The function is a generic function that behaves either as or VEC_DISTANCE_COSINE, depending on the underlying index type.

Example

See Also

This page is licensed: CC BY-SA / Gnu FDL

Cosine distance
CREATE TABLE with Vectors
VEC_DISTANCE
VEC_DISTANCE_EUCLIDEAN
VEC_DISTANCE
VEC_DISTANCE_EUCLIDEAN
Vector Overview
CREATE TABLE with Vectors
SELECT VEC_DISTANCE_COSINE(vec_fromtext('[1,2,3]'), vec_fromtext('[3,5,7]'));
+-----------------------------------------------------------------------+
| VEC_DISTANCE_COSINE(vec_fromtext('[1,2,3]'), vec_fromtext('[3,5,7]')) |
+-----------------------------------------------------------------------+
|                                                   0.00258509695694209 |
+-----------------------------------------------------------------------+

VEC_FromText

Convert text to vector. This function parses a JSON array string representation of a vector and converts it into the binary VECTOR data type.

Syntax

VEC_FromText(s)

Description

VEC_FromText converts a text representation of the vector (json array of numbers) to a vector (little-endian IEEE float sequence of bytes, 4 bytes per float).

Example

This page is licensed: CC BY-SA / Gnu FDL

SELECT HEX(vec_fromtext('[1,2,3]')); 
+------------------------------+
| HEX(vec_fromtext('[1,2,3]')) |
+------------------------------+
| 0000803F0000004000004040     |
+------------------------------+

VEC_ToText

Convert vector to text. This function takes a binary VECTOR data type and returns its JSON array string representation.

Syntax

VEC_ToText(v)

Description

VEC_ToText converts a binary vector into a json array of numbers (floats). Returns NULL and throws a warning if given an invalid vector.

Example

Invalid vector:

See Also

This page is licensed: CC BY-SA / Gnu FDL

SELECT VEC_ToText(x'e360d63ebe554f3fcdbc523f4522193f5236083d');
+---------------------------------------------------------+
| VEC_ToText(x'e360d63ebe554f3fcdbc523f4522193f5236083d') |
+---------------------------------------------------------+
| [0.418708,0.809902,0.823193,0.598179,0.033255]          |
+---------------------------------------------------------+
Error 4201
Error 4201: Invalid binary vector format
SELECT VEC_ToText(x'aabbcc');
+-----------------------+
| VEC_ToText(x'aabbcc') |
+-----------------------+
| NULL                  |
+-----------------------+
1 row in set, 1 warning (0.000 sec)

Warning (Code 4201): Invalid binary vector format. Must use IEEE standard float 
  representation in little-endian format. Use VEC_FromText() to generate it.

Vectors are available from .

Vectors are available from .

Vectors are available from .

Cover

WEBINAR

The Next Generation of MariaDB: Powered by Vector Search

Watch Now
Cover

WEBINAR

The Next Generation of MariaDB: Powered by Vector Search

Watch Now
Cover

WEBINAR

The Next Generation of MariaDB: Powered by Vector Search

Watch Now
MariaDB 11.7
MariaDB 11.7
MariaDB 11.7

VEC_DISTANCE_EUCLIDEAN

Calculate Euclidean distance. This function computes the Euclidean (L2) distance between two vectors, representing the straight-line distance.

are available from .

Syntax

Description

VEC_Distance_Euclidean is an SQL function that calculates a Euclidean (L2) distance between two points.

Vectors must be of the same length, a distance between two vectors of different lengths is not defined and VEC_Distance_Euclidean returns NULL in such cases.

If the vector index was not built for the euclidean function (see ), the index is not used, and a full table scan performed instead. The function is a generic function that behaves either as VEC_DISTANCE_EUCLIDEAN or , depending on the underlying index type.

Example

See Also

This page is licensed: CC BY-SA / Gnu FDL

VEC_DISTANCE_EUCLIDEAN(v, s)
CREATE TABLE with Vectors
VEC_DISTANCE
VEC_DISTANCE_COSINE
VEC_DISTANCE
VEC_DISTANCE_COSINE
Vector Overview
CREATE TABLE with Vectors
Vectors
MariaDB 11.7
INSERT INTO v VALUES 
     (1, x'e360d63ebe554f3fcdbc523f4522193f5236083d'),
     (2, x'f511303f72224a3fdd05fe3eb22a133ffae86a3f'),
     (3,x'f09baa3ea172763f123def3e0c7fe53e288bf33e'),
     (4,x'b97a523f2a193e3eb4f62e3f2d23583e9dd60d3f'),
     (5,x'f7c5df3e984b2b3e65e59d3d7376db3eac63773e'),
     (6,x'de01453ffa486d3f10aa4d3fdd66813c71cb163f'),
     (7,x'76edfc3e4b57243f10f8423fb158713f020bda3e'),
     (8,x'56926c3fdf098d3e2c8c5e3d1ad4953daa9d0b3e'),
     (9,x'7b713f3e5258323f80d1113d673b2b3f66e3583f'),
     (10,x'6ca1d43e9df91b3fe580da3e1c247d3f147cf33e');

SELECT id FROM v 
  ORDER BY VEC_Distance_Euclidean(v, x'6ca1d43e9df91b3fe580da3e1c247d3f147cf33e');
+----+
| id |
+----+
| 10 |
|  7 |
|  3 |
|  9 |
|  2 |
|  1 |
|  5 |
|  4 |
|  6 |
|  8 |
+----+
Cover

WEBINAR

The Next Generation of MariaDB: Powered by Vector Search

Watch Now

Vector Functions

Explore vector functions. This section details SQL functions for manipulating and querying vector data types, enabling efficient similarity search and AI/ML applications within your database.

VEC_DISTANCE

Calculate distance between vectors. This function computes the distance between two vectors using either Euclidean or Cosine metric, depending on the index.

are available from .

Syntax

Description

VEC_DISTANCE is a generic function that behaves either as , calculating the Euclidean (L2) distance between two points. Or , calculating the Cosine distance between two vectors, depending on the underlying index type.

If the underlying index cannot be determined, an is returned:

Example

See Also

This page is licensed: CC BY-SA / Gnu FDL

VEC_DISTANCE(v, s)
VEC_DISTANCE_EUCLIDEAN
VEC_DISTANCE_COSINE
error 4206
VEC_DISTANCE_COSINE
VEC_DISTANCE_EUCLIDEAN
Vector Overview
CREATE TABLE with Vectors
Vectors
Cover

WEBINAR

The Next Generation of MariaDB: Powered by Vector Search

MariaDB 11.7
ERROR 4206 (HY000): Cannot determine distance type for VEC_DISTANCE, index is not found
INSERT INTO v VALUES 
     (1, x'e360d63ebe554f3fcdbc523f4522193f5236083d'),
     (2, x'f511303f72224a3fdd05fe3eb22a133ffae86a3f'),
     (3,x'f09baa3ea172763f123def3e0c7fe53e288bf33e'),
     (4,x'b97a523f2a193e3eb4f62e3f2d23583e9dd60d3f'),
     (5,x'f7c5df3e984b2b3e65e59d3d7376db3eac63773e'),
     (6,x'de01453ffa486d3f10aa4d3fdd66813c71cb163f'),
     (7,x'76edfc3e4b57243f10f8423fb158713f020bda3e'),
     (8,x'56926c3fdf098d3e2c8c5e3d1ad4953daa9d0b3e'),
     (9,x'7b713f3e5258323f80d1113d673b2b3f66e3583f'),
     (10,x'6ca1d43e9df91b3fe580da3e1c247d3f147cf33e');

SELECT id FROM v 
  ORDER BY VEC_DISTANCE(v, x'6ca1d43e9df91b3fe580da3e1c247d3f147cf33e');
+----+
| id |
+----+
| 10 |
|  7 |
|  3 |
|  9 |
|  2 |
|  1 |
|  5 |
|  4 |
|  6 |
|  8 |
+----+
Watch Now