Skip to main content

nodedb_sql/types/
collection.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Collection and column metadata types for query planning.
4
5use super::query::EngineType;
6use crate::types_expr::SqlDataType;
7
8/// Metadata about a collection for query planning.
9#[derive(Debug, Clone)]
10pub struct CollectionInfo {
11    pub name: String,
12    pub engine: EngineType,
13    pub columns: Vec<ColumnInfo>,
14    pub primary_key: Option<String>,
15    pub has_auto_tier: bool,
16    /// Secondary indexes available for planner rewrites. Populated by the
17    /// catalog adapter from `StoredCollection.indexes`. `Building` entries
18    /// are included so the planner can see them but MUST be skipped when
19    /// choosing an index lookup — only `Ready` indexes back query rewrites.
20    pub indexes: Vec<IndexSpec>,
21    /// When `true`, this collection stores every write as an immutable
22    /// version keyed by `system_from_ms`. Enables `FOR SYSTEM_TIME AS OF`
23    /// and `FOR VALID_TIME` queries. Only meaningful for document engines
24    /// today; other engines ignore this flag.
25    pub bitemporal: bool,
26    /// Primary engine hint from the catalog.
27    pub primary: nodedb_types::PrimaryEngine,
28    /// Vector-primary configuration. `Some` only when
29    /// `primary == PrimaryEngine::Vector`.
30    pub vector_primary: Option<nodedb_types::VectorPrimaryConfig>,
31    /// How this collection's rows are distributed across vShards.
32    ///
33    /// Authoritative per-collection partition metadata. Future routing layers
34    /// read this instead of inferring distribution from engine type.
35    pub partition_strategy: nodedb_types::PartitionStrategy,
36}
37
38/// Secondary index metadata surfaced to the SQL planner.
39#[derive(Debug, Clone)]
40pub struct IndexSpec {
41    pub name: String,
42    /// Canonical field path (`$.email`, `$.user.name`, or plain column name
43    /// for strict documents — the catalog layer stores them uniformly).
44    pub field: String,
45    pub unique: bool,
46    pub case_insensitive: bool,
47    /// Build state. Only `Ready` indexes drive query rewrites.
48    pub state: IndexState,
49    /// Partial-index predicate as raw SQL text (`WHERE <expr>` body
50    /// without the keyword), or `None` for full indexes. The planner
51    /// uses this to reject rewrites whose WHERE clause doesn't entail
52    /// the predicate — matching against such a partial index would
53    /// omit rows the index didn't cover.
54    pub predicate: Option<String>,
55}
56
57/// Planner-facing index state. Mirrors the catalog variant but lives here
58/// so the SQL crate doesn't depend on `nodedb` internals.
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub enum IndexState {
61    Building,
62    Ready,
63}
64
65/// Metadata about a single column.
66#[derive(Debug, Clone)]
67pub struct ColumnInfo {
68    pub name: String,
69    pub data_type: SqlDataType,
70    pub nullable: bool,
71    pub is_primary_key: bool,
72    /// Default value expression (e.g. "UUID_V7", "ULID", "NANOID(10)", "0", "'active'").
73    pub default: Option<String>,
74    /// Raw type string as stored in the catalog (e.g. `"JSON"`, `"TEXT"`, `"FLOAT64"`).
75    /// `None` for columns synthesized by the planner (e.g. auto-injected `id`).
76    /// Columnar INSERT converters use this to reconstruct the exact `ColumnType`
77    /// so JSON / Geometry / UUID columns are not incorrectly inferred as String.
78    pub raw_type: Option<String>,
79}