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());
}
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));
}
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));
}
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)));
}
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)"));
}
Aggregations