Search in sources :

Example 6 with GroupProjection

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

the class GroupByScalarPlannerTest method testGroupByWithMultipleScalarPlan.

@Test
public void testGroupByWithMultipleScalarPlan() throws Exception {
    Merge merge = e.plan("select abs(id + 1) from users group by id");
    Collect collect = (Collect) merge.subPlan();
    RoutedCollectPhase collectPhase = ((RoutedCollectPhase) collect.collectPhase());
    assertEquals(DataTypes.LONG, collectPhase.outputTypes().get(0));
    assertThat(collectPhase.maxRowGranularity(), is(RowGranularity.DOC));
    assertThat(collectPhase.projections().size(), is(2));
    assertThat(collectPhase.projections().get(0), instanceOf(GroupProjection.class));
    assertThat(collectPhase.projections().get(0).requiredGranularity(), is(RowGranularity.SHARD));
    assertThat(collectPhase.projections().get(1), instanceOf(EvalProjection.class));
    assertThat(collectPhase.projections().get(1).outputs().get(0), isFunction("abs"));
    assertThat(collectPhase.toCollect(), contains(isReference("id", DataTypes.LONG)));
    GroupProjection groupProjection = (GroupProjection) collectPhase.projections().get(0);
    assertThat(groupProjection.keys().get(0).valueType(), is(DataTypes.LONG));
    MergePhase mergePhase = merge.mergePhase();
    assertEquals(DataTypes.LONG, mergePhase.inputTypes().iterator().next());
    assertEquals(DataTypes.LONG, mergePhase.outputTypes().get(0));
}
Also used : MergePhase(io.crate.execution.dsl.phases.MergePhase) Collect(io.crate.planner.node.dql.Collect) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 7 with GroupProjection

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

the class GroupByScalarPlannerTest method testGroupByScalarWithMultipleColumnArgumentsPlan.

@Test
public void testGroupByScalarWithMultipleColumnArgumentsPlan() throws Exception {
    Merge merge = e.plan("select abs(id + other_id) from users group by id, other_id");
    Merge subplan = (Merge) merge.subPlan();
    Collect collect = (Collect) subplan.subPlan();
    RoutedCollectPhase collectPhase = ((RoutedCollectPhase) collect.collectPhase());
    assertThat(collectPhase.projections().size(), is(1));
    assertThat(collectPhase.projections().get(0), instanceOf(GroupProjection.class));
    assertThat(collectPhase.projections().get(0).requiredGranularity(), is(RowGranularity.SHARD));
    assertThat(collectPhase.toCollect(), contains(isReference("id", DataTypes.LONG), isReference("other_id", DataTypes.LONG)));
    GroupProjection groupProjection = (GroupProjection) collectPhase.projections().get(0);
    assertThat(groupProjection.keys().size(), is(2));
    assertThat(groupProjection.keys().get(0).valueType(), is(DataTypes.LONG));
    assertThat(groupProjection.keys().get(1).valueType(), is(DataTypes.LONG));
    MergePhase mergePhase = subplan.mergePhase();
    assertThat(mergePhase.projections().size(), is(2));
    assertThat(mergePhase.projections().get(0), instanceOf(GroupProjection.class));
    assertThat(mergePhase.projections().get(1), instanceOf(EvalProjection.class));
    assertThat(mergePhase.projections().get(1).outputs(), contains(isFunction("abs")));
}
Also used : MergePhase(io.crate.execution.dsl.phases.MergePhase) Collect(io.crate.planner.node.dql.Collect) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 8 with GroupProjection

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

the class GroupByPlannerTest method testNestedGroupByAggregation.

@Test
public void testNestedGroupByAggregation() throws Exception {
    var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).build();
    Collect collect = e.plan("select count(*) from (" + "  select max(load['1']) as maxLoad, hostname " + "  from sys.nodes " + "  group by hostname having max(load['1']) > 50) as nodes " + "group by hostname");
    assertThat("would require merge if more than 1 nodeIds", collect.nodeIds().size(), is(1));
    CollectPhase collectPhase = collect.collectPhase();
    assertThat(collectPhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(FilterProjection.class), instanceOf(EvalProjection.class), instanceOf(GroupProjection.class), instanceOf(EvalProjection.class)));
    Projection firstGroupProjection = collectPhase.projections().get(0);
    assertThat(((GroupProjection) firstGroupProjection).mode(), is(AggregateMode.ITER_FINAL));
    Projection secondGroupProjection = collectPhase.projections().get(3);
    assertThat(((GroupProjection) secondGroupProjection).mode(), is(AggregateMode.ITER_FINAL));
}
Also used : Collect(io.crate.planner.node.dql.Collect) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) FilterProjection(io.crate.execution.dsl.projection.FilterProjection) Projection(io.crate.execution.dsl.projection.Projection) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) CollectPhase(io.crate.execution.dsl.phases.CollectPhase) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 9 with GroupProjection

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

the class GroupByPlannerTest method testGroupByHaving.

@Test
public void testGroupByHaving() throws Exception {
    var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
    Merge distributedGroupByMerge = e.plan("select avg(date), name from users group by name having min(date) > '1970-01-01'");
    Merge reduceMerge = (Merge) distributedGroupByMerge.subPlan();
    CollectPhase collectPhase = ((Collect) reduceMerge.subPlan()).collectPhase();
    assertThat(collectPhase.projections().size(), is(1));
    assertThat(collectPhase.projections().get(0), instanceOf(GroupProjection.class));
    MergePhase reducePhase = reduceMerge.mergePhase();
    assertThat(reducePhase.projections().size(), is(3));
    // grouping
    assertThat(reducePhase.projections().get(0), instanceOf(GroupProjection.class));
    GroupProjection groupProjection = (GroupProjection) reducePhase.projections().get(0);
    assertThat(groupProjection.values().size(), is(2));
    // filter the having clause
    assertThat(reducePhase.projections().get(1), instanceOf(FilterProjection.class));
    FilterProjection filterProjection = (FilterProjection) reducePhase.projections().get(1);
    assertThat(reducePhase.projections().get(2), instanceOf(EvalProjection.class));
    EvalProjection eval = (EvalProjection) reducePhase.projections().get(2);
    assertThat(eval.outputs().get(0).valueType(), Is.<DataType>is(DataTypes.DOUBLE));
    assertThat(eval.outputs().get(1).valueType(), Is.<DataType>is(DataTypes.STRING));
}
Also used : FilterProjection(io.crate.execution.dsl.projection.FilterProjection) 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) CollectPhase(io.crate.execution.dsl.phases.CollectPhase) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 10 with GroupProjection

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

the class GroupByPlannerTest method testCountDistinctWithGroupBy.

@Test
public void testCountDistinctWithGroupBy() throws Exception {
    var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
    Merge distributedGroupByMerge = e.plan("select count(distinct id), name from users group by name order by count(distinct id)");
    Merge reducerMerge = (Merge) distributedGroupByMerge.subPlan();
    CollectPhase collectPhase = ((Collect) reducerMerge.subPlan()).collectPhase();
    // collect
    assertThat(collectPhase.toCollect().get(0), instanceOf(Reference.class));
    assertThat(collectPhase.toCollect().size(), is(2));
    assertThat(((Reference) collectPhase.toCollect().get(0)).column().name(), is("id"));
    assertThat(((Reference) collectPhase.toCollect().get(1)).column().name(), is("name"));
    Projection projection = collectPhase.projections().get(0);
    assertThat(projection, instanceOf(GroupProjection.class));
    GroupProjection groupProjection = (GroupProjection) projection;
    Symbol groupKey = groupProjection.keys().get(0);
    assertThat(groupKey, instanceOf(InputColumn.class));
    assertThat(((InputColumn) groupKey).index(), is(1));
    assertThat(groupProjection.values().size(), is(1));
    assertThat(groupProjection.mode(), is(AggregateMode.ITER_PARTIAL));
    Aggregation aggregation = groupProjection.values().get(0);
    Symbol aggregationInput = aggregation.inputs().get(0);
    assertThat(aggregationInput.symbolType(), is(SymbolType.INPUT_COLUMN));
    // reducer
    MergePhase mergePhase = reducerMerge.mergePhase();
    assertThat(mergePhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(OrderedTopNProjection.class), instanceOf(EvalProjection.class)));
    Projection groupProjection1 = mergePhase.projections().get(0);
    groupProjection = (GroupProjection) groupProjection1;
    assertThat(groupProjection.keys().get(0), instanceOf(InputColumn.class));
    assertThat(((InputColumn) groupProjection.keys().get(0)).index(), is(0));
    assertThat(groupProjection.mode(), is(AggregateMode.PARTIAL_FINAL));
    assertThat(groupProjection.values().get(0), instanceOf(Aggregation.class));
    OrderedTopNProjection topNProjection = (OrderedTopNProjection) mergePhase.projections().get(1);
    Symbol collection_count = topNProjection.outputs().get(0);
    assertThat(collection_count, SymbolMatchers.isInputColumn(0));
    // handler
    MergePhase localMergeNode = distributedGroupByMerge.mergePhase();
    assertThat(localMergeNode.projections(), Matchers.emptyIterable());
}
Also used : SymbolMatchers.isAggregation(io.crate.testing.SymbolMatchers.isAggregation) Aggregation(io.crate.expression.symbol.Aggregation) CountAggregation(io.crate.execution.engine.aggregation.impl.CountAggregation) MergePhase(io.crate.execution.dsl.phases.MergePhase) Merge(io.crate.planner.Merge) Collect(io.crate.planner.node.dql.Collect) Reference(io.crate.metadata.Reference) SymbolMatchers.isReference(io.crate.testing.SymbolMatchers.isReference) Symbol(io.crate.expression.symbol.Symbol) InputColumn(io.crate.expression.symbol.InputColumn) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) FilterProjection(io.crate.execution.dsl.projection.FilterProjection) Projection(io.crate.execution.dsl.projection.Projection) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) CollectPhase(io.crate.execution.dsl.phases.CollectPhase) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) 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