Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-6581] Incorrect INTERVAL math for WEEK and QUARTER #3968

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions core/src/main/java/org/apache/calcite/sql/SqlIntervalQualifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ private static BigDecimal normalizeSecondFraction(String secondFracStr) {
return new BigDecimal("0." + secondFracStr).multiply(THOUSAND);
}

private static int[] fillIntervalValueArray(
private static int[] fillYearMonthIntervalValueArray(
int sign,
BigDecimal year,
BigDecimal month) {
Expand All @@ -588,7 +588,7 @@ private static int[] fillIntervalValueArray(
return ret;
}

private static int[] fillIntervalValueArray(
private static int[] fillDayTimeIntervalValueArray(
int sign,
BigDecimal day,
BigDecimal hour,
Expand Down Expand Up @@ -636,7 +636,7 @@ private int[] evaluateIntervalLiteralAsYear(
checkLeadFieldInRange(typeSystem, sign, year, TimeUnit.YEAR, pos);

// package values up for return
return fillIntervalValueArray(sign, year, ZERO);
return fillYearMonthIntervalValueArray(sign, year, ZERO);
} else {
throw invalidValueException(pos, originalValue);
}
Expand Down Expand Up @@ -676,7 +676,7 @@ private int[] evaluateIntervalLiteralAsYearToMonth(
}

// package values up for return
return fillIntervalValueArray(sign, year, month);
return fillYearMonthIntervalValueArray(sign, year, month);
} else {
throw invalidValueException(pos, originalValue);
}
Expand Down Expand Up @@ -711,7 +711,7 @@ private int[] evaluateIntervalLiteralAsMonth(
checkLeadFieldInRange(typeSystem, sign, month, TimeUnit.MONTH, pos);

// package values up for return
return fillIntervalValueArray(sign, ZERO, month);
return fillYearMonthIntervalValueArray(sign, ZERO, month);
} else {
throw invalidValueException(pos, originalValue);
}
Expand Down Expand Up @@ -746,7 +746,8 @@ private int[] evaluateIntervalLiteralAsQuarter(
checkLeadFieldInRange(typeSystem, sign, quarter, TimeUnit.QUARTER, pos);

// package values up for return
return fillIntervalValueArray(sign, ZERO, quarter);
final BigDecimal months = quarter.multiply(BigDecimal.valueOf(3));
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think quarters should be convertible to seconds, they do not have a fixed length

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are being converted to 3 months here, since QUARTER is treated as a kind of YEAR-MONTH interval.

Copy link
Contributor

Choose a reason for hiding this comment

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

sorry, I misread the JIRA issue

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, ok!

return fillYearMonthIntervalValueArray(sign, ZERO, months);
} else {
throw invalidValueException(pos, originalValue);
}
Expand Down Expand Up @@ -781,7 +782,8 @@ private int[] evaluateIntervalLiteralAsWeek(
checkLeadFieldInRange(typeSystem, sign, week, TimeUnit.WEEK, pos);

// package values up for return
return fillIntervalValueArray(sign, ZERO, week);
final BigDecimal days = week.multiply(BigDecimal.valueOf(7));
return fillDayTimeIntervalValueArray(sign, days, ZERO, ZERO, ZERO, ZERO);
} else {
throw invalidValueException(pos, originalValue);
}
Expand Down Expand Up @@ -816,7 +818,7 @@ private int[] evaluateIntervalLiteralAsDay(
checkLeadFieldInRange(typeSystem, sign, day, TimeUnit.DAY, pos);

// package values up for return
return fillIntervalValueArray(sign, day, ZERO, ZERO, ZERO, ZERO);
return fillDayTimeIntervalValueArray(sign, day, ZERO, ZERO, ZERO, ZERO);
} else {
throw invalidValueException(pos, originalValue);
}
Expand Down Expand Up @@ -856,7 +858,7 @@ private int[] evaluateIntervalLiteralAsDayToHour(
}

// package values up for return
return fillIntervalValueArray(sign, day, hour, ZERO, ZERO, ZERO);
return fillDayTimeIntervalValueArray(sign, day, hour, ZERO, ZERO, ZERO);
} else {
throw invalidValueException(pos, originalValue);
}
Expand Down Expand Up @@ -899,7 +901,7 @@ private int[] evaluateIntervalLiteralAsDayToMinute(
}

// package values up for return
return fillIntervalValueArray(sign, day, hour, minute, ZERO, ZERO);
return fillDayTimeIntervalValueArray(sign, day, hour, minute, ZERO, ZERO);
} else {
throw invalidValueException(pos, originalValue);
}
Expand Down Expand Up @@ -969,7 +971,7 @@ private int[] evaluateIntervalLiteralAsDayToSecond(
}

// package values up for return
return fillIntervalValueArray(
return fillDayTimeIntervalValueArray(
sign,
day,
hour,
Expand Down Expand Up @@ -1010,7 +1012,7 @@ private int[] evaluateIntervalLiteralAsHour(
checkLeadFieldInRange(typeSystem, sign, hour, TimeUnit.HOUR, pos);

// package values up for return
return fillIntervalValueArray(sign, ZERO, hour, ZERO, ZERO, ZERO);
return fillDayTimeIntervalValueArray(sign, ZERO, hour, ZERO, ZERO, ZERO);
} else {
throw invalidValueException(pos, originalValue);
}
Expand Down Expand Up @@ -1051,7 +1053,7 @@ private int[] evaluateIntervalLiteralAsHourToMinute(
}

// package values up for return
return fillIntervalValueArray(sign, ZERO, hour, minute, ZERO, ZERO);
return fillDayTimeIntervalValueArray(sign, ZERO, hour, minute, ZERO, ZERO);
} else {
throw invalidValueException(pos, originalValue);
}
Expand Down Expand Up @@ -1119,7 +1121,7 @@ private int[] evaluateIntervalLiteralAsHourToSecond(
}

// package values up for return
return fillIntervalValueArray(
return fillDayTimeIntervalValueArray(
sign,
ZERO,
hour,
Expand Down Expand Up @@ -1160,7 +1162,7 @@ private int[] evaluateIntervalLiteralAsMinute(
checkLeadFieldInRange(typeSystem, sign, minute, TimeUnit.MINUTE, pos);

// package values up for return
return fillIntervalValueArray(sign, ZERO, ZERO, minute, ZERO, ZERO);
return fillDayTimeIntervalValueArray(sign, ZERO, ZERO, minute, ZERO, ZERO);
} else {
throw invalidValueException(pos, originalValue);
}
Expand Down Expand Up @@ -1224,7 +1226,7 @@ private int[] evaluateIntervalLiteralAsMinuteToSecond(
}

// package values up for return
return fillIntervalValueArray(
return fillDayTimeIntervalValueArray(
sign,
ZERO,
ZERO,
Expand Down Expand Up @@ -1291,7 +1293,7 @@ private int[] evaluateIntervalLiteralAsSecond(
}

// package values up for return
return fillIntervalValueArray(
return fillDayTimeIntervalValueArray(
sign, ZERO, ZERO, ZERO, second, secondFrac);
} else {
throw invalidValueException(pos, originalValue);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.calcite.sql.parser;

import org.apache.calcite.avatica.util.TimeUnit;
import org.apache.calcite.sql.SqlIntervalQualifier;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* Tests {@link SqlParserUtil}. Currently, this test focuses on tests for the methods that work
* with {@link SqlIntervalQualifier}. It may be expanded to test other functionality in the future.
*/
public class SqlParserUtilTest {
Copy link
Member

Choose a reason for hiding this comment

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

SqlParserIntervalQualifierTest?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I figured that at some point, this test file might be expanded to include more tests for SqlParserUtil, so I named it after the main class itself. However, please let me know if it's considered better to scope the test files more narrowly.

Copy link
Member

Choose a reason for hiding this comment

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

It seems that the impact is not great.
I think we can add JavaDoc to let subsequent contributors know what this test file is for.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added some additional javadoc.

private static final SqlParserPos POSITION = SqlParserPos.ZERO;

@Test void testSecondIntervalToMillis() {
final SqlIntervalQualifier qualifier =
new SqlIntervalQualifier(TimeUnit.SECOND, null, POSITION);
assertThat(SqlParserUtil.intervalToMillis("2.1", qualifier), equalTo(2_100L));
}

@Test void testMinuteIntervalToMillis() {
final SqlIntervalQualifier qualifier =
new SqlIntervalQualifier(TimeUnit.MINUTE, null, POSITION);
assertThat(SqlParserUtil.intervalToMillis("2", qualifier), equalTo(120_000L));
}

@Test void testMinuteToSecondIntervalToMillis() {
final SqlIntervalQualifier qualifier =
new SqlIntervalQualifier(TimeUnit.MINUTE, TimeUnit.SECOND, POSITION);
assertThat(SqlParserUtil.intervalToMillis("2:30", qualifier), equalTo(150_000L));
}

@Test void testHourIntervalToMillis() {
final SqlIntervalQualifier qualifier =
new SqlIntervalQualifier(TimeUnit.HOUR, null, POSITION);
assertThat(SqlParserUtil.intervalToMillis("2", qualifier), equalTo(7_200_000L));
}

@Test void testHourToMinuteIntervalToMillis() {
final SqlIntervalQualifier qualifier =
new SqlIntervalQualifier(TimeUnit.HOUR, TimeUnit.MINUTE, POSITION);
assertThat(SqlParserUtil.intervalToMillis("2:03", qualifier), equalTo(7_380_000L));
}

@Test void testHourToSecondIntervalToMillis() {
final SqlIntervalQualifier qualifier =
new SqlIntervalQualifier(TimeUnit.HOUR, TimeUnit.SECOND, POSITION);
assertThat(SqlParserUtil.intervalToMillis("2:03:30", qualifier), equalTo(7_410_000L));
}

@Test void testDayIntervalToMillis() {
final SqlIntervalQualifier qualifier =
new SqlIntervalQualifier(TimeUnit.DAY, null, POSITION);
assertThat(SqlParserUtil.intervalToMillis("2", qualifier), equalTo(172_800_000L));
}

@Test void testDayToHourIntervalToMillis() {
final SqlIntervalQualifier qualifier =
new SqlIntervalQualifier(TimeUnit.DAY, TimeUnit.HOUR, POSITION);
assertThat(SqlParserUtil.intervalToMillis("2 1", qualifier), equalTo(176_400_000L));
}

@Test void testDayToMinuteIntervalToMillis() {
final SqlIntervalQualifier qualifier =
new SqlIntervalQualifier(TimeUnit.DAY, TimeUnit.MINUTE, POSITION);
assertThat(SqlParserUtil.intervalToMillis("2 1:03", qualifier), equalTo(176_580_000L));
}

@Test void testDayToSecondIntervalToMillis() {
final SqlIntervalQualifier qualifier =
new SqlIntervalQualifier(TimeUnit.DAY, TimeUnit.SECOND, POSITION);
assertThat(SqlParserUtil.intervalToMillis("2 1:02:30", qualifier), equalTo(176_550_000L));
}

@Test void testWeekIntervalToMillis() {
final SqlIntervalQualifier qualifier =
new SqlIntervalQualifier(TimeUnit.WEEK, null, POSITION);
assertThat(SqlParserUtil.intervalToMillis("2", qualifier), equalTo(1_209_600_000L));
}

@Test void testMonthIntervalToMonths() {
final SqlIntervalQualifier qualifier =
new SqlIntervalQualifier(TimeUnit.WEEK, null, POSITION);
assertThat(SqlParserUtil.intervalToMillis("2", qualifier), equalTo(1_209_600_000L));
}

@Test void testQuarterIntervalToMonths() {
final SqlIntervalQualifier qualifier =
new SqlIntervalQualifier(TimeUnit.QUARTER, null, POSITION);
assertThat(SqlParserUtil.intervalToMonths("2", qualifier), equalTo(6L));
}

@Test void testYearIntervalToMonths() {
final SqlIntervalQualifier qualifier =
new SqlIntervalQualifier(TimeUnit.YEAR, null, POSITION);
assertThat(SqlParserUtil.intervalToMonths("2", qualifier), equalTo(24L));
}

@Test void testYearToMonthIntervalToMonths() {
final SqlIntervalQualifier qualifier =
new SqlIntervalQualifier(TimeUnit.YEAR, TimeUnit.MONTH, POSITION);
assertThat(SqlParserUtil.intervalToMonths("2-3", qualifier), equalTo(27L));
}
}
12 changes: 12 additions & 0 deletions core/src/test/resources/sql/misc.iq
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,18 @@ order by empno;

!ok

# [CALCITE-6581] INTERVAL with WEEK and QUARTER
select timestamp '1970-01-01' + interval '2' week as w,
timestamp '1970-01-01 00:00:00' + interval '2' quarter as q;
+---------------------+---------------------+
| W | Q |
+---------------------+---------------------+
| 1970-01-15 00:00:00 | 1970-07-01 00:00:00 |
+---------------------+---------------------+
(1 row)

!ok

# [CALCITE-1486] Invalid "Invalid literal" error for complex expression
select 8388608/(60+27.39);
+-------------------+
Expand Down
Loading