Search in sources :

Example 11 with AggregationSelectItem

use of io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem in project sharding-jdbc by shardingjdbc.

the class GroupByMemoryMergedResult method aggregate.

private void aggregate(final QueryResult queryResult, final GroupByValue groupByValue, final Map<GroupByValue, Map<AggregationSelectItem, AggregationUnit>> aggregationMap) throws SQLException {
    for (AggregationSelectItem each : selectStatement.getAggregationSelectItems()) {
        List<Comparable<?>> values = new ArrayList<>(2);
        if (each.getDerivedAggregationSelectItems().isEmpty()) {
            values.add(getAggregationValue(queryResult, each));
        } else {
            for (AggregationSelectItem derived : each.getDerivedAggregationSelectItems()) {
                values.add(getAggregationValue(queryResult, derived));
            }
        }
        aggregationMap.get(groupByValue).get(each).merge(values);
    }
}
Also used : AggregationSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem) ArrayList(java.util.ArrayList)

Example 12 with AggregationSelectItem

use of io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem in project sharding-jdbc by shardingjdbc.

the class DQLMergeEngineTest method assertBuildGroupByMemoryMergedResultWithAggregationOnly.

@Test
public void assertBuildGroupByMemoryMergedResultWithAggregationOnly() throws SQLException {
    selectStatement.getItems().add(new AggregationSelectItem(AggregationType.COUNT, "(*)", Optional.<String>absent()));
    mergeEngine = new DQLMergeEngine(queryResults, selectStatement);
    assertThat(mergeEngine.merge(), instanceOf(GroupByMemoryMergedResult.class));
}
Also used : AggregationSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem) GroupByMemoryMergedResult(io.shardingjdbc.core.merger.dql.groupby.GroupByMemoryMergedResult) Test(org.junit.Test)

Example 13 with AggregationSelectItem

use of io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem in project sharding-jdbc by shardingjdbc.

the class AggregationSelectItemAssert method assertAggregationSelectItems.

void assertAggregationSelectItems(final Set<SelectItem> actual, final List<ExpectedAggregationSelectItem> expected) {
    List<AggregationSelectItem> aggregationSelectItems = getAggregationSelectItems(actual);
    assertThat(assertMessage.getFullAssertMessage("Table tokens size error: "), aggregationSelectItems.size(), is(expected.size()));
    int count = 0;
    for (AggregationSelectItem each : aggregationSelectItems) {
        assertAggregationSelectItem(each, expected.get(count));
        count++;
    }
}
Also used : AggregationSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem) ExpectedAggregationSelectItem(io.shardingjdbc.core.parsing.integrate.jaxb.item.ExpectedAggregationSelectItem)

Example 14 with AggregationSelectItem

use of io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem in project sharding-jdbc by shardingjdbc.

the class AbstractSelectParser method appendAvgDerivedColumns.

private void appendAvgDerivedColumns(final ItemsToken itemsToken, final SelectStatement selectStatement) {
    int derivedColumnOffset = 0;
    for (SelectItem each : selectStatement.getItems()) {
        if (!(each instanceof AggregationSelectItem) || AggregationType.AVG != ((AggregationSelectItem) each).getType()) {
            continue;
        }
        AggregationSelectItem avgItem = (AggregationSelectItem) each;
        String countAlias = String.format(DERIVED_COUNT_ALIAS, derivedColumnOffset);
        AggregationSelectItem countItem = new AggregationSelectItem(AggregationType.COUNT, avgItem.getInnerExpression(), Optional.of(countAlias));
        String sumAlias = String.format(DERIVED_SUM_ALIAS, derivedColumnOffset);
        AggregationSelectItem sumItem = new AggregationSelectItem(AggregationType.SUM, avgItem.getInnerExpression(), Optional.of(sumAlias));
        avgItem.getDerivedAggregationSelectItems().add(countItem);
        avgItem.getDerivedAggregationSelectItems().add(sumItem);
        // TODO replace avg to constant, avoid calculate useless avg
        itemsToken.getItems().add(countItem.getExpression() + " AS " + countAlias + " ");
        itemsToken.getItems().add(sumItem.getExpression() + " AS " + sumAlias + " ");
        derivedColumnOffset++;
    }
}
Also used : AggregationSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem) SelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.SelectItem) AggregationSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem)

Example 15 with AggregationSelectItem

use of io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem in project sharding-jdbc by shardingjdbc.

the class SelectListClauseParser method parseAggregationSelectItem.

private SelectItem parseAggregationSelectItem(final SelectStatement selectStatement) {
    AggregationType aggregationType = AggregationType.valueOf(lexerEngine.getCurrentToken().getLiterals().toUpperCase());
    lexerEngine.nextToken();
    return new AggregationSelectItem(aggregationType, lexerEngine.skipParentheses(selectStatement), aliasExpressionParser.parseSelectItemAlias());
}
Also used : AggregationSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem) AggregationType(io.shardingjdbc.core.constant.AggregationType)

Aggregations

AggregationSelectItem (io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem)15 GroupByMemoryMergedResult (io.shardingjdbc.core.merger.dql.groupby.GroupByMemoryMergedResult)4 Test (org.junit.Test)4 MergedResult (io.shardingjdbc.core.merger.MergedResult)3 GroupByStreamMergedResult (io.shardingjdbc.core.merger.dql.groupby.GroupByStreamMergedResult)3 IteratorStreamMergedResult (io.shardingjdbc.core.merger.dql.iterator.IteratorStreamMergedResult)3 OrderByStreamMergedResult (io.shardingjdbc.core.merger.dql.orderby.OrderByStreamMergedResult)3 LimitDecoratorMergedResult (io.shardingjdbc.core.merger.dql.pagination.LimitDecoratorMergedResult)3 RowNumberDecoratorMergedResult (io.shardingjdbc.core.merger.dql.pagination.RowNumberDecoratorMergedResult)3 TopAndRowNumberDecoratorMergedResult (io.shardingjdbc.core.merger.dql.pagination.TopAndRowNumberDecoratorMergedResult)3 Limit (io.shardingjdbc.core.parsing.parser.context.limit.Limit)3 AggregationUnit (io.shardingjdbc.core.merger.dql.groupby.aggregation.AggregationUnit)2 TestQueryResult (io.shardingjdbc.core.merger.fixture.TestQueryResult)2 ExpectedAggregationSelectItem (io.shardingjdbc.core.parsing.integrate.jaxb.item.ExpectedAggregationSelectItem)2 OrderItem (io.shardingjdbc.core.parsing.parser.context.OrderItem)2 SelectItem (io.shardingjdbc.core.parsing.parser.context.selectitem.SelectItem)2 SelectStatement (io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 Before (org.junit.Before)2