littlegraph-core 0.2.1

Embedded openCypher graph database backed by Neo4j record storage
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
#![allow(dead_code)]

use std::collections::BTreeMap;

use crate::value::Value;

#[derive(Clone, Debug, PartialEq)]
pub enum Statement {
    Single(Query),
    Create(CreateStatement),
    CreateRelationship(CreateRelationshipStatement),
    Set(SetStatement),
    Foreach(ForeachStatement),
    Delete(DeleteStatement),
    Remove(RemoveStatement),
    Merge(MergeStatement),
    Union { first: Query, parts: Vec<UnionPart> },
}

#[derive(Clone, Debug, PartialEq)]
pub struct CreateStatement {
    pub node: NodePattern,
    pub returns: ReturnProjection,
}

#[derive(Clone, Debug, PartialEq)]
pub struct CreateRelationshipStatement {
    pub matches: Vec<MatchClause>,
    pub pattern: Pattern,
    pub returns: ReturnProjection,
}

#[derive(Clone, Debug, PartialEq)]
pub struct SetStatement {
    pub matches: Vec<MatchClause>,
    pub items: Vec<SetItem>,
    pub returns: ReturnProjection,
}

#[derive(Clone, Debug, PartialEq)]
pub struct ForeachStatement {
    pub matches: Vec<MatchClause>,
    pub variable: String,
    pub expression: Expression,
    pub items: Vec<SetItem>,
    pub returns: ReturnProjection,
}

#[derive(Clone, Debug, PartialEq)]
pub enum SetItem {
    Property {
        variable: String,
        key: String,
        expression: Expression,
    },
    PropertiesMerge {
        variable: String,
        expression: Expression,
    },
    PropertiesReplace {
        variable: String,
        expression: Expression,
    },
    Label {
        variable: String,
        labels: Vec<String>,
    },
}

#[derive(Clone, Debug, PartialEq)]
pub struct DeleteStatement {
    pub matches: Vec<MatchClause>,
    pub variables: Vec<String>,
    pub detach: bool,
}

#[derive(Clone, Debug, PartialEq)]
pub struct RemoveStatement {
    pub matches: Vec<MatchClause>,
    pub items: Vec<RemoveItem>,
    pub returns: ReturnProjection,
}

#[derive(Clone, Debug, PartialEq)]
pub enum RemoveItem {
    Property {
        variable: String,
        key: String,
    },
    Label {
        variable: String,
        labels: Vec<String>,
    },
}

#[derive(Clone, Debug, PartialEq)]
pub enum MergeStatement {
    Node {
        node: NodePattern,
        actions: Vec<MergeAction>,
        returns: ReturnProjection,
    },
    Relationship {
        matches: Vec<MatchClause>,
        pattern: Pattern,
        actions: Vec<MergeAction>,
        returns: ReturnProjection,
    },
}

#[derive(Clone, Debug, PartialEq)]
pub struct MergeAction {
    pub branch: MergeBranch,
    pub items: Vec<SetItem>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MergeBranch {
    OnCreate,
    OnMatch,
}

#[derive(Clone, Debug, PartialEq)]
pub struct UnionPart {
    pub all: bool,
    pub query: Query,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Query {
    pub has_initial_match: bool,
    pub optional_match: bool,
    pub pattern: Pattern,
    pub patterns: Vec<Pattern>,
    pub predicate: Option<Predicate>,
    pub extra_matches: Vec<MatchClause>,
    pub unwinds: Vec<UnwindClause>,
    pub post_unwind_matches: Vec<MatchClause>,
    pub procedure_calls: Vec<ProcedureCall>,
    pub post_procedure_unwinds: Vec<UnwindClause>,
    pub post_procedure_matches: Vec<MatchClause>,
    pub with: Option<WithProjection>,
    pub with_predicate: Option<Predicate>,
    pub with_order_by: Vec<OrderItem>,
    pub with_skip: Option<RowCount>,
    pub with_limit: Option<RowCount>,
    pub post_with_unwinds: Vec<UnwindClause>,
    pub post_with_matches: Vec<MatchClause>,
    pub post_with_procedure_calls: Vec<ProcedureCall>,
    pub post_with_procedure_unwinds: Vec<UnwindClause>,
    pub post_with_procedure_matches: Vec<MatchClause>,
    pub post_with_with: Option<WithProjection>,
    pub post_with_with_predicate: Option<Predicate>,
    pub post_with_with_order_by: Vec<OrderItem>,
    pub post_with_with_skip: Option<RowCount>,
    pub post_with_with_limit: Option<RowCount>,
    pub post_with_with_unwinds: Vec<UnwindClause>,
    pub post_with_with_matches: Vec<MatchClause>,
    pub post_with_with_procedure_calls: Vec<ProcedureCall>,
    pub post_with_with_procedure_unwinds: Vec<UnwindClause>,
    pub post_with_with_procedure_matches: Vec<MatchClause>,
    pub post_with_with_calls: Vec<CallSubquery>,
    pub calls: Vec<CallSubquery>,
    pub post_call_unwinds: Vec<UnwindClause>,
    pub post_call_matches: Vec<MatchClause>,
    pub post_call_with: Option<WithProjection>,
    pub post_call_with_predicate: Option<Predicate>,
    pub post_call_with_order_by: Vec<OrderItem>,
    pub post_call_with_skip: Option<RowCount>,
    pub post_call_with_limit: Option<RowCount>,
    pub post_call_with_unwinds: Vec<UnwindClause>,
    pub post_call_with_matches: Vec<MatchClause>,
    pub distinct: bool,
    pub returns: ReturnProjection,
    pub implicit_return: bool,
    pub order_by: Vec<OrderItem>,
    pub skip: Option<RowCount>,
    pub limit: Option<RowCount>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct ProcedureCall {
    pub name: String,
    pub arguments: Vec<Expression>,
    pub yields: Vec<ProcedureYield>,
    pub predicate: Option<Predicate>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProcedureYield {
    pub field: String,
    pub alias: Option<String>,
}

impl ProcedureYield {
    pub fn binding(&self) -> &str {
        self.alias.as_deref().unwrap_or(&self.field)
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct MatchClause {
    pub optional_match: bool,
    pub pattern: Pattern,
    pub patterns: Vec<Pattern>,
    pub predicate: Option<Predicate>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct WithProjection {
    pub distinct: bool,
    pub projection: ReturnProjection,
}

#[derive(Clone, Debug, PartialEq)]
pub struct UnwindClause {
    pub expression: Expression,
    pub variable: String,
}

#[derive(Clone, Debug, PartialEq)]
pub struct CallSubquery {
    pub imports: Vec<String>,
    pub predicate: Option<Predicate>,
    pub unwinds: Vec<UnwindClause>,
    pub matches: Vec<MatchClause>,
    pub post_match_unwinds: Vec<UnwindClause>,
    pub with: Option<WithProjection>,
    pub with_predicate: Option<Predicate>,
    pub with_order_by: Vec<OrderItem>,
    pub with_skip: Option<RowCount>,
    pub with_limit: Option<RowCount>,
    pub post_with_unwinds: Vec<UnwindClause>,
    pub post_with_matches: Vec<MatchClause>,
    pub post_with_with: Option<WithProjection>,
    pub post_with_with_predicate: Option<Predicate>,
    pub post_with_with_order_by: Vec<OrderItem>,
    pub post_with_with_skip: Option<RowCount>,
    pub post_with_with_limit: Option<RowCount>,
    pub post_with_with_unwinds: Vec<UnwindClause>,
    pub post_with_with_matches: Vec<MatchClause>,
    pub returns: ReturnProjection,
    pub order_by: Vec<OrderItem>,
    pub skip: Option<RowCount>,
    pub limit: Option<RowCount>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Pattern {
    pub variable: Option<String>,
    pub shortest_path: Option<ShortestPathMode>,
    pub start: NodePattern,
    pub chains: Vec<PatternChain>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ShortestPathMode {
    Single,
    All,
}

#[derive(Clone, Debug, PartialEq)]
pub struct PatternChain {
    pub left_variable: Option<String>,
    pub relationship: RelationshipPattern,
    pub node: NodePattern,
}

#[derive(Clone, Debug, PartialEq)]
pub struct NodePattern {
    pub variable: Option<String>,
    pub labels: Vec<String>,
    pub properties: Vec<PropertyPredicate>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct RelationshipPattern {
    pub variable: Option<String>,
    pub types: Vec<String>,
    pub direction: Direction,
    pub variable_length: bool,
    pub min_hops: usize,
    pub max_hops: Option<usize>,
    pub properties: Vec<PropertyPredicate>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Direction {
    Outgoing,
    Incoming,
    Undirected,
}

#[derive(Clone, Debug, PartialEq)]
pub struct PropertyPredicate {
    pub key: String,
    pub expression: Expression,
}

#[derive(Clone, Debug, PartialEq)]
pub enum Expression {
    Literal(Value),
    Parameter(String),
    Operand(Operand),
    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>),
    List(Vec<Expression>),
    Map(BTreeMap<String, Expression>),
    MapProjection {
        variable: String,
        items: Vec<MapProjectionItem>,
    },
    Coalesce(Vec<Expression>),
    ToLower(Box<Expression>),
    ToUpper(Box<Expression>),
    ToString(Box<Expression>),
    ToInteger(Box<Expression>),
    ToFloat(Box<Expression>),
    ToBoolean(Box<Expression>),
    ToStringOrNull(Box<Expression>),
    ToIntegerOrNull(Box<Expression>),
    ToFloatOrNull(Box<Expression>),
    ToBooleanOrNull(Box<Expression>),
    IsEmpty(Box<Expression>),
    Head(Box<Expression>),
    Last(Box<Expression>),
    Tail(Box<Expression>),
    Range {
        start: Box<Expression>,
        end: Box<Expression>,
        step: Option<Box<Expression>>,
    },
    ListIndex {
        value: Box<Expression>,
        index: Box<Expression>,
    },
    ListSlice {
        value: Box<Expression>,
        start: Option<Box<Expression>>,
        end: Option<Box<Expression>>,
    },
    Size(Box<Expression>),
    Substring {
        value: Box<Expression>,
        start: Box<Expression>,
        length: Option<Box<Expression>>,
    },
    Replace {
        value: Box<Expression>,
        search: Box<Expression>,
        replacement: Box<Expression>,
    },
    Trim {
        mode: TrimMode,
        expression: Box<Expression>,
    },
    Split {
        value: Box<Expression>,
        delimiter: Box<Expression>,
    },
    StringSlice {
        mode: StringSliceMode,
        value: Box<Expression>,
        length: Box<Expression>,
    },
    Reverse(Box<Expression>),
    Abs(Box<Expression>),
    Rounding {
        mode: RoundingMode,
        expression: Box<Expression>,
    },
    Sign(Box<Expression>),
    Sqrt(Box<Expression>),
    Round(Box<Expression>),
    Log(Box<Expression>),
    Log10(Box<Expression>),
    Exp(Box<Expression>),
    Pi,
    E,
    AngleConversion {
        mode: AngleConversionMode,
        expression: Box<Expression>,
    },
    Trigonometric {
        mode: TrigonometricMode,
        expression: Box<Expression>,
    },
    InverseTrigonometric {
        mode: InverseTrigonometricMode,
        expression: Box<Expression>,
    },
    Atan2 {
        y: Box<Expression>,
        x: Box<Expression>,
    },
    Haversin(Box<Expression>),
    ListPredicate {
        mode: ListPredicateMode,
        variable: String,
        list: Box<Expression>,
        predicate: Box<Predicate>,
    },
    ListComprehension {
        variable: String,
        list: Box<Expression>,
        predicate: Option<Box<Predicate>>,
        projection: Box<Expression>,
    },
    PatternComprehension {
        pattern: Pattern,
        predicate: Option<Box<Predicate>>,
        projection: Box<Expression>,
    },
    Reduce {
        accumulator: String,
        initial: Box<Expression>,
        variable: String,
        list: Box<Expression>,
        expression: Box<Expression>,
    },
    Case {
        alternatives: Vec<CaseAlternative>,
        else_expression: Box<Expression>,
    },
    SimpleCase {
        operand: Box<Expression>,
        alternatives: Vec<SimpleCaseAlternative>,
        else_expression: Box<Expression>,
    },
    Predicate(Box<Predicate>),
}

#[derive(Clone, Debug, PartialEq)]
pub enum MapProjectionItem {
    AllProperties,
    Property(String),
    Variable(String),
    Expression { key: String, expression: Expression },
}

#[derive(Clone, Debug, PartialEq)]
pub struct CaseAlternative {
    pub predicate: Predicate,
    pub expression: Expression,
}

#[derive(Clone, Debug, PartialEq)]
pub struct SimpleCaseAlternative {
    pub value: Expression,
    pub expression: Expression,
}

#[derive(Clone, Debug, PartialEq)]
pub enum TrimMode {
    Both,
    Left,
    Right,
}

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

#[derive(Clone, Debug, PartialEq)]
pub enum RoundingMode {
    Ceil,
    Floor,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ListPredicateMode {
    All,
    Any,
    None,
    Single,
}

#[derive(Clone, Debug, PartialEq)]
pub enum AngleConversionMode {
    Degrees,
    Radians,
}

#[derive(Clone, Debug, PartialEq)]
pub enum TrigonometricMode {
    Sin,
    Cos,
    Tan,
    Cot,
}

#[derive(Clone, Debug, PartialEq)]
pub enum InverseTrigonometricMode {
    Acos,
    Asin,
    Atan,
}

#[derive(Clone, Debug, PartialEq)]
pub enum Predicate {
    Comparison {
        left: Expression,
        operator: ComparisonOperator,
        right: Expression,
    },
    In {
        item: Expression,
        list: Expression,
    },
    StringMatch {
        left: Expression,
        operator: StringOperator,
        right: Expression,
    },
    And(Box<Predicate>, Box<Predicate>),
    Or(Box<Predicate>, Box<Predicate>),
    Xor(Box<Predicate>, Box<Predicate>),
    Not(Box<Predicate>),
    Pattern(Pattern),
    IsNull(Expression),
    IsNotNull(Expression),
    Exists(Operand),
    ExistsSubquery(Vec<MatchClause>),
    Boolean(Expression),
}

#[derive(Clone, Debug, PartialEq)]
pub enum Operand {
    Literal(Value),
    Parameter(String),
    Property {
        variable: String,
        key: String,
    },
    DynamicProperty {
        variable: String,
        key: Box<Expression>,
    },
    Variable(String),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ComparisonOperator {
    Equal,
    NotEqual,
    LessThan,
    LessThanOrEqual,
    GreaterThan,
    GreaterThanOrEqual,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StringOperator {
    StartsWith,
    EndsWith,
    Contains,
    Regex,
}

#[derive(Clone, Debug, PartialEq)]
pub struct ReturnItem {
    pub expression: ReturnExpression,
    pub alias: Option<String>,
}

#[derive(Clone, Debug, PartialEq)]
pub enum ReturnExpression {
    Star,
    Variable(String),
    Property { variable: String, key: String },
    PropertyWildcard(String),
    Scalar(Expression),
    Labels(String),
    Type(String),
    Properties(String),
    Keys(String),
    ElementId(String),
    Id(String),
    StartNode(String),
    EndNode(String),
    Nodes(String),
    Relationships(String),
    Length(String),
    CountStar,
    CountDistinctStar,
    CountVariable(String),
    CountDistinctVariable(String),
    CountProperty { variable: String, key: String },
    CountDistinctProperty { variable: String, key: String },
    CountExpression(Expression),
    CountDistinctExpression(Expression),
    SumProperty { variable: String, key: String },
    AvgProperty { variable: String, key: String },
    MinProperty { variable: String, key: String },
    MaxProperty { variable: String, key: String },
    SumExpression(Expression),
    AvgExpression(Expression),
    SumDistinctExpression(Expression),
    AvgDistinctExpression(Expression),
    MinExpression(Expression),
    MaxExpression(Expression),
    MinDistinctExpression(Expression),
    MaxDistinctExpression(Expression),
    CollectVariable(String),
    CollectProperty { variable: String, key: String },
    CollectDistinctVariable(String),
    CollectDistinctProperty { variable: String, key: String },
    CollectExpression(Expression),
    CollectDistinctExpression(Expression),
}

#[derive(Clone, Debug, PartialEq)]
pub enum ReturnProjection {
    Items(Vec<ReturnItem>),
    Star,
}

#[derive(Clone, Debug, PartialEq)]
pub struct OrderItem {
    pub item: ReturnItem,
    pub direction: OrderDirection,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OrderDirection {
    Ascending,
    Descending,
}

#[derive(Clone, Debug, PartialEq)]
pub enum RowCount {
    Literal(i64),
    Parameter(String),
    Expression(Expression),
    InvalidLiteral(String),
}