You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/06/08 15:58:56 UTC

[shardingsphere] branch master updated: refactor convertToSQLStatement method, make it more easy to understand (#18238)

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

zhangliang 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 4f40688acac refactor convertToSQLStatement method, make it more easy to understand (#18238)
4f40688acac is described below

commit 4f40688acaceb86ac00a75a5b69c1bfe9ede806b
Author: liguoping <xd...@163.com>
AuthorDate: Wed Jun 8 23:58:50 2022 +0800

    refactor convertToSQLStatement method, make it more easy to understand (#18238)
    
    * refactor convertToSQLStatement method, make it more easy to understand
    
    * add union all test case
---
 .../optimizer/converter/SQLNodeConverterEngine.java   |  6 ++++--
 .../converter/type/CombineOperatorConverter.java      | 18 ++++++++++++++++++
 .../engine/SQLNodeConvertEngineParameterizedTest.java |  1 +
 .../src/main/resources/case/dml/select-union.xml      | 19 +++++++++++++++++++
 .../main/resources/sql/supported/dml/select-union.xml |  1 +
 5 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/SQLNodeConverterEngine.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/SQLNodeConverterEngine.java
index 310569346a4..e3fd737f749 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/SQLNodeConverterEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/SQLNodeConverterEngine.java
@@ -71,8 +71,10 @@ public final class SQLNodeConverterEngine {
             SqlNode rightSqlNode = ((SqlBasicCall) sqlNode).getOperandList().get(1);
             SelectStatement leftSelectStatement = (SelectStatement) convertToSQLStatement(leftSqlNode);
             SelectStatement rightSelectStatement = (SelectStatement) convertToSQLStatement(rightSqlNode);
-            leftSelectStatement.getCombines().add(
-                    new CombineSegment(rightSqlNode.getParserPosition().getColumnNum() - 7, rightSqlNode.getParserPosition().getEndColumnNum() - 1, CombineType.UNION, rightSelectStatement));
+            CombineType combineType = CombineOperatorConverter.convert(((SqlBasicCall) sqlNode).getOperator());
+            int startIndex = rightSqlNode.getParserPosition().getColumnNum() - (((SqlBasicCall) sqlNode).getOperator().getName() + " ").length() - 1;
+            int stopIndex = rightSqlNode.getParserPosition().getEndColumnNum() - 1;
+            leftSelectStatement.getCombines().add(new CombineSegment(startIndex, stopIndex, combineType, rightSelectStatement));
             return leftSelectStatement;
         }
         throw new UnsupportedOperationException("Unsupported SQL statement conversion.");
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/type/CombineOperatorConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/type/CombineOperatorConverter.java
index 13610f0d362..37ad8f93d56 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/type/CombineOperatorConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/type/CombineOperatorConverter.java
@@ -54,4 +54,22 @@ public final class CombineOperatorConverter {
         Preconditions.checkState(REGISTRY.containsKey(combineType), "Unsupported combine type: `%s`", combineType);
         return REGISTRY.get(combineType);
     }
+    
+    /**
+     * Convert to combine type.
+     * @param sqlOperator SQL operator to be converted
+     * @return converted combine type
+     */
+    public static CombineType convert(final SqlOperator sqlOperator) {
+        CombineType result = null;
+        for (CombineType each : REGISTRY.keySet()) {
+            SqlOperator currentOperator = REGISTRY.get(each);
+            if (currentOperator == sqlOperator) {
+                result = each;
+                break;
+            }
+        }
+        Preconditions.checkState(result != null, "Unsupported sql operator: `%s`", sqlOperator);
+        return result;
+    }
 }
diff --git a/shardingsphere-test/shardingsphere-optimize-test/src/test/java/org/apache/shardingsphere/infra/federation/converter/parameterized/engine/SQLNodeConvertEngineParameterizedTest.java b/shardingsphere-test/shardingsphere-optimize-test/src/test/java/org/apache/shardingsphere/infra/federation/converter/parameterized/engine/SQLNodeConvertEngineParameterizedTest.java
index d8de470efad..5f488a58f67 100644
--- a/shardingsphere-test/shardingsphere-optimize-test/src/test/java/org/apache/shardingsphere/infra/federation/converter/parameterized/engine/SQLNodeConvertEngineParameterizedTest.java
+++ b/shardingsphere-test/shardingsphere-optimize-test/src/test/java/org/apache/shardingsphere/infra/federation/converter/parameterized/engine/SQLNodeConvertEngineParameterizedTest.java
@@ -93,6 +93,7 @@ public final class SQLNodeConvertEngineParameterizedTest {
         SUPPORTED_SQL_CASE_IDS.add("select_constant_without_table");
         SUPPORTED_SQL_CASE_IDS.add("select_with_schema");
         SUPPORTED_SQL_CASE_IDS.add("select_with_union");
+        SUPPORTED_SQL_CASE_IDS.add("select_with_union_all");
         SUPPORTED_SQL_CASE_IDS.add("select_cast_function");
         SUPPORTED_SQL_CASE_IDS.add("select_with_same_table_name_and_alias");
         SUPPORTED_SQL_CASE_IDS.add("select_count_like_concat");
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select-union.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select-union.xml
index 1a24e3af1af..2333fb54bf9 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select-union.xml
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select-union.xml
@@ -36,6 +36,25 @@
         </combine>
     </select>
 
+    <select sql-case-id="select_with_union_all">
+        <projections start-index="7" stop-index="7">
+            <shorthand-projection start-index="7" stop-index="7" />
+        </projections>
+        <from>
+            <simple-table name="table1" start-index="14" stop-index="19" />
+        </from>
+        <union union-type="UNION_ALL" start-index="21" stop-index="50">
+            <select>
+                <projections start-index="38" stop-index="38">
+                    <shorthand-projection start-index="38" stop-index="38" />
+                </projections>
+                <from>
+                    <simple-table name="table2" start-index="45" stop-index="50" />
+                </from>
+            </select>
+        </union>
+    </select>
+
     <select sql-case-id="select_union">
         <projections start-index="7" stop-index="14">
             <column-projection name="order_id" start-index="7" stop-index="14" />
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select-union.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select-union.xml
index 2a9edb9ddfb..e219b852eb8 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select-union.xml
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select-union.xml
@@ -18,6 +18,7 @@
 
 <sql-cases>
     <sql-case id="select_with_union" value="SELECT * from table1 union select * from table2" db-types="MySQL,PostgreSQL,openGauss" />
+    <sql-case id="select_with_union_all" value="SELECT * from table1 union all select * from table2" db-types="MySQL,PostgreSQL,openGauss" />
     <sql-case id="select_union" value="SELECT order_id FROM t_order UNION SELECT order_id FROM t_order_item" db-types="MySQL,PostgreSQL,openGauss" />
     <sql-case id="select_intersect" value="SELECT * FROM table1 INTERSECT SELECT * FROM table2 INTERSECT SELECT * FROM table3" db-types="PostgreSQL,openGauss" />
     <sql-case id="select_except" value="SELECT * FROM table1 EXCEPT ALL SELECT * FROM table2 EXCEPT ALL SELECT * FROM table3" db-types="PostgreSQL,openGauss" />