diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 5b2099669ef1f..a5d8f2c357bb2 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -9,6 +9,7 @@ 1. [ISSUE #308](https://github.com/dangdangdotcom/sharding-jdbc/issues/308) 数据库原生的自增GeneratedKey的返回无效 1. [ISSUE #309](https://github.com/dangdangdotcom/sharding-jdbc/issues/310) 子查询中的orderby和groupby不列入解析上下文 1. [ISSUE #313](https://github.com/dangdangdotcom/sharding-jdbc/issues/313) 支持<>操作符 +1. [ISSUE #320](https://github.com/dangdangdotcom/sharding-jdbc/issues/320) GROUP BY + LIMIT的SQL改写错误 ## 1.5.0 diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/dialect/oracle/OracleSelectParser.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/dialect/oracle/OracleSelectParser.java index ba8faa8ae1462..2c5fc1bc35a4b 100755 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/dialect/oracle/OracleSelectParser.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/dialect/oracle/OracleSelectParser.java @@ -186,7 +186,7 @@ protected void parseGroupBy() { if (getSqlParser().skipIfEqual(DefaultKeyword.HAVING)) { throw new UnsupportedOperationException("Cannot support Having"); } - getSelectStatement().setGroupByLastPosition(getSqlParser().getLexer().getCurrentToken().getEndPosition()); + getSelectStatement().setGroupByLastPosition(getSqlParser().getLexer().getCurrentToken().getEndPosition() - getSqlParser().getLexer().getCurrentToken().getLiterals().length()); } } diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/statement/dql/select/AbstractSelectParser.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/statement/dql/select/AbstractSelectParser.java index 414c213c65587..6691e0b2e312b 100755 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/statement/dql/select/AbstractSelectParser.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/statement/dql/select/AbstractSelectParser.java @@ -252,7 +252,7 @@ protected void parseGroupBy() { if (sqlParser.skipIfEqual(DefaultKeyword.HAVING)) { throw new UnsupportedOperationException("Cannot support Having"); } - selectStatement.setGroupByLastPosition(sqlParser.getLexer().getCurrentToken().getEndPosition()); + selectStatement.setGroupByLastPosition(sqlParser.getLexer().getCurrentToken().getEndPosition() - getSqlParser().getLexer().getCurrentToken().getLiterals().length()); } else if (sqlParser.skipIfEqual(DefaultKeyword.HAVING)) { throw new UnsupportedOperationException("Cannot support Having"); } diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/SQLRewriteEngine.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/SQLRewriteEngine.java index dad55cb6ab54d..94617cc439c2b 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/SQLRewriteEngine.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/SQLRewriteEngine.java @@ -90,7 +90,7 @@ public SQLBuilder rewrite(final boolean isRewriteLimit) { } else if (each instanceof OffsetToken) { appendLimitOffsetToken(result, (OffsetToken) each, count, sqlTokens, isRewriteLimit); } else if (each instanceof OrderByToken) { - appendOrderByToken(result); + appendOrderByToken(result, count, sqlTokens); } count++; } @@ -147,7 +147,7 @@ private void appendLimitOffsetToken(final SQLBuilder sqlBuilder, final OffsetTok sqlBuilder.appendLiterals(originalSQL.substring(beginPosition, endPosition)); } - private void appendOrderByToken(final SQLBuilder sqlBuilder) { + private void appendOrderByToken(final SQLBuilder sqlBuilder, final int count, final List sqlTokens) { SelectStatement selectStatement = (SelectStatement) sqlStatement; StringBuilder orderByLiterals = new StringBuilder(" ORDER BY "); int i = 0; @@ -161,6 +161,9 @@ private void appendOrderByToken(final SQLBuilder sqlBuilder) { } orderByLiterals.append(" "); sqlBuilder.appendLiterals(orderByLiterals.toString()); + int beginPosition = ((SelectStatement) sqlStatement).getGroupByLastPosition(); + int endPosition = sqlTokens.size() - 1 == count ? originalSQL.length() : sqlTokens.get(count + 1).getBeginPosition(); + sqlBuilder.appendLiterals(originalSQL.substring(beginPosition, endPosition)); } /** diff --git a/sharding-jdbc-core/src/test/resources/integrate/assert/select_group_by.xml b/sharding-jdbc-core/src/test/resources/integrate/assert/select_group_by.xml index fd29c27de58e0..458e05ffb970a 100644 --- a/sharding-jdbc-core/src/test/resources/integrate/assert/select_group_by.xml +++ b/sharding-jdbc-core/src/test/resources/integrate/assert/select_group_by.xml @@ -42,4 +42,10 @@ + + + + + + diff --git a/sharding-jdbc-core/src/test/resources/integrate/dataset/db/expect/select_group_by/SelectLimit.xml b/sharding-jdbc-core/src/test/resources/integrate/dataset/db/expect/select_group_by/SelectLimit.xml new file mode 100644 index 0000000000000..a2119fbfd3698 --- /dev/null +++ b/sharding-jdbc-core/src/test/resources/integrate/dataset/db/expect/select_group_by/SelectLimit.xml @@ -0,0 +1,3 @@ + + + diff --git a/sharding-jdbc-core/src/test/resources/integrate/dataset/dbtbl/expect/select_group_by/SelectLimit.xml b/sharding-jdbc-core/src/test/resources/integrate/dataset/dbtbl/expect/select_group_by/SelectLimit.xml new file mode 100644 index 0000000000000..a2119fbfd3698 --- /dev/null +++ b/sharding-jdbc-core/src/test/resources/integrate/dataset/dbtbl/expect/select_group_by/SelectLimit.xml @@ -0,0 +1,3 @@ + + + diff --git a/sharding-jdbc-core/src/test/resources/integrate/dataset/masterslave/expect/select_group_by/SelectLimit.xml b/sharding-jdbc-core/src/test/resources/integrate/dataset/masterslave/expect/select_group_by/SelectLimit.xml new file mode 100644 index 0000000000000..a2119fbfd3698 --- /dev/null +++ b/sharding-jdbc-core/src/test/resources/integrate/dataset/masterslave/expect/select_group_by/SelectLimit.xml @@ -0,0 +1,3 @@ + + + diff --git a/sharding-jdbc-core/src/test/resources/integrate/dataset/tbl/expect/select_group_by/SelectLimit.xml b/sharding-jdbc-core/src/test/resources/integrate/dataset/tbl/expect/select_group_by/SelectLimit.xml new file mode 100644 index 0000000000000..a2119fbfd3698 --- /dev/null +++ b/sharding-jdbc-core/src/test/resources/integrate/dataset/tbl/expect/select_group_by/SelectLimit.xml @@ -0,0 +1,3 @@ + + + diff --git a/sharding-jdbc-core/src/test/resources/sql/select_group_by.xml b/sharding-jdbc-core/src/test/resources/sql/select_group_by.xml index 495fe4ce55a2d..d17fc4e46de1c 100644 --- a/sharding-jdbc-core/src/test/resources/sql/select_group_by.xml +++ b/sharding-jdbc-core/src/test/resources/sql/select_group_by.xml @@ -8,4 +8,5 @@ +