Search in sources :

Example 6 with Projection

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

the class GroupByPlannerTest method testGroupByWithHavingAndNoSelectListReordering.

@Test
public void testGroupByWithHavingAndNoSelectListReordering() throws Exception {
    var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
    Merge planNode = e.plan("select name, count(*) from users group by name having count(*) > 1");
    Merge reducerMerge = (Merge) planNode.subPlan();
    MergePhase reduceMergePhase = reducerMerge.mergePhase();
    // group projection
    // outputs: name, count(*)
    // filter projection
    // outputs: name, count(*)
    assertThat(reduceMergePhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(FilterProjection.class)));
    Projection projection = reduceMergePhase.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(*)
    assertThat(((InputColumn) filterProjection.outputs().get(0)).index(), is(0));
    assertThat(((InputColumn) filterProjection.outputs().get(1)).index(), is(1));
    MergePhase localMerge = planNode.mergePhase();
    assertThat(localMerge.projections(), empty());
}
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 Projection

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

the class GroupByPlannerTest method testGroupByHavingAndNoSelectListReOrderingWithLimit.

@Test
public void testGroupByHavingAndNoSelectListReOrderingWithLimit() throws Exception {
    var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
    Merge planNode = e.plan("select name, count(*) from users group by name having count(*) > 1 limit 100");
    Merge reducerMerge = (Merge) planNode.subPlan();
    MergePhase reducePhase = reducerMerge.mergePhase();
    // group projection
    // outputs: name, count(*)
    // filter projection
    // outputs: name, count(*)
    // topN projection
    // outputs: name, count(*)
    Projection projection = reducePhase.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(*)
    assertThat(((InputColumn) filterProjection.outputs().get(0)).index(), is(0));
    assertThat(((InputColumn) filterProjection.outputs().get(1)).index(), is(1));
    // outputs: name, count(*)
    TopNProjection topN = (TopNProjection) reducePhase.projections().get(2);
    assertThat(((InputColumn) topN.outputs().get(0)).index(), is(0));
    assertThat(((InputColumn) topN.outputs().get(1)).index(), is(1));
    MergePhase localMerge = planNode.mergePhase();
    // topN projection
    // outputs: name, count(*)
    topN = (TopNProjection) localMerge.projections().get(0);
    assertThat(((InputColumn) topN.outputs().get(0)).index(), is(0));
    assertThat(((InputColumn) topN.outputs().get(1)).index(), is(1));
}
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 Projection

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

the class GroupByPlannerTest method testNonDistributedGroupByOnClusteredColumnSorted.

@Test
public void testNonDistributedGroupByOnClusteredColumnSorted() throws Exception {
    var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
    Merge merge = e.plan("select count(*), id from users group by id order by 1 desc nulls last limit 20");
    Collect collect = ((Collect) merge.subPlan());
    RoutedCollectPhase collectPhase = ((RoutedCollectPhase) collect.collectPhase());
    List<Projection> collectProjections = collectPhase.projections();
    assertThat(collectProjections, contains(instanceOf(GroupProjection.class), instanceOf(OrderedTopNProjection.class), // swap id, count(*) -> count(*), id
    instanceOf(EvalProjection.class)));
    assertThat(collectProjections.get(1), instanceOf(OrderedTopNProjection.class));
    assertThat(((OrderedTopNProjection) collectProjections.get(1)).orderBy().size(), is(1));
    assertThat(collectProjections.get(0).requiredGranularity(), is(RowGranularity.SHARD));
    MergePhase mergePhase = merge.mergePhase();
    assertThat(mergePhase.projections(), contains(instanceOf(TopNProjection.class)));
    PositionalOrderBy positionalOrderBy = mergePhase.orderByPositions();
    assertThat(positionalOrderBy, notNullValue());
    assertThat(positionalOrderBy.indices().length, is(1));
    assertThat(positionalOrderBy.indices()[0], is(0));
    assertThat(positionalOrderBy.reverseFlags()[0], is(true));
    assertThat(positionalOrderBy.nullsFirst()[0], is(false));
}
Also used : PositionalOrderBy(io.crate.planner.PositionalOrderBy) MergePhase(io.crate.execution.dsl.phases.MergePhase) Merge(io.crate.planner.Merge) 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) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) 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 Projection

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

the class SubQueryPlannerTest method testJoinWithGlobalAggregationOnSubSelectsWithLimitAndOffset.

@Test
public void testJoinWithGlobalAggregationOnSubSelectsWithLimitAndOffset() throws Exception {
    Join join = e.plan("select count(*) from " + " (select i, a from t1 order by a limit 10 offset 2) t1 " + "join" + " (select i from t2 order by i desc limit 5 offset 5) t2 " + "on t1.i = t2.i");
    QueryThenFetch leftQtf = (QueryThenFetch) join.left();
    Collect left = (Collect) leftQtf.subPlan();
    assertThat("1 node, otherwise mergePhases would be required", left.nodeIds().size(), is(1));
    assertThat(left.collectPhase().toCollect(), isSQL("doc.t1._fetchid, doc.t1.a"));
    assertThat(((RoutedCollectPhase) left.collectPhase()).orderBy(), isSQL("doc.t1.a"));
    assertThat(left.collectPhase().projections(), contains(isTopN(10, 2), instanceOf(FetchProjection.class)));
    Collect right = (Collect) join.right();
    assertThat("1 node, otherwise mergePhases would be required", right.nodeIds().size(), is(1));
    assertThat(((RoutedCollectPhase) right.collectPhase()).orderBy(), isSQL("doc.t2.i DESC"));
    assertThat(right.collectPhase().projections(), contains(isTopN(5, 5)));
    List<Projection> nlProjections = join.joinPhase().projections();
    assertThat(nlProjections, contains(instanceOf(EvalProjection.class), instanceOf(AggregationProjection.class)));
}
Also used : QueryThenFetch(io.crate.planner.node.dql.QueryThenFetch) Collect(io.crate.planner.node.dql.Collect) Join(io.crate.planner.node.dql.join.Join) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) FilterProjection(io.crate.execution.dsl.projection.FilterProjection) AggregationProjection(io.crate.execution.dsl.projection.AggregationProjection) Projection(io.crate.execution.dsl.projection.Projection) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) FetchProjection(io.crate.execution.dsl.projection.FetchProjection) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 10 with Projection

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

the class SubQueryPlannerTest method testNestedSimpleSelectWithJoin.

@Test
public void testNestedSimpleSelectWithJoin() throws Exception {
    Join nl = e.plan("select t1x from (" + "select t1.x as t1x, t2.i as t2i from t1 as t1, t1 as t2 order by t1x asc limit 10" + ") t order by t1x desc limit 3");
    List<Projection> projections = nl.joinPhase().projections();
    assertThat(projections, Matchers.contains(instanceOf(EvalProjection.class), isTopN(10, 0), instanceOf(OrderedTopNProjection.class), instanceOf(EvalProjection.class), isTopN(3, 0)));
    assertThat(projections.get(0).outputs(), isSQL("INPUT(1), INPUT(1)"));
    assertThat(projections.get(4).outputs(), isSQL("INPUT(0)"));
}
Also used : Join(io.crate.planner.node.dql.join.Join) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) FilterProjection(io.crate.execution.dsl.projection.FilterProjection) AggregationProjection(io.crate.execution.dsl.projection.AggregationProjection) Projection(io.crate.execution.dsl.projection.Projection) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) FetchProjection(io.crate.execution.dsl.projection.FetchProjection) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Aggregations

Projection (io.crate.execution.dsl.projection.Projection)24 Test (org.junit.Test)16 TopNProjection (io.crate.execution.dsl.projection.TopNProjection)15 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)15 FilterProjection (io.crate.execution.dsl.projection.FilterProjection)14 GroupProjection (io.crate.execution.dsl.projection.GroupProjection)14 EvalProjection (io.crate.execution.dsl.projection.EvalProjection)13 OrderedTopNProjection (io.crate.execution.dsl.projection.OrderedTopNProjection)12 Collect (io.crate.planner.node.dql.Collect)12 Symbol (io.crate.expression.symbol.Symbol)10 MergePhase (io.crate.execution.dsl.phases.MergePhase)9 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)8 InputColumn (io.crate.expression.symbol.InputColumn)8 AggregationProjection (io.crate.execution.dsl.projection.AggregationProjection)7 Merge (io.crate.planner.Merge)7 FetchProjection (io.crate.execution.dsl.projection.FetchProjection)5 RoutedCollectPhase (io.crate.execution.dsl.phases.RoutedCollectPhase)4 QueryThenFetch (io.crate.planner.node.dql.QueryThenFetch)3 Join (io.crate.planner.node.dql.join.Join)3 CollectPhase (io.crate.execution.dsl.phases.CollectPhase)2