hirn-query
⚠️ Experimental: This project is under active development. APIs, on-disk formats, and behaviour may change without notice. Not recommended for production use.
HirnQL parser, typed AST, and DataFusion plan compiler for the hirn cognitive memory database.
Overview
This crate provides the complete query pipeline for HirnQL — hirn's declarative query language. It contains the parser, typed AST analysis, and multi-stage compilation pipeline that transforms HirnQL text into DataFusion LogicalPlan trees.
Architecture
7-Stage Query Pipeline
┌─────────┐ ┌────────┐ ┌─────────┐ ┌─────────┐ ┌───────┐ ┌─────────┐ ┌─────────┐
│ Parse │ → │ Limits │ → │ Analyze │ → │ Compile │ → │ Cache │ → │ Explain │ → │ Execute │
└─────────┘ └────────┘ └─────────┘ └─────────┘ └───────┘ └─────────┘ └─────────┘
- Parse — Pest PEG grammar → raw AST (
Statement) - Limits — DoS protection (max query length, expand depth, limit)
- Analyze — Namespace resolution, type validation →
TypedStatement - Compile —
TypedStatement→ DataFusionLogicalPlan(tree ofHirnPlanNodeextension nodes) - Cache — DashMap-based
PlanCachewith LRU eviction - Explain — Optional
EXPLAIN [ANALYZE]formatting - Execute — Handed to
hirn-execfor physical plan conversion
Plan Compilation
Each statement compiles to a pipeline of hirn extension nodes:
| Statement | Pipeline |
|---|---|
| RECALL | [QueryComplexity] → HybridSearch → [GraphActivation] → [CausalChain] → HebbianBuffer → [ContextBudget] |
| THINK | [QueryComplexity] → HybridSearch → [GraphActivation] → [IterativeRetrieval] → QualityGate → HebbianBuffer → ContextBudget |
| REMEMBER | RpeScore → ProspectiveIndexing → SvoExtraction → InterferenceDetector → Remember |
| CONSOLIDATE | ImperativeBoundary(Consolidate) |
| TRAVERSE | Traverse (leaf) |
Brackets indicate conditionally emitted operators based on query clauses.
Module Layout
src/
├── parser/
│ ├── hirnql.pest # PEG grammar (Pest) — all clause orderings defined here
│ ├── parse.rs # Parser: text → raw AST, DoS limits enforcement
│ └── ast.rs # Raw AST types (Statement, RecallStmt, ThinkStmt, etc.)
├── compiler/
│ ├── typed_ast.rs # Analyze: raw AST → TypedStatement (resolve namespaces, validate)
│ ├── plan_compiler.rs # Compile: TypedStatement → DataFusion LogicalPlan
│ └── pipeline.rs # QueryPipeline: orchestrates all stages, PlanCache
└── lib.rs # Public re-exports
Usage
use ;
// Parse
let stmt = parse?;
// Analyze (resolve types)
let ctx = default;
let typed = analyze?;
// Compile to DataFusion plan
let plan = compile?;
Or use the full pipeline:
use ;
let cache = new;
let pipeline = new;
let compiled = pipeline.compile?;
Key Concepts
Depth Scheduling
DEPTH AUTO— classify query complexity automatically (default)DEPTH FULL— always run full pipelineDEPTH SUMMARY— skip graph activation for faster results
Cognitive Operators
- QueryComplexity — classifies query as Simple/Medium/Complex
- QualityGate — confidence-based fallback to LLM deliberation
- IterativeRetrieval — multi-hop retrieve→reformulate→retrieve loop
- InterferenceDetector — duplicate/conflict detection before writes
Grammar Clause Ordering
Clauses must appear in grammar-defined order. Misordering causes parse errors. See src/parser/hirnql.pest for exact ordering.