strongduanmu commented on a change in pull request #11426:
URL: https://github.com/apache/shardingsphere/pull/11426#discussion_r673866068



##########
File path: 
shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
##########
@@ -605,9 +608,36 @@ public ASTNode visitQueryExpressionBody(final 
QueryExpressionBodyContext ctx) {
         if (1 == ctx.getChildCount() && ctx.getChild(0) instanceof 
QueryPrimaryContext) {
             return visit(ctx.queryPrimary());
         }
-        throw new IllegalStateException("union select is not supported yet.");
+        if (ctx.queryExpressionBody() != null) {

Review comment:
       @tuichenchuxin `null != ctx.queryExpressionBody()` is better.

##########
File path: 
shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
##########
@@ -605,9 +608,36 @@ public ASTNode visitQueryExpressionBody(final 
QueryExpressionBodyContext ctx) {
         if (1 == ctx.getChildCount() && ctx.getChild(0) instanceof 
QueryPrimaryContext) {
             return visit(ctx.queryPrimary());
         }
-        throw new IllegalStateException("union select is not supported yet.");
+        if (ctx.queryExpressionBody() != null) {
+            MySQLSelectStatement result = (MySQLSelectStatement) 
visit(ctx.queryExpressionBody());
+            ParserRuleContext ruleContext = ctx.queryPrimary() != null ? 
ctx.queryPrimary() : ctx.queryExpressionParens(0);
+            MySQLSelectStatement union = (MySQLSelectStatement) 
visit(ruleContext);
+            UnionSegment unionSegment = new UnionSegment(getUnionType(ctx), 
union, ctx.UNION().getSymbol().getStartIndex(), 
ruleContext.getStop().getStopIndex());
+            addUnionSegments(result, unionSegment);
+            return result;
+        }
+        MySQLSelectStatement result = (MySQLSelectStatement) 
visit(ctx.queryExpressionParens(0));

Review comment:
       This code block looks same with previous, please consider to extract 
with method.

##########
File path: 
shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
##########
@@ -605,9 +608,36 @@ public ASTNode visitQueryExpressionBody(final 
QueryExpressionBodyContext ctx) {
         if (1 == ctx.getChildCount() && ctx.getChild(0) instanceof 
QueryPrimaryContext) {
             return visit(ctx.queryPrimary());
         }
-        throw new IllegalStateException("union select is not supported yet.");
+        if (ctx.queryExpressionBody() != null) {
+            MySQLSelectStatement result = (MySQLSelectStatement) 
visit(ctx.queryExpressionBody());
+            ParserRuleContext ruleContext = ctx.queryPrimary() != null ? 
ctx.queryPrimary() : ctx.queryExpressionParens(0);
+            MySQLSelectStatement union = (MySQLSelectStatement) 
visit(ruleContext);
+            UnionSegment unionSegment = new UnionSegment(getUnionType(ctx), 
union, ctx.UNION().getSymbol().getStartIndex(), 
ruleContext.getStop().getStopIndex());
+            addUnionSegments(result, unionSegment);
+            return result;
+        }
+        MySQLSelectStatement result = (MySQLSelectStatement) 
visit(ctx.queryExpressionParens(0));
+        ParserRuleContext ruleContext = ctx.queryPrimary() != null ? 
ctx.queryPrimary() : ctx.queryExpressionParens(1);
+        MySQLSelectStatement union = (MySQLSelectStatement) visit(ruleContext);
+        UnionSegment unionSegment = new UnionSegment(getUnionType(ctx), union, 
ctx.UNION().getSymbol().getStartIndex(), ruleContext.getStop().getStopIndex());
+        addUnionSegments(result, unionSegment);

Review comment:
       Please init UnionSegments in MySQLSelectStatement.

##########
File path: 
shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/dml/SelectStatementHandler.java
##########
@@ -113,4 +115,17 @@
         }
         return Optional.empty();
     }
+
+    /**
+     * Get union segment.
+     *
+     * @param selectStatement select statement
+     * @return union segment list
+     */
+    public static Optional<List<UnionSegment>> getUnionSegments(final 
SelectStatement selectStatement) {

Review comment:
       UnionSegment should be a common segment for different dialects.

##########
File path: 
shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/dml/SelectStatement.java
##########
@@ -52,6 +54,8 @@
     private OrderBySegment orderBy;
     
     private WithSegment withSegment;
+
+    private List<UnionSegment> unionSegments;

Review comment:
       `Collection<UnionSegment> unionSegments = new LinkedList()` is better.

##########
File path: 
shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/dml/impl/SelectStatementAssert.java
##########
@@ -68,8 +72,9 @@ public static void assertIs(final SQLCaseAssertContext 
assertContext, final Sele
         assertTable(assertContext, actual, expected);
         assertLockClause(assertContext, actual, expected);
         assertWithClause(assertContext, actual, expected);
+        assertUnions(assertContext, actual, expected);
     }
-    

Review comment:
       Please keep indents consistent with the previous one.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to