Skip to content

fix(compiler): partition semi-additive collapse by source grain - #234

Merged
mischuh merged 1 commit into
mainfrom
fix/semi-additive-partition-by-grain
Jul 18, 2026
Merged

fix(compiler): partition semi-additive collapse by source grain#234
mischuh merged 1 commit into
mainfrom
fix/semi-additive-partition-by-grain

Conversation

@mischuh

@mischuh mischuh commented Jul 18, 2026

Copy link
Copy Markdown
Owner

Summary

  • _build_semi_additive's ROW_NUMBER() window was partitioned by the queried output dimensions instead of the source's grain (minus collapse_dimension). A scalar query (no dimensions) therefore had an empty PARTITION BY, so the window ranked the entire table and rn = 1 matched only one arbitrary row among several tied-latest rows — e.g. an ending_inventory metric over 14 vehicles at the same latest snapshot_date returned 1 instead of 14.
  • Fixes this by resolving the source's declared grain (minus collapse_dimension) to dimensions and always partitioning on that, independent of which dimensions are requested for output.
  • Found while wiring up real inventory data for the rental example (#233) — the existing test_ac1_scalar_no_dims test only asserted ROW_NUMBER() was present, not that the partitioning (and therefore the result) was correct, so this regressed silently.

Test plan

  • Strengthened test_ac1_scalar_no_dims to assert PARTITION BY is present
  • Added test_ac1_scalar_no_dims_sums_across_entities, a DuckDB-backed regression test executing the compiled SQL and asserting the correct summed value
  • ruff check . && ruff format --check .
  • mypy canonic/compiler/strategies/semi_additive.py
  • pytest tests/ -x --tb=short (1988 passed)

🤖 Generated with Claude Code

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.
@mischuh mischuh self-assigned this Jul 18, 2026
@mischuh
mischuh merged commit 2923fd1 into main Jul 18, 2026
6 checks passed
@mischuh
mischuh deleted the fix/semi-additive-partition-by-grain branch July 18, 2026 15:01
@mischuh mischuh mentioned this pull request Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant