Search in sources :

Example 6 with FilterProjection

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

the class GroupByPlannerTest method testGroupByWithHavingAndNoLimit.

@Test
public void testGroupByWithHavingAndNoLimit() throws Exception {
    var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
    Merge planNode = e.plan("select count(*), name from users group by name having count(*) > 1");
    Merge reducerMerge = (Merge) planNode.subPlan();
    // reducer
    MergePhase mergePhase = reducerMerge.mergePhase();
    // group projection
    // outputs: name, count(*)
    Projection projection = mergePhase.projections().get(1);
    assertThat(projection, instanceOf(FilterProjection.class));
    FilterProjection filterProjection = (FilterProjection) projection;
    Symbol countArgument = ((Function) filterProjection.query()).arguments().get(0);
    assertThat(countArgument, instanceOf(InputColumn.class));
    // pointing to second output from group projection
    assertThat(((InputColumn) countArgument).index(), is(1));
    assertThat(mergePhase.outputTypes().get(0), equalTo(DataTypes.LONG));
    assertThat(mergePhase.outputTypes().get(1), equalTo(DataTypes.STRING));
    mergePhase = planNode.mergePhase();
    assertThat(mergePhase.outputTypes().get(0), equalTo(DataTypes.LONG));
    assertThat(mergePhase.outputTypes().get(1), equalTo(DataTypes.STRING));
}
Also used : FilterProjection(io.crate.execution.dsl.projection.FilterProjection) MergePhase(io.crate.execution.dsl.phases.MergePhase) Merge(io.crate.planner.Merge) 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) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 7 with FilterProjection

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

the class GroupByPlannerTest method testGroupByWithHavingAndLimit.

@Test
public void testGroupByWithHavingAndLimit() throws Exception {
    var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
    Merge planNode = e.plan("select count(*), name from users group by name having count(*) > 1 limit 100");
    Merge reducerMerge = (Merge) planNode.subPlan();
    // reducer
    MergePhase mergePhase = reducerMerge.mergePhase();
    Projection projection = mergePhase.projections().get(1);
    assertThat(projection, instanceOf(FilterProjection.class));
    FilterProjection filterProjection = (FilterProjection) projection;
    Symbol countArgument = ((Function) filterProjection.query()).arguments().get(0);
    assertThat(countArgument, instanceOf(InputColumn.class));
    // pointing to second output from group projection
    assertThat(((InputColumn) countArgument).index(), is(1));
    // outputs: name, count(*)
    TopNProjection topN = (TopNProjection) mergePhase.projections().get(2);
    assertThat(topN.outputs().get(0).valueType(), Is.is(DataTypes.STRING));
    assertThat(topN.outputs().get(1).valueType(), Is.<DataType>is(DataTypes.LONG));
    MergePhase localMerge = planNode.mergePhase();
    // topN projection
    // outputs: count(*), name
    topN = (TopNProjection) localMerge.projections().get(0);
    assertThat(topN.outputs().get(0).valueType(), Is.<DataType>is(DataTypes.LONG));
    assertThat(topN.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) 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) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 8 with FilterProjection

use of io.crate.execution.dsl.projection.FilterProjection 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 9 with FilterProjection

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

the class ProjectorsTest method testProjectionsWithCorrectGranularityAreApplied.

@Test
public void testProjectionsWithCorrectGranularityAreApplied() {
    GroupProjection groupProjection = new GroupProjection(new ArrayList<>(), new ArrayList<>(), AggregateMode.ITER_FINAL, RowGranularity.SHARD);
    FilterProjection filterProjection = new FilterProjection(new InputColumn(0), Collections.emptyList());
    filterProjection.requiredGranularity(RowGranularity.DOC);
    Projectors projectors = new Projectors(Arrays.asList(filterProjection, groupProjection), UUID.randomUUID(), CoordinatorTxnCtx.systemTransactionContext(), RamAccounting.NO_ACCOUNTING, memoryManager, projectorFactory);
    assertThat(projectors.providesIndependentScroll(), is(true));
    assertThat(projectors.projectors.size(), is(1));
    assertThat(projectors.projectors.get(0), instanceOf(GroupingProjector.class));
}
Also used : FilterProjection(io.crate.execution.dsl.projection.FilterProjection) InputColumn(io.crate.expression.symbol.InputColumn) GroupingProjector(io.crate.execution.engine.aggregation.GroupingProjector) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 10 with FilterProjection

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

the class InsertPlannerTest method testGroupByHavingInsertInto.

@Test
public void testGroupByHavingInsertInto() {
    Merge planNode = e.plan("insert into users (id, name) (select name, count(*) from users group by name having count(*) > 3)");
    Merge groupByNode = (Merge) planNode.subPlan();
    MergePhase mergePhase = groupByNode.mergePhase();
    assertThat(mergePhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(FilterProjection.class), instanceOf(EvalProjection.class), instanceOf(ColumnIndexWriterProjection.class)));
    FilterProjection filterProjection = (FilterProjection) mergePhase.projections().get(1);
    assertThat(filterProjection.outputs().size(), is(2));
    assertThat(filterProjection.outputs().get(0), instanceOf(InputColumn.class));
    assertThat(filterProjection.outputs().get(1), instanceOf(InputColumn.class));
    InputColumn inputColumn = (InputColumn) filterProjection.outputs().get(0);
    assertThat(inputColumn.index(), is(0));
    inputColumn = (InputColumn) filterProjection.outputs().get(1);
    assertThat(inputColumn.index(), is(1));
    MergePhase localMergeNode = planNode.mergePhase();
    assertThat(localMergeNode.projections().size(), is(1));
    assertThat(localMergeNode.projections().get(0), instanceOf(MergeCountProjection.class));
    assertThat(localMergeNode.finalProjection().get().outputs().size(), is(1));
}
Also used : MergeCountProjection(io.crate.execution.dsl.projection.MergeCountProjection) FilterProjection(io.crate.execution.dsl.projection.FilterProjection) MergePhase(io.crate.execution.dsl.phases.MergePhase) InputColumn(io.crate.expression.symbol.InputColumn) SymbolMatchers.isInputColumn(io.crate.testing.SymbolMatchers.isInputColumn) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Aggregations

FilterProjection (io.crate.execution.dsl.projection.FilterProjection)10 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)9 Test (org.junit.Test)9 InputColumn (io.crate.expression.symbol.InputColumn)8 Symbol (io.crate.expression.symbol.Symbol)7 MergePhase (io.crate.execution.dsl.phases.MergePhase)6 GroupProjection (io.crate.execution.dsl.projection.GroupProjection)6 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)5 EvalProjection (io.crate.execution.dsl.projection.EvalProjection)5 Merge (io.crate.planner.Merge)5 OrderedTopNProjection (io.crate.execution.dsl.projection.OrderedTopNProjection)4 Projection (io.crate.execution.dsl.projection.Projection)4 TopNProjection (io.crate.execution.dsl.projection.TopNProjection)4 GroupingProjector (io.crate.execution.engine.aggregation.GroupingProjector)2 EqOperator (io.crate.expression.operator.EqOperator)2 Function (io.crate.expression.symbol.Function)2 TestingRowConsumer (io.crate.testing.TestingRowConsumer)2 Bucket (io.crate.data.Bucket)1 CollectionBucket (io.crate.data.CollectionBucket)1 Projector (io.crate.data.Projector)1