Commit 78df234b authored by terrymanu's avatar terrymanu
Browse files

refactor: DerivedUtils = DerivedColumnUtils

parent 750cbf19
Loading
Loading
Loading
Loading
+10 −10
Original line number Diff line number Diff line
@@ -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";
    
@@ -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++;
        }
    }
+2 −2
Original line number Diff line number Diff line
@@ -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;
@@ -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;
    }