Search in sources :

Example 11 with Aggregation

use of io.crate.expression.symbol.Aggregation in project crate by crate.

the class ProjectionBuilder method getAggregations.

private ArrayList<Aggregation> getAggregations(Collection<Function> functions, AggregateMode mode, InputColumns.SourceSymbols sourceSymbols, SearchPath searchPath, java.util.function.Function<Symbol, Symbol> subQueryAndParamBinder) {
    ArrayList<Aggregation> aggregations = new ArrayList<>(functions.size());
    for (Function function : functions) {
        assert function.type() == FunctionType.AGGREGATE : "function type must be " + FunctionType.AGGREGATE;
        List<Symbol> aggregationInputs;
        Symbol filterInput;
        switch(mode) {
            case ITER_FINAL:
            case ITER_PARTIAL:
                // ITER means that there is no aggregation part upfront, therefore the input
                // symbols need to be in arguments
                aggregationInputs = InputColumns.create(function.arguments(), sourceSymbols);
                Symbol filter = function.filter();
                if (filter != null) {
                    filterInput = InputColumns.create(filter, sourceSymbols);
                } else {
                    filterInput = Literal.BOOLEAN_TRUE;
                }
                break;
            case PARTIAL_FINAL:
                aggregationInputs = List.of(sourceSymbols.getICForSource(function));
                filterInput = Literal.BOOLEAN_TRUE;
                break;
            default:
                throw new AssertionError("Invalid mode: " + mode.name());
        }
        AggregationFunction<?, ?> aggregationFunction = (AggregationFunction<?, ?>) nodeCtx.functions().getQualified(function, searchPath);
        assert aggregationFunction != null : "Aggregation function implementation not found using full qualified lookup: " + function;
        var valueType = mode.returnType(aggregationFunction);
        var functionInfo = FunctionInfo.of(aggregationFunction.signature(), aggregationFunction.boundSignature().getArgumentDataTypes(), valueType);
        Aggregation aggregation = new Aggregation(aggregationFunction.signature(), functionInfo, aggregationFunction.boundSignature().getReturnType().createType(), valueType, Lists2.map(aggregationInputs, subQueryAndParamBinder), subQueryAndParamBinder.apply(filterInput));
        aggregations.add(aggregation);
    }
    return aggregations;
}
Also used : Aggregation(io.crate.expression.symbol.Aggregation) AggregationFunction(io.crate.execution.engine.aggregation.AggregationFunction) AggregationFunction(io.crate.execution.engine.aggregation.AggregationFunction) Function(io.crate.expression.symbol.Function) Symbol(io.crate.expression.symbol.Symbol) ArrayList(java.util.ArrayList)

Aggregations

Aggregation (io.crate.expression.symbol.Aggregation)11 Test (org.junit.Test)10 InputColumn (io.crate.expression.symbol.InputColumn)9 Symbol (io.crate.expression.symbol.Symbol)9 CountAggregation (io.crate.execution.engine.aggregation.impl.CountAggregation)7 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)6 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)4 StreamInput (org.elasticsearch.common.io.stream.StreamInput)4 Row (io.crate.data.Row)3 GroupProjection (io.crate.execution.dsl.projection.GroupProjection)3 Function (io.crate.expression.symbol.Function)3 ArrayList (java.util.ArrayList)3 Bucket (io.crate.data.Bucket)2 CollectionBucket (io.crate.data.CollectionBucket)2 Projector (io.crate.data.Projector)2 MergePhase (io.crate.execution.dsl.phases.MergePhase)2 OrderedTopNProjection (io.crate.execution.dsl.projection.OrderedTopNProjection)2 Projection (io.crate.execution.dsl.projection.Projection)2 TopNProjection (io.crate.execution.dsl.projection.TopNProjection)2 AggregationContext (io.crate.execution.engine.aggregation.AggregationContext)2