Skip to content

Commit

Permalink
Fix code format for #7353 (#7505)
Browse files Browse the repository at this point in the history
* Change BinaryOperationExpression attribute initialization method, from the set to the constructor

* Change BetweenExpression attribute initialization method, from the set to the constructor

* Change InExpression attribute initialization method, from the set to the constructor

* simple WhereClauseShardingConditionEngine

* fix

* fix

* fix
  • Loading branch information
jingshanglu committed Sep 18, 2020
1 parent 17d1375 commit d467b1a
Show file tree
Hide file tree
Showing 28 changed files with 336 additions and 508 deletions.
Expand Up @@ -33,7 +33,7 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.SimpleExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionBuildUtil;
import org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionBuilder;
import org.apache.shardingsphere.sql.parser.sql.common.util.ColumnExtractFromExpression;

import java.util.Collection;
Expand Down Expand Up @@ -69,8 +69,8 @@ public List<EncryptCondition> createEncryptConditions(final SQLStatementContext
}

ExpressionSegment expression = ((WhereAvailable) sqlStatementContext).getWhere().get().getExpr();
ExpressionBuildUtil expressionBuildUtil = new ExpressionBuildUtil(expression);
Collection<AndPredicate> andPredicates = new LinkedList<>(expressionBuildUtil.extractAndPredicates().getAndPredicates());
ExpressionBuilder expressionBuilder = new ExpressionBuilder(expression);
Collection<AndPredicate> andPredicates = new LinkedList<>(expressionBuilder.extractAndPredicates().getAndPredicates());
List<EncryptCondition> result = new LinkedList<>();
for (AndPredicate each : andPredicates) {
result.addAll(createEncryptConditions(sqlStatementContext, each));
Expand Down
Expand Up @@ -31,7 +31,7 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionBuildUtil;
import org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionBuilder;
import org.apache.shardingsphere.sql.parser.sql.common.util.ColumnExtractFromExpression;

import java.util.Collection;
Expand Down Expand Up @@ -59,8 +59,8 @@ public Collection<SubstitutableColumnNameToken> generateSQLTokens(final SQLState
Preconditions.checkState(((WhereAvailable) sqlStatementContext).getWhere().isPresent());
Collection<SubstitutableColumnNameToken> result = new LinkedHashSet<>();
ExpressionSegment expression = ((WhereAvailable) sqlStatementContext).getWhere().get().getExpr();
ExpressionBuildUtil util = new ExpressionBuildUtil(expression);
Collection<AndPredicate> andPredicates = new LinkedList<>(util.extractAndPredicates().getAndPredicates());
ExpressionBuilder expressionBuilder = new ExpressionBuilder(expression);
Collection<AndPredicate> andPredicates = new LinkedList<>(expressionBuilder.extractAndPredicates().getAndPredicates());
for (AndPredicate each : andPredicates) {
result.addAll(generateSQLTokens(sqlStatementContext, each));
}
Expand Down
Expand Up @@ -30,7 +30,7 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.SimpleExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionBuildUtil;
import org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionBuilder;
import org.apache.shardingsphere.sql.parser.sql.common.util.ColumnExtractFromExpression;

import java.util.Collection;
Expand Down Expand Up @@ -61,8 +61,8 @@ public Optional<ShadowCondition> createShadowCondition(final SQLStatementContext
return Optional.empty();
}
ExpressionSegment expression = ((WhereAvailable) sqlStatementContext).getWhere().get().getExpr();
ExpressionBuildUtil util = new ExpressionBuildUtil(expression);
Collection<AndPredicate> andPredicates = new LinkedList<>(util.extractAndPredicates().getAndPredicates());
ExpressionBuilder expressionBuilder = new ExpressionBuilder(expression);
Collection<AndPredicate> andPredicates = new LinkedList<>(expressionBuilder.extractAndPredicates().getAndPredicates());
for (AndPredicate each : andPredicates) {
Optional<ShadowCondition> condition = createShadowCondition(each);
if (condition.isPresent()) {
Expand Down
Expand Up @@ -29,7 +29,7 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionBuildUtil;
import org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionBuilder;
import org.apache.shardingsphere.sql.parser.sql.common.util.ColumnExtractFromExpression;

import java.util.Collection;
Expand All @@ -53,8 +53,8 @@ public Collection<SQLToken> generateSQLTokens(final SQLStatementContext sqlState
Preconditions.checkState(((WhereAvailable) sqlStatementContext).getWhere().isPresent());
Collection<SQLToken> result = new LinkedList<>();
ExpressionSegment expression = ((WhereAvailable) sqlStatementContext).getWhere().get().getExpr();
ExpressionBuildUtil util = new ExpressionBuildUtil(expression);
Collection<AndPredicate> andPredicates = new LinkedList<>(util.extractAndPredicates().getAndPredicates());
ExpressionBuilder expressionBuilder = new ExpressionBuilder(expression);
Collection<AndPredicate> andPredicates = new LinkedList<>(expressionBuilder.extractAndPredicates().getAndPredicates());
for (AndPredicate each : andPredicates) {
result.addAll(generateSQLTokens(((WhereAvailable) sqlStatementContext).getWhere().get(), each));
}
Expand Down
Expand Up @@ -32,7 +32,7 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionBuildUtil;
import org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionBuilder;

import java.util.Collection;
import java.util.LinkedList;
Expand Down Expand Up @@ -72,8 +72,8 @@ public boolean isShadow() {
return false;
}
ExpressionSegment expression = whereSegment.get().getExpr();
ExpressionBuildUtil util = new ExpressionBuildUtil(expression);
Collection<AndPredicate> andPredicates = new LinkedList<>(util.extractAndPredicates().getAndPredicates());
ExpressionBuilder expressionBuilder = new ExpressionBuilder(expression);
Collection<AndPredicate> andPredicates = new LinkedList<>(expressionBuilder.extractAndPredicates().getAndPredicates());
for (AndPredicate andPredicate : andPredicates) {
if (judgePredicateSegments(andPredicate.getPredicates())) {
return true;
Expand Down
Expand Up @@ -72,24 +72,9 @@ public void isShadowSQLInLiteralExpression() {
}

private SelectStatementContext selectStatementContext() {
BinaryOperationExpression left = new BinaryOperationExpression();
left.setLeft(new ColumnSegment(0, 0, new IdentifierValue("id")));
left.setRight(new ParameterMarkerExpressionSegment(0, 0, 0));
left.setText("id=?");
left.setOperator("=");

BinaryOperationExpression right = new BinaryOperationExpression();
right.setLeft(new ColumnSegment(0, 0, new IdentifierValue("shadow")));
right.setRight(new LiteralExpressionSegment(45, 48, "true"));
right.setText("shadow=true");
right.setOperator("=");

BinaryOperationExpression binaryOperationExpression = new BinaryOperationExpression();
binaryOperationExpression.setLeft(left);
binaryOperationExpression.setRight(right);
binaryOperationExpression.setOperator("and");
binaryOperationExpression.setText("id=? and shadow=true");

BinaryOperationExpression left = new BinaryOperationExpression(0, 0, new ColumnSegment(0, 0, new IdentifierValue("id")), new ParameterMarkerExpressionSegment(0, 0, 0), "=", "id=?");
BinaryOperationExpression right = new BinaryOperationExpression(0, 0, new ColumnSegment(0, 0, new IdentifierValue("shadow")), new LiteralExpressionSegment(45, 48, "true"), "=", "shadow=true");
BinaryOperationExpression binaryOperationExpression = new BinaryOperationExpression(0, 0, left, right, "and", "id=? and shadow=true");
WhereSegment whereSegment = new WhereSegment(0, 0, binaryOperationExpression);
SelectStatement selectStatement = new SelectStatement();
selectStatement.setWhere(whereSegment);
Expand Down
Expand Up @@ -109,10 +109,9 @@ private void judgeForInsert(final InsertStatement insertStatement) {

@Test
public void judgeForWhereSegment() {
BinaryOperationExpression expression = new BinaryOperationExpression();
expression.setLeft(new ColumnSegment(0, 0, new IdentifierValue("shadow")));
expression.setRight(new LiteralExpressionSegment(0, 0, true));
expression.setOperator("=");
ColumnSegment left = new ColumnSegment(0, 0, new IdentifierValue("shadow"));
LiteralExpressionSegment right = new LiteralExpressionSegment(0, 0, true);
BinaryOperationExpression expression = new BinaryOperationExpression(0, 0, left, right, "=", null);
WhereSegment whereSegment = new WhereSegment(0, 0, expression);
SelectStatement selectStatement = new SelectStatement();
selectStatement.setWhere(whereSegment);
Expand All @@ -123,8 +122,9 @@ public void judgeForWhereSegment() {
SelectStatementContext selectStatementContext = new SelectStatementContext(schemaMetaData, Collections.emptyList(), selectStatement);
SimpleShadowDataSourceJudgeEngine simpleShadowDataSourceRouter = new SimpleShadowDataSourceJudgeEngine(shadowRule, selectStatementContext);
assertTrue("should be shadow", simpleShadowDataSourceRouter.isShadow());
expression.setLeft(new ColumnSegment(0, 0, new IdentifierValue("shadow")));
expression.setRight(new LiteralExpressionSegment(0, 0, false));
expression = new BinaryOperationExpression(0, 0, new ColumnSegment(0, 0, new IdentifierValue("shadow")), new LiteralExpressionSegment(0, 0, false), "=", null);
whereSegment = new WhereSegment(0, 0, expression);
selectStatement.setWhere(whereSegment);
projectionsSegment.getProjections().clear();
projectionsSegment.getProjections().addAll(Collections.singletonList(new ExpressionProjectionSegment(0, 0, "false")));
assertFalse("should not be shadow", simpleShadowDataSourceRouter.isShadow());
Expand Down
Expand Up @@ -33,14 +33,12 @@
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.binder.type.WhereAvailable;
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.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.InExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionBuildUtil;
import org.apache.shardingsphere.sql.parser.sql.common.util.ColumnExtractFromExpression;
import org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionBuilder;
import org.apache.shardingsphere.sql.parser.sql.common.util.SafeNumberOperationUtils;
import org.apache.shardingsphere.sql.parser.sql.common.util.WhereSegmentExtractUtils;

Expand Down Expand Up @@ -94,8 +92,8 @@ public List<ShardingCondition> createShardingConditions(final SQLStatementContex
private Collection<ShardingCondition> createShardingConditions(final SQLStatementContext<?> sqlStatementContext, final ExpressionSegment expressionSegment, final List<Object> parameters) {
Collection<ShardingCondition> result = new LinkedList<>();

ExpressionBuildUtil util = new ExpressionBuildUtil(expressionSegment);
Collection<AndPredicate> andPredicates = new LinkedList<>(util.extractAndPredicates().getAndPredicates());
ExpressionBuilder expressionBuilder = new ExpressionBuilder(expressionSegment);
Collection<AndPredicate> andPredicates = new LinkedList<>(expressionBuilder.extractAndPredicates().getAndPredicates());
for (AndPredicate each : andPredicates) {
Map<Column, Collection<RouteValue>> routeValueMap = createRouteValueMap(sqlStatementContext, each, parameters);
if (routeValueMap.isEmpty()) {
Expand All @@ -108,34 +106,17 @@ private Collection<ShardingCondition> createShardingConditions(final SQLStatemen

private Map<Column, Collection<RouteValue>> createRouteValueMap(final SQLStatementContext<?> sqlStatementContext, final AndPredicate expressions, final List<Object> parameters) {
Map<Column, Collection<RouteValue>> result = new HashMap<>();

for (ExpressionSegment each : expressions.getPredicates()) {
Optional<RouteValue> routeValue = Optional.empty();
Column column = null;
if (each instanceof BinaryOperationExpression && ((BinaryOperationExpression) each).getLeft() instanceof ColumnSegment) {
ColumnSegment columnSegment = (ColumnSegment) ((BinaryOperationExpression) each).getLeft();
Optional<String> tableName = sqlStatementContext.getTablesContext().findTableName(columnSegment, schemaMetaData);
if (tableName.isPresent() && shardingRule.isShardingColumn(columnSegment.getIdentifier().getValue(), tableName.get())) {
column = new Column(columnSegment.getIdentifier().getValue(), tableName.get());
routeValue = ConditionValueGeneratorFactory.generate(each, column, parameters);
}
Optional<ColumnSegment> columnSegment = ColumnExtractFromExpression.extract(each);
if (!columnSegment.isPresent()) {
continue;
}
if (each instanceof InExpression && ((InExpression) each).getLeft() instanceof ColumnSegment) {
ColumnSegment columnSegment = (ColumnSegment) ((InExpression) each).getLeft();
Optional<String> tableName = sqlStatementContext.getTablesContext().findTableName(columnSegment, schemaMetaData);
if (tableName.isPresent() && shardingRule.isShardingColumn(columnSegment.getIdentifier().getValue(), tableName.get())) {
column = new Column(columnSegment.getIdentifier().getValue(), tableName.get());
routeValue = ConditionValueGeneratorFactory.generate(each, column, parameters);
}
}
if (each instanceof BetweenExpression && ((BetweenExpression) each).getLeft() instanceof ColumnSegment) {
ColumnSegment columnSegment = (ColumnSegment) ((BetweenExpression) each).getLeft();
Optional<String> tableName = sqlStatementContext.getTablesContext().findTableName(columnSegment, schemaMetaData);
if (tableName.isPresent() && shardingRule.isShardingColumn(columnSegment.getIdentifier().getValue(), tableName.get())) {
column = new Column(columnSegment.getIdentifier().getValue(), tableName.get());
routeValue = ConditionValueGeneratorFactory.generate(each, column, parameters);
}
Optional<String> tableName = sqlStatementContext.getTablesContext().findTableName(columnSegment.get(), schemaMetaData);
if (!(tableName.isPresent() && shardingRule.isShardingColumn(columnSegment.get().getIdentifier().getValue(), tableName.get()))) {
continue;
}
Column column = new Column(columnSegment.get().getIdentifier().getValue(), tableName.get());
Optional<RouteValue> routeValue = ConditionValueGeneratorFactory.generate(each, column, parameters);
if (routeValue.isPresent()) {
if (!result.containsKey(column)) {
Collection<RouteValue> routeValues = new LinkedList<>();
Expand Down

0 comments on commit d467b1a

Please sign in to comment.