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: #5214
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
Signed-off-by: Rémi Rahir (rar) <[email protected]>
  • Loading branch information
rrahir committed Nov 15, 2024
1 parent 079a126 commit 07c48aa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/plugins/ui_core_views/cell_evaluation/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ export class Evaluator {
}
for (let i = 0; i < positions.length; ++i) {
const position = positions[i];
if (this.nextPositionsToUpdate.has(position)) {
continue;
}
const evaluatedCell = this.computeCell(position);
if (evaluatedCell !== EMPTY_CELL) {
this.evaluatedCells.set(position, evaluatedCell);
Expand Down Expand Up @@ -339,7 +342,6 @@ export class Evaluator {
return createEvaluatedCell(e);
} finally {
this.cellsBeingComputed.delete(cellId);
this.nextPositionsToUpdate.delete(position);
}
}

Expand Down Expand Up @@ -401,6 +403,7 @@ export class Evaluator {
const invalidatedPositions = this.formulaDependencies().getCellsDependingOn(
excludeTopLeft(resultZone).map((zone) => ({ sheetId, zone }))
);
invalidatedPositions.delete({ sheetId, col: resultZone.left, row: resultZone.top });
this.nextPositionsToUpdate.addMany(invalidatedPositions);
}

Expand Down
19 changes: 16 additions & 3 deletions tests/evaluation/evaluation_formula_array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,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 Expand Up @@ -707,9 +720,9 @@ describe("evaluate formulas that return an array", () => {

setCellContent(model, "A1", "2");

expect(getEvaluatedCell(model, "B1").value).toBe("#SPILL!");
expect(getEvaluatedCell(model, "B2").value).toBe(null);
expect(getEvaluatedCell(model, "B3").value).toBe(0);
expect(getEvaluatedCell(model, "B1").value).toBe(42);
expect(getEvaluatedCell(model, "B2").value).toBe(42);
expect(getEvaluatedCell(model, "B3").value).toBe(42);

expect(getEvaluatedCell(model, "A3").value).toBe("#SPILL!");
});
Expand Down

0 comments on commit 07c48aa

Please sign in to comment.