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

use super::*;
use crate::graph::{OptimizerCatalogIndexes, OptimizerCatalogStatistics};
use hawdb_core::CompressedHistogram;

fn node(variable: &str, label: &str) -> GraphMatchNode {
    GraphMatchNode {
        variable: variable.into(),
        label: label.into(),
        properties: BTreeMap::new(),
    }
}

fn program() -> GraphMatchProgram {
    GraphMatchProgram {
        imports: Vec::new(),
        introduced: vec!["a".into(), "b".into()],
        projected_nodes: BTreeMap::new(),
        projected_relationships: BTreeMap::new(),
        steps: vec![
            GraphMatchStep::Node(node("a", "A")),
            GraphMatchStep::Expand {
                source: "a".into(),
                relationship: None,
                rel_type: "R".into(),
                rel_types: Vec::new(),
                properties: BTreeMap::new(),
                direction: RelationshipDirection::Outgoing,
                min_hops: 1,
                max_hops: 1,
                target: node("b", "B"),
            },
        ],
        predicate: None,
        predicate_steps: Vec::new(),
        edge_predicates: Vec::new(),
        optional: false,
        hash_join: None,
        id_set_conjuncts: Vec::new(),
        seeded_union: Vec::new(),
    }
}

fn catalog() -> OptimizerCatalog {
    OptimizerCatalog::new(
        OptimizerCatalogIndexes::new([("A".into(), "key".into())], [], [], []),
        OptimizerCatalogStatistics {
            label_counts: BTreeMap::from([("A".into(), 100), ("B".into(), 1_000)]),
            path_counts: BTreeMap::from([(("A".into(), "R".into(), "B".into()), 500)]),
            property_distinct_counts: BTreeMap::from([(("A".into(), "key".into()), 100)]),
            ..OptimizerCatalogStatistics::default()
        },
    )
}

fn cost(program: &GraphMatchProgram, catalog: &OptimizerCatalog) -> PlanCostBreakdown {
    estimate(
        program,
        catalog,
        &PlanBindings::default(),
        PlanCostBreakdown::new(1, 0, 0, 0, 0),
    )
}

#[test]
fn scan_seek_and_expansion_share_frequency_and_direction_statistics() {
    let mut program = program();
    let mut catalog = catalog();
    assert_eq!(cost(&program, &catalog).estimated_rows, 500);
    let GraphMatchStep::Node(seed) = &mut program.steps[0] else {
        unreachable!()
    };
    seed.properties.insert("key".into(), Value::Int(1));
    let selective = cost(&program, &catalog);
    assert_eq!((selective.estimated_rows, selective.random_io), (5, 9));

    let histogram = CompressedHistogram::from_sample(
        100,
        100,
        0,
        0,
        std::iter::repeat_n(Value::Int(1), 80)
            .chain((2..=21).map(Value::Int))
            .collect(),
    )
    .unwrap();
    std::sync::Arc::make_mut(&mut catalog.property_distributions)
        .insert(("A".into(), "key".into()), histogram);
    let frequent = cost(&program, &catalog);
    assert_eq!(frequent.estimated_rows, 400);
    assert!(frequent.random_io > selective.random_io);

    let GraphMatchStep::Expand { direction, .. } = &mut program.steps[1] else {
        unreachable!()
    };
    *direction = RelationshipDirection::Incoming;
    // No B -> A paths in the published statistics: a planning floor, not an
    // empty-result rewrite or a full Cartesian product.
    assert_eq!(cost(&program, &catalog).estimated_rows, 1);
}

#[test]
fn seeded_union_costs_actual_branches_and_charges_input_once() {
    let mut branch = program();
    let GraphMatchStep::Node(seed) = &mut branch.steps[0] else {
        unreachable!()
    };
    seed.properties.insert("key".into(), Value::Int(1));
    let mut union = program();
    union.seeded_union = vec![branch.clone(), branch];
    let input = PlanCostBreakdown::new(3, 7, 11, 13, 17);
    let cost = estimate(&union, &catalog(), &PlanBindings::default(), input);
    assert_eq!((cost.estimated_rows, cost.random_io), (30, 11 + 2 * 3 * 9));
    assert_eq!(
        (cost.cpu, cost.sequential_io, cost.output_rows),
        (7, 13, 17)
    );
}

#[test]
fn bound_endpoint_filters_candidates_and_optional_preserves_input_floor() {
    let mut program = program();
    // Returning to a is an identity check, not another fresh label scan.
    let GraphMatchStep::Expand { target, .. } = &mut program.steps[1] else {
        unreachable!()
    };
    *target = node("a", "A");
    let mut catalog = catalog();
    catalog
        .path_counts
        .insert(("A".into(), "R".into(), "A".into()), 50);
    let required = cost(&program, &catalog);
    assert_eq!(required.estimated_rows, 1);
    // The 500 A -> B candidates are visited before the bound A filter too.
    assert_eq!(required.random_io, 751);
    program.optional = true;
    let optional = estimate(
        &program,
        &catalog,
        &PlanBindings::default(),
        PlanCostBreakdown::new(10, 0, 0, 0, 0),
    );
    assert_eq!(optional.estimated_rows, 10);
}

#[test]
fn unbounded_and_disjunctive_paths_keep_a_bounded_fallback_after_the_seek() {
    let mut program = program();
    let GraphMatchStep::Node(seed) = &mut program.steps[0] else {
        unreachable!()
    };
    seed.properties.insert("key".into(), Value::Int(1));
    let GraphMatchStep::Expand {
        max_hops,
        rel_types,
        ..
    } = &mut program.steps[1]
    else {
        unreachable!()
    };
    *max_hops = usize::MAX;
    *rel_types = vec!["R".into(), "S".into()];
    assert_eq!(cost(&program, &catalog()).estimated_rows, 1_000);
    assert_eq!(cost(&program, &catalog()).random_io, 1_003);
}

#[test]
fn imported_unlabelled_nodes_retain_the_input_label_for_costing() {
    use hawdb_plan_cypher::{GraphBindingImport, GraphEntityKind};
    let mut program = program();
    program.imports.push(GraphBindingImport {
        variable: "a".into(),
        column: "a".into(),
        kind: GraphEntityKind::Node,
    });
    program.introduced.retain(|name| name != "a");
    let GraphMatchStep::Node(node) = &mut program.steps[0] else {
        unreachable!()
    };
    node.label.clear();
    let plan = PhysicalPlan::GraphMatchExec {
        program,
        input: Some(Box::new(PhysicalPlan::SeqNodeScan {
            variable: "a".into(),
            label: "A".into(),
        })),
    };
    let estimate = super::super::estimate_physical_plan_cost_breakdown(&plan, &catalog());
    assert_eq!(estimate.estimated_rows, 500);
}

#[test]
fn relationship_frequency_filters_matches_without_discounting_candidate_reads() {
    let mut program = program();
    let GraphMatchStep::Expand { properties, .. } = &mut program.steps[1] else {
        unreachable!()
    };
    properties.insert("flag".into(), Value::Int(1));
    let mut catalog = catalog();
    catalog.rel_type_counts.insert("R".into(), 500);
    let histogram = CompressedHistogram::from_sample(
        500,
        500,
        0,
        0,
        std::iter::repeat_n(Value::Int(1), 10)
            .chain(std::iter::repeat_n(Value::Int(0), 490))
            .collect(),
    )
    .unwrap();
    std::sync::Arc::make_mut(&mut catalog.rel_property_distributions)
        .insert(("R".into(), "flag".into()), histogram);
    let estimate = cost(&program, &catalog);
    assert_eq!(estimate.estimated_rows, 10);
    assert_eq!(estimate.random_io, 701);
}

#[test]
fn large_intermediates_saturate_without_turning_into_empty_estimates() {
    let mut program = program();
    program.steps.push(GraphMatchStep::Node(node("c", "A")));
    program.introduced.push("c".into());
    let mut catalog = catalog();
    catalog.label_counts.insert("A".into(), u64::MAX);
    catalog.label_counts.insert("B".into(), u64::MAX);
    catalog
        .path_counts
        .insert(("A".into(), "R".into(), "B".into()), u64::MAX);
    let estimate = cost(&program, &catalog);
    assert_eq!(estimate.estimated_rows, u64::MAX);
    assert_eq!(estimate.random_io, u64::MAX);
    assert_eq!(estimate.cost, u64::MAX);
}

#[test]
fn complete_absent_hop_charges_dense_and_sparse_dead_end_prefixes() {
    for (a_count, b_count, first_hops) in [(100, 1_000, 5_000), (1_000, 1_000, 10)] {
        for direction in [
            RelationshipDirection::Outgoing,
            RelationshipDirection::Incoming,
        ] {
            let mut program = program();
            let mut catalog = catalog();
            catalog.label_counts.insert("A".into(), a_count);
            catalog.label_counts.insert("B".into(), b_count);
            catalog.rel_type_counts.insert("R".into(), first_hops);
            catalog
                .rel_type_source_counts
                .insert("R".into(), a_count.min(first_hops));
            catalog
                .path_counts
                .insert(("A".into(), "R".into(), "B".into()), first_hops);
            catalog
                .bounded_path_counts
                .insert(("A".into(), "R".into(), "B".into(), 1), first_hops);
            catalog.bounded_path_complete_through_hop = Some(3);
            let incoming = direction == RelationshipDirection::Incoming;
            let (source_label, target_label, population) = if incoming {
                ("B", "A", b_count)
            } else {
                ("A", "B", a_count)
            };
            program.steps[0] = GraphMatchStep::Node(node("a", source_label));
            let GraphMatchStep::Expand {
                target,
                direction: current_direction,
                min_hops,
                max_hops,
                ..
            } = &mut program.steps[1]
            else {
                unreachable!()
            };
            *target = node("b", target_label);
            *current_direction = direction;
            *min_hops = 2;
            *max_hops = 2;
            // All R edges run A -> B, so both traversal orientations have
            // first-hop prefixes but no two-hop completion.
            assert_eq!(catalog.bounded_path_count("A", "R", "B", 2), Some(0));
            let complete = cost(&program, &catalog);
            assert_eq!(complete.estimated_rows, 1);
            assert!(
                complete.random_io > 2 * population + first_hops,
                "{complete:?}"
            );
            let expand = catalog.estimate_expand_rows("A", "R", &BTreeMap::new(), "B", 2, 2);
            assert!(expand.estimated_rows > 0);
            assert!(expand.hop_estimates[1].rows >= first_hops);
            assert!(!expand.hop_estimates[1].exact);
        }
    }
}

#[test]
fn failed_prefixes_include_intermediate_labels_outside_the_endpoint_pair() {
    let mut program = program();
    let GraphMatchStep::Expand {
        min_hops, max_hops, ..
    } = &mut program.steps[1]
    else {
        unreachable!()
    };
    *min_hops = 2;
    *max_hops = 2;
    let mut catalog = catalog();
    catalog.rel_type_counts.insert("R".into(), 10_000);
    catalog.label_counts.insert("X".into(), 9_999);
    catalog
        .path_counts
        .insert(("A".into(), "R".into(), "B".into()), 1);
    catalog
        .path_counts
        .insert(("A".into(), "R".into(), "X".into()), 9_999);
    catalog
        .bounded_path_counts
        .insert(("A".into(), "R".into(), "B".into(), 1), 1);
    catalog
        .bounded_path_counts
        .insert(("A".into(), "R".into(), "X".into(), 1), 9_999);
    catalog.bounded_path_complete_through_hop = Some(3);
    // Every first hop dead-ends. The single A -> B edge does not describe
    // the 9,999 additional A -> X prefixes inspected by the same traversal.
    let estimate = cost(&program, &catalog);
    assert_eq!(estimate.estimated_rows, 1);
    assert!(estimate.random_io > 10_000 + 2 * 100, "{estimate:?}");
}

#[test]
fn positive_completions_charge_failed_prefixes_in_both_directions() {
    for reverse in [false, true] {
        let mut program = program();
        let GraphMatchStep::Expand {
            min_hops,
            max_hops,
            direction,
            ..
        } = &mut program.steps[1]
        else {
            unreachable!()
        };
        *min_hops = 2;
        *max_hops = 2;
        *direction = if reverse {
            RelationshipDirection::Incoming
        } else {
            RelationshipDirection::Outgoing
        };
        let mut catalog = catalog();
        catalog.label_counts.insert("A".into(), 1);
        catalog.label_counts.insert("B".into(), 10_001);
        catalog.label_counts.insert("X".into(), 1);
        catalog.rel_type_counts.insert("R".into(), 10_002);
        catalog.path_counts.clear();
        for (source, target, count) in [("A", "B", 10_000), ("A", "X", 1), ("X", "B", 1)] {
            let (source, target) = if reverse {
                (target, source)
            } else {
                (source, target)
            };
            catalog
                .path_counts
                .insert((source.into(), "R".into(), target.into()), count);
            catalog
                .bounded_path_counts
                .insert((source.into(), "R".into(), target.into(), 1), count);
        }
        let (source, target) = if reverse { ("B", "A") } else { ("A", "B") };
        catalog
            .bounded_path_counts
            .insert((source.into(), "R".into(), target.into(), 2), 1);
        catalog.bounded_path_complete_through_hop = Some(3);
        let estimate = cost(&program, &catalog);
        assert_eq!(estimate.estimated_rows, 1);
        // Ten thousand first-hop dead ends still require candidate visits and
        // adjacency probes, even with one successful two-hop completion.
        assert!(
            estimate.random_io > 2 * 10_001,
            "reverse={reverse}: {estimate:?}"
        );
    }
}

#[test]
fn positive_completions_charge_deeper_prefixes_with_other_labels() {
    for reverse in [false, true] {
        let mut program = program();
        let GraphMatchStep::Expand {
            min_hops,
            max_hops,
            direction,
            ..
        } = &mut program.steps[1]
        else {
            unreachable!()
        };
        *min_hops = 3;
        *max_hops = 3;
        *direction = if reverse {
            RelationshipDirection::Incoming
        } else {
            RelationshipDirection::Outgoing
        };
        let mut catalog = catalog();
        catalog.label_counts.insert("A".into(), 1);
        catalog.label_counts.insert("B".into(), 10_001);
        catalog.label_counts.insert("X".into(), 10_000);
        catalog.label_counts.insert("Y".into(), 1);
        catalog.label_counts.insert("Z".into(), 1);
        catalog.rel_type_counts.insert("R".into(), 20_003);
        catalog.path_counts.clear();
        for (source, target, count) in [
            ("A", "B", 10_000),
            ("A", "Y", 1),
            ("Y", "X", 10_000),
            ("Y", "Z", 1),
            ("Z", "B", 1),
        ] {
            let (source, target) = if reverse {
                (target, source)
            } else {
                (source, target)
            };
            catalog
                .path_counts
                .insert((source.into(), "R".into(), target.into()), count);
            catalog
                .bounded_path_counts
                .insert((source.into(), "R".into(), target.into(), 1), count);
        }
        for (target, hop, count) in [("X", 2, 10_000), ("Z", 2, 1), ("B", 3, 1)] {
            let (source, target) = if reverse {
                (target, "A")
            } else {
                ("A", target)
            };
            catalog
                .bounded_path_counts
                .insert((source.into(), "R".into(), target.into(), hop), count);
        }
        catalog.bounded_path_complete_through_hop = Some(3);
        let estimate = cost(&program, &catalog);
        assert_eq!(estimate.estimated_rows, 1);
        // Both shorter depths visit 10,001 prefixes, including X/Y/Z nodes
        // absent from the requested A/B endpoint pair.
        assert!(
            estimate.random_io > 4 * 10_001,
            "reverse={reverse}: {estimate:?}"
        );
    }
}