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 823256ea238 support variables and 2 operators (#28296)
823256ea238 is described below

commit 823256ea238017e4a568b02bb4d51c1f18c80a97
Author: Yunbo Ni <[email protected]>
AuthorDate: Mon Sep 4 16:12:54 2023 +0800

    support variables and 2 operators (#28296)
    
    * feat: variable segment converter
    
    * feat: assignment op (:=)
    
    * feat: regexp op (REGEXP)
    
    * fix: check style
---
 .../segment/expression/ExpressionConverter.java    |  5 ++++
 .../impl/BinaryOperationExpressionConverter.java   |  2 ++
 .../expression/impl/SQLExtensionOperatorTable.java |  4 +++
 .../expression/impl/VariableSegmentConverter.java  | 34 ++++++++++++++++++++++
 .../test/resources/converter/select-expression.xml |  3 ++
 .../src/test/resources/converter/select.xml        |  2 ++
 6 files changed, 50 insertions(+)

diff --git 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/ExpressionConverter.java
 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/ExpressionConverter.java
index 6cb5542caf5..898ec2ac906 100644
--- 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/ExpressionConverter.java
+++ 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/ExpressionConverter.java
@@ -19,6 +19,7 @@ package 
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.expre
 
 import org.apache.calcite.sql.SqlNode;
 import 
org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dal.VariableSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BetweenExpression;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
@@ -60,6 +61,7 @@ import 
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.projec
 import 
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.projection.impl.DataTypeConverter;
 import 
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.expression.impl.NotExpressionConverter;
 import 
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.expression.impl.MatchExpressionConverter;
+import 
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.expression.impl.VariableSegmentConverter;
 
 import java.util.Optional;
 
@@ -131,6 +133,9 @@ public final class ExpressionConverter implements 
SQLSegmentConverter<Expression
         if (segment instanceof CollateExpression) {
             return new 
CollateExpressionConverter().convert((CollateExpression) segment);
         }
+        if (segment instanceof VariableSegment) {
+            return new VariableSegmentConverter().convert((VariableSegment) 
segment);
+        }
         if (segment instanceof UnaryOperationExpression) {
             return new 
UnaryOperationExpressionConverter().convert((UnaryOperationExpression) segment);
         }
diff --git 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/BinaryOperationExpressionConverter.java
 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/BinaryOperationExpressionConverter.java
index cd4b2f43a86..729913874f2 100644
--- 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/BinaryOperationExpressionConverter.java
+++ 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/BinaryOperationExpressionConverter.java
@@ -79,9 +79,11 @@ public final class BinaryOperationExpressionConverter 
implements SQLSegmentConve
         register(SQLExtensionOperatorTable.SIGNED_LEFT_SHIFT);
         register(SQLExtensionOperatorTable.XOR);
         register(SQLExtensionOperatorTable.LOGICAL_AND);
+        register(SQLExtensionOperatorTable.REGEXP);
         register(SQLExtensionOperatorTable.NOT_REGEXP);
         register(SQLExtensionOperatorTable.SOUNDS_LIKE);
         register(SQLExtensionOperatorTable.NULL_SAFE);
+        register(SQLExtensionOperatorTable.ASSIGNMENT);
     }
     
     private static void register(final SqlOperator sqlOperator) {
diff --git 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/SQLExtensionOperatorTable.java
 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/SQLExtensionOperatorTable.java
index c7ae21a671c..946b4058c36 100644
--- 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/SQLExtensionOperatorTable.java
+++ 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/SQLExtensionOperatorTable.java
@@ -48,6 +48,8 @@ public final class SQLExtensionOperatorTable {
     
     public static final SqlBinaryOperator LOGICAL_AND = new 
SqlBinaryOperator("&&", SqlKind.OTHER, 24, true, null, null, null);
     
+    public static final SqlBinaryOperator REGEXP = new 
SqlBinaryOperator("REGEXP", SqlKind.OTHER, 30, true, null, null, null);
+    
     public static final SqlBinaryOperator NOT_REGEXP = new 
SqlBinaryOperator("NOT REGEXP", SqlKind.OTHER, 30, true, null, null, null);
     
     public static final SqlBinaryOperator SOUNDS_LIKE = new 
SqlBinaryOperator("SOUNDS LIKE", SqlKind.OTHER, 30, true, null, null, null);
@@ -56,5 +58,7 @@ public final class SQLExtensionOperatorTable {
     
     public static final SqlBinaryOperator COLLATE = new 
SqlBinaryOperator("COLLATE", SqlKind.OTHER, 30, true, null, null, null);
     
+    public static final SqlBinaryOperator ASSIGNMENT = new 
SqlBinaryOperator(":=", SqlKind.OTHER, 30, true, null, null, null);
+    
     public static final SqlPrefixOperator TILDE = new SqlPrefixOperator("~", 
SqlKind.OTHER, 26, null, null, null);
 }
diff --git 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/VariableSegmentConverter.java
 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/VariableSegmentConverter.java
new file mode 100644
index 00000000000..6f160315e07
--- /dev/null
+++ 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/VariableSegmentConverter.java
@@ -0,0 +1,34 @@
+/*
+ * 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.segment.expression.impl;
+
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dal.VariableSegment;
+import 
org.apache.shardingsphere.sqlfederation.compiler.converter.segment.SQLSegmentConverter;
+
+import java.util.Optional;
+
+public final class VariableSegmentConverter implements 
SQLSegmentConverter<VariableSegment, SqlNode> {
+    
+    @Override
+    public Optional<SqlNode> convert(final VariableSegment segment) {
+        return Optional.of(new SqlIdentifier(segment.getText(), 
SqlParserPos.ZERO));
+    }
+}
diff --git 
a/test/it/optimizer/src/test/resources/converter/select-expression.xml 
b/test/it/optimizer/src/test/resources/converter/select-expression.xml
index 86da2181105..ffb33c349f9 100644
--- a/test/it/optimizer/src/test/resources/converter/select-expression.xml
+++ b/test/it/optimizer/src/test/resources/converter/select-expression.xml
@@ -49,6 +49,9 @@
     <test-cases sql-case-id="select_where_with_predicate_with_in_subquery" 
expected-sql="SELECT * FROM &quot;t_order&quot; WHERE 
&quot;t_order&quot;.&quot;order_id&quot; NOT IN (SELECT &quot;order_id&quot; 
FROM &quot;t_order_item&quot; WHERE &quot;status&quot; &gt; 1)" 
db-types="PostgreSQL, openGauss" sql-case-types="LITERAL" />
     <test-cases sql-case-id="select_where_with_predicate_with_in_subquery" 
expected-sql="SELECT * FROM &quot;t_order&quot; WHERE 
&quot;t_order&quot;.&quot;order_id&quot; NOT IN (SELECT &quot;order_id&quot; 
FROM &quot;t_order_item&quot; WHERE &quot;status&quot; &gt; ?)" 
db-types="PostgreSQL, openGauss" sql-case-types="PLACEHOLDER" />
     <test-cases sql-case-id="select_where_with_simple_expr_with_collate" 
expected-sql="SELECT * FROM `t_order` WHERE `order_id` COLLATE 
'utf8mb4_0900_ai_ci'" db-types="MySQL" sql-case-types="LITERAL" />
+    <test-cases sql-case-id="select_where_with_simple_expr_with_variable" 
expected-sql="SELECT * FROM `t_order` WHERE `max_connections` &lt; `order_id`" 
db-types="MySQL" />
+    <test-cases sql-case-id="select_with_regexp" expected-sql="SELECT * FROM 
`t_order_item` AS `t` WHERE `t`.`status` REGEXP ? AND `t`.`item_id` IN (?, ?)" 
db-types="MySQL" sql-case-types="PLACEHOLDER" />
+    <test-cases sql-case-id="select_with_regexp" expected-sql="SELECT * FROM 
`t_order_item` AS `t` WHERE `t`.`status` REGEXP 'init' AND `t`.`item_id` IN (1, 
2)" db-types="MySQL" sql-case-types="LITERAL" />
     <test-cases sql-case-id="select_where_with_simple_expr_with_tilde" 
expected-sql="SELECT * FROM `t_order` WHERE ~ `t_order`.`order_id`" 
db-types="MySQL" />
     <test-cases 
sql-case-id="select_where_with_simple_expr_with_odbc_escape_syntax" 
expected-sql="SELECT * FROM `t_order` WHERE ?" db-types="MySQL" 
sql-case-types="PLACEHOLDER" />
     <test-cases 
sql-case-id="select_where_with_simple_expr_with_odbc_escape_syntax" 
expected-sql="SELECT * FROM `t_order` WHERE '1994-02-04 12:23:00'" 
db-types="MySQL" sql-case-types="LITERAL" />
diff --git a/test/it/optimizer/src/test/resources/converter/select.xml 
b/test/it/optimizer/src/test/resources/converter/select.xml
index 90fb19db01a..02cd475d2ad 100644
--- a/test/it/optimizer/src/test/resources/converter/select.xml
+++ b/test/it/optimizer/src/test/resources/converter/select.xml
@@ -20,5 +20,7 @@
     <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 &quot;order_id&quot; FROM 
&quot;sharding_db&quot;.&quot;public&quot;.&quot;t_order&quot; WHERE 
&quot;user_id&quot; = ? AND &quot;order_id&quot; = ?" 
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 &quot;order_id&quot; FROM 
&quot;sharding_db&quot;.&quot;public&quot;.&quot;t_order&quot; WHERE 
&quot;user_id&quot; = 1 AND &quot;order_id&quot; = 1" 
db-types="PostgreSQL,openGauss" sql-case-types="LITERAL" />
+    <test-cases sql-case-id="select_with_assignment_operator" 
expected-sql="SELECT `rn` := 1, `now_code` := '' FROM `t_order`" 
db-types="MySQL" />
+    <test-cases sql-case-id="select_with_assignment_operator_and_keyword" 
expected-sql="SELECT `KEY` := '', `num` := 123 FROM `t_order`" db-types="MySQL" 
/>
     <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" />
 </sql-node-converter-test-cases>

Reply via email to