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    Date,
65    Time,
66    Interval,
67    Decimal {
68        precision: u8,
69        scale: u8,
70    },
71    Json,
72    Array {
73        element: Box<DataType>,
74    },
75    Map {
76        key: Box<DataType>,
77        value: Box<DataType>,
78    },
79    Struct {
80        fields: Vec<StructField>,
81    },
82    Vector {
83        dimension: u32,
84        metric: Option<VectorMetric>,
85    },
86}
87
88#[derive(Debug, Clone, Serialize, Deserialize)]
89pub struct StructField {
90    pub name: String,
91    pub data_type: DataType,
92}
93
94#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
95pub enum VectorMetric {
96    Cosine,
97    L2,
98    Inner,
99}
100
101#[derive(Debug, Clone, Serialize, Deserialize)]
102#[serde(tag = "variant")]
103// `Expr` grew with the issue #148 aggregate clauses; boxing `Default.value`
104// would churn every construction/pattern site of a rarely-instantiated
105// variant for no measurable gain.
106#[allow(clippy::large_enum_variant)]
107pub enum ColumnConstraint {
108    NotNull { span: Span },
109    PrimaryKey { span: Span },
110    Unique { span: Span },
111    Default { value: Expr, span: Span },
112}
113
114#[derive(Debug, Clone, Serialize, Deserialize)]
115#[serde(tag = "variant")]
116pub enum TableConstraint {
117    PrimaryKey { columns: Vec<String>, span: Span },
118}
119
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct DropTable {
122    pub if_exists: bool,
123    pub name: String,
124    pub span: Span,
125}
126
127#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct CreateIndex {
129    pub if_not_exists: bool,
130    pub name: String,
131    pub table: String,
132    pub column: String,
133    pub method: Option<IndexMethod>,
134    pub options: Vec<IndexOption>,
135    pub span: Span,
136}
137
138#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
139pub enum IndexMethod {
140    BTree,
141    Hnsw,
142    Fts,
143}
144
145#[derive(Debug, Clone, Serialize, Deserialize)]
146pub struct IndexOption {
147    pub key: String,
148    pub value: String,
149    pub span: Span,
150}
151
152#[derive(Debug, Clone, Serialize, Deserialize)]
153pub struct DropIndex {
154    pub if_exists: bool,
155    pub name: String,
156    pub span: Span,
157}
158
159impl Spanned for CreateTable {
160    fn span(&self) -> Span {
161        self.span
162    }
163}
164
165impl Spanned for CreateContinuousAggregate {
166    fn span(&self) -> Span {
167        self.span
168    }
169}
170
171impl Spanned for ContinuousAggregateOption {
172    fn span(&self) -> Span {
173        self.span
174    }
175}
176
177impl Spanned for ColumnDef {
178    fn span(&self) -> Span {
179        self.span
180    }
181}
182
183impl Spanned for ColumnConstraint {
184    fn span(&self) -> Span {
185        match self {
186            ColumnConstraint::NotNull { span }
187            | ColumnConstraint::PrimaryKey { span }
188            | ColumnConstraint::Unique { span }
189            | ColumnConstraint::Default { span, .. } => *span,
190        }
191    }
192}
193
194impl Spanned for TableConstraint {
195    fn span(&self) -> Span {
196        match self {
197            TableConstraint::PrimaryKey { span, .. } => *span,
198        }
199    }
200}
201
202impl Spanned for DropTable {
203    fn span(&self) -> Span {
204        self.span
205    }
206}
207
208impl Spanned for CreateIndex {
209    fn span(&self) -> Span {
210        self.span
211    }
212}
213
214impl Spanned for IndexOption {
215    fn span(&self) -> Span {
216        self.span
217    }
218}
219
220impl Spanned for DropIndex {
221    fn span(&self) -> Span {
222        self.span
223    }
224}