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
424
425
426
427
428
429
430
431
432
433
use std::collections::BTreeMap;
use std::sync::Arc;

use super::FormulaPlaneMode;
use super::arena::AstNodeId;
use super::formula_source::{SourceFamilyId, SourceFormulaOrder};

#[derive(Clone, Debug)]
pub struct FormulaIngestRecord {
    pub row: u32,
    pub col: u32,
    pub ast_id: AstNodeId,
    pub formula_text: Option<Arc<str>>,
    pub(crate) source_order: Option<SourceFormulaOrder>,
    pub(crate) source_family: Option<SourceFamilyId>,
    pub(crate) partition_owner: Option<SourceFamilyId>,
    /// Load-time family grouping (Program 2): `Some(anchor)` (0-based)
    /// when this cell is a relative copy of the formula at `anchor`, whose
    /// arena AST is `ast_id`; the member's own AST was never interned.
    pub(crate) member_anchor: Option<(u32, u32)>,
}

impl FormulaIngestRecord {
    pub fn new(row: u32, col: u32, ast_id: AstNodeId, formula_text: Option<Arc<str>>) -> Self {
        Self {
            row,
            col,
            ast_id,
            formula_text,
            source_order: None,
            source_family: None,
            partition_owner: None,
            member_anchor: None,
        }
    }

    /// A relative copy of `template` (the formula at 0-based `anchor`).
    pub(crate) fn member(row: u32, col: u32, template: AstNodeId, anchor: (u32, u32)) -> Self {
        Self {
            member_anchor: Some(anchor),
            ..Self::new(row, col, template, None)
        }
    }

    /// Whether this record is a grouped family member (its `ast_id` is the
    /// family template, valid at the anchor cell, not at this cell).
    pub fn is_family_member(&self) -> bool {
        self.member_anchor.is_some()
    }

    pub(crate) fn with_source_proof(
        mut self,
        source_order: SourceFormulaOrder,
        source_family: Option<SourceFamilyId>,
        partition_owner: Option<SourceFamilyId>,
    ) -> Self {
        self.source_order = Some(source_order);
        self.source_family = source_family;
        self.partition_owner = partition_owner;
        self
    }
}

/// Load-time family grouping state for one sheet (Program 2, P2-M2):
/// the last formula staged in each column and the one before the current
/// cell in its row. A formula that is exactly the formula above it (or to
/// its left) relocated becomes a member of that formula's family and is
/// never interned; see `Engine::stage_formula_ast`.
#[derive(Debug, Default)]
pub struct FormulaFamilyGrouper {
    /// col0 -> (row0 of the last formula staged in the column, its family)
    pub(crate) by_col: rustc_hash::FxHashMap<u32, (u32, GroupedFamily)>,
    /// (row0, col0, family) of the last formula staged.
    pub(crate) last: Option<(u32, u32, GroupedFamily)>,
    /// Members grouped so far.
    pub(crate) members: u64,
}

impl FormulaFamilyGrouper {
    pub fn new() -> Self {
        Self::default()
    }

    /// Number of formulas staged as family members.
    pub fn members(&self) -> u64 {
        self.members
    }

    pub(crate) fn note(&mut self, row0: u32, col0: u32, family: GroupedFamily) {
        self.by_col.insert(col0, (row0, family.clone()));
        self.last = Some((row0, col0, family));
    }
}

/// A family template candidate: the template's arena root, its 0-based
/// anchor cell, and (computed on first use) per reference whether the
/// template's text is its rendering; `Some(None)` when the template
/// cannot have members (it needs a structural rewrite).
#[derive(Clone, Debug)]
pub(crate) struct GroupedFamily {
    pub(crate) template: AstNodeId,
    pub(crate) anchor: (u32, u32),
    pub(crate) rendered: Option<Option<Arc<[bool]>>>,
}

#[derive(Clone, Debug)]
pub struct FormulaIngestBatch {
    pub sheet_name: String,
    pub formulas: Vec<FormulaIngestRecord>,
}

impl FormulaIngestBatch {
    pub fn new(sheet_name: impl Into<String>, formulas: Vec<FormulaIngestRecord>) -> Self {
        Self {
            sheet_name: sheet_name.into(),
            formulas,
        }
    }

    pub fn len(&self) -> usize {
        self.formulas.len()
    }

    pub fn is_empty(&self) -> bool {
        self.formulas.is_empty()
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FormulaIngestReport {
    pub mode: FormulaPlaneMode,
    pub formula_cells_seen: u64,
    pub graph_formula_cells_materialized: u64,

    pub shadow_candidate_cells: u64,
    pub shadow_accepted_span_cells: u64,
    pub shadow_fallback_cells: u64,
    pub shadow_templates_interned: u64,
    pub shadow_spans_created: u64,

    pub graph_formula_vertices_avoided_shadow: u64,
    pub ast_roots_avoided_shadow: u64,
    pub edge_rows_avoided_shadow: u64,

    pub graph_vertices_created: u64,
    pub graph_edges_created: u64,

    pub source_formula_events: u64,
    pub source_formula_records_spooled: u64,
    pub source_spool_encoded_bytes: u64,
    pub source_spool_peak_memory_bytes: u64,
    pub source_spool_spilled_bytes: u64,
    pub source_spool_spill_files: u64,
    pub source_spool_replays: u64,
    pub source_ordinary_events: u64,
    pub source_shared_anchor_events: u64,
    pub source_shared_descendant_events: u64,
    pub source_unknown_events: u64,
    pub source_families_seen: u64,
    pub source_family_cells_seen: u64,
    pub source_family_shadow_eligible: u64,
    pub source_family_shadow_eligible_cells: u64,
    pub source_family_promoted: u64,
    pub source_family_promoted_cells: u64,
    pub source_family_fallback: u64,
    pub source_family_fallback_cells: u64,
    pub source_forward_descendants: u64,
    pub source_evidence_limit_fallbacks: u64,
    pub source_evidence_peak_bytes: u64,
    pub source_anchor_parses: u64,
    pub source_anchor_asts: u64,
    pub source_anchor_analyses: u64,
    pub source_descendant_strings_avoided: u64,
    pub source_descendant_events_avoided: u64,
    pub source_descendant_analyses_avoided: u64,
    pub source_compressed_families_prepared: u64,
    pub source_compressed_cells_prepared: u64,
    pub source_partitioned_families_seen: u64,
    pub source_partitioned_families_prepared: u64,
    pub source_partitioned_families_rejected: u64,
    pub source_partition_fragments_prepared: u64,
    pub source_partition_span_cells_prepared: u64,
    pub source_partition_fallback_cells: u64,
    pub source_partition_analyses_reused: u64,
    pub source_partition_function_semantics: u64,
    pub source_partition_holes: u64,
    pub source_partition_ordinary_exceptions: u64,
    pub source_partition_failures: u64,
    pub source_partition_surviving_cells: u64,

    pub fallback_reasons: BTreeMap<String, u64>,
}

impl Default for FormulaIngestReport {
    fn default() -> Self {
        Self {
            mode: FormulaPlaneMode::Off,
            formula_cells_seen: 0,
            graph_formula_cells_materialized: 0,
            shadow_candidate_cells: 0,
            shadow_accepted_span_cells: 0,
            shadow_fallback_cells: 0,
            shadow_templates_interned: 0,
            shadow_spans_created: 0,
            graph_formula_vertices_avoided_shadow: 0,
            ast_roots_avoided_shadow: 0,
            edge_rows_avoided_shadow: 0,
            graph_vertices_created: 0,
            graph_edges_created: 0,
            source_formula_events: 0,
            source_formula_records_spooled: 0,
            source_spool_encoded_bytes: 0,
            source_spool_peak_memory_bytes: 0,
            source_spool_spilled_bytes: 0,
            source_spool_spill_files: 0,
            source_spool_replays: 0,
            source_ordinary_events: 0,
            source_shared_anchor_events: 0,
            source_shared_descendant_events: 0,
            source_unknown_events: 0,
            source_families_seen: 0,
            source_family_cells_seen: 0,
            source_family_shadow_eligible: 0,
            source_family_shadow_eligible_cells: 0,
            source_family_promoted: 0,
            source_family_promoted_cells: 0,
            source_family_fallback: 0,
            source_family_fallback_cells: 0,
            source_forward_descendants: 0,
            source_evidence_limit_fallbacks: 0,
            source_evidence_peak_bytes: 0,
            source_anchor_parses: 0,
            source_anchor_asts: 0,
            source_anchor_analyses: 0,
            source_descendant_strings_avoided: 0,
            source_descendant_events_avoided: 0,
            source_descendant_analyses_avoided: 0,
            source_compressed_families_prepared: 0,
            source_compressed_cells_prepared: 0,
            source_partitioned_families_seen: 0,
            source_partitioned_families_prepared: 0,
            source_partitioned_families_rejected: 0,
            source_partition_fragments_prepared: 0,
            source_partition_span_cells_prepared: 0,
            source_partition_fallback_cells: 0,
            source_partition_analyses_reused: 0,
            source_partition_function_semantics: 0,
            source_partition_holes: 0,
            source_partition_ordinary_exceptions: 0,
            source_partition_failures: 0,
            source_partition_surviving_cells: 0,
            fallback_reasons: BTreeMap::new(),
        }
    }
}

impl FormulaIngestReport {
    pub(crate) fn with_mode(mode: FormulaPlaneMode) -> Self {
        Self {
            mode,
            ..Self::default()
        }
    }

    pub(crate) fn accumulate(&mut self, other: &Self) {
        self.formula_cells_seen = self
            .formula_cells_seen
            .saturating_add(other.formula_cells_seen);
        self.graph_formula_cells_materialized = self
            .graph_formula_cells_materialized
            .saturating_add(other.graph_formula_cells_materialized);
        self.shadow_candidate_cells = self
            .shadow_candidate_cells
            .saturating_add(other.shadow_candidate_cells);
        self.shadow_accepted_span_cells = self
            .shadow_accepted_span_cells
            .saturating_add(other.shadow_accepted_span_cells);
        self.shadow_fallback_cells = self
            .shadow_fallback_cells
            .saturating_add(other.shadow_fallback_cells);
        self.shadow_templates_interned = self
            .shadow_templates_interned
            .saturating_add(other.shadow_templates_interned);
        self.shadow_spans_created = self
            .shadow_spans_created
            .saturating_add(other.shadow_spans_created);
        self.graph_formula_vertices_avoided_shadow = self
            .graph_formula_vertices_avoided_shadow
            .saturating_add(other.graph_formula_vertices_avoided_shadow);
        self.ast_roots_avoided_shadow = self
            .ast_roots_avoided_shadow
            .saturating_add(other.ast_roots_avoided_shadow);
        self.edge_rows_avoided_shadow = self
            .edge_rows_avoided_shadow
            .saturating_add(other.edge_rows_avoided_shadow);
        self.graph_vertices_created = self
            .graph_vertices_created
            .saturating_add(other.graph_vertices_created);
        self.graph_edges_created = self
            .graph_edges_created
            .saturating_add(other.graph_edges_created);
        self.source_formula_events = self
            .source_formula_events
            .saturating_add(other.source_formula_events);
        self.source_formula_records_spooled = self
            .source_formula_records_spooled
            .saturating_add(other.source_formula_records_spooled);
        self.source_spool_encoded_bytes = self
            .source_spool_encoded_bytes
            .saturating_add(other.source_spool_encoded_bytes);
        self.source_spool_peak_memory_bytes = self
            .source_spool_peak_memory_bytes
            .max(other.source_spool_peak_memory_bytes);
        self.source_spool_spilled_bytes = self
            .source_spool_spilled_bytes
            .saturating_add(other.source_spool_spilled_bytes);
        self.source_spool_spill_files = self
            .source_spool_spill_files
            .saturating_add(other.source_spool_spill_files);
        self.source_spool_replays = self
            .source_spool_replays
            .saturating_add(other.source_spool_replays);
        self.source_ordinary_events = self
            .source_ordinary_events
            .saturating_add(other.source_ordinary_events);
        self.source_shared_anchor_events = self
            .source_shared_anchor_events
            .saturating_add(other.source_shared_anchor_events);
        self.source_shared_descendant_events = self
            .source_shared_descendant_events
            .saturating_add(other.source_shared_descendant_events);
        self.source_unknown_events = self
            .source_unknown_events
            .saturating_add(other.source_unknown_events);
        self.source_families_seen = self
            .source_families_seen
            .saturating_add(other.source_families_seen);
        self.source_family_cells_seen = self
            .source_family_cells_seen
            .saturating_add(other.source_family_cells_seen);
        self.source_family_shadow_eligible = self
            .source_family_shadow_eligible
            .saturating_add(other.source_family_shadow_eligible);
        self.source_family_shadow_eligible_cells = self
            .source_family_shadow_eligible_cells
            .saturating_add(other.source_family_shadow_eligible_cells);
        self.source_family_promoted = self
            .source_family_promoted
            .saturating_add(other.source_family_promoted);
        self.source_family_promoted_cells = self
            .source_family_promoted_cells
            .saturating_add(other.source_family_promoted_cells);
        self.source_family_fallback = self
            .source_family_fallback
            .saturating_add(other.source_family_fallback);
        self.source_family_fallback_cells = self
            .source_family_fallback_cells
            .saturating_add(other.source_family_fallback_cells);
        self.source_forward_descendants = self
            .source_forward_descendants
            .saturating_add(other.source_forward_descendants);
        self.source_evidence_limit_fallbacks = self
            .source_evidence_limit_fallbacks
            .saturating_add(other.source_evidence_limit_fallbacks);
        self.source_evidence_peak_bytes = self
            .source_evidence_peak_bytes
            .max(other.source_evidence_peak_bytes);
        self.source_anchor_parses = self
            .source_anchor_parses
            .saturating_add(other.source_anchor_parses);
        self.source_anchor_asts = self
            .source_anchor_asts
            .saturating_add(other.source_anchor_asts);
        self.source_anchor_analyses = self
            .source_anchor_analyses
            .saturating_add(other.source_anchor_analyses);
        self.source_descendant_strings_avoided = self
            .source_descendant_strings_avoided
            .saturating_add(other.source_descendant_strings_avoided);
        self.source_descendant_events_avoided = self
            .source_descendant_events_avoided
            .saturating_add(other.source_descendant_events_avoided);
        self.source_descendant_analyses_avoided = self
            .source_descendant_analyses_avoided
            .saturating_add(other.source_descendant_analyses_avoided);
        self.source_compressed_families_prepared = self
            .source_compressed_families_prepared
            .saturating_add(other.source_compressed_families_prepared);
        self.source_compressed_cells_prepared = self
            .source_compressed_cells_prepared
            .saturating_add(other.source_compressed_cells_prepared);
        self.source_partitioned_families_seen = self
            .source_partitioned_families_seen
            .saturating_add(other.source_partitioned_families_seen);
        self.source_partitioned_families_prepared = self
            .source_partitioned_families_prepared
            .saturating_add(other.source_partitioned_families_prepared);
        self.source_partitioned_families_rejected = self
            .source_partitioned_families_rejected
            .saturating_add(other.source_partitioned_families_rejected);
        self.source_partition_fragments_prepared = self
            .source_partition_fragments_prepared
            .saturating_add(other.source_partition_fragments_prepared);
        self.source_partition_span_cells_prepared = self
            .source_partition_span_cells_prepared
            .saturating_add(other.source_partition_span_cells_prepared);
        self.source_partition_fallback_cells = self
            .source_partition_fallback_cells
            .saturating_add(other.source_partition_fallback_cells);
        self.source_partition_analyses_reused = self
            .source_partition_analyses_reused
            .saturating_add(other.source_partition_analyses_reused);
        self.source_partition_function_semantics = self
            .source_partition_function_semantics
            .saturating_add(other.source_partition_function_semantics);
        self.source_partition_holes = self
            .source_partition_holes
            .saturating_add(other.source_partition_holes);
        self.source_partition_ordinary_exceptions = self
            .source_partition_ordinary_exceptions
            .saturating_add(other.source_partition_ordinary_exceptions);
        self.source_partition_failures = self
            .source_partition_failures
            .saturating_add(other.source_partition_failures);
        self.source_partition_surviving_cells = self
            .source_partition_surviving_cells
            .saturating_add(other.source_partition_surviving_cells);
        for (reason, count) in &other.fallback_reasons {
            let total = self.fallback_reasons.entry(reason.clone()).or_default();
            *total = total.saturating_add(*count);
        }
    }
}