Jelq
A small pipeline language for NDJSON streams. Numbers are exact decimals (wei, IDs, prices),
and the stream is never buffered. It either gives the exact answer or stops and names the line.
Spec: SPEC.md. Interpreter: one file, jelq.py, standard library only (Python ≥ 3.11).
Coming from jq? See docs/jq-users.md. Worked examples: examples/.
pip install . # from a checkout; gives you the `jelq` command
jelq PROGRAM [FILE ...] # NDJSON in (files or stdin), NDJSON out
jelq -f program.jelq data.ndjson # program from a file
jelq --help # stages, builtins, number rules
# The example from the design brief
jelq '. |> filter(.amount > 1_000_000) |> map({id: .id, value: .amount}) |> acc $acc + .value with 0'
# Per-account wei totals; amounts given as hex or decimal strings, uint256-exact
jelq 'sum(int(.wei)) by .acct'
# Revenue per SKU from decimal prices: exact to the cent (others print 5001275192.630049)
jelq 'sum(.price * .qty) by .sku'
# Prices shipped as strings, an average rounded only where you say so
jelq 'map({sku: .sku, avg: div(num(.total), .count, 2)})'
# Progressive refinement: a running total after every record, as strings for JS consumers
tail -f ticks.ndjson | jelq 'filter(.side == "buy") |> scan $acc + .qty with 0 |> map(str(.))'
# Log lines about failed payouts, with exact millisecond timestamps
jelq 'filter(test(lower(.msg), "payout failed")) |> map({at: fromdate(.ts), msg: .msg})'
# take() terminates even on an infinite stream
yes '{"n":1}' | jelq 'scan $acc + .n with 0 |> take(3)'
Stages: filter, map, expand, take, acc, scan (plus sum, count, min and max,
which are shorthand for acc). Each of them, with or without by KEY grouping where it
applies, is defined in SPEC.md §4.
Numbers:
0.1 + 0.2 is 0.3, 0.07 * 100 is 7.00, and 13 / 4 is 3.25.
1 / 3 is an error, because it has no exact decimal form. Round explicitly with
div(1, 3, 2) → 0.33 or round(x, 2), both half to even.
- There is no digit cap: two 4-million-digit integers sum in 0.14 s (SPEC.md §1).
Strings and dates:
test, split, join, startswith, endswith, lower, upper.
fromdate and todate convert ISO 8601 dates to and from exact seconds. Nanoseconds
survive. A time with no timezone is an error, never a guess.
What it will not do: binary floats, implicit rounding, truthiness (filter(.x) needs a
boolean), sorting or anything else that needs the whole stream.
Tests
python3 tests/test_jelq.py # or: pytest tests/
There are 53 tests (a few seconds), in five groups:
- Exact integers: 2⁵³+1 round-trips byte for byte, every operator matches Python
int
on random 300-digit numbers, million-digit inputs work, and uint256 and hex conversion
are exact.
- Exact decimals: every operator,
/ and div() are checked against fractions.Fraction
on random decimals (up to 40 places, 90 digits). The money cases floats get wrong
are covered, as are half-even rounding and the exponent-notation guard.
- Streaming:
take pulls exactly what it needs from an infinite source, and the first
output arrives before later input is read. Peak memory is flat from 5k to 50k records,
with a negative control. The CLI emits output while stdin is still open.
- Semantics: grouping, empty streams, strict typing, error line numbers, parse errors
with a caret, CLI options, string and date functions (dates checked against Python
datetime arithmetic), and the examples.
- Fuzzing:
python3 tests/fuzz.py N SEED generates random programs and records. The only
error allowed is a clean Jelq error; output must never contain a float, must survive an
encode/decode round trip, and must be deterministic. 100,000 programs over 4 seeds:
0 violations. A short run is part of the suite.
Comparison with jq, gojq, jaq
See bench/RESULTS.md. The short version:
- On 15 big-integer tasks, jq 1.8.2 is silently wrong on 10. gojq is exact on all 15, the
same as Jelq.
- On 9 decimal-money tasks, Jelq is correct on all 9. Every other tool is correct on 1 and
prints a wrong amount on the other 8 (
0.9999999999999999, 7.000000000000001,
5001275192.630049).
- At 1M records Jelq is about as fast as jq and gojq, and about 2× slower than jaq.
License
Licensed under either of Apache License, Version 2.0 or MIT license,
at your option. Unless you explicitly state otherwise, any contribution you submit for inclusion
in this project is dual licensed as above, without additional terms or conditions.