From 204e1d06e58a89c412493bf217aa80347bb695a5 Mon Sep 17 00:00:00 2001 From: niu niu Date: Sun, 12 Nov 2023 20:40:45 +0800 Subject: [PATCH 1/2] Add oracle select union statement parse test --- .../resources/case/dml/select-combine.xml | 790 ++++++++++++++++++ .../sql/supported/dml/select-combine.xml | 15 + 2 files changed, 805 insertions(+) diff --git a/test/it/parser/src/main/resources/case/dml/select-combine.xml b/test/it/parser/src/main/resources/case/dml/select-combine.xml index 22db627e62c36..e838b1bce4257 100644 --- a/test/it/parser/src/main/resources/case/dml/select-combine.xml +++ b/test/it/parser/src/main/resources/case/dml/select-combine.xml @@ -1018,4 +1018,794 @@ + + + + + + + + + + + + + + + + + + diff --git a/test/it/parser/src/main/resources/sql/supported/dml/select-combine.xml b/test/it/parser/src/main/resources/sql/supported/dml/select-combine.xml index 2b55b07faa56f..21f2e552739c6 100644 --- a/test/it/parser/src/main/resources/sql/supported/dml/select-combine.xml +++ b/test/it/parser/src/main/resources/sql/supported/dml/select-combine.xml @@ -41,4 +41,19 @@ + + + + + + + + + From 20672a597014afff8523019e40e0908426791ff7 Mon Sep 17 00:00:00 2001 From: niu niu Date: Tue, 14 Nov 2023 19:07:19 +0800 Subject: [PATCH 2/2] Fix select union parse --- .../antlr4/imports/oracle/DMLStatement.g4 | 6 +- .../type/OracleDMLStatementVisitor.java | 56 +-- .../resources/case/dml/select-combine.xml | 464 +++++++++--------- 3 files changed, 256 insertions(+), 270 deletions(-) diff --git a/parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/DMLStatement.g4 b/parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/DMLStatement.g4 index de4f968a4ed07..6839d945dbe84 100644 --- a/parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/DMLStatement.g4 +++ b/parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/DMLStatement.g4 @@ -136,11 +136,11 @@ select ; selectSubquery - : (selectCombineClause | queryBlock | parenthesisSelectSubquery) pivotClause? orderByClause? rowLimitingClause + : selectSubquery combineType selectSubquery | ((queryBlock | parenthesisSelectSubquery) pivotClause? orderByClause? rowLimitingClause) ; -selectCombineClause - : ((queryBlock | parenthesisSelectSubquery) orderByClause? rowLimitingClause) ((UNION ALL? | INTERSECT | MINUS) selectSubquery)+ +combineType + : UNION ALL? | INTERSECT | MINUS ; parenthesisSelectSubquery diff --git a/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/type/OracleDMLStatementVisitor.java b/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/type/OracleDMLStatementVisitor.java index 00d19ef9aff13..5248474fc3c56 100644 --- a/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/type/OracleDMLStatementVisitor.java +++ b/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/type/OracleDMLStatementVisitor.java @@ -82,7 +82,6 @@ import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.QueryTableExprContext; import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.ReferenceModelContext; import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.RollupCubeClauseContext; -import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.SelectCombineClauseContext; import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.SelectContext; import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.SelectFromClauseContext; import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.SelectJoinOptionContext; @@ -533,12 +532,14 @@ public ASTNode visitAssignmentValue(final AssignmentValueContext ctx) { @Override public ASTNode visitSelectSubquery(final SelectSubqueryContext ctx) { OracleSelectStatement result; - if (null != ctx.queryBlock()) { - result = (OracleSelectStatement) visit(ctx.queryBlock()); - } else if (null != ctx.selectCombineClause()) { - result = (OracleSelectStatement) visit(ctx.selectCombineClause()); + if (null != ctx.combineType()) { + result = new OracleSelectStatement(); + OracleSelectStatement left = (OracleSelectStatement) visit(ctx.selectSubquery(0)); + result.setProjections(left.getProjections()); + result.setFrom(left.getFrom()); + createSelectCombineClause(ctx, result, left); } else { - result = (OracleSelectStatement) visit(ctx.parenthesisSelectSubquery()); + result = null != ctx.queryBlock() ? (OracleSelectStatement) visit(ctx.queryBlock()) : (OracleSelectStatement) visit(ctx.parenthesisSelectSubquery()); } if (null != ctx.orderByClause()) { result.setOrderBy((OrderBySegment) visit(ctx.orderByClause())); @@ -547,6 +548,20 @@ public ASTNode visitSelectSubquery(final SelectSubqueryContext ctx) { return result; } + private void createSelectCombineClause(final SelectSubqueryContext ctx, final OracleSelectStatement result, final OracleSelectStatement left) { + CombineType combineType; + if (null != ctx.combineType().UNION() && null != ctx.combineType().ALL()) { + combineType = CombineType.UNION_ALL; + } else if (null != ctx.combineType().UNION()) { + combineType = CombineType.UNION; + } else if (null != ctx.combineType().INTERSECT()) { + combineType = CombineType.INTERSECT; + } else { + combineType = CombineType.MINUS; + } + result.setCombine(new CombineSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), left, combineType, (OracleSelectStatement) visit(ctx.selectSubquery(1)))); + } + @Override public ASTNode visitQueryBlock(final QueryBlockContext ctx) { OracleSelectStatement result = new OracleSelectStatement(); @@ -649,40 +664,11 @@ public ASTNode visitReferenceModel(final ReferenceModelContext ctx) { return new SubquerySegment(ctx.selectSubquery().start.getStartIndex(), ctx.selectSubquery().stop.getStopIndex(), subquery, getOriginalText(ctx.selectSubquery())); } - @Override - public ASTNode visitSelectCombineClause(final SelectCombineClauseContext ctx) { - OracleSelectStatement result = new OracleSelectStatement(); - OracleSelectStatement left = null != ctx.queryBlock() ? (OracleSelectStatement) visit(ctx.queryBlock()) : (OracleSelectStatement) visit(ctx.parenthesisSelectSubquery()); - if (null != ctx.selectSubquery()) { - result.setProjections(left.getProjections()); - result.setFrom(left.getFrom()); - setSelectCombineClause(ctx, result, left); - } - if (null != ctx.orderByClause()) { - result.setOrderBy((OrderBySegment) visit(ctx.orderByClause())); - } - return result; - } - @Override public ASTNode visitParenthesisSelectSubquery(final ParenthesisSelectSubqueryContext ctx) { return visit(ctx.selectSubquery()); } - private void setSelectCombineClause(final SelectCombineClauseContext ctx, final OracleSelectStatement result, final OracleSelectStatement left) { - CombineType combineType; - if (null != ctx.UNION(0) && null != ctx.ALL(0)) { - combineType = CombineType.UNION_ALL; - } else if (null != ctx.UNION(0)) { - combineType = CombineType.UNION; - } else if (null != ctx.INTERSECT(0)) { - combineType = CombineType.INTERSECT; - } else { - combineType = CombineType.MINUS; - } - result.setCombine(new CombineSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), left, combineType, (OracleSelectStatement) visit(ctx.selectSubquery(0)))); - } - @Override public ASTNode visitWithClause(final WithClauseContext ctx) { Collection commonTableExpressions = new LinkedList<>(); diff --git a/test/it/parser/src/main/resources/case/dml/select-combine.xml b/test/it/parser/src/main/resources/case/dml/select-combine.xml index e838b1bce4257..1e5a1f22f64e4 100644 --- a/test/it/parser/src/main/resources/case/dml/select-combine.xml +++ b/test/it/parser/src/main/resources/case/dml/select-combine.xml @@ -954,7 +954,7 @@ - + @@ -962,32 +962,32 @@ - - - - - - - - - + - - + + - + - - + + - + + + + + + + + + @@ -1112,152 +1112,152 @@ - - - - - - - - - - - - - - - - - - = - - - - - - - - - - - = - - - - - - - - - - - - - - - - - - - + - - - - - + + + + + - + - + - + = - + - + - + = - + - + - + - + - - - - - + + + + + - + - + - + = - + - + - + = - + - + - + - - - - - + + + + + + + + + + + + + + + + + + + + = + + + + + + + + + + + = + + + + + + + + + + + + + + + + + + + + + @@ -1469,7 +1469,7 @@ - + @@ -1478,30 +1478,30 @@ - - - - - - - - - - = - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + = + + + + + @@ -1555,37 +1555,37 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - < - - - + + + + + + + + + + + + + + + + + + + + + + + + + + < + + + @@ -1599,7 +1599,7 @@ - + @@ -1609,119 +1609,119 @@ - - - - - - - - - - = - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - = - - - - - - - - - + + + + - + - + - - - - + + + + - + - + - + - + - + - > + = - - - - + + + + - + - + - + - + - - - - - - - - + - < + = + + + + + + + + + + + + + + + + + + + + > + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + < + + +