Skip to content

Commit

Permalink
[FIX] evaluation: Ensure dependency invalidation for array formulas
Browse files Browse the repository at this point in the history
How to reproduce:
-> see the attached test

What is happening?
During the evaluation of SUMIFS, it will fetch its ranges.
First it will first fetch an empty range (E4:E7)  since MUNIT hasn't
been evaluated yet.
Afterwards, it will fetch H4, which in turn will request C4
and therefore force the evaluation of the spreaded function MUNIT.

AT this point, there is a disparity of values because the values
of the range (E4:E7) have changed.
To address those situations, the evaluator invalidates the dependencies
of all the spreaded zone. Which means marking A1 to be recomputed.

Unfortunately, A1 is still being evaluated and since #4589 (specifically
commit 8d22e86) we remove the cell from the queue after we evaluted it.

This revision changes the approach taken in 8d22e86 to allow
legitimate invalidations take place while avoiding useless
reevaluations.

Task: 4252800
Part-of: #5209
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
  • Loading branch information
rrahir committed Nov 15, 2024
1 parent a14bf60 commit 7640aa5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/plugins/ui_core_views/cell_evaluation/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class Evaluator {
const arrayFormulas = this.spreadingRelations.getFormulaPositionsSpreadingOn(positionId);
const cells = new JetSet<PositionId>(arrayFormulas);
const arrayFormulaPositionId = this.getArrayFormulaSpreadingOnId(positionId);
if (arrayFormulaPositionId) {
if (arrayFormulaPositionId !== undefined) {
// ignore the formula spreading on the position. Keep only the blocked ones
cells.delete(arrayFormulaPositionId);
}
Expand All @@ -221,6 +221,9 @@ export class Evaluator {
}
for (let i = 0; i < positionIds.length; ++i) {
const cell = positionIds[i];
if (this.nextPositionsToUpdate.has(cell)) {
continue;
}
this.setEvaluatedCell(cell, this.computeCell(cell));
}
}
Expand Down Expand Up @@ -259,7 +262,6 @@ export class Evaluator {
return this.handleError(e);
} finally {
this.cellsBeingComputed.delete(cellId);
this.nextPositionsToUpdate.delete(positionId);
}
}

Expand Down Expand Up @@ -348,6 +350,7 @@ export class Evaluator {
{ sheetId, zone: rightPartZone },
{ sheetId, zone: leftColumnZone },
]);
invalidatedPositions.delete(this.encoder.encode(arrayFormulaPosition));
this.nextPositionsToUpdate.addMany(invalidatedPositions);
}

Expand Down
13 changes: 13 additions & 0 deletions tests/evaluation/evaluation_formula_array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,19 @@ describe("evaluate formulas that return an array", () => {
expect(mockCompute).toHaveBeenCalledTimes(3);
});

test("Formula depending on array formula is reevaluated when the array formula result changes", () => {
const model = new Model();
setCellContent(model, "A1", "=sumifs(E4:E7,H4:H7,1)");
setCellContent(model, "C4", "=MUNIT(4)");
setCellContent(model, "H4", "=C4");
setCellContent(model, "H6", "=E6");
expect(getEvaluatedCell(model, "A1").value).toBe(1);

// Force a reevaluation to avoid the incremental evaluation following each update_cell
model.dispatch("EVALUATE_CELLS");
expect(getEvaluatedCell(model, "A1").value).toBe(1);
});

test("Spreaded formulas with range deps invalidate only once the dependencies of themselves", () => {
let c = 0;
functionRegistry.add("INCREMENTONEVAL", {
Expand Down

0 comments on commit 7640aa5

Please sign in to comment.