formualizer-eval 0.10.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
use super::common::abs_cell_ref;
use crate::engine::{DependencyGraph, VertexKind};
use formualizer_common::{ExcelErrorKind, LiteralValue};
use formualizer_parse::parser::{ASTNode, ASTNodeType, ReferenceType};

#[test]
fn test_dependency_extraction_from_ast() {
    let mut graph = DependencyGraph::new();

    // Create some cells to reference
    graph
        .set_cell_formula(
            "Sheet1",
            1,
            1,
            super::common::literal_ast(LiteralValue::Int(10)),
        )
        .unwrap();
    graph
        .set_cell_formula(
            "Sheet1",
            2,
            2,
            super::common::literal_ast(LiteralValue::Int(20)),
        )
        .unwrap();

    // Create a formula that references A1 (Sheet1:1:1)
    let ast_with_ref = ASTNode {
        node_type: ASTNodeType::Reference {
            original: "A1".to_string(),
            reference: ReferenceType::cell(None, 1, 1),
        },
        source_token: None,
        contains_volatile: false,
    };

    graph
        .set_cell_formula("Sheet1", 3, 3, ast_with_ref)
        .unwrap();

    // Verify the dependency was created
    assert_eq!(graph.vertex_len(), 3); // A1, B2, C3

    // Find C3 vertex (should be the last one created)
    let c3_vertex_id = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 3, 3))
        .unwrap();
    assert_eq!(graph.get_dependencies(c3_vertex_id).len(), 1);

    // The dependency should point to A1's vertex
    let a1_addr = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 1, 1))
        .unwrap();

    assert_eq!(graph.get_dependencies(c3_vertex_id)[0], a1_addr);

    // A1 should have C3 as a dependent
    let a1_vertex_id = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 1, 1))
        .unwrap();
    assert_eq!(graph.get_dependents(a1_vertex_id).len(), 1);

    let c3_addr = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 3, 3))
        .unwrap();

    assert_eq!(graph.get_dependents(a1_vertex_id)[0], c3_addr);
}

#[test]
fn test_dependency_extraction_multiple_references() {
    let mut graph = DependencyGraph::new();

    // Create cells A1 and B1
    graph
        .set_cell_formula(
            "Sheet1",
            1,
            1,
            super::common::literal_ast(LiteralValue::Int(10)),
        )
        .unwrap();
    graph
        .set_cell_formula(
            "Sheet1",
            1,
            2,
            super::common::literal_ast(LiteralValue::Int(20)),
        )
        .unwrap();

    // Create a binary operation A1 + B1
    let ast_binary = ASTNode {
        node_type: ASTNodeType::BinaryOp {
            op: "+".to_string(),
            left: Box::new(ASTNode {
                node_type: ASTNodeType::Reference {
                    original: "A1".to_string(),
                    reference: ReferenceType::cell(None, 1, 1),
                },
                source_token: None,
                contains_volatile: false,
            }),
            right: Box::new(ASTNode {
                node_type: ASTNodeType::Reference {
                    original: "B1".to_string(),
                    reference: ReferenceType::cell(None, 1, 2),
                },
                source_token: None,
                contains_volatile: false,
            }),
        },
        source_token: None,
        contains_volatile: false,
    };

    graph.set_cell_formula("Sheet1", 2, 1, ast_binary).unwrap();

    // Verify dependencies were extracted
    let a2_vertex_id = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 2, 1))
        .unwrap();
    let dependencies = graph.get_dependencies(a2_vertex_id);

    assert_eq!(dependencies.len(), 2);

    // Both A1 and B1 should be dependencies
    let a1_addr = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 1, 1))
        .unwrap();
    let b1_addr = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 1, 2))
        .unwrap();

    // A1 and B1 are value cells, not formulas, so they don't have dependencies
    // We should check if A2's dependencies contain A1 and B1
    assert!(dependencies.contains(&a1_addr));
    assert!(dependencies.contains(&b1_addr));
}

#[test]
fn test_dependency_edge_management() {
    let mut graph = DependencyGraph::new();

    // Create A1
    graph
        .set_cell_formula(
            "Sheet1",
            1,
            1,
            super::common::literal_ast(LiteralValue::Int(10)),
        )
        .unwrap();

    // Create A2 = A1
    let ast_ref_a1 = ASTNode {
        node_type: ASTNodeType::Reference {
            original: "A1".to_string(),
            reference: ReferenceType::cell(None, 1, 1),
        },
        source_token: None,
        contains_volatile: false,
    };

    graph.set_cell_formula("Sheet1", 2, 1, ast_ref_a1).unwrap();

    // Verify initial edges
    let a1_vertex_id = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 1, 1))
        .unwrap();
    let a2_vertex_id = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 2, 1))
        .unwrap();

    assert_eq!(graph.get_dependencies(a2_vertex_id).len(), 1);
    assert_eq!(graph.get_dependents(a1_vertex_id).len(), 1);

    // Now update A2 to reference B1 instead
    graph
        .set_cell_formula(
            "Sheet1",
            1,
            2,
            super::common::literal_ast(LiteralValue::Int(20)),
        )
        .unwrap(); // Create B1

    let ast_ref_b1 = ASTNode {
        node_type: ASTNodeType::Reference {
            original: "B1".to_string(),
            reference: ReferenceType::cell(None, 1, 2),
        },
        source_token: None,
        contains_volatile: false,
    };

    graph.set_cell_formula("Sheet1", 2, 1, ast_ref_b1).unwrap();

    // Verify edges were updated
    let a1_vertex_id = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 1, 1))
        .unwrap();
    let a2_vertex_id = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 2, 1))
        .unwrap();
    let b1_vertex_id = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 1, 2))
        .unwrap();

    // A1 should no longer have A2 as dependent
    assert_eq!(graph.get_dependents(a1_vertex_id).len(), 0);

    // A2 should now depend on B1
    let b1_addr = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 1, 2))
        .unwrap();

    assert_eq!(graph.get_dependencies(a2_vertex_id).len(), 1);
    assert_eq!(graph.get_dependencies(a2_vertex_id)[0], b1_addr);

    // B1 should have A2 as dependent
    assert_eq!(graph.get_dependents(b1_vertex_id).len(), 1);
}

#[test]
fn test_circular_dependency_detection() {
    let mut graph = DependencyGraph::new();

    // Try to create A1 = A1 (self-reference)
    let ast_self_ref = ASTNode {
        node_type: ASTNodeType::Reference {
            original: "A1".to_string(),
            reference: ReferenceType::cell(None, 1, 1),
        },
        source_token: None,
        contains_volatile: false,
    };

    let result = graph.set_cell_formula("Sheet1", 1, 1, ast_self_ref);

    // Should fail with circular reference error
    assert!(result.is_err());
    match result.unwrap_err().kind {
        ExcelErrorKind::Circ => {} // Expected
        other => panic!("Expected circular reference error, got {other:?}"),
    }

    // A1 should be an empty placeholder, not a formula
    assert_eq!(graph.vertex_len(), 1);
    let a1_vertex_id = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 1, 1))
        .unwrap();
    match &graph.get_vertex_kind(a1_vertex_id) {
        VertexKind::Empty => {} // Expected
        other => {
            panic!("A1 should be an Empty vertex after failed formula update, but was {other:?}")
        }
    }
}

#[test]
fn test_complex_circular_dependency() {
    let mut graph = DependencyGraph::new();

    // This test will verify more complex circular dependency detection
    // For now, we'll create a simple case and expand later

    // Create A1 = B1, B1 = A1 scenario
    let ast_ref_b1 = ASTNode {
        node_type: ASTNodeType::Reference {
            original: "B1".to_string(),
            reference: ReferenceType::cell(None, 1, 2),
        },
        source_token: None,
        contains_volatile: false,
    };

    // Create A1 = B1 (B1 doesn't exist yet, so this should work)
    graph.set_cell_formula("Sheet1", 1, 1, ast_ref_b1).unwrap();

    // Now try to create B1 = A1 (should create a cycle)
    let ast_ref_a1 = ASTNode {
        node_type: ASTNodeType::Reference {
            original: "A1".to_string(),
            reference: ReferenceType::cell(None, 1, 1),
        },
        source_token: None,
        contains_volatile: false,
    };

    // For Milestone 1.2, creating this dependency is allowed.
    // The full cycle detection is handled by the Scheduler in Milestone 2.
    let result = graph.set_cell_formula("Sheet1", 1, 2, ast_ref_a1);
    assert!(result.is_ok());

    // Verify the dependency chain was created
    assert_eq!(graph.vertex_len(), 2);

    let a1_vertex_id = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 1, 1))
        .unwrap();
    let b1_vertex_id = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 1, 2))
        .unwrap();

    // A1 should depend on B1
    assert_eq!(graph.get_dependencies(a1_vertex_id).len(), 1);
    assert_eq!(graph.get_dependencies(b1_vertex_id).len(), 1);

    // B1 should depend on A1
    assert_eq!(graph.get_dependencies(b1_vertex_id).len(), 1);
    assert_eq!(graph.get_dependencies(a1_vertex_id).len(), 1);

    // A1 should have B1 as a dependent
    assert_eq!(graph.get_dependents(a1_vertex_id).len(), 1);
    assert_eq!(graph.get_dependents(b1_vertex_id).len(), 1);

    // B1 should have A1 as a dependent
    assert_eq!(graph.get_dependents(b1_vertex_id).len(), 1);
    assert_eq!(graph.get_dependents(a1_vertex_id).len(), 1);
}

#[test]
fn test_cross_sheet_dependencies() {
    let mut graph = DependencyGraph::new();

    // Create Sheet1!A1 = 10
    graph
        .set_cell_formula(
            "Sheet1",
            1,
            1,
            super::common::literal_ast(LiteralValue::Int(10)),
        )
        .unwrap();

    // Create Sheet2!A1 = Sheet1!A1
    let ast_cross_sheet = ASTNode {
        node_type: ASTNodeType::Reference {
            original: "Sheet1!A1".to_string(),
            reference: ReferenceType::cell(Some("Sheet1".to_string()), 1, 1),
        },
        source_token: None,
        contains_volatile: false,
    };

    graph
        .set_cell_formula("Sheet2", 1, 1, ast_cross_sheet)
        .unwrap();

    // Verify cross-sheet dependency
    assert_eq!(graph.vertex_len(), 2);

    let sheet1_addr = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 1, 1))
        .unwrap();

    let sheet2_addr = graph
        .get_vertex_id_for_address(&abs_cell_ref(1, 1, 1))
        .unwrap();

    let sheet2_vertex_id = graph
        .get_vertex_id_for_address(&abs_cell_ref(1, 1, 1))
        .unwrap();
    let sheet1_vertex_id = graph
        .get_vertex_id_for_address(&abs_cell_ref(0, 1, 1))
        .unwrap();

    // Sheet2!A1 should depend on Sheet1!A1
    assert_eq!(graph.get_dependencies(sheet2_vertex_id).len(), 1);
    assert_eq!(graph.get_dependencies(sheet2_vertex_id)[0], sheet1_addr);

    // Sheet1!A1 should have Sheet2!A1 as dependent
    assert_eq!(graph.get_dependents(sheet1_vertex_id).len(), 1);
    assert_eq!(graph.get_dependents(sheet1_vertex_id)[0], sheet2_addr);
}

#[test]
fn test_relative_sheet_dependency() {
    let mut graph = DependencyGraph::new();

    // Create Sheet2!A1 = 10
    graph
        .set_cell_formula(
            "Sheet2",
            1,
            1,
            super::common::literal_ast(LiteralValue::Int(10)),
        )
        .unwrap();

    // Create Sheet2!B1 = A1 (which should resolve to Sheet2!A1)
    let ast_relative_ref = ASTNode {
        node_type: ASTNodeType::Reference {
            original: "A1".to_string(),
            reference: ReferenceType::cell(None, 1, 1),
        },
        source_token: None,
        contains_volatile: false,
    };

    graph
        .set_cell_formula("Sheet2", 1, 2, ast_relative_ref)
        .unwrap();

    // Verify the dependency is within Sheet2
    assert_eq!(graph.vertex_len(), 2);

    let sheet2_a1_id = graph
        .get_vertex_id_for_address(&abs_cell_ref(1, 1, 1))
        .unwrap();

    let sheet2_b1_id = graph
        .get_vertex_id_for_address(&abs_cell_ref(1, 1, 2))
        .unwrap();

    let sheet2_b1_vertex_id = graph
        .get_vertex_id_for_address(&abs_cell_ref(1, 1, 2))
        .unwrap();
    assert_eq!(graph.get_dependencies(sheet2_b1_vertex_id).len(), 1);
    assert_eq!(graph.get_dependencies(sheet2_b1_vertex_id)[0], sheet2_a1_id);
}