Skip to content

fix(db_engine_specs): use fixed Monday week start for Oracle and Impala Week grain - #45858

Open
dhruv-techdev wants to merge 3 commits into
apache:masterfrom
dhruv-techdev:fix/oracle-impala-week-grain
Open

dhruv-techdev wants to merge 3 commits into
apache:masterfrom
dhruv-techdev:fix/oracle-impala-week-grain

Conversation

@dhruv-techdev

Copy link
Copy Markdown
Contributor

SUMMARY

The Week time grain for Oracle and Impala used TRUNC(..., 'WW'). In both databases, 'WW' truncates to the same weekday as January 1st, not to a fixed week start. As a result:

  • The weekday a week starts on changes every year (2025 weeks start on Wednesday, 2026 weeks on Thursday).
  • Every year ends with a 1-day "week" (2 days in leap years), e.g. 2025-12-31 gets its own bucket.
  • Weeks never cross a year boundary.

This PR changes the unit to one that always truncates to Monday, matching the fixed week start used by other engines:

  • Oracle: 'WW' → 'IW' (ISO week). 'IW' doesn't depend on NLS_TERRITORY, unlike 'D'/'DY'.
  • Impala: 'WW' → 'D'. Impala's TRUNC does not accept 'IW'; 'D'/'DY'/'DAY' truncate to the previous Monday (see TruncDayOfWeek / StrToTruncUnit in be/src/exprs/udf-builtins.cc).

The change is one line per engine spec, plus a regression test for each that pins the generated SQL.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

Oracle Database 23ai Free (gvenzl/oracle-free:23-slim). BEFORE (WW) is the old TimeGrain.WEEK expression and AFTER (IW) is the new one:
Screenshot 2026-10-29 at 15 15 23

TESTING INSTRUCTIONS

Unit tests

pytest tests/unit_tests/db_engine_specs/test_oracle.py tests/unit_tests/db_engine_specs/test_impala.py

Both new tests fail on master ('WW' vs 'IW' / 'D') and pass with this change.

Manual check on Oracle

docker run -d --name oracle-ww -p 1521:1521 -e ORACLE_PASSWORD=test gvenzl/oracle-free:23-slim
until docker logs oracle-ww 2>&1 | grep -q "DATABASE IS READY TO USE"; do sleep 5; done
docker exec -i oracle-ww sqlplus -s system/test@//localhost/FREEPDB1 <<'SQL'
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD DY';
SELECT d, TRUNC(CAST(d AS DATE), 'WW') AS old_week, TRUNC(CAST(d AS DATE), 'IW') AS new_week
FROM (
  SELECT DATE '2025-12-30' d FROM dual UNION ALL
  SELECT DATE '2025-12-31'   FROM dual UNION ALL
  SELECT DATE '2026-01-05'   FROM dual
);
SQL
docker rm -f oracle-ww

new_week should always be a Monday. In the UI, a time-series chart on an Oracle dataset with Time grain = Week should show Monday-aligned buckets and no stub week at year end.

ADDITIONAL INFORMATION

Notes for reviewers

  • Oracle behaviour was verified on a live Oracle 23ai Free instance (output above). Impala's 'D' is verified against the Impala source; it has not been run on a live Impala instance.
  • Existing Oracle/Impala charts using the Week grain will regroup into Monday-start weeks. Happy to add an UPDATING.md entry if reviewers consider this a breaking change.

@codecov

codecov Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.73%. Comparing base (8262ab9) to head (6a46056).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #45858      +/-   ##
==========================================
- Coverage   84.73%   84.73%   -0.01%     
==========================================
  Files        3069     3069              
  Lines      205781   205792      +11     
  Branches    48391    48393       +2     
==========================================
+ Hits       174368   174377       +9     
- Misses      28121    28122       +1     
- Partials     3292     3293       +1     
Flag Coverage Δ
hive 34.50% <ø> (-0.01%) ⬇️
mysql 52.60% <ø> (-0.01%) ⬇️
postgres 52.60% <ø> (-0.01%) ⬇️
presto 36.23% <ø> (-0.01%) ⬇️
python 87.76% <ø> (-0.01%) ⬇️
sqlite 52.37% <ø> (-0.01%) ⬇️
unit 82.52% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@rebenitez1802 rebenitez1802 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the fix! I checked the Impala side against a live Impala 4.5.2: TRUNC(x, 'D') returns the same-or-previous Monday for every date I tried (a 4,000-day sweep). It also works on TIMESTAMP, DATE, STRING and NULL inputs, and 'D' is as old in Impala as 'WW', so older versions are covered too. On Oracle, 'IW' is the ISO Monday and doesn't depend on NLS_TERRITORY. The only subclass is AscendEngineSpec, and it overrides the grains, so it isn't affected. Bonus: time comparison already keys weeks Monday-first (%Y-W%W), so this also fixes misaligned week-over-week joins.

One non-blocking note inline about UPDATING.md. Also, the PR title got truncated ("…Impa…"); it would be worth restoring the full title before merge.

TimeGrain.HOUR: "TRUNC(CAST({col} as DATE), 'HH')",
TimeGrain.DAY: "TRUNC(CAST({col} as DATE), 'DDD')",
TimeGrain.WEEK: "TRUNC(CAST({col} as DATE), 'WW')",
TimeGrain.WEEK: "TRUNC(CAST({col} as DATE), 'IW')",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium: add an UPDATING.md note for the Week regrouping (non-blocking)

This changes how existing Oracle and Impala Week-grain charts are bucketed. Week starts can move by up to 6 days, the 1–2 day stub at year end disappears, and per-bucket totals change. The query cache key doesn't include the generated SQL, so results cached under 'WW' keep being served until their TTL expires. The Db2 week-grain change (#43236) documented the same kind of shift in UPDATING.md.

UPDATING.md isn't part of this diff, so GitHub can't offer an applyable suggestion there. Here is an entry for the ## Next section:

- The `Week` time grain for Oracle (`TRUNC(..., 'IW')`) and Impala
  (`TRUNC(..., 'D')`) now always truncates to Monday. Previously `'WW'`
  aligned weeks to the weekday of January 1, so the week start changed every
  year and each year ended with a 1–2 day stub week. Existing Week-grain charts
  on these engines will regroup, and cached results may show the old buckets
  until they expire.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the thorough review, and for verifying 'D' on a live Impala!

Added your entry to the ## Next section of UPDATING.md in cd1ed69, and merged the latest master to resolve the conflict with the other new entries there. I also restored the full PR title.

@dhruv-techdev dhruv-techdev changed the title fix(db_engine_specs): use fixed Monday week start for Oracle and Impa… fix(db_engine_specs): use fixed Monday week start for Oracle and Impala Week grain Oct 30, 2026
@pull-request-size pull-request-size Bot added size/M and removed size/S labels Oct 30, 2026
@netlify

netlify Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit 6a46056
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6ae513f0030fbcdfe1f2e599
😎 Deploy Preview https://deploy-preview-45858--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@codeant-ai-for-open-source

Copy link
Copy Markdown
Contributor

CodeAnt  CodeAnt PR Risk

🟢  Low Risk

  • The PR appears safe to merge; no actionable issues were identified in the review.

Assessed commit: 6a46056ccebd

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Oracle and Impala "Week" time grain uses TRUNC(..., 'WW'): week start shifts every year and each year ends with a 1–2 day stub week

2 participants