Skip to content

Commit

Permalink
fixed #291
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jul 17, 2017
1 parent a19caef commit 28cae13
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 10 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.M4

### 功能提升

1. [ISSUE #291](https://github.com/dangdangdotcom/sharding-jdbc/issues/291) 用流式方式处理仅包含GroupBy的SQL

## 1.5.0.M3

### 里程碑
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ protected void parseGroupBy() {
if (getSqlParser().skipIfEqual(DefaultKeyword.HAVING)) {
throw new UnsupportedOperationException("Cannot support Having");
}
getSelectStatement().setGroupByLastPosition(getSqlParser().getLexer().getCurrentToken().getEndPosition());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.dangdang.ddframe.rdb.sharding.parsing.parser.expression.SQLPropertyExpression;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.statement.SQLStatementParser;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.ItemsToken;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.OrderByToken;
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 @@ -79,6 +80,7 @@ public final SelectStatement parse() {
selectStatement.getOrderByItems().addAll(parseOrderBy());
customizedSelect();
appendDerivedColumns();
appendDerivedOrderBy();
return selectStatement;
}

Expand Down Expand Up @@ -242,6 +244,7 @@ protected void parseGroupBy() {
if (sqlParser.skipIfEqual(DefaultKeyword.HAVING)) {
throw new UnsupportedOperationException("Cannot support Having");
}
selectStatement.setGroupByLastPosition(sqlParser.getLexer().getCurrentToken().getEndPosition());
} else if (sqlParser.skipIfEqual(DefaultKeyword.HAVING)) {
throw new UnsupportedOperationException("Cannot support Having");
}
Expand Down Expand Up @@ -411,4 +414,11 @@ private boolean isContainsItem(final OrderItem orderItem) {
}
return false;
}

private void appendDerivedOrderBy() {
if (!getSelectStatement().getGroupByItems().isEmpty() && getSelectStatement().getOrderByItems().isEmpty()) {
getSelectStatement().getOrderByItems().addAll(getSelectStatement().getGroupByItems());
getSelectStatement().getSqlTokens().add(new OrderByToken(getSelectStatement().getGroupByLastPosition()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public final class SelectStatement extends AbstractSQLStatement {

private int selectListLastPosition;

private int groupByLastPosition;

private final List<SelectItem> items = new LinkedList<>();

private final List<OrderItem> groupByItems = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.parsing.parser.token;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* 排序标记对象.
*
* @author zhangliang
*/
@RequiredArgsConstructor
@Getter
public final class OrderByToken implements SQLToken {

private final int beginPosition;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

import com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.OrderItem;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.limit.Limit;
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.ItemsToken;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.OffsetToken;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.OrderByToken;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.RowCountToken;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.SQLToken;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.TableToken;
Expand Down Expand Up @@ -87,6 +89,8 @@ public SQLBuilder rewrite(final boolean isRewriteLimit) {
appendLimitRowCount(result, (RowCountToken) each, count, sqlTokens, isRewriteLimit);
} else if (each instanceof OffsetToken) {
appendLimitOffsetToken(result, (OffsetToken) each, count, sqlTokens, isRewriteLimit);
} else if (each instanceof OrderByToken) {
appendOrderByToken(result);
}
count++;
}
Expand Down Expand Up @@ -143,6 +147,22 @@ private void appendLimitOffsetToken(final SQLBuilder sqlBuilder, final OffsetTok
sqlBuilder.appendLiterals(originalSQL.substring(beginPosition, endPosition));
}

private void appendOrderByToken(final SQLBuilder sqlBuilder) {
SelectStatement selectStatement = (SelectStatement) sqlStatement;
StringBuilder orderByLiterals = new StringBuilder(" ORDER BY ");
int i = 0;
for (OrderItem each : selectStatement.getOrderByItems()) {
if (0 == i) {
orderByLiterals.append(each.getColumnLabel()).append(" ").append(each.getType().name());
} else {
orderByLiterals.append(",").append(each.getColumnLabel()).append(" ").append(each.getType().name());
}
i++;
}
orderByLiterals.append(" ");
sqlBuilder.appendLiterals(orderByLiterals.toString());
}

/**
* 生成SQL语句.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@

@RunWith(Suite.class)
@Suite.SuiteClasses({
ShardingTablesOnlyForPreparedStatementWithGroupByTest.class,
ShardingTablesOnlyForPreparedStatementWithSelectTest.class,
ShardingTablesOnlyForPreparedStatementWithAggregateTest.class,
ShardingTablesOnlyForPreparedStatementWithDMLTest.class,
ShardingTablesOnlyForStatementWithAggregateTest.class,
ShardingTablesOnlyForStatementWithDMLTest.class,
ShardingTablesOnlyForStatementWithSelectTest.class
ShardingTablesOnlyForPreparedStatementWithGroupByTest.class,
ShardingTablesOnlyForPreparedStatementWithSelectTest.class,
ShardingTablesOnlyForPreparedStatementWithAggregateTest.class,
ShardingTablesOnlyForPreparedStatementWithDMLTest.class,
ShardingTablesOnlyForStatementWithAggregateTest.class,
ShardingTablesOnlyForStatementWithDMLTest.class,
ShardingTablesOnlyForStatementWithSelectTest.class
})
public class AllShardingTablesOnlyTests {
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,13 @@ private static Object[] getDataParameter(final Assert assertObj) {

@Override
public OrderItem apply(final OrderByColumn input) {
return Strings.isNullOrEmpty(input.getName()) ? new OrderItem(input.getIndex(), OrderType.valueOf(input.getOrderByType().toUpperCase()))
: new OrderItem(input.getOwner(), input.getName(), OrderType.valueOf(input.getOrderByType().toUpperCase()), Optional.fromNullable(input.getAlias()));
if (Strings.isNullOrEmpty(input.getName())) {
return new OrderItem(input.getIndex(), OrderType.valueOf(input.getOrderByType().toUpperCase()));
}
if (Strings.isNullOrEmpty(input.getOwner())) {
return new OrderItem(input.getName(), OrderType.valueOf(input.getOrderByType().toUpperCase()), Optional.fromNullable(input.getAlias()));
}
return new OrderItem(input.getOwner(), input.getName(), OrderType.valueOf(input.getOrderByType().toUpperCase()), Optional.fromNullable(input.getAlias()));
}
});
selectStatement.getOrderByItems().addAll(orderItems);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final class SelectSingleTableTest extends AbstractDynamicRouteSqlTest {
@Test
public void assertGroupBy() {
assertSingleTargetWithoutParameter("select sum(qty) from order where order_id = 1 group by tenant_id", "ds_1",
"select sum(qty) , tenant_id AS GROUP_BY_DERIVED_0 from order_1 where order_id = 1 group by tenant_id");
"select sum(qty) , tenant_id AS GROUP_BY_DERIVED_0 from order_1 where order_id = 1 group by tenant_id ORDER BY GROUP_BY_DERIVED_0 ASC ");
// assertMultipleTargetsWithoutParameters("select sum(qty) from order group by tenant_id", 4, Arrays.asList("ds_0", "ds_1"),
// Arrays.asList("select sum(qty) , tenant_id as sharding_gen_1 from order_0 group by tenant_id", "select sum(qty) , tenant_id as sharding_gen_1 from order_1 group by tenant_id"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<aggregation-select-items>
<aggregation-select-item inner-expression="(date)" aggregation-type="MAX" index="2" />
</aggregation-select-items>
<order-by-columns>
<order-by-column name="order_id" order-by-type="ASC" />
</order-by-columns>
</assert>

<assert id="assertSelectWithUnsupportedFunction" sql="SELECT id, BIT_OR(date) FROM order">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<group-by-columns>
<group-by-column owner="o" name="state" alias="GROUP_BY_DERIVED_0" order-by-type="ASC" />
</group-by-columns>
<order-by-columns>
<order-by-column owner="o" name="state" alias="GROUP_BY_DERIVED_0" order-by-type="ASC" />
</order-by-columns>
</assert>

<assert id="assertSelectWithGroupBy" sql="SELECT o.state FROM order o GROUP BY o.state">
Expand All @@ -16,6 +19,9 @@
<group-by-columns>
<group-by-column owner="o" name="state" order-by-type="ASC" />
</group-by-columns>
<order-by-columns>
<order-by-column owner="o" name="state" order-by-type="ASC" />
</order-by-columns>
</assert>

<assert id="assertSelectWithGroupByAndOrderByAndDirectiveColumn" sql="SELECT o.order_id orderId FROM order o GROUP BY o.state ASC, o.order_id DESC">
Expand All @@ -26,5 +32,9 @@
<group-by-column owner="o" name="state" alias="GROUP_BY_DERIVED_0" order-by-type="ASC" />
<group-by-column owner="o" name="order_id" alias="GROUP_BY_DERIVED_1" order-by-type="DESC" />
</group-by-columns>
<order-by-columns>
<order-by-column owner="o" name="state" alias="GROUP_BY_DERIVED_0" order-by-type="ASC" />
<order-by-column owner="o" name="order_id" alias="GROUP_BY_DERIVED_1" order-by-type="DESC" />
</order-by-columns>
</assert>
</asserts>

0 comments on commit 28cae13

Please sign in to comment.