Skip to content

Commit

Permalink
[CALCITE-6115] Interval type specifier with zero fractional second pr…
Browse files Browse the repository at this point in the history
…ecision does not pass validation
  • Loading branch information
LeonidChistov authored and asolimando committed Dec 14, 2023
1 parent f449b0f commit e4bd21e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ private int[] evaluateIntervalLiteralAsDayToSecond(
final int fractionalSecondPrecision =
getFractionalSecondPrecision(typeSystem);
String intervalPatternWithFracSec =
"(\\d+) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})\\.(\\d{1,"
"(\\d+) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})\\.(\\d{0,"
+ fractionalSecondPrecision + "})";
String intervalPatternWithoutFracSec =
"(\\d+) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})";
Expand Down Expand Up @@ -1034,7 +1034,7 @@ private int[] evaluateIntervalLiteralAsHourToSecond(
final int fractionalSecondPrecision =
getFractionalSecondPrecision(typeSystem);
String intervalPatternWithFracSec =
"(\\d+):(\\d{1,2}):(\\d{1,2})\\.(\\d{1,"
"(\\d+):(\\d{1,2}):(\\d{1,2})\\.(\\d{0,"
+ fractionalSecondPrecision + "})";
String intervalPatternWithoutFracSec =
"(\\d+):(\\d{1,2}):(\\d{1,2})";
Expand Down Expand Up @@ -1142,7 +1142,7 @@ private int[] evaluateIntervalLiteralAsMinuteToSecond(
final int fractionalSecondPrecision =
getFractionalSecondPrecision(typeSystem);
String intervalPatternWithFracSec =
"(\\d+):(\\d{1,2})\\.(\\d{1," + fractionalSecondPrecision + "})";
"(\\d+):(\\d{1,2})\\.(\\d{0," + fractionalSecondPrecision + "})";
String intervalPatternWithoutFracSec =
"(\\d+):(\\d{1,2})";

Expand Down Expand Up @@ -1211,7 +1211,7 @@ private int[] evaluateIntervalLiteralAsSecond(
final int fractionalSecondPrecision =
getFractionalSecondPrecision(typeSystem);
String intervalPatternWithFracSec =
"(\\d+)\\.(\\d{1," + fractionalSecondPrecision + "})";
"(\\d+)\\.(\\d{0," + fractionalSecondPrecision + "})";
String intervalPatternWithoutFracSec =
"(\\d+)";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public enum SqlTypeName {
public static final int DEFAULT_INTERVAL_START_PRECISION = 2;
public static final int DEFAULT_INTERVAL_FRACTIONAL_SECOND_PRECISION = 6;
public static final int MIN_INTERVAL_START_PRECISION = 1;
public static final int MIN_INTERVAL_FRACTIONAL_SECOND_PRECISION = 1;
public static final int MIN_INTERVAL_FRACTIONAL_SECOND_PRECISION = 0;
public static final int MAX_INTERVAL_START_PRECISION = 10;
public static final int MAX_INTERVAL_FRACTIONAL_SECOND_PRECISION = 9;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ public static void checkActualAndReferenceFiles() {
sql("select interval mgr hour as h from emp").ok();
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6115">[CALCITE-6115]
* Interval type specifier with zero fractional second precision does not pass validation</a>.
*/
@Test void testIntervalSecondNoFractionalPart() {
sql("select interval '1' second(1,0) as h from emp").ok();
}

@Test void testAliasList() {
final String sql = "select a + b from (\n"
+ " select deptno, 1 as uno, name from dept\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2277,7 +2277,7 @@ void testLikeAndSimilarFails() {
assertThat(typeName.getMinPrecision(), is(1));
assertThat(typeSystem.getMaxPrecision(typeName), is(10));
assertThat(typeSystem.getDefaultPrecision(typeName), is(2));
assertThat(typeName.getMinScale(), is(1));
assertThat(typeName.getMinScale(), is(0));
assertThat(typeSystem.getMaxScale(typeName), is(9));
assertThat(typeName.getDefaultScale(), is(6));
}
Expand Down Expand Up @@ -2335,6 +2335,8 @@ private IntervalTest.Fixture2 getFixture2(SqlValidatorFixture f2) {
.columnType("INTERVAL HOUR NOT NULL");
sql("select interval emp.mgr hour as h from emp")
.columnType("INTERVAL HOUR");
expr("interval '1' second(1, 0)")
.columnType("INTERVAL SECOND(1, 0) NOT NULL");
}

@Test void testIntervalOperators() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3312,6 +3312,17 @@ from emp]]>
<![CDATA[
LogicalProject(EXPR$0=[*($0, 13:INTERVAL YEAR TO MONTH)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testIntervalSecondNoFractionalPart">
<Resource name="sql">
<![CDATA[select interval '1' second(1,0) as h from emp]]>
</Resource>
<Resource name="plan">
<![CDATA[
LogicalProject(H=[1000:INTERVAL SECOND(1, 0)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
Expand Down
12 changes: 0 additions & 12 deletions testkit/src/main/java/org/apache/calcite/test/IntervalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1477,9 +1477,6 @@ public void subTestIntervalDayToSecondNegative() {
f.expr("INTERVAL '0 0:0:0' ^DAY(0) TO SECOND^")
.fails("Interval leading field precision '0' out of range for "
+ "INTERVAL DAY\\(0\\) TO SECOND");
f.expr("INTERVAL '0 0:0:0' ^DAY TO SECOND(0)^")
.fails("Interval fractional second precision '0' out of range for "
+ "INTERVAL DAY TO SECOND\\(0\\)");
}

/**
Expand Down Expand Up @@ -1706,9 +1703,6 @@ public void subTestIntervalHourToSecondNegative() {
f.expr("INTERVAL '0:0:0' ^HOUR(0) TO SECOND^")
.fails("Interval leading field precision '0' out of range for "
+ "INTERVAL HOUR\\(0\\) TO SECOND");
f.expr("INTERVAL '0:0:0' ^HOUR TO SECOND(0)^")
.fails("Interval fractional second precision '0' out of range for "
+ "INTERVAL HOUR TO SECOND\\(0\\)");
}

/**
Expand Down Expand Up @@ -1850,9 +1844,6 @@ public void subTestIntervalMinuteToSecondNegative() {
f.expr("INTERVAL '0:0' ^MINUTE(0) TO SECOND^")
.fails("Interval leading field precision '0' out of range for"
+ " INTERVAL MINUTE\\(0\\) TO SECOND");
f.expr("INTERVAL '0:0' ^MINUTE TO SECOND(0)^")
.fails("Interval fractional second precision '0' out of range for"
+ " INTERVAL MINUTE TO SECOND\\(0\\)");
}

/**
Expand Down Expand Up @@ -1927,9 +1918,6 @@ public void subTestIntervalSecondNegative() {
f.expr("INTERVAL '0' ^SECOND(0)^")
.fails("Interval leading field precision '0' out of range for"
+ " INTERVAL SECOND\\(0\\)");
f.expr("INTERVAL '0' ^SECOND(1, 0)^")
.fails("Interval fractional second precision '0' out of range for"
+ " INTERVAL SECOND\\(1, 0\\)");
}

public void subTestMisc() {
Expand Down

0 comments on commit e4bd21e

Please sign in to comment.