drasi-query-ast 0.3.3

Drasi Core Abstract Syntax Tree
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
// Copyright 2024 The Drasi Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::BTreeMap;
use std::hash::Hasher;
use std::sync::Arc;

#[derive(Debug, Clone, PartialEq)]
pub struct Query {
    pub parts: Vec<QueryPart>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct QueryPart {
    pub match_clauses: Vec<MatchClause>,
    pub where_clauses: Vec<Expression>,
    pub return_clause: ProjectionClause,
}

impl Default for QueryPart {
    fn default() -> Self {
        Self {
            match_clauses: Vec::new(),
            where_clauses: Vec::new(),
            return_clause: ProjectionClause::Item(Vec::new()),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct MatchClause {
    pub start: NodeMatch,
    pub path: Vec<(RelationMatch, NodeMatch)>,
    pub optional: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ProjectionClause {
    Item(Vec<Expression>),
    GroupBy {
        grouping: Vec<Expression>,
        aggregates: Vec<Expression>,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub struct SetClause {
    pub name: Arc<str>,
    pub key: Arc<str>,
    pub value: Expression,
}

#[derive(Debug, Clone, PartialEq)]
pub enum CreateClause {
    CreateNode {
        name: Option<Arc<str>>,
        label: Arc<str>,
        properties: Vec<(Arc<str>, Expression)>,
    },
    CreateEdge {
        name: Option<Arc<str>>,
        label: Arc<str>,
        origin: Arc<str>,
        target: Arc<str>,
        properties: Vec<(Arc<str>, Expression)>,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub struct Annotation {
    pub name: Option<Arc<str>>,
}

impl Annotation {
    #[allow(dead_code)]
    pub fn new(name: Arc<str>) -> Self {
        Self { name: Some(name) }
    }

    #[allow(dead_code)]
    pub fn empty() -> Self {
        Self { name: None }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct NodeMatch {
    pub annotation: Annotation,
    pub labels: Vec<Arc<str>>,
    pub property_predicates: Vec<Expression>,
}

impl NodeMatch {
    #[allow(dead_code)]
    pub fn new(
        annotation: Annotation,
        labels: Vec<Arc<str>>,
        property_predicates: Vec<Expression>,
    ) -> Self {
        Self {
            annotation,
            labels,
            property_predicates,
        }
    }

    #[allow(dead_code)]
    pub fn empty() -> Self {
        Self {
            annotation: Annotation::empty(),
            labels: vec![],
            property_predicates: vec![],
        }
    }

    pub fn with_annotation(annotation: Annotation, label: Arc<str>) -> Self {
        Self {
            annotation,
            labels: vec![label],
            property_predicates: vec![],
        }
    }

    pub fn without_label(annotation: Annotation) -> Self {
        Self {
            annotation,
            labels: vec![],
            property_predicates: vec![],
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Direction {
    Left,
    Right,
    Either,
}

#[derive(Debug, Clone, PartialEq)]
pub struct VariableLengthMatch {
    pub min_hops: Option<i64>,
    pub max_hops: Option<i64>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct RelationMatch {
    pub direction: Direction,
    pub annotation: Annotation,
    pub variable_length: Option<VariableLengthMatch>,
    pub labels: Vec<Arc<str>>,
    pub property_predicates: Vec<Expression>,
}

impl RelationMatch {
    pub fn either(
        annotation: Annotation,
        labels: Vec<Arc<str>>,
        property_predicates: Vec<Expression>,
        variable_length: Option<VariableLengthMatch>,
    ) -> Self {
        Self {
            direction: Direction::Either,
            annotation,
            labels,
            property_predicates,
            variable_length,
        }
    }

    pub fn left(
        annotation: Annotation,
        labels: Vec<Arc<str>>,
        property_predicates: Vec<Expression>,
        variable_length: Option<VariableLengthMatch>,
    ) -> Self {
        Self {
            direction: Direction::Left,
            annotation,
            labels,
            property_predicates,
            variable_length,
        }
    }

    pub fn right(
        annotation: Annotation,
        labels: Vec<Arc<str>>,
        property_predicates: Vec<Expression>,
        variable_length: Option<VariableLengthMatch>,
    ) -> Self {
        Self {
            direction: Direction::Right,
            annotation,
            labels,
            property_predicates,
            variable_length,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum Literal {
    Integer(i64),
    Real(f64),
    Boolean(bool),
    Text(Arc<str>),
    Date(Arc<str>),
    LocalTime(Arc<str>),
    ZonedTime(Arc<str>),
    LocalDateTime(Arc<str>),
    ZonedDateTime(Arc<str>),
    Duration(Arc<str>),
    Object(Vec<(Arc<str>, Literal)>),
    Expression(Box<Expression>),
    Null,
}

impl std::hash::Hash for Literal {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            Literal::Integer(v) => v.hash(state),
            Literal::Real(v) => v.to_bits().hash(state),
            Literal::Boolean(v) => v.hash(state),
            Literal::Text(v) => v.hash(state),
            Literal::Date(v) => v.hash(state),
            Literal::LocalTime(v) => v.hash(state),
            Literal::ZonedTime(v) => v.hash(state),
            Literal::LocalDateTime(v) => v.hash(state),
            Literal::ZonedDateTime(v) => v.hash(state),
            Literal::Duration(v) => v.hash(state),
            Literal::Object(v) => v.hash(state),
            Literal::Expression(v) => v.hash(state),
            Literal::Null => state.write_u8(0),
        }
    }
}

impl Eq for Literal {}

pub trait ParentExpression {
    fn get_children(&self) -> Vec<&Expression>;
}

#[derive(Debug, Clone, PartialEq, Hash, Eq)]
pub enum Expression {
    UnaryExpression(UnaryExpression),
    BinaryExpression(BinaryExpression),
    FunctionExpression(FunctionExpression),
    CaseExpression(CaseExpression),
    ListExpression(ListExpression),
    ObjectExpression(ObjectExpression), //do we really need this?
    IteratorExpression(IteratorExpression),
}

impl ParentExpression for Expression {
    fn get_children(&self) -> Vec<&Expression> {
        match self {
            Expression::UnaryExpression(expr) => expr.get_children(),
            Expression::BinaryExpression(expr) => expr.get_children(),
            Expression::FunctionExpression(expr) => expr.get_children(),
            Expression::CaseExpression(expr) => expr.get_children(),
            Expression::ListExpression(expr) => expr.get_children(),
            Expression::ObjectExpression(expr) => expr.get_children(),
            Expression::IteratorExpression(expr) => expr.get_children(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Hash, Eq)]
pub enum UnaryExpression {
    Not(Box<Expression>),
    Exists(Box<Expression>),
    IsNull(Box<Expression>),
    IsNotNull(Box<Expression>),
    Literal(Literal),
    Property {
        name: Arc<str>,
        key: Arc<str>,
    },
    ExpressionProperty {
        exp: Box<Expression>,
        key: Arc<str>,
    },
    Parameter(Arc<str>),
    Identifier(Arc<str>),
    Variable {
        name: Arc<str>,
        value: Box<Expression>,
    },
    Alias {
        source: Box<Expression>,
        alias: Arc<str>,
    },
    ListRange {
        //i64 instead of Expression?
        start_bound: Option<Box<Expression>>,
        end_bound: Option<Box<Expression>>,
    },
}

impl UnaryExpression {
    pub fn literal(value: Literal) -> Expression {
        Expression::UnaryExpression(UnaryExpression::Literal(value))
    }

    pub fn parameter(name: Arc<str>) -> Expression {
        Expression::UnaryExpression(UnaryExpression::Parameter(name))
    }

    pub fn property(name: Arc<str>, key: Arc<str>) -> Expression {
        Expression::UnaryExpression(UnaryExpression::Property { name, key })
    }

    pub fn expression_property(exp: Expression, key: Arc<str>) -> Expression {
        Expression::UnaryExpression(UnaryExpression::ExpressionProperty {
            exp: Box::new(exp),
            key,
        })
    }

    pub fn alias(source: Expression, alias: Arc<str>) -> Expression {
        Expression::UnaryExpression(Self::Alias {
            source: Box::new(source),
            alias,
        })
    }

    pub fn not(cond: Expression) -> Expression {
        Expression::UnaryExpression(Self::Not(Box::new(cond)))
    }

    pub fn ident(ident: &str) -> Expression {
        Expression::UnaryExpression(Self::Identifier(ident.into()))
    }

    pub fn is_null(expr: Expression) -> Expression {
        Expression::UnaryExpression(Self::IsNull(Box::new(expr)))
    }

    pub fn variable(name: Arc<str>, value: Expression) -> Expression {
        Expression::UnaryExpression(Self::Variable {
            name,
            value: Box::new(value),
        })
    }

    pub fn is_not_null(expr: Expression) -> Expression {
        Expression::UnaryExpression(Self::IsNotNull(Box::new(expr)))
    }
    pub fn list_range(
        start_bound: Option<Expression>,
        end_bound: Option<Expression>,
    ) -> Expression {
        Expression::UnaryExpression(Self::ListRange {
            start_bound: start_bound.map(Box::new),
            end_bound: end_bound.map(Box::new),
        })
    }
}

impl ParentExpression for UnaryExpression {
    fn get_children(&self) -> Vec<&Expression> {
        match self {
            UnaryExpression::Not(expr) => vec![expr],
            UnaryExpression::Exists(expr) => vec![expr],
            UnaryExpression::IsNull(expr) => vec![expr],
            UnaryExpression::IsNotNull(expr) => vec![expr],
            UnaryExpression::Literal(_) => Vec::new(),
            UnaryExpression::Property { .. } => Vec::new(),
            UnaryExpression::Parameter(_) => Vec::new(),
            UnaryExpression::Identifier(_) => Vec::new(),
            UnaryExpression::Variable { name: _, value } => vec![value],
            UnaryExpression::Alias { source, .. } => vec![source],
            UnaryExpression::ExpressionProperty { .. } => Vec::new(),
            UnaryExpression::ListRange { .. } => Vec::new(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Hash, Eq)]
pub enum BinaryExpression {
    And(Box<Expression>, Box<Expression>),
    Or(Box<Expression>, Box<Expression>),

    Eq(Box<Expression>, Box<Expression>),
    Ne(Box<Expression>, Box<Expression>),
    Lt(Box<Expression>, Box<Expression>),
    Le(Box<Expression>, Box<Expression>),
    Gt(Box<Expression>, Box<Expression>),
    Ge(Box<Expression>, Box<Expression>),
    In(Box<Expression>, Box<Expression>),

    Add(Box<Expression>, Box<Expression>),
    Subtract(Box<Expression>, Box<Expression>),
    Multiply(Box<Expression>, Box<Expression>),
    Divide(Box<Expression>, Box<Expression>),
    Modulo(Box<Expression>, Box<Expression>),
    Exponent(Box<Expression>, Box<Expression>),
    HasLabel(Box<Expression>, Box<Expression>),
    Index(Box<Expression>, Box<Expression>),
}

impl BinaryExpression {
    pub fn and(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::And(Box::new(a), Box::new(b)))
    }

    pub fn or(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::Or(Box::new(a), Box::new(b)))
    }

    pub fn eq(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::Eq(Box::new(a), Box::new(b)))
    }

    pub fn ne(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::Ne(Box::new(a), Box::new(b)))
    }

    pub fn lt(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::Lt(Box::new(a), Box::new(b)))
    }

    pub fn le(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::Le(Box::new(a), Box::new(b)))
    }

    pub fn gt(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::Gt(Box::new(a), Box::new(b)))
    }

    pub fn in_(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::In(Box::new(a), Box::new(b)))
    }

    pub fn ge(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::Ge(Box::new(a), Box::new(b)))
    }

    pub fn add(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::Add(Box::new(a), Box::new(b)))
    }

    pub fn subtract(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::Subtract(Box::new(a), Box::new(b)))
    }

    pub fn multiply(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::Multiply(Box::new(a), Box::new(b)))
    }

    pub fn divide(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::Divide(Box::new(a), Box::new(b)))
    }

    pub fn modulo(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::Modulo(Box::new(a), Box::new(b)))
    }

    pub fn exponent(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::Exponent(Box::new(a), Box::new(b)))
    }

    pub fn has_label(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::HasLabel(Box::new(a), Box::new(b)))
    }

    pub fn index(a: Expression, b: Expression) -> Expression {
        Expression::BinaryExpression(Self::Index(Box::new(a), Box::new(b)))
    }
}

impl ParentExpression for BinaryExpression {
    fn get_children(&self) -> Vec<&Expression> {
        match self {
            BinaryExpression::And(a, b) => vec![a, b],
            BinaryExpression::Or(a, b) => vec![a, b],
            BinaryExpression::Eq(a, b) => vec![a, b],
            BinaryExpression::Ne(a, b) => vec![a, b],
            BinaryExpression::Lt(a, b) => vec![a, b],
            BinaryExpression::Le(a, b) => vec![a, b],
            BinaryExpression::Gt(a, b) => vec![a, b],
            BinaryExpression::Ge(a, b) => vec![a, b],
            BinaryExpression::In(a, b) => vec![a, b],
            BinaryExpression::Add(a, b) => vec![a, b],
            BinaryExpression::Subtract(a, b) => vec![a, b],
            BinaryExpression::Multiply(a, b) => vec![a, b],
            BinaryExpression::Divide(a, b) => vec![a, b],
            BinaryExpression::Modulo(a, b) => vec![a, b],
            BinaryExpression::Exponent(a, b) => vec![a, b],
            BinaryExpression::HasLabel(a, b) => vec![a, b],
            BinaryExpression::Index(a, b) => vec![a, b],
        }
    }
}

#[derive(Debug, Clone, PartialEq, Hash, Eq)]
pub struct FunctionExpression {
    pub name: Arc<str>,
    pub args: Vec<Expression>,
    pub position_in_query: usize,
}

impl FunctionExpression {
    pub fn function(name: Arc<str>, args: Vec<Expression>, position_in_query: usize) -> Expression {
        Expression::FunctionExpression(FunctionExpression {
            name,
            args,
            position_in_query,
        })
    }

    pub fn eq_ignore_position_in_query(&self, other: &Self) -> bool {
        self.name == other.name && self.args == other.args
    }
}

impl ParentExpression for FunctionExpression {
    fn get_children(&self) -> Vec<&Expression> {
        self.args.iter().collect()
    }
}

#[derive(Debug, Clone, PartialEq, Hash, Eq)]
pub struct CaseExpression {
    pub match_: Option<Box<Expression>>,
    pub when: Vec<(Expression, Expression)>,
    pub else_: Option<Box<Expression>>,
}

impl CaseExpression {
    pub fn case(
        match_: Option<Expression>,
        when: Vec<(Expression, Expression)>,
        else_: Option<Expression>,
    ) -> Expression {
        Expression::CaseExpression(CaseExpression {
            match_: match_.map(Box::new),
            when,
            else_: else_.map(Box::new),
        })
    }
}

impl ParentExpression for CaseExpression {
    fn get_children(&self) -> Vec<&Expression> {
        let mut children = Vec::new();
        if let Some(match_) = &self.match_ {
            children.push(match_.as_ref());
        }
        for (when, then) in &self.when {
            children.push(when);
            children.push(then);
        }
        if let Some(else_) = &self.else_ {
            children.push(else_);
        }
        children
    }
}

#[derive(Debug, Clone, PartialEq, Hash, Eq)]
pub struct ObjectExpression {
    pub elements: BTreeMap<Arc<str>, Expression>,
}

impl ObjectExpression {
    pub fn object_from_vec(elements: Vec<(Arc<str>, Expression)>) -> Expression {
        let mut map = BTreeMap::new();
        for (key, value) in elements {
            map.insert(key, value);
        }
        Expression::ObjectExpression(ObjectExpression { elements: map })
    }

    pub fn object(elements: BTreeMap<Arc<str>, Expression>) -> Expression {
        Expression::ObjectExpression(ObjectExpression { elements })
    }
}

impl ParentExpression for ObjectExpression {
    fn get_children(&self) -> Vec<&Expression> {
        let keys: Vec<_> = self.elements.values().clone().collect();

        keys
    }
}

#[derive(Debug, Clone, PartialEq, Hash, Eq)]
pub struct ListExpression {
    pub elements: Vec<Expression>,
}

impl ListExpression {
    pub fn list(elements: Vec<Expression>) -> Expression {
        Expression::ListExpression(ListExpression { elements })
    }
}

impl ParentExpression for ListExpression {
    fn get_children(&self) -> Vec<&Expression> {
        let mut children = Vec::new();
        for element in &self.elements {
            children.push(element);
        }

        children
    }
}

#[derive(Debug, Clone, PartialEq, Hash, Eq)]
pub struct IteratorExpression {
    pub item_identifier: Arc<str>,
    pub list_expression: Box<Expression>,
    pub filter: Option<Box<Expression>>,
    pub map_expression: Option<Box<Expression>>,
}

impl IteratorExpression {
    pub fn map(
        item_identifier: Arc<str>,
        list_expression: Expression,
        map_expression: Expression,
    ) -> Expression {
        Expression::IteratorExpression(IteratorExpression {
            item_identifier,
            list_expression: Box::new(list_expression),
            filter: None,
            map_expression: Some(Box::new(map_expression)),
        })
    }

    pub fn map_with_filter(
        item_identifier: Arc<str>,
        list_expression: Expression,
        map_expression: Expression,
        filter: Expression,
    ) -> Expression {
        Expression::IteratorExpression(IteratorExpression {
            item_identifier,
            list_expression: Box::new(list_expression),
            filter: Some(Box::new(filter)),
            map_expression: Some(Box::new(map_expression)),
        })
    }

    pub fn iterator(item_identifier: Arc<str>, list_expression: Expression) -> Expression {
        Expression::IteratorExpression(IteratorExpression {
            item_identifier,
            list_expression: Box::new(list_expression),
            filter: None,
            map_expression: None,
        })
    }

    pub fn iterator_with_filter(
        item_identifier: Arc<str>,
        list_expression: Expression,
        filter: Expression,
    ) -> Expression {
        Expression::IteratorExpression(IteratorExpression {
            item_identifier,
            list_expression: Box::new(list_expression),
            filter: Some(Box::new(filter)),
            map_expression: None,
        })
    }
}

impl ParentExpression for IteratorExpression {
    fn get_children(&self) -> Vec<&Expression> {
        let mut children = Vec::new();
        children.push(&*self.list_expression);
        if let Some(filter) = &self.filter {
            children.push(filter);
        }
        children
    }
}