Search in sources :

Example 1 with AggregationSelectItem

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

the class GroupByStreamMergedResult method aggregate.

private void aggregate(final Map<AggregationSelectItem, AggregationUnit> aggregationUnitMap) throws SQLException {
    for (Entry<AggregationSelectItem, AggregationUnit> entry : aggregationUnitMap.entrySet()) {
        List<Comparable<?>> values = new ArrayList<>(2);
        if (entry.getKey().getDerivedAggregationSelectItems().isEmpty()) {
            values.add(getAggregationValue(entry.getKey()));
        } else {
            for (AggregationSelectItem each : entry.getKey().getDerivedAggregationSelectItems()) {
                values.add(getAggregationValue(each));
            }
        }
        entry.getValue().merge(values);
    }
}
Also used : AggregationSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem) AggregationUnit(io.shardingjdbc.core.merger.dql.groupby.aggregation.AggregationUnit) ArrayList(java.util.ArrayList)

Example 2 with AggregationSelectItem

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

the class SelectStatement method setIndexForAggregationItem.

private void setIndexForAggregationItem(final Map<String, Integer> columnLabelIndexMap) {
    for (AggregationSelectItem each : getAggregationSelectItems()) {
        Preconditions.checkState(columnLabelIndexMap.containsKey(each.getColumnLabel()), String.format("Can't find index: %s, please add alias for aggregate selections", each));
        each.setIndex(columnLabelIndexMap.get(each.getColumnLabel()));
        for (AggregationSelectItem derived : each.getDerivedAggregationSelectItems()) {
            Preconditions.checkState(columnLabelIndexMap.containsKey(derived.getColumnLabel()), String.format("Can't find index: %s", derived));
            derived.setIndex(columnLabelIndexMap.get(derived.getColumnLabel()));
        }
    }
}
Also used : AggregationSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem)

Example 3 with AggregationSelectItem

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

the class SelectStatement method getAggregationSelectItems.

/**
 * Get aggregation select items.
 *
 * @return aggregation select items
 */
public List<AggregationSelectItem> getAggregationSelectItems() {
    List<AggregationSelectItem> result = new LinkedList<>();
    for (SelectItem each : items) {
        if (each instanceof AggregationSelectItem) {
            AggregationSelectItem aggregationSelectItem = (AggregationSelectItem) each;
            result.add(aggregationSelectItem);
            result.addAll(aggregationSelectItem.getDerivedAggregationSelectItems());
        }
    }
    return result;
}
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) LinkedList(java.util.LinkedList)

Example 4 with AggregationSelectItem

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

the class DQLMergeEngineTest method assertBuildGroupByMemoryMergedResultWithAggregationOnlyWithSQLServerLimit.

@Test
public void assertBuildGroupByMemoryMergedResultWithAggregationOnlyWithSQLServerLimit() throws SQLException {
    selectStatement.setLimit(new Limit(DatabaseType.SQLServer));
    selectStatement.getItems().add(new AggregationSelectItem(AggregationType.COUNT, "(*)", Optional.<String>absent()));
    mergeEngine = new DQLMergeEngine(queryResults, selectStatement);
    MergedResult actual = mergeEngine.merge();
    assertThat(actual, instanceOf(TopAndRowNumberDecoratorMergedResult.class));
    assertThat(((TopAndRowNumberDecoratorMergedResult) actual).getMergedResult(), instanceOf(GroupByMemoryMergedResult.class));
}
Also used : AggregationSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem) TopAndRowNumberDecoratorMergedResult(io.shardingjdbc.core.merger.dql.pagination.TopAndRowNumberDecoratorMergedResult) GroupByStreamMergedResult(io.shardingjdbc.core.merger.dql.groupby.GroupByStreamMergedResult) MergedResult(io.shardingjdbc.core.merger.MergedResult) LimitDecoratorMergedResult(io.shardingjdbc.core.merger.dql.pagination.LimitDecoratorMergedResult) GroupByMemoryMergedResult(io.shardingjdbc.core.merger.dql.groupby.GroupByMemoryMergedResult) TopAndRowNumberDecoratorMergedResult(io.shardingjdbc.core.merger.dql.pagination.TopAndRowNumberDecoratorMergedResult) RowNumberDecoratorMergedResult(io.shardingjdbc.core.merger.dql.pagination.RowNumberDecoratorMergedResult) IteratorStreamMergedResult(io.shardingjdbc.core.merger.dql.iterator.IteratorStreamMergedResult) OrderByStreamMergedResult(io.shardingjdbc.core.merger.dql.orderby.OrderByStreamMergedResult) GroupByMemoryMergedResult(io.shardingjdbc.core.merger.dql.groupby.GroupByMemoryMergedResult) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) Test(org.junit.Test)

Example 5 with AggregationSelectItem

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

the class DQLMergeEngineTest method assertBuildGroupByMemoryMergedResultWithAggregationOnlyWithMySQLLimit.

@Test
public void assertBuildGroupByMemoryMergedResultWithAggregationOnlyWithMySQLLimit() throws SQLException {
    selectStatement.setLimit(new Limit(DatabaseType.MySQL));
    selectStatement.getItems().add(new AggregationSelectItem(AggregationType.COUNT, "(*)", Optional.<String>absent()));
    mergeEngine = new DQLMergeEngine(queryResults, selectStatement);
    MergedResult actual = mergeEngine.merge();
    assertThat(actual, instanceOf(LimitDecoratorMergedResult.class));
    assertThat(((LimitDecoratorMergedResult) actual).getMergedResult(), instanceOf(GroupByMemoryMergedResult.class));
}
Also used : AggregationSelectItem(io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem) GroupByStreamMergedResult(io.shardingjdbc.core.merger.dql.groupby.GroupByStreamMergedResult) MergedResult(io.shardingjdbc.core.merger.MergedResult) LimitDecoratorMergedResult(io.shardingjdbc.core.merger.dql.pagination.LimitDecoratorMergedResult) GroupByMemoryMergedResult(io.shardingjdbc.core.merger.dql.groupby.GroupByMemoryMergedResult) TopAndRowNumberDecoratorMergedResult(io.shardingjdbc.core.merger.dql.pagination.TopAndRowNumberDecoratorMergedResult) RowNumberDecoratorMergedResult(io.shardingjdbc.core.merger.dql.pagination.RowNumberDecoratorMergedResult) IteratorStreamMergedResult(io.shardingjdbc.core.merger.dql.iterator.IteratorStreamMergedResult) OrderByStreamMergedResult(io.shardingjdbc.core.merger.dql.orderby.OrderByStreamMergedResult) GroupByMemoryMergedResult(io.shardingjdbc.core.merger.dql.groupby.GroupByMemoryMergedResult) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) LimitDecoratorMergedResult(io.shardingjdbc.core.merger.dql.pagination.LimitDecoratorMergedResult) Test(org.junit.Test)

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