hawdb-optimizer-graph 0.29.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
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
// Copyright 2026 Nowledge
// SPDX-License-Identifier: Apache-2.0

//! Cost-based start of a linear `Expand` chain (#1485).
//!
//! A first MATCH whose relationship types are distinct normalizes to a
//! `NodeScan` of its first node in text order followed by one `Expand` per
//! relationship. When a later node of that chain is index-seekable and
//! clearly cheaper to start from (see [`super::pattern_start`]), this pass
//! rebuilds the chain from that node: it scans the new start, expands towards
//! the chain's end, then back to its first node with each crossed direction
//! flipped. Every filter of the chain is lifted above the new chain, and the
//! ordinary logical rewrite pushes each conjunct back down to the expansion
//! that binds its variables, so the new start's equality becomes its seek.
//!
//! An `Expand` chain is a conjunction of independent relationship
//! constraints; it enforces no uniqueness between its expansions (the binder
//! only builds one when the types differ, or when the clauses differ), and a
//! variable-length trail is the same trail walked either way. Walking it
//! from another node therefore changes only the work and the row order, not
//! the row multiset. A named variable-length relationship binds a list in
//! text order and is never walked backwards.

use super::pattern_start::{self, PathEdge, PathNode, StartDecision};
use super::OptimizerCatalog;
use crate::{OptimizerQualificationOptions, OptionalOptimizerRule, RuleEvent};
use hawdb_plan_cypher::{
    GraphBindingImport, GraphEntityKind, GraphMatchHashJoin, GraphMatchNode, GraphMatchProgram,
    GraphMatchStep, LogicalPlan, Predicate,
};
use std::collections::{BTreeMap, BTreeSet};

/// A split of a chain at `meeting` that the hash join makes clearly cheaper.
struct ChainSplit {
    meeting: usize,
    estimate: pattern_start::MeetingEstimate,
    input_rows: f64,
    total: f64,
}

/// Rewrites every eligible chain in `plan`.
pub(super) fn rewrite(
    plan: LogicalPlan,
    catalog: &OptimizerCatalog,
    options: OptimizerQualificationOptions,
    events: &mut Vec<RuleEvent>,
) -> LogicalPlan {
    // Optimistic catalogs carry no path statistics, so no chain is estimated.
    if catalog.path_counts.is_empty() {
        return plan;
    }
    visit(plan, catalog, options, events)
}

fn visit(
    plan: LogicalPlan,
    catalog: &OptimizerCatalog,
    options: OptimizerQualificationOptions,
    events: &mut Vec<RuleEvent>,
) -> LogicalPlan {
    if let Some(chain) = Chain::collect(&plan) {
        // A chain's only leaf is its scan; there is nothing else to visit.
        return chain.restart(catalog, options, events).unwrap_or(plan);
    }
    let mut child = |input: Box<LogicalPlan>| Box::new(visit(*input, catalog, options, events));
    match plan {
        LogicalPlan::VectorIndexSearch(scan) => LogicalPlan::VectorIndexSearch(Box::new(
            (*scan).map_input(|input| visit(input, catalog, options, events)),
        )),
        LogicalPlan::GraphMatch { program, input } => LogicalPlan::GraphMatch {
            program,
            input: input.map(child),
        },
        LogicalPlan::NodeCartesianProduct { left, right } => {
            let left = child(left);
            LogicalPlan::NodeCartesianProduct {
                left,
                right: child(right),
            }
        }
        LogicalPlan::NodeColumnLookup {
            variable,
            label,
            property,
            column,
            optional,
            node_visibility_predicate,
            input,
        } => LogicalPlan::NodeColumnLookup {
            variable,
            label,
            property,
            column,
            optional,
            node_visibility_predicate,
            input: child(input),
        },
        LogicalPlan::Expand {
            source_variable,
            source_label,
            rel_variable,
            rel_type,
            rel_properties,
            direction,
            target_variable,
            target_label,
            min_hops,
            max_hops,
            optional,
            input,
        } => LogicalPlan::Expand {
            source_variable,
            source_label,
            rel_variable,
            rel_type,
            rel_properties,
            direction,
            target_variable,
            target_label,
            min_hops,
            max_hops,
            optional,
            input: child(input),
        },
        LogicalPlan::OptionalDegree {
            source_variable,
            rel_type,
            rel_properties,
            direction,
            target_label,
            target_properties,
            alias,
            input,
        } => LogicalPlan::OptionalDegree {
            source_variable,
            rel_type,
            rel_properties,
            direction,
            target_label,
            target_properties,
            alias,
            input: child(input),
        },
        LogicalPlan::Filter { predicate, input } => LogicalPlan::Filter {
            predicate,
            input: child(input),
        },
        LogicalPlan::Project { items, input } => LogicalPlan::Project {
            items,
            input: child(input),
        },
        LogicalPlan::Aggregate {
            group_keys,
            items,
            input,
        } => LogicalPlan::Aggregate {
            group_keys,
            items,
            input: child(input),
        },
        LogicalPlan::Distinct { input } => LogicalPlan::Distinct {
            input: child(input),
        },
        LogicalPlan::Sort { items, input } => LogicalPlan::Sort {
            items,
            input: child(input),
        },
        LogicalPlan::Limit {
            offset,
            limit,
            input,
        } => LogicalPlan::Limit {
            offset,
            limit,
            input: child(input),
        },
        leaf => leaf,
    }
}

/// One required expansion of a chain, oriented from `nodes[i]` to
/// `nodes[i + 1]` in text order.
struct ChainExpand<'a> {
    rel_variable: &'a Option<String>,
    rel_type: &'a str,
    rel_properties: &'a std::collections::BTreeMap<String, hawdb_core::Value>,
    direction: hawdb_core::RelationshipDirection,
    min_hops: usize,
    max_hops: usize,
}

struct ChainNode<'a> {
    variable: &'a str,
    label: &'a str,
}

/// `Filter* (Expand | Filter)* NodeScan`, where each required `Expand`
/// continues from the previous one's target and binds fresh variables.
struct Chain<'a> {
    nodes: Vec<ChainNode<'a>>,
    expands: Vec<ChainExpand<'a>>,
    predicates: Vec<&'a Predicate>,
}

impl<'a> Chain<'a> {
    fn collect(plan: &'a LogicalPlan) -> Option<Self> {
        let mut predicates = Vec::new();
        let mut expands = Vec::new();
        let mut current = plan;
        let (variable, label) = loop {
            match current {
                LogicalPlan::Filter { predicate, input } => {
                    predicates.push(predicate);
                    current = input;
                }
                LogicalPlan::Expand {
                    optional: false,
                    input,
                    ..
                } => {
                    expands.push(current);
                    current = input;
                }
                LogicalPlan::NodeScan { variable, label } => break (variable, label),
                _ => return None,
            }
        };
        if expands.is_empty() {
            return None;
        }
        let mut nodes = vec![ChainNode { variable, label }];
        let mut chain_expands = Vec::with_capacity(expands.len());
        let mut variables = BTreeSet::from([variable.as_str()]);
        for expand in expands.into_iter().rev() {
            let LogicalPlan::Expand {
                source_variable,
                rel_variable,
                rel_type,
                rel_properties,
                direction,
                target_variable,
                target_label,
                min_hops,
                max_hops,
                ..
            } = expand
            else {
                unreachable!("only expansions were collected");
            };
            let previous = nodes.last().expect("the scan is the first node");
            if source_variable != previous.variable
                || !variables.insert(target_variable)
                || rel_variable
                    .as_ref()
                    .is_some_and(|name| !variables.insert(name))
            {
                return None;
            }
            nodes.push(ChainNode {
                variable: target_variable,
                label: target_label,
            });
            chain_expands.push(ChainExpand {
                rel_variable,
                rel_type,
                rel_properties,
                direction: *direction,
                min_hops: *min_hops,
                max_hops: *max_hops,
            });
        }
        Some(Self {
            nodes,
            expands: chain_expands,
            predicates,
        })
    }

    /// The chain rebuilt from a clearly cheaper start, or `None` to keep it.
    fn restart(
        &self,
        catalog: &OptimizerCatalog,
        options: OptimizerQualificationOptions,
        events: &mut Vec<RuleEvent>,
    ) -> Option<LogicalPlan> {
        let mut conjuncts = Vec::new();
        for predicate in &self.predicates {
            flatten(predicate, &mut conjuncts);
        }
        let nodes = self
            .nodes
            .iter()
            .map(|node| PathNode {
                variable: node.variable,
                label: node.label,
                equalities: conjuncts
                    .iter()
                    .filter_map(|conjunct| match conjunct {
                        Predicate::PropertyEq {
                            variable,
                            property,
                            value,
                        } if variable == node.variable && *value != hawdb_core::Value::Null => {
                            Some(property.as_str())
                        }
                        _ => None,
                    })
                    .collect(),
                values: conjuncts
                    .iter()
                    .filter_map(|conjunct| match conjunct {
                        Predicate::PropertyEq {
                            variable,
                            property,
                            value,
                        } if variable == node.variable => Some((property.as_str(), value)),
                        _ => None,
                    })
                    .collect(),
                bound: false,
            })
            .collect::<Vec<_>>();
        let edges = self
            .expands
            .iter()
            .map(|expand| PathEdge {
                rel_type: (!expand.rel_type.is_empty()).then_some(expand.rel_type),
                equalities: expand.rel_properties.keys().map(String::as_str).collect(),
                values: expand
                    .rel_properties
                    .iter()
                    .map(|(key, value)| (key.as_str(), value))
                    .collect(),
                direction: expand.direction,
                min_hops: expand.min_hops,
                max_hops: expand.max_hops,
                reversible: expand.rel_variable.is_none()
                    || (expand.min_hops == 1 && expand.max_hops == 1),
            })
            .collect::<Vec<_>>();
        let restart = pattern_start::choose_start(catalog, &nodes, &edges, 0);
        if let Some(split) = self.best_split(catalog, &nodes, &edges)
            && !matches!(&restart, Some(StartDecision::Chosen(choice)) if choice.work <= split.total)
        {
            let rule = OptionalOptimizerRule::GraphMatchHashJoin;
            let detail = format!(
                "split Expand chain at {}: expand {} to it, then join a GraphMatchExec built once from {}: estimated work {:.1} + {:.1} build matches + {:.0} probes instead of {:.0} rows x {:.1} per row",
                self.nodes[split.meeting].variable,
                self.nodes[0].variable,
                self.nodes[split.meeting + split.estimate.build_start].variable,
                split.estimate.build,
                split.estimate.build_rows,
                split.input_rows,
                split.input_rows,
                split.estimate.per_row,
            );
            if options.rule_enabled(rule) {
                events.push(RuleEvent::applied(rule.as_str(), detail));
                return Some(self.split(split.meeting, &conjuncts));
            }
            events.push(RuleEvent::skipped(
                rule.as_str(),
                format!("disabled by statement qualification: {detail}"),
            ));
        }
        let rule = OptionalOptimizerRule::CostBasedPatternStart;
        let choice = match restart? {
            StartDecision::Kept { detail } => {
                events.push(RuleEvent::estimated(
                    rule.as_str(),
                    format!("keep Expand chain start {}: {detail}", nodes[0].variable),
                ));
                return None;
            }
            StartDecision::Chosen(choice) => choice,
        };
        let start = &self.nodes[choice.start];
        let detail = format!(
            "start Expand chain at {} with an index seek on {}.{}: estimated work {:.1} instead of {:.1} from {}",
            start.variable,
            start.label,
            choice.property,
            choice.work,
            choice.current_work,
            self.nodes[0].variable
        );
        if !options.rule_enabled(rule) {
            events.push(RuleEvent::skipped(
                rule.as_str(),
                format!("disabled by statement qualification: {detail}"),
            ));
            return None;
        }
        events.push(RuleEvent::applied(rule.as_str(), detail));
        Some(self.rebuild(choice.start))
    }

    /// The cheapest node to split the chain at: the chain runs from its start
    /// to that node, and the rest of the chain is one GraphMatch that the
    /// lowering can run as a hash join on it. Only a rest whose relationship
    /// types are distinct qualifies: a GraphMatch keeps its relationships
    /// pairwise distinct, which an `Expand` chain does not require.
    fn best_split(
        &self,
        catalog: &OptimizerCatalog,
        nodes: &[PathNode<'_>],
        edges: &[PathEdge<'_>],
    ) -> Option<ChainSplit> {
        let mut best: Option<ChainSplit> = None;
        for meeting in 1..self.expands.len() {
            let rest = &self.expands[meeting..];
            let mut types = BTreeSet::new();
            if rest
                .iter()
                .any(|expand| expand.rel_type.is_empty() || !types.insert(expand.rel_type))
            {
                continue;
            }
            let Some(prefix) =
                pattern_start::walk(catalog, &nodes[..=meeting], &edges[..meeting], 0)
            else {
                continue;
            };
            let Some(estimate) =
                pattern_start::meeting(catalog, &nodes[meeting..], &edges[meeting..], 0, 0)
            else {
                continue;
            };
            let input_rows = prefix.rows.max(1.0);
            if !estimate.hash_join_is_cheaper(input_rows) {
                continue;
            }
            let total = prefix.work + estimate.build + estimate.build_rows + input_rows;
            if best.as_ref().is_none_or(|best| total < best.total) {
                best = Some(ChainSplit {
                    meeting,
                    estimate,
                    input_rows,
                    total,
                });
            }
        }
        best
    }

    /// The chain up to `meeting`, then a GraphMatch over the rest that imports
    /// the meeting node. Conjuncts that read only the first part stay on it;
    /// a constant equality on a node of the rest becomes its pattern property
    /// (so the executor can seek it); the others keep their order in the
    /// GraphMatch predicate.
    fn split(&self, meeting: usize, conjuncts: &[&Predicate]) -> LogicalPlan {
        let mut plan = LogicalPlan::NodeScan {
            variable: self.nodes[0].variable.to_string(),
            label: self.nodes[0].label.to_string(),
        };
        let mut first = BTreeSet::from([self.nodes[0].variable.to_string()]);
        for (index, expand) in self.expands[..meeting].iter().enumerate() {
            first.insert(self.nodes[index + 1].variable.to_string());
            first.extend(expand.rel_variable.iter().cloned());
            plan = LogicalPlan::Expand {
                source_variable: self.nodes[index].variable.to_string(),
                source_label: self.nodes[index].label.to_string(),
                rel_variable: expand.rel_variable.clone(),
                rel_type: expand.rel_type.to_string(),
                rel_properties: expand.rel_properties.clone(),
                direction: expand.direction,
                target_variable: self.nodes[index + 1].variable.to_string(),
                target_label: self.nodes[index + 1].label.to_string(),
                min_hops: expand.min_hops,
                max_hops: expand.max_hops,
                optional: false,
                input: Box::new(plan),
            };
        }
        let key = self.nodes[meeting].variable;
        let rest_nodes = self.nodes[meeting + 1..]
            .iter()
            .map(|node| node.variable)
            .collect::<BTreeSet<_>>();
        let mut properties: BTreeMap<&str, BTreeMap<String, hawdb_core::Value>> = BTreeMap::new();
        let mut on_first = Vec::new();
        let mut on_rest = Vec::new();
        for conjunct in conjuncts {
            if super::lowering::predicate_variables(conjunct)
                .iter()
                .all(|name| first.contains(name))
            {
                on_first.push((*conjunct).clone());
                continue;
            }
            if let Predicate::PropertyEq {
                variable,
                property,
                value,
            } = conjunct
                && rest_nodes.contains(variable.as_str())
                && !properties
                    .get(variable.as_str())
                    .is_some_and(|existing| existing.contains_key(property))
            {
                properties
                    .entry(variable.as_str())
                    .or_default()
                    .insert(property.clone(), value.clone());
                continue;
            }
            on_rest.push((*conjunct).clone());
        }
        if !on_first.is_empty() {
            plan = LogicalPlan::Filter {
                predicate: if on_first.len() == 1 {
                    on_first.pop().expect("one conjunct")
                } else {
                    Predicate::And(on_first)
                },
                input: Box::new(plan),
            };
        }
        let node = |index: usize| GraphMatchNode {
            variable: self.nodes[index].variable.to_string(),
            label: self.nodes[index].label.to_string(),
            properties: properties
                .get(self.nodes[index].variable)
                .cloned()
                .unwrap_or_default(),
        };
        let mut steps = vec![GraphMatchStep::Node(GraphMatchNode {
            properties: BTreeMap::new(),
            ..node(meeting)
        })];
        let mut introduced = Vec::new();
        for (offset, expand) in self.expands[meeting..].iter().enumerate() {
            let index = meeting + offset;
            introduced.extend(expand.rel_variable.iter().cloned());
            introduced.push(self.nodes[index + 1].variable.to_string());
            steps.push(GraphMatchStep::Expand {
                source: self.nodes[index].variable.to_string(),
                relationship: expand.rel_variable.clone(),
                rel_type: expand.rel_type.to_string(),
                rel_types: Vec::new(),
                properties: expand.rel_properties.clone(),
                direction: expand.direction,
                min_hops: expand.min_hops,
                max_hops: expand.max_hops,
                target: node(index + 1),
            });
        }
        LogicalPlan::GraphMatch {
            program: GraphMatchProgram {
                imports: vec![GraphBindingImport {
                    variable: key.to_string(),
                    column: key.to_string(),
                    kind: GraphEntityKind::Node,
                }],
                introduced,
                projected_nodes: BTreeMap::new(),
                projected_relationships: BTreeMap::new(),
                steps,
                predicate: match on_rest.len() {
                    0 => None,
                    1 => on_rest.pop(),
                    _ => Some(Predicate::And(on_rest)),
                },
                predicate_steps: Vec::new(),
                edge_predicates: Vec::new(),
                optional: false,
                // Decided here, with this chain's own row estimate; lowering
                // places the build's conjuncts.
                hash_join: Some(GraphMatchHashJoin {
                    key: key.to_string(),
                    build_conjuncts: Vec::new(),
                    residual_order: Vec::new(),
                    residual_placed: 0,
                    probe_conjuncts: 0,
                }),
                id_set_conjuncts: Vec::new(),
            },
            input: Some(Box::new(plan)),
        }
    }

    fn rebuild(&self, start: usize) -> LogicalPlan {
        let mut plan = LogicalPlan::NodeScan {
            variable: self.nodes[start].variable.to_string(),
            label: self.nodes[start].label.to_string(),
        };
        let forward = (start..self.expands.len()).map(|index| (index, index + 1, false));
        let backward = (0..start).rev().map(|index| (index + 1, index, true));
        for (from, to, reversed) in forward.chain(backward) {
            let expand = &self.expands[from.min(to)];
            plan = LogicalPlan::Expand {
                source_variable: self.nodes[from].variable.to_string(),
                source_label: self.nodes[from].label.to_string(),
                rel_variable: expand.rel_variable.clone(),
                rel_type: expand.rel_type.to_string(),
                rel_properties: expand.rel_properties.clone(),
                direction: if reversed {
                    pattern_start::reverse(expand.direction)
                } else {
                    expand.direction
                },
                target_variable: self.nodes[to].variable.to_string(),
                target_label: self.nodes[to].label.to_string(),
                min_hops: expand.min_hops,
                max_hops: expand.max_hops,
                optional: false,
                input: Box::new(plan),
            };
        }
        // Filters keep their original order, innermost first.
        let mut predicates = self
            .predicates
            .iter()
            .rev()
            .map(|predicate| (*predicate).clone())
            .collect::<Vec<_>>();
        match predicates.len() {
            0 => plan,
            1 => LogicalPlan::Filter {
                predicate: predicates.pop().expect("one predicate"),
                input: Box::new(plan),
            },
            _ => LogicalPlan::Filter {
                predicate: Predicate::And(predicates),
                input: Box::new(plan),
            },
        }
    }
}

fn flatten<'p>(predicate: &'p Predicate, output: &mut Vec<&'p Predicate>) {
    match predicate {
        Predicate::And(children) => {
            for child in children {
                flatten(child, output);
            }
        }
        predicate => output.push(predicate),
    }
}

#[cfg(test)]
mod tests;