Loading sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/DerivedUtils.java→sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/DerivedColumnUtils.java +10 −10 Original line number Diff line number Diff line Loading @@ -31,7 +31,7 @@ import com.google.common.base.Optional; * * @author zhangliang */ public final class DerivedUtils { public final class DerivedColumnUtils { private static final String DERIVED_COUNT_ALIAS = "AVG_DERIVED_COUNT_%s"; Loading Loading @@ -62,16 +62,16 @@ public final class DerivedUtils { if (!(each instanceof AggregationSelectItemContext) || AggregationType.AVG != ((AggregationSelectItemContext) each).getAggregationType()) { continue; } AggregationSelectItemContext aggregationSelectItemContext = (AggregationSelectItemContext) each; AggregationSelectItemContext countSelectItemContext = new AggregationSelectItemContext( aggregationSelectItemContext.getInnerExpression(), Optional.of(String.format(DERIVED_COUNT_ALIAS, derivedColumnOffset)), -1, AggregationType.COUNT); AggregationSelectItemContext sumSelectItemContext = new AggregationSelectItemContext( aggregationSelectItemContext.getInnerExpression(), Optional.of(String.format(DERIVED_SUM_ALIAS, derivedColumnOffset)), -1, AggregationType.SUM); aggregationSelectItemContext.getDerivedAggregationSelectItemContexts().add(countSelectItemContext); aggregationSelectItemContext.getDerivedAggregationSelectItemContexts().add(sumSelectItemContext); AggregationSelectItemContext avgContext = (AggregationSelectItemContext) each; String countAlias = String.format(DERIVED_COUNT_ALIAS, derivedColumnOffset); AggregationSelectItemContext countContext = new AggregationSelectItemContext(avgContext.getInnerExpression(), Optional.of(countAlias), -1, AggregationType.COUNT); String sumAlias = String.format(DERIVED_SUM_ALIAS, derivedColumnOffset); AggregationSelectItemContext sumContext = new AggregationSelectItemContext(avgContext.getInnerExpression(), Optional.of(sumAlias), -1, AggregationType.SUM); avgContext.getDerivedAggregationSelectItemContexts().add(countContext); avgContext.getDerivedAggregationSelectItemContexts().add(sumContext); // TODO 将AVG列替换成常数,避免数据库再计算无用的AVG函数 itemsToken.getItems().add(countSelectItemContext.getExpression() + " AS " + countSelectItemContext.getAlias().get() + " "); itemsToken.getItems().add(sumSelectItemContext.getExpression() + " AS " + sumSelectItemContext.getAlias().get() + " "); itemsToken.getItems().add(countContext.getExpression() + " AS " + countAlias + " "); itemsToken.getItems().add(sumContext.getExpression() + " AS " + sumAlias + " "); derivedColumnOffset++; } } Loading sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/router/SQLRouteEngine.java +2 −2 Original line number Diff line number Diff line Loading @@ -34,7 +34,7 @@ import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.UpdateSQLContext import com.dangdang.ddframe.rdb.sharding.parsing.parser.exception.SQLParsingException; import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.OffsetLimitToken; import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.RowCountLimitToken; import com.dangdang.ddframe.rdb.sharding.rewrite.DerivedUtils; import com.dangdang.ddframe.rdb.sharding.rewrite.DerivedColumnUtils; import com.dangdang.ddframe.rdb.sharding.rewrite.GenerateKeysUtils; import com.dangdang.ddframe.rdb.sharding.rewrite.SQLBuilder; import com.dangdang.ddframe.rdb.sharding.rewrite.SQLRewriteEngine; Loading Loading @@ -100,7 +100,7 @@ public final class SQLRouteEngine { GenerateKeysUtils.appendGenerateKeys(shardingRule, parameters, (InsertSQLContext) result); } if (result instanceof SelectSQLContext) { DerivedUtils.appendDerivedColumns((SelectSQLContext) result); DerivedColumnUtils.appendDerivedColumns((SelectSQLContext) result); } return result; } Loading Loading
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/DerivedUtils.java→sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/DerivedColumnUtils.java +10 −10 Original line number Diff line number Diff line Loading @@ -31,7 +31,7 @@ import com.google.common.base.Optional; * * @author zhangliang */ public final class DerivedUtils { public final class DerivedColumnUtils { private static final String DERIVED_COUNT_ALIAS = "AVG_DERIVED_COUNT_%s"; Loading Loading @@ -62,16 +62,16 @@ public final class DerivedUtils { if (!(each instanceof AggregationSelectItemContext) || AggregationType.AVG != ((AggregationSelectItemContext) each).getAggregationType()) { continue; } AggregationSelectItemContext aggregationSelectItemContext = (AggregationSelectItemContext) each; AggregationSelectItemContext countSelectItemContext = new AggregationSelectItemContext( aggregationSelectItemContext.getInnerExpression(), Optional.of(String.format(DERIVED_COUNT_ALIAS, derivedColumnOffset)), -1, AggregationType.COUNT); AggregationSelectItemContext sumSelectItemContext = new AggregationSelectItemContext( aggregationSelectItemContext.getInnerExpression(), Optional.of(String.format(DERIVED_SUM_ALIAS, derivedColumnOffset)), -1, AggregationType.SUM); aggregationSelectItemContext.getDerivedAggregationSelectItemContexts().add(countSelectItemContext); aggregationSelectItemContext.getDerivedAggregationSelectItemContexts().add(sumSelectItemContext); AggregationSelectItemContext avgContext = (AggregationSelectItemContext) each; String countAlias = String.format(DERIVED_COUNT_ALIAS, derivedColumnOffset); AggregationSelectItemContext countContext = new AggregationSelectItemContext(avgContext.getInnerExpression(), Optional.of(countAlias), -1, AggregationType.COUNT); String sumAlias = String.format(DERIVED_SUM_ALIAS, derivedColumnOffset); AggregationSelectItemContext sumContext = new AggregationSelectItemContext(avgContext.getInnerExpression(), Optional.of(sumAlias), -1, AggregationType.SUM); avgContext.getDerivedAggregationSelectItemContexts().add(countContext); avgContext.getDerivedAggregationSelectItemContexts().add(sumContext); // TODO 将AVG列替换成常数,避免数据库再计算无用的AVG函数 itemsToken.getItems().add(countSelectItemContext.getExpression() + " AS " + countSelectItemContext.getAlias().get() + " "); itemsToken.getItems().add(sumSelectItemContext.getExpression() + " AS " + sumSelectItemContext.getAlias().get() + " "); itemsToken.getItems().add(countContext.getExpression() + " AS " + countAlias + " "); itemsToken.getItems().add(sumContext.getExpression() + " AS " + sumAlias + " "); derivedColumnOffset++; } } Loading
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/router/SQLRouteEngine.java +2 −2 Original line number Diff line number Diff line Loading @@ -34,7 +34,7 @@ import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.UpdateSQLContext import com.dangdang.ddframe.rdb.sharding.parsing.parser.exception.SQLParsingException; import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.OffsetLimitToken; import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.RowCountLimitToken; import com.dangdang.ddframe.rdb.sharding.rewrite.DerivedUtils; import com.dangdang.ddframe.rdb.sharding.rewrite.DerivedColumnUtils; import com.dangdang.ddframe.rdb.sharding.rewrite.GenerateKeysUtils; import com.dangdang.ddframe.rdb.sharding.rewrite.SQLBuilder; import com.dangdang.ddframe.rdb.sharding.rewrite.SQLRewriteEngine; Loading Loading @@ -100,7 +100,7 @@ public final class SQLRouteEngine { GenerateKeysUtils.appendGenerateKeys(shardingRule, parameters, (InsertSQLContext) result); } if (result instanceof SelectSQLContext) { DerivedUtils.appendDerivedColumns((SelectSQLContext) result); DerivedColumnUtils.appendDerivedColumns((SelectSQLContext) result); } return result; } Loading