use of org.apache.geode.cache.query.Aggregator in project geode by apache.
the class CompiledGroupBySelect method terminateAndAddToResults.
private boolean terminateAndAddToResults(boolean isStruct, SelectResults newResults, Aggregator[] aggregators, Object prev, ExecutionContext context, boolean isStrucFields, int limitValue) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
Object[] newRowArray = isStruct ? copyStruct((Struct) prev) : null;
Object newObject = null;
int bitstart = 0;
if (limitValue == 0) {
return false;
}
for (Aggregator aggregator : aggregators) {
if (isStruct) {
int pos = this.aggregateColsPos.nextSetBit(bitstart);
bitstart = pos + 1;
Object scalarResult = aggregator.terminate();
newRowArray[pos] = scalarResult;
} else {
newObject = aggregator.terminate();
}
}
if (isStruct) {
if (isStrucFields) {
((StructFields) newResults).addFieldValues(newRowArray);
} else {
newResults.add(new StructImpl((StructTypeImpl) ((Struct) prev).getStructType(), newRowArray));
}
} else {
newResults.add(newObject);
}
boolean keepAdding = true;
if (this.originalOrderByClause == null && limitValue > 0 && (context.getIsPRQueryNode() || context.getBucketList() == null) && newResults.size() == limitValue) {
keepAdding = false;
}
// rfresh the aggregators
refreshAggregators(aggregators, context);
return keepAdding;
}
use of org.apache.geode.cache.query.Aggregator in project geode by apache.
the class CompiledGroupBySelect method applyAggregateAndGroupBy.
public SelectResults applyAggregateAndGroupBy(SelectResults baseResults, ExecutionContext context) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
ObjectType elementType = baseResults.getCollectionType().getElementType();
boolean isStruct = elementType != null && elementType.isStructType();
boolean isBucketNodes = context.getBucketList() != null;
boolean createOrderedResultSet = isBucketNodes && this.orderByAttrs != null;
boolean[] objectChangedMarker = new boolean[] { false };
int limitValue = evaluateLimitValue(context, limit);
SelectResults newResults = createResultSet(context, elementType, isStruct, createOrderedResultSet);
Aggregator[] aggregators = new Aggregator[this.aggregateFunctions.length];
refreshAggregators(aggregators, context);
if (this.orderByAttrs != null) {
applyGroupBy(baseResults, context, isStruct, newResults, aggregators, !createOrderedResultSet, objectChangedMarker, limitValue);
} else {
Iterator iter = baseResults.iterator();
Object current = null;
boolean unterminated = iter.hasNext();
while (iter.hasNext()) {
current = iter.next();
accumulate(isStruct, aggregators, current, objectChangedMarker);
}
if (unterminated) {
this.terminateAndAddToResults(isStruct, newResults, aggregators, current, context, !createOrderedResultSet, limitValue);
}
}
return newResults;
}
use of org.apache.geode.cache.query.Aggregator in project geode by apache.
the class CompiledGroupBySelect method refreshAggregators.
private void refreshAggregators(Aggregator[] aggregators, ExecutionContext context) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
int i = 0;
for (CompiledAggregateFunction aggFunc : this.aggregateFunctions) {
Aggregator agg = (Aggregator) aggFunc.evaluate(context);
aggregators[i++] = agg;
}
}
use of org.apache.geode.cache.query.Aggregator in project geode by apache.
the class CompiledGroupBySelect method accumulate.
private void accumulate(boolean isStruct, Aggregator[] aggregators, Object current, boolean[] objectChangedMarker) {
int bitstart = 0;
for (Aggregator aggregator : aggregators) {
if (isStruct) {
int pos = this.aggregateColsPos.nextSetBit(bitstart);
bitstart = pos + 1;
Struct struct = (Struct) current;
Object scalar = PDXUtils.convertPDX(struct.getFieldValues()[pos], false, true, true, true, objectChangedMarker, isStruct);
aggregator.accumulate(scalar);
} else {
current = PDXUtils.convertPDX(current, false, true, true, true, objectChangedMarker, isStruct);
aggregator.accumulate(current);
}
}
}
use of org.apache.geode.cache.query.Aggregator in project geode by apache.
the class CompiledAggregateFunctionJUnitTest method testMaxMin.
@Test
public void testMaxMin() throws Exception {
CompiledAggregateFunction caf1 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.MAX);
ExecutionContext context1 = new ExecutionContext(null, cache);
Aggregator agg = (Aggregator) caf1.evaluate(context1);
assertTrue(agg instanceof MaxMin);
MaxMin maxMin = (MaxMin) agg;
Class maxMinClass = MaxMin.class;
Field findMax = maxMinClass.getDeclaredField("findMax");
findMax.setAccessible(true);
assertTrue((Boolean) findMax.get(maxMin));
CompiledAggregateFunction caf2 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.MIN);
ExecutionContext context2 = new ExecutionContext(null, cache);
Aggregator agg1 = (Aggregator) caf2.evaluate(context1);
assertTrue(agg1 instanceof MaxMin);
MaxMin maxMin1 = (MaxMin) agg1;
assertFalse((Boolean) findMax.get(maxMin1));
}
Aggregations