Skip to content

fix: pass the second state to Pivot's Difference - #187

Open
Peter Shrosbree (pcshrosbree) wants to merge 1 commit into
microsoft:masterfrom
pcshrosbree:upstream/pivot-difference
Open

Peter Shrosbree (pcshrosbree) wants to merge 1 commit into
microsoft:masterfrom
pcshrosbree:upstream/pivot-difference

Conversation

@pcshrosbree

@pcshrosbree Peter Shrosbree (pcshrosbree) commented Nov 2, 2026 •

Copy link
Copy Markdown

TL;DR: Pivot can return wrong results when an interval event ends while another event in the same group is still alive. This fixes the second argument passed to Difference and adds three regression tests, each run in Trill's row-mode and columnar test configurations.

Fixes #184.

Pivot builds one combined aggregate whose difference calls each attribute's Difference(). It passed state1 for both arguments, so each attribute computed Difference(state1, state1) (PivotExtensions.cs line 84). The snapshot window calls difference when an interval event ends while another event of its group is still alive. After that, the group's result left out the events still alive: 0 with Sum and NaN with Average in the example in #184. This change passes state2 as the second argument.

The new test file PivotDifferenceTests.cs runs three tests in two configurations: one forces row mode, and the other requests columnar mode with fallback disabled. In both, the aggregate itself runs in row mode, because it returns an array and Trill cannot hold an array result in columns (SnapshotWindowStreamable.cs line 378). That check comes before code generation, so the columnar configuration does not throw. The tests:

  • Sum over a:1 [1,6) and a:3 [3,8) gives 3 from time 6;
  • Average over the same events gives 3 from time 6;
  • with a:5 [6,8) added, Sum gives 8 from time 6.

On this branch's base, with only the test file added, all 6 runs fail on the output at time 6. With the fix, all 6 pass. I ran them on netcoreapp3.1, rolled forward to .NET 10.0.13 on Linux x64, because only .NET 10 runtimes are installed here.

Note

Prepared with AI assistance (Anthropic Claude, OpenAI Codex). I reviewed it before posting.

Pivot combines one aggregate per output field into a single aggregate
whose state is an array. Its Difference passed the first state array as
both arguments, so each field computed Difference(state1, state1)
instead of Difference(state1, state2).

The engine calls Difference when an interval event ends while another
event of its group is still alive. For Sum and Average,
Difference(state1, state1) is an empty state, so the group's result
then left out the events still alive. With a:1 over [1,6) and a:3 over
[3,8), Pivot reported 0 with Sum and NaN with Average from time 6 to
time 8; the right result is 3. An event that ends through an end edge goes through
Deaccumulate instead, so it was not affected.

PivotDifferenceTests runs in row and columnar mode. Its three tests fail
before this change and pass after it.
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.

Pivot gives a wrong result after an interval event ends while another event of its group is alive

1 participant