hawdb-plan-cypher 0.35.0

An embedded Rust graph database for local data-plane workloads
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
// Copyright 2026 Nowledge
// SPDX-License-Identifier: Apache-2.0

use super::*;

pub(super) fn bind_shortest_path_pipeline(
    query: &QueryPipeline,
    parameters: &BTreeMap<String, Value>,
) -> Result<LogicalPlan> {
    let [matched, returned] = query.clauses.as_slice() else {
        return Err(unsupported(
            "ALL SHORTEST requires a MATCH followed by RETURN",
        ));
    };
    let ClauseKind::Match {
        optional: false,
        patterns,
        predicate,
    } = &matched.kind
    else {
        return Err(unsupported("ALL SHORTEST requires a non-optional MATCH"));
    };
    let ClauseKind::Return(projection) = &returned.kind else {
        return Err(unsupported("ALL SHORTEST requires a RETURN projection"));
    };
    if projection.distinct
        || projection.predicate.is_some()
        || !projection.order_by.is_empty()
        || projection.offset.is_some()
        || projection.limit.is_some()
    {
        return Err(unsupported(
            "ALL SHORTEST does not support projection modifiers",
        ));
    }
    let [pattern] = patterns.as_slice() else {
        return Err(unsupported("ALL SHORTEST requires one path pattern"));
    };
    let Some(path_variable) = &pattern.variable else {
        return Err(unsupported("ALL SHORTEST requires a path variable"));
    };
    let [step] = pattern.steps.as_slice() else {
        return Err(unsupported(
            "ALL SHORTEST requires one bounded relationship pattern",
        ));
    };
    let source = &pattern.first;
    let target = &step.target;
    let relationship = &step.relationship;
    reject_rel_type_disjunction(relationship, "ALL SHORTEST path reads")?;
    if relationship.search != PathSearch::AllShortest {
        return Err(unsupported("path projection requires ALL SHORTEST"));
    }
    if relationship.direction == RelationshipDirection::Incoming {
        return Err(unsupported(
            "ALL SHORTEST path reads do not support incoming-only patterns",
        ));
    }
    if !source.properties.is_empty() || !target.properties.is_empty() {
        return Err(unsupported(
            "ALL SHORTEST path reads require endpoint ids in WHERE predicates",
        ));
    }
    if relationship.min_hops == 0
        || relationship.max_hops == 0
        || relationship.max_hops == hawdb_cypher::UNBOUNDED_MAX_HOPS
        || relationship.min_hops > relationship.max_hops
    {
        return Err(unsupported(
            "ALL SHORTEST path reads require a finite positive hop range",
        ));
    }
    if relationship.variable.is_some() && !relationship.rel_type.is_empty() {
        return Err(unsupported(
            "ALL SHORTEST path reads do not bind relationship variables",
        ));
    }
    if !relationship.properties.is_empty() {
        return Err(unsupported(
            "ALL SHORTEST does not support relationship property filters",
        ));
    }
    let scope = BTreeSet::from([source.variable.clone(), target.variable.clone()]);
    let predicate = predicate.as_ref().map(|predicate| &predicate.kind);
    if let Some(predicate) = predicate {
        validate_predicate(&scope, predicate)?;
    }
    let source_id = endpoint_id_value(predicate, &source.variable, parameters, "source")?;
    let target_id = endpoint_id_value(predicate, &target.variable, parameters, "target")?;
    // Endpoint visibility predicates also constrain intermediate nodes in the executor.
    // Do not repurpose them for ordinary WHERE conditions or silently discard conditions.
    if let Some(predicate) = predicate {
        validate_endpoint_predicates(
            predicate,
            &source.variable,
            &target.variable,
            &mut BTreeSet::new(),
        )?;
    }
    let returns = projection
        .items
        .iter()
        .map(|item| {
            let (referenced_path, expression) = match &item.expression.kind {
                ReturnExpressionKind::Path(ShortestPathReturnExpression::NodePropertyList {
                    path_variable,
                    property,
                }) => (
                    path_variable,
                    ShortestPathProjectionExpression::NodePropertyList {
                        property: property.clone(),
                    },
                ),
                ReturnExpressionKind::Path(ShortestPathReturnExpression::Length {
                    path_variable,
                }) => (path_variable, ShortestPathProjectionExpression::Length),
                _ => return Err(unsupported("ALL SHORTEST requires a path projection")),
            };
            if referenced_path != path_variable {
                return Err(unsupported(
                    "shortest path projection references an unknown path",
                ));
            }
            let name = item
                .alias
                .clone()
                .ok_or_else(|| unsupported("shortest path projection requires an alias"))?;
            Ok(ShortestPathProjection { expression, name })
        })
        .collect::<Result<Vec<_>>>()?;
    Ok(LogicalPlan::ShortestPath {
        source_variable: source.variable.clone(),
        source_label: source.label.clone(),
        source_id,
        source_visibility_predicate: None,
        rel_type: relationship.rel_type.clone(),
        direction: relationship.direction,
        target_variable: target.variable.clone(),
        target_label: target.label.clone(),
        target_id,
        target_visibility_predicate: None,
        min_hops: relationship.min_hops,
        max_hops: relationship.max_hops,
        returns,
    })
}

fn validate_endpoint_predicates<'a>(
    predicate: &'a PropertyPredicate,
    source: &str,
    target: &str,
    seen: &mut BTreeSet<&'a str>,
) -> Result<()> {
    match predicate {
        PropertyPredicate::And(children) => {
            for child in children {
                validate_endpoint_predicates(child, source, target, seen)?;
            }
            Ok(())
        }
        PropertyPredicate::Eq {
            variable, property, ..
        } if (variable == source || variable == target) && property == "id" => {
            if !seen.insert(variable) {
                return Err(unsupported(
                    "ALL SHORTEST requires one id equality per endpoint",
                ));
            }
            Ok(())
        }
        _ => Err(unsupported(
            "ALL SHORTEST supports only endpoint id equalities",
        )),
    }
}

fn unsupported(message: &str) -> HawDBError {
    HawDBError::Semantic(message.to_string())
}

pub(super) fn endpoint_id_value(
    predicate: Option<&PropertyPredicate>,
    variable: &str,
    parameters: &BTreeMap<String, Value>,
    endpoint_name: &str,
) -> Result<Value> {
    let Some(predicate) = predicate else {
        return Err(HawDBError::Semantic(format!(
            "ALL SHORTEST path reads require {endpoint_name} id predicate"
        )));
    };
    find_endpoint_id_value(predicate, variable, parameters)?.ok_or_else(|| {
        HawDBError::Semantic(format!(
            "ALL SHORTEST path reads require {endpoint_name} id equality on '{variable}.id'"
        ))
    })
}

pub(super) fn find_endpoint_id_value(
    predicate: &PropertyPredicate,
    variable: &str,
    parameters: &BTreeMap<String, Value>,
) -> Result<Option<Value>> {
    match predicate {
        PropertyPredicate::And(predicates) => {
            for predicate in predicates {
                if let Some(value) = find_endpoint_id_value(predicate, variable, parameters)? {
                    return Ok(Some(value));
                }
            }
            Ok(None)
        }
        PropertyPredicate::Eq {
            variable: predicate_variable,
            property,
            value,
        } if predicate_variable == variable && property == "id" => {
            Ok(Some(bind_value(value, parameters)?))
        }
        _ => Ok(None),
    }
}

/// Clause-level input of [`bind_shortest_path_match`].
pub(super) struct ShortestPathMatch<'a> {
    pub(super) optional: bool,
    pub(super) patterns: &'a [hawdb_cypher::MatchPattern],
    pub(super) predicate: Option<&'a hawdb_cypher::PredicateExpression>,
}

/// Refuses WHERE conjuncts that read a shortest-path variable of the same
/// MATCH clause, before either shortest-path binder runs, so the refusal is
/// reported for every syntax form. Neo4j applies such predicates during the
/// search; evaluating them after selection would silently differ.
pub(super) fn reject_shortest_path_predicates(clauses: &[hawdb_cypher::Clause]) -> Result<()> {
    for clause in clauses {
        let hawdb_cypher::ClauseKind::Match {
            patterns,
            predicate: Some(predicate),
            ..
        } = &clause.kind
        else {
            continue;
        };
        let paths = patterns
            .iter()
            .filter(|pattern| is_shortest_path_pattern(pattern))
            .filter_map(|pattern| pattern.variable.clone())
            .collect::<BTreeSet<_>>();
        if paths.is_empty() {
            continue;
        }
        let mut variables = BTreeSet::new();
        collect_predicate_variables(&predicate.kind, &mut variables);
        if let Some(path) = variables.intersection(&paths).next() {
            return Err(HawDBError::Semantic(format!(
                "WHERE predicates on shortest-path variable '{path}' are not supported in the same MATCH clause; filter after the search with `WITH {path} WHERE ...`"
            )));
        }
    }
    Ok(())
}

pub(super) fn is_shortest_path_pattern(pattern: &hawdb_cypher::MatchPattern) -> bool {
    pattern
        .steps
        .iter()
        .any(|step| step.relationship.search.is_shortest())
}

/// Binds a MATCH or OPTIONAL MATCH clause containing `shortestPath(...)` or
/// `allShortestPaths(...)` patterns.
///
/// Each shortest-path pattern becomes endpoint `Node` steps followed by a
/// `ShortestPath` step, so both endpoints are bound before the search. A
/// required MATCH whose other patterns are single nodes first binds every
/// endpoint under the WHERE conjuncts that do not read a path variable, so
/// endpoint predicates are applied (and may use indexes) before any search.
/// OPTIONAL MATCH keeps the whole clause in one program because its NULL row
/// belongs to the complete pattern, and so do clauses with relationship
/// patterns, whose relationships the searches must not reuse.
pub(super) fn bind_shortest_path_match(
    clause: ShortestPathMatch<'_>,
    input: Option<LogicalPlan>,
    scope: &mut Scope,
    parameters: &BTreeMap<String, Value>,
) -> Result<LogicalPlan> {
    let ShortestPathMatch {
        optional,
        patterns,
        predicate,
    } = clause;
    scope.adopt_unwound_nodes(patterns);
    let imports = scope.imports();
    let mut constrained = scope.graph_names();
    if let Some(predicate) = predicate {
        collect_predicate_variables(&predicate.kind, &mut constrained);
    }
    for pattern in patterns
        .iter()
        .filter(|pattern| !is_shortest_path_pattern(pattern))
    {
        constrained.insert(pattern.first.variable.clone());
        constrained.extend(
            pattern
                .steps
                .iter()
                .map(|step| step.target.variable.clone()),
        );
    }
    let mut introduced = Vec::new();
    let mut steps = Vec::new();
    let mut path_steps = Vec::new();
    let mut paths = Vec::new();
    for (index, pattern) in patterns.iter().enumerate() {
        if !is_shortest_path_pattern(pattern) {
            bind_ordinary_pattern(pattern, scope, &mut introduced, &mut steps, parameters)?;
            continue;
        }
        let [step] = pattern.steps.as_slice() else {
            return Err(unsupported(
                "shortestPath requires exactly one relationship pattern",
            ));
        };
        let relationship = &step.relationship;
        reject_rel_type_disjunction(relationship, "shortestPath patterns")?;
        if relationship.variable.is_some() {
            return Err(unsupported(
                "shortestPath does not bind relationship variables; use relationships(path)",
            ));
        }
        if !relationship.properties.is_empty() {
            return Err(unsupported(
                "shortestPath does not support relationship property patterns",
            ));
        }
        if relationship.min_hops > 1 {
            return Err(unsupported(
                "shortestPath supports a minimum of 0 or 1 hops",
            ));
        }
        let bounded = relationship.max_hops != hawdb_cypher::UNBOUNDED_MAX_HOPS;
        if bounded && relationship.max_hops > MAX_SHORTEST_PATH_HOPS {
            return Err(HawDBError::Semantic(format!(
                "shortestPath maximum of {} hops exceeds the limit of {MAX_SHORTEST_PATH_HOPS}",
                relationship.max_hops
            )));
        }
        let path = pattern
            .variable
            .clone()
            .unwrap_or_else(|| format!("\0shortest_path.{index}"));
        if scope.0.contains_key(&path) {
            return Err(HawDBError::Semantic(format!(
                "path variable '{path}' is already bound"
            )));
        }
        // Bind the path first so an endpoint cannot reuse its name.
        scope.0.insert(path.clone(), BindingType::Path);
        for endpoint in [&pattern.first, &step.target] {
            if endpoint.anonymous
                || (endpoint.properties.is_empty() && !constrained.contains(&endpoint.variable))
            {
                return Err(HawDBError::Semantic(format!(
                    "shortestPath endpoint '{}' must be bound by an earlier clause, an inline property map or a WHERE predicate",
                    if endpoint.anonymous {
                        "()"
                    } else {
                        endpoint.variable.as_str()
                    }
                )));
            }
            scope.bind_graph(&endpoint.variable, GraphEntityKind::Node, &mut introduced)?;
            let node = bind_node(endpoint, parameters)?;
            push_endpoint_node(&mut steps, node.clone());
            path_steps.push(GraphMatchStep::Node(node));
        }
        path_steps.push(GraphMatchStep::ShortestPath(Box::new(
            GraphMatchShortestPath {
                path: path.clone(),
                source: pattern.first.variable.clone(),
                target: step.target.variable.clone(),
                rel_type: relationship.rel_type.clone(),
                direction: relationship.direction,
                min_hops: relationship.min_hops,
                max_hops: if bounded {
                    relationship.max_hops
                } else {
                    MAX_SHORTEST_PATH_HOPS
                },
                bounded,
                selection: if relationship.search == PathSearch::AllShortest {
                    ShortestPathSelection::All
                } else {
                    ShortestPathSelection::Any
                },
            },
        )));
        paths.push(path);
    }
    if steps.len().saturating_add(path_steps.len()) > 32 {
        return Err(HawDBError::Semantic(
            "MATCH exceeds maximum pattern depth".to_string(),
        ));
    }
    scope.materialized();
    // Relationship uniqueness spans the clause, so ordinary relationship
    // patterns stay in the same program as the searches they constrain.
    let split = !optional
        && patterns
            .iter()
            .all(|pattern| is_shortest_path_pattern(pattern) || pattern.steps.is_empty());
    let path_names = paths.iter().cloned().collect::<BTreeSet<_>>();
    let mut endpoint_conjuncts = Vec::new();
    let mut path_conjuncts = Vec::new();
    if let Some(predicate) = predicate {
        let conjuncts = match &predicate.kind {
            PropertyPredicate::And(children) => children.iter().collect(),
            predicate => vec![predicate],
        };
        for conjunct in conjuncts {
            let mut variables = BTreeSet::new();
            collect_predicate_variables(conjunct, &mut variables);
            // Neo4j applies WHERE predicates on a shortest-path variable while
            // searching (the shortest path that satisfies them). Evaluating
            // them after selection would silently differ, so require the
            // explicit post-filter form instead.
            if let Some(path) = variables.intersection(&path_names).next() {
                return Err(HawDBError::Semantic(format!(
                    "WHERE predicates on shortest-path variable '{path}' are not supported in the same MATCH clause; filter after the search with `WITH {path} WHERE ...`"
                )));
            }
            let bound = bind_predicate(conjunct, scope, parameters)?;
            if !split {
                path_conjuncts.push(bound);
            } else {
                endpoint_conjuncts.push(bound);
            }
        }
    }
    let combine = |mut predicates: Vec<Predicate>| match predicates.len() {
        0 => None,
        1 => predicates.pop(),
        _ => Some(Predicate::And(predicates)),
    };
    if !split {
        introduced.extend(paths);
        steps.extend(path_steps);
        return Ok(LogicalPlan::GraphMatch {
            program: GraphMatchProgram {
                imports,
                introduced,
                projected_nodes: BTreeMap::new(),
                projected_relationships: BTreeMap::new(),
                steps,
                predicate: combine(path_conjuncts),
                predicate_steps: Vec::new(),
                edge_predicates: Vec::new(),
                optional,
                hash_join: None,
                id_set_conjuncts: Vec::new(),
                seeded_union: Vec::new(),
            },
            input: input.map(Box::new),
        });
    }
    let endpoint_predicate = combine(endpoint_conjuncts);
    // Endpoints that were all bound earlier need no separate binding program.
    let (input, imports) = if introduced.is_empty()
        && endpoint_predicate.is_none()
        && patterns.iter().all(is_shortest_path_pattern)
    {
        (input, imports)
    } else {
        let endpoints = LogicalPlan::GraphMatch {
            program: GraphMatchProgram {
                imports,
                introduced,
                projected_nodes: BTreeMap::new(),
                projected_relationships: BTreeMap::new(),
                steps,
                predicate: endpoint_predicate,
                predicate_steps: Vec::new(),
                edge_predicates: Vec::new(),
                optional: false,
                hash_join: None,
                id_set_conjuncts: Vec::new(),
                seeded_union: Vec::new(),
            },
            input: input.map(Box::new),
        };
        (Some(endpoints), Vec::new())
    };
    Ok(LogicalPlan::GraphMatch {
        program: GraphMatchProgram {
            imports,
            introduced: paths,
            projected_nodes: BTreeMap::new(),
            projected_relationships: BTreeMap::new(),
            steps: path_steps,
            predicate: combine(path_conjuncts),
            predicate_steps: Vec::new(),
            edge_predicates: Vec::new(),
            optional: false,
            hash_join: None,
            id_set_conjuncts: Vec::new(),
            seeded_union: Vec::new(),
        },
        input: input.map(Box::new),
    })
}

/// Adds a shortest-path endpoint to the binding steps without repeating a
/// node the clause already binds.
///
/// `MATCH (a:Person {id: 1}), (b:Person {id: 2}), p = shortestPath((a)-..-(b))`
/// names each endpoint twice. A repeated `Node` step only re-checks its label
/// and properties against the bound node, so folding the endpoint's
/// constraints into the earlier step keeps the same rows, and leaves one step
/// per variable, which normalization can lower to independent node access
/// (and so to index seeks) instead of a generic nested scan. Folding is
/// limited to constraints that combine without comparing values: a label
/// only into an unlabeled or identically labeled step, and properties only
/// when at most one side has any, so the plan shape never depends on
/// parameter values. Anything else keeps the explicit re-check step.
fn push_endpoint_node(steps: &mut Vec<GraphMatchStep>, node: GraphMatchNode) {
    let existing = steps.iter_mut().find_map(|step| match step {
        GraphMatchStep::Node(existing) if existing.variable == node.variable => Some(existing),
        _ => None,
    });
    let Some(existing) = existing else {
        steps.push(GraphMatchStep::Node(node));
        return;
    };
    let labels_fold =
        node.label.is_empty() || existing.label.is_empty() || node.label == existing.label;
    let properties_fold = node.properties.is_empty() || existing.properties.is_empty();
    if !(labels_fold && properties_fold) {
        steps.push(GraphMatchStep::Node(node));
        return;
    }
    if existing.label.is_empty() {
        existing.label = node.label;
    }
    if existing.properties.is_empty() {
        existing.properties = node.properties;
    }
}

/// The ordinary-pattern half of the generic MATCH binder, shared with
/// clauses that also contain shortest-path patterns.
fn bind_ordinary_pattern(
    pattern: &hawdb_cypher::MatchPattern,
    scope: &mut Scope,
    introduced: &mut Vec<String>,
    steps: &mut Vec<GraphMatchStep>,
    parameters: &BTreeMap<String, Value>,
) -> Result<()> {
    if let Some(variable) = &pattern.variable {
        if scope.0.contains_key(variable) {
            return Err(HawDBError::Semantic(format!(
                "path variable '{variable}' is already bound"
            )));
        }
        scope
            .0
            .insert(variable.clone(), BindingType::UnmaterializedPath);
    }
    scope.bind_graph(&pattern.first.variable, GraphEntityKind::Node, introduced)?;
    steps.push(GraphMatchStep::Node(bind_node(&pattern.first, parameters)?));
    let mut source = pattern.first.variable.clone();
    for step in &pattern.steps {
        let relationship = &step.relationship;
        validate_bounded_relationship(relationship)?;
        if let Some(variable) = &relationship.variable {
            scope.bind_graph(variable, GraphEntityKind::Relationship, introduced)?;
        }
        scope.bind_graph(&step.target.variable, GraphEntityKind::Node, introduced)?;
        steps.push(GraphMatchStep::Expand {
            source,
            relationship: relationship.variable.clone(),
            rel_type: relationship.rel_type.clone(),
            rel_types: relationship.rel_type_disjunction.clone(),
            properties: bind_properties(&relationship.properties, parameters)?,
            direction: relationship.direction,
            min_hops: relationship.min_hops,
            max_hops: relationship.max_hops,
            target: bind_node(&step.target, parameters)?,
        });
        source = step.target.variable.clone();
    }
    Ok(())
}