Skip to main content

hawdb_sql_syntax/
ast.rs

1// Copyright 2026 Nowledge
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::Span;
16
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub enum PgqStatement {
19    CreatePropertyGraph(CreatePropertyGraph),
20}
21
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub enum PostgresStatementSyntax {
24    CreatePropertyGraph(CreatePropertyGraph),
25    Select(Box<PostgresSelectSyntax>),
26}
27
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct PostgresSelectSyntax {
30    pub distinct: bool,
31    pub projection: Vec<LabeledExpressionSyntax>,
32    pub from: Vec<PostgresFromSyntax>,
33    pub selection: Option<ExpressionSyntax>,
34    pub group_by: Vec<ExpressionSyntax>,
35    pub having: Option<ExpressionSyntax>,
36    pub order_by: Vec<OrderBySyntax>,
37    pub limit: Option<ExpressionSyntax>,
38    pub offset: Option<ExpressionSyntax>,
39    pub locking: Option<LockingClauseSyntax>,
40    pub span: Span,
41}
42
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub struct PostgresFromSyntax {
45    pub relation: PostgresFromItemSyntax,
46    pub joins: Vec<PostgresJoinSyntax>,
47    pub span: Span,
48}
49
50#[derive(Debug, Clone, PartialEq, Eq)]
51pub enum PostgresFromItemSyntax {
52    Relation(RelationTableSyntax),
53    GraphTable(GraphTable),
54}
55
56#[derive(Debug, Clone, PartialEq, Eq)]
57pub struct PostgresJoinSyntax {
58    pub kind: PostgresJoinKind,
59    pub relation: PostgresFromItemSyntax,
60    pub condition: Option<ExpressionSyntax>,
61    pub span: Span,
62}
63
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub enum PostgresJoinKind {
66    Inner,
67    Left,
68    Cross,
69}
70
71#[derive(Debug, Clone, PartialEq, Eq)]
72pub struct OrderBySyntax {
73    pub expression: ExpressionSyntax,
74    pub direction: Option<OrderDirectionSyntax>,
75    pub nulls: Option<NullOrderSyntax>,
76    pub span: Span,
77}
78
79#[derive(Debug, Clone, Copy, PartialEq, Eq)]
80pub enum OrderDirectionSyntax {
81    Ascending,
82    Descending,
83}
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq)]
86pub enum NullOrderSyntax {
87    First,
88    Last,
89}
90
91#[derive(Debug, Clone, Copy, PartialEq, Eq)]
92pub struct LockingClauseSyntax {
93    pub strength: LockStrengthSyntax,
94    pub span: Span,
95}
96
97#[derive(Debug, Clone, Copy, PartialEq, Eq)]
98pub enum LockStrengthSyntax {
99    Share,
100    Update,
101}
102
103#[derive(Debug, Clone, PartialEq, Eq)]
104pub struct RelationTableSyntax {
105    pub name: QualifiedName,
106    pub alias: Option<TableAlias>,
107    pub span: Span,
108}
109
110#[derive(Debug, Clone, PartialEq, Eq)]
111pub struct Identifier {
112    pub span: Span,
113    pub quoted: bool,
114}
115
116#[derive(Debug, Clone, PartialEq, Eq)]
117pub struct QualifiedName {
118    pub parts: Vec<Identifier>,
119    pub span: Span,
120}
121
122#[derive(Debug, Clone, PartialEq, Eq)]
123pub struct CreatePropertyGraph {
124    pub temporary: bool,
125    pub name: QualifiedName,
126    pub vertex_tables: Vec<VertexTableDefinition>,
127    pub edge_tables: Vec<EdgeTableDefinition>,
128    pub span: Span,
129}
130
131#[derive(Debug, Clone, PartialEq, Eq)]
132pub struct VertexTableDefinition {
133    pub table: QualifiedName,
134    pub alias: Option<Identifier>,
135    pub key: Vec<Identifier>,
136    pub exposure: Option<ElementExposure>,
137    pub span: Span,
138}
139
140#[derive(Debug, Clone, PartialEq, Eq)]
141pub struct EdgeTableDefinition {
142    pub table: QualifiedName,
143    pub alias: Option<Identifier>,
144    pub key: Vec<Identifier>,
145    pub source: EdgeEndpoint,
146    pub destination: EdgeEndpoint,
147    pub exposure: Option<ElementExposure>,
148    pub span: Span,
149}
150
151#[derive(Debug, Clone, PartialEq, Eq)]
152pub struct EdgeEndpoint {
153    pub key: Vec<Identifier>,
154    pub vertex: Identifier,
155    pub vertex_key: Vec<Identifier>,
156    pub span: Span,
157}
158
159#[derive(Debug, Clone, PartialEq, Eq)]
160pub struct ElementExposure {
161    pub labels: Vec<ElementLabelExposure>,
162    pub span: Span,
163}
164
165#[derive(Debug, Clone, PartialEq, Eq)]
166pub struct ElementLabelExposure {
167    pub label: ElementLabel,
168    pub properties: PropertyExposure,
169    pub span: Span,
170}
171
172#[derive(Debug, Clone, PartialEq, Eq)]
173pub enum ElementLabel {
174    Implicit,
175    Default,
176    Named(Identifier),
177}
178
179#[derive(Debug, Clone, PartialEq, Eq)]
180pub enum PropertyExposure {
181    AllColumns,
182    NoProperties,
183    Expressions(Vec<LabeledExpressionSyntax>),
184}
185
186#[derive(Debug, Clone, PartialEq, Eq)]
187pub struct LabeledExpressionSyntax {
188    pub expression: ExpressionSyntax,
189    pub alias: Option<Identifier>,
190    pub span: Span,
191}
192
193#[derive(Debug, Clone, PartialEq, Eq)]
194pub struct GraphTable {
195    pub graph: QualifiedName,
196    pub pattern: GraphPatternSyntax,
197    pub columns: Vec<GraphTableColumn>,
198    pub alias: Option<TableAlias>,
199    pub span: Span,
200}
201
202#[derive(Debug, Clone, PartialEq, Eq)]
203pub struct GraphPatternSyntax {
204    pub paths: Vec<GraphPathSyntax>,
205    pub predicate: Option<ExpressionSyntax>,
206    pub span: Span,
207}
208
209#[derive(Debug, Clone, PartialEq, Eq)]
210pub struct GraphPathSyntax {
211    pub factors: Vec<GraphPathFactorSyntax>,
212    pub span: Span,
213}
214
215#[derive(Debug, Clone, PartialEq, Eq)]
216pub struct GraphPathFactorSyntax {
217    pub primary: GraphPathPrimarySyntax,
218    pub quantifier: Option<GraphPatternQuantifier>,
219    pub span: Span,
220}
221
222#[derive(Debug, Clone, PartialEq, Eq)]
223pub enum GraphPathPrimarySyntax {
224    Vertex(GraphElementPatternSyntax),
225    Edge(GraphEdgePatternSyntax),
226    Parenthesized {
227        path: Box<GraphPathSyntax>,
228        predicate: Option<ExpressionSyntax>,
229        span: Span,
230    },
231}
232
233#[derive(Debug, Clone, PartialEq, Eq)]
234pub struct GraphElementPatternSyntax {
235    pub variable: Option<Identifier>,
236    pub labels: Vec<Identifier>,
237    pub predicate: Option<ExpressionSyntax>,
238    pub span: Span,
239}
240
241#[derive(Debug, Clone, PartialEq, Eq)]
242pub struct GraphEdgePatternSyntax {
243    pub direction: GraphEdgeDirection,
244    pub variable: Option<Identifier>,
245    pub labels: Vec<Identifier>,
246    pub predicate: Option<ExpressionSyntax>,
247    pub abbreviated: bool,
248    pub span: Span,
249}
250
251#[derive(Debug, Clone, Copy, PartialEq, Eq)]
252pub enum GraphEdgeDirection {
253    Left,
254    Right,
255    Any,
256}
257
258#[derive(Debug, Clone, PartialEq, Eq)]
259pub struct GraphPatternQuantifier {
260    pub min: u32,
261    pub max: u32,
262    pub span: Span,
263}
264
265#[derive(Debug, Clone, PartialEq, Eq)]
266pub struct ExpressionSyntax {
267    pub kind: ExpressionKindSyntax,
268    pub span: Span,
269}
270
271#[derive(Debug, Clone, PartialEq, Eq)]
272pub enum ExpressionKindSyntax {
273    Column(QualifiedName),
274    Wildcard(Option<QualifiedName>),
275    Parameter(u32),
276    Literal(LiteralSyntax),
277    TypedString {
278        data_type: Identifier,
279        value: Span,
280    },
281    Unary {
282        operator: UnaryOperatorSyntax,
283        expression: Box<ExpressionSyntax>,
284    },
285    Binary {
286        left: Box<ExpressionSyntax>,
287        operator: BinaryOperatorSyntax,
288        right: Box<ExpressionSyntax>,
289    },
290    IsNull {
291        expression: Box<ExpressionSyntax>,
292        negated: bool,
293    },
294    InList {
295        expression: Box<ExpressionSyntax>,
296        values: Vec<ExpressionSyntax>,
297        negated: bool,
298    },
299    Between {
300        expression: Box<ExpressionSyntax>,
301        low: Box<ExpressionSyntax>,
302        high: Box<ExpressionSyntax>,
303        negated: bool,
304    },
305    Function {
306        name: QualifiedName,
307        arguments: Vec<ExpressionSyntax>,
308        distinct: bool,
309    },
310    Cast {
311        expression: Box<ExpressionSyntax>,
312        data_type: QualifiedName,
313    },
314    Collate {
315        expression: Box<ExpressionSyntax>,
316        collation: QualifiedName,
317    },
318    Parenthesized(Box<ExpressionSyntax>),
319}
320
321#[derive(Debug, Clone, PartialEq, Eq)]
322pub enum LiteralSyntax {
323    Null,
324    Boolean(bool),
325    Number(Span),
326    String(Span),
327}
328
329#[derive(Debug, Clone, Copy, PartialEq, Eq)]
330pub enum UnaryOperatorSyntax {
331    Not,
332    Plus,
333    Minus,
334}
335
336#[derive(Debug, Clone, Copy, PartialEq, Eq)]
337pub enum BinaryOperatorSyntax {
338    Or,
339    And,
340    Equal,
341    NotEqual,
342    Less,
343    LessOrEqual,
344    Greater,
345    GreaterOrEqual,
346    Concat,
347    Add,
348    Subtract,
349    Multiply,
350    Divide,
351    Modulo,
352}
353
354#[derive(Debug, Clone, PartialEq, Eq)]
355pub struct GraphTableColumn {
356    pub expression: ExpressionSyntax,
357    pub alias: Option<Identifier>,
358    pub span: Span,
359}
360
361#[derive(Debug, Clone, PartialEq, Eq)]
362pub struct TableAlias {
363    pub name: Identifier,
364    pub columns: Vec<Identifier>,
365    pub span: Span,
366}