Skip to content

fix: correct WEEK interval literal duration in calciteLiteralToDruidLiteral (#18665) - #19906

Open
waterWang wants to merge 1 commit into
apache:masterfrom
waterWang:fix/week-interval-duration
Open

fix: correct WEEK interval literal duration in calciteLiteralToDruidLiteral (#18665)#19906
waterWang wants to merge 1 commit into
apache:masterfrom
waterWang:fix/week-interval-duration

Conversation

@waterWang

Copy link
Copy Markdown

Description

Fixes #18665

INTERVAL 1 WEEK in SQL resolves to PT1H (1 hour) instead of P7D (7 days).

SELECT MILLIS_TO_TIMESTAMP(0) + INTERVAL 1 WEEK

Returns 1970-01-01T01:00:00.000Z instead of 1970-01-08T00:00:00.000Z.

Root Cause

Calcite has a known quirk where WEEK interval literals are stored as 1 hour in the INTERVAL_DAY_TIME family. The INTERVAL 1 WEEK literal's RexLiteral.value() returns 3600000 (1 hour in ms) instead of 604800000 (7 days in ms).

Fix

In calciteLiteralToDruidLiteral, detect the WEEK qualifier on the SqlIntervalQualifier and multiply the stored millisecond value by 7 × 24 to convert from the incorrect 1-hour representation to the correct 7-day duration.

Impact

All INTERVAL N WEEK expressions now correctly resolve to N weeks instead of N hours.
The fix applies to all contexts where INTERVAL_DAY_TIME literals are converted to Druid expressions: timestamp arithmetic (+/-), TIMESTAMPDIFF, and other interval-using operations.

Verification

SELECT MILLIS_TO_TIMESTAMP(0) + INTERVAL 1 WEEK
-- Before: 1970-01-01T01:00:00.000Z
-- After:  1970-01-08T00:00:00.000Z

…iteral (apache#18665)

Calcite has a known quirk where WEEK interval literals are stored as 1 hour
in the INTERVAL_DAY_TIME family. This causes `INTERVAL 1 WEEK` to resolve
to PT1H (1 hour) instead of P7D (7 days).

Detect the WEEK qualifier in the SqlIntervalQualifier and convert the
millisecond value to 7 days (multiply by 7 * 24).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

INTERVAL 1 WEEK in SQL is wrong interval

1 participant