Skip to main content

surrealdb_expr/expr/
index_kind.rs

1//! The parsed index-parameter clause of a DEFINE INDEX statement.
2//!
3//! Mirrors the catalog's `Index` with the COUNT condition held as a parsed
4//! expression rather than canonical text; lowering to a definition renders it
5//! through the catalog's stored-text funnel.
6
7use surrealdb_types::{SqlFormat, ToSql};
8
9use crate::expr::Cond;
10use crate::sql;
11use crate::sql::index::{DiskAnnParams, FullTextParams, HnswParams};
12
13#[derive(Clone, Debug, Default, Eq, PartialEq, Hash)]
14pub enum Index {
15	/// (Basic) non unique
16	#[default]
17	Idx,
18	/// Unique index
19	Uniq,
20	/// HNSW index for distance-based metrics
21	Hnsw(HnswParams),
22	/// Index with Full-Text search capabilities
23	FullText(FullTextParams),
24	/// Count index
25	Count(Option<Cond>),
26	/// DiskANN index for distance-based metrics
27	DiskAnn(DiskAnnParams),
28}
29
30impl From<sql::index::Index> for Index {
31	fn from(v: sql::index::Index) -> Self {
32		match v {
33			sql::index::Index::Idx => Self::Idx,
34			sql::index::Index::Uniq => Self::Uniq,
35			sql::index::Index::Hnsw(p) => Self::Hnsw(p),
36			sql::index::Index::FullText(p) => Self::FullText(p),
37			sql::index::Index::Count(c) => Self::Count(c.map(Into::into)),
38			sql::index::Index::DiskAnn(p) => Self::DiskAnn(p),
39		}
40	}
41}
42
43impl From<Index> for sql::index::Index {
44	fn from(v: Index) -> Self {
45		match v {
46			Index::Idx => Self::Idx,
47			Index::Uniq => Self::Uniq,
48			Index::Hnsw(p) => Self::Hnsw(p),
49			Index::FullText(p) => Self::FullText(p),
50			Index::Count(c) => Self::Count(c.map(Into::into)),
51			Index::DiskAnn(p) => Self::DiskAnn(p),
52		}
53	}
54}
55
56impl ToSql for Index {
57	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
58		let stmt: sql::index::Index = self.clone().into();
59		stmt.fmt_sql(f, fmt);
60	}
61}