Files
zen/core/expression/tests/spec/standard/05-closures-basic.csv
T
stefan-gorules 4a378bafc4 feat(expression): lane VM and typed-mask engine for batch and columna evaluation (#553)
* feat(expression): lane VM and typed-mask engine for batch and columnar evaluation

Adds a register-based lane VM (core/expression/src/lane) and a typed-mask
engine (core/expression/src/typed) that evaluate ZEN expressions over
batches and columns, matching the stack VM's results. zen-types gains
shape-backed objects and dot-path helpers; existing public APIs only grow.

The lane VM has no expression size limit: deep and wide expressions are
compiled and run with the same stack growth the parser uses.

ZEN_EXPRESSION_CONFIG.lane (off by default) switches Isolate::run_standard
and run_unary to the lane VM at runtime. Lane internals and the typed
module are doc(hidden); the documented entry points are LaneProgram,
LaneRunner, Session, Columns and Output.

Includes an executable specification of the expression language
(core/expression/tests/spec, 28,852 cases) with differential tests that
run every lane path against the stack VM.

zen-expression and zen-types now declare rust-version 1.88.

* refactor(types): remove the shape transition cap

Shapes are freed when nothing uses them, so the 4,096-children cap per
shape no longer bounds memory. Shape::with, Shape::of and Shape::without
now always return a shape; objects only switch to a hash map past
SPILL_AT keys, as on master.

* fix(expression): debug-profile stack safety and Windows test build

CI runs tests in the debug profile, where frames are larger and tail
recursion is not optimised. Recursive lane, typed and parser helpers now
grow the stack like the rest (Const::number, the kernel builder, typed
lowering and lookup, lane compile helpers, Node::member_key).

The parser's closure depth counter is u32 instead of u8, so 256 nested
closures no longer wrap. median([]) and empty templates no longer
overflow in debug builds; release results are unchanged.

The deep/wide test runs on a 256 KiB stack so unprotected recursion
fails quickly in any profile. The conformance helper compares with
as_str(), which is unambiguous on Windows.

* fix(expression): stack checks in the lane executor, LF test data on Windows

In x86-64 debug builds Executor::cold has a ~93 KB frame, so nested
closures (execute -> cold -> body) could use more stack between two
checks than the stack-growth margin and overflow. execute and cold now
check the stack themselves; both run per instruction batch, not per row.

The expression test data is checked out with LF on every platform.
Windows runners converted it to CRLF, which broke multi-line spec cases.
2026-10-28 11:39:54 +01:00

8.7 KiB

1expression (string);input (json 5);output (json 5);note
2# map: element transformation, one result per element, order preserved
3map([1, 2, 3], # * 2);;[2, 4, 6]
4map([], # * 2);;[]
5map([7], # + 1);;[8]
6map([1, 2, 3], #);;[1, 2, 3]
7map([1, 2], 'x');;['x', 'x']
8map([1, 2], null);;[null, null]
9map([1, 2], 5);;[5, 5]
10map([1, 'a', true, null], #);;[1, 'a', true, null]
11map([1, 'a', true, null, [1], {a: 1}], type(#));;['number', 'string', 'bool', 'null', 'array', 'object']
12map([1, null, 3], # ?? 0);;[1, 0, 3]
13map([1, null, 3], # * 2);;!error
14map([1, 2, 3], # > 1 ? 'big' : 'small');;['small', 'big', 'big']
15map([1, 2, 3], # == 2 ? 'x' : null);;[null, 'x', null]
16map([0.1, 0.2], # + 0.1);;[0.2, 0.3]
17map([1.5, 2.25, -0.10], # * 2);;[3, 4.5, -0.2]
18map([1.5, 2.25, -0.10], string(# * 2));;['3.0', '4.50', '-0.20']
19map(['a', 'b', 'c'], # + '!');;['a!', 'b!', 'c!']
20map(['héllo', '日本', ''], upper(#));;['HÉLLO', '日本', '']
21map([true, false], not #);;[false, true]
22map([[1, 2], [], [3]], len(#));;[2, 0, 1]
23map([[1, 2], [3]], #);;[[1, 2], [3]]
24map([{a: 1}, {a: 2}], #);;[{a: 1}, {a: 2}]
25map([{a: 1}, {b: 2}], #.a);;[1, null]
26map([{a: {b: 1}}, {a: null}, {}], #.a.b);;[1, null, null]
27map([1, 2], [#, # * 10]);;[[1, 10], [2, 20]]
28map([1, 2], {v: #});;[{v: 1}, {v: 2}]
29map([1, 2], {v: #, [string(#)]: # * 2});;[{v: 1, '1': 2}, {v: 2, '2': 4}]
30map([1, 2], `n${#}`);;['n1', 'n2']
31map([d('2024-01-01'), d('2024-01-02')], #.day());;[1, 2]
32map([1, 0, 2], 1 / #);;[1, null, 0.5];question: division by zero inside a closure yields null (not an error); see operators chapter
33map([1, 2, 3], # % 2 == 0);;[false, true, false]
34# map: non-list first argument
35map(null, #);;!error;question: a null list is an error rather than an empty list
36map(5, #);;!error
37map('abc', #);;!error
38map(true, #);;!error
39map({a: 1}, #);;!error
40map(d('2024-01-01'), #);;!error
41# map: parse errors
42map([1, 2]);;!error
43map();;!error
44map([1, 2], #, 3);;!error
45map([1, 2], # , );;!error
46map([1, 2] as, #);;!error
47map([1, 2] as 1, #);;!error
48# map: element errors propagate
49map([1, 'a', 3], # * 2);;!error
50map([1, 2, 'a'], # * 2);;!error
51map(['a', 1], upper(#));;!error
52# filter: keeps elements whose predicate is true, preserves order and values
53filter([1, 2, 3, 4, 5], # > 3);;[4, 5]
54filter([], # > 3);;[]
55filter([4], # > 3);;[4]
56filter([1], # > 3);;[]
57filter([1, 2, 3], true);;[1, 2, 3]
58filter([1, 2, 3], false);;[]
59filter([true, false, true], #);;[true, true]
60filter([1, null, 3], # != null);;[1, 3]
61filter([1, 'a', true, null], # == 'a');;['a']
62filter([1, 'a', true, null], type(#) == 'number');;[1]
63filter([1.5, 2.25, -0.10], # < 2);;[1.5, -0.10]
64filter(['a', 'b', 'c', 'd'], # in ['a', 'c']);;['a', 'c']
65filter(['héllo', 'abc', 'hé'], startsWith(#, 'h'));;['héllo', 'hé']
66filter([{a: 1}, {a: null}, {}], #.a != null);;[{a: 1}]
67filter([{id: 1}, {id: 2}], #.id > 1);;[{id: 2}]
68filter([[1, 2], [3]], len(#) > 1);;[[1, 2]]
69filter([[1, 2], [3]], some(#, # > 2));;[[3]]
70filter([1, 2, 3], not (# == 2));;[1, 3]
71filter([1, 2, 3], !(# == 2));;[1, 3]
72filter([d('2024-01-01'), d('2024-03-02')], #.month() > 1);;['2024-03-02T00:00:00Z']
73# filter: predicate must be boolean
74filter([1, 2], 1);;!error
75filter([1, 2], 'yes');;!error
76filter([1, 2], null);;!error;question: a null predicate is an error, not treated as false
77filter([1, null, 3], #);;!error
78filter([1, 2], [#]);;!error
79filter([], 1);;[]
80# filter: non-list first argument and element errors
81filter(null, true);;!error
82filter(5, true);;!error
83filter('abc', true);;!error
84filter({a: 1}, true);;!error
85filter([1, 'a', 3], # > 1);;!error
86filter([1, 0, 2], 1 / # > 0);;!error
87# some: true when any element matches, stops at the first match
88some([1, 2, 3], # > 2);;true
89some([1, 2, 3], # > 3);;false
90some([], true);;false
91some([], # > 0);;false
92some([5], # > 3);;true
93some([1], # > 3);;false
94some(['a', 'b', 'c'], # == 'b');;true
95some(['a', 'b', 'c'], # == 'd');;false
96some([1, 'a', null], # == null);;true
97some([{a: 1}, {}], #.a == null);;true
98some([[1], [2, 3]], 3 in #);;true
99some([false, false], #);;false
100some([false, true], #);;true
101some([2, 'a'], # > 1);;true
102some(['a', 2], # > 1);;!error
103some([5, null], # > 3);;true
104some([1, null, 5], # > 3);;!error
105some([1, 2], 'a');;!error
106some([false, null], #);;!error;question: a null predicate result is an error, not falsy
107some(null, true);;!error
108some('abc', true);;!error
109some({a: 1}, true);;!error
110# all: true when every element matches, stops at the first failure
111all([1, 2, 3], # > 0);;true
112all([1, 2, 3], # > 1);;false
113all([], false);;true
114all([], # > 100);;true
115all([1], # > 0);;true
116all([0], # > 0);;false
117all(['a', 'b', 'c'], # in ['a', 'b', 'c', 'd']);;true
118all(['a', 'b', 'c'], # in ['a', 'b']);;false
119all([true, true], #);;true
120all([true, false], #);;false
121all([1, 2], # > 0 and # < 3);;true
122all([{a: 1}, {a: 2}], #.a > 0);;true
123all([{a: 1}, {}], #.a != null);;false
124all([0, 'a'], # > 0);;false
125all(['a', 0], # > 0);;!error
126all([1, 'a'], # > 0);;!error
127all([true, null], #);;!error;question: a null predicate result is an error, not falsy
128all([1, 2], 1);;!error
129all(null, true);;!error
130all(5, true);;!error
131# none: true when no element matches, stops at the first match
132none([1, 2, 3], # > 5);;true
133none([1, 2, 3], # > 2);;false
134none([], true);;true
135none([], # > 0);;true
136none([1], # == 1);;false
137none([1], # == 2);;true
138none(['a', 'b'], # == 'c');;true
139none([false, false], #);;true
140none([true, false], #);;false
141none([{a: 1}, {a: 2}], #.a > 5);;true
142none([5, 'a'], # > 1);;false
143none(['a', 5], # > 1);;!error
144none([1, 2], 'x');;!error
145none(null, true);;!error
146none({a: 1}, true);;!error
147# one: true when exactly one element matches
148one([1, 2, 3], # == 2);;true
149one([1, 2, 3], # == 5);;false
150one([1, 2, 3], # > 1);;false
151one([1, 2, 3], # > 0);;false
152one([], true);;false
153one([1], true);;true
154one([1], false);;false
155one([1, 1], # == 1);;false
156one(['a', 'b', 'c'], # == 'c');;true
157one([true, false], #);;true
158one([true, true], #);;false
159one([{a: 1}, {a: 2}, {}], #.a == 2);;true
160one([1, 1, 'a'], # == 1);;false
161one([1, 2, 'a'], # > 0);;!error;question: one() keeps evaluating after a second match, so a later erroring element fails the whole call
162one(['a', 1], # > 0);;!error
163one([1, 2], 1);;!error
164one(null, true);;!error
165one('abc', true);;!error
166# count: number of matching elements
167count([1, 2, 3, 4, 5, 2], # == 2);;2
168count([1, 2, 3], # > 5);;0
169count([], true);;0
170count([], # > 0);;0
171count([1], true);;1
172count([1, 2, 3], true);;3
173count([1, 2, 3], false);;0
174count([true, false, true], #);;2
175count([1, 'a', null, true], type(#) == 'string');;1
176count([{a: 1}, {a: 2}, {}], #.a != null);;2
177count([[1], [], [2, 3]], len(#) > 0);;2
178count(['héllo', 'hé', 'x'], contains(#, 'é'));;2
179count([1..10], # % 3 == 0);;3
180count([1, 2], 1);;!error
181count([1, 'a'], # > 0);;!error
182count([1], #);;!error
183count(null, true);;!error
184count({a: 1}, true);;!error
185# count agrees with len of filter and with len for an always-true predicate
186count([1, 2, 3, 4], # % 2 == 0) == len(filter([1, 2, 3, 4], # % 2 == 0));;true
187count([1, 2, 3], true) == len([1, 2, 3]);;true
188count([[1, 2, 3]], true);;1
189len([[1, 2, 3]]);;1
190# flatMap: map then flatten one level
191flatMap([[1, 2], [3, 4], [5, 6]], #);;[1, 2, 3, 4, 5, 6]
192flatMap([], #);;[]
193flatMap([[]], #);;[]
194flatMap([[], [1]], #);;[1]
195flatMap([1, 2], [#, # * 10]);;[1, 10, 2, 20]
196flatMap([1, 2], [#]);;[1, 2]
197flatMap([1, 2], #);;[1, 2]
198flatMap([1, [2, [3]]], #);;[1, 2, [3]]
199flatMap([[1], [2]], [#]);;[[1], [2]]
200flatMap([[1], null], #);;[1, null]
201flatMap([1, 2], []);;[]
202flatMap(['a', 'b'], [#, upper(#)]);;['a', 'A', 'b', 'B']
203flatMap([{t: ['a', 'b']}, {t: []}, {}], #.t);;['a', 'b', null]
204flatMap([{t: ['a', 'b']}, {t: []}, {}], #.t ?? []);;['a', 'b']
205flatMap([[1, 2], [3]], map(#, # + 1));;[2, 3, 4]
206flatMap(null, #);;!error
207flatMap('ab', #);;!error
208flatMap([1, 'a'], # * 2);;!error
209# intervals as closure sources
210map([0..3], #);;[0, 1, 2, 3]
211map((0..3], #);;[1, 2, 3]
212map([0..3), #);;[0, 1, 2]
213map((0..3), #);;[1, 2]
214map([1..1], #);;[1]
215map((1..1), #);;[]
216map([3..1], #);;[]
217map([-2..1], #);;[-2, -1, 0, 1]
218map([1..5], # * 2);;[2, 4, 6, 8, 10]
219filter([0..10], # % 2 == 0);;[0, 2, 4, 6, 8, 10]
220some([1..3], # == 2);;true
221all((0..5], # > 0);;true
222none([1..3), # == 3);;true
223one([1..3], # == 3);;true
224count((1..5), true);;3
225flatMap([1..2], [#, #]);;[1, 1, 2, 2]
226sum(map([1..4], #));;10
227map([a..b], #);{a: 1, b: 4};[1, 2, 3, 4]
228map((a..b), #);{a: 1, b: 4};[2, 3]
229map([a..b], #);{a: 'x', b: 4};!error
230map([a..b], #);{a: null, b: 4};!error
231map(['a'..'c'], #);;!error
232map([1.5..3], #);;[2, 3];bug: iterating [1.5..3] yields 1, which lies outside the interval (bound truncated)
233map([1..2.5), #);;[1, 2];bug: iterating [1..2.5) drops 2, which lies inside the interval (bound truncated before exclusion)
234map((0.5..2], #);;[1, 2]
235map([0..2], #) == [0, 1, 2];;false;question: array == compares by identity, so equal arrays are not equal (see equality chapter)
2361..5;;!error
237map(1..5, #);;!error