Files
zen/core/expression/tests/spec/standard/08-arithmetic.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

12 KiB

1expression (string);input (json 5);output (json 5);note
2# add(n, unit): every unit and alias
3d('2023-10-15').add(1, 'seconds');;'2023-10-15T00:00:01Z'
4d('2023-10-15').add(1, 'second');;'2023-10-15T00:00:01Z'
5d('2023-10-15').add(1, 'secs');;'2023-10-15T00:00:01Z'
6d('2023-10-15').add(1, 'sec');;'2023-10-15T00:00:01Z'
7d('2023-10-15').add(1, 's');;'2023-10-15T00:00:01Z'
8d('2023-10-15').add(1, 'minutes');;'2023-10-15T00:01:00Z'
9d('2023-10-15').add(1, 'minute');;'2023-10-15T00:01:00Z'
10d('2023-10-15').add(1, 'min');;'2023-10-15T00:01:00Z'
11d('2023-10-15').add(1, 'mins');;'2023-10-15T00:01:00Z'
12d('2023-10-15').add(1, 'm');;'2023-10-15T00:01:00Z'
13d('2023-10-15').add(1, 'hours');;'2023-10-15T01:00:00Z'
14d('2023-10-15').add(1, 'hour');;'2023-10-15T01:00:00Z'
15d('2023-10-15').add(1, 'hr');;'2023-10-15T01:00:00Z'
16d('2023-10-15').add(1, 'hrs');;'2023-10-15T01:00:00Z'
17d('2023-10-15').add(1, 'h');;'2023-10-15T01:00:00Z'
18d('2023-10-15').add(1, 'days');;'2023-10-16T00:00:00Z'
19d('2023-10-15').add(1, 'day');;'2023-10-16T00:00:00Z'
20d('2023-10-15').add(1, 'd');;'2023-10-16T00:00:00Z'
21d('2023-10-15').add(1, 'weeks');;'2023-10-22T00:00:00Z'
22d('2023-10-15').add(1, 'week');;'2023-10-22T00:00:00Z'
23d('2023-10-15').add(1, 'w');;'2023-10-22T00:00:00Z'
24d('2023-10-15').add(1, 'months');;'2023-11-15T00:00:00Z'
25d('2023-10-15').add(1, 'month');;'2023-11-15T00:00:00Z'
26d('2023-10-15').add(1, 'mo');;'2023-11-15T00:00:00Z'
27d('2023-10-15').add(1, 'M');;'2023-11-15T00:00:00Z'
28d('2023-10-15').add(1, 'quarters');;'2024-01-15T00:00:00Z'
29d('2023-10-15').add(1, 'quarter');;'2024-01-15T00:00:00Z'
30d('2023-10-15').add(1, 'qtr');;'2024-01-15T00:00:00Z'
31d('2023-10-15').add(1, 'q');;'2024-01-15T00:00:00Z'
32d('2023-10-15').add(1, 'years');;'2024-10-15T00:00:00Z'
33d('2023-10-15').add(1, 'year');;'2024-10-15T00:00:00Z'
34d('2023-10-15').add(1, 'y');;'2024-10-15T00:00:00Z'
35# add(n, unit): unknown units and case sensitivity
36d('2023-10-15').add(1, 'ms');;!error
37d('2023-10-15').add(1, 'millisecond');;!error
38d('2023-10-15').add(1, 'D');;!error
39d('2023-10-15').add(1, 'Y');;!error
40d('2023-10-15').add(1, 'H');;!error
41d('2023-10-15').add(1, 'W');;!error
42d('2023-10-15').add(1, 'S');;!error
43d('2023-10-15').add(1, 'Day');;!error
44d('2023-10-15').add(1, 'fortnight');;!error
45d('2023-10-15').add(1, '');;!error
46d('2023-10-15').add(1);;!error
47d('2023-10-15').add(null);;!error
48d('2023-10-15').add(true, 'd');;!error
49d('2023-10-15').add(1, null);;!error
50# add(n, unit): zero, negative, fractional and large amounts
51d('2023-10-15').add(0, 'd');;'2023-10-15T00:00:00Z'
52d('2023-10-15').add(-1, 'd');;'2023-10-14T00:00:00Z'
53d('2023-10-15').add(-1, 'M');;'2023-09-15T00:00:00Z'
54d('2023-10-15').add(-1, 'y');;'2022-10-15T00:00:00Z'
55d('2023-10-15').add(-2, 'q');;'2023-04-15T00:00:00Z'
56d('2023-10-15').add(1.5, 'h');;'2023-10-15T01:30:00Z'
57d('2023-10-15').add(1.5, 'd');;'2023-10-16T12:00:00Z'
58d('2023-10-15').add(0.5, 'm');;'2023-10-15T00:00:30Z'
59d('2023-10-15').add(0.5, 'w');;'2023-10-18T12:00:00Z'
60d('2023-10-15').add(0.5, 's').timestamp();;1697328000000;question: sub-second amounts are truncated to whole seconds
61d('2023-10-15').add(1.5, 'M');;'2023-11-15T00:00:00Z';question: fractional calendar amounts are truncated to whole months
62d('2023-10-15').add(1.9, 'y');;'2024-10-15T00:00:00Z';question: fractional calendar amounts are truncated to whole years
63d('2023-10-15').add(-1.5, 'M');;'2023-09-15T00:00:00Z';question: fractional calendar amounts are truncated toward zero
64d('2023-10-15').add(1000, 'y');;'3023-10-15T00:00:00Z'
65d('2023-10-15').add(10000, 'd');;'2051-03-02T00:00:00Z'
66d('2023-10-15').add(1000000, 'y');;'Invalid date'
67d('2023-10-15').add(1e30, 's');;!error
68# sub(n, unit)
69d('2023-10-15').sub(1, 's');;'2023-10-14T23:59:59Z'
70d('2023-10-15').sub(1, 'm');;'2023-10-14T23:59:00Z'
71d('2023-10-15').sub(1, 'h');;'2023-10-14T23:00:00Z'
72d('2023-10-15').sub(1, 'd');;'2023-10-14T00:00:00Z'
73d('2023-10-15').sub(1, 'w');;'2023-10-08T00:00:00Z'
74d('2023-10-15').sub(1, 'M');;'2023-09-15T00:00:00Z'
75d('2023-10-15').sub(1, 'q');;'2023-07-15T00:00:00Z'
76d('2023-10-15').sub(1, 'y');;'2022-10-15T00:00:00Z'
77d('2023-10-15').sub(-1, 'd');;'2023-10-16T00:00:00Z'
78d('2023-10-15').sub(13, 'M');;'2022-09-15T00:00:00Z'
79d('2023-01-01').sub(1, 's');;'2022-12-31T23:59:59Z'
80d('2023-10-15').sub(1, 'ms');;!error
81# duration strings
82d('2023-10-15').add('1d');;'2023-10-16T00:00:00Z'
83d('2023-10-15').add('1day');;'2023-10-16T00:00:00Z'
84d('2023-10-15').add('2days');;'2023-10-17T00:00:00Z'
85d('2023-10-15').add('1 d');;'2023-10-16T00:00:00Z'
86d('2023-10-15').add(' 1d ');;'2023-10-16T00:00:00Z'
87d('2023-10-15').add('2h 30m');;'2023-10-15T02:30:00Z'
88d('2023-10-15').add('2h30m');;'2023-10-15T02:30:00Z'
89d('2023-10-15').add('1w');;'2023-10-22T00:00:00Z'
90d('2023-10-15').add('36h');;'2023-10-16T12:00:00Z'
91d('2023-10-15').add('90m');;'2023-10-15T01:30:00Z'
92d('2023-10-15').add('45s');;'2023-10-15T00:00:45Z'
93d('2023-10-15').add('1M');;'2023-11-15T00:00:00Z'
94d('2023-10-15').add('1m');;'2023-10-15T00:01:00Z'
95d('2023-10-15').add('1mo');;'2023-11-15T00:00:00Z'
96d('2023-10-15').add('1q');;'2024-01-15T00:00:00Z'
97d('2023-10-15').add('1y');;'2024-10-15T00:00:00Z'
98d('2023-10-15').add('1y 2M 3d 4h 5m 6s');;'2024-12-18T04:05:06Z'
99d('2023-10-15').add('1d 5h');;'2023-10-16T05:00:00Z'
100d('2023-10-15').add('1d 1d');;'2023-10-17T00:00:00Z'
101d('2023-10-15').add('1M 1M');;'2023-12-15T00:00:00Z';bug: repeated month components overwrite instead of accumulating (hours and days do accumulate)
102d('2023-10-15').add('1y 1y');;'2025-10-15T00:00:00Z';bug: repeated year components overwrite instead of accumulating
103d('2023-10-15').add('1q 1M');;'2024-02-15T00:00:00Z';bug: a month component overwrites the preceding quarter instead of adding to it
104d('2023-10-15').add('0d');;'2023-10-15T00:00:00Z'
105d('2023-10-15').add('100000d');;'2297-07-30T00:00:00Z'
106d('2023-10-15').sub('1d');;'2023-10-14T00:00:00Z'
107d('2023-10-15').sub('2h 30m');;'2023-10-14T21:30:00Z'
108d('2023-10-15').sub('1y 1M');;'2022-09-15T00:00:00Z'
109d('2023-10-15').add('');;!error
110d('2023-10-15').add(' ');;!error
111d('2023-10-15').add('d');;!error
112d('2023-10-15').add('10');;!error
113d('2023-10-15').add('1x');;!error
114d('2023-10-15').add('1D');;!error
115d('2023-10-15').add('1ms');;!error
116d('2023-10-15').add('-1d');;!error
117d('2023-10-15').add('1.5h');;!error
118d('2023-10-15').add('1d,2h');;!error
119d('2023-10-15').add('P1D');;!error
120d('2023-10-15').add('99999999999999999999d');;!error
121# month-end clamping
122d('2023-01-31').add(1, 'M');;'2023-02-28T00:00:00Z'
123d('2024-01-31').add(1, 'M');;'2024-02-29T00:00:00Z'
124d('2023-01-31').add(2, 'M');;'2023-03-31T00:00:00Z'
125d('2023-03-31').add(1, 'M');;'2023-04-30T00:00:00Z'
126d('2023-03-31').sub(1, 'M');;'2023-02-28T00:00:00Z'
127d('2024-03-31').sub(1, 'M');;'2024-02-29T00:00:00Z'
128d('2023-05-31').add(1, 'q');;'2023-08-31T00:00:00Z'
129d('2023-11-30').add(1, 'q');;'2024-02-29T00:00:00Z'
130d('2023-01-31').add(1, 'M').add(1, 'M');;'2023-03-28T00:00:00Z'
131d('2023-12-31').add(1, 'M');;'2024-01-31T00:00:00Z'
132d('2023-12-31').add(2, 'M');;'2024-02-29T00:00:00Z'
133d('1900-01-31').add(1, 'M');;'1900-02-28T00:00:00Z'
134d('2000-01-31').add(1, 'M');;'2000-02-29T00:00:00Z'
135d('2024-02-29').add(12, 'M');;'2025-02-28T00:00:00Z'
136d('2024-02-29').add(48, 'M');;'2028-02-29T00:00:00Z'
137d('2024-02-29').add(4, 'y');;'2028-02-29T00:00:00Z'
138d('2024-02-29').add(1, 'y');;'2025-02-28T00:00:00Z';bug: adding a year to Feb 29 gives an invalid date instead of clamping to Feb 28 like months do
139d('2024-02-29').sub(1, 'y');;'2023-02-28T00:00:00Z';bug: subtracting a year from Feb 29 gives an invalid date instead of clamping
140d('2024-02-29').add('1y');;'2025-02-28T00:00:00Z';bug: adding a year to Feb 29 gives an invalid date instead of clamping
141d('2024-02-29').add(100, 'y');;'2124-02-29T00:00:00Z'
142d('2000-02-29').add(100, 'y');;'2100-02-28T00:00:00Z';bug: 2100 is not a leap year, the result should clamp to Feb 28 instead of being invalid
143d('2024-02-29').add(1, 'q');;'2024-05-29T00:00:00Z'
144# duration components apply from the smallest unit up (seconds, then months, then years)
145d('2023-01-30').add('1M 1d');;'2023-02-28T00:00:00Z';question: seconds/days apply before months; applying the largest unit first (Temporal, moment) would give 2023-03-01
146d('2024-02-28').add('1y 1d');;'Invalid date';question: the day is applied first (Feb 29) and the year then fails; largest-first would give 2025-03-01
147# date arithmetic through operators is not defined
148d('2023-10-15') + '1d';;'2023-10-15T00:00:00Z1d'
149d('2023-10-15') - d('2023-10-14');;!error
150d('2023-10-15') + 86400000;;!error
151# add/sub on an invalid date stays invalid
152d('foo').add(1, 'd');;'Invalid date'
153d('foo').sub('1d');;'Invalid date'
154d(null).add(1, 'M').isValid();;false
155# add/sub on rows: repeated dates, month ends, leap days, invalid, missing
156d(s).add(1, 'month');{s: '2023-01-31'};'2023-02-28T00:00:00Z'
157d(s).add(1, 'month');{s: '2023-01-31'};'2023-02-28T00:00:00Z'
158d(s).add(1, 'month');{s: '2024-01-31'};'2024-02-29T00:00:00Z'
159d(s).add(1, 'month');{s: '2023-12-15 13:45'};'2024-01-15T13:45:00Z'
160d(s).add(1, 'month');{s: '2024-02-29'};'2024-03-29T00:00:00Z'
161d(s).add(1, 'month');{s: 'garbage'};'Invalid date'
162d(s).add(1, 'month');{s: null};'Invalid date'
163d(s).add(1, 'month');{};'Invalid date'
164d(s).add(1, 'month');{s: 0};'1970-02-01T00:00:00Z'
165d(s).add(1, 'y');{s: '2023-06-15'};'2024-06-15T00:00:00Z'
166d(s).add(1, 'y');{s: '2023-06-15'};'2024-06-15T00:00:00Z'
167d(s).add(1, 'y');{s: '2024-02-29'};'2025-02-28T00:00:00Z';bug: adding a year to Feb 29 gives an invalid date instead of clamping
168d(s).add(1, 'y');{s: '2023-02-28'};'2024-02-28T00:00:00Z'
169d(s).add(1, 'y');{s: '1999-12-31 23:59:59'};'2000-12-31T23:59:59Z'
170d(s).add(1, 'y');{s: 'x'};'Invalid date'
171d(s).sub(1, 'd');{s: '2024-03-01'};'2024-02-29T00:00:00Z'
172d(s).sub(1, 'd');{s: '2023-03-01'};'2023-02-28T00:00:00Z'
173d(s).sub(1, 'd');{s: '2023-01-01'};'2022-12-31T00:00:00Z'
174d(s).sub(1, 'd');{s: '2023-01-01'};'2022-12-31T00:00:00Z'
175d(s).sub(1, 'd');{s: '1970-01-01'};'1969-12-31T00:00:00Z'
176d(s).sub(1, 'd');{s: ''};'Invalid date'
177d(s).add(n, u);{s: '2023-10-15', n: 1, u: 'd'};'2023-10-16T00:00:00Z'
178d(s).add(n, u);{s: '2023-10-15', n: 2, u: 'M'};'2023-12-15T00:00:00Z'
179d(s).add(n, u);{s: '2023-10-15', n: 2, u: 'm'};'2023-10-15T00:02:00Z'
180d(s).add(n, u);{s: '2023-10-15', n: -3, u: 'q'};'2023-01-15T00:00:00Z'
181d(s).add(n, u);{s: '2023-10-15', n: 1.5, u: 'h'};'2023-10-15T01:30:00Z'
182d(s).add(n, u);{s: '2023-10-15', n: 1, u: 'x'};!error
183d(s).add(n, u);{s: '2023-10-15', n: null, u: 'd'};!error
184d(s).add(n, u);{s: 'x', n: 1, u: 'd'};'Invalid date'
185d(s).add(dur);{s: '2023-10-15', dur: '1d'};'2023-10-16T00:00:00Z'
186d(s).add(dur);{s: '2023-10-15', dur: '2h 30m'};'2023-10-15T02:30:00Z'
187d(s).add(dur);{s: '2023-10-15', dur: '1M'};'2023-11-15T00:00:00Z'
188d(s).add(dur);{s: '2023-01-31', dur: '1M'};'2023-02-28T00:00:00Z'
189d(s).add(dur);{s: '2023-10-15', dur: 'soon'};!error
190d(s).add(dur);{s: '2023-10-15', dur: null};!error
191d(s, 'UTC').add(1, 'M');{s: '2023-01-31'};'2023-02-28T00:00:00Z'
192d(s, 'UTC').add(1, 'M');{s: '2023-01-31'};'2023-02-28T00:00:00Z'
193d(s, 'UTC').add(1, 'M');{s: '2024-01-30'};'2024-02-29T00:00:00Z'
194d(s, 'UTC').add(1, 'M');{s: '2023-10-15T23:59:59Z'};'2023-11-15T23:59:59Z'
195d(s, 'UTC').add(1, 'M');{s: 'bad'};'Invalid date'
196d(s, 'UTC').add(1, 'M');{};'Invalid date'
197d(s, 'Etc/GMT+5').add(1, 'd');{s: '2023-10-15'};'2023-10-16T00:00:00-05:00'
198d(s, 'Etc/GMT+5').add(1, 'd');{s: '2023-12-31 22:00'};'2024-01-01T22:00:00-05:00'
199d(s, 'Etc/GMT+5').add(1, 'd');{s: '2024-02-28'};'2024-02-29T00:00:00-05:00'
200d(s, 'Etc/GMT+5').add(1, 'd');{s: 'nope'};'Invalid date'
201d(s, 'Asia/Kolkata').sub(1, 'M');{s: '2023-03-31'};'2023-02-28T00:00:00+05:30'
202d(s, 'Asia/Kolkata').sub(1, 'M');{s: '2024-03-31'};'2024-02-29T00:00:00+05:30'
203d(s, 'Asia/Kolkata').sub(1, 'M');{s: '2023-01-15 12:00'};'2022-12-15T12:00:00+05:30'
204d(s, 'Asia/Kolkata').sub(1, 'M');{s: '2023-01-15 12:00'};'2022-12-15T12:00:00+05:30'
205d(s, 'Asia/Kathmandu').add(1, 'q');{s: '2023-11-30'};'2024-02-29T00:00:00+05:45'
206d(s, 'Asia/Kathmandu').add(1, 'q');{s: '2023-08-31'};'2023-11-30T00:00:00+05:45'
207d(s, 'Asia/Kathmandu').add(1, 'q');{s: null};'Invalid date'
208d(s).add(1, 'h').hour();{s: '2023-10-15 23:30'};0
209d(s).add(1, 'h').hour();{s: '2023-10-15 10:30'};11
210d(s).add(1, 'h').hour();{s: 'x'};null
211d(s).add(1, 'h').hour();{};null