Repository navigation
fix: count weekly buckets in days so Sunday rows land in their week - #1069
EladBarkay merged 3 commits into
Conversation
On Postgres, datediff(week) starts weeks on Sunday while buckets run Monday to Monday, so Sunday rows were counted in the next bucket and the last one got no bucket at all, which crashed the anomaly test result sort. Fixes elementary-data/elementary#2370
|
👋 @AdamoElProfesor |
|
Important Review skippedReview was skipped as selected files did not have any reviewable changes. ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (2)
Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review. 📝 WalkthroughWalkthroughThe weekly bucket calculation now converts the period to days and scales the count by seven. An integration test checks weekly ChangesWeekly column anomaly buckets
Priority: ➖ Normal Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix · Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to This change makes weekly column anomaly buckets count in days, so Sunday rows land in the correct Monday-to-Monday week. It adds a regression test for this case. No concrete merge-blocking risk was found. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring CoverageExplanation Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 1 files. (1 skipped: 1 unsupported.) ✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
Comment |
EladBarkay
left a comment
There was a problem hiding this comment.
Thanks @AdamoElProfesor, clean fix and a well-designed test! 🙏
Fix: verified on Postgres 16. date_trunc('week') starts on Monday while dbt-postgres's datediff('week') counts from Sunday, so counting days from min_bucket_start is the right approach. It also fixes the incremental path (incremental models and sources), where min_bucket_start is the backfill start and can be any weekday: with a Monday run followed by a Thursday run, master produces a NULL bucket and a false anomaly, and this PR fixes it.
Test: varying the Sunday null counts is a nice touch, since a one-day shift can't hide. Two things before we merge:
- It assumes weeks start on Monday. On BigQuery (
timestamp_trunc(..., week)) and SQL Server (DATEFIRST) weeks start on Sunday, so theisoweekday() == 1check and theexpected_null_countslookup would fail there. Instead, you could compute each bucket's expected count from the data, as the Sunday nulls in[bucket_start, bucket_start + 7 days). - It doesn't check that every week is present.
assert null_countspasses as long as one week exists, so a change that dropped weeks, including the latest one (the week that crashed), would still pass. Asserting that all 12 weeks are there would cover it.
Non-blocking follow-ups (happy to take these on our side): a two-run incremental test (Monday, then Thursday), a weekly volume_anomalies test, and the same fix for periods_until_max in get_buckets_configuration.sql:88.
Dismissing this approval until the test is fixed: the new test fails on SQL Server (CI run: 2026-07-26 is not a Monday). This is point #1 from my review: SQL Server starts weeks on Sunday, and BigQuery does too. Point #2 (checking that all 12 weeks are present) can be a follow-up. The macro fix itself looks good.
|
Dismissing this approval until the test is fixed: the new test fails on SQL Server (CI run: |
|
Thanks again @AdamoElProfesor! Since this one matters, we picked it up in #1083 so we could finish it. Your commit is kept there, so you keep the credit. We couldn't push to your fork's branch, so we opened a new PR. What #1083 adds on top of your fix:
We'll close this PR once #1083 is merged. |
Fixes elementary-data/elementary#2370
Problem
Weekly buckets run Monday to Monday, but on Postgres
datediff(..., 'week')starts weeks on Sunday. Inget_start_bucket_in_data, every Sunday row was assigned to the next bucket:max_bucket_endpointed to a bucket that doesn't exist, so the left join produced a metric row with a NULLbucket_start/bucket_end, and thesort(attribute='bucket_end')added in 0.26.0 failed with'<' not supported between instances of 'NoneType' and 'str'.Fix
For weekly buckets,
get_start_bucket_in_datanow counts days and groups them by 7 (week× N becomesday× 7N). Sincemin_bucket_startis truncated to the start of the week, this lands every row in the right bucket, whatever the adapter considers the first day of the week. Other periods are unchanged.Test
Added
test_column_anomalies_weekly_buckets_with_sunday_rows: 12 weeks of data with nulls only on Sundays and a different count each week. It checks that the test passes, that no metric has a NULL bucket, and that each weeklynull_countmatches its Monday-to-Sunday week. It failed onmasterwith the error from the issue and passes with the fix. The existing anomaly suites (column, dimension, volume, all columns, backfill, ranges, freshness) still pass on Postgres.Note:
get_metric_buckets_min_and_maxalso usesdatediff(..., 'week')to computemax_bucket_end. I left it out of this PR to keep it focused, happy to look at it separately.Summary by CodeRabbit