Skip to main content

alopex_sql/ast/
ddl.rs

1use super::dml::Select;
2use super::expr::Expr;
3use super::span::{Span, Spanned};
4use serde::{Deserialize, Serialize};
5
6/// A Skulk-owned continuous aggregate definition carried by the SQL parser.
7///
8/// Alopex owns the wire representation but does not execute this statement.
9/// The generic-host behavior is added separately after every host boundary is
10/// extension-safe.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12#[serde(deny_unknown_fields)]
13pub struct CreateContinuousAggregate {
14    pub name: String,
15    pub name_span: Span,
16    #[serde(with = "crate::nim_bridge::continuous_aggregate_select_wire")]
17    pub query: Select,
18    pub options: Vec<ContinuousAggregateOption>,
19    pub span: Span,
20}
21
22/// One ordered option in a continuous aggregate definition.
23#[derive(Debug, Clone, Serialize, Deserialize)]
24#[serde(deny_unknown_fields)]
25pub struct ContinuousAggregateOption {
26    pub key: String,
27    pub key_span: Span,
28    pub value: String,
29    pub value_span: Span,
30    pub span: Span,
31}
32
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct CreateTable {
35    pub if_not_exists: bool,
36    pub name: String,
37    pub columns: Vec<ColumnDef>,
38    pub constraints: Vec<TableConstraint>,
39    pub with_options: Vec<IndexOption>,
40    pub span: Span,
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize)]
44pub struct ColumnDef {
45    pub name: String,
46    pub data_type: DataType,
47    pub constraints: Vec<ColumnConstraint>,
48    pub span: Span,
49}
50
51#[derive(Debug, Clone, Serialize, Deserialize)]
52#[serde(tag = "variant")]
53pub enum DataType {
54    Integer,
55    Int,
56    BigInt,
57    Float,
58    Double,
59    Text,
60    Blob,
61    Boolean,
62    Bool,
63    Timestamp,
64    Vector {
65        dimension: u32,
66        metric: Option<VectorMetric>,
67    },
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
71pub enum VectorMetric {
72    Cosine,
73    L2,
74    Inner,
75}
76
77#[derive(Debug, Clone, Serialize, Deserialize)]
78#[serde(tag = "variant")]
79// `Expr` grew with the issue #148 aggregate clauses; boxing `Default.value`
80// would churn every construction/pattern site of a rarely-instantiated
81// variant for no measurable gain.
82#[allow(clippy::large_enum_variant)]
83pub enum ColumnConstraint {
84    NotNull { span: Span },
85    PrimaryKey { span: Span },
86    Unique { span: Span },
87    Default { value: Expr, span: Span },
88}
89
90#[derive(Debug, Clone, Serialize, Deserialize)]
91#[serde(tag = "variant")]
92pub enum TableConstraint {
93    PrimaryKey { columns: Vec<String>, span: Span },
94}
95
96#[derive(Debug, Clone, Serialize, Deserialize)]
97pub struct DropTable {
98    pub if_exists: bool,
99    pub name: String,
100    pub span: Span,
101}
102
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct CreateIndex {
105    pub if_not_exists: bool,
106    pub name: String,
107    pub table: String,
108    pub column: String,
109    pub method: Option<IndexMethod>,
110    pub options: Vec<IndexOption>,
111    pub span: Span,
112}
113
114#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
115pub enum IndexMethod {
116    BTree,
117    Hnsw,
118}
119
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct IndexOption {
122    pub key: String,
123    pub value: String,
124    pub span: Span,
125}
126
127#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct DropIndex {
129    pub if_exists: bool,
130    pub name: String,
131    pub span: Span,
132}
133
134impl Spanned for CreateTable {
135    fn span(&self) -> Span {
136        self.span
137    }
138}
139
140impl Spanned for CreateContinuousAggregate {
141    fn span(&self) -> Span {
142        self.span
143    }
144}
145
146impl Spanned for ContinuousAggregateOption {
147    fn span(&self) -> Span {
148        self.span
149    }
150}
151
152impl Spanned for ColumnDef {
153    fn span(&self) -> Span {
154        self.span
155    }
156}
157
158impl Spanned for ColumnConstraint {
159    fn span(&self) -> Span {
160        match self {
161            ColumnConstraint::NotNull { span }
162            | ColumnConstraint::PrimaryKey { span }
163            | ColumnConstraint::Unique { span }
164            | ColumnConstraint::Default { span, .. } => *span,
165        }
166    }
167}
168
169impl Spanned for TableConstraint {
170    fn span(&self) -> Span {
171        match self {
172            TableConstraint::PrimaryKey { span, .. } => *span,
173        }
174    }
175}
176
177impl Spanned for DropTable {
178    fn span(&self) -> Span {
179        self.span
180    }
181}
182
183impl Spanned for CreateIndex {
184    fn span(&self) -> Span {
185        self.span
186    }
187}
188
189impl Spanned for IndexOption {
190    fn span(&self) -> Span {
191        self.span
192    }
193}
194
195impl Spanned for DropIndex {
196    fn span(&self) -> Span {
197        self.span
198    }
199}