formualizer-eval 0.6.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
//! Graph-owned FormulaPlane authority shell.
//!
//! The authority owns accepted spans, tracks overlay punchouts, rebuilds
//! producer/read indexes, and projects edited regions into span-local dirty work
//! for opt-in authoritative FormulaPlane evaluation. Unsupported or demoted
//! formulas remain on the legacy dependency graph.

use rustc_hash::FxHashSet;

use super::producer::{FormulaConsumerReadIndex, FormulaProducerId, FormulaProducerResultIndex};
use super::region_index::Region;
use super::runtime::{FormulaPlane, FormulaSpanRef};

#[derive(Debug, Default)]
pub(crate) struct FormulaAuthority {
    pub(crate) plane: FormulaPlane,
    pub(crate) producer_results: FormulaProducerResultIndex,
    pub(crate) consumer_reads: FormulaConsumerReadIndex,
    indexes_epoch: u64,
    /// Externally-observed changed regions accumulated since the last
    /// `take_pending_changed_regions` call. Edits that intersect span read
    /// regions drive bounded span dirty work via `compute_dirty_closure`.
    pending_changed_regions: Vec<Region>,
    pending_seen: FxHashSet<Region>,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct FormulaAuthorityIndexReport {
    pub(crate) plane_epoch: u64,
    pub(crate) indexes_epoch: u64,
    pub(crate) spans_seen: usize,
    pub(crate) spans_indexed: usize,
    pub(crate) producer_result_entries: usize,
    pub(crate) consumer_read_entries: usize,
    pub(crate) missing_read_summary_count: usize,
    pub(crate) stale_or_invalid_summary_count: usize,
}

impl FormulaAuthority {
    pub(crate) fn indexes_epoch(&self) -> u64 {
        self.indexes_epoch
    }

    pub(crate) fn active_span_count(&self) -> usize {
        self.plane.spans.active_spans().count()
    }

    pub(crate) fn active_span_refs(&self) -> Vec<FormulaSpanRef> {
        self.plane
            .spans
            .active_spans()
            .map(|span| FormulaSpanRef {
                id: span.id,
                generation: span.generation,
                version: span.version,
            })
            .collect()
    }

    pub(crate) fn record_changed_region(&mut self, region: Region) {
        if self.pending_seen.insert(region) {
            self.pending_changed_regions.push(region);
        }
    }

    pub(crate) fn take_pending_changed_regions(&mut self) -> Vec<Region> {
        self.pending_seen.clear();
        std::mem::take(&mut self.pending_changed_regions)
    }

    pub(crate) fn pending_changed_regions(&self) -> &[Region] {
        &self.pending_changed_regions
    }

    pub(crate) fn pending_changed_region_count(&self) -> usize {
        self.pending_changed_regions.len()
    }

    pub(crate) fn mark_all_active_spans_dirty(&mut self) {
        // Conservative escape hatch: invalidate every span by bumping the
        // authority index epoch. The FormulaPlane coordinator treats an unseen
        // epoch as `WholeAll`, which is the only representation that guarantees
        // self-dirtying for spans whose result region (rather than read region)
        // was structurally affected.
        if self.active_span_count() == 0 {
            return;
        }
        self.indexes_epoch = self.indexes_epoch.saturating_add(1);

        // Also publish result regions as changed regions so downstream span
        // consumers can be discovered through the normal dirty-closure path if
        // the caller evaluates before another epoch-bumping rebuild.
        let regions: Vec<Region> = self
            .plane
            .spans
            .active_spans()
            .map(|span| Region::from_domain(span.result_region.domain()))
            .collect();
        for region in regions {
            self.record_changed_region(region);
        }
    }

    pub(crate) fn rebuild_indexes(&mut self) -> FormulaAuthorityIndexReport {
        let mut producer_results = FormulaProducerResultIndex::default();
        let mut consumer_reads = FormulaConsumerReadIndex::default();
        let mut report = FormulaAuthorityIndexReport {
            plane_epoch: self.plane.epoch().0,
            ..FormulaAuthorityIndexReport::default()
        };

        for span in self.plane.spans.active_spans() {
            report.spans_seen = report.spans_seen.saturating_add(1);
            let result_region =
                super::region_index::Region::from_domain(span.result_region.domain());
            let producer = FormulaProducerId::Span(span.id);
            producer_results.insert_producer(producer, result_region);
            report.producer_result_entries = report.producer_result_entries.saturating_add(1);

            let Some(read_summary_id) = span.read_summary_id else {
                report.missing_read_summary_count =
                    report.missing_read_summary_count.saturating_add(1);
                continue;
            };
            let Some(read_summary) = self.plane.span_read_summaries.get(read_summary_id) else {
                report.stale_or_invalid_summary_count =
                    report.stale_or_invalid_summary_count.saturating_add(1);
                continue;
            };
            if read_summary.result_region != result_region {
                report.stale_or_invalid_summary_count =
                    report.stale_or_invalid_summary_count.saturating_add(1);
                continue;
            }

            for dependency in &read_summary.dependencies {
                consumer_reads.insert_read(
                    producer,
                    dependency.read_region,
                    read_summary.result_region,
                    dependency.projection,
                );
                report.consumer_read_entries = report.consumer_read_entries.saturating_add(1);
            }
            report.spans_indexed = report.spans_indexed.saturating_add(1);
        }

        self.indexes_epoch = self.indexes_epoch.saturating_add(1);
        report.indexes_epoch = self.indexes_epoch;
        self.producer_results = producer_results;
        self.consumer_reads = consumer_reads;
        report
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use formualizer_parse::parser::parse;

    use super::*;
    use crate::engine::arena::DataStore;
    use crate::engine::sheet_registry::SheetRegistry;
    use crate::formula_plane::producer::{
        AxisProjection, DirtyProjectionRule, ProducerDirtyDomain, ProjectionResult,
        SpanReadDependency, SpanReadSummary, compute_dirty_closure,
    };
    use crate::formula_plane::region_index::{Region, RegionKey};
    use crate::formula_plane::runtime::{
        FormulaSpanId, NewFormulaSpan, PlacementDomain, ResultRegion,
    };

    fn template(authority: &mut FormulaAuthority) -> crate::formula_plane::ids::FormulaTemplateId {
        authority.plane.intern_template(
            Arc::<str>::from("test-template"),
            {
                let mut data_store = DataStore::new();
                let sheet_registry = SheetRegistry::new();
                data_store.store_ast(&parse("=A1+1").unwrap(), &sheet_registry)
            },
            1,
            1,
            Some(Arc::<str>::from("=A1+1")),
        )
    }

    fn add_span_with_summary(
        authority: &mut FormulaAuthority,
        domain: PlacementDomain,
        summary: SpanReadSummary,
    ) -> FormulaSpanId {
        let sheet_id = domain.sheet_id();
        let template_id = template(authority);
        let read_summary_id = authority.plane.insert_span_read_summary(summary);
        authority
            .plane
            .insert_span(NewFormulaSpan {
                sheet_id,
                template_id,
                result_region: ResultRegion::scalar_cells(domain.clone()),
                domain,
                intrinsic_mask_id: None,
                read_summary_id: Some(read_summary_id),
                binding_set_id: None,
                is_constant_result: false,
            })
            .id
    }

    #[test]
    fn authority_rebuild_indexes_span_result_regions() {
        let mut authority = FormulaAuthority::default();
        let domain = PlacementDomain::row_run(0, 0, 9, 2);
        let summary = SpanReadSummary {
            result_region: Region::from_domain(&domain),
            dependencies: Vec::new(),
        };
        let span_id = add_span_with_summary(&mut authority, domain, summary);

        let report = authority.rebuild_indexes();

        assert_eq!(report.spans_seen, 1);
        assert_eq!(report.spans_indexed, 1);
        assert_eq!(report.producer_result_entries, 1);
        assert_eq!(report.consumer_read_entries, 0);
        assert_eq!(authority.producer_results.len(), 1);
        assert_eq!(authority.consumer_reads.len(), 0);
        assert_eq!(
            authority
                .producer_results
                .producer_result_region(FormulaProducerId::Span(span_id)),
            Some(Region::col_interval(0, 2, 0, 9))
        );
    }

    #[test]
    fn authority_rebuild_indexes_span_read_dependencies() {
        let mut authority = FormulaAuthority::default();
        let domain = PlacementDomain::row_run(0, 0, 9, 2);
        let result_region = Region::from_domain(&domain);
        let projection = DirtyProjectionRule::AffineCell {
            row: AxisProjection::Relative { offset: 0 },
            col: AxisProjection::Relative { offset: -1 },
        };
        let read_region = projection.read_region_for_result(0, result_region).unwrap();
        let span_id = add_span_with_summary(
            &mut authority,
            domain,
            SpanReadSummary {
                result_region,
                dependencies: vec![SpanReadDependency {
                    read_region,
                    projection,
                }],
            },
        );

        let report = authority.rebuild_indexes();

        assert_eq!(report.spans_indexed, 1);
        assert_eq!(report.consumer_read_entries, 1);
        let dirty = authority
            .consumer_reads
            .query_changed_region(Region::point(0, 5, 1));
        assert_eq!(dirty.matches.len(), 1);
        assert_eq!(
            dirty.matches[0].value.consumer,
            FormulaProducerId::Span(span_id)
        );
        assert_eq!(
            dirty.matches[0].value.dirty,
            ProjectionResult::Exact(ProducerDirtyDomain::Cells(vec![RegionKey::new(0, 5, 2)]))
        );
    }

    #[test]
    fn authority_rebuild_indexes_missing_read_summary_counts_and_indexes_result() {
        let mut authority = FormulaAuthority::default();
        let domain = PlacementDomain::row_run(0, 0, 9, 2);
        let template_id = template(&mut authority);
        let span = authority
            .plane
            .insert_span(NewFormulaSpan {
                sheet_id: 0,
                template_id,
                result_region: ResultRegion::scalar_cells(domain.clone()),
                domain,
                intrinsic_mask_id: None,
                read_summary_id: None,
                binding_set_id: None,
                is_constant_result: false,
            })
            .id;

        let report = authority.rebuild_indexes();

        assert_eq!(report.spans_seen, 1);
        assert_eq!(report.spans_indexed, 0);
        assert_eq!(report.missing_read_summary_count, 1);
        assert_eq!(report.producer_result_entries, 1);
        assert_eq!(report.consumer_read_entries, 0);
        assert_eq!(
            authority
                .producer_results
                .producer_result_region(FormulaProducerId::Span(span)),
            Some(Region::col_interval(0, 2, 0, 9))
        );
    }

    #[test]
    fn authority_rebuild_indexes_stale_summary_counts_without_read_entry() {
        let mut authority = FormulaAuthority::default();
        let domain = PlacementDomain::row_run(0, 0, 9, 2);
        let mismatched_result = Region::col_interval(0, 3, 0, 9);
        add_span_with_summary(
            &mut authority,
            domain,
            SpanReadSummary {
                result_region: mismatched_result,
                dependencies: vec![SpanReadDependency {
                    read_region: Region::col_interval(0, 1, 0, 9),
                    projection: DirtyProjectionRule::WholeResult,
                }],
            },
        );

        let report = authority.rebuild_indexes();

        assert_eq!(report.spans_seen, 1);
        assert_eq!(report.spans_indexed, 0);
        assert_eq!(report.stale_or_invalid_summary_count, 1);
        assert_eq!(report.producer_result_entries, 1);
        assert_eq!(report.consumer_read_entries, 0);
        assert_eq!(authority.consumer_reads.len(), 0);
    }

    #[test]
    fn authority_dirty_closure_uses_rebuilt_indexes() {
        let mut authority = FormulaAuthority::default();
        let b_domain = PlacementDomain::row_run(0, 0, 9, 1);
        let c_domain = PlacementDomain::row_run(0, 0, 9, 2);
        let projection = DirtyProjectionRule::AffineCell {
            row: AxisProjection::Relative { offset: 0 },
            col: AxisProjection::Relative { offset: -1 },
        };

        let b_result = Region::from_domain(&b_domain);
        let b_read = projection.read_region_for_result(0, b_result).unwrap();
        let b_span = add_span_with_summary(
            &mut authority,
            b_domain,
            SpanReadSummary {
                result_region: b_result,
                dependencies: vec![SpanReadDependency {
                    read_region: b_read,
                    projection,
                }],
            },
        );

        let c_result = Region::from_domain(&c_domain);
        let c_read = projection.read_region_for_result(0, c_result).unwrap();
        let c_span = add_span_with_summary(
            &mut authority,
            c_domain,
            SpanReadSummary {
                result_region: c_result,
                dependencies: vec![SpanReadDependency {
                    read_region: c_read,
                    projection,
                }],
            },
        );

        let report = authority.rebuild_indexes();
        assert_eq!(report.spans_indexed, 2);
        assert_eq!(report.consumer_read_entries, 2);

        let closure = compute_dirty_closure(
            &authority.consumer_reads,
            [Region::point(0, 5, 0)],
            |producer| authority.producer_results.producer_result_region(producer),
        );

        assert_eq!(closure.fallbacks, Vec::new());
        assert_eq!(closure.work.len(), 2);
        assert_eq!(closure.work[0].producer, FormulaProducerId::Span(b_span));
        assert_eq!(
            closure.work[0].dirty,
            ProducerDirtyDomain::Cells(vec![RegionKey::new(0, 5, 1)])
        );
        assert_eq!(closure.work[1].producer, FormulaProducerId::Span(c_span));
        assert_eq!(
            closure.work[1].dirty,
            ProducerDirtyDomain::Cells(vec![RegionKey::new(0, 5, 2)])
        );
    }

    #[test]
    fn authority_rebuild_replaces_stale_index_entries() {
        let mut authority = FormulaAuthority::default();
        let domain = PlacementDomain::row_run(0, 0, 9, 2);
        let summary = SpanReadSummary {
            result_region: Region::from_domain(&domain),
            dependencies: Vec::new(),
        };
        let template_id = template(&mut authority);
        let read_summary_id = authority.plane.insert_span_read_summary(summary);
        let span_ref = authority.plane.insert_span(NewFormulaSpan {
            sheet_id: 0,
            template_id,
            result_region: ResultRegion::scalar_cells(domain.clone()),
            domain,
            intrinsic_mask_id: None,
            read_summary_id: Some(read_summary_id),
            binding_set_id: None,
            is_constant_result: false,
        });
        let first = authority.rebuild_indexes();
        assert_eq!(first.producer_result_entries, 1);

        assert!(authority.plane.remove_span(span_ref));
        let second = authority.rebuild_indexes();
        assert_eq!(second.spans_seen, 0);
        assert_eq!(second.producer_result_entries, 0);
        assert_eq!(authority.producer_results.len(), 0);
        assert!(authority.indexes_epoch() > first.indexes_epoch);
    }
}