fix: make min and max aggregates ignore retracted values - #90
Open
cpruijsen wants to merge 1 commit into
Open
Conversation
Retracted extremes stayed in the result because min and max ignored multiplicity. Net each value first and only consider those still present.
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.
groupByminandmaxnow net each value's multiplicity and only consider values that are still present (multiplicity > 0), the same ruleavg/modeandexamples/min_max.tsalready use.minandmaxpreviously ignored multiplicity, so a retracted extreme stayed in the result. Same change in@electric-sql/d2tsand@electric-sql/d2mini. SQLitegroupByre-exports these functions, so it picks up the fix with no extra copy.Fixes #88
Decision
Consolidate by value, then min/max over net-positive multiplicities. The alternative is to throw when any value has negative net multiplicity, as
MultiSet.min/MultiSet.maxdo. A streaming retract is a normal-1in this operator, not an illegal collection; throwing would break the path the issue describes. Can switch to the MultiSet semantics if you want that instead.Test plan
packages/d2tstests/operators/groupBy.test.ts,with min and max aggregates: category A is{10, 20, 5}, then+25, then-5/-25→minimum: 10,maximum: 20.packages/d2minisame test (delta order is retract-then-insert; values must change, not only emit).packages/d2tstests/operators-sqlite/groupBy.test.ts(same assertions; uses the re-exportedmin/max; needs Node 20 /better-sqlite3native bindings).