-
Notifications
You must be signed in to change notification settings - Fork 140
feat: Implement new config param, allowCircularReferences #1541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mountEvarus
wants to merge
2
commits into
handsontable:develop
Choose a base branch
from
Stellar-Fusion:feat/circular-deps
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+465
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ import {Ast, RelativeDependency} from './parser' | |
| import {Statistics, StatType} from './statistics' | ||
|
|
||
| export class Evaluator { | ||
| private readonly iterationCount = 100 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be configurable. I'd introduce a new flag |
||
|
|
||
| constructor( | ||
| private readonly config: Config, | ||
|
|
@@ -43,6 +44,7 @@ export class Evaluator { | |
|
|
||
| public partialRun(vertices: Vertex[]): ContentChanges { | ||
| const changes = ContentChanges.empty() | ||
| const cycled: Vertex[] = [] | ||
|
|
||
| this.stats.measure(StatType.EVALUATION, () => { | ||
| this.dependencyGraph.graph.getTopSortedWithSccSubgraphFrom(vertices, | ||
|
|
@@ -68,15 +70,17 @@ export class Evaluator { | |
| if (vertex instanceof RangeVertex) { | ||
| vertex.clearCache() | ||
| } else if (vertex instanceof FormulaVertex) { | ||
| const address = vertex.getAddress(this.lazilyTransformingAstService) | ||
| this.columnSearch.remove(getRawValue(vertex.valueOrUndef()), address) | ||
| const error = new CellError(ErrorType.CYCLE, undefined, vertex) | ||
| vertex.setCellValue(error) | ||
| changes.addChange(error, address) | ||
| const firstCycleChanges = this.iterateCircularDependencies([vertex], 1) | ||
| changes.addAll(firstCycleChanges) | ||
| cycled.push(vertex) | ||
| } | ||
| }, | ||
| ) | ||
| }) | ||
|
|
||
| const cycledChanges = this.iterateCircularDependencies(cycled, this.iterationCount - 1) | ||
| changes.addAll(cycledChanges) | ||
|
|
||
| return changes | ||
| } | ||
|
|
||
|
|
@@ -105,11 +109,8 @@ export class Evaluator { | |
| * Recalculates formulas in the topological sort order | ||
| */ | ||
| private recomputeFormulas(cycled: Vertex[], sorted: Vertex[]): void { | ||
| cycled.forEach((vertex: Vertex) => { | ||
| if (vertex instanceof FormulaVertex) { | ||
| vertex.setCellValue(new CellError(ErrorType.CYCLE, undefined, vertex)) | ||
| } | ||
| }) | ||
| this.iterateCircularDependencies(cycled) | ||
|
|
||
| sorted.forEach((vertex: Vertex) => { | ||
| if (vertex instanceof FormulaVertex) { | ||
| const newCellValue = this.recomputeFormulaVertexValue(vertex) | ||
|
|
@@ -121,6 +122,143 @@ export class Evaluator { | |
| }) | ||
| } | ||
|
|
||
| private blockCircularDependencies(cycled: Vertex[]): ContentChanges { | ||
| const changes = ContentChanges.empty() | ||
|
|
||
| cycled.forEach((vertex: Vertex) => { | ||
| if (vertex instanceof RangeVertex) { | ||
| vertex.clearCache() | ||
| } else if (vertex instanceof FormulaVertex) { | ||
| const address = vertex.getAddress(this.lazilyTransformingAstService) | ||
| this.columnSearch.remove(getRawValue(vertex.valueOrUndef()), address) | ||
| const error = new CellError(ErrorType.CYCLE, undefined, vertex) | ||
| vertex.setCellValue(error) | ||
| changes.addChange(error, address) | ||
| } | ||
| }) | ||
|
|
||
| return changes | ||
| } | ||
|
|
||
| /** | ||
| * Iterates over all circular dependencies (cycled vertices) for 100 iterations | ||
| * Handles cascading dependencies by processing cycles in dependency order | ||
| */ | ||
| private iterateCircularDependencies(cycled: Vertex[], cycles = this.iterationCount): ContentChanges { | ||
| if (!this.config.allowCircularReferences) { | ||
| return this.blockCircularDependencies(cycled) | ||
| } | ||
|
|
||
| const changes = ContentChanges.empty() | ||
| cycled.forEach((vertex: Vertex) => { | ||
| if (vertex instanceof FormulaVertex && !vertex.isComputed()) { | ||
| vertex.setCellValue(0) | ||
| } | ||
| }) | ||
|
|
||
| for (let i = 0; i < cycles; i++) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| this.clearCachesForCyclicRanges(cycled) | ||
|
|
||
| cycled.forEach((vertex: Vertex) => { | ||
| if (!(vertex instanceof FormulaVertex)) { | ||
| return | ||
| } | ||
|
|
||
| const address = vertex.getAddress(this.lazilyTransformingAstService) | ||
| const newCellValue = this.recomputeFormulaVertexValue(vertex) | ||
|
|
||
| if (i < cycles - 1) { | ||
| return | ||
| } | ||
|
|
||
| this.columnSearch.add(getRawValue(newCellValue), address) | ||
| changes.addChange(newCellValue, address) | ||
mountEvarus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
| } | ||
|
|
||
| const dependentChanges = this.updateNonCyclicDependents(cycled) | ||
| changes.addAll(dependentChanges) | ||
|
|
||
| return changes | ||
| } | ||
|
|
||
| /** | ||
| * Updates all non-cyclic cells that depend on the given cycled vertices | ||
| * Uses topological sorting to ensure correct dependency order | ||
| */ | ||
| private updateNonCyclicDependents(cycled: Vertex[]): ContentChanges { | ||
| const changes = ContentChanges.empty() | ||
| const cyclicSet = new Set(cycled) | ||
|
|
||
| const dependents = new Set<Vertex>() | ||
| cycled.forEach(vertex => { | ||
| this.dependencyGraph.graph.adjacentNodes(vertex).forEach(dependent => { | ||
| if (!cyclicSet.has(dependent) && dependent instanceof FormulaVertex) { | ||
| dependents.add(dependent) | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| if (dependents.size === 0) { | ||
| return changes | ||
| } | ||
|
|
||
| const {sorted} = this.dependencyGraph.topSortWithScc() | ||
| const orderedDependents = sorted.filter(vertex => dependents.has(vertex)) | ||
|
|
||
| orderedDependents.forEach(vertex => { | ||
| if (vertex instanceof FormulaVertex) { | ||
| const newCellValue = this.recomputeFormulaVertexValue(vertex) | ||
| const address = vertex.getAddress(this.lazilyTransformingAstService) | ||
| this.columnSearch.add(getRawValue(newCellValue), address) | ||
| changes.addChange(newCellValue, address) | ||
| } | ||
| }) | ||
|
|
||
| return changes | ||
| } | ||
|
|
||
| /** | ||
| * Clears function caches for ranges that contain any of the given cyclic vertices | ||
| * This ensures fresh computation during circular dependency iteration | ||
| */ | ||
| private clearCachesForCyclicRanges(cycled: Vertex[]): void { | ||
| const cyclicAddresses = new Set<string>() | ||
| cycled.forEach((vertex: Vertex) => { | ||
| if (vertex instanceof FormulaVertex) { | ||
| const address = vertex.getAddress(this.lazilyTransformingAstService) | ||
| cyclicAddresses.add(`${address.sheet}:${address.col}:${address.row}`) | ||
| } | ||
| }) | ||
|
|
||
| const sheetsWithCycles = new Set<number>() | ||
| cycled.forEach((vertex: Vertex) => { | ||
| if (vertex instanceof FormulaVertex) { | ||
| const address = vertex.getAddress(this.lazilyTransformingAstService) | ||
| sheetsWithCycles.add(address.sheet) | ||
| } | ||
| }) | ||
|
|
||
| sheetsWithCycles.forEach(sheet => { | ||
| for (const rangeVertex of this.dependencyGraph.rangeMapping.rangesInSheet(sheet)) { | ||
| const range = rangeVertex.range | ||
| let containsCyclicCell = false | ||
|
|
||
| for (const address of range.addresses(this.dependencyGraph)) { | ||
| const addressKey = `${address.sheet}:${address.col}:${address.row}` | ||
| if (cyclicAddresses.has(addressKey)) { | ||
| containsCyclicCell = true | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if (containsCyclicCell) { | ||
| rangeVertex.clearCache() | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| private recomputeFormulaVertexValue(vertex: FormulaVertex): InterpreterValue { | ||
| const address = vertex.getAddress(this.lazilyTransformingAstService) | ||
| if (vertex instanceof ArrayVertex && (vertex.array.size.isRef || !this.dependencyGraph.isThereSpaceForArray(vertex))) { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rename this option to make it similar to the analogues feature in other spreadsheet software.