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

//! Independent allocation oracle for the actual facade key/IR/trace owner.

use super::*;
use crate::api::query_request::ScoringRequest;
use hawdb_core::graph_rag::{
    DecayTerm, MissingScoringFeature, ScoreFeature, ScoringCombination, ScoringProgram,
    ScoringSpec, ScoringTerm,
};
use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;
use std::sync::atomic::{AtomicIsize, AtomicUsize, Ordering};

thread_local! {
    static TRACK: Cell<bool> = const { Cell::new(false) };
}
static LIVE: AtomicUsize = AtomicUsize::new(0);
static LARGEST: AtomicUsize = AtomicUsize::new(0);
// Highest net live bytes since the window opened; frees of older blocks go negative.
static PEAK: AtomicIsize = AtomicIsize::new(0);

struct Allocator;

fn allocated(bytes: usize) {
    if TRACK.try_with(Cell::get).unwrap_or(false) {
        let live = LIVE.fetch_add(bytes, Ordering::Relaxed).wrapping_add(bytes);
        LARGEST.fetch_max(bytes, Ordering::Relaxed);
        PEAK.fetch_max(live as isize, Ordering::Relaxed);
    }
}

fn deallocated(bytes: usize) {
    if TRACK.try_with(Cell::get).unwrap_or(false) {
        LIVE.fetch_sub(bytes, Ordering::Relaxed);
    }
}

// SAFETY: unchanged layouts/pointers are forwarded to System. Const TLS and
// atomics do not allocate or panic. Only the oracle's thread enables tracking;
// all other unit tests retain ordinary allocation behavior.
unsafe impl GlobalAlloc for Allocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        let pointer = unsafe { System.alloc(layout) };
        if !pointer.is_null() {
            allocated(layout.size());
        }
        pointer
    }
    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
        let pointer = unsafe { System.alloc_zeroed(layout) };
        if !pointer.is_null() {
            allocated(layout.size());
        }
        pointer
    }
    unsafe fn dealloc(&self, pointer: *mut u8, layout: Layout) {
        unsafe { System.dealloc(pointer, layout) };
        deallocated(layout.size());
    }
    unsafe fn realloc(&self, pointer: *mut u8, layout: Layout, size: usize) -> *mut u8 {
        let result = unsafe { System.realloc(pointer, layout, size) };
        if !result.is_null() {
            deallocated(layout.size());
            allocated(size);
        }
        result
    }
}

#[global_allocator]
static ALLOCATOR: Allocator = Allocator;

/// Tracks allocations on the opening thread only, until dropped.
pub(crate) struct Window;

impl Window {
    pub(crate) fn new() -> Self {
        LIVE.store(0, Ordering::Relaxed);
        LARGEST.store(0, Ordering::Relaxed);
        PEAK.store(0, Ordering::Relaxed);
        TRACK.set(true);
        Self
    }
    fn retained(&self) -> usize {
        LIVE.load(Ordering::Relaxed)
    }
    /// Highest net live bytes this thread allocated while the window was open.
    pub(crate) fn peak(&self) -> usize {
        PEAK.load(Ordering::Relaxed).max(0) as usize
    }
}

impl Drop for Window {
    fn drop(&mut self) {
        TRACK.set(false);
    }
}

fn fixture(candidates: usize) -> (String, BTreeMap<String, Value>) {
    let mut query = "MATCH (n:Memory) WHERE n.id IN $ids RETURN n.id AS id".to_owned();
    let mut parameters = BTreeMap::from([(
        "ids".into(),
        Value::List(
            (0..candidates)
                .map(|index| Value::String(format!("memory-{index:08}")))
                .collect(),
        ),
    )]);
    for field in 0..5 {
        query.push_str(", CASE n.id");
        for index in 0..candidates {
            query.push_str(&format!(" WHEN $id_{index} THEN $score_{field}_{index}"));
            parameters.insert(
                format!("id_{index}"),
                Value::String(format!("memory-{index:08}")),
            );
            parameters.insert(format!("score_{field}_{index}"), Value::Float(0.5));
        }
        query.push_str(&format!(" ELSE null END AS score_{field}"));
    }
    query.push_str(" ORDER BY score_0 LIMIT 20");
    (query, parameters)
}

fn cached(
    query: &str,
    statement: &cypher::Statement,
    parameters: &BTreeMap<String, Value>,
) -> (PlanCacheKey, Arc<CachedPlan>, usize) {
    let parameterized = parameterize_logical_plan(statement, parameters).unwrap();
    let (physical_template, mut trace) =
        CascadesOptimizer::default().optimize_with_trace(parameterized.logical());
    // Include spare capacity and nonempty framework-owned metadata.
    trace.warnings.reserve(32);
    trace.warnings.push("retention probe".repeat(32));
    trace.decisions.reserve(128);
    let key = PlanCacheKey {
        normalized_query: query.to_owned(),
        statistics_values: BTreeMap::from([(
            Value::String("stat-marker".repeat(16)),
            StatisticsParameterValue::Exact(Value::String("hot-value".repeat(64))),
        )]),
        parameters: parameterized.cache_key().clone(),
        environment: OptimizerEnvironmentKey {
            schema: OptimizerSchemaKey::from_catalog(&Catalog::default()),
            proof_schema: Some(ProofSchemaKey {
                public_tables: vec![hawdb_core::TableId(7); 32],
                public_properties: vec![hawdb_core::PropertyId(9); 64],
                exists: vec![(
                    hawdb_core::ConstraintSubject::Node(LabelId(7)),
                    "required".repeat(32),
                )],
            }),
            statistics_generation: Some(42),
            ordered_range_indexes: vec![("Memory".into(), "updated_at".into())],
            ordered_composite_indexes: Some(Arc::new(vec![(
                "Memory".into(),
                vec![
                    ("space_id".into(), PropertyType::String),
                    ("updated_at".into(), PropertyType::Int),
                ],
            )])),
            node_index_size_classes: vec![(LabelId(7), true)],
        },
        max_optimizer_groups: Some(128),
        access_control: Some(AccessControlPlanCacheKey {
            policy_epoch: 9,
            visibility_property: "visibility".repeat(16),
            visibility_value_count: 4,
        }),
        scoring: Some(
            ScoringRequest::new(
                ScoringProgram::new(
                    ScoringCombination::WeightedSum,
                    MissingScoringFeature::Reject,
                    ScoringSpec {
                        terms: vec![ScoringTerm {
                            weight: 1.0,
                            feature: ScoreFeature::NodeProperty("importance".repeat(32)),
                        }],
                        decay: vec![DecayTerm {
                            feature: ScoreFeature::TimestampProperty("updated_at".repeat(32)),
                            half_life: 1000.0,
                            min_factor: 0.1,
                        }],
                    },
                )
                .unwrap(),
                "score".repeat(32),
                20,
            )
            .unwrap()
            .with_graph_seed_input("seed".repeat(16), "candidate".repeat(16))
            .unwrap()
            .with_reference_time_millis(0)
            .bind()
            .cache_key(),
        ),
        point_statistics: Some(PointStatisticsKey {
            label: "Memory".into(),
            property: "id".repeat(32),
            matched_rows: 3,
            index_seek_is_cheaper: true,
        }),
    };
    let value = Arc::new(CachedPlan {
        physical_template,
        trace,
        has_parameter_slots: parameterized.slot_count() > 0,
        has_source_regexes: false,
    });
    let charge = key
        .retained_heap_bytes()
        .unwrap()
        .checked_add(value.retained_heap_bytes().unwrap())
        .unwrap();
    (key, value, charge)
}

#[test]
fn actual_physical_cache_requested_heap_stays_within_charges_and_limits() {
    row_unwind_refuses_large_output_before_copying();
    for candidates in [20, 100, 512] {
        let (query, parameters) = fixture(candidates);
        let statement = cypher::parse(&query).unwrap();
        let (actual, charged) = {
            let window = Window::new();
            let mut cache = PlanCache::with_byte_limits(Some(128), Some(16 << 20), Some(8 << 20));
            let (key, value, charge) = cached(&query, &statement, &parameters);
            cache.insert_weighted(key, value, charge);
            assert_eq!(cache.stats().entries, 1, "{candidates}: {charge}");
            let actual = window.retained();
            let charged = cache.charged_bytes().unwrap();
            assert!(
                actual <= charged,
                "{candidates}: actual={actual}, charged={charged}"
            );
            assert!(charged <= 16 << 20);
            drop(cache);
            assert_eq!(window.retained(), 0);
            (actual, charged)
        };
        println!("physical-cache candidates={candidates} actual={actual} charged={charged}");
    }
    // Observe owned list expressions and UNWIND source payloads independently
    // of the accounting implementation, including nested retained values.
    for query in [
        "MATCH (n:Memory) RETURN [x IN reverse(range(0, 128))[1..100] WHERE x > 3 | x] AS filtered, range(0, 12)[1] AS indexed, [n.id, [n.body, 'literal-owned-payload']] AS literal_list".to_owned(),
        format!("MATCH (n:Memory) UNWIND [n.id, '{}', ['{}'], null] AS candidate RETURN candidate", "unwind".repeat(4096), "nested".repeat(2048)),
        format!("UNWIND ['{}', ['{}'], null] AS candidate RETURN candidate", "unwind".repeat(4096), "nested".repeat(2048)),
    ] {
        let statement = cypher::parse(&query).unwrap();
        let parameters = BTreeMap::new();
        let window = Window::new();
        let mut cache = PlanCache::with_byte_limits(Some(8), Some(16 << 20), Some(8 << 20));
        let (key, value, charge) = cached(&query, &statement, &parameters);
        cache.insert_weighted(key, value, charge);
        assert_eq!(cache.stats().entries, 1);
        assert!(window.retained() <= cache.charged_bytes().unwrap());
        drop(cache);
        assert_eq!(window.retained(), 0);
    }
    // Temporal calls, property access, map literals and a boxed duration
    // parameter stay in the same observed cache owner.
    {
        let query = "MATCH (p:P) RETURN p.birthday.month AS month, duration.between(p.a, p.b).minutes AS minutes, date.truncate('month', p.on) AS start, {retained_temporal_key: p.a, took: $took} AS map, $took AS took";
        let statement = cypher::parse(query).unwrap();
        let parameters = BTreeMap::from([(
            "took".into(),
            Value::Duration(Box::new(
                hawdb_core::TemporalDuration::from_parts(14, 3, 7_200_000_000_000).unwrap(),
            )),
        )]);
        let window = Window::new();
        let mut cache = PlanCache::with_byte_limits(Some(8), Some(16 << 20), Some(8 << 20));
        let (key, value, charge) = cached(query, &statement, &parameters);
        cache.insert_weighted(key, value, charge);
        assert_eq!(cache.stats().entries, 1);
        assert!(window.retained() <= cache.charged_bytes().unwrap());
        drop(cache);
        assert_eq!(window.retained(), 0);
    }
    // Measure the temporal IR through the real owner. Duration boxes dominate
    // key/container slack, so omitting their heap charge cannot hide in it.
    {
        use hawdb_core::temporal::{self, TemporalFunction, TemporalKind};
        use hawdb_plan_cypher::{Projection, ProjectionExpression};
        let date = temporal::parse_date("2026-11-01").unwrap();
        let time = temporal::parse_local_time("12:34:56").unwrap();
        let local = temporal::parse_local_datetime("2026-11-01T12:34:56").unwrap();
        let zoned = temporal::parse_datetime("2026-11-01T12:34:56+02:00").unwrap();
        let duration = temporal::parse_duration("P1M2DT3S").unwrap();
        let (query, parameters) = fixture(20);
        let statement = cypher::parse(&query).unwrap();
        let window = Window::new();
        let mut cache = PlanCache::with_byte_limits(Some(8), Some(16 << 20), Some(8 << 20));
        let (key, mut value, _) = cached(&query, &statement, &parameters);
        let mut literals = (0..4096)
            .map(|_| ProjectionExpression::Literal(Value::duration(duration)))
            .collect::<Vec<_>>();
        literals.extend([
            ProjectionExpression::Literal(Value::Date(date)),
            ProjectionExpression::Literal(Value::LocalTime(time)),
            ProjectionExpression::Literal(Value::LocalDateTime(local)),
            ProjectionExpression::Literal(Value::DateTime(zoned)),
        ]);
        let mut arguments = Vec::with_capacity(32);
        arguments.push(ProjectionExpression::Literal(Value::String(
            "2026-11-01".into(),
        )));
        literals.push(ProjectionExpression::TemporalFunction {
            function: TemporalFunction::Construct(TemporalKind::Date),
            arguments,
        });
        let mut field = String::with_capacity(1024);
        field.push_str("temporal");
        Arc::get_mut(&mut value).unwrap().physical_template = PhysicalPlan::ProjectExec {
            items: vec![Projection {
                name: "temporal".into(),
                expression: ProjectionExpression::PropertyAccess {
                    expression: Box::new(ProjectionExpression::MapLiteral(vec![(
                        field.clone(),
                        ProjectionExpression::List(literals),
                    )])),
                    key: field,
                },
            }],
            input: Box::new(PhysicalPlan::EmptyExec),
        };
        let charge = key
            .retained_heap_bytes()
            .unwrap()
            .checked_add(value.retained_heap_bytes().unwrap())
            .unwrap();
        cache.insert_weighted(key, value, charge);
        assert_eq!(cache.stats().entries, 1);
        assert!(window.retained() <= cache.charged_bytes().unwrap());
        drop(cache);
        assert_eq!(window.retained(), 0);
    }
    // Keep newly added path IR in the actual cache owner under this same
    // independent allocator. Spare string capacities and the boxed traversal
    // descriptor must be charged even when their visible names are short.
    for selection in [
        hawdb_plan_cypher::ShortestPathSelection::Any,
        hawdb_plan_cypher::ShortestPathSelection::All,
    ] {
        use hawdb_plan_cypher::{
            GraphMatchNode, GraphMatchProgram, GraphMatchShortestPath, GraphMatchStep,
            PathFunction, Projection, ProjectionExpression,
        };
        let (query, parameters) = fixture(20);
        let statement = cypher::parse(&query).unwrap();
        let window = Window::new();
        let mut cache = PlanCache::with_byte_limits(Some(8), Some(16 << 20), Some(8 << 20));
        let (key, mut value, _) = cached(&query, &statement, &parameters);
        let spare = |value: &str| {
            let mut owned = String::with_capacity(1024);
            owned.push_str(value);
            owned
        };
        let program = GraphMatchProgram {
            imports: Vec::new(),
            introduced: vec!["a".into(), "b".into(), "p".into()],
            projected_nodes: BTreeMap::new(),
            projected_relationships: BTreeMap::new(),
            steps: vec![
                GraphMatchStep::Node(GraphMatchNode {
                    variable: "a".into(),
                    label: "Person".into(),
                    properties: BTreeMap::from([("id".into(), Value::Int(1))]),
                }),
                GraphMatchStep::Node(GraphMatchNode {
                    variable: "b".into(),
                    label: "Person".into(),
                    properties: BTreeMap::from([("id".into(), Value::Int(7))]),
                }),
                GraphMatchStep::ShortestPath(Box::new(GraphMatchShortestPath {
                    path: spare("p"),
                    source: spare("a"),
                    target: spare("b"),
                    rel_type: spare("KNOWS"),
                    direction: cypher::RelationshipDirection::Outgoing,
                    min_hops: 1,
                    max_hops: 4,
                    bounded: true,
                    selection,
                })),
            ],
            predicate: None,
            predicate_steps: Vec::new(),
            edge_predicates: Vec::new(),
            optional: true,
            hash_join: None,
            id_set_conjuncts: Vec::new(),
            seeded_union: Vec::new(),
        };
        let items = [
            PathFunction::Length,
            PathFunction::Nodes,
            PathFunction::Relationships,
            PathFunction::NodeProperties(spare("name")),
        ]
        .into_iter()
        .enumerate()
        .map(|(index, function)| Projection {
            name: format!("path_field_{index}"),
            expression: ProjectionExpression::Path {
                function,
                column: spare("p"),
            },
        })
        .collect();
        Arc::get_mut(&mut value).unwrap().physical_template = PhysicalPlan::ProjectExec {
            items,
            input: Box::new(PhysicalPlan::GraphMatchExec {
                program,
                input: None,
            }),
        };
        let charge = key
            .retained_heap_bytes()
            .unwrap()
            .checked_add(value.retained_heap_bytes().unwrap())
            .unwrap();
        cache.insert_weighted(key, value, charge);
        assert_eq!(cache.stats().entries, 1);
        assert!(window.retained() <= cache.charged_bytes().unwrap());
        drop(cache);
        assert_eq!(window.retained(), 0);
    }
    let (query, parameters) = fixture(20);
    let statement = cypher::parse(&query).unwrap();
    let entry_charge = cached(&query, &statement, &parameters).2;
    let budget = entry_charge.checked_mul(4).unwrap();
    let window = Window::new();
    let mut cache = PlanCache::with_byte_limits(Some(128), Some(budget), Some(entry_charge * 2));
    for index in 0..32 {
        let (mut key, value, _) = cached(&query, &statement, &parameters);
        if index % 2 == 0 {
            key.scoring = Some(ScoringPlanCacheKey::Host {
                plan: hawdb_plan_cypher::HostScoringPlan::new(
                    "host-retention".repeat(16),
                    "version".repeat(16),
                    std::num::NonZeroU64::new(1).unwrap(),
                    "score".repeat(16),
                    std::num::NonZeroUsize::new(512).unwrap(),
                    20,
                )
                .unwrap()
                .with_graph_seed_input("seed".repeat(16), "candidate".repeat(16))
                .unwrap(),
                candidate_window: true,
            });
        }
        key.normalized_query
            .push_str(&format!(" /* shape-{index} */"));
        let charge = key
            .retained_heap_bytes()
            .unwrap()
            .checked_add(value.retained_heap_bytes().unwrap())
            .unwrap();
        cache.insert_weighted(key, value, charge);
        assert!(window.retained() <= cache.charged_bytes().unwrap());
        assert!(cache.charged_bytes().unwrap() <= budget);
    }
    assert!(cache.stats().evictions > 0);
    drop(cache);
    assert_eq!(window.retained(), 0);
}

fn row_unwind_refuses_large_output_before_copying() {
    use hawdb_executor::binding::Binding;
    use hawdb_executor::observer::NoopExecutionObserver;
    use hawdb_executor::pipeline::{
        BatchControl, BatchExecutionContext, BindingBatch, BindingBatchSource,
    };
    use hawdb_executor::{ExecutionLimit, ExecutionMemoryConfig, QueryMemoryLedger};
    use hawdb_plan_cypher::ProjectionExpression;
    use std::num::NonZeroUsize;
    struct OneRow;
    impl BindingBatchSource for OneRow {
        fn execute(
            &mut self,
            _: &PhysicalPlan,
            _: ExecutionLimit,
            emit: &mut dyn FnMut(BindingBatch) -> hawdb_core::Result<BatchControl>,
        ) -> hawdb_core::Result<BatchControl> {
            emit(vec![Binding::values(BTreeMap::new())])
        }
    }
    let expression =
        ProjectionExpression::Literal(Value::List(vec![Value::String("x".repeat(1 << 20))]));
    let catalog = Catalog::default();
    let memory = ExecutionMemoryConfig {
        batch_payload_bytes: NonZeroUsize::new(1024).unwrap(),
        ..ExecutionMemoryConfig::default()
    };
    let ledger = QueryMemoryLedger::new(memory.query_memory_bytes);
    let window = Window::new();
    let error = hawdb_executor::transform::stream_unwind_rows_batches(
        hawdb_executor::transform::UnwindRows {
            expression: &expression,
            variable: "item",
            consumes_source: false,
        },
        &PhysicalPlan::EmptyExec,
        &mut OneRow,
        BatchExecutionContext {
            catalog: &catalog,
            memory: &memory,
            memory_ledger: &ledger,
            task_context: None,
            observer: &NoopExecutionObserver,
            graph: None,
        },
        ExecutionLimit::unlimited(),
        &mut |_| panic!("over-budget output"),
    )
    .unwrap_err();
    assert!(error.to_string().contains("batch_payload_bytes"), "{error}");
    assert!(
        LARGEST.load(Ordering::Relaxed) < 1 << 20,
        "UNWIND cloned the rejected payload"
    );
    assert_eq!(ledger.snapshot().used_bytes, 0);
    drop(error);
    drop(window);
}