selene-db-gql 1.3.0

ISO/IEC 39075:2024 GQL parser, planner, optimizer, and executor for selene-db.
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
//! Shared optimizer walkers.

use crate::{
    ExistsBody, PatternElement, StatementCategory, ValueExpr,
    plan::{
        BindingDef, BindingTableSchema, CatalogOp, EdgeMatch, ExecutionPlan, FilterPredicate,
        FilterPredicateKind, JoinTree, MutationOp, OrderKey, PipelineOp,
        PlannedTypePropertyConstraint, ProjectExpr, PropertyInit, Transformed,
        optimize::{OptimizeContext, Rule, binding_refs},
    },
};

/// Visit every expression-bearing IR site in one plan, excluding nested
/// subplans reached through `Union`, `Chain`, and `JoinTree::Subplan`.
///
/// Expression-bearing rows that carry `binding_refs` are refreshed after a
/// mutation when their binding context can resolve every referenced variable.
pub(crate) fn walk_value_exprs(
    plan: &mut ExecutionPlan,
    visit: &mut impl FnMut(&mut ValueExpr) -> bool,
) -> bool {
    let mut changed = false;
    if let Some(pattern) = &mut plan.pattern_plan {
        changed |= walk_predicates(&mut pattern.filters, &pattern.bindings, visit);
        changed |= walk_join_tree_exprs(&mut pattern.join_tree, &pattern.bindings, visit);
    }
    for op in &mut plan.pipeline {
        // Pipeline aliases do not currently carry enough binding metadata to
        // rebuild a row context by name here. Preserve existing refs on any
        // variable-bearing expression and refresh literal-only rows to empty.
        changed |= walk_pipeline_op_exprs(op, &[], visit);
    }
    changed
}

/// Walk a [`FilterPredicate`] expression and refresh its `binding_refs` when
/// the visitor mutates it.
pub(crate) fn walk_and_sync_binding_refs_filter(
    predicate: &mut FilterPredicate,
    bindings: &[BindingDef],
    visit: &mut impl FnMut(&mut ValueExpr) -> bool,
) -> bool {
    let changed = walk_expr(&mut predicate.expr, visit);
    if changed {
        sync_filter_binding_refs(predicate, bindings);
    }
    changed
}

/// Walk a [`ProjectExpr`] expression and refresh its `binding_refs` when the
/// visitor mutates it.
pub(crate) fn walk_and_sync_binding_refs_project(
    project: &mut ProjectExpr,
    bindings: &[BindingDef],
    visit: &mut impl FnMut(&mut ValueExpr) -> bool,
) -> bool {
    let changed = walk_expr(&mut project.expr, visit);
    if changed {
        sync_binding_refs(
            "ProjectExpr",
            &project.expr,
            &mut project.binding_refs,
            bindings,
        );
    }
    changed
}

/// Walk an [`OrderKey`] expression and refresh its `binding_refs` when the
/// visitor mutates it.
pub(crate) fn walk_and_sync_binding_refs_order(
    order: &mut OrderKey,
    bindings: &[BindingDef],
    visit: &mut impl FnMut(&mut ValueExpr) -> bool,
) -> bool {
    let changed = walk_expr(&mut order.expr, visit);
    if changed {
        sync_binding_refs("OrderKey", &order.expr, &mut order.binding_refs, bindings);
    }
    changed
}

/// Walk a [`PropertyInit`] value expression and refresh its `binding_refs` when
/// the visitor mutates it.
pub(crate) fn walk_and_sync_binding_refs_property_init(
    init: &mut PropertyInit,
    bindings: &[BindingDef],
    visit: &mut impl FnMut(&mut ValueExpr) -> bool,
) -> bool {
    let changed = walk_expr(&mut init.value.expr, visit);
    if changed {
        sync_binding_refs(
            "PropertyInit",
            &init.value.expr,
            &mut init.value.binding_refs,
            bindings,
        );
    }
    changed
}

/// Recurse into nested execution plans reached by pipeline or join-tree
/// subplan boundaries.
pub(crate) fn recurse_subplans(
    mut plan: ExecutionPlan,
    visit: &mut impl FnMut(ExecutionPlan) -> Transformed<ExecutionPlan>,
) -> Transformed<ExecutionPlan> {
    let mut changed = false;
    if let Some(pattern) = &mut plan.pattern_plan {
        changed |= recurse_join_tree_subplans(&mut pattern.join_tree, visit);
    }
    for op in &mut plan.pipeline {
        match op {
            PipelineOp::Union { rhs, .. }
            | PipelineOp::Chain(rhs)
            | PipelineOp::CorrelatedChain(rhs) => {
                changed |= recurse_plan_box(rhs, visit);
            }
            PipelineOp::CallSubquery(subquery) => {
                changed |= recurse_plan_box(&mut subquery.body, visit);
            }
            PipelineOp::ExplainPlan { inner, .. } => changed |= recurse_plan_box(inner, visit),
            PipelineOp::Filter(_)
            | PipelineOp::Project(_)
            | PipelineOp::Let(_)
            | PipelineOp::Unwind { .. }
            | PipelineOp::OrderBy(_)
            | PipelineOp::Limit { .. }
            | PipelineOp::TopK { .. }
            | PipelineOp::GroupBy { .. }
            | PipelineOp::Distinct
            | PipelineOp::Match(_)
            | PipelineOp::OptionalMatch(_)
            | PipelineOp::Call(_)
            | PipelineOp::Mutation(_)
            | PipelineOp::Catalog(_)
            | PipelineOp::Tx(_)
            | PipelineOp::Session(_) => {}
        }
    }
    Transformed { plan, changed }
}

/// Recurse into nested plans with the same rule and context.
pub(crate) fn recurse_rule_subplans<R>(
    plan: ExecutionPlan,
    rule: &R,
    ctx: &OptimizeContext<'_>,
) -> Transformed<ExecutionPlan>
where
    R: Rule + ?Sized,
{
    recurse_subplans(plan, &mut |subplan| rule.rewrite(subplan, ctx))
}

/// Visit `Expand` edges that are safe targets for filter pushdown.
///
/// Recurses only into `Expand` children, both `HashJoin` sides, and the
/// preserved (`left`) side of `Outer`. Every other join-tree node terminates
/// the walk: `Scan` and `DisjunctiveScan` (leaf scans with no `Expand`
/// underneath), `Repeat` / `Questioned` / `PathSearch` / `PathModeFilter`
/// (path-shaped wrappers whose edges are not plain pushdown targets), and the
/// `WorstCaseOptimal` / `Subplan` boundaries (rules don't reach across those).
/// The optional (`right`) side of `Outer` is likewise skipped.
///
/// Why: pushing a single-binding predicate into an `Expand` edge under
/// `JoinTree::Outer.right` evaluates it before null-extension, dropping
/// rows that a post-OPTIONAL `FILTER` would have null-extended and kept.
/// Recursing only into `Outer.left` preserves the rule's preserved-side
/// pushdown opportunities while leaving optional-side filters in
/// `pattern.filters`, where they correctly run after null-extension.
pub(crate) fn walk_expand_nodes(
    tree: &mut JoinTree,
    visit: &mut impl FnMut(&mut EdgeMatch) -> bool,
) -> bool {
    match tree {
        JoinTree::Unit
        | JoinTree::Scan(_)
        | JoinTree::Repeat { .. }
        | JoinTree::Questioned { .. }
        | JoinTree::PathSearch { .. }
        | JoinTree::PathModeFilter { .. }
        | JoinTree::WorstCaseOptimal { .. }
        | JoinTree::Subplan(_) => false,
        JoinTree::Expand { child, edge, .. } => {
            let changed_child = walk_expand_nodes(child, visit);
            visit(edge) | changed_child
        }
        // MatchModeFilter wraps the whole graph pattern (the join-tree root for
        // DIFFERENT EDGES). Descend so edge-filter pushdown still reaches the
        // Expand nodes beneath it; the pattern-wide filter runs afterward on the
        // surviving rows, so narrowing edge candidates first is safe.
        JoinTree::MatchModeFilter { child, .. } => walk_expand_nodes(child, visit),
        JoinTree::HashJoin { left, right, .. } => {
            walk_expand_nodes(left, visit) | walk_expand_nodes(right, visit)
        }
        JoinTree::Outer { left, .. } => walk_expand_nodes(left, visit),
        // DisjunctiveScan branches are leaf scans; no Expand under them.
        JoinTree::DisjunctiveScan { .. } => false,
    }
}

fn recurse_join_tree_subplans(
    tree: &mut JoinTree,
    visit: &mut impl FnMut(ExecutionPlan) -> Transformed<ExecutionPlan>,
) -> bool {
    match tree {
        JoinTree::Unit | JoinTree::Scan(_) | JoinTree::WorstCaseOptimal { .. } => false,
        JoinTree::Expand { child, .. }
        | JoinTree::Questioned { child, .. }
        | JoinTree::Repeat { child, .. }
        | JoinTree::PathSearch { child, .. }
        | JoinTree::PathModeFilter { child, .. }
        | JoinTree::MatchModeFilter { child, .. } => recurse_join_tree_subplans(child, visit),
        JoinTree::HashJoin { left, right, .. } | JoinTree::Outer { left, right, .. } => {
            recurse_join_tree_subplans(left, visit) | recurse_join_tree_subplans(right, visit)
        }
        JoinTree::Subplan(plan) => recurse_plan_box(plan, visit),
        // DisjunctiveScan branches are leaf scans; no nested ExecutionPlan.
        JoinTree::DisjunctiveScan { .. } => false,
    }
}

fn recurse_plan_box(
    plan: &mut Box<ExecutionPlan>,
    visit: &mut impl FnMut(ExecutionPlan) -> Transformed<ExecutionPlan>,
) -> bool {
    let current = std::mem::replace(plan, Box::new(empty_plan()));
    let transformed = visit(*current);
    **plan = transformed.plan;
    transformed.changed
}

fn empty_plan() -> ExecutionPlan {
    ExecutionPlan {
        category: StatementCategory::ReadOnly,
        pattern_plan: None,
        pipeline: Vec::new(),
        output_schema: BindingTableSchema {
            columns: Vec::new(),
        },
        impl_defined_caps: Default::default(),
        expr_ids: Default::default(),
        subqueries: Default::default(),
        next_expr_id: crate::ExprId::new(0),
        next_pipeline_op_id: crate::PipelineOpId::new(0),
    }
}

fn walk_join_tree_exprs(
    tree: &mut JoinTree,
    bindings: &[BindingDef],
    visit: &mut impl FnMut(&mut ValueExpr) -> bool,
) -> bool {
    match tree {
        JoinTree::Unit => false,
        JoinTree::Scan(scan) => walk_predicates(&mut scan.property_predicates, bindings, visit),
        JoinTree::Expand { child, edge, .. } => {
            let changed_child = walk_join_tree_exprs(child, bindings, visit);
            let changed_edge = walk_predicates(&mut edge.property_predicates, bindings, visit)
                | walk_predicates(&mut edge.right_property_predicates, bindings, visit);
            changed_child | changed_edge
        }
        JoinTree::Questioned { child, edge, .. } => {
            let changed_child = walk_join_tree_exprs(child, bindings, visit);
            let changed_edge = walk_predicates(&mut edge.property_predicates, bindings, visit)
                | walk_predicates(&mut edge.right_property_predicates, bindings, visit);
            changed_child | changed_edge
        }
        JoinTree::Repeat { child, edge, .. } => {
            let changed_child = walk_join_tree_exprs(child, bindings, visit);
            let changed_edge = walk_predicates(&mut edge.property_predicates, bindings, visit)
                | walk_predicates(&mut edge.inline_predicates, bindings, visit)
                | walk_predicates(&mut edge.final_property_predicates, bindings, visit);
            changed_child | changed_edge
        }
        JoinTree::PathSearch { child, .. }
        | JoinTree::PathModeFilter { child, .. }
        | JoinTree::MatchModeFilter { child, .. } => walk_join_tree_exprs(child, bindings, visit),
        JoinTree::HashJoin { left, right, .. } => {
            walk_join_tree_exprs(left, bindings, visit)
                | walk_join_tree_exprs(right, bindings, visit)
        }
        JoinTree::Outer {
            left,
            right,
            right_filters,
            ..
        } => {
            walk_join_tree_exprs(left, bindings, visit)
                | walk_join_tree_exprs(right, bindings, visit)
                | walk_predicates(right_filters, bindings, visit)
        }
        JoinTree::WorstCaseOptimal { .. } | JoinTree::Subplan(_) => false,
        // Walk each branch's property predicates. Per-branch scans are leaves
        // with no edge or nested tree to recurse through.
        JoinTree::DisjunctiveScan { branches, .. } => {
            branches.iter_mut().fold(false, |changed, branch| {
                walk_predicates(&mut branch.property_predicates, bindings, visit) | changed
            })
        }
    }
}

fn walk_pipeline_op_exprs(
    op: &mut PipelineOp,
    bindings: &[BindingDef],
    visit: &mut impl FnMut(&mut ValueExpr) -> bool,
) -> bool {
    match op {
        PipelineOp::Filter(pred) => walk_and_sync_binding_refs_filter(pred, bindings, visit),
        PipelineOp::Project(items) | PipelineOp::Let(items) => {
            items.iter_mut().fold(false, |changed, item| {
                walk_and_sync_binding_refs_project(item, bindings, visit) | changed
            })
        }
        PipelineOp::Unwind { source, .. } => {
            walk_and_sync_binding_refs_project(source, bindings, visit)
        }
        PipelineOp::OrderBy(keys) => keys.iter_mut().fold(false, |changed, key| {
            walk_and_sync_binding_refs_order(key, bindings, visit) | changed
        }),
        PipelineOp::TopK { keys, .. } => keys.iter_mut().fold(false, |changed, key| {
            walk_and_sync_binding_refs_order(key, bindings, visit) | changed
        }),
        PipelineOp::GroupBy { keys, aggregates } => {
            let key_changed = keys.iter_mut().fold(false, |changed, key| {
                walk_and_sync_binding_refs_project(key, bindings, visit) | changed
            });
            let aggregate_changed = aggregates.iter_mut().fold(false, |changed, aggregate| {
                aggregate.args.iter_mut().fold(false, |arg_changed, arg| {
                    walk_expr(&mut arg.expr, visit) | arg_changed
                }) | changed
            });
            key_changed | aggregate_changed
        }
        PipelineOp::Call(call) => call.args.iter_mut().fold(false, |changed, arg| {
            walk_and_sync_binding_refs_project(arg, bindings, visit) | changed
        }),
        PipelineOp::Match(pattern) | PipelineOp::OptionalMatch(pattern) => {
            walk_join_tree_exprs(&mut pattern.join_tree, &pattern.bindings, visit)
        }
        PipelineOp::Mutation(mutation) => walk_mutation_exprs(mutation, bindings, visit),
        PipelineOp::Catalog(catalog) => walk_catalog_exprs(catalog, bindings, visit),
        PipelineOp::Limit { .. }
        | PipelineOp::Distinct
        | PipelineOp::Union { .. }
        | PipelineOp::Chain(_)
        | PipelineOp::CorrelatedChain(_)
        | PipelineOp::CallSubquery(_)
        | PipelineOp::ExplainPlan { .. }
        | PipelineOp::Tx(_)
        // SESSION SET VALUE's RHS is a value specification evaluated against an
        // empty binding, so it carries no binding references for the optimizer.
        | PipelineOp::Session(_) => false,
    }
}

fn walk_mutation_exprs(
    mutation: &mut MutationOp,
    bindings: &[BindingDef],
    visit: &mut impl FnMut(&mut ValueExpr) -> bool,
) -> bool {
    match mutation {
        MutationOp::InsertNode { property_inits, .. }
        | MutationOp::InsertEdge { property_inits, .. } => {
            property_inits.iter_mut().fold(false, |changed, init| {
                walk_and_sync_binding_refs_property_init(init, bindings, visit) | changed
            })
        }
        MutationOp::SetProperty { value, .. } => {
            walk_and_sync_binding_refs_project(value, bindings, visit)
        }
        MutationOp::SetLabel { .. }
        | MutationOp::RemoveProperty { .. }
        | MutationOp::RemoveLabel { .. }
        | MutationOp::DeleteTargets { .. } => false,
    }
}

fn walk_catalog_exprs(
    catalog: &mut CatalogOp,
    bindings: &[BindingDef],
    visit: &mut impl FnMut(&mut ValueExpr) -> bool,
) -> bool {
    match catalog {
        CatalogOp::CreateNodeType { properties, .. }
        | CatalogOp::CreateEdgeType { properties, .. } => {
            properties.iter_mut().fold(false, |changed, property| {
                property
                    .constraints
                    .iter_mut()
                    .fold(false, |constraint_changed, constraint| match constraint {
                        PlannedTypePropertyConstraint::Default(expr, _) => {
                            walk_and_sync_binding_refs_project(expr, bindings, visit)
                                | constraint_changed
                        }
                        PlannedTypePropertyConstraint::NotNull(_)
                        | PlannedTypePropertyConstraint::Immutable(_)
                        | PlannedTypePropertyConstraint::Unique(_)
                        | PlannedTypePropertyConstraint::Indexed { .. } => constraint_changed,
                    })
                    | changed
            })
        }
        CatalogOp::CreateGraph { .. }
        | CatalogOp::DropGraph { .. }
        | CatalogOp::DropNodeType { .. }
        | CatalogOp::DropEdgeType { .. }
        | CatalogOp::TruncateNodeType { .. }
        | CatalogOp::TruncateEdgeType { .. }
        | CatalogOp::CreateIndex { .. }
        | CatalogOp::DropIndex { .. }
        | CatalogOp::ShowNodeTypes(_)
        | CatalogOp::ShowEdgeTypes(_)
        | CatalogOp::ShowIndexes(_)
        | CatalogOp::ShowProcedures(_) => false,
    }
}

fn walk_predicates(
    predicates: &mut [FilterPredicate],
    bindings: &[BindingDef],
    visit: &mut impl FnMut(&mut ValueExpr) -> bool,
) -> bool {
    predicates.iter_mut().fold(false, |changed, pred| {
        walk_and_sync_binding_refs_filter(pred, bindings, visit) | changed
    })
}

fn sync_filter_binding_refs(predicate: &mut FilterPredicate, bindings: &[BindingDef]) {
    let Some(mut refs) = binding_refs::collect_binding_refs(&predicate.expr, bindings) else {
        tracing::debug!(
            site = "FilterPredicate",
            "optimizer expression rewrite could not recompute binding_refs; leaving existing refs"
        );
        return;
    };
    if let FilterPredicateKind::PropertyEquals {
        binding: Some(binding),
        ..
    } = predicate.kind
    {
        refs.push(binding);
        refs.sort();
        refs.dedup();
    }
    predicate.binding_refs = refs;
}

fn sync_binding_refs(
    site: &'static str,
    expr: &ValueExpr,
    binding_refs: &mut Vec<crate::BindingId>,
    bindings: &[BindingDef],
) {
    if let Some(refs) = binding_refs::collect_binding_refs(expr, bindings) {
        *binding_refs = refs;
    } else {
        tracing::debug!(
            site = site,
            "optimizer expression rewrite could not recompute binding_refs; leaving existing refs"
        );
    }
}

fn walk_expr(expr: &mut ValueExpr, visit: &mut impl FnMut(&mut ValueExpr) -> bool) -> bool {
    // Recurse into direct `ValueExpr` children (post-order), tracking whether
    // any descendant was rewritten. `for_each_child_mut` yields the `IS
    // [SOURCE|DESTINATION] OF` operand as a child, so the edge-binding walk that
    // the optimizer relies on is preserved. Subquery bodies are not `ValueExpr`
    // children: match-bodied `Exists` descends into its `MatchClause`
    // explicitly, while query-bodied `Exists` and `ValueSubquery` are optimized
    // as their own planned subqueries.
    let mut changed_children = false;
    expr.for_each_child_mut(&mut |child| {
        changed_children |= walk_expr(child, visit);
    });
    if let ValueExpr::Exists {
        body: ExistsBody::Match(pattern),
        ..
    } = expr
    {
        changed_children |= walk_match_clause(pattern, visit);
    }
    visit(expr) | changed_children
}

fn walk_match_clause(
    clause: &mut crate::MatchClause,
    visit: &mut impl FnMut(&mut ValueExpr) -> bool,
) -> bool {
    let pattern_changed = clause.patterns.iter_mut().fold(false, |changed, pattern| {
        pattern
            .elements
            .iter_mut()
            .fold(false, |element_changed, element| {
                walk_pattern_element(element, visit) | element_changed
            })
            | changed
    });
    let where_changed = clause
        .where_clause
        .as_mut()
        .is_some_and(|value| walk_expr(value, visit));
    pattern_changed | where_changed
}

fn walk_pattern_element(
    element: &mut PatternElement,
    visit: &mut impl FnMut(&mut ValueExpr) -> bool,
) -> bool {
    match element {
        PatternElement::Node(node) => {
            let property_changed = node
                .properties
                .iter_mut()
                .fold(false, |changed, (_, value)| {
                    walk_expr(value, visit) | changed
                });
            let where_changed = node
                .inline_where
                .as_mut()
                .is_some_and(|value| walk_expr(value, visit));
            property_changed | where_changed
        }
        PatternElement::Edge(edge) => {
            let property_changed = edge
                .properties
                .iter_mut()
                .fold(false, |changed, (_, value)| {
                    walk_expr(value, visit) | changed
                });
            let where_changed = edge
                .inline_where
                .as_mut()
                .is_some_and(|value| walk_expr(value, visit));
            property_changed | where_changed
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use selene_core::DbString;

    use crate::{
        BinaryOp, Literal, SourceSpan,
        analyze::{AnalyzedType, BindingId, ExprId},
        plan::{BindingElement, FilterPredicateKind},
    };

    fn db_string(value: &str) -> DbString {
        selene_core::db_string(value).expect("test string fits DB string cap")
    }

    fn span() -> SourceSpan {
        SourceSpan::new(0, 1)
    }

    fn binding(raw: u32, name: &str) -> BindingDef {
        BindingDef {
            binding: BindingId::new(raw),
            name: db_string(name),
            element: BindingElement::Node,
            ty: AnalyzedType::DYNAMIC,
            label_predicate: None,
            span: span(),
        }
    }

    fn variable(name: &str) -> ValueExpr {
        ValueExpr::Variable {
            name: db_string(name),
            span: span(),
        }
    }

    fn binary_refs(left: &str, right: &str) -> ValueExpr {
        ValueExpr::BinaryOp {
            op: BinaryOp::Eq,
            lhs: Box::new(variable(left)),
            rhs: Box::new(variable(right)),
            span: span(),
        }
    }

    fn dynamic_project(expr: ValueExpr, refs: Vec<BindingId>) -> ProjectExpr {
        ProjectExpr {
            expr,
            expr_id: ExprId::new(0),
            ty: AnalyzedType::DYNAMIC,
            declared_type: None,
            alias: None,
            binding_refs: refs,
            span: span(),
        }
    }

    #[test]
    fn walk_and_sync_filter_refreshes_binding_refs() {
        let bindings = vec![binding(0, "n"), binding(1, "m")];
        let n = bindings[0].binding;
        let m = bindings[1].binding;
        let mut predicate = FilterPredicate {
            expr: binary_refs("n", "m"),
            expr_id: ExprId::new(0),
            ty: AnalyzedType::DYNAMIC,
            binding_refs: vec![n, m],
            kind: FilterPredicateKind::Expression,
            index_consumed: false,
            span: span(),
        };

        let changed = walk_and_sync_binding_refs_filter(&mut predicate, &bindings, &mut |expr| {
            if matches!(expr, ValueExpr::BinaryOp { .. }) {
                *expr = variable("n");
                true
            } else {
                false
            }
        });

        assert!(changed);
        assert_eq!(predicate.binding_refs, vec![n]);
    }

    #[test]
    fn walk_and_sync_filter_preserves_property_equals_binding() {
        let bindings = vec![binding(0, "n")];
        let n = bindings[0].binding;
        let mut predicate = FilterPredicate {
            expr: ValueExpr::BinaryOp {
                op: BinaryOp::Add,
                lhs: Box::new(ValueExpr::Literal(Literal::Integer(1, span()))),
                rhs: Box::new(ValueExpr::Literal(Literal::Integer(2, span()))),
                span: span(),
            },
            expr_id: ExprId::new(0),
            ty: AnalyzedType::DYNAMIC,
            binding_refs: vec![n],
            kind: FilterPredicateKind::PropertyEquals {
                binding: Some(n),
                key: db_string("age"),
            },
            index_consumed: false,
            span: span(),
        };

        let changed = walk_and_sync_binding_refs_filter(&mut predicate, &bindings, &mut |expr| {
            if matches!(expr, ValueExpr::BinaryOp { .. }) {
                *expr = ValueExpr::Literal(Literal::Integer(3, span()));
                true
            } else {
                false
            }
        });

        assert!(changed);
        assert_eq!(predicate.binding_refs, vec![n]);
    }

    #[test]
    fn walk_and_sync_property_init_refreshes_binding_refs() {
        let bindings = vec![binding(0, "n"), binding(1, "m")];
        let n = bindings[0].binding;
        let m = bindings[1].binding;
        let mut init = PropertyInit {
            key: db_string("age"),
            value: dynamic_project(binary_refs("n", "m"), vec![n, m]),
            span: span(),
        };

        let changed = walk_and_sync_binding_refs_property_init(&mut init, &bindings, &mut |expr| {
            if matches!(expr, ValueExpr::BinaryOp { .. }) {
                *expr = variable("m");
                true
            } else {
                false
            }
        });

        assert!(changed);
        assert_eq!(init.value.binding_refs, vec![m]);
    }
}