Use correct aggregation function for quantities total#8769
Merged
pieterbeulque merged 3 commits intomainfrom Jan 7, 2026
Merged
Use correct aggregation function for quantities total#8769pieterbeulque merged 3 commits intomainfrom
pieterbeulque merged 3 commits intomainfrom
Conversation
Add test_interval_non_summable_aggregation to verify that the `total` field in MeterQuantities is computed correctly for non-summable aggregations (max, min) across multiple days. This test exposes a regression introduced in commit 668ea64 where the total was always computed using SUM, even for meters configured with MAX, MIN, or other non-summable aggregations. Example of the bug: - Meter with MAX aggregation over 3 days - Day 1: max = 20, Day 2: none = 0, Day 3: max = 15 - Expected total: max(20, 0, 15) = 20 - Actual (buggy) total: sum(20 + 0 + 15) = 35
Fix regression introduced in commit 668ea64 where the `total` field in MeterQuantities was always computed using SUM, even for meters configured with non-summable aggregations like MAX or MIN. The fix determines the appropriate SQL window function based on whether the meter's aggregation is summable: - For summable aggregations (count, sum): continue using func.sum - For non-summable aggregations (max, min): use the meter's aggregation function (e.g., func.max for MAX aggregation) Example of the bug this fixes: - Meter with MAX aggregation tracking "total_enabled_servers" - Daily values over 30 days: each day reports max of 1 - Expected total: max(1, 1, ..., 1) = 1 - Buggy total: sum(1 + 1 + ... + 1) = 30 Note: avg and unique aggregations require special handling (weighted averages and deduplication respectively) that is not addressed here.
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
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
Fixes a regression in the
/v1/meters/{id}/quantitiesAPI endpoint where the total field was incorrectly calculated for meters with non-summable aggregations (MAX, MIN).Bug
Users reported seeing incorrect values in the total field:
Root Cause
This is a regression introduced in commit 668ea64 (Dec 18, 2025) titled "meter: Speed up query by avoiding large cartesian join".
Before the regression
The original code correctly used the meter's aggregation function for the total:
After the regression
Commit 668ea64
The performance optimization refactored the query to use pre-computed daily aggregates, but hardcoded func.sum() for the total:
This works correctly for summable aggregations (count, sum) but is incorrect for non-summable ones (max, min, avg, unique).
Fix
The fix determines the appropriate SQL window function based on whether the meter's aggregation is summable:
This is still broken for average & unique aggregation meters.