This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 ad6ba988793 Enhance SQLNodeConverterEngine to support
'select_string_constant_type_cast' (#27258)
ad6ba988793 is described below
commit ad6ba9887936983c0fa2b2a7febd6b7953d55b2d
Author: Yunbo Ni <[email protected]>
AuthorDate: Mon Sep 4 16:05:02 2023 +0800
Enhance SQLNodeConverterEngine to support
'select_string_constant_type_cast' (#27258)
* feat(task5): support INT4 and MONEY type
* feat: support typecast
* feat: all postgresql type cast
* feat: sync merge
* fix: add new line in select.xml
* feat: datatype converter spi
* fix: IN4->INT4
---
.../impl/TypeCastExpressionConverter.java | 4 +-
.../compiler/converter/type/DataTypeConverter.java | 59 ++++++++++++++++++++++
.../src/test/resources/converter/select.xml | 1 +
3 files changed, 62 insertions(+), 2 deletions(-)
diff --git
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/TypeCastExpressionConverter.java
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/TypeCastExpressionConverter.java
index 837c2173296..97387b0542a 100644
---
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/TypeCastExpressionConverter.java
+++
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/TypeCastExpressionConverter.java
@@ -24,10 +24,10 @@ import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlTypeNameSpec;
import org.apache.calcite.sql.fun.SqlCastFunction;
import org.apache.calcite.sql.parser.SqlParserPos;
-import org.apache.calcite.sql.type.SqlTypeName;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.TypeCastExpression;
import
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.SQLSegmentConverter;
import
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.expression.ExpressionConverter;
+import
org.apache.shardingsphere.sqlfederation.compiler.converter.type.DataTypeConverter;
import java.util.Arrays;
import java.util.Optional;
@@ -43,7 +43,7 @@ public final class TypeCastExpressionConverter implements
SQLSegmentConverter<Ty
if (!expression.isPresent()) {
return Optional.empty();
}
- SqlTypeNameSpec sqlTypeName = new
SqlBasicTypeNameSpec(SqlTypeName.valueOf(segment.getDataType().toUpperCase()),
SqlParserPos.ZERO);
+ SqlTypeNameSpec sqlTypeName = new
SqlBasicTypeNameSpec(DataTypeConverter.convert(segment.getDataType().toUpperCase()),
SqlParserPos.ZERO);
return Optional.of(new SqlBasicCall(new SqlCastFunction(),
Arrays.asList(expression.get(), new SqlDataTypeSpec(sqlTypeName,
SqlParserPos.ZERO)), SqlParserPos.ZERO));
}
}
diff --git
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/type/DataTypeConverter.java
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/type/DataTypeConverter.java
new file mode 100644
index 00000000000..38ba8550ac6
--- /dev/null
+++
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/type/DataTypeConverter.java
@@ -0,0 +1,59 @@
+/*
+ * 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.
+ */
+
+package org.apache.shardingsphere.sqlfederation.compiler.converter.type;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.calcite.sql.type.SqlTypeName;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Datatype converter.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public class DataTypeConverter {
+
+ private static final Map<String, SqlTypeName> REGISTRY = new HashMap<>();
+
+ static {
+ registerDataType();
+ }
+
+ private static void registerDataType() {
+ REGISTRY.put("INT", SqlTypeName.INTEGER);
+ REGISTRY.put("INT2", SqlTypeName.SMALLINT);
+ REGISTRY.put("INT4", SqlTypeName.INTEGER);
+ REGISTRY.put("INT8", SqlTypeName.BIGINT);
+ REGISTRY.put("MONEY", SqlTypeName.DECIMAL);
+ }
+
+ /**
+ * Convert to SQL operator.
+ *
+ * @param dataType data type to be converted
+ * @return converted SQL operator
+ */
+ public static SqlTypeName convert(final String dataType) {
+ if (!REGISTRY.containsKey(dataType)) {
+ return SqlTypeName.valueOf(dataType);
+ }
+ return REGISTRY.get(dataType);
+ }
+}
diff --git a/test/it/optimizer/src/test/resources/converter/select.xml
b/test/it/optimizer/src/test/resources/converter/select.xml
index 50f904e895e..90fb19db01a 100644
--- a/test/it/optimizer/src/test/resources/converter/select.xml
+++ b/test/it/optimizer/src/test/resources/converter/select.xml
@@ -17,6 +17,7 @@
-->
<sql-node-converter-test-cases>
+ <test-cases sql-case-id="select_string_constant_type_cast"
expected-sql="SELECT CAST('1' AS INTEGER), CAST('2' AS DECIMAL)"
dbtypes="openGauss,PostgreSQL" />
<test-cases
sql-case-id="select_with_database_name_and_schema_name_in_table"
expected-sql="SELECT "order_id" FROM
"sharding_db"."public"."t_order" WHERE
"user_id" = ? AND "order_id" = ?"
db-types="PostgreSQL,openGauss" sql-case-types="PLACEHOLDER" />
<test-cases
sql-case-id="select_with_database_name_and_schema_name_in_table"
expected-sql="SELECT "order_id" FROM
"sharding_db"."public"."t_order" WHERE
"user_id" = 1 AND "order_id" = 1"
db-types="PostgreSQL,openGauss" sql-case-types="LITERAL" />
<test-cases sql-case-id="select_with_json_value_return_type"
expected-sql="SELECT * FROM `t_order` WHERE JSON_VALUE(`items`, '''$.name'''
'RETURNING' VARCHAR(100)) = 'jack'" db-types="MySQL" />