You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file provides guidance to AI agents (Claude Code, GitHub Copilot, Cursor, etc.) when working with code in this repository.
4
4
5
-
## What Is Gnata
6
-
7
-
Gnata is a full JSONata 2.x implementation in Go, built for production streaming workloads. JSONata is a query and transformation language for JSON data. Gnata provides two-tier evaluation: fast-path (GJSON zero-copy) for simple expressions and full AST evaluation for complex ones, plus a lock-free `StreamEvaluator` for high-throughput batched evaluation.
5
+
## Project Overview
6
+
7
+
gnata-sqlite is a fork of [RecoLabs/gnata](https://github.com/RecoLabs/gnata) — a full JSONata 2.x implementation in Go. This fork extends it with:
8
+
9
+
-**SQLite C extension** (`sqlite/`) — registers `jsonata()`, `jsonata_query()`, and `jsonata_each` as SQL functions/virtual tables via CGo
- Pure-path fast path (e.g., `Account.Name`) — uses GJSON zero-copy
36
84
- Comparison fast path (e.g., `a.b = "x"`) — zero allocations
85
+
- Function fast path (e.g., `$exists(a.b)`) — direct GJSON evaluation
37
86
- Full AST evaluation required
38
87
39
88
### Two-Tier Evaluation
40
89
41
90
-`Eval(ctx, any)` — Evaluate against pre-parsed Go values via full AST walk
42
91
-`EvalBytes(ctx, json.RawMessage)` — Fast-path expressions use GJSON directly on raw JSON bytes; full-path falls back to unmarshal + Eval
43
92
44
-
### StreamEvaluator (stream.go)
93
+
### StreamEvaluator (`stream.go`)
45
94
46
95
Batch-evaluates multiple expressions against events. Schema-keyed `GroupPlan` caching deduplicates field extraction across expressions. Lock-free reads via `atomic.Pointer` snapshot; writes serialized by `sync.Mutex`. Single JSON scan per event via `gjson.GetManyBytes`.
47
96
48
-
### Evaluator Dispatch (internal/evaluator/)
97
+
### Query Planner (`internal/planner/`)
98
+
99
+
Decomposes JSONata expressions into `QueryPlan` for SQL streaming aggregation. Each plan contains:
100
+
-`Accumulators` — fed per row via `StepBatch` (single GJSON scan per row)
101
+
-`FinalExpr` — evaluated once at finalization
102
+
-`Predicates` — deduplicated, evaluated once per row; accumulators reference by index
103
+
- Used by `jsonata_query()` SQL aggregate function in the SQLite extension
104
+
105
+
### SQLite Bridge (`sqlite/`)
106
+
107
+
CGo extension that registers SQL functions with SQLite:
55+ built-in JSONata functions across categorized files: `string_funcs.go`, `string_match_replace.go`, `string_format_number.go`, `string_format_integer.go`, `string_encoding.go`, `numeric_funcs.go`, `array_funcs.go`, `object_funcs.go`, `hof_funcs.go`, `boolean_funcs.go`, `datetime_funcs.go`, `datetime_format.go`, `datetime_parse.go`. All registered via `functions.RegisterAll`.
136
+
Dispatch-map of `funcFastHandlers` maps each `FuncFastKind` to a standalone handler function (e.g., `evalFuncContains`, `evalFuncString`). Each handler operates directly on `gjson.Result` for zero-copy evaluation.
64
137
65
-
### Fast-Path Byte Evaluation (func_fast.go)
138
+
##Key Types
66
139
67
-
Dispatch-map of `funcFastHandlers` maps each `FuncFastKind` to a standalone handler function (e.g., `evalFuncContains`, `evalFuncString`). Each handler operates directly on `gjson.Result` for zero-copy evaluation. `FuncFastRound` is intentionally absent — it requires banker's rounding handled by the full evaluator.
140
+
| Type | File | Description |
141
+
|------|------|-------------|
142
+
|`gnata.Expression`|`gnata.go`| Compiled, goroutine-safe JSONata expression with fast-path metadata |
143
+
|`gnata.StreamEvaluator`|`stream.go`| Batch evaluator with copy-on-write expression slice + `BoundedCache` for schema plans |
144
+
|`evaluator.Environment`|`internal/evaluator/env.go`| Lexical scope chain for variable bindings and function registry |
Only one direct dependency (pure Go, no CGo for core):
80
163
-`tidwall/gjson` — Zero-copy JSON field extraction for fast-path byte-level evaluation
81
164
82
-
Regex uses Go's standard `regexp` package (no external regex library).
83
-
84
-
## Testing
85
-
86
-
Tests use separate `_test` packages (`gnata_test`, `lexer_test`, `parser_test`). The primary test suite (`suite_test.go`) loads 1,200+ JSON test cases from `testdata/groups/` (100+ subdirectories) — each `.json` file is a case with `expr`, `dataset`, `bindings`, and `result` fields. Datasets live in `testdata/datasets/`. Additional unit tests in `evaluator_test.go` cover regression tests using table-driven test (TDT) style.
165
+
The `sqlite/` package requires CGo (links against SQLite via `sqlite3ext.h`). Regex uses Go's standard `regexp` package.
0 commit comments