From eee8907740d97b5cfaa607285bca70e7c4750d5e Mon Sep 17 00:00:00 2001 From: "fanshu.kong" <1714585117@qq.com> Date: Tue, 20 Sep 2022 21:02:39 +0800 Subject: [PATCH] AL-6695 cast date to timestamp when date compare with timestamp --- cassandra/pom.xml | 4 +- core/pom.xml | 4 +- .../sql2rel/StandardConvertletTable.java | 42 ++++++++++++++++++- druid/pom.xml | 4 +- elasticsearch2/pom.xml | 4 +- elasticsearch5/pom.xml | 4 +- example/csv/pom.xml | 4 +- example/function/pom.xml | 4 +- example/pom.xml | 4 +- file/pom.xml | 4 +- geode/pom.xml | 4 +- linq4j/pom.xml | 4 +- mongodb/pom.xml | 4 +- pig/pom.xml | 4 +- piglet/pom.xml | 4 +- plus/pom.xml | 4 +- pom.xml | 2 +- server/pom.xml | 4 +- spark/pom.xml | 4 +- splunk/pom.xml | 4 +- ubenchmark/pom.xml | 2 +- 21 files changed, 78 insertions(+), 40 deletions(-) diff --git a/cassandra/pom.xml b/cassandra/pom.xml index 608a00830672..90f136bcac76 100644 --- a/cassandra/pom.xml +++ b/cassandra/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-cassandra jar - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite Cassandra Cassandra adapter for Calcite diff --git a/core/pom.xml b/core/pom.xml index f3824f66197e..319f89a7174c 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-core jar - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite Core Core Calcite APIs and engine. diff --git a/core/src/main/java/org/apache/calcite/sql2rel/StandardConvertletTable.java b/core/src/main/java/org/apache/calcite/sql2rel/StandardConvertletTable.java index 2ac3c920c34e..c50f03d4e88e 100644 --- a/core/src/main/java/org/apache/calcite/sql2rel/StandardConvertletTable.java +++ b/core/src/main/java/org/apache/calcite/sql2rel/StandardConvertletTable.java @@ -27,6 +27,7 @@ import org.apache.calcite.rex.RexCall; import org.apache.calcite.rex.RexCallBinding; import org.apache.calcite.rex.RexDynamicParam; +import org.apache.calcite.rex.RexInputRef; import org.apache.calcite.rex.RexLiteral; import org.apache.calcite.rex.RexNode; import org.apache.calcite.rex.RexRangeRef; @@ -859,7 +860,7 @@ public static List convertExpressionList(SqlRexContext cx, } if (exprs.size() > 1) { RelDataType type = - consistentType(cx, consistency, RexUtil.types(exprs)); + consistentType(cx, consistency, RexUtil.types(exprs), exprs); /* OVERRIDE POINT */ if (type == null) { @@ -907,9 +908,14 @@ private static RelDataType consistentType2(SqlRexContext cx, } private static RelDataType consistentType(SqlRexContext cx, - SqlOperandTypeChecker.Consistency consistency, List types) { + SqlOperandTypeChecker.Consistency consistency, List types, List exprs) { switch (consistency) { case COMPARE: + if (Boolean.parseBoolean( + System.getProperty("calcite.convert-date-to-timeStamp-enabled", "false")) + && isDateCompareWithTimeStamp(types, exprs)) { + return castDateToTimeStamp(types); + } final Set families = Sets.newHashSet(RexUtil.families(types)); if (families.size() < 2) { @@ -946,6 +952,11 @@ private static RelDataType consistentType(SqlRexContext cx, /* OVERRIDE POINT */ //https://github.com/Kyligence/KAP/issues/13872 case LEAST_RESTRICTIVE_NO_CONVERT_TO_VARYING: + if (Boolean.parseBoolean( + System.getProperty("calcite.convert-date-to-timeStamp-enabled", "false")) + && isDateCompareWithTimeStamp(types, exprs)) { + return castDateToTimeStamp(types); + } return cx.getTypeFactory().leastRestrictive(types, false); default: return null; @@ -988,6 +999,33 @@ private RexNode convertPlus(SqlRexContext cx, SqlCall call) { } } + public static boolean isDateCompareWithTimeStamp(List types, List exprs) { + + if (types.size() != 2 || exprs.size() != 2) { + return false; + } + + RelDataType leftType = types.get(0); + RelDataType rightType = types.get(1); + + RexNode leftNode = exprs.get(0); + RexNode rightNode = exprs.get(1); + + if (leftNode instanceof RexInputRef && rightNode instanceof RexLiteral) { + return leftType.getFamily().equals(SqlTypeFamily.TIMESTAMP) + && rightType.getFamily().equals(SqlTypeFamily.DATE); + } + if (leftNode instanceof RexLiteral && rightNode instanceof RexInputRef) { + return leftType.getFamily().equals(SqlTypeFamily.DATE) + && rightType.getFamily().equals(SqlTypeFamily.TIMESTAMP); + } + return false; + } + + public static RelDataType castDateToTimeStamp(List types) { + return types.get(0).getFamily().equals(SqlTypeFamily.TIMESTAMP) ? types.get(0) : types.get(1); + } + private RexNode convertIsDistinctFrom( SqlRexContext cx, SqlCall call, diff --git a/druid/pom.xml b/druid/pom.xml index 9ceab219fb0e..1698af7bced7 100644 --- a/druid/pom.xml +++ b/druid/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-druid jar - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite Druid Druid adapter for Calcite diff --git a/elasticsearch2/pom.xml b/elasticsearch2/pom.xml index b7cba4dede0a..8b76386132a9 100644 --- a/elasticsearch2/pom.xml +++ b/elasticsearch2/pom.xml @@ -21,12 +21,12 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-elasticsearch2 jar - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite Elasticsearch Elasticsearch adapter for Calcite diff --git a/elasticsearch5/pom.xml b/elasticsearch5/pom.xml index ee472e4e4508..82108ddfcff5 100644 --- a/elasticsearch5/pom.xml +++ b/elasticsearch5/pom.xml @@ -21,12 +21,12 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-elasticsearch5 jar - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite Elasticsearch5 Elasticsearch5 adapter for Calcite diff --git a/example/csv/pom.xml b/example/csv/pom.xml index 0ad931b21866..cdddea62ce95 100644 --- a/example/csv/pom.xml +++ b/example/csv/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite-example - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-example-csv jar - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite Example CSV An example Calcite provider that reads CSV files diff --git a/example/function/pom.xml b/example/function/pom.xml index 0b9f338baffe..2a8fba1bf245 100644 --- a/example/function/pom.xml +++ b/example/function/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite-example - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-example-function jar - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite Example Function Examples of user-defined Calcite functions diff --git a/example/pom.xml b/example/pom.xml index 3b17afc5237a..5507e2e50423 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -20,13 +20,13 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-example pom - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite Examples Calcite examples diff --git a/file/pom.xml b/file/pom.xml index 50302bcd37de..a039fa90b865 100644 --- a/file/pom.xml +++ b/file/pom.xml @@ -19,13 +19,13 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-file jar - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite File Calcite provider that reads files and URIs diff --git a/geode/pom.xml b/geode/pom.xml index 184e4108ec2e..e6d6a7907854 100644 --- a/geode/pom.xml +++ b/geode/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-geode jar - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite Geode Geode adapter for Calcite diff --git a/linq4j/pom.xml b/linq4j/pom.xml index b74ffeb9c1d6..9335552c6fde 100644 --- a/linq4j/pom.xml +++ b/linq4j/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-linq4j jar - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite Linq4j Calcite APIs for LINQ (Language-Integrated Query) in Java diff --git a/mongodb/pom.xml b/mongodb/pom.xml index 37f2bf469cf8..717c4e6f4676 100644 --- a/mongodb/pom.xml +++ b/mongodb/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-mongodb jar -1.116.0-kylin-4.x-r023 +1.116.0-kylin-4.x-r024 Calcite MongoDB MongoDB adapter for Calcite diff --git a/pig/pom.xml b/pig/pom.xml index d88335701797..b177e6f69397 100644 --- a/pig/pom.xml +++ b/pig/pom.xml @@ -20,11 +20,11 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-pig jar - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite Pig Pig adapter for Calcite diff --git a/piglet/pom.xml b/piglet/pom.xml index 8bd538af4d6a..5e948a17185e 100644 --- a/piglet/pom.xml +++ b/piglet/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-piglet jar - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite Piglet Pig-like language built on top of Calcite algebra diff --git a/plus/pom.xml b/plus/pom.xml index 735431ec343a..17f7ecb0e7b2 100644 --- a/plus/pom.xml +++ b/plus/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-plus jar - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite Plus Miscellaneous extras for Calcite diff --git a/pom.xml b/pom.xml index 702ae1b3dd9e..eb4463317530 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ limitations under the License. org.apache.calcite calcite pom - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite diff --git a/server/pom.xml b/server/pom.xml index c6ea95a6a922..bd27653c56ef 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-server jar - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite Server Calcite Server diff --git a/spark/pom.xml b/spark/pom.xml index ddb0419e1d8e..7964c0468fd6 100644 --- a/spark/pom.xml +++ b/spark/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-spark jar - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite Spark diff --git a/splunk/pom.xml b/splunk/pom.xml index 858fd5f82687..5cf99d64dca8 100644 --- a/splunk/pom.xml +++ b/splunk/pom.xml @@ -20,12 +20,12 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 calcite-splunk jar - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024 Calcite Splunk Splunk adapter for Calcite; also a JDBC driver for Splunk diff --git a/ubenchmark/pom.xml b/ubenchmark/pom.xml index 4f3ea5497be2..9f17e945c257 100644 --- a/ubenchmark/pom.xml +++ b/ubenchmark/pom.xml @@ -20,7 +20,7 @@ limitations under the License. org.apache.calcite calcite - 1.116.0-kylin-4.x-r023 + 1.116.0-kylin-4.x-r024