[ 
https://issues.apache.org/jira/browse/CALCITE-3312?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17706098#comment-17706098
 ] 

TJ Banghart edited comment on CALCITE-3312 at 3/29/23 4:03 AM:
---------------------------------------------------------------

The problem begins in {{StandardConvertletTable.java}} when we convert a 
{{TIMESTAMPDIFF}} into [datetime 
arithmetic|https://github.com/apache/calcite/blob/5c7be55ffee836366dcc7fefb6adfc0b8c47465f/core/src/main/java/org/apache/calcite/sql2rel/StandardConvertletTable.java#L2131-L2140]
 using {{MINUS_DATE}} followed by {{rexBuilder.makeCast}} and 
{{{}rexBuilder.multiplyDivide{}}}.

If we delegate {{{}REINTERPRET{}}}'s unparse method to its single operand we 
end up with some no-good expression that attempts to cast an interval to some 
other type (in this case INT):

{{SELECT CAST(((TIME '15:30:00' - TIME '14:35:00')) /INT 60000 AS INT) FROM 
foodmart.product}}

Perhaps we need to have a config option to prevent this type of conversion. Is 
there anyway to override Calcite's default sql2rel conversions?

We could also recreate the original {{TIMESTAMPDIFF}} using the target 
dialect's version of datetime subtraction but that may also require conversion 
of {{TIMESTAMPDIFF}} calls to a standard form so we could easily identify 
datetime subtraction (vs a series of {{{}CAST{}}}, {{REINTERPRET}} and 
{{MINUS_DATE}} operators).


was (Author: tjbanghart):
The problem begins in {{StandardConvertletTable.java}} when we convert a 
{{TIMESTAMPDIFF}} into [datetime 
arithmetic|https://github.com/apache/calcite/blob/5c7be55ffee836366dcc7fefb6adfc0b8c47465f/core/src/main/java/org/apache/calcite/sql2rel/StandardConvertletTable.java#L2131-L2140]
 using {{MINUS_DATE}} followed by {{rexBuilder.makeCast}} and 
{{{}rexBuilder.multiplyDivide{}}}.

If we delegate {{{}REINTERPRET{}}}'s unparse method to its single operand we 
end up with some no-good expression that attempts to cast an interval to some 
other type (in this case INT):

{{SELECT CAST(((TIME '15:30:00' - TIME '14:35:00')) /INT 60000 AS INT) FROM 
foodmart.product}}

Perhaps we need to have a config option to prevent this type of conversion. Is 
there anyway to override Calcite's default sql2rel conversions? 

We could also recreate the original {{TIMESTAMPDIFF}} using the target 
dialect's version of datetime subtraction but that may also prevent conversion 
of {{TIMESTAMPDIFF}} calls so we could easily identify datetime subtraction (vs 
a series of {{{}CAST{}}}, {{REINTERPRET}} and {{MINUS_DATE}} operators).

> TIMESTAMPDIFF cannot be converted to SQL
> ----------------------------------------
>
>                 Key: CALCITE-3312
>                 URL: https://issues.apache.org/jira/browse/CALCITE-3312
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>            Reporter: Pavel Gubin
>            Assignee: TJ Banghart
>            Priority: Major
>
> The following test in {{RelToSqlConverterTest}}:
> {code}
>   @Test public void testTimestampDiff() {
>     String query = "SELECT {fn TIMESTAMPDIFF(SQL_TSI_YEAR,TIMESTAMP 
> '2016-01-01 00:00:00', TIMESTAMP '2017-01-01 00:00:00')}";
>     String expected = "";
>     sql(query).ok(expected);
>   }
> {code}
> fails with:
> {noformat}
> java.lang.UnsupportedOperationException: class 
> org.apache.calcite.sql.SqlSyntax$6: SPECIAL
>       at org.apache.calcite.util.Util.needToImplement(Util.java:967)
>       at org.apache.calcite.sql.SqlSyntax$6.unparse(SqlSyntax.java:116)
>       at org.apache.calcite.sql.SqlOperator.unparse(SqlOperator.java:348)
>       at org.apache.calcite.sql.SqlDialect.unparseCall(SqlDialect.java:402)
>       at org.apache.calcite.sql.SqlCall.unparse(SqlCall.java:105)
>       at org.apache.calcite.sql.SqlUtil.unparseBinarySyntax(SqlUtil.java:382)
>       at org.apache.calcite.sql.SqlSyntax$3.unparse(SqlSyntax.java:65)
>       at org.apache.calcite.sql.SqlOperator.unparse(SqlOperator.java:348)
>       at org.apache.calcite.sql.SqlDialect.unparseCall(SqlDialect.java:402)
>       at org.apache.calcite.sql.SqlCall.unparse(SqlCall.java:108)
>       at 
> org.apache.calcite.sql.fun.SqlCastFunction.unparse(SqlCastFunction.java:174)
>       at org.apache.calcite.sql.SqlDialect.unparseCall(SqlDialect.java:402)
>       at org.apache.calcite.sql.SqlCall.unparse(SqlCall.java:108)
>       at org.apache.calcite.sql.SqlNodeList.commaList(SqlNodeList.java:121)
>       at 
> org.apache.calcite.sql.SqlOperator.unparseListClause(SqlOperator.java:365)
>       at 
> org.apache.calcite.sql.SqlOperator.unparseListClause(SqlOperator.java:354)
>       at 
> org.apache.calcite.sql.SqlSelectOperator.unparse(SqlSelectOperator.java:152)
>       at org.apache.calcite.sql.SqlDialect.unparseCall(SqlDialect.java:402)
>       at org.apache.calcite.sql.SqlSelect.unparse(SqlSelect.java:230)
>       at org.apache.calcite.sql.SqlNode.toSqlString(SqlNode.java:152)
>       at org.apache.calcite.sql.SqlNode.toSqlString(SqlNode.java:157)
>       at 
> org.apache.calcite.rel.rel2sql.RelToSqlConverterTest.toSql(RelToSqlConverterTest.java:145)
>       at 
> org.apache.calcite.rel.rel2sql.RelToSqlConverterTest.access$100(RelToSqlConverterTest.java:77)
>       at 
> org.apache.calcite.rel.rel2sql.RelToSqlConverterTest$Sql.exec(RelToSqlConverterTest.java:3634)
>       at 
> org.apache.calcite.rel.rel2sql.RelToSqlConverterTest$Sql.ok(RelToSqlConverterTest.java:3609)
>       at 
> org.apache.calcite.rel.rel2sql.RelToSqlConverterTest.testTimestampDiff(RelToSqlConverterTest.java:1942)
>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>       at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>       at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>       at java.lang.reflect.Method.invoke(Method.java:498)
>       at 
> org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
>       at 
> org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
>       at 
> org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
>       at 
> org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
>       at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
>       at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
>       at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
>       at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
>       at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
>       at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
>       at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
>       at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
>       at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
>       at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
>       at 
> com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
>       at 
> com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
>       at 
> com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
>       at 
> com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
> {noformat}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to