Skip to content

Commit

Permalink
Merge c84527a into 2fd13d1
Browse files Browse the repository at this point in the history
  • Loading branch information
jingshanglu committed Sep 28, 2020
2 parents 2fd13d1 + c84527a commit da3ad56
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 29 deletions.
Expand Up @@ -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" />
Expand Down
Expand Up @@ -314,11 +314,10 @@ triggerOrder
;

expr
: expr logicalOperator expr
: booleanPrimary
| expr logicalOperator expr
| expr XOR expr
| notOperator_ expr
| LP_ expr RP_
| booleanPrimary
;

logicalOperator
Expand Down Expand Up @@ -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_
Expand Down
Expand Up @@ -73,6 +73,7 @@
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;
Expand Down Expand Up @@ -253,9 +254,6 @@ public final ASTNode visitExpr(final ExprContext ctx) {
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));
Expand All @@ -272,10 +270,7 @@ public final ASTNode visitExpr(final ExprContext ctx) {

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;
}

Expand Down Expand Up @@ -416,7 +411,10 @@ private ExpressionSegment createLiteralExpression(final LiteralsContext context)
@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()) {
Expand All @@ -437,6 +435,17 @@ public final ASTNode visitSimpleExpr(final SimpleExprContext ctx) {
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);
}

Expand Down
Expand Up @@ -245,10 +245,7 @@ public final ASTNode visitExpr(final ExprContext ctx) {
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;
}

Expand Down
Expand Up @@ -238,10 +238,7 @@ public final ASTNode visitExpr(final ExprContext ctx) {
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;
}

Expand Down
Expand Up @@ -252,10 +252,7 @@ public final ASTNode visitExpr(final ExprContext ctx) {
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;
}

Expand Down
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -107,6 +108,9 @@ private void extractTablesFromExpression(final ExpressionSegment expressionSegme
extractTablesFromExpression(each);
}
}
if (expressionSegment instanceof ExistsSubqueryExpression) {
extractTablesFromSelect(((ExistsSubqueryExpression) expressionSegment).getSubquery().getSelect());
}
if (expressionSegment instanceof BetweenExpression) {
extractTablesFromExpression(((BetweenExpression) expressionSegment).getLeft());
extractTablesFromExpression(((BetweenExpression) expressionSegment).getBetweenExpr());
Expand Down
@@ -0,0 +1,37 @@
/*
* 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;
}
Expand Up @@ -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;
}

0 comments on commit da3ad56

Please sign in to comment.