Skip to main content

omnigraph_compiler/query/
ast.rs

1pub const NOW_PARAM_NAME: &str = "__nanograph_now";
2
3#[derive(Debug, Clone)]
4pub struct QueryFile {
5    pub queries: Vec<QueryDecl>,
6}
7
8#[derive(Debug, Clone)]
9pub struct QueryDecl {
10    pub name: String,
11    pub description: Option<String>,
12    pub instruction: Option<String>,
13    pub params: Vec<Param>,
14    pub match_clause: Vec<Clause>,
15    pub return_clause: Vec<Projection>,
16    pub order_clause: Vec<Ordering>,
17    pub limit: Option<u64>,
18    pub mutations: Vec<Mutation>,
19}
20
21#[derive(Debug, Clone)]
22pub struct Param {
23    pub name: String,
24    pub type_name: String,
25    pub nullable: bool,
26}
27
28#[derive(Debug, Clone)]
29pub enum Clause {
30    Binding(Binding),
31    Traversal(Traversal),
32    Filter(Filter),
33    Negation(Vec<Clause>),
34}
35
36#[derive(Debug, Clone)]
37pub struct Binding {
38    pub variable: String,
39    pub type_name: String,
40    pub prop_matches: Vec<PropMatch>,
41}
42
43#[derive(Debug, Clone)]
44pub struct PropMatch {
45    pub prop_name: String,
46    pub value: MatchValue,
47}
48
49#[derive(Debug, Clone)]
50pub enum MatchValue {
51    Literal(Literal),
52    Variable(String),
53    Now,
54}
55
56#[derive(Debug, Clone)]
57pub struct Traversal {
58    pub src: String,
59    pub edge_name: String,
60    pub dst: String,
61    pub min_hops: u32,
62    pub max_hops: Option<u32>,
63}
64
65#[derive(Debug, Clone)]
66pub struct Filter {
67    pub left: Expr,
68    pub op: CompOp,
69    pub right: Expr,
70}
71
72#[derive(Debug, Clone, Copy, PartialEq, Eq)]
73pub enum CompOp {
74    Eq,
75    Ne,
76    Gt,
77    Lt,
78    Ge,
79    Le,
80    Contains,
81}
82
83impl std::fmt::Display for CompOp {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        match self {
86            Self::Eq => write!(f, "="),
87            Self::Ne => write!(f, "!="),
88            Self::Gt => write!(f, ">"),
89            Self::Lt => write!(f, "<"),
90            Self::Ge => write!(f, ">="),
91            Self::Le => write!(f, "<="),
92            Self::Contains => write!(f, "contains"),
93        }
94    }
95}
96
97#[derive(Debug, Clone)]
98pub enum Expr {
99    Now,
100    PropAccess {
101        variable: String,
102        property: String,
103    },
104    Nearest {
105        variable: String,
106        property: String,
107        query: Box<Expr>,
108    },
109    Search {
110        field: Box<Expr>,
111        query: Box<Expr>,
112    },
113    Fuzzy {
114        field: Box<Expr>,
115        query: Box<Expr>,
116        max_edits: Option<Box<Expr>>,
117    },
118    MatchText {
119        field: Box<Expr>,
120        query: Box<Expr>,
121    },
122    Bm25 {
123        field: Box<Expr>,
124        query: Box<Expr>,
125    },
126    Rrf {
127        primary: Box<Expr>,
128        secondary: Box<Expr>,
129        k: Option<Box<Expr>>,
130    },
131    Variable(String),
132    Literal(Literal),
133    Aggregate {
134        func: AggFunc,
135        arg: Box<Expr>,
136    },
137    AliasRef(String),
138}
139
140#[derive(Debug, Clone, Copy, PartialEq, Eq)]
141pub enum AggFunc {
142    Count,
143    Sum,
144    Avg,
145    Min,
146    Max,
147}
148
149impl std::fmt::Display for AggFunc {
150    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
151        match self {
152            Self::Count => write!(f, "count"),
153            Self::Sum => write!(f, "sum"),
154            Self::Avg => write!(f, "avg"),
155            Self::Min => write!(f, "min"),
156            Self::Max => write!(f, "max"),
157        }
158    }
159}
160
161#[derive(Debug, Clone)]
162pub enum Literal {
163    Null,
164    String(String),
165    Integer(i64),
166    Float(f64),
167    Bool(bool),
168    Date(String),
169    DateTime(String),
170    List(Vec<Literal>),
171}
172
173#[derive(Debug, Clone)]
174pub struct Projection {
175    pub expr: Expr,
176    pub alias: Option<String>,
177}
178
179#[derive(Debug, Clone)]
180pub struct Ordering {
181    pub expr: Expr,
182    pub descending: bool,
183}
184
185#[derive(Debug, Clone)]
186pub enum Mutation {
187    Insert(InsertMutation),
188    Update(UpdateMutation),
189    Delete(DeleteMutation),
190}
191
192#[derive(Debug, Clone)]
193pub struct InsertMutation {
194    pub type_name: String,
195    pub assignments: Vec<MutationAssignment>,
196}
197
198#[derive(Debug, Clone)]
199pub struct UpdateMutation {
200    pub type_name: String,
201    pub assignments: Vec<MutationAssignment>,
202    pub predicate: MutationPredicate,
203}
204
205#[derive(Debug, Clone)]
206pub struct DeleteMutation {
207    pub type_name: String,
208    pub predicate: MutationPredicate,
209}
210
211#[derive(Debug, Clone)]
212pub struct MutationAssignment {
213    pub property: String,
214    pub value: MatchValue,
215}
216
217#[derive(Debug, Clone)]
218pub struct MutationPredicate {
219    pub property: String,
220    pub op: CompOp,
221    pub value: MatchValue,
222}