Skip to content

Commit

Permalink
ISPN-15254 Support aggregation on entity with asterisk
Browse files Browse the repository at this point in the history
  • Loading branch information
fax4ever committed Oct 20, 2023
1 parent bc728f6 commit cea717a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ setFunction
| ^(AVG { delegate.activateAggregation(AggregationFunction.AVG); } numericValueExpression)
| ^(MAX { delegate.activateAggregation(AggregationFunction.MAX); } numericValueExpression)
| ^(MIN { delegate.activateAggregation(AggregationFunction.MIN); } numericValueExpression)
| ^(COUNT (ASTERISK { delegate.activateAggregation(AggregationFunction.COUNT); } | (DISTINCT { delegate.activateAggregation(AggregationFunction.COUNT_DISTINCT); } | ALL { delegate.activateAggregation(AggregationFunction.COUNT); }) countFunctionArguments))
| ^(COUNT (ASTERISK { delegate.activateAsteriskAggregation(AggregationFunction.COUNT); } | (DISTINCT { delegate.activateAggregation(AggregationFunction.COUNT_DISTINCT); } | ALL { delegate.activateAggregation(AggregationFunction.COUNT); }) countFunctionArguments))
;
versionFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public String getOperator() {

void activateAggregation(AggregationFunction aggregationFunction);

void activateAsteriskAggregation(AggregationFunction aggregationFunction);

void deactivateAggregation();

void projectVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ private enum Phase {

private final String queryString;

private boolean asteriskCount = false;

QueryRendererDelegateImpl(String queryString, ObjectPropertyHelper<TypeMetadata> propertyHelper) {
this.queryString = queryString;
this.propertyHelper = propertyHelper;
Expand Down Expand Up @@ -537,6 +539,12 @@ public void activateAggregation(AggregationFunction aggregationFunction) {
propertyPath = null;
}

@Override
public void activateAsteriskAggregation(AggregationFunction aggregationFunction) {
activateAggregation(aggregationFunction);
asteriskCount = true;
}

@Override
public void deactivateAggregation() {
aggregationFunction = null;
Expand Down Expand Up @@ -589,6 +597,19 @@ public void groupingValue(String collateName) {
groupBy = new ArrayList<>(ARRAY_INITIAL_LENGTH);
}
groupBy.add(resolveAlias(propertyPath));

if (!asteriskCount) {
return;
}
if (projections == null) {
projections = new ArrayList<>(ARRAY_INITIAL_LENGTH);
projectedTypes = new ArrayList<>(ARRAY_INITIAL_LENGTH);
projectedNullMarkers = new ArrayList<>(ARRAY_INITIAL_LENGTH);
}
projections.add(new CacheValueAggregationPropertyPath<>());
projectedTypes.add(null);
projectedNullMarkers.add(null);
asteriskCount = false;
}

private Object parameterValue(String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,11 @@ public void test() {
}

Query<Object[]> query;

query = cache.query("select status, count(code) from org.infinispan.query.model.Sale where day = :day group by status order by status");
query.setParameter("day", NUMBER_OF_DAYS / 2);
assertThat(query.list()).containsExactly(AGGREGATION_RESULT);
// inverted count / group
query = cache.query("select count(code), status from org.infinispan.query.model.Sale where day = :day group by status order by status");
query.setParameter("day", NUMBER_OF_DAYS / 2);
assertThat(query.list()).containsExactly(REV_AGGREGATION_RESULT);
// no order by
query = cache.query("select status, count(code) from org.infinispan.query.model.Sale where day = :day group by status");
query = cache.query("select s.status, count(s) from org.infinispan.query.model.Sale s where s.day = :day group by s.status");
query.setParameter("day", NUMBER_OF_DAYS / 2);
assertThat(query.list()).containsExactlyInAnyOrder(AGGREGATION_RESULT);
// alias
query = cache.query("select s.status, count(s.code) from org.infinispan.query.model.Sale s where s.day = :day group by s.status order by s.status");
query.setParameter("day", NUMBER_OF_DAYS / 2);
assertThat(query.list()).containsExactly(AGGREGATION_RESULT);
// alias && count on entity
query = cache.query("select s.status, count(s) from org.infinispan.query.model.Sale s where s.day = :day group by s.status order by s.status");
assertThat(query.list()).containsExactly(FULL_AGGREGATION_RESULT);
// no alias && count on entity
query = cache.query("select status, count(*) from org.infinispan.query.model.Sale where day = :day group by status");
query.setParameter("day", NUMBER_OF_DAYS / 2);
assertThat(query.list()).containsExactly(FULL_AGGREGATION_RESULT);
}
Expand Down

0 comments on commit cea717a

Please sign in to comment.