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 aaff9ca991e Fix NullPointerException when execute select union 
statement contains subquery (#18840)
aaff9ca991e is described below

commit aaff9ca991e25e4dd6615152164ad381c97a0c58
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Tue Jul 5 09:21:22 2022 +0800

    Fix NullPointerException when execute select union statement contains 
subquery (#18840)
---
 .../parser/sql/common/util/SubqueryExtractUtil.java    |  8 ++++++++
 .../sql/common/util/SubqueryExtractUtilTest.java       | 18 ++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/SubqueryExtractUtil.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/SubqueryExtractUtil.java
index be261baeca7..5ed91c8ed76 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/SubqueryExtractUtil.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/SubqueryExtractUtil.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.sql.parser.sql.common.util;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.sql.parser.sql.common.constant.SubqueryType;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.combine.CombineSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BetweenExpression;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExistsSubqueryExpression;
@@ -64,6 +65,13 @@ public final class SubqueryExtractUtil {
         if (selectStatement.getWhere().isPresent()) {
             extractSubquerySegmentsFromExpression(result, 
selectStatement.getWhere().get().getExpr());
         }
+        extractSubquerySegmentsFromCombines(result, 
selectStatement.getCombines());
+    }
+    
+    private static void extractSubquerySegmentsFromCombines(final 
List<SubquerySegment> result, final Collection<CombineSegment> combineSegments) 
{
+        for (CombineSegment each : combineSegments) {
+            extractSubquerySegments(result, each.getSelectStatement());
+        }
     }
     
     private static void extractSubquerySegmentsFromProjections(final 
List<SubquerySegment> result, final ProjectionsSegment projections) {
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/util/SubqueryExtractUtilTest.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/util/SubqueryExtractUtilTest.java
index 6b56d73339d..cdcc2847865 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/util/SubqueryExtractUtilTest.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/util/SubqueryExtractUtilTest.java
@@ -18,7 +18,9 @@
 package org.apache.shardingsphere.sql.parser.sql.common.util;
 
 import 
org.apache.shardingsphere.sql.parser.sql.common.constant.AggregationType;
+import org.apache.shardingsphere.sql.parser.sql.common.constant.CombineType;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.combine.CombineSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.InExpression;
@@ -164,4 +166,20 @@ public final class SubqueryExtractUtilTest {
                 left, new SubqueryExpressionSegment(new SubquerySegment(0, 0, 
new MySQLSelectStatement())), false)));
         return new SubquerySegment(0, 0, selectStatement);
     }
+    
+    @Test
+    public void assertGetSubquerySegmentsWithCombineSegment() {
+        SelectStatement selectStatement = new MySQLSelectStatement();
+        selectStatement.getCombines().add(new CombineSegment(0, 0, 
CombineType.UNION, createSelectStatementForCombineSegment()));
+        Collection<SubquerySegment> actual = 
SubqueryExtractUtil.getSubquerySegments(selectStatement);
+        assertThat(actual.size(), is(1));
+    }
+    
+    private SelectStatement createSelectStatementForCombineSegment() {
+        SelectStatement result = new MySQLSelectStatement();
+        ExpressionSegment left = new ColumnSegment(0, 0, new 
IdentifierValue("order_id"));
+        result.setWhere(new WhereSegment(0, 0, new InExpression(0, 0,
+                left, new SubqueryExpressionSegment(new SubquerySegment(0, 0, 
new MySQLSelectStatement())), false)));
+        return result;
+    }
 }

Reply via email to