Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add oracle select union statement parse test #29015

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand All @@ -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();
Expand Down Expand Up @@ -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<CommonTableExpressionSegment> commonTableExpressions = new LinkedList<>();
Expand Down
Loading