gqlite 1.5.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/ext/Cargo.toml +12 -6
  3. data/ext/db-index/Cargo.toml +29 -0
  4. data/ext/db-index/src/hnsw/error.rs +66 -0
  5. data/ext/db-index/src/hnsw/graph_store.rs +272 -0
  6. data/ext/db-index/src/hnsw/id.rs +36 -0
  7. data/ext/db-index/src/hnsw/implementation.rs +1517 -0
  8. data/ext/db-index/src/hnsw/kernels.rs +1228 -0
  9. data/ext/db-index/src/hnsw/metric.rs +244 -0
  10. data/ext/db-index/src/hnsw/scalar.rs +72 -0
  11. data/ext/db-index/src/hnsw/simple_store.rs +140 -0
  12. data/ext/db-index/src/hnsw/store.rs +472 -0
  13. data/ext/db-index/src/hnsw/vector.rs +105 -0
  14. data/ext/db-index/src/hnsw/vectors.rs +568 -0
  15. data/ext/db-index/src/hnsw.rs +42 -0
  16. data/ext/db-index/src/lib.rs +3 -0
  17. data/ext/gqlitedb/Cargo.toml +19 -8
  18. data/ext/gqlitedb/benches/common/pokec.rs +62 -2
  19. data/ext/gqlitedb/benches/pokec_divan.rs +66 -2
  20. data/ext/gqlitedb/benches/pokec_iai.rs +60 -3
  21. data/ext/gqlitedb/release.toml +2 -2
  22. data/ext/gqlitedb/src/aggregators/arithmetic.rs +3 -1
  23. data/ext/gqlitedb/src/aggregators/containers.rs +1 -1
  24. data/ext/gqlitedb/src/aggregators/stats.rs +21 -12
  25. data/ext/gqlitedb/src/capi.rs +20 -24
  26. data/ext/gqlitedb/src/compiler/expression_analyser.rs +28 -20
  27. data/ext/gqlitedb/src/compiler/variables_manager.rs +104 -38
  28. data/ext/gqlitedb/src/compiler.rs +506 -227
  29. data/ext/gqlitedb/src/connection.rs +151 -11
  30. data/ext/gqlitedb/src/consts.rs +1 -2
  31. data/ext/gqlitedb/src/error.rs +123 -61
  32. data/ext/gqlitedb/src/functions/common.rs +64 -0
  33. data/ext/gqlitedb/src/functions/containers.rs +2 -46
  34. data/ext/gqlitedb/src/functions/edge.rs +1 -1
  35. data/ext/gqlitedb/src/functions/math.rs +87 -15
  36. data/ext/gqlitedb/src/functions/node.rs +1 -1
  37. data/ext/gqlitedb/src/functions/path.rs +48 -11
  38. data/ext/gqlitedb/src/functions/scalar.rs +47 -4
  39. data/ext/gqlitedb/src/functions/string.rs +123 -1
  40. data/ext/gqlitedb/src/functions/value.rs +1 -1
  41. data/ext/gqlitedb/src/functions.rs +165 -28
  42. data/ext/gqlitedb/src/graph.rs +1 -9
  43. data/ext/gqlitedb/src/interpreter/evaluators.rs +968 -130
  44. data/ext/gqlitedb/src/interpreter/instructions.rs +39 -2
  45. data/ext/gqlitedb/src/lib.rs +5 -4
  46. data/ext/gqlitedb/src/parser/gql.pest +1 -1
  47. data/ext/gqlitedb/src/planner.rs +329 -0
  48. data/ext/gqlitedb/src/prelude.rs +3 -3
  49. data/ext/gqlitedb/src/store/pgrx.rs +1 -1
  50. data/ext/gqlitedb/src/store/postgres.rs +742 -16
  51. data/ext/gqlitedb/src/store/redb/hnsw_store.rs +702 -0
  52. data/ext/gqlitedb/src/store/redb/index.rs +274 -0
  53. data/ext/gqlitedb/src/store/redb.rs +1268 -113
  54. data/ext/gqlitedb/src/store/sqlbase/sqlmetadata.rs +28 -0
  55. data/ext/gqlitedb/src/store/sqlbase/sqlstore.rs +103 -0
  56. data/ext/gqlitedb/src/store/sqlbase.rs +146 -16
  57. data/ext/gqlitedb/src/store/sqlite.rs +576 -14
  58. data/ext/gqlitedb/src/store/vector_extract.rs +56 -0
  59. data/ext/gqlitedb/src/store.rs +140 -29
  60. data/ext/gqlitedb/src/tests/compiler.rs +207 -10
  61. data/ext/gqlitedb/src/tests/connection/postgres.rs +38 -3
  62. data/ext/gqlitedb/src/tests/connection/redb.rs +23 -0
  63. data/ext/gqlitedb/src/tests/connection/sqlite.rs +31 -0
  64. data/ext/gqlitedb/src/tests/connection.rs +61 -1
  65. data/ext/gqlitedb/src/tests/evaluators.rs +162 -7
  66. data/ext/gqlitedb/src/tests/parser.rs +54 -23
  67. data/ext/gqlitedb/src/tests/planner.rs +511 -0
  68. data/ext/gqlitedb/src/tests/store/postgres.rs +7 -0
  69. data/ext/gqlitedb/src/tests/store/redb.rs +8 -0
  70. data/ext/gqlitedb/src/tests/store/sqlite.rs +8 -0
  71. data/ext/gqlitedb/src/tests/store/vector_index/postgres.rs +182 -0
  72. data/ext/gqlitedb/src/tests/store/vector_index/redb.rs +386 -0
  73. data/ext/gqlitedb/src/tests/store/vector_index/sqlite.rs +166 -0
  74. data/ext/gqlitedb/src/tests/store/vector_index.rs +2313 -0
  75. data/ext/gqlitedb/src/tests/store.rs +78 -14
  76. data/ext/gqlitedb/src/tests/templates/ast.rs +92 -16
  77. data/ext/gqlitedb/src/tests/templates/programs.rs +14 -7
  78. data/ext/gqlitedb/src/tests.rs +15 -9
  79. data/ext/gqlitedb/src/utils.rs +6 -1
  80. data/ext/gqlitedb/src/value/compare.rs +61 -3
  81. data/ext/gqlitedb/src/value.rs +136 -7
  82. data/ext/gqlitedb/templates/sql/postgres/metadata_delete.sql +1 -0
  83. data/ext/gqlitedb/templates/sql/postgres/node_select.sql +1 -1
  84. data/ext/gqlitedb/templates/sql/sqlite/metadata_delete.sql +1 -0
  85. data/ext/gqliterb/src/lib.rs +53 -8
  86. data/ext/gqlparser/Cargo.toml +25 -0
  87. data/ext/gqlparser/README.MD +9 -0
  88. data/ext/gqlparser/benches/pokec_divan.rs +34 -0
  89. data/ext/gqlparser/src/common.rs +69 -0
  90. data/ext/gqlparser/src/gqls/ast.rs +69 -0
  91. data/ext/gqlparser/src/gqls/constraint.rs +79 -0
  92. data/ext/gqlparser/src/gqls/error.rs +22 -0
  93. data/ext/gqlparser/src/gqls/parser.rs +813 -0
  94. data/ext/gqlparser/src/gqls/prelude.rs +8 -0
  95. data/ext/gqlparser/src/gqls/properties.rs +115 -0
  96. data/ext/gqlparser/src/gqls/resolve.rs +207 -0
  97. data/ext/gqlparser/src/gqls.rs +265 -0
  98. data/ext/gqlparser/src/lib.rs +7 -0
  99. data/ext/gqlparser/src/oc/ast.rs +680 -0
  100. data/ext/gqlparser/src/oc/error.rs +172 -0
  101. data/ext/gqlparser/src/oc/lexer.rs +429 -0
  102. data/ext/gqlparser/src/oc/parser/tests.rs +2284 -0
  103. data/ext/gqlparser/src/oc/parser.rs +2005 -0
  104. data/ext/gqlparser/src/oc.rs +33 -0
  105. data/ext/gqlparser/src/prelude.rs +3 -0
  106. data/ext/graphcore/Cargo.toml +1 -0
  107. data/ext/graphcore/src/error.rs +26 -0
  108. data/ext/graphcore/src/graph.rs +177 -51
  109. data/ext/graphcore/src/lib.rs +4 -2
  110. data/ext/graphcore/src/open_cypher.rs +12 -0
  111. data/ext/graphcore/src/table.rs +50 -2
  112. data/ext/graphcore/src/timestamp.rs +127 -104
  113. data/ext/graphcore/src/value/tensor.rs +739 -0
  114. data/ext/graphcore/src/value/value_map.rs +1 -1
  115. data/ext/graphcore/src/value.rs +343 -19
  116. metadata +93 -28
  117. data/ext/gqlitedb/src/parser/ast.rs +0 -604
  118. data/ext/gqlitedb/src/parser/parser_impl.rs +0 -1213
  119. data/ext/gqlitedb/src/parser.rs +0 -4
@@ -1,1213 +0,0 @@
1
- use std::str::FromStr;
2
-
3
- use pest::{
4
- pratt_parser::{Assoc, Op, PrattParser},
5
- Parser,
6
- };
7
- use pest_derive::Parser;
8
-
9
- use crate::prelude::*;
10
- use parser::ast;
11
-
12
- trait TryNext: Iterator
13
- {
14
- fn try_next(&mut self) -> Result<Self::Item>
15
- {
16
- self
17
- .next()
18
- .ok_or_else(|| error::InternalError::MissingElementIterator.into())
19
- }
20
- }
21
-
22
- fn remove_hex_prefix(string: &str) -> String
23
- {
24
- if &string[0..1] == "-"
25
- {
26
- format!("-{}", &string[3..])
27
- }
28
- else
29
- {
30
- string[2..].into()
31
- }
32
- }
33
-
34
- fn validate_float(value: f64, text: &str) -> Result<f64>
35
- {
36
- if value.is_finite()
37
- {
38
- Ok(value)
39
- }
40
- else
41
- {
42
- Err(
43
- CompileTimeError::FloatingPointOverflow {
44
- text: text.to_owned(),
45
- }
46
- .into(),
47
- )
48
- }
49
- }
50
-
51
- impl<T: Iterator> TryNext for T {}
52
-
53
- #[derive(Parser)]
54
- #[grammar = "parser/gql.pest"]
55
- pub(crate) struct GQLParser;
56
-
57
- pub(crate) struct AstBuilder
58
- {
59
- pratt: PrattParser<Rule>,
60
- var_ids: ast::VariableIdentifiers,
61
- }
62
-
63
- impl AstBuilder
64
- {
65
- fn build_pair(&self, pair: pest::iterators::Pair<Rule>) -> Result<(String, ast::Expression)>
66
- {
67
- let mut it = pair.into_inner();
68
- let k = it.try_next()?;
69
- let v = self.build_expression(it.try_next()?.into_inner())?;
70
- Ok((k.as_str().to_string(), v))
71
- }
72
-
73
- fn build_expression(&self, pairs: pest::iterators::Pairs<Rule>) -> Result<ast::Expression>
74
- {
75
- self
76
- .pratt
77
- .map_primary(|primary| self.build_expression_primary(primary))
78
- .map_prefix(|op, rhs| match op.as_rule()
79
- {
80
- Rule::negation => Ok(ast::Negation { value: rhs? }.into()),
81
- Rule::not => Ok(ast::LogicalNegation { value: rhs? }.into()),
82
- unknown_expression => Err(
83
- error::InternalError::UnxpectedExpression(
84
- "build_expression/map_prefix",
85
- format!("{unknown_expression:?}"),
86
- )
87
- .into(),
88
- ),
89
- })
90
- .map_postfix(|lhs, op| match op.as_rule()
91
- {
92
- Rule::is_null => Ok(ast::IsNull { value: lhs? }.into()),
93
- Rule::is_not_null => Ok(ast::IsNotNull { value: lhs? }.into()),
94
- Rule::member_access =>
95
- {
96
- let it = op.into_inner();
97
- Ok(ast::Expression::MemberAccess(Box::new(ast::MemberAccess {
98
- left: lhs?,
99
- path: it
100
- .map(|el| match el.as_rule()
101
- {
102
- Rule::ident => el.as_str().to_string(),
103
- Rule::string_literal => el.into_inner().as_str().to_string(),
104
- _ => todo!(),
105
- })
106
- .collect(),
107
- })))
108
- }
109
- Rule::index_access =>
110
- {
111
- let mut it = op.into_inner();
112
-
113
- Ok(ast::Expression::IndexAccess(Box::new(ast::IndexAccess {
114
- left: lhs?,
115
- index: it
116
- .next()
117
- .map(|el| self.build_expression(el.into_inner()))
118
- .unwrap()?,
119
- })))
120
- }
121
- Rule::range_access =>
122
- {
123
- let mut it = op.into_inner();
124
- let start = Some(
125
- it.next()
126
- .map(|el| self.build_expression(el.into_inner()))
127
- .unwrap()?,
128
- );
129
- let end = it
130
- .next()
131
- .map(|el| self.build_expression(el.into_inner()))
132
- .transpose()?;
133
-
134
- Ok(ast::Expression::RangeAccess(Box::new(ast::RangeAccess {
135
- left: lhs?,
136
- start,
137
- end,
138
- })))
139
- }
140
- Rule::range_access_to =>
141
- {
142
- let mut it = op.into_inner();
143
- let end = Some(
144
- it.next()
145
- .map(|el| self.build_expression(el.into_inner()))
146
- .unwrap()?,
147
- );
148
-
149
- Ok(ast::Expression::RangeAccess(Box::new(ast::RangeAccess {
150
- left: lhs?,
151
- start: None,
152
- end,
153
- })))
154
- }
155
- unknown_expression => Err(
156
- error::InternalError::UnxpectedExpression(
157
- "build_expression/map_postfix",
158
- format!("{unknown_expression:?}"),
159
- )
160
- .into(),
161
- ),
162
- })
163
- .map_infix(|lhs, op, rhs| match op.as_rule()
164
- {
165
- Rule::addition => Ok(
166
- ast::Addition {
167
- left: lhs?,
168
- right: rhs?,
169
- }
170
- .into(),
171
- ),
172
- Rule::subtraction => Ok(
173
- ast::Subtraction {
174
- left: lhs?,
175
- right: rhs?,
176
- }
177
- .into(),
178
- ),
179
- Rule::multiplication => Ok(
180
- ast::Multiplication {
181
- left: lhs?,
182
- right: rhs?,
183
- }
184
- .into(),
185
- ),
186
- Rule::division => Ok(
187
- ast::Division {
188
- left: lhs?,
189
- right: rhs?,
190
- }
191
- .into(),
192
- ),
193
- Rule::modulo => Ok(
194
- ast::Modulo {
195
- left: lhs?,
196
- right: rhs?,
197
- }
198
- .into(),
199
- ),
200
- Rule::exponent => Ok(
201
- ast::Exponent {
202
- left: lhs?,
203
- right: rhs?,
204
- }
205
- .into(),
206
- ),
207
- Rule::or => Ok(
208
- ast::LogicalOr {
209
- left: lhs?,
210
- right: rhs?,
211
- }
212
- .into(),
213
- ),
214
- Rule::and => Ok(
215
- ast::LogicalAnd {
216
- left: lhs?,
217
- right: rhs?,
218
- }
219
- .into(),
220
- ),
221
- Rule::xor => Ok(
222
- ast::LogicalXor {
223
- left: lhs?,
224
- right: rhs?,
225
- }
226
- .into(),
227
- ),
228
- Rule::equal => Ok(
229
- ast::RelationalEqual {
230
- left: lhs?,
231
- right: rhs?,
232
- }
233
- .into(),
234
- ),
235
- Rule::different => Ok(
236
- ast::RelationalDifferent {
237
- left: lhs?,
238
- right: rhs?,
239
- }
240
- .into(),
241
- ),
242
- Rule::inferior => Ok(
243
- ast::RelationalInferior {
244
- left: lhs?,
245
- right: rhs?,
246
- }
247
- .into(),
248
- ),
249
- Rule::superior => Ok(
250
- ast::RelationalSuperior {
251
- left: lhs?,
252
- right: rhs?,
253
- }
254
- .into(),
255
- ),
256
- Rule::inferior_equal => Ok(
257
- ast::RelationalInferiorEqual {
258
- left: lhs?,
259
- right: rhs?,
260
- }
261
- .into(),
262
- ),
263
- Rule::superior_equal => Ok(
264
- ast::RelationalSuperiorEqual {
265
- left: lhs?,
266
- right: rhs?,
267
- }
268
- .into(),
269
- ),
270
- Rule::not_in => Ok(
271
- ast::RelationalNotIn {
272
- left: lhs?,
273
- right: rhs?,
274
- }
275
- .into(),
276
- ),
277
- Rule::in_ => Ok(
278
- ast::RelationalIn {
279
- left: lhs?,
280
- right: rhs?,
281
- }
282
- .into(),
283
- ),
284
- unknown_expression => Err(
285
- error::InternalError::UnxpectedExpression(
286
- "build_expression/map_postfix",
287
- format!("{unknown_expression:?}"),
288
- )
289
- .into(),
290
- ),
291
- })
292
- .parse(pairs)
293
- }
294
-
295
- fn build_expression_primary(&self, pair: pest::iterators::Pair<Rule>) -> Result<ast::Expression>
296
- {
297
- match pair.as_rule()
298
- {
299
- Rule::expression_term =>
300
- {
301
- let pair = pair.into_inner().try_next()?;
302
- match pair.as_rule()
303
- {
304
- Rule::null_lit => Ok(ast::Expression::Value(ast::Value {
305
- value: value::Value::Null,
306
- })),
307
- Rule::true_lit => Ok(ast::Expression::Value(ast::Value {
308
- value: value::Value::Boolean(true),
309
- })),
310
- Rule::false_lit => Ok(ast::Expression::Value(ast::Value {
311
- value: value::Value::Boolean(false),
312
- })),
313
- Rule::int => Ok(ast::Expression::Value(ast::Value {
314
- value: {
315
- value::Value::Integer(
316
- i64::from_str(pair.as_str())
317
- .map_err(|e| error::parse_int_error_to_compile_error(pair.as_str(), e))?,
318
- )
319
- },
320
- })),
321
- Rule::octa_int => Ok(ast::Expression::Value(ast::Value {
322
- value: {
323
- value::Value::Integer(
324
- i64::from_str_radix(&remove_hex_prefix(pair.as_str()), 8)
325
- .map_err(|e| error::parse_int_error_to_compile_error(pair.as_str(), e))?,
326
- )
327
- },
328
- })),
329
- Rule::hexa_int => Ok(ast::Expression::Value(ast::Value {
330
- value: {
331
- value::Value::Integer(
332
- i64::from_str_radix(&remove_hex_prefix(pair.as_str()), 16)
333
- .map_err(|e| error::parse_int_error_to_compile_error(pair.as_str(), e))?,
334
- )
335
- },
336
- })),
337
- Rule::num => Ok(ast::Expression::Value(ast::Value {
338
- value: value::Value::Float(validate_float(pair.as_str().parse()?, pair.as_str())?),
339
- })),
340
- Rule::ident => Ok(ast::Expression::Variable(ast::Variable {
341
- identifier: self.var_ids.create_variable_from_name(pair.as_str()),
342
- })),
343
- Rule::parameter => Ok(ast::Expression::Parameter(ast::Parameter {
344
- name: pair.as_str().to_string(),
345
- })),
346
- Rule::array => Ok(ast::Expression::Array(ast::Array {
347
- array: pair
348
- .into_inner()
349
- .map(|pair| self.build_expression(pair.into_inner()))
350
- .collect::<Result<Vec<ast::Expression>>>()?,
351
- })),
352
- Rule::map => self.build_map(pair),
353
- Rule::string_literal => Ok(ast::Expression::Value(ast::Value {
354
- value: value::Value::String(pair.into_inner().try_next()?.as_str().to_string()),
355
- })),
356
- Rule::function_call =>
357
- {
358
- let mut it = pair.into_inner();
359
- let function_name = it
360
- .next()
361
- .ok_or_else(|| error::InternalError::MissingFunctionName)?
362
- .as_str();
363
- Ok(ast::Expression::FunctionCall(ast::FunctionCall {
364
- name: function_name.to_string(),
365
- arguments: it
366
- .map(|pair| self.build_expression(pair.into_inner()))
367
- .collect::<Result<_>>()?,
368
- }))
369
- }
370
- Rule::function_star =>
371
- {
372
- let mut it = pair.into_inner();
373
- let function_name = it
374
- .next()
375
- .ok_or_else(|| error::InternalError::MissingFunctionName)?
376
- .as_str();
377
- if function_name.to_lowercase() != "count"
378
- {
379
- Err(error::CompileTimeError::UnknownFunction {
380
- name: function_name.to_owned(),
381
- })?;
382
- }
383
-
384
- Ok(ast::Expression::FunctionCall(ast::FunctionCall {
385
- name: "count".into(),
386
- arguments: vec![ast::Expression::Value(ast::Value { value: 0.into() })],
387
- }))
388
- }
389
- Rule::parenthesised_expression =>
390
- {
391
- let mut it = pair.into_inner();
392
- self.build_expression(it.try_next()?.into_inner())
393
- }
394
- Rule::label_check_expression =>
395
- {
396
- let it = pair.into_inner();
397
- Ok(ast::Expression::FunctionCall(ast::FunctionCall {
398
- name: "has_labels".into(),
399
- arguments: it
400
- .enumerate()
401
- .map(|(i, pair)| {
402
- if i == 0
403
- {
404
- ast::Expression::Variable(ast::Variable {
405
- identifier: self.var_ids.create_variable_from_name(pair.as_str()),
406
- })
407
- }
408
- else
409
- {
410
- ast::Expression::Value(ast::Value {
411
- value: value::Value::String(pair.as_str().into()),
412
- })
413
- }
414
- })
415
- .collect(),
416
- }))
417
- }
418
- unknown_expression => Err(
419
- error::InternalError::UnxpectedExpression(
420
- "build_expression_term",
421
- format!("{unknown_expression:?}"),
422
- )
423
- .into(),
424
- ),
425
- }
426
- }
427
- unknown_expression => Err(
428
- error::InternalError::UnxpectedExpression(
429
- "build_expression_term",
430
- format!("{unknown_expression:?}"),
431
- )
432
- .into(),
433
- ),
434
- }
435
- }
436
-
437
- fn build_map(&self, pair: pest::iterators::Pair<Rule>) -> Result<ast::Expression>
438
- {
439
- match pair.as_rule()
440
- {
441
- Rule::map => Ok(ast::Expression::Map({
442
- let map = pair
443
- .into_inner()
444
- .map(|k_v_pair| self.build_pair(k_v_pair))
445
- .collect::<Result<_>>()?;
446
- ast::Map { map }
447
- })),
448
- unknown_expression => Err(
449
- error::InternalError::UnxpectedExpression("build_map", format!("{unknown_expression:?}"))
450
- .into(),
451
- ),
452
- }
453
- }
454
-
455
- fn build_modifiers(&self, pair: pest::iterators::Pair<Rule>) -> Result<ast::Modifiers>
456
- {
457
- let mut skip = None;
458
- let mut limit = None;
459
- let mut order_by = None;
460
- for subpair in pair.into_inner()
461
- {
462
- match subpair.as_rule()
463
- {
464
- Rule::limit =>
465
- {
466
- let mut subpair = subpair.into_inner();
467
- subpair.try_next()?; // eat limit_kw
468
-
469
- limit = Some(self.build_expression(subpair.try_next()?.into_inner())?)
470
- }
471
- Rule::skip =>
472
- {
473
- let mut subpair = subpair.into_inner();
474
- subpair.try_next()?; // eat limit_kw
475
-
476
- skip = Some(self.build_expression(subpair.try_next()?.into_inner())?)
477
- }
478
- Rule::order_by =>
479
- {
480
- let mut subpair = subpair.into_inner();
481
- subpair.try_next()?; // eat order_by_kw
482
-
483
- order_by = Some(ast::OrderBy {
484
- expressions: subpair
485
- .map(|r| match r.as_rule()
486
- {
487
- Rule::order_by_asc_expression => Ok(ast::OrderByExpression {
488
- asc: true,
489
- expression: self.build_expression(r.into_inner().try_next()?.into_inner())?,
490
- }),
491
- Rule::order_by_desc_expression => Ok(ast::OrderByExpression {
492
- asc: false,
493
- expression: self.build_expression(r.into_inner().try_next()?.into_inner())?,
494
- }),
495
- _ => Err::<_, crate::prelude::ErrorType>(
496
- InternalError::UnexpectedPair {
497
- context: "build_modifiers/order_by",
498
- pair: format!("{:#?}", r),
499
- }
500
- .into(),
501
- ),
502
- })
503
- .collect::<Result<_>>()?,
504
- })
505
- }
506
- _ => Err(InternalError::UnexpectedPair {
507
- context: "build_modifiers",
508
- pair: format!("{:#?}", subpair),
509
- })?,
510
- }
511
- }
512
- Ok(ast::Modifiers {
513
- skip,
514
- limit,
515
- order_by,
516
- })
517
- }
518
-
519
- fn build_named_expression(
520
- &self,
521
- pair: pest::iterators::Pair<Rule>,
522
- ) -> Result<ast::NamedExpression>
523
- {
524
- match pair.as_rule()
525
- {
526
- Rule::named_expression =>
527
- {
528
- let mut inner = pair.into_inner();
529
- match inner.len()
530
- {
531
- 1 =>
532
- {
533
- let expr = inner.try_next()?;
534
- Ok(ast::NamedExpression {
535
- identifier: self.var_ids.create_variable_from_name(expr.as_str().trim()),
536
- expression: self.build_expression(expr.into_inner())?,
537
- })
538
- }
539
- 2 => Ok({
540
- let expression = self.build_expression(inner.try_next()?.into_inner())?;
541
- let identifier = self
542
- .var_ids
543
- .create_variable_from_name(inner.try_next()?.as_str());
544
- ast::NamedExpression {
545
- identifier,
546
- expression,
547
- }
548
- }),
549
- _ =>
550
- {
551
- panic!(
552
- "Invalid number of terms in named expressions {}",
553
- inner.len()
554
- );
555
- }
556
- }
557
- }
558
- unknown_expression => Err(
559
- error::InternalError::UnxpectedExpression(
560
- "build_named_expressions",
561
- format!("{unknown_expression:?}"),
562
- )
563
- .into(),
564
- ),
565
- }
566
- }
567
-
568
- fn build_labels(pair: pest::iterators::Pair<Rule>) -> Result<ast::LabelExpression>
569
- {
570
- match pair.as_rule()
571
- {
572
- Rule::labels => Self::build_labels(pair.into_inner().try_next()?),
573
- Rule::label_alternative =>
574
- {
575
- let mut r = ast::LabelExpression::None;
576
- let inner = pair.into_inner();
577
- for next in inner
578
- {
579
- r = r.or(Self::build_labels(next)?);
580
- }
581
- Ok(r)
582
- }
583
- Rule::label_inclusion =>
584
- {
585
- let mut r = ast::LabelExpression::None;
586
- let inner = pair.into_inner();
587
- for next in inner
588
- {
589
- r = r.and(Self::build_labels(next)?);
590
- }
591
- Ok(r)
592
- }
593
- Rule::label_atom => Ok(ast::LabelExpression::String(pair.as_str().to_string())),
594
- _ => Err(
595
- InternalError::UnexpectedPair {
596
- context: "build_labels",
597
- pair: format!("{:#?}", pair),
598
- }
599
- .into(),
600
- ),
601
- }
602
- }
603
-
604
- fn build_node_pattern(&self, pair: pest::iterators::Pair<Rule>) -> Result<ast::NodePattern>
605
- {
606
- let it = pair.into_inner();
607
- let mut variable = None;
608
- let mut labels = ast::LabelExpression::None;
609
- let mut properties = None;
610
-
611
- for pair in it
612
- {
613
- match pair.as_rule()
614
- {
615
- Rule::ident =>
616
- {
617
- variable = Some(pair.as_str().to_string());
618
- }
619
- Rule::labels =>
620
- {
621
- labels = Self::build_labels(pair)?;
622
- }
623
- Rule::map => properties = Some(self.build_map(pair)?),
624
- Rule::parameter =>
625
- {
626
- properties = Some(ast::Expression::Parameter(ast::Parameter {
627
- name: pair.as_str().to_string(),
628
- }))
629
- }
630
- unknown_expression =>
631
- {
632
- return Err(
633
- error::InternalError::UnxpectedExpression(
634
- "build_node_pattern",
635
- format!("{unknown_expression:?}"),
636
- )
637
- .into(),
638
- );
639
- }
640
- }
641
- }
642
- Ok(ast::NodePattern {
643
- variable: self.var_ids.create_variable_from_name_optional(variable),
644
- labels,
645
- properties,
646
- })
647
- }
648
-
649
- fn build_edge_pattern(
650
- &self,
651
- source_node: ast::NodePattern,
652
- edge_pair: pest::iterators::Pair<Rule>,
653
- destination_node: ast::NodePattern,
654
- allow_undirected_edge: bool,
655
- ) -> Result<ast::EdgePattern>
656
- {
657
- let edge_rule = edge_pair.as_rule();
658
- let it = edge_pair.into_inner().try_next()?.into_inner();
659
- let mut variable = None;
660
- let mut labels = ast::LabelExpression::None;
661
- let mut properties = None;
662
-
663
- for pair in it
664
- {
665
- match pair.as_rule()
666
- {
667
- Rule::ident =>
668
- {
669
- variable = Some(pair.as_str().to_string());
670
- }
671
- Rule::labels =>
672
- {
673
- labels = Self::build_labels(pair)?;
674
- }
675
- Rule::map => properties = Some(self.build_map(pair)?),
676
- Rule::parameter =>
677
- {
678
- properties = Some(ast::Expression::Parameter(ast::Parameter {
679
- name: pair.as_str().to_string(),
680
- }))
681
- }
682
- unknown_expression =>
683
- {
684
- return Err(
685
- error::InternalError::UnxpectedExpression(
686
- "build_edge_pattern",
687
- format!("{unknown_expression:?}"),
688
- )
689
- .into(),
690
- );
691
- }
692
- }
693
- }
694
-
695
- match edge_rule
696
- {
697
- Rule::directed_edge_pattern => Ok(ast::EdgePattern {
698
- variable: self.var_ids.create_variable_from_name_optional(variable),
699
- source: source_node,
700
- destination: destination_node,
701
- directivity: graph::EdgeDirectivity::Directed,
702
- labels,
703
- properties,
704
- }),
705
- Rule::reversed_edge_pattern => Ok(ast::EdgePattern {
706
- variable: self.var_ids.create_variable_from_name_optional(variable),
707
- source: destination_node,
708
- destination: source_node,
709
- directivity: graph::EdgeDirectivity::Directed,
710
- labels,
711
- properties,
712
- }),
713
- Rule::undirected_edge_pattern =>
714
- {
715
- if !allow_undirected_edge
716
- {
717
- Err(CompileTimeError::RequiresDirectedRelationship {
718
- context: "creation",
719
- })?;
720
- }
721
- Ok(ast::EdgePattern {
722
- variable: self.var_ids.create_variable_from_name_optional(variable),
723
- source: source_node,
724
- destination: destination_node,
725
- directivity: graph::EdgeDirectivity::Undirected,
726
- labels,
727
- properties,
728
- })
729
- }
730
- unknown_expression => Err(
731
- error::InternalError::UnxpectedExpression(
732
- "build_pattern/edge_pattern",
733
- format!("{unknown_expression:?}"),
734
- )
735
- .into(),
736
- ),
737
- }
738
- }
739
-
740
- fn build_patterns(
741
- &self,
742
- iterator: &mut pest::iterators::Pairs<Rule>,
743
- allow_undirected_edge: bool,
744
- ) -> Result<Vec<ast::Pattern>>
745
- {
746
- let mut vec = vec![];
747
-
748
- for pair in iterator
749
- {
750
- vec.append(&mut self.build_pattern(pair, allow_undirected_edge)?);
751
- }
752
- Ok(vec)
753
- }
754
-
755
- fn build_pattern(
756
- &self,
757
- pair: pest::iterators::Pair<Rule>,
758
- allow_undirected_edge: bool,
759
- ) -> Result<Vec<ast::Pattern>>
760
- {
761
- let mut vec = vec![];
762
-
763
- match pair.as_rule()
764
- {
765
- Rule::node_pattern =>
766
- {
767
- vec.push(ast::Pattern::Node(
768
- self.build_node_pattern(pair.into_inner().try_next()?)?,
769
- ));
770
- }
771
- Rule::edge_pattern =>
772
- {
773
- let mut it = pair.into_inner().peekable();
774
- let mut source_node = self.build_node_pattern(it.try_next()?)?;
775
-
776
- while let Some(next) = it.next()
777
- {
778
- let mut destination_node = self.build_node_pattern(it.try_next()?)?;
779
-
780
- if it.peek().is_some() && destination_node.variable.is_none()
781
- {
782
- destination_node.variable = Some(self.var_ids.create_anonymous_variable());
783
- }
784
-
785
- let edge_pattern = self.build_edge_pattern(
786
- source_node,
787
- next,
788
- destination_node.clone(),
789
- allow_undirected_edge,
790
- )?;
791
- vec.push(ast::Pattern::Edge(edge_pattern));
792
- source_node = destination_node;
793
- }
794
- }
795
- Rule::path_pattern =>
796
- {
797
- let mut it = pair.into_inner();
798
- let variable = it.try_next()?.as_str().to_string();
799
- let source_node = self.build_node_pattern(it.try_next()?)?;
800
- let edge_it = it.try_next()?;
801
- let destination_node = self.build_node_pattern(it.try_next()?)?;
802
-
803
- let edge_pattern = self.build_edge_pattern(
804
- source_node,
805
- edge_it,
806
- destination_node,
807
- allow_undirected_edge,
808
- )?;
809
- vec.push(ast::Pattern::Path(ast::PathPattern {
810
- variable: self.var_ids.create_variable_from_name(variable),
811
- edge: edge_pattern,
812
- }));
813
- }
814
- unknown_expression =>
815
- {
816
- return Err(
817
- error::InternalError::UnxpectedExpression(
818
- "build_node_or_edge_vec",
819
- format!("{unknown_expression:?}"),
820
- )
821
- .into(),
822
- );
823
- }
824
- };
825
- Ok(vec)
826
- }
827
-
828
- fn build_match(&self, pair: pest::iterators::Pair<Rule>, optional: bool)
829
- -> Result<ast::Statement>
830
- {
831
- let inner = pair.into_inner();
832
- let mut where_expression = None;
833
- let mut patterns = vec![];
834
- for pair in inner
835
- {
836
- match pair.as_rule()
837
- {
838
- Rule::where_modifier =>
839
- {
840
- where_expression =
841
- Some(self.build_expression(pair.into_inner().try_next()?.into_inner())?)
842
- }
843
- _ => patterns.append(&mut self.build_pattern(pair, true)?),
844
- }
845
- }
846
-
847
- Ok(ast::Statement::Match(ast::Match {
848
- patterns,
849
- where_expression,
850
- optional,
851
- }))
852
- }
853
-
854
- fn build_return_with_statement(
855
- &self,
856
- pairs: pest::iterators::Pairs<Rule>,
857
- ) -> Result<(
858
- bool,
859
- Vec<ast::NamedExpression>,
860
- ast::Modifiers,
861
- Option<ast::Expression>,
862
- )>
863
- {
864
- let mut all = false;
865
- let mut expressions = vec![];
866
- let mut modifiers = Default::default();
867
- let mut where_expression = Default::default();
868
-
869
- for sub_pair in pairs
870
- {
871
- match sub_pair.as_rule()
872
- {
873
- Rule::star => all = true,
874
- Rule::named_expression => expressions.push(self.build_named_expression(sub_pair)?),
875
- Rule::modifiers => modifiers = self.build_modifiers(sub_pair)?,
876
- Rule::where_modifier =>
877
- {
878
- where_expression =
879
- Some(self.build_expression(sub_pair.into_inner().try_next()?.into_inner())?)
880
- }
881
- _ => Err(InternalError::UnexpectedPair {
882
- context: "build_ast_from_statement/with_statement",
883
- pair: sub_pair.as_str().to_string(),
884
- })?,
885
- }
886
- }
887
- Ok((all, expressions, modifiers, where_expression))
888
- }
889
- fn build_ident(&self, iterator: &mut pest::iterators::Pairs<Rule>) -> Result<String>
890
- {
891
- let pair = iterator.next().ok_or_else(|| InternalError::MissingPair {
892
- context: "build_ident",
893
- })?;
894
- match pair.as_rule()
895
- {
896
- Rule::ident => Ok(pair.as_str().to_string()),
897
- _ => Err(
898
- InternalError::UnexpectedPair {
899
- context: "build_ident",
900
- pair: pair.to_string(),
901
- }
902
- .into(),
903
- ),
904
- }
905
- }
906
- fn build_ast_from_statement(&self, pair: pest::iterators::Pair<Rule>) -> Result<ast::Statement>
907
- {
908
- match pair.as_rule()
909
- {
910
- Rule::create_graph_statement => Ok(ast::Statement::CreateGraph(ast::CreateGraph {
911
- name: self.build_ident(&mut pair.into_inner())?,
912
- if_not_exists: false,
913
- })),
914
- Rule::create_graph_if_not_exists_statement =>
915
- {
916
- Ok(ast::Statement::CreateGraph(ast::CreateGraph {
917
- name: self.build_ident(&mut pair.into_inner())?,
918
- if_not_exists: true,
919
- }))
920
- }
921
- Rule::drop_graph_statement => Ok(ast::Statement::DropGraph(ast::DropGraph {
922
- name: self.build_ident(&mut pair.into_inner())?,
923
- if_exists: false,
924
- })),
925
- Rule::drop_graph_if_exists_statement => Ok(ast::Statement::DropGraph(ast::DropGraph {
926
- name: self.build_ident(&mut pair.into_inner())?,
927
- if_exists: true,
928
- })),
929
- Rule::use_graph_statement => Ok(ast::Statement::UseGraph(ast::UseGraph {
930
- name: self.build_ident(&mut pair.into_inner())?,
931
- })),
932
- Rule::create_statement => Ok(ast::Statement::Create(ast::Create {
933
- patterns: self.build_patterns(&mut pair.into_inner(), false)?,
934
- })),
935
- Rule::match_statement => self.build_match(pair, false),
936
- Rule::optional_match_statement => self.build_match(pair.into_inner().try_next()?, true),
937
- Rule::return_statement =>
938
- {
939
- let (all, expressions, modifiers, where_expression) =
940
- self.build_return_with_statement(pair.into_inner())?;
941
-
942
- Ok(ast::Statement::Return(ast::Return {
943
- all,
944
- expressions,
945
- modifiers,
946
- where_expression,
947
- }))
948
- }
949
- Rule::with_statement =>
950
- {
951
- let (all, expressions, modifiers, where_expression) =
952
- self.build_return_with_statement(pair.into_inner())?;
953
-
954
- Ok(ast::Statement::With(ast::With {
955
- all,
956
- expressions,
957
- modifiers,
958
- where_expression,
959
- }))
960
- }
961
- Rule::unwind_statement =>
962
- {
963
- let pair = pair
964
- .into_inner()
965
- .next()
966
- .ok_or_else(|| InternalError::MissingPair {
967
- context: "build_ast_from_statement/unwind_statement",
968
- })?;
969
-
970
- let ne = match pair.as_rule()
971
- {
972
- Rule::named_expression => self.build_named_expression(pair),
973
- _ => Err(
974
- InternalError::UnexpectedPair {
975
- context: "build_ast_from_statement/with_statement",
976
- pair: pair.as_str().to_string(),
977
- }
978
- .into(),
979
- ),
980
- }?;
981
- Ok(ast::Statement::Unwind(ast::Unwind {
982
- identifier: ne.identifier,
983
- expression: ne.expression,
984
- }))
985
- }
986
- Rule::delete_statement | Rule::detach_delete_statement =>
987
- {
988
- let detach = match pair.as_rule()
989
- {
990
- Rule::delete_statement => false,
991
- Rule::detach_delete_statement => true,
992
- _ =>
993
- {
994
- return Err(
995
- InternalError::UnexpectedPair {
996
- context: "build_ast_from_statement/delete_statement",
997
- pair: pair.to_string(),
998
- }
999
- .into(),
1000
- )
1001
- }
1002
- };
1003
-
1004
- let pairs = pair.into_inner();
1005
-
1006
- let expressions = pairs
1007
- .into_iter()
1008
- .map(|pair| self.build_expression(pair.into_inner()))
1009
- .collect::<Result<_>>()?;
1010
-
1011
- Ok(ast::Statement::Delete(ast::Delete {
1012
- detach,
1013
- expressions,
1014
- }))
1015
- }
1016
- Rule::set_statement =>
1017
- {
1018
- let mut updates = Vec::<ast::OneUpdate>::new();
1019
- for pair in pair.into_inner()
1020
- {
1021
- match pair.as_rule()
1022
- {
1023
- Rule::set_eq_expression | Rule::set_add_expression =>
1024
- {
1025
- let add_property = pair.as_rule() == Rule::set_add_expression;
1026
-
1027
- let mut pair = pair.into_inner();
1028
- let mut pair_left = pair.try_next()?.into_inner();
1029
- let target = self
1030
- .var_ids
1031
- .create_variable_from_name(pair_left.try_next()?.as_str());
1032
- let path = pair_left.map(|el| el.as_str().to_string()).collect();
1033
- let expression = self.build_expression(pair.try_next()?.into_inner())?;
1034
- let update_property = ast::UpdateProperty {
1035
- target,
1036
- path,
1037
- expression,
1038
- };
1039
- if add_property
1040
- {
1041
- updates.push(ast::OneUpdate::AddProperty(update_property));
1042
- }
1043
- else
1044
- {
1045
- updates.push(ast::OneUpdate::SetProperty(update_property));
1046
- }
1047
- }
1048
- Rule::set_label_expression =>
1049
- {
1050
- let mut pair = pair.into_inner();
1051
- let target = self
1052
- .var_ids
1053
- .create_variable_from_name(pair.try_next()?.as_str());
1054
- let labels = pair.map(|el| el.as_str().to_string()).collect();
1055
- updates.push(ast::OneUpdate::AddLabels(ast::AddRemoveLabels {
1056
- target,
1057
- labels,
1058
- }));
1059
- }
1060
- unknown_expression => Err(error::InternalError::UnxpectedExpression(
1061
- "build_ast_from_statement/set_statement",
1062
- format!("{unknown_expression:?}"),
1063
- ))?,
1064
- }
1065
- }
1066
- Ok(ast::Statement::Update(ast::Update { updates }))
1067
- }
1068
- Rule::remove_statement =>
1069
- {
1070
- let mut updates = Vec::<ast::OneUpdate>::new();
1071
- for pair in pair.into_inner()
1072
- {
1073
- match pair.as_rule()
1074
- {
1075
- Rule::remove_member_access =>
1076
- {
1077
- let mut pair = pair.into_inner();
1078
- let target = self
1079
- .var_ids
1080
- .create_variable_from_name(pair.try_next()?.as_str());
1081
- let path = pair.map(|el| el.as_str().to_string()).collect();
1082
- updates.push(ast::OneUpdate::RemoveProperty(ast::RemoveProperty {
1083
- target,
1084
- path,
1085
- }));
1086
- }
1087
- Rule::set_label_expression =>
1088
- {
1089
- let mut pair = pair.into_inner();
1090
- let target = self
1091
- .var_ids
1092
- .create_variable_from_name(pair.try_next()?.as_str());
1093
- let labels = pair.map(|el| el.as_str().to_string()).collect();
1094
- updates.push(ast::OneUpdate::RemoveLabels(ast::AddRemoveLabels {
1095
- target,
1096
- labels,
1097
- }));
1098
- }
1099
- unknown_expression => Err(error::InternalError::UnxpectedExpression(
1100
- "build_ast_from_statement/remove_statement",
1101
- format!("{unknown_expression:?}"),
1102
- ))?,
1103
- }
1104
- }
1105
- Ok(ast::Statement::Update(ast::Update { updates }))
1106
- }
1107
- Rule::call_statement =>
1108
- {
1109
- let name = pair
1110
- .into_inner()
1111
- .map(|pair| pair.as_str())
1112
- .collect::<Vec<&str>>()
1113
- .join(".");
1114
- Ok(ast::Statement::Call(ast::Call {
1115
- name,
1116
- arguments: Default::default(),
1117
- }))
1118
- }
1119
- unknown_expression => Err(
1120
- error::InternalError::UnxpectedExpression(
1121
- "build_ast_from_statement",
1122
- format!("{unknown_expression:?}"),
1123
- )
1124
- .into(),
1125
- ),
1126
- }
1127
- }
1128
- }
1129
-
1130
- pub(crate) fn parse(input: &str) -> Result<ast::Queries>
1131
- {
1132
- let pratt = PrattParser::new()
1133
- .op(Op::infix(Rule::xor, Assoc::Left))
1134
- .op(Op::infix(Rule::or, Assoc::Left))
1135
- .op(Op::infix(Rule::and, Assoc::Left))
1136
- .op(Op::infix(Rule::equal, Assoc::Left) | Op::infix(Rule::different, Assoc::Left))
1137
- .op(
1138
- Op::infix(Rule::inferior, Assoc::Left)
1139
- | Op::infix(Rule::inferior_equal, Assoc::Left)
1140
- | Op::infix(Rule::superior, Assoc::Left)
1141
- | Op::infix(Rule::superior_equal, Assoc::Left),
1142
- )
1143
- .op(Op::infix(Rule::not_in, Assoc::Left) | Op::infix(Rule::in_, Assoc::Left))
1144
- .op(Op::infix(Rule::addition, Assoc::Left) | Op::infix(Rule::subtraction, Assoc::Left))
1145
- .op(
1146
- Op::infix(Rule::multiplication, Assoc::Left)
1147
- | Op::infix(Rule::division, Assoc::Left)
1148
- | Op::infix(Rule::modulo, Assoc::Left)
1149
- | Op::infix(Rule::exponent, Assoc::Left),
1150
- )
1151
- .op(Op::prefix(Rule::not) | Op::prefix(Rule::negation))
1152
- .op(
1153
- Op::postfix(Rule::is_null)
1154
- | Op::postfix(Rule::is_not_null)
1155
- | Op::postfix(Rule::member_access)
1156
- | Op::postfix(Rule::index_access)
1157
- | Op::postfix(Rule::range_access)
1158
- | Op::postfix(Rule::range_access_to),
1159
- );
1160
- let ast_builder = AstBuilder {
1161
- pratt,
1162
- var_ids: Default::default(),
1163
- };
1164
- let pairs = GQLParser::parse(Rule::queries, input)?;
1165
- let mut queries = Vec::<ast::Statements>::new();
1166
- if crate::consts::SHOW_PARSE_TREE
1167
- {
1168
- println!("pairs = {:#?}", pairs);
1169
- }
1170
- for q_pair in pairs
1171
- {
1172
- match q_pair.as_rule()
1173
- {
1174
- Rule::query =>
1175
- {
1176
- let mut stmts = ast::Statements::new();
1177
-
1178
- for pair in q_pair.into_inner()
1179
- {
1180
- match pair.as_rule()
1181
- {
1182
- Rule::statement =>
1183
- {
1184
- stmts.push(ast_builder.build_ast_from_statement(pair.into_inner().try_next()?)?);
1185
- }
1186
- unknown_expression =>
1187
- {
1188
- Err(error::InternalError::UnxpectedExpression(
1189
- "parse",
1190
- format!("{unknown_expression:?}"),
1191
- ))?;
1192
- }
1193
- }
1194
- }
1195
- queries.push(stmts);
1196
- }
1197
- Rule::EOI =>
1198
- {}
1199
- unknown_expression =>
1200
- {
1201
- Err(error::InternalError::UnxpectedExpression(
1202
- "parse",
1203
- format!("{unknown_expression:?}"),
1204
- ))?;
1205
- }
1206
- }
1207
- }
1208
- if crate::consts::SHOW_AST
1209
- {
1210
- println!("ast = {:#?}", &queries);
1211
- }
1212
- Ok(queries)
1213
- }