feat: semi-additive measures - #2502
Draft
betodealmeida wants to merge 19 commits into
Draft
Conversation
✅ Deploy Preview for thriving-cassata-78ae72 canceled.
|
# Conflicts: # datajunction-server/datajunction_server/models/deployment.py
betodealmeida
force-pushed
the
semi-additive-metrics
branch
from
September 4, 2026 16:41
872a8e9 to
ca4d879
Compare
betodealmeida
force-pushed
the
semi-additive-metrics
branch
from
September 4, 2026 17:21
ca4d879 to
5b3ba3f
Compare
betodealmeida
force-pushed
the
semi-additive-metrics
branch
from
September 4, 2026 21:13
733896b to
17e0ca9
Compare
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
This PR adds metric reaggregation support for semi-additive measures, aligned with the proposal #2245. Metrics can declare protected dimensions, and the aggregation behavior changes when the query grain drops the dimension.
For example, suppose we have this metric:
Meaning:
If we get a request that includes the protected dimension:
We generate this SQL:
Because
date_id_orderis still in the output grain, there is no semi-additive collapse. Normal aggregation is safe.On the other hand, if we don't request the protected dimension we shouldn't aggregate over it:
The generated SQL:
Here, the user asked for
category only, but DJ keepsdate_id_orderas a private inner grain. Then it collapses each category’s daily balances withMAX_BY(value, date), meaning "take the value from the latest date."Without this, DJ would generate something like:
That would incorrectly sum balances across dates, which is wrong for snapshots or balances, for example.
I've updated the UI to show and allow setting the semi-additive dimension:
When editing:
Valid options:
Test Plan
make checkpassesmake testshows 100% unit test coverageDeployment Plan