fix(compiler): partition semi-additive collapse by source grain - #234
Merged
Conversation
ROW_NUMBER() was partitioned by the queried output dimensions instead of the source's grain, so a scalar query (no dimensions) ranked the entire table and rn=1 matched only one arbitrary row instead of one per entity — e.g. ending_inventory returned 1 instead of summing the latest snapshot across all vehicles.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
_build_semi_additive'sROW_NUMBER()window was partitioned by the queried output dimensions instead of the source's grain (minuscollapse_dimension). A scalar query (no dimensions) therefore had an emptyPARTITION BY, so the window ranked the entire table andrn = 1matched only one arbitrary row among several tied-latest rows — e.g. anending_inventorymetric over 14 vehicles at the same latestsnapshot_datereturned1instead of14.grain(minuscollapse_dimension) to dimensions and always partitioning on that, independent of which dimensions are requested for output.test_ac1_scalar_no_dimstest only assertedROW_NUMBER()was present, not that the partitioning (and therefore the result) was correct, so this regressed silently.Test plan
test_ac1_scalar_no_dimsto assertPARTITION BYis presenttest_ac1_scalar_no_dims_sums_across_entities, a DuckDB-backed regression test executing the compiled SQL and asserting the correct summed valueruff check . && ruff format --check .mypy canonic/compiler/strategies/semi_additive.pypytest tests/ -x --tb=short(1988 passed)🤖 Generated with Claude Code