Search in sources :

Example 1 with GroupProjection

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

the class GroupHashAggregate method build.

@Override
public ExecutionPlan build(PlannerContext plannerContext, Set<PlanHint> hints, ProjectionBuilder projectionBuilder, int limit, int offset, @Nullable OrderBy order, @Nullable Integer pageSizeHint, Row params, SubQueryResults subQueryResults) {
    if (hints.contains(PlanHint.PREFER_SOURCE_LOOKUP)) {
        hints = new HashSet<>(hints);
        hints.remove(PlanHint.PREFER_SOURCE_LOOKUP);
    }
    ExecutionPlan executionPlan = source.build(plannerContext, hints, projectionBuilder, NO_LIMIT, 0, null, null, params, subQueryResults);
    if (executionPlan.resultDescription().hasRemainingLimitOrOffset()) {
        executionPlan = Merge.ensureOnHandler(executionPlan, plannerContext);
    }
    SubQueryAndParamBinder paramBinder = new SubQueryAndParamBinder(params, subQueryResults);
    List<Symbol> sourceOutputs = source.outputs();
    if (shardsContainAllGroupKeyValues()) {
        GroupProjection groupProjection = projectionBuilder.groupProjection(sourceOutputs, groupKeys, aggregates, paramBinder, AggregateMode.ITER_FINAL, source.preferShardProjections() ? RowGranularity.SHARD : RowGranularity.CLUSTER, plannerContext.transactionContext().sessionContext().searchPath());
        executionPlan.addProjection(groupProjection, TopN.NO_LIMIT, 0, null);
        return executionPlan;
    }
    if (ExecutionPhases.executesOnHandler(plannerContext.handlerNode(), executionPlan.resultDescription().nodeIds())) {
        if (source.preferShardProjections()) {
            executionPlan.addProjection(projectionBuilder.groupProjection(sourceOutputs, groupKeys, aggregates, paramBinder, AggregateMode.ITER_PARTIAL, RowGranularity.SHARD, plannerContext.transactionContext().sessionContext().searchPath()));
            executionPlan.addProjection(projectionBuilder.groupProjection(outputs, groupKeys, aggregates, paramBinder, AggregateMode.PARTIAL_FINAL, RowGranularity.NODE, plannerContext.transactionContext().sessionContext().searchPath()), TopN.NO_LIMIT, 0, null);
            return executionPlan;
        } else {
            executionPlan.addProjection(projectionBuilder.groupProjection(sourceOutputs, groupKeys, aggregates, paramBinder, AggregateMode.ITER_FINAL, RowGranularity.NODE, plannerContext.transactionContext().sessionContext().searchPath()), TopN.NO_LIMIT, 0, null);
            return executionPlan;
        }
    }
    GroupProjection toPartial = projectionBuilder.groupProjection(sourceOutputs, groupKeys, aggregates, paramBinder, AggregateMode.ITER_PARTIAL, source.preferShardProjections() ? RowGranularity.SHARD : RowGranularity.NODE, plannerContext.transactionContext().sessionContext().searchPath());
    executionPlan.addProjection(toPartial);
    executionPlan.setDistributionInfo(DistributionInfo.DEFAULT_MODULO);
    GroupProjection toFinal = projectionBuilder.groupProjection(outputs, groupKeys, aggregates, paramBinder, AggregateMode.PARTIAL_FINAL, RowGranularity.CLUSTER, plannerContext.transactionContext().sessionContext().searchPath());
    return createMerge(plannerContext, executionPlan, Collections.singletonList(toFinal), executionPlan.resultDescription().nodeIds());
}
Also used : ExecutionPlan(io.crate.planner.ExecutionPlan) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Symbol(io.crate.expression.symbol.Symbol) GroupProjection(io.crate.execution.dsl.projection.GroupProjection)

Example 2 with GroupProjection

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

the class GroupByOptimizedIterator method getSingleStringKeyGroupProjection.

private static GroupProjection getSingleStringKeyGroupProjection(Collection<? extends Projection> shardProjections) {
    if (shardProjections.size() != 1) {
        return null;
    }
    Projection shardProjection = shardProjections.iterator().next();
    if (!(shardProjection instanceof GroupProjection)) {
        return null;
    }
    GroupProjection groupProjection = (GroupProjection) shardProjection;
    if (groupProjection.keys().size() != 1 || groupProjection.keys().get(0).valueType() != DataTypes.STRING) {
        return null;
    }
    return groupProjection;
}
Also used : Projection(io.crate.execution.dsl.projection.Projection) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) GroupProjection(io.crate.execution.dsl.projection.GroupProjection)

Example 3 with GroupProjection

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

the class ProjectingRowConsumerTest method testConsumerDoesNotRequireScrollYieldsProjectingConsumerWithoutScrollRequirements.

@Test
public void testConsumerDoesNotRequireScrollYieldsProjectingConsumerWithoutScrollRequirements() {
    GroupProjection groupProjection = new GroupProjection(new ArrayList<>(), new ArrayList<>(), AggregateMode.ITER_FINAL, RowGranularity.DOC);
    RowConsumer delegateConsumerRequiresScroll = new DummyRowConsumer(false);
    RowConsumer projectingConsumer = ProjectingRowConsumer.create(delegateConsumerRequiresScroll, Collections.singletonList(groupProjection), UUID.randomUUID(), txnCtx, RamAccounting.NO_ACCOUNTING, memoryManager, projectorFactory);
    assertThat(projectingConsumer.requiresScroll(), is(false));
}
Also used : RowConsumer(io.crate.data.RowConsumer) TestingRowConsumer(io.crate.testing.TestingRowConsumer) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 4 with GroupProjection

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

the class GroupByPlannerTest method testGroupByWithAggregationStringLiteralArguments.

@Test
public void testGroupByWithAggregationStringLiteralArguments() throws IOException {
    var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
    Merge distributedGroupByMerge = e.plan("select count('foo'), name from users group by name");
    RoutedCollectPhase collectPhase = ((RoutedCollectPhase) ((Collect) ((Merge) distributedGroupByMerge.subPlan()).subPlan()).collectPhase());
    assertThat(collectPhase.toCollect(), contains(isReference("name")));
    GroupProjection groupProjection = (GroupProjection) collectPhase.projections().get(0);
    assertThat(groupProjection.values().get(0), isAggregation("count"));
}
Also used : Merge(io.crate.planner.Merge) Collect(io.crate.planner.node.dql.Collect) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 5 with GroupProjection

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

the class GroupByPlannerTest method testGroupByWithAggregationPlan.

@Test
public void testGroupByWithAggregationPlan() throws Exception {
    var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
    Merge distributedGroupByMerge = e.plan("select count(*), name from users group by name");
    Merge reducerMerge = (Merge) distributedGroupByMerge.subPlan();
    // distributed collect
    RoutedCollectPhase collectPhase = ((RoutedCollectPhase) ((Collect) reducerMerge.subPlan()).collectPhase());
    assertThat(collectPhase.maxRowGranularity(), is(RowGranularity.DOC));
    assertThat(collectPhase.nodeIds().size(), is(2));
    assertThat(collectPhase.toCollect().size(), is(1));
    assertThat(collectPhase.projections().size(), is(1));
    assertThat(collectPhase.projections().get(0), instanceOf(GroupProjection.class));
    assertThat(collectPhase.outputTypes().size(), is(2));
    assertEquals(DataTypes.STRING, collectPhase.outputTypes().get(0));
    assertEquals(CountAggregation.LongStateType.INSTANCE, collectPhase.outputTypes().get(1));
    MergePhase mergePhase = reducerMerge.mergePhase();
    assertThat(mergePhase.numUpstreams(), is(2));
    assertThat(mergePhase.nodeIds().size(), is(2));
    assertEquals(mergePhase.inputTypes(), collectPhase.outputTypes());
    // for function evaluation and column-reordering there is always a EvalProjection
    assertThat(mergePhase.projections().size(), is(2));
    assertThat(mergePhase.projections().get(1), instanceOf(EvalProjection.class));
    assertThat(mergePhase.projections().get(0), instanceOf(GroupProjection.class));
    GroupProjection groupProjection = (GroupProjection) mergePhase.projections().get(0);
    InputColumn inputColumn = (InputColumn) groupProjection.values().get(0).inputs().get(0);
    assertThat(inputColumn.index(), is(1));
    assertThat(mergePhase.outputTypes().size(), is(2));
    assertEquals(DataTypes.LONG, mergePhase.outputTypes().get(0));
    assertEquals(DataTypes.STRING, mergePhase.outputTypes().get(1));
    MergePhase localMerge = distributedGroupByMerge.mergePhase();
    assertThat(localMerge.numUpstreams(), is(2));
    assertThat(localMerge.nodeIds().size(), is(1));
    assertThat(localMerge.nodeIds().iterator().next(), is(NODE_ID));
    assertEquals(mergePhase.outputTypes(), localMerge.inputTypes());
    assertThat(localMerge.projections(), empty());
}
Also used : MergePhase(io.crate.execution.dsl.phases.MergePhase) Merge(io.crate.planner.Merge) Collect(io.crate.planner.node.dql.Collect) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) InputColumn(io.crate.expression.symbol.InputColumn) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Aggregations

GroupProjection (io.crate.execution.dsl.projection.GroupProjection)18 Test (org.junit.Test)14 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)13 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)9 RoutedCollectPhase (io.crate.execution.dsl.phases.RoutedCollectPhase)9 Collect (io.crate.planner.node.dql.Collect)9 MergePhase (io.crate.execution.dsl.phases.MergePhase)7 EvalProjection (io.crate.execution.dsl.projection.EvalProjection)7 InputColumn (io.crate.expression.symbol.InputColumn)5 FilterProjection (io.crate.execution.dsl.projection.FilterProjection)4 Projection (io.crate.execution.dsl.projection.Projection)4 Symbol (io.crate.expression.symbol.Symbol)4 Merge (io.crate.planner.Merge)4 CollectPhase (io.crate.execution.dsl.phases.CollectPhase)3 OrderedTopNProjection (io.crate.execution.dsl.projection.OrderedTopNProjection)3 TopNProjection (io.crate.execution.dsl.projection.TopNProjection)3 CountAggregation (io.crate.execution.engine.aggregation.impl.CountAggregation)3 Aggregation (io.crate.expression.symbol.Aggregation)3 Reference (io.crate.metadata.Reference)3 TestingRowConsumer (io.crate.testing.TestingRowConsumer)3