hirn-query 0.1.0

HirnQL parser, AST, and compiler for the hirn cognitive memory database
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
//! `QueryPipeline` — 7-stage HirnQL compilation and execution pipeline.
//!
//! ```text
//! Stage 1: Parse     — HirnQL text → AST
//! Stage 2: Analyze   — AST → TypedStatement (namespace resolution, validation)
//! Stage 3: Rewrite   — logical plan rewrite pass (no-op; Cedar policy runs at
//!                       physical optimizer level via `PolicyPushdownRule` in hirn-exec)
//! Stage 4: Plan      — TypedStatement → DataFusion LogicalPlan
//! Stage 5: Optimize  — DataFusion optimizer + custom rules
//! Stage 6: Execute   — LogicalPlan → PhysicalPlan → RecordBatchStream
//! Stage 7: Collect   — RecordBatchStream → results
//! ```
//!
//! Stages 1–4 live here (pure transformations). Stages 5–7 require a
//! DataFusion `SessionContext` and live in `hirn-engine`.

use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::sync::Arc;

use dashmap::DashMap;
use datafusion_expr::LogicalPlan;
use datafusion_expr::logical_plan::Extension;
use parking_lot::Mutex;

use hirn_core::error::{HirnError, HirnResult};

use super::plan_compiler;
use super::typed_ast::{self, AnalyzeContext, TypedStatement};
use crate::parser;
use crate::parser::ast::Statement;

/// Compiled query ready for optimization + execution (stages 5–7).
#[derive(Debug, Clone)]
pub struct CompiledPlan {
    /// The original query text.
    pub source: String,
    /// The raw parsed AST.
    pub ast: Statement,
    /// The resolved typed AST.
    pub typed: TypedStatement,
    /// DataFusion logical plan.
    pub plan: LogicalPlan,
}

/// DashMap-backed plan cache with O(log N) LRU eviction via a min-heap.
///
/// Cache key is a hash of normalized query text. Cache entries store the
/// normalized source string so that 64-bit hash collisions are detected
/// and rejected rather than silently returning a wrong plan (N-M19).
///
/// # Eviction (N-H14)
///
/// A `BinaryHeap<(Reverse<u64>, u64)>` tracks `(Reverse<access_count>, key)`
/// pairs. Eviction pops from the min-heap using lazy deletion: entries whose
/// `access_count` has increased since they were pushed to the heap are skipped
/// and a fresh entry is pushed so the updated count is reflected.
pub struct PlanCache {
    entries: DashMap<u64, CacheEntry>,
    /// Min-heap for O(log N) eviction. Entries are `(Reverse<access_count>, key)`.
    /// Stale heap entries (count changed) are skipped lazily during eviction.
    eviction_heap: Mutex<BinaryHeap<(Reverse<u64>, u64)>>,
    max_entries: usize,
}

#[derive(Clone)]
struct CacheEntry {
    /// Normalized source query used to detect 64-bit hash collisions (N-M19).
    normalized_source: Arc<str>,
    plan: Arc<CompiledPlan>,
    access_count: u64,
}

impl PlanCache {
    /// Create a plan cache with the given maximum number of entries.
    pub fn new(max_entries: usize) -> Self {
        Self {
            entries: DashMap::with_capacity(max_entries.min(256)),
            eviction_heap: Mutex::new(BinaryHeap::with_capacity(max_entries.min(256))),
            max_entries,
        }
    }

    /// Look up a cached plan by query hash.
    ///
    /// Returns `None` on a miss **or** on a hash collision (N-M19): the caller
    /// must pass the same normalized source string that was used to compute
    /// `key` so that the stored string is compared for equality before serving
    /// the cached plan.
    pub fn get(&self, key: u64, normalized_source: &str) -> Option<Arc<CompiledPlan>> {
        self.entries.get_mut(&key).and_then(|mut entry| {
            // Reject hash collisions: key matches but source differs.
            if entry.normalized_source.as_ref() != normalized_source {
                tracing::warn!(
                    key,
                    cached_source = %entry.normalized_source,
                    incoming_source = %normalized_source,
                    "plan cache: 64-bit hash collision — skipping cached plan"
                );
                return None;
            }
            entry.access_count += 1;
            // Push the updated count into the heap so the lazy-deletion
            // eviction always reflects the most recent access frequency.
            self.eviction_heap
                .lock()
                .push((Reverse(entry.access_count), key));
            Some(Arc::clone(&entry.plan))
        })
    }

    /// Insert a compiled plan. Evicts the least-recently-used entry when at
    /// capacity using O(log N) heap-pop with lazy deletion (N-H14).
    ///
    /// Concurrent `put` calls may temporarily exceed `max_entries` by the number
    /// of concurrent writers; the cap is enforced on a best-effort basis without
    /// a global write lock (N-M03).
    pub fn put(&self, key: u64, normalized_source: Arc<str>, plan: Arc<CompiledPlan>) {
        if self.entries.len() >= self.max_entries {
            let evicted = self.try_evict_one();
            // If the heap was exhausted (all entries freshly accessed), force-remove
            // an arbitrary entry so the cache stays bounded (N-M03).
            if !evicted && self.entries.len() >= self.max_entries {
                // Eagerly extract the key before the if-let so the DashMap iterator
                // guard is dropped before the subsequent remove (N-M03 / significant-drop).
                let arbitrary_key = self.entries.iter().next().map(|e| *e.key());
                if let Some(entry) = arbitrary_key {
                    self.entries.remove(&entry);
                }
            }
        }
        self.eviction_heap.lock().push((Reverse(1), key));
        self.entries.insert(
            key,
            CacheEntry {
                normalized_source,
                plan,
                access_count: 1,
            },
        );
    }

    /// Pop the min-heap until one live (non-stale) entry is evicted.
    /// Returns `true` if an entry was removed from `self.entries`.
    fn try_evict_one(&self) -> bool {
        let mut heap = self.eviction_heap.lock();
        loop {
            match heap.pop() {
                None => return false,
                Some((Reverse(snapshot_count), evict_key)) => {
                    // `remove_if` is atomic: evict only if count matches snapshot.
                    if self
                        .entries
                        .remove_if(&evict_key, |_, v| v.access_count == snapshot_count)
                        .is_some()
                    {
                        return true;
                    }
                    // Count changed — this heap entry is stale; try next.
                }
            }
        }
    }

    /// Number of cached entries.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Whether the cache is empty.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Remove all cached entries.
    pub fn clear(&self) {
        self.entries.clear();
    }
}

impl std::fmt::Debug for PlanCache {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PlanCache")
            .field("len", &self.entries.len())
            .field("max_entries", &self.max_entries)
            // eviction_heap omitted — locking during Debug is undesirable.
            .finish_non_exhaustive()
    }
}

/// The 7-stage query pipeline. Stages 1–4 are executed here; stages 5–7
/// are deferred to the engine which holds the `SessionContext`.
pub struct QueryPipeline {
    ctx: AnalyzeContext,
    cache: Option<Arc<PlanCache>>,
}

impl QueryPipeline {
    /// Create a new pipeline with the given context.
    pub fn new(ctx: AnalyzeContext) -> Self {
        Self { ctx, cache: None }
    }

    /// Attach a shared plan cache.
    pub fn with_cache(mut self, cache: Arc<PlanCache>) -> Self {
        self.cache = Some(cache);
        self
    }

    /// Run stages 1–4, returning a `CompiledPlan`.
    ///
    /// If a cache is attached and the query was previously compiled *with the
    /// same default namespace*, returns the cached plan directly.
    ///
    /// Use [`compile_with_ctx`] to override the default [`AnalyzeContext`] on a
    /// per-call basis (e.g. for requests from a specific agent whose default
    /// namespace differs from `self.ctx`).
    pub fn compile(&self, query: &str) -> HirnResult<Arc<CompiledPlan>> {
        self.compile_with_ctx(query, &self.ctx)
    }

    /// Run stages 1–4 with an explicit [`AnalyzeContext`], returning a `CompiledPlan`.
    ///
    /// The plan cache key is mixed with the caller-supplied default namespace so
    /// that two requests for the same query text but different default namespaces
    /// receive correctly resolved plans (not the same cached entry).
    ///
    /// If a cache is attached and there is a hit for `(query, ctx.default_namespace)`,
    /// returns the cached plan directly (skipping parse + analyze + plan).
    pub fn compile_with_ctx(&self, query: &str, ctx: &AnalyzeContext) -> HirnResult<Arc<CompiledPlan>> {
        let (normalized, base_key) = plan_compiler::query_normalize_and_hash(query);
        // Mix the default-namespace interned ID into the cache key so that the
        // same query text compiled under different default namespaces produces
        // independent cache entries.  Uses FNV-style mix to avoid trivial
        // cancellation.
        let ns_id = ctx.default_namespace.as_interned_id();
        let key = base_key
            .wrapping_mul(0x9e37_79b9_7f4a_7c15_u64)
            .wrapping_add(ns_id as u64);

        // Cache hit? Verified against stored normalized source to catch hash
        // collisions (N-M19).
        if let Some(ref cache) = self.cache {
            if let Some(plan) = cache.get(key, &normalized) {
                return Ok(plan);
            }
        }

        // Stage 1: Parse.
        let ast = parser::parse(query)
            .map_err(|e| HirnError::InvalidInput(format!("parse error: {e}")))?;

        // Stage 2: Analyze.
        let typed = typed_ast::analyze(&ast, ctx)?;

        // Stage 3: Rewrite — logical rewrite pass.  Policy enforcement is handled at
        // the *physical* optimizer level by `PolicyPushdownRule` in `hirn-exec`;
        // this stage is reserved for future logical-level rewrites (e.g. expansion
        // macros, cross-namespace normalization).
        let typed = self.rewrite(typed)?;

        // Stage 4: Plan.
        let plan = plan_compiler::compile(&typed)?;

        let compiled = Arc::new(CompiledPlan {
            source: query.to_string(),
            ast,
            typed,
            plan,
        });

        // Store in cache with normalized source for future collision checks (N-M19).
        if let Some(ref cache) = self.cache {
            cache.put(key, normalized.into(), Arc::clone(&compiled));
        }

        Ok(compiled)
    }

    /// Stage 3: Rewrite — logical rewrite pass.
    ///
    /// Currently a no-op; Cedar policy enforcement runs later as
    /// `PolicyPushdownRule` in the DataFusion physical optimizer (hirn-exec).
    /// Reserved for future logical-level rewrites.
    fn rewrite(&self, typed: TypedStatement) -> HirnResult<TypedStatement> {
        Ok(typed)
    }

    /// Format a query's logical plan as an indented text tree (like PostgreSQL EXPLAIN).
    ///
    /// Returns the plan tree as a `String`. The plan is compiled through stages 1–4
    /// and then formatted using DataFusion's `display_indent_schema()`.
    pub fn explain(&self, query: &str) -> HirnResult<String> {
        let compiled = self.compile(query)?;
        Ok(format_plan_tree(&compiled.plan))
    }

    /// Access the analyze context.
    pub fn context(&self) -> &AnalyzeContext {
        &self.ctx
    }
}

/// Format a DataFusion `LogicalPlan` as an indented plan tree.
///
/// Each operator is printed on its own line with 2-space indentation per depth level,
/// similar to PostgreSQL's `EXPLAIN` output.
pub fn format_plan_tree(plan: &LogicalPlan) -> String {
    let mut lines = Vec::new();
    format_plan_node(plan, 0, &mut lines);
    lines.join("\n")
}

fn format_plan_node(plan: &LogicalPlan, depth: usize, lines: &mut Vec<String>) {
    let indent = "  ".repeat(depth);
    lines.push(format!("{}{}", indent, plan_node_label(plan)));
    for child in plan.inputs() {
        format_plan_node(child, depth + 1, lines);
    }
}

fn plan_node_label(plan: &LogicalPlan) -> String {
    match plan {
        LogicalPlan::Extension(Extension { node }) => node.name().to_string(),
        _ => plan.display().to_string(),
    }
}

impl std::fmt::Debug for QueryPipeline {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("QueryPipeline")
            .field("ctx", &self.ctx)
            .field("cache", &self.cache.is_some())
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;

    fn pipeline() -> QueryPipeline {
        QueryPipeline::new(AnalyzeContext::default())
    }

    #[test]
    fn compile_recall_produces_plan() {
        let p = pipeline();
        let result = p.compile(r#"RECALL episodic ABOUT "test" LIMIT 5"#);
        assert!(result.is_ok());
        let compiled = result.unwrap();
        assert!(matches!(compiled.ast, Statement::Recall(_)));
        assert!(matches!(compiled.typed, TypedStatement::Recall(_)));
        let display = format!("{}", compiled.plan);
        assert!(display.contains("HybridSearch"), "plan: {display}");
    }

    #[test]
    fn compile_think_produces_plan() {
        let p = pipeline();
        let compiled = p.compile(r#"THINK ABOUT "test" BUDGET 4096"#).unwrap();
        assert!(matches!(compiled.typed, TypedStatement::Think(_)));
        let display = format!("{}", compiled.plan);
        assert!(display.contains("QualityGate"), "plan: {display}");
    }

    #[test]
    fn compile_rejects_removed_embedded_mutation_verbs() {
        let p = pipeline();
        for query in [
            r#"REMEMBER episode CONTENT "event happened""#,
            r#"FORGET "01J000000000000000000000""#,
            "WATCH ALL FORMAT json",
            "CONSOLIDATE WHERE episodic.access_count > 5",
        ] {
            let err = p.compile(query).unwrap_err();
            assert!(
                err.to_string().contains("not supported"),
                "unexpected error for `{query}`: {err}"
            );
        }
    }

    #[test]
    fn compile_parse_error() {
        let p = pipeline();
        let err = p.compile("NOT_A_QUERY").unwrap_err();
        assert!(matches!(err, HirnError::InvalidInput(_)));
    }

    #[test]
    fn cache_hit() {
        let cache = Arc::new(PlanCache::new(100));
        let p = pipeline().with_cache(cache.clone());
        let q = r#"RECALL episodic ABOUT "test" LIMIT 10"#;
        p.compile(q).unwrap();
        assert_eq!(cache.len(), 1);
        // Second compile should hit cache.
        p.compile(q).unwrap();
        assert_eq!(cache.len(), 1);
    }

    #[test]
    fn cache_different_queries() {
        let cache = Arc::new(PlanCache::new(100));
        let p = pipeline().with_cache(cache.clone());
        p.compile(r#"RECALL episodic ABOUT "a""#).unwrap();
        p.compile(r#"RECALL episodic ABOUT "b""#).unwrap();
        assert_eq!(cache.len(), 2);
    }

    #[test]
    fn cache_eviction() {
        let cache = Arc::new(PlanCache::new(2));
        let p = pipeline().with_cache(cache.clone());
        p.compile(r#"RECALL episodic ABOUT "a""#).unwrap();
        p.compile(r#"RECALL episodic ABOUT "b""#).unwrap();
        assert_eq!(cache.len(), 2);
        p.compile(r#"RECALL episodic ABOUT "c""#).unwrap();
        assert_eq!(cache.len(), 2); // One evicted.
    }

    #[test]
    fn cache_clear() {
        let cache = Arc::new(PlanCache::new(100));
        let p = pipeline().with_cache(cache.clone());
        p.compile(r#"RECALL episodic ABOUT "a""#).unwrap();
        p.compile(r#"RECALL episodic ABOUT "b""#).unwrap();
        assert_eq!(cache.len(), 2);
        cache.clear();
        assert!(cache.is_empty());
    }

    #[test]
    fn pipeline_without_cache() {
        let p = pipeline();
        let compiled = p.compile(r#"RECALL episodic ABOUT "test""#).unwrap();
        assert!(!compiled.source.is_empty());
    }

    #[test]
    fn stages_independently_callable() {
        // Stage 1: Parse.
        let ast = parser::parse(r#"RECALL episodic ABOUT "test" LIMIT 5"#).unwrap();
        // Stage 2: Analyze.
        let ctx = AnalyzeContext::default();
        let typed = typed_ast::analyze(&ast, &ctx).unwrap();
        // Stage 4: Plan.
        let plan = plan_compiler::compile(&typed).unwrap();
        let display = format!("{plan}");
        assert!(display.contains("HybridSearch"), "plan: {display}");
    }

    #[test]
    fn explain_returns_plan_tree() {
        let p = pipeline();
        let tree = p
            .explain(r#"RECALL episodic ABOUT "test" LIMIT 5"#)
            .unwrap();
        // Plan tree should contain operator names at different indentation levels.
        assert!(
            tree.contains("HybridSearch") || tree.contains("Limit"),
            "plan tree: {tree}"
        );
        // Should have multiple lines (indented children).
        assert!(tree.lines().count() > 1, "plan tree: {tree}");
    }

    #[test]
    fn explain_correct_shows_extension_name() {
        let p = pipeline();
        let tree = p
            .explain(r#"EXPLAIN CORRECT "01ARZ3NDEKTSV4RRFFQ69G5FAV" SET description = "updated""#)
            .unwrap();
        assert!(tree.contains("HirnDirectCorrect"), "plan tree: {tree}");
    }

    #[test]
    fn explain_supersede_shows_extension_name() {
        let p = pipeline();
        let tree = p
            .explain(
                r#"EXPLAIN SUPERSEDE "01ARZ3NDEKTSV4RRFFQ69G5FAV" SET description = "replacement""#,
            )
            .unwrap();
        assert!(tree.contains("HirnDirectSupersede"), "plan tree: {tree}");
    }

    #[test]
    fn explain_merge_memory_shows_extension_name() {
        let p = pipeline();
        let tree = p
            .explain(
                r#"EXPLAIN MERGE MEMORY "01ARZ3NDEKTSV4RRFFQ69G5FAA" INTO "01ARZ3NDEKTSV4RRFFQ69G5FAV""#,
            )
            .unwrap();
        assert!(tree.contains("HirnDirectMergeMemory"), "plan tree: {tree}");
    }

    #[test]
    fn explain_history_shows_extension_name() {
        let p = pipeline();
        let tree = p
            .explain(r#"EXPLAIN HISTORY "01ARZ3NDEKTSV4RRFFQ69G5FAV" NAMESPACE custom"#)
            .unwrap();
        assert!(
            tree.contains("HirnSemanticHistoryScan"),
            "plan tree: {tree}"
        );
    }

    #[test]
    fn explain_retract_shows_extension_name() {
        let p = pipeline();
        let tree = p
            .explain(r#"EXPLAIN RETRACT "01ARZ3NDEKTSV4RRFFQ69G5FAV" REASON "obsolete""#)
            .unwrap();
        assert!(tree.contains("HirnDirectRetract"), "plan tree: {tree}");
    }

    #[test]
    fn explain_of_cached_query_still_shows_plan() {
        let cache = Arc::new(PlanCache::new(10));
        let p = pipeline().with_cache(cache);
        // First call compiles and caches.
        let tree1 = p.explain(r#"RECALL episodic ABOUT "test""#).unwrap();
        // Second call hits cache but should still produce the same plan tree.
        let tree2 = p.explain(r#"RECALL episodic ABOUT "test""#).unwrap();
        assert_eq!(tree1, tree2);
        assert!(!tree1.is_empty());
    }

    #[test]
    fn format_plan_tree_indents_children() {
        let p = pipeline();
        let compiled = p
            .compile(r#"RECALL episodic ABOUT "test" EXPAND GRAPH DEPTH 2 LIMIT 5"#)
            .unwrap();
        let tree = super::format_plan_tree(&compiled.plan);
        // Root should start at column 0, children should be indented.
        let lines: Vec<&str> = tree.lines().collect();
        assert!(!lines.is_empty());
        // First line has no indentation.
        assert!(!lines[0].starts_with(' '), "root: {}", lines[0]);
        // If there are children, they should be indented.
        if lines.len() > 1 {
            assert!(lines[1].starts_with("  "), "child: {}", lines[1]);
        }
    }

    #[test]
    fn cached_query_executes_under_5us() {
        let cache = Arc::new(PlanCache::new(100));
        let p = pipeline().with_cache(cache.clone());
        // Warm up: compile and cache
        p.compile(r#"RECALL episodic ABOUT "test""#).unwrap();
        // Measure cached hit
        let start = std::time::Instant::now();
        let iterations = 1000;
        for _ in 0..iterations {
            let _ = p.compile(r#"RECALL episodic ABOUT "test""#).unwrap();
        }
        let elapsed = start.elapsed();
        let per_op = elapsed / iterations;
        assert!(
            per_op.as_micros() < 5,
            "cached query took {per_op:?} per op, expected < 5µs"
        );
    }
}