Search in sources :

Example 6 with TopNProjection

use of io.crate.execution.dsl.projection.TopNProjection 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)

Example 7 with TopNProjection

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

the class GroupByPlannerTest method testGroupByWithAggregationAndLimit.

@Test
public void testGroupByWithAggregationAndLimit() 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 limit 1 offset 1");
    Merge reducerMerge = (Merge) distributedGroupByMerge.subPlan();
    // distributed merge
    MergePhase distributedMergePhase = reducerMerge.mergePhase();
    assertThat(distributedMergePhase.projections().get(0), instanceOf(GroupProjection.class));
    assertThat(distributedMergePhase.projections().get(1), instanceOf(TopNProjection.class));
    // limit must include offset because the real limit can only be applied on the handler
    // after all rows have been gathered.
    TopNProjection topN = (TopNProjection) distributedMergePhase.projections().get(1);
    assertThat(topN.limit(), is(2));
    assertThat(topN.offset(), is(0));
    // local merge
    MergePhase localMergePhase = distributedGroupByMerge.mergePhase();
    assertThat(localMergePhase.projections().get(0), instanceOf(TopNProjection.class));
    topN = (TopNProjection) localMergePhase.projections().get(0);
    assertThat(topN.limit(), is(1));
    assertThat(topN.offset(), is(1));
    assertThat(topN.outputs().get(0), instanceOf(InputColumn.class));
    assertThat(((InputColumn) topN.outputs().get(0)).index(), is(0));
    assertThat(topN.outputs().get(1), instanceOf(InputColumn.class));
    assertThat(((InputColumn) topN.outputs().get(1)).index(), is(1));
}
Also used : MergePhase(io.crate.execution.dsl.phases.MergePhase) Merge(io.crate.planner.Merge) InputColumn(io.crate.expression.symbol.InputColumn) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 8 with TopNProjection

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

the class MergeNodeTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    List<Symbol> keys = Collections.singletonList(new InputColumn(0, DataTypes.STRING));
    List<Aggregation> aggregations = Collections.singletonList(new Aggregation(CountAggregation.COUNT_STAR_SIGNATURE, CountAggregation.COUNT_STAR_SIGNATURE.getReturnType().createType(), Collections.emptyList()));
    GroupProjection groupProjection = new GroupProjection(keys, aggregations, AggregateMode.PARTIAL_FINAL, RowGranularity.CLUSTER);
    TopNProjection topNProjection = new TopNProjection(10, 0, Symbols.typeView(groupProjection.outputs()));
    List<Projection> projections = Arrays.asList(groupProjection, topNProjection);
    MergePhase node = new MergePhase(UUID.randomUUID(), 0, "merge", 2, 1, Set.of("node1", "node2"), List.of(DataTypes.UNDEFINED, DataTypes.STRING), projections, DistributionInfo.DEFAULT_BROADCAST, null);
    BytesStreamOutput output = new BytesStreamOutput();
    node.writeTo(output);
    StreamInput input = output.bytes().streamInput();
    MergePhase node2 = new MergePhase(input);
    assertThat(node.numUpstreams(), is(node2.numUpstreams()));
    assertThat(node.nodeIds(), is(node2.nodeIds()));
    assertThat(node.jobId(), is(node2.jobId()));
    assertEquals(node.inputTypes(), node2.inputTypes());
    assertThat(node.phaseId(), is(node2.phaseId()));
    assertThat(node.distributionInfo(), is(node2.distributionInfo()));
}
Also used : Aggregation(io.crate.expression.symbol.Aggregation) CountAggregation(io.crate.execution.engine.aggregation.impl.CountAggregation) MergePhase(io.crate.execution.dsl.phases.MergePhase) Symbol(io.crate.expression.symbol.Symbol) InputColumn(io.crate.expression.symbol.InputColumn) StreamInput(org.elasticsearch.common.io.stream.StreamInput) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) Projection(io.crate.execution.dsl.projection.Projection) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test)

Example 9 with TopNProjection

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

the class ProjectionToProjectorVisitorTest method testSimpleTopNProjection.

@Test
public void testSimpleTopNProjection() throws Exception {
    TopNProjection projection = new TopNProjection(10, 2, Collections.singletonList(DataTypes.LONG));
    Projector projector = visitor.create(projection, txnCtx, RamAccounting.NO_ACCOUNTING, memoryManager, UUID.randomUUID());
    assertThat(projector, instanceOf(SimpleTopNProjector.class));
    TestingRowConsumer consumer = new TestingRowConsumer();
    consumer.accept(projector.apply(TestingBatchIterators.range(0, 20)), null);
    List<Object[]> result = consumer.getResult();
    assertThat(result.size(), is(10));
    assertThat(result.get(0), is(new Object[] { 2 }));
}
Also used : Projector(io.crate.data.Projector) SortingProjector(io.crate.execution.engine.sort.SortingProjector) SortingTopNProjector(io.crate.execution.engine.sort.SortingTopNProjector) GroupingProjector(io.crate.execution.engine.aggregation.GroupingProjector) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) TestingRowConsumer(io.crate.testing.TestingRowConsumer) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Aggregations

TopNProjection (io.crate.execution.dsl.projection.TopNProjection)9 MergePhase (io.crate.execution.dsl.phases.MergePhase)6 InputColumn (io.crate.expression.symbol.InputColumn)6 Test (org.junit.Test)6 GroupProjection (io.crate.execution.dsl.projection.GroupProjection)5 OrderedTopNProjection (io.crate.execution.dsl.projection.OrderedTopNProjection)5 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)5 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)4 EvalProjection (io.crate.execution.dsl.projection.EvalProjection)4 Projection (io.crate.execution.dsl.projection.Projection)4 Symbol (io.crate.expression.symbol.Symbol)4 Merge (io.crate.planner.Merge)4 FilterProjection (io.crate.execution.dsl.projection.FilterProjection)3 CountAggregation (io.crate.execution.engine.aggregation.impl.CountAggregation)2 Aggregation (io.crate.expression.symbol.Aggregation)2 Projector (io.crate.data.Projector)1 CollectPhase (io.crate.execution.dsl.phases.CollectPhase)1 RoutedCollectPhase (io.crate.execution.dsl.phases.RoutedCollectPhase)1 TopNDistinctProjection (io.crate.execution.dsl.projection.TopNDistinctProjection)1 GroupingProjector (io.crate.execution.engine.aggregation.GroupingProjector)1