[CALCITE-922] Extract value of an INTERVAL literal (Hsuan-Yi Chu) Add test case (Julian Hyde)
Close apache/incubator-calcite#155 Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/c5f2599f Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/c5f2599f Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/c5f2599f Branch: refs/heads/branch-release Commit: c5f2599f493ecdd12494e6cb0bb36ee0f09fe857 Parents: 9d0fef3 Author: Hsuan-Yi Chu <[email protected]> Authored: Thu Oct 15 23:50:35 2015 -0700 Committer: Julian Hyde <[email protected]> Committed: Fri Oct 23 16:44:05 2015 -0700 ---------------------------------------------------------------------- .../java/org/apache/calcite/sql/SqlLiteral.java | 15 +++++++++++ .../sql2rel/SqlNodeToRexConverterImpl.java | 15 +++-------- .../calcite/test/SqlToRelConverterTest.java | 10 ++++++++ .../calcite/test/SqlToRelConverterTest.xml | 26 ++++++++++++++++++++ core/src/test/resources/sql/misc.oq | 19 ++++++++++++++ 5 files changed, 74 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/c5f2599f/core/src/main/java/org/apache/calcite/sql/SqlLiteral.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/sql/SqlLiteral.java b/core/src/main/java/org/apache/calcite/sql/SqlLiteral.java index b7aa493..3295fb5 100644 --- a/core/src/main/java/org/apache/calcite/sql/SqlLiteral.java +++ b/core/src/main/java/org/apache/calcite/sql/SqlLiteral.java @@ -23,6 +23,7 @@ import org.apache.calcite.sql.fun.SqlLiteralChainOperator; import org.apache.calcite.sql.fun.SqlStdOperatorTable; import org.apache.calcite.sql.parser.SqlParserPos; import org.apache.calcite.sql.parser.SqlParserUtil; +import org.apache.calcite.sql.type.SqlTypeFamily; import org.apache.calcite.sql.type.SqlTypeName; import org.apache.calcite.sql.type.SqlTypeUtil; import org.apache.calcite.sql.util.SqlVisitor; @@ -273,6 +274,12 @@ public class SqlLiteral extends SqlNode { * <li>If the node is a {@link SqlIntervalQualifier}, * returns its {@link TimeUnitRange}. * + * <li>If the node is INTERVAL_DAY_TIME in {@link SqlTypeFamily}, + * returns its sign multiplied by its millisecond equivalent value + * + * <li>If the node is INTERVAL_YEAR_MONTH in {@link SqlTypeFamily}, + * returns its sign multiplied by its months equivalent value + * * <li>Otherwise the behavior is not specified. * </ul> */ @@ -284,6 +291,14 @@ public class SqlLiteral extends SqlNode { return (NlsString) literal.value; case NUMERIC: return (BigDecimal) literal.value; + case INTERVAL_YEAR_MONTH: + final SqlIntervalLiteral.IntervalValue valMonth = + (SqlIntervalLiteral.IntervalValue) literal.value; + return valMonth.getSign() * SqlParserUtil.intervalToMonths(valMonth); + case INTERVAL_DAY_TIME: + final SqlIntervalLiteral.IntervalValue valTime = + (SqlIntervalLiteral.IntervalValue) literal.value; + return valTime.getSign() * SqlParserUtil.intervalToMillis(valTime); } } if (SqlUtil.isLiteralChain(node)) { http://git-wip-us.apache.org/repos/asf/calcite/blob/c5f2599f/core/src/main/java/org/apache/calcite/sql2rel/SqlNodeToRexConverterImpl.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/sql2rel/SqlNodeToRexConverterImpl.java b/core/src/main/java/org/apache/calcite/sql2rel/SqlNodeToRexConverterImpl.java index 07e0fce..bf7fdd0 100644 --- a/core/src/main/java/org/apache/calcite/sql2rel/SqlNodeToRexConverterImpl.java +++ b/core/src/main/java/org/apache/calcite/sql2rel/SqlNodeToRexConverterImpl.java @@ -28,7 +28,6 @@ import org.apache.calcite.sql.SqlIntervalQualifier; import org.apache.calcite.sql.SqlLiteral; import org.apache.calcite.sql.SqlTimeLiteral; import org.apache.calcite.sql.SqlTimestampLiteral; -import org.apache.calcite.sql.parser.SqlParserUtil; import org.apache.calcite.sql.type.SqlTypeName; import org.apache.calcite.sql.validate.SqlValidator; import org.apache.calcite.util.BitString; @@ -140,19 +139,13 @@ public class SqlNodeToRexConverterImpl implements SqlNodeToRexConverter { return rexBuilder.makeDateLiteral((Calendar) value); case INTERVAL_YEAR_MONTH: - intervalValue = - (SqlIntervalLiteral.IntervalValue) value; - l = SqlParserUtil.intervalToMonths(intervalValue); - return rexBuilder.makeIntervalLiteral( - BigDecimal.valueOf(l), - intervalValue.getIntervalQualifier()); case INTERVAL_DAY_TIME: - intervalValue = - (SqlIntervalLiteral.IntervalValue) value; - l = SqlParserUtil.intervalToMillis(intervalValue); + SqlIntervalQualifier sqlIntervalQualifier = + ((SqlIntervalLiteral.IntervalValue) value).getIntervalQualifier(); + l = (long) SqlLiteral.value(literal); return rexBuilder.makeIntervalLiteral( BigDecimal.valueOf(l), - intervalValue.getIntervalQualifier()); + sqlIntervalQualifier); default: throw Util.unexpected(literal.getTypeName()); } http://git-wip-us.apache.org/repos/asf/calcite/blob/c5f2599f/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java index 1494a78..dbd8a63 100644 --- a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java +++ b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java @@ -59,6 +59,16 @@ public class SqlToRelConverterTest extends SqlToRelTestBase { check("select 1 from emp", "${plan}"); } + @Test public void testIntervalLiteralYearToMonth() { + check("select cast(empno as Integer) * (INTERVAL '1-1' YEAR TO MONTH)\n" + + "from emp", "${plan}"); + } + + @Test public void testIntervalLiteralHourToMinute() { + check("select cast(empno as Integer) * (INTERVAL '1:1' HOUR TO MINUTE)\n" + + "from emp", "${plan}"); + } + @Test public void testAliasList() { check( "select a + b from (\n" http://git-wip-us.apache.org/repos/asf/calcite/blob/c5f2599f/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml ---------------------------------------------------------------------- diff --git a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml index 5912ed6..e145812 100644 --- a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml +++ b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml @@ -478,6 +478,32 @@ LogicalProject(EXPR$0=[1]) ]]> </Resource> </TestCase> + <TestCase name="testIntervalLiteralYearToMonth"> + <Resource name="sql"> + <![CDATA[select cast(empno as Integer) * (INTERVAL '1-1' YEAR TO MONTH) +from emp +]]> + </Resource> + <Resource name="plan"> + <![CDATA[ +LogicalProject(EXPR$0=[*(CAST($0):INTEGER NOT NULL, 13)]) + LogicalTableScan(table=[[CATALOG, SALES, EMP]]) +]]> + </Resource> + </TestCase> + <TestCase name="testIntervalLiteralHourToMinute"> + <Resource name="sql"> + <![CDATA[select cast(empno as Integer) * (INTERVAL '1:1' HOUR TO MINUTE) +from emp +]]> + </Resource> + <Resource name="plan"> + <![CDATA[ +LogicalProject(EXPR$0=[*(CAST($0):INTEGER NOT NULL, 3660000)]) + LogicalTableScan(table=[[CATALOG, SALES, EMP]]) +]]> + </Resource> + </TestCase> <TestCase name="testSelectDistinct"> <Resource name="sql"> <![CDATA[select distinct sal + 5 from emp]]> http://git-wip-us.apache.org/repos/asf/calcite/blob/c5f2599f/core/src/test/resources/sql/misc.oq ---------------------------------------------------------------------- diff --git a/core/src/test/resources/sql/misc.oq b/core/src/test/resources/sql/misc.oq index bcc90b1..aa55654 100644 --- a/core/src/test/resources/sql/misc.oq +++ b/core/src/test/resources/sql/misc.oq @@ -1089,4 +1089,23 @@ group by deptno; !ok +# [CALCITE-922] Value of INTERVAL literal +select deptno * interval '2' day as d2, + deptno * interval -'3' hour as h3, + deptno * interval -'-4' hour as h4, + deptno * interval -'4:30' hour to minute as h4_5, + deptno * interval -'-1-3' year to month as y1_25 +from "scott".dept; ++-----+------+------+---------+--------+ +| D2 | H3 | H4 | H4_5 | Y1_25 | ++-----+------+------+---------+--------+ +| +20 | -30 | +40 | -45:00 | +12-06 | +| +40 | -60 | +80 | -90:00 | +25-00 | +| +60 | -90 | +120 | -135:00 | +37-06 | +| +80 | -120 | +160 | -180:00 | +50-00 | ++-----+------+------+---------+--------+ +(4 rows) + +!ok + # End misc.oq
