Search in sources :

Example 1 with AggregationProjection

use of io.crate.execution.dsl.projection.AggregationProjection in project crate by crate.

the class HashAggregate method build.

@Override
public ExecutionPlan build(PlannerContext plannerContext, Set<PlanHint> planHints, ProjectionBuilder projectionBuilder, int limit, int offset, @Nullable OrderBy order, @Nullable Integer pageSizeHint, Row params, SubQueryResults subQueryResults) {
    ExecutionPlan executionPlan = source.build(plannerContext, planHints, projectionBuilder, LogicalPlanner.NO_LIMIT, 0, null, null, params, subQueryResults);
    AggregationOutputValidator.validateOutputs(aggregates);
    var paramBinder = new SubQueryAndParamBinder(params, subQueryResults);
    var sourceOutputs = source.outputs();
    if (executionPlan.resultDescription().hasRemainingLimitOrOffset()) {
        executionPlan = Merge.ensureOnHandler(executionPlan, plannerContext);
    }
    if (ExecutionPhases.executesOnHandler(plannerContext.handlerNode(), executionPlan.resultDescription().nodeIds())) {
        if (source.preferShardProjections()) {
            executionPlan.addProjection(projectionBuilder.aggregationProjection(sourceOutputs, aggregates, paramBinder, AggregateMode.ITER_PARTIAL, RowGranularity.SHARD, plannerContext.transactionContext().sessionContext().searchPath()));
            executionPlan.addProjection(projectionBuilder.aggregationProjection(aggregates, aggregates, paramBinder, AggregateMode.PARTIAL_FINAL, RowGranularity.CLUSTER, plannerContext.transactionContext().sessionContext().searchPath()));
            return executionPlan;
        }
        AggregationProjection fullAggregation = projectionBuilder.aggregationProjection(sourceOutputs, aggregates, paramBinder, AggregateMode.ITER_FINAL, RowGranularity.CLUSTER, plannerContext.transactionContext().sessionContext().searchPath());
        executionPlan.addProjection(fullAggregation);
        return executionPlan;
    }
    AggregationProjection toPartial = projectionBuilder.aggregationProjection(sourceOutputs, aggregates, paramBinder, AggregateMode.ITER_PARTIAL, source.preferShardProjections() ? RowGranularity.SHARD : RowGranularity.NODE, plannerContext.transactionContext().sessionContext().searchPath());
    executionPlan.addProjection(toPartial);
    AggregationProjection toFinal = projectionBuilder.aggregationProjection(aggregates, aggregates, paramBinder, AggregateMode.PARTIAL_FINAL, RowGranularity.CLUSTER, plannerContext.transactionContext().sessionContext().searchPath());
    return new Merge(executionPlan, new MergePhase(plannerContext.jobId(), plannerContext.nextExecutionPhaseId(), MERGE_PHASE_NAME, executionPlan.resultDescription().nodeIds().size(), 1, Collections.singletonList(plannerContext.handlerNode()), executionPlan.resultDescription().streamOutputs(), Collections.singletonList(toFinal), DistributionInfo.DEFAULT_BROADCAST, null), LogicalPlanner.NO_LIMIT, 0, aggregates.size(), 1, null);
}
Also used : ExecutionPlan(io.crate.planner.ExecutionPlan) MergePhase(io.crate.execution.dsl.phases.MergePhase) Merge(io.crate.planner.Merge) AggregationProjection(io.crate.execution.dsl.projection.AggregationProjection)

Example 2 with AggregationProjection

use of io.crate.execution.dsl.projection.AggregationProjection in project crate by crate.

the class ProjectionToProjectorVisitorTest method testAggregationProjector.

@Test
public void testAggregationProjector() throws Exception {
    AggregationProjection projection = new AggregationProjection(Arrays.asList(new Aggregation(avgSignature, avgSignature.getReturnType().createType(), Collections.singletonList(new InputColumn(1))), new Aggregation(CountAggregation.SIGNATURE, CountAggregation.SIGNATURE.getReturnType().createType(), Collections.singletonList(new InputColumn(0)))), RowGranularity.SHARD, AggregateMode.ITER_FINAL);
    Projector projector = visitor.create(projection, txnCtx, RamAccounting.NO_ACCOUNTING, memoryManager, UUID.randomUUID());
    assertThat(projector, instanceOf(AggregationPipe.class));
    BatchIterator<Row> batchIterator = projector.apply(InMemoryBatchIterator.of(new CollectionBucket(Arrays.asList($("foo", 10), $("bar", 20))), SENTINEL, true));
    TestingRowConsumer consumer = new TestingRowConsumer();
    consumer.accept(batchIterator, null);
    Bucket rows = consumer.getBucket();
    assertThat(rows.size(), is(1));
    assertThat(rows, contains(isRow(15.0, 2L)));
}
Also used : Aggregation(io.crate.expression.symbol.Aggregation) CountAggregation(io.crate.execution.engine.aggregation.impl.CountAggregation) Projector(io.crate.data.Projector) SortingProjector(io.crate.execution.engine.sort.SortingProjector) SortingTopNProjector(io.crate.execution.engine.sort.SortingTopNProjector) GroupingProjector(io.crate.execution.engine.aggregation.GroupingProjector) Bucket(io.crate.data.Bucket) CollectionBucket(io.crate.data.CollectionBucket) InputColumn(io.crate.expression.symbol.InputColumn) Row(io.crate.data.Row) TestingHelpers.isRow(io.crate.testing.TestingHelpers.isRow) AggregationProjection(io.crate.execution.dsl.projection.AggregationProjection) AggregationPipe(io.crate.execution.engine.aggregation.AggregationPipe) CollectionBucket(io.crate.data.CollectionBucket) TestingRowConsumer(io.crate.testing.TestingRowConsumer) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Aggregations

AggregationProjection (io.crate.execution.dsl.projection.AggregationProjection)2 Bucket (io.crate.data.Bucket)1 CollectionBucket (io.crate.data.CollectionBucket)1 Projector (io.crate.data.Projector)1 Row (io.crate.data.Row)1 MergePhase (io.crate.execution.dsl.phases.MergePhase)1 AggregationPipe (io.crate.execution.engine.aggregation.AggregationPipe)1 GroupingProjector (io.crate.execution.engine.aggregation.GroupingProjector)1 CountAggregation (io.crate.execution.engine.aggregation.impl.CountAggregation)1 SortingProjector (io.crate.execution.engine.sort.SortingProjector)1 SortingTopNProjector (io.crate.execution.engine.sort.SortingTopNProjector)1 Aggregation (io.crate.expression.symbol.Aggregation)1 InputColumn (io.crate.expression.symbol.InputColumn)1 ExecutionPlan (io.crate.planner.ExecutionPlan)1 Merge (io.crate.planner.Merge)1 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)1 TestingHelpers.isRow (io.crate.testing.TestingHelpers.isRow)1 TestingRowConsumer (io.crate.testing.TestingRowConsumer)1 Test (org.junit.Test)1