You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/09/14 09:21:33 UTC

[shardingsphere] branch master updated: fix calcite class cast exception when sql contains data/time/timestamp field (#12420)

This is an automated email from the ASF dual-hosted git repository.

panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 5e10648  fix calcite class cast exception when sql contains data/time/timestamp field (#12420)
5e10648 is described below

commit 5e106485afc3514a7f455293195349b23dad1fb7
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Tue Sep 14 17:21:01 2021 +0800

    fix calcite class cast exception when sql contains data/time/timestamp field (#12420)
    
    * fix calcite class cast exception when sql contains data/time/timestamp field
    
    * modify test case
---
 .../sql/federate/FederateJDBCExecutorTest.java     | 10 +++----
 .../core/metadata/FederateTableMetadata.java       |  6 ++--
 ...data_type_across_single_and_sharding_tables.xml | 33 ++++++++++++++++++++++
 ...data_type_across_single_and_sharding_tables.xml | 33 ++++++++++++++++++++++
 .../cases/dql/dql-integration-test-cases.xml       |  4 +++
 5 files changed, 78 insertions(+), 8 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/federate/FederateJDBCExecutorTest.java b/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/federate/FederateJDBCExecutorTest.java
index 6065d0b..36c788a 100644
--- a/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/federate/FederateJDBCExecutorTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/federate/FederateJDBCExecutorTest.java
@@ -88,11 +88,11 @@ public final class FederateJDBCExecutorTest {
     @Test
     public void testSimpleSelect() {
         RelNode relNode = optimizer.optimize(SELECT_SQL_BY_ID_ACROSS_SINGLE_AND_SHARDING_TABLES);
-        String temp = "EnumerableCalc(expr#0..4=[{inputs}],proj#0..1=[{exprs}],information=[$t4])"
-            + "  EnumerableCalc(expr#0..4=[{inputs}],expr#5=[=($t1,$t3)],proj#0..4=[{exprs}],$condition=[$t5])"
-            + "    EnumerableNestedLoopJoin(condition=[true],joinType=[inner])"
-            + "      EnumerableTableScan(table=[[federate_jdbc,t_order_federate]])"
-            + "      EnumerableTableScan(table=[[federate_jdbc,t_user_info]])";
+        String temp = "EnumerableCalc(expr#0..4=[{inputs}], proj#0..1=[{exprs}], information=[$t4])" 
+                + "  EnumerableCalc(expr#0..4=[{inputs}], expr#5=[CAST($t1):VARCHAR], expr#6=[CAST($t3):VARCHAR], expr#7=[=($t5, $t6)], proj#0..4=[{exprs}], $condition=[$t7])" 
+                + "    EnumerableNestedLoopJoin(condition=[true], joinType=[inner])" 
+                + "      EnumerableTableScan(table=[[federate_jdbc, t_order_federate]])" 
+                + "      EnumerableTableScan(table=[[federate_jdbc, t_user_info]])";
         String expected = temp.replaceAll("\\s*", "");
         String actual = relNode.explain().replaceAll("\\s*", "");
         assertThat(actual, is(expected));
diff --git a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/metadata/FederateTableMetadata.java b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/metadata/FederateTableMetadata.java
index 9e80632..0cbeba3 100644
--- a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/metadata/FederateTableMetadata.java
+++ b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/metadata/FederateTableMetadata.java
@@ -18,12 +18,12 @@
 package org.apache.shardingsphere.infra.optimize.core.metadata;
 
 import lombok.Getter;
+import org.apache.calcite.avatica.SqlType;
 import org.apache.calcite.rel.type.RelDataTypeFactory;
 import org.apache.calcite.rel.type.RelDataTypeImpl;
 import org.apache.calcite.rel.type.RelDataTypeSystem;
 import org.apache.calcite.rel.type.RelProtoDataType;
 import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
-import org.apache.calcite.sql.type.SqlTypeName;
 import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData;
 import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
 
@@ -54,8 +54,8 @@ public final class FederateTableMetadata {
     private RelProtoDataType createRelDataType(final TableMetaData tableMetaData) {
         RelDataTypeFactory.Builder fieldInfo = TYPE_FACTORY.builder();
         for (ColumnMetaData each : tableMetaData.getColumns().values()) {
-            SqlTypeName sqlTypeName = SqlTypeName.getNameForJdbcType(each.getDataType());
-            fieldInfo.add(each.getName(), null == sqlTypeName ? TYPE_FACTORY.createUnknownType() : TYPE_FACTORY.createTypeWithNullability(TYPE_FACTORY.createSqlType(sqlTypeName), true));
+            Class<?> clazz = SqlType.valueOf(each.getDataType()).clazz;
+            fieldInfo.add(each.getName(), null == clazz ? TYPE_FACTORY.createUnknownType() : TYPE_FACTORY.createTypeWithNullability(TYPE_FACTORY.createJavaType(clazz), true));
         }
         return RelDataTypeImpl.proto(fieldInfo.build());
     }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/dql/dataset/db/select_with_data_type_across_single_and_sharding_tables.xml b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/dql/dataset/db/select_with_data_type_across_single_and_sharding_tables.xml
new file mode 100644
index 0000000..ab957a3
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/dql/dataset/db/select_with_data_type_across_single_and_sharding_tables.xml
@@ -0,0 +1,33 @@
+<!--
+  ~ 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.
+  -->
+
+<dataset>
+    <metadata>
+        <column name="item_id" />
+        <column name="order_id" />
+        <column name="creation_date" />
+        <column name="id" />
+    </metadata>
+    <row values="100001, 1000, 2017-08-08, 1000" />
+    <row values="100002, 1000, 2017-08-08, 1000" />
+    <row values="110101, 1101, 2017-08-08, 1101" />
+    <row values="110102, 1101, 2017-08-08, 1101" />
+    <row values="230001, 2300, 2017-08-08, 2300" />
+    <row values="230002, 2300, 2017-08-08, 2300" />
+    <row values="240101, 2401, 2017-08-08, 2401" />
+    <row values="240102, 2401, 2017-08-08, 2401" />
+</dataset>
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/dql/dataset/tbl/select_with_data_type_across_single_and_sharding_tables.xml b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/dql/dataset/tbl/select_with_data_type_across_single_and_sharding_tables.xml
new file mode 100644
index 0000000..d36f3a4
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/dql/dataset/tbl/select_with_data_type_across_single_and_sharding_tables.xml
@@ -0,0 +1,33 @@
+<!--
+  ~ 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.
+  -->
+
+<dataset>
+    <metadata>
+        <column name="item_id" />
+        <column name="order_id" />
+        <column name="creation_date" />
+        <column name="id" />
+    </metadata>
+    <row values="100000, 1000, 2017-08-08, 1000" />
+    <row values="100001, 1000, 2017-08-08, 1000" />
+    <row values="100100, 1001, 2017-08-08, 1001" />
+    <row values="100101, 1001, 2017-08-08, 1001" />
+    <row values="100200, 1002, 2017-08-08, 1002" />
+    <row values="100201, 1002, 2017-08-08, 1002" />
+    <row values="100300, 1003, 2017-08-08, 1003" />
+    <row values="100301, 1003, 2017-08-08, 1003" />
+</dataset>
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/dql/dql-integration-test-cases.xml b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/dql/dql-integration-test-cases.xml
index 4333409..5195dd2 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/dql/dql-integration-test-cases.xml
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/dql/dql-integration-test-cases.xml
@@ -604,4 +604,8 @@
         <assertion parameters="1:int" expected-data-file="select_sql_by_id_across_single_tables_with_encrypt_user_id.xml" />
     </test-case>
 
+    <test-case sql="SELECT o.item_id, o.order_id, o.creation_date, s.id FROM t_order_item o INNER JOIN t_single_table s ON o.order_id = s.id" scenario-types="db,tbl">
+        <assertion expected-data-file="select_with_data_type_across_single_and_sharding_tables.xml"/>
+    </test-case>
+
 </integration-test-cases>