未验证 提交 ebb3a5bb 编写于 作者: J JingShang Lu 提交者: GitHub

add support parser for ExistsSubqueryExpression (#7650)

上级 c7621ab9
......@@ -27,6 +27,12 @@
<output sql="SELECT * FROM t_account_0 WHERE account_id = 100" />
</rewrite-assertion>
<rewrite-assertion id="select_with_not_exsist" db-type="MySQL">
<input sql="SELECT * FROM t_account a WHERE not exists (select * from t_account_detail where a.account_id=account_id and account_id=1000) and account_id = 100" />
<output sql="SELECT * FROM t_account_0 a WHERE not exists (select * from t_account_detail_0 where a.account_id=account_id and account_id=1000) and account_id = 100" />
<output sql="SELECT * FROM t_account_1 a WHERE not exists (select * from t_account_detail_1 where a.account_id=account_id and account_id=1000) and account_id = 100" />
</rewrite-assertion>
<rewrite-assertion id="select_with_sum_fun">
<input sql="SELECT SUM(DISTINCT account_id), SUM(account_id) FROM t_account WHERE account_id = 100" />
<output sql="SELECT SUM(DISTINCT account_id), SUM(account_id) FROM t_account_0 WHERE account_id = 100" />
......
......@@ -314,11 +314,10 @@ triggerOrder
;
expr
: expr logicalOperator expr
: booleanPrimary
| expr logicalOperator expr
| expr XOR expr
| notOperator_ expr
| LP_ expr RP_
| booleanPrimary
;
logicalOperator
......@@ -377,7 +376,7 @@ simpleExpr
| simpleExpr COLLATE (STRING_ | identifier)
| variable
| simpleExpr OR_ simpleExpr
| (PLUS_ | MINUS_ | TILDE_ | NOT_ | BINARY) simpleExpr
| (PLUS_ | MINUS_ | TILDE_ | notOperator_ | BINARY) simpleExpr
| ROW? LP_ expr (COMMA_ expr)* RP_
| EXISTS? subquery
| LBE_ identifier expr RBE_
......
......@@ -73,6 +73,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSe
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
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;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.InExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ListExpression;
......@@ -253,9 +254,6 @@ public abstract class MySQLVisitor extends MySQLStatementBaseVisitor<ASTNode> {
if (null != ctx.booleanPrimary()) {
return visit(ctx.booleanPrimary());
}
if (null != ctx.LP_()) {
return visit(ctx.expr(0));
}
if (null != ctx.XOR()) {
ExpressionSegment left = (ExpressionSegment) visit(ctx.expr(0));
ExpressionSegment right = (ExpressionSegment) visit(ctx.expr(1));
......@@ -272,10 +270,7 @@ public abstract class MySQLVisitor extends MySQLStatementBaseVisitor<ASTNode> {
return result;
}
NotExpression result = new NotExpression();
result.setStartIndex(ctx.start.getStartIndex());
result.setStopIndex(ctx.stop.getStopIndex());
result.setExpression((ExpressionSegment) visit(ctx.expr(0)));
NotExpression result = new NotExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), (ExpressionSegment) visit(ctx.expr(0)));
return result;
}
......@@ -416,7 +411,10 @@ public abstract class MySQLVisitor extends MySQLStatementBaseVisitor<ASTNode> {
@Override
public final ASTNode visitSimpleExpr(final SimpleExprContext ctx) {
if (null != ctx.subquery()) {
SubquerySegment subquerySegment = new SubquerySegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), (MySQLSelectStatement) visit(ctx.subquery()));
SubquerySegment subquerySegment = new SubquerySegment(ctx.subquery().getStart().getStartIndex(), ctx.subquery().getStop().getStopIndex(), (MySQLSelectStatement) visit(ctx.subquery()));
if (null != ctx.EXISTS()) {
return new ExistsSubqueryExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), subquerySegment);
}
return new SubqueryExpressionSegment(subquerySegment);
}
if (null != ctx.parameterMarker()) {
......@@ -437,6 +435,17 @@ public abstract class MySQLVisitor extends MySQLStatementBaseVisitor<ASTNode> {
if (null != ctx.matchExpression_()) {
return visit(ctx.matchExpression_());
}
if (null != ctx.notOperator_()) {
ASTNode expression = visit(ctx.simpleExpr(0));
if (expression instanceof ExistsSubqueryExpression) {
((ExistsSubqueryExpression) expression).setNot(true);
return expression;
}
return new NotExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), (ExpressionSegment) expression);
}
if (null != ctx.LP_() && 1 == ctx.expr().size()) {
return visit(ctx.expr(0));
}
return visitRemainSimpleExpr(ctx);
}
......
......@@ -245,10 +245,7 @@ public abstract class OracleVisitor extends OracleStatementBaseVisitor<ASTNode>
BinaryOperationExpression result = new BinaryOperationExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), left, right, operator, text);
return result;
}
NotExpression result = new NotExpression();
result.setStartIndex(ctx.start.getStartIndex());
result.setStopIndex(ctx.stop.getStopIndex());
result.setExpression((ExpressionSegment) visit(ctx.expr(0)));
NotExpression result = new NotExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), (ExpressionSegment) visit(ctx.expr(0)));
return result;
}
......
......@@ -238,10 +238,7 @@ public abstract class SQL92Visitor extends SQL92StatementBaseVisitor<ASTNode> {
BinaryOperationExpression result = new BinaryOperationExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), left, right, operator, text);
return result;
}
NotExpression result = new NotExpression();
result.setStartIndex(ctx.start.getStartIndex());
result.setStopIndex(ctx.stop.getStopIndex());
result.setExpression((ExpressionSegment) visit(ctx.expr(0)));
NotExpression result = new NotExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), (ExpressionSegment) visit(ctx.expr(0)));
return result;
}
......
......@@ -252,10 +252,7 @@ public abstract class SQLServerVisitor extends SQLServerStatementBaseVisitor<AST
BinaryOperationExpression result = new BinaryOperationExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), left, right, operator, text);
return result;
}
NotExpression result = new NotExpression();
result.setStartIndex(ctx.start.getStartIndex());
result.setStopIndex(ctx.stop.getStopIndex());
result.setExpression((ExpressionSegment) visit(ctx.expr(0)));
NotExpression result = new NotExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), (ExpressionSegment) visit(ctx.expr(0)));
return result;
}
......
......@@ -21,6 +21,7 @@ import lombok.Getter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
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;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.InExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ListExpression;
......@@ -107,6 +108,9 @@ public final class TableExtractor {
extractTablesFromExpression(each);
}
}
if (expressionSegment instanceof ExistsSubqueryExpression) {
extractTablesFromSelect(((ExistsSubqueryExpression) expressionSegment).getSubquery().getSelect());
}
if (expressionSegment instanceof BetweenExpression) {
extractTablesFromExpression(((BetweenExpression) expressionSegment).getLeft());
extractTablesFromExpression(((BetweenExpression) expressionSegment).getBetweenExpr());
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment;
@RequiredArgsConstructor
@Getter
public class ExistsSubqueryExpression implements ExpressionSegment {
private final int startIndex;
private final int stopIndex;
private final SubquerySegment subquery;
@Setter
private boolean not;
}
......@@ -18,15 +18,15 @@
package org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr;
import lombok.Getter;
import lombok.Setter;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Getter
@Setter
public final class NotExpression implements ExpressionSegment {
private int startIndex;
private final int startIndex;
private int stopIndex;
private final int stopIndex;
private ExpressionSegment expression;
private final ExpressionSegment expression;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册