formualizer-eval 0.11.0

High-performance Arrow-backed Excel formula engine with dependency graph and incremental recalculation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! A dynamic-array spill whose target holds another formula (a scalar
//! formula or another spill anchor) is blocked: the anchor becomes
//! `#SPILL!` (with its expected extent), the blocking formula evaluates
//! normally, and evaluation completes. Policy follows Excel's documented
//! semantics (no Excel oracle in this suite).
//!
//! Every scenario runs across the evaluation entry points (plain, delta and
//! cancellable, which take different layer paths), sequential and parallel
//! configs, and `family_execution` on and off.

use crate::engine::{CancelToken, Engine, EvalConfig};
use crate::test_workbook::TestWorkbook;
use formualizer_common::{ExcelErrorExtra, ExcelErrorKind, LiteralValue};
use formualizer_parse::parser::parse;

#[derive(Clone, Copy, Debug)]
enum Entry {
    Plain,
    Delta,
    Cancellable,
}

#[derive(Clone, Copy, Debug)]
struct Mode {
    entry: Entry,
    parallel: bool,
    family: bool,
}

fn modes() -> Vec<Mode> {
    let mut out = Vec::new();
    for entry in [Entry::Plain, Entry::Delta, Entry::Cancellable] {
        for parallel in [false, true] {
            for family in [true, false] {
                out.push(Mode {
                    entry,
                    parallel,
                    family,
                });
            }
        }
    }
    out
}

fn engine(mode: Mode) -> Engine<TestWorkbook> {
    crate::builtins::load_builtins();
    Engine::new(
        TestWorkbook::new(),
        EvalConfig {
            enable_parallel: mode.parallel,
            max_threads: if mode.parallel { Some(4) } else { None },
            family_execution: mode.family,
            ..Default::default()
        },
    )
}

fn eval(e: &mut Engine<TestWorkbook>, mode: Mode) {
    let result = match mode.entry {
        Entry::Plain => e.evaluate_all().map(|_| ()),
        Entry::Delta => e.evaluate_all_with_delta().map(|_| ()),
        Entry::Cancellable => e.evaluate_all_cancellable(CancelToken::new()).map(|_| ()),
    };
    if let Err(err) = result {
        panic!("{mode:?}: evaluation aborted: {err:?}");
    }
}

fn f(e: &mut Engine<TestWorkbook>, row: u32, col: u32, src: &str) {
    e.set_cell_formula("Sheet1", row, col, parse(src).unwrap())
        .unwrap();
}

fn get(e: &Engine<TestWorkbook>, row: u32, col: u32) -> Option<LiteralValue> {
    match e.get_cell_value("Sheet1", row, col) {
        Some(LiteralValue::Int(i)) => Some(LiteralValue::Number(i as f64)),
        Some(LiteralValue::Empty) | None => None,
        other => other,
    }
}

fn n(x: f64) -> Option<LiteralValue> {
    Some(LiteralValue::Number(x))
}

#[track_caller]
fn assert_kind(value: Option<LiteralValue>, kind: ExcelErrorKind, ctx: &str) {
    match value {
        Some(LiteralValue::Error(e)) => assert_eq!(e.kind, kind, "{ctx}: {e:?}"),
        other => panic!("{ctx}: expected {kind:?}, got {other:?}"),
    }
}

/// The value a value-blocked `=SEQUENCE(rows)` anchor holds in this mode:
/// the existing blocked-anchor encoding a formula-blocked anchor must match.
fn value_blocked_anchor(mode: Mode, rows: u32) -> Option<LiteralValue> {
    let mut e = engine(mode);
    e.set_cell_value("Sheet1", rows, 1, LiteralValue::Number(99.0))
        .unwrap();
    f(&mut e, 1, 1, &format!("=SEQUENCE({rows})"));
    eval(&mut e, mode);
    get(&e, 1, 1)
}

/// The anchor holds `#SPILL!`, encoded exactly as a value-blocked anchor of
/// the same attempted extent; where the error keeps its extent, it is the
/// attempted one.
#[track_caller]
fn assert_blocked(mode: Mode, value: Option<LiteralValue>, rows: u32, ctx: &str) {
    match &value {
        Some(LiteralValue::Error(e)) => {
            assert_eq!(e.kind, ExcelErrorKind::Spill, "{ctx}: {e:?}");
            if let ExcelErrorExtra::Spill {
                expected_rows,
                expected_cols,
            } = e.extra
            {
                assert_eq!((expected_rows, expected_cols), (rows, 1), "{ctx}");
            }
        }
        other => panic!("{ctx}: expected #SPILL!, got {other:?}"),
    }
    assert_eq!(value, value_blocked_anchor(mode, rows), "{ctx}: encoding");
}

fn has_spill(e: &Engine<TestWorkbook>, row: u32, col: u32) -> bool {
    let cell = e.graph.make_cell_ref("Sheet1", row, col);
    let vid = e.graph.get_vertex_for_cell(&cell).expect("anchor vertex");
    e.graph
        .spill_cells_for_anchor(vid)
        .is_some_and(|cells| !cells.is_empty())
}

/// A1 `=SEQUENCE(3)` with a scalar formula in A3, defined in both orders,
/// plus a dependent and spill-reference readers of the anchor.
#[test]
fn scalar_formula_blocker_yields_spill_error() {
    for mode in modes() {
        for anchor_first in [true, false] {
            let ctx = format!("{mode:?} anchor_first={anchor_first}");
            let mut e = engine(mode);
            if anchor_first {
                f(&mut e, 1, 1, "=SEQUENCE(3)");
                f(&mut e, 3, 1, "=1+1");
            } else {
                f(&mut e, 3, 1, "=1+1");
                f(&mut e, 1, 1, "=SEQUENCE(3)");
            }
            // Independent same-layer work, so parallel configs have a
            // multi-vertex layer to split.
            for r in 1..=8 {
                f(&mut e, r, 5, &format!("={r}*2"));
            }
            f(&mut e, 1, 2, "=A1+0");
            f(&mut e, 1, 3, "=SUM(A1#)");
            eval(&mut e, mode);

            assert_blocked(mode, get(&e, 1, 1), 3, &ctx);
            assert_eq!(get(&e, 2, 1), None, "{ctx}: no spill child");
            assert_eq!(get(&e, 3, 1), n(2.0), "{ctx}: blocker evaluates");
            assert_kind(get(&e, 1, 2), ExcelErrorKind::Spill, &ctx);
            assert_kind(get(&e, 1, 3), ExcelErrorKind::Ref, &ctx);
            assert_eq!(get(&e, 8, 5), n(16.0), "{ctx}");
            assert!(!has_spill(&e, 1, 1), "{ctx}: no spill registered");
        }
    }
}

/// A1 `=SEQUENCE(3)` and A2 `=SEQUENCE(2)`: A2's anchor is a formula in
/// A1's target, so A1 is blocked and A2 spills, whatever the definition
/// order.
#[test]
fn anchor_inside_another_spill_target_blocks_the_outer_anchor() {
    for mode in modes() {
        for outer_first in [true, false] {
            let ctx = format!("{mode:?} outer_first={outer_first}");
            let mut e = engine(mode);
            if outer_first {
                f(&mut e, 1, 1, "=SEQUENCE(3)");
                f(&mut e, 2, 1, "=SEQUENCE(2)");
            } else {
                f(&mut e, 2, 1, "=SEQUENCE(2)");
                f(&mut e, 1, 1, "=SEQUENCE(3)");
            }
            for r in 1..=8 {
                f(&mut e, r, 5, &format!("={r}*2"));
            }
            eval(&mut e, mode);

            assert_blocked(mode, get(&e, 1, 1), 3, &ctx);
            assert_eq!(get(&e, 2, 1), n(1.0), "{ctx}");
            assert_eq!(get(&e, 3, 1), n(2.0), "{ctx}");
            assert!(!has_spill(&e, 1, 1), "{ctx}");
            assert!(has_spill(&e, 2, 1), "{ctx}");
        }
    }
}

/// A column of same-template anchors, each blocked by the next one. Family
/// execution groups them into one run.
#[test]
fn family_of_anchors_each_blocked_by_the_next() {
    for mode in modes() {
        let ctx = format!("{mode:?}");
        let mut e = engine(mode);
        for r in 1..=12 {
            f(&mut e, r, 2, "=SEQUENCE(2)");
        }
        eval(&mut e, mode);
        for r in 1..=11 {
            assert_blocked(mode, get(&e, r, 2), 2, &format!("{ctx} B{r}"));
        }
        assert_eq!(get(&e, 12, 2), n(1.0), "{ctx}");
        assert_eq!(get(&e, 13, 2), n(2.0), "{ctx}");
    }
}

/// Edits: a formula placed into a live spill blocks it; removing the
/// blocker lets the anchor spill again on the next recalc; shrinking the
/// anchor so the spill fits also re-spills.
#[test]
fn formula_blocker_edits_block_and_release_the_spill() {
    for mode in modes() {
        let ctx = format!("{mode:?}");
        let mut e = engine(mode);
        e.set_cell_value("Sheet1", 1, 4, LiteralValue::Number(3.0))
            .unwrap();
        f(&mut e, 1, 1, "=SEQUENCE(D1)");
        f(&mut e, 1, 2, "=A1+0");
        f(&mut e, 1, 3, "=SUM(A1#)");
        eval(&mut e, mode);
        assert_eq!(get(&e, 3, 1), n(3.0), "{ctx}: initial spill");
        assert_eq!(get(&e, 1, 3), n(6.0), "{ctx}");

        // A formula lands inside the live spill.
        f(&mut e, 3, 1, "=1+1");
        eval(&mut e, mode);
        assert_blocked(mode, get(&e, 1, 1), 3, &format!("{ctx} blocked by edit"));
        assert_eq!(get(&e, 2, 1), None, "{ctx}: spill cleared");
        assert_eq!(get(&e, 3, 1), n(2.0), "{ctx}: blocker evaluates");
        assert_kind(get(&e, 1, 2), ExcelErrorKind::Spill, &ctx);
        assert_kind(get(&e, 1, 3), ExcelErrorKind::Ref, &ctx);

        // Removing the blocker: the anchor re-spills on the next recalc.
        e.set_cell_value("Sheet1", 3, 1, LiteralValue::Empty)
            .unwrap();
        eval(&mut e, mode);
        assert_eq!(get(&e, 1, 1), n(1.0), "{ctx}: re-spilled");
        assert_eq!(get(&e, 3, 1), n(3.0), "{ctx}: re-spilled");
        assert_eq!(get(&e, 1, 2), n(1.0), "{ctx}: dependent follows");
        assert_eq!(get(&e, 1, 3), n(6.0), "{ctx}: reader follows");

        // Block again, then shrink the anchor so the spill fits.
        f(&mut e, 3, 1, "=1+1");
        eval(&mut e, mode);
        assert_blocked(mode, get(&e, 1, 1), 3, &format!("{ctx} re-blocked"));
        e.set_cell_value("Sheet1", 1, 4, LiteralValue::Number(2.0))
            .unwrap();
        eval(&mut e, mode);
        assert_eq!(get(&e, 1, 1), n(1.0), "{ctx}: fits");
        assert_eq!(get(&e, 2, 1), n(2.0), "{ctx}: fits");
        assert_eq!(get(&e, 3, 1), n(2.0), "{ctx}: blocker unchanged");
        assert_eq!(get(&e, 1, 3), n(3.0), "{ctx}: reader follows");
    }
}

/// Moving the blocker's formula to a value outside the target releases the
/// spill too (the blocker is changed rather than cleared).
#[test]
fn formula_blocker_replaced_by_formula_elsewhere_releases_the_spill() {
    for mode in modes() {
        let ctx = format!("{mode:?}");
        let mut e = engine(mode);
        f(&mut e, 3, 1, "=1+1");
        f(&mut e, 1, 1, "=SEQUENCE(3)");
        eval(&mut e, mode);
        assert_blocked(mode, get(&e, 1, 1), 3, &ctx);

        e.set_cell_value("Sheet1", 3, 1, LiteralValue::Empty)
            .unwrap();
        f(&mut e, 4, 1, "=1+1");
        eval(&mut e, mode);
        assert_eq!(get(&e, 3, 1), n(3.0), "{ctx}: re-spilled");
        assert_eq!(get(&e, 4, 1), n(2.0), "{ctx}");
    }
}

/// Values and spill ownership (as the owning anchor's position) of A1:F6:
/// what must not depend on evaluation or edit history.
type Snapshot = Vec<(Option<LiteralValue>, Option<(u32, u32)>)>;

fn snapshot(e: &Engine<TestWorkbook>) -> Snapshot {
    let mut out = Vec::new();
    for row in 1..=6 {
        for col in 1..=6 {
            let cell = e.graph.make_cell_ref("Sheet1", row, col);
            let owner = e.graph.spill_registry_anchor_for_cell(cell).map(|vid| {
                let anchor = e.graph.get_cell_ref(vid).expect("owner cell");
                // Every claim belongs to a well-formed extent.
                let (first, last) = e
                    .graph
                    .spill_extent_for_anchor(vid)
                    .expect("owner extent is a rectangle");
                assert_eq!(first, anchor, "extent starts at its anchor");
                assert!(
                    (first.coord.row()..=last.coord.row()).contains(&cell.coord.row())
                        && (first.coord.col()..=last.coord.col()).contains(&cell.coord.col()),
                    "claim inside the owner's extent"
                );
                (anchor.coord.row() + 1, anchor.coord.col() + 1)
            });
            out.push((get(e, row, col), owner));
        }
    }
    out
}

/// Build `cells` (row, col, formula) in `order`, evaluating after each edit
/// when `stepwise`; return the final snapshot.
fn build(mode: Mode, cells: &[(u32, u32, &str)], order: &[usize], stepwise: bool) -> Snapshot {
    let mut e = engine(mode);
    for r in 1..=8 {
        f(&mut e, r, 8, &format!("={r}*2"));
    }
    for &i in order {
        let (row, col, src) = cells[i];
        f(&mut e, row, col, src);
        if stepwise {
            eval(&mut e, mode);
        }
    }
    eval(&mut e, mode);
    snapshot(&e)
}

/// The final state of colliding anchors is the same after a fresh load and
/// after either edit order (incumbent first, then the intruder, and the
/// reverse), in every mode.
#[track_caller]
fn assert_history_independent(cells: &[(u32, u32, &str)], expect: &[(u32, u32, Option<f64>)]) {
    for mode in modes() {
        let fresh = build(mode, cells, &[0, 1], false);
        let fresh_rev = build(mode, cells, &[1, 0], false);
        let edit = build(mode, cells, &[0, 1], true);
        let edit_rev = build(mode, cells, &[1, 0], true);
        assert_eq!(fresh, fresh_rev, "{mode:?}: fresh, definition order");
        assert_eq!(fresh, edit, "{mode:?}: first then second");
        assert_eq!(fresh, edit_rev, "{mode:?}: second then first");
        for &(row, col, want) in expect {
            let got = &fresh[((row - 1) * 6 + (col - 1)) as usize].0;
            match want {
                Some(x) => assert_eq!(got, &n(x), "{mode:?} ({row},{col})"),
                None => assert_kind(got.clone(), ExcelErrorKind::Spill, &format!("{mode:?}")),
            }
        }
    }
}

/// Nested: the later anchor (or a scalar formula) lies in the first
/// anchor's rectangle, so it is a formula blocker for that anchor whichever
/// was there first.
#[test]
fn nested_blockers_resolve_independently_of_history() {
    assert_history_independent(
        &[(1, 1, "={1,2;3,4}"), (2, 1, "={5,6;7,8}")],
        &[(1, 1, None), (2, 1, Some(5.0)), (3, 2, Some(8.0))],
    );
    assert_history_independent(
        &[(1, 1, "=SEQUENCE(3)"), (3, 1, "=1+1")],
        &[(1, 1, None), (3, 1, Some(2.0))],
    );
}

/// Crossing: A2:C2 and B1:B3 collide at B2 with neither anchor inside the
/// other's rectangle. The anchor first in (sheet, column, row) order, A2,
/// spills; B1 is `#SPILL!`, whatever the history.
#[test]
fn crossing_spills_resolve_independently_of_history() {
    assert_history_independent(
        &[(2, 1, "=SEQUENCE(1,3)"), (1, 2, "=SEQUENCE(3)")],
        &[
            (2, 1, Some(1.0)),
            (2, 2, Some(2.0)),
            (2, 3, Some(3.0)),
            (1, 2, None),
        ],
    );
    // Mirrored so the yielding anchor is not always the one defined last.
    assert_history_independent(
        &[(4, 2, "=SEQUENCE(3)"), (5, 1, "=SEQUENCE(1,4)")],
        &[(5, 1, Some(1.0)), (5, 4, Some(4.0)), (4, 2, None)],
    );
}

/// Removing the intruder (a scalar formula, or an anchor) lets the
/// incumbent spill again on the next recalc, with no stale claims left.
#[test]
fn removing_the_intruder_restores_the_incumbent() {
    for mode in modes() {
        for (row, col, intruder) in [
            (3, 1, "=1+1"),
            (2, 1, "=SEQUENCE(2)"),
            (2, 1, "=SEQUENCE(1,3)"),
        ] {
            let ctx = format!("{mode:?} {intruder} at ({row},{col})");
            let mut e = engine(mode);
            f(&mut e, 1, 1, "=SEQUENCE(3)");
            f(&mut e, 1, 3, "=SUM(A1#)");
            eval(&mut e, mode);
            let spilled = snapshot(&e);

            f(&mut e, row, col, intruder);
            eval(&mut e, mode);
            assert_kind(get(&e, 1, 1), ExcelErrorKind::Spill, &ctx);
            assert_kind(get(&e, 1, 3), ExcelErrorKind::Ref, &ctx);
            assert!(!has_spill(&e, 1, 1), "{ctx}: incumbent claims nothing");
            let a = e.graph.make_cell_ref("Sheet1", 1, 1);
            for r in 1..=3 {
                let cell = e.graph.make_cell_ref("Sheet1", r, 1);
                assert_ne!(
                    e.graph.spill_registry_anchor_for_cell(cell),
                    e.graph.get_vertex_for_cell(&a),
                    "{ctx}: no stale claim on row {r}"
                );
            }
            snapshot(&e);

            e.set_cell_value("Sheet1", row, col, LiteralValue::Empty)
                .unwrap();
            eval(&mut e, mode);
            assert_eq!(snapshot(&e), spilled, "{ctx}: restored");
            assert_eq!(get(&e, 1, 3), n(6.0), "{ctx}");
        }
    }
}

/// The spill a crossing anchor yielded to goes away: the yielding anchor
/// spills on the next recalc.
#[test]
fn removing_the_crossing_winner_releases_the_loser() {
    for mode in modes() {
        let ctx = format!("{mode:?}");
        let mut e = engine(mode);
        f(&mut e, 2, 1, "=SEQUENCE(1,3)");
        f(&mut e, 1, 2, "=SEQUENCE(3)");
        f(&mut e, 1, 4, "=B1+0");
        eval(&mut e, mode);
        assert_kind(get(&e, 1, 2), ExcelErrorKind::Spill, &ctx);
        assert_kind(get(&e, 1, 4), ExcelErrorKind::Spill, &ctx);

        e.set_cell_value("Sheet1", 2, 1, LiteralValue::Empty)
            .unwrap();
        eval(&mut e, mode);
        assert_eq!(get(&e, 1, 2), n(1.0), "{ctx}");
        assert_eq!(get(&e, 2, 2), n(2.0), "{ctx}");
        assert_eq!(get(&e, 3, 2), n(3.0), "{ctx}");
        assert_eq!(get(&e, 2, 3), None, "{ctx}: winner's child cleared");
        assert_eq!(get(&e, 1, 4), n(1.0), "{ctx}: dependent follows");
        assert!(has_spill(&e, 1, 2), "{ctx}");
    }
}