Home / File/ vector.ts — drizzle-orm Source File

vector.ts — drizzle-orm Source File

Architecture documentation for vector.ts, a typescript file in the drizzle-orm codebase. 4 imports, 0 dependents.

File typescript DrizzleORM SQLDialects 4 imports 7 functions

Entity Profile

Dependency Diagram

graph LR
  d155802d_6bf7_144f_94a8_70b957517dac["vector.ts"]
  99347ab2_b1a1_faf4_e37c_7643e4b2eb8a["sql.ts"]
  d155802d_6bf7_144f_94a8_70b957517dac --> 99347ab2_b1a1_faf4_e37c_7643e4b2eb8a
  eb432528_5e12_9f5b_6ed4_4aeab03024d8["sql"]
  d155802d_6bf7_144f_94a8_70b957517dac --> eb432528_5e12_9f5b_6ed4_4aeab03024d8
  05f0a280_d0c9_693a_a4bf_83cc671012d2["column.ts"]
  d155802d_6bf7_144f_94a8_70b957517dac --> 05f0a280_d0c9_693a_a4bf_83cc671012d2
  d31ad410_b8fc_5d88_a194_70aa7419676a["query-builder.ts"]
  d155802d_6bf7_144f_94a8_70b957517dac --> d31ad410_b8fc_5d88_a194_70aa7419676a
  style d155802d_6bf7_144f_94a8_70b957517dac fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { AnyColumn } from '~/column.ts';
import type { TypedQueryBuilder } from '~/query-builders/query-builder.ts';
import { type SQL, sql, type SQLWrapper } from '../sql.ts';

function toSql(value: number[] | string[]): string {
	return JSON.stringify(value);
}

/**
 * Used in sorting and in querying, if used in sorting,
 * this specifies that the given column or expression should be sorted in an order
 * that minimizes the L2 distance to the given value.
 * If used in querying, this specifies that it should return the L2 distance
 * between the given column or expression and the given value.
 *
 * ## Examples
 *
 * ```ts
 * // Sort cars by embedding similarity
 * // to the given embedding
 * db.select().from(cars)
 *   .orderBy(l2Distance(cars.embedding, embedding));
 * ```
 *
 * ```ts
 * // Select distance of cars and embedding
 * // to the given embedding
 * db.select({distance: l2Distance(cars.embedding, embedding)}).from(cars)
 * ```
 */
export function l2Distance(
	column: SQLWrapper | AnyColumn,
	value: number[] | string[] | TypedQueryBuilder<any> | string,
): SQL {
	if (Array.isArray(value)) {
		return sql`${column} <-> ${toSql(value)}`;
	}
	return sql`${column} <-> ${value}`;
}

/**
 * L1 distance is one of the possible distance measures between two probability distribution vectors and it is
 * calculated as the sum of the absolute differences.
 * The smaller the distance between the observed probability vectors, the higher the accuracy of the synthetic data
 *
 * ## Examples
 *
 * ```ts
 * // Sort cars by embedding similarity
 * // to the given embedding
 * db.select().from(cars)
 *   .orderBy(l1Distance(cars.embedding, embedding));
 * ```
 *
 * ```ts
 * // Select distance of cars and embedding
 * // to the given embedding
 * db.select({distance: l1Distance(cars.embedding, embedding)}).from(cars)
 * ```
 */
// ... (119 more lines)

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does vector.ts do?
vector.ts is a source file in the drizzle-orm codebase, written in typescript. It belongs to the DrizzleORM domain, SQLDialects subdomain.
What functions are defined in vector.ts?
vector.ts defines 7 function(s): cosineDistance, hammingDistance, innerProduct, jaccardDistance, l1Distance, l2Distance, toSql.
What does vector.ts depend on?
vector.ts imports 4 module(s): column.ts, query-builder.ts, sql, sql.ts.
Where is vector.ts in the architecture?
vector.ts is located at drizzle-orm/src/sql/functions/vector.ts (domain: DrizzleORM, subdomain: SQLDialects, directory: drizzle-orm/src/sql/functions).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free