Snowflake Gen AI Functions · Topic 9 of 26

VECTOR Data Type and Similarity Functions - Inner Product, L1, L2 and Cosine

Understand the VECTOR type and the four vector similarity functions. Know which are higher is closer or lower is closer, plus return types and precision.

  • 4 min
  • Includes a diagram
In a nutshell
  • VECTOR(INT or FLOAT, dimension), with a maximum dimension of 4096.
  • Cosine similarity is a FLOAT from -1 to 1; inner product is larger for similar vectors.
  • L1 and L2 are distances: smaller means closer, and L2 of 0 means identical.

What it is

Snowflake has a VECTOR data type for embeddings and a family of vector functions that work on it. Four of them compare two vectors: VECTOR_INNER_PRODUCT, VECTOR_L1_DISTANCE, VECTOR_L2_DISTANCE and VECTOR_COSINE_SIMILARITY. You use them for semantic search, recommendations and duplicate detection on embeddings created by AI_EMBED.

The most important exam skill is knowing which direction is "closer". Similarity functions give larger numbers for closer vectors. Distance functions give smaller numbers for closer vectors.

Query textAI_EMBEDQuery vectorcompare withVector columnstored embeddingsVECTOR_COSINE_SIMILARITYScore per rowORDER BYTop matches

How it works

The VECTOR type

The syntax is VECTOR(<type>, <dimension>). The element type is INT (32 bit integers) or FLOAT (32 bit floating point). The dimension is a positive integer with a maximum of 4096.

CREATE TABLE docs (id INT, embedding VECTOR(FLOAT, 1024));
SELECT [0.1, 0.2, 0.3]::VECTOR(FLOAT, 3);

Important rules:

  • An array can be explicitly cast to VECTOR if its element count equals the dimension and its element type matches. A VECTOR can be cast back to an array.
  • VECTOR columns cannot be loaded or unloaded directly. Unload as an array and cast back after loading.
  • A VECTOR cannot be stored in a VARIANT column or used as a clustering key.
  • Direct comparisons such as v1 < v2 are byte wise and are not numeric, so use the similarity functions to compare vectors.

The four comparison functions

Cosine similarity and inner product give higher values for closer vectors and are sorted descending. L2 and L1 distances give lower values for closer vectors and are sorted ascending.

FunctionReturnsCloser vectors giveNotes
VECTOR_COSINE_SIMILARITYFLOAT from -1 to 1Higher valueAngle between vectors; magnitude ignored
VECTOR_INNER_PRODUCTREALHigher valueLarger for similar vectors
VECTOR_L2_DISTANCEFLOAT, 0 or moreLower valueStraight line distance; 0 means identical
VECTOR_L1_DISTANCEFLOAT, 0 or moreLower valueSum of absolute differences (Manhattan); 0 means identical

All four take two VECTOR arguments of the same element type and dimension.

SELECT id,
       VECTOR_COSINE_SIMILARITY(embedding, q.vec) AS sim
FROM docs, (SELECT AI_EMBED('snowflake-arctic-embed-l-v2.0', 'reset password')::VECTOR(FLOAT, 1024) AS vec) q
ORDER BY sim DESC
LIMIT 5;

With cosine similarity, identical vectors return 1, orthogonal vectors return 0 and opposite vectors return -1. With distances, you sort ascending (ORDER BY dist ASC) to get the nearest rows.

Choosing a function

ScenarioBest fit
Embeddings where direction matters more than lengthVECTOR_COSINE_SIMILARITY
Vectors that were normalized firstVECTOR_INNER_PRODUCT or cosine, which then agree on ranking
A true distance in space, with 0 as identicalVECTOR_L2_DISTANCE
A distance that is less sensitive to one large difference in one dimensionVECTOR_L1_DISTANCE
Exam tip

When a question says "highest score is best", the answer is cosine similarity or inner product. When it says "lowest value is best", the answer is L1 or L2 distance.

Precision

Vector functions are optimized in a way that can reduce floating point precision. Results can have a margin of error up to 1e-4, so do not test two scores for exact equality.

Watch out

VECTOR_COSINE_SIMILARITY is not supported in the Snowpark API. In Snowpark, use SQL for that function.

Where embeddings come from

AI_EMBED produces VECTOR values. Search at scale is better done by a Cortex Search service, but the vector functions are what you use when you manage the embedding column yourself.

Common mistakes

Sorting a distance descending. The largest distance is the farthest row.

Sorting a similarity ascending. The smallest similarity is the least relevant row.

Comparing vectors of different dimensions or element types. Both inputs must match.

Using < or = on vectors to compare them. That is a byte comparison, not a numeric one.

Expecting exact scores. Results have a margin of error up to 1e-4.

What to remember for the exam

  • VECTOR(INT or FLOAT, dimension), with a maximum dimension of 4096.
  • Cosine similarity is a FLOAT from -1 to 1; inner product is larger for similar vectors.
  • L1 and L2 are distances: smaller means closer, and L2 of 0 means identical.
  • All take two vectors of equal type and dimension.
  • Margin of error is up to 1e-4.
  • VECTOR_COSINE_SIMILARITY is not available in the Snowpark API.

Check yourself

  • Which functions return larger numbers for more similar vectors?

    VECTOR_COSINE_SIMILARITY and VECTOR_INNER_PRODUCT.

  • What does an L2 distance of 0 mean?

    The two vectors are identical.

  • What is the largest dimension of a VECTOR?

    4096.

Topic 9 of 26 in Snowflake Gen AI Functions

Test yourself on Snowflake Gen AI Functions

The full Snowflake GES-C02 question bank has 585 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Snowflake. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.