Skip to content

Commit

Permalink
fixed # 292
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jul 14, 2017
1 parent ef14a22 commit b0f79f0
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 24 deletions.
6 changes: 6 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ next = "/00-overview/contribution/"

+++

## 1.5.0.M3

### 缺陷修正

1. [ISSUE #292](https://github.com/dangdangdotcom/sharding-jdbc/issues/292) 内存方式处理GROUP BY语句如有分页信息则需改写

## 1.5.0.M2

### 功能提升
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.dangdang.ddframe.rdb.sharding.parsing.parser.statement.SQLStatement;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.statement.select.SelectStatement;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.OffsetToken;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.RowCountToken;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.TableToken;
import com.dangdang.ddframe.rdb.sharding.util.SQLUtil;
import com.google.common.base.Optional;
Expand Down Expand Up @@ -350,7 +351,10 @@ private void parseRowNumberCondition(final SelectStatement selectStatement) {
}
if (Symbol.LT == symbol || Symbol.LT_EQ == symbol) {
if (sqlExpression instanceof SQLNumberExpression) {
selectStatement.getLimit().setRowCount(new LimitValue(((SQLNumberExpression) sqlExpression).getNumber().intValue(), -1));
int rowCount = ((SQLNumberExpression) sqlExpression).getNumber().intValue();
selectStatement.getLimit().setRowCount(new LimitValue(rowCount, -1));
selectStatement.getSqlTokens().add(
new RowCountToken(getLexer().getCurrentToken().getEndPosition() - String.valueOf(rowCount).length() - getLexer().getCurrentToken().getLiterals().length(), rowCount));
} else if (sqlExpression instanceof SQLPlaceholderExpression) {
selectStatement.getLimit().setRowCount(new LimitValue(-1, ((SQLPlaceholderExpression) sqlExpression).getIndex()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ public int getRowCountValue() {
*
* @param parameters 参数
* @param isRewrite 是否重写参数
* @param isFetchAll 是否获取所有数据
*/
public void processParameters(final List<Object> parameters, final boolean isRewrite) {
public void processParameters(final List<Object> parameters, final boolean isRewrite, final boolean isFetchAll) {
fill(parameters);
if (isRewrite) {
rewrite(parameters);
rewrite(parameters, isFetchAll);
}
}

Expand All @@ -92,10 +93,12 @@ private void fill(final List<Object> parameters) {
}
}

private void rewrite(final List<Object> parameters) {
private void rewrite(final List<Object> parameters, final boolean isFetchAll) {
int rewriteOffset = 0;
int rewriteRowCount;
if (rowCountRewriteFlag) {
if (isFetchAll) {
rewriteRowCount = Integer.MAX_VALUE;
} else if (rowCountRewriteFlag) {
rewriteRowCount = null == rowCount ? -1 : getOffsetValue() + rowCount.getValue();
} else {
rewriteRowCount = rowCount.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.dangdang.ddframe.rdb.sharding.parsing.parser.expression.SQLNumberExpression;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.expression.SQLPlaceholderExpression;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.statement.select.SelectStatement;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.RowCountToken;
import com.google.common.base.Optional;

/**
Expand Down Expand Up @@ -68,11 +69,14 @@ public void parseTop(final SelectStatement selectStatement) {
skipIfEqual(Symbol.LEFT_PAREN);
SQLExpression sqlExpression = parseExpression();
skipIfEqual(Symbol.RIGHT_PAREN);
LimitValue rowCount;
LimitValue rowCountValue;
if (sqlExpression instanceof SQLNumberExpression) {
rowCount = new LimitValue(((SQLNumberExpression) sqlExpression).getNumber().intValue(), -1);
int rowCount = ((SQLNumberExpression) sqlExpression).getNumber().intValue();
rowCountValue = new LimitValue(rowCount, -1);
selectStatement.getSqlTokens().add(
new RowCountToken(getLexer().getCurrentToken().getEndPosition() - String.valueOf(rowCount).length() - getLexer().getCurrentToken().getLiterals().length(), rowCount));
} else if (sqlExpression instanceof SQLPlaceholderExpression) {
rowCount = new LimitValue(-1, ((SQLPlaceholderExpression) sqlExpression).getIndex());
rowCountValue = new LimitValue(-1, ((SQLPlaceholderExpression) sqlExpression).getIndex());
} else {
throw new SQLParsingException(getLexer());
}
Expand All @@ -81,10 +85,10 @@ public void parseTop(final SelectStatement selectStatement) {
}
if (null == selectStatement.getLimit()) {
Limit limit = new Limit(false);
limit.setRowCount(rowCount);
limit.setRowCount(rowCountValue);
selectStatement.setLimit(limit);
} else {
selectStatement.getLimit().setRowCount(rowCount);
selectStatement.getLimit().setRowCount(rowCountValue);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.dangdang.ddframe.rdb.sharding.routing.type.complex.CartesianTableReference;
import com.google.common.base.Optional;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
Expand All @@ -53,20 +52,13 @@ public final class SQLRewriteEngine {

private final List<SQLToken> sqlTokens = new LinkedList<>();

private final Collection<String> tableNames;

private final Limit limit;
private final SQLStatement sqlStatement;

public SQLRewriteEngine(final ShardingRule shardingRule, final String originalSQL, final SQLStatement sqlStatement) {
this.shardingRule = shardingRule;
this.originalSQL = originalSQL;
this.sqlStatement = sqlStatement;
sqlTokens.addAll(sqlStatement.getSqlTokens());
tableNames = sqlStatement.getTables().getTableNames();
if (sqlStatement instanceof SelectStatement) {
limit = ((SelectStatement) sqlStatement).getLimit();
} else {
limit = null;
}
}

/**
Expand Down Expand Up @@ -112,7 +104,7 @@ public int compare(final SQLToken o1, final SQLToken o2) {
}

private void appendTableToken(final SQLBuilder sqlBuilder, final TableToken tableToken, final int count, final List<SQLToken> sqlTokens) {
String tableName = tableNames.contains(tableToken.getTableName()) ? tableToken.getTableName() : tableToken.getOriginalLiterals();
String tableName = sqlStatement.getTables().getTableNames().contains(tableToken.getTableName()) ? tableToken.getTableName() : tableToken.getOriginalLiterals();
sqlBuilder.appendTable(tableName);
int beginPosition = tableToken.getBeginPosition() + tableToken.getOriginalLiterals().length();
int endPosition = sqlTokens.size() - 1 == count ? originalSQL.length() : sqlTokens.get(count + 1).getBeginPosition();
Expand All @@ -130,7 +122,15 @@ private void appendItemsToken(final SQLBuilder sqlBuilder, final ItemsToken item
}

private void appendLimitRowCount(final SQLBuilder sqlBuilder, final RowCountToken rowCountToken, final int count, final List<SQLToken> sqlTokens, final boolean isRewrite) {
sqlBuilder.appendLiterals(isRewrite ? String.valueOf(rowCountToken.getRowCount() + limit.getOffsetValue()) : String.valueOf(rowCountToken.getRowCount()));
SelectStatement selectStatement = (SelectStatement) sqlStatement;
Limit limit = selectStatement.getLimit();
if (!isRewrite) {
sqlBuilder.appendLiterals(String.valueOf(rowCountToken.getRowCount()));
} else if ((!selectStatement.getGroupByItems().isEmpty() || !selectStatement.getAggregationSelectItems().isEmpty()) && !selectStatement.isSameGroupByAndOrderByItems()) {
sqlBuilder.appendLiterals(String.valueOf(Integer.MAX_VALUE));
} else {
sqlBuilder.appendLiterals(String.valueOf(limit.isRowCountRewriteFlag() ? rowCountToken.getRowCount() + limit.getOffsetValue() : rowCountToken.getRowCount()));
}
int beginPosition = rowCountToken.getBeginPosition() + String.valueOf(rowCountToken.getRowCount()).length();
int endPosition = sqlTokens.size() - 1 == count ? originalSQL.length() : sqlTokens.get(count + 1).getBeginPosition();
sqlBuilder.appendLiterals(originalSQL.substring(beginPosition, endPosition));
Expand Down Expand Up @@ -189,7 +189,7 @@ private Map<String, String> getTableTokens(final CartesianTableReference cartesi

private Map<String, String> getBindingTableTokens(final TableUnit tableUnit, final BindingTableRule bindingTableRule) {
Map<String, String> result = new HashMap<>();
for (String eachTable : tableNames) {
for (String eachTable : sqlStatement.getTables().getTableNames()) {
if (!eachTable.equalsIgnoreCase(tableUnit.getLogicTableName()) && bindingTableRule.hasLogicTable(eachTable)) {
result.put(eachTable, bindingTableRule.getBindingActualTable(tableUnit.getDataSourceName(), eachTable, tableUnit.getActualTableName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public SQLRouteResult route(final String logicSQL, final List<Object> parameters
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, logicSQL, sqlStatement);
boolean isSingleRouting = routingResult.isSingleRouting();
if (sqlStatement instanceof SelectStatement && null != ((SelectStatement) sqlStatement).getLimit()) {
((SelectStatement) sqlStatement).getLimit().processParameters(parameters, !isSingleRouting);
SelectStatement selectStatement = (SelectStatement) sqlStatement;
boolean isNeedFetchAll = (!selectStatement.getGroupByItems().isEmpty() || !selectStatement.getAggregationSelectItems().isEmpty()) && !selectStatement.isSameGroupByAndOrderByItems();
selectStatement.getLimit().processParameters(parameters, !isSingleRouting, isNeedFetchAll);
}
SQLBuilder sqlBuilder = rewriteEngine.rewrite(!isSingleRouting);
if (routingResult instanceof CartesianRoutingResult) {
Expand Down

0 comments on commit b0f79f0

Please sign in to comment.