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);
}
}
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));
}
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++;
}
}
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++;
}
}
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());
}
Aggregations