Search in sources :

Example 1 with Aggregator

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;
}
Also used : StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl) Aggregator(org.apache.geode.cache.query.Aggregator) Struct(org.apache.geode.cache.query.Struct)

Example 2 with Aggregator

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;
}
Also used : ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) Iterator(java.util.Iterator) Aggregator(org.apache.geode.cache.query.Aggregator)

Example 3 with Aggregator

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;
    }
}
Also used : Aggregator(org.apache.geode.cache.query.Aggregator)

Example 4 with Aggregator

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);
        }
    }
}
Also used : Aggregator(org.apache.geode.cache.query.Aggregator) Struct(org.apache.geode.cache.query.Struct)

Example 5 with Aggregator

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));
}
Also used : Field(java.lang.reflect.Field) Aggregator(org.apache.geode.cache.query.Aggregator) DistinctAggregator(org.apache.geode.cache.query.internal.aggregate.DistinctAggregator) MaxMin(org.apache.geode.cache.query.internal.aggregate.MaxMin) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

Aggregator (org.apache.geode.cache.query.Aggregator)5 Struct (org.apache.geode.cache.query.Struct)2 Field (java.lang.reflect.Field)1 Iterator (java.util.Iterator)1 SelectResults (org.apache.geode.cache.query.SelectResults)1 DistinctAggregator (org.apache.geode.cache.query.internal.aggregate.DistinctAggregator)1 MaxMin (org.apache.geode.cache.query.internal.aggregate.MaxMin)1 StructTypeImpl (org.apache.geode.cache.query.internal.types.StructTypeImpl)1 ObjectType (org.apache.geode.cache.query.types.ObjectType)1 UnitTest (org.apache.geode.test.junit.categories.UnitTest)1 Test (org.junit.Test)1