Skip to main content

velesdb_core/velesql/ast/
ddl.rs

1//! DDL statement types for VelesQL.
2//!
3//! This module defines CREATE/DROP statement AST nodes.
4
5use serde::{Deserialize, Serialize};
6
7/// DDL statement.
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9#[non_exhaustive]
10pub enum DdlStatement {
11    /// CREATE COLLECTION statement.
12    CreateCollection(CreateCollectionStatement),
13    /// DROP COLLECTION statement.
14    DropCollection(DropCollectionStatement),
15    /// CREATE INDEX ON collection (field) -- secondary metadata index.
16    CreateIndex(CreateIndexStatement),
17    /// DROP INDEX ON collection (field) -- remove secondary metadata index.
18    DropIndex(DropIndexStatement),
19    /// `ANALYZE [COLLECTION] name` — compute CBO statistics.
20    Analyze(AnalyzeStatement),
21    /// `TRUNCATE [COLLECTION] name` — delete all rows.
22    Truncate(TruncateStatement),
23    /// ALTER COLLECTION name SET (options) -- modify collection settings.
24    AlterCollection(AlterCollectionStatement),
25}
26
27/// CREATE COLLECTION statement.
28#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
29pub struct CreateCollectionStatement {
30    /// Collection name.
31    pub name: String,
32    /// What kind of collection to create.
33    pub kind: CreateCollectionKind,
34}
35
36/// Kind of collection to create.
37#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
38#[non_exhaustive]
39pub enum CreateCollectionKind {
40    /// Vector collection (default).
41    Vector(VectorCollectionParams),
42    /// Graph collection with optional embeddings.
43    Graph(GraphCollectionParams),
44    /// Metadata-only collection (no vectors).
45    Metadata,
46}
47
48/// Parameters for creating a vector collection.
49#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
50pub struct VectorCollectionParams {
51    /// Vector dimension (required).
52    pub dimension: usize,
53    /// Distance metric name (resolved at execution time).
54    pub metric: String,
55    /// Storage mode: "full", "sq8", "binary", "pq".
56    pub storage: Option<String>,
57    /// HNSW `m` parameter.
58    pub m: Option<usize>,
59    /// HNSW `ef_construction` parameter.
60    pub ef_construction: Option<usize>,
61}
62
63/// Parameters for creating a graph collection.
64#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
65pub struct GraphCollectionParams {
66    /// Optional embedding dimension (None = no embeddings).
67    pub dimension: Option<usize>,
68    /// Distance metric name (required if dimension is set).
69    pub metric: Option<String>,
70    /// Schema mode.
71    pub schema_mode: GraphSchemaMode,
72}
73
74/// Graph schema mode.
75#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
76#[non_exhaustive]
77pub enum GraphSchemaMode {
78    /// No schema enforcement.
79    Schemaless,
80    /// Typed schema with node/edge definitions.
81    Typed(Vec<SchemaDefinition>),
82}
83
84/// A single schema definition (node type or edge type).
85#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
86#[non_exhaustive]
87pub enum SchemaDefinition {
88    /// Node type with properties.
89    Node {
90        /// Node type name.
91        name: String,
92        /// Property definitions: (name, type_name).
93        properties: Vec<(String, String)>,
94    },
95    /// Edge type connecting two node types.
96    Edge {
97        /// Edge type name.
98        name: String,
99        /// Source node type.
100        from_type: String,
101        /// Target node type.
102        to_type: String,
103    },
104}
105
106/// DROP COLLECTION statement.
107#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
108pub struct DropCollectionStatement {
109    /// Collection name.
110    pub name: String,
111    /// Whether IF EXISTS was specified.
112    pub if_exists: bool,
113}
114
115/// CREATE INDEX statement -- secondary metadata index on a payload field.
116#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
117pub struct CreateIndexStatement {
118    /// Target collection name.
119    pub collection: String,
120    /// Payload field to index.
121    pub field: String,
122}
123
124/// DROP INDEX statement -- remove secondary metadata index.
125#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
126pub struct DropIndexStatement {
127    /// Target collection name.
128    pub collection: String,
129    /// Payload field whose index to drop.
130    pub field: String,
131}
132
133/// ANALYZE statement -- compute CBO statistics for query optimizer.
134#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
135pub struct AnalyzeStatement {
136    /// Collection name to analyze.
137    pub collection: String,
138}
139
140/// TRUNCATE statement -- delete all rows from a collection.
141#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
142pub struct TruncateStatement {
143    /// Collection name to truncate.
144    pub collection: String,
145}
146
147/// ALTER COLLECTION SET statement -- modify collection settings at runtime.
148#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
149pub struct AlterCollectionStatement {
150    /// Collection name to alter.
151    pub collection: String,
152    /// Key-value pairs of options to set.
153    pub options: Vec<(String, String)>,
154}