Search in sources :

Example 71 with Reference

use of io.crate.metadata.Reference in project crate by crate.

the class GroupProjectionTest method testStreaming2.

@Test
public void testStreaming2() throws Exception {
    Reference nameRef = createReference("name", DataTypes.STRING);
    List<Symbol> keys = Collections.singletonList(nameRef);
    List<Aggregation> aggregations = Collections.singletonList(Aggregation.finalAggregation(new FunctionInfo(new FunctionIdent(CountAggregation.NAME, ImmutableList.of()), DataTypes.LONG), ImmutableList.of(), Aggregation.Step.PARTIAL));
    GroupProjection groupProjection = new GroupProjection(keys, aggregations, RowGranularity.CLUSTER);
    BytesStreamOutput out = new BytesStreamOutput();
    Projection.toStream(groupProjection, out);
    StreamInput in = StreamInput.wrap(out.bytes());
    GroupProjection p2 = (GroupProjection) Projection.fromStream(in);
    assertThat(p2.keys().size(), is(1));
    assertThat(p2.values().size(), is(1));
}
Also used : CountAggregation(io.crate.operation.aggregation.impl.CountAggregation) Aggregation(io.crate.analyze.symbol.Aggregation) FunctionIdent(io.crate.metadata.FunctionIdent) TestingHelpers.createReference(io.crate.testing.TestingHelpers.createReference) Reference(io.crate.metadata.Reference) Symbol(io.crate.analyze.symbol.Symbol) StreamInput(org.elasticsearch.common.io.stream.StreamInput) FunctionInfo(io.crate.metadata.FunctionInfo) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 72 with Reference

use of io.crate.metadata.Reference in project crate by crate.

the class OrderByPositionVisitorTest method testSymbols.

@Test
public void testSymbols() throws Exception {
    Reference ref = TestingHelpers.createReference("column", DataTypes.STRING);
    int[] orderByPositions = OrderByPositionVisitor.orderByPositions(ImmutableList.of(ref, new InputColumn(1), new InputColumn(0)), ImmutableList.of(ref, Literal.of(1)));
    assertArrayEquals(new int[] { 0, 1, 0 }, orderByPositions);
}
Also used : Reference(io.crate.metadata.Reference) InputColumn(io.crate.analyze.symbol.InputColumn) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 73 with Reference

use of io.crate.metadata.Reference in project crate by crate.

the class OrderByPositionVisitorTest method testSymbolNotContained.

@Test
public void testSymbolNotContained() throws Exception {
    expectedException.expect(IllegalArgumentException.class);
    expectedException.expectMessage("Cannot sort by: other - not part of source symbols");
    Reference ref = TestingHelpers.createReference("column", DataTypes.STRING);
    OrderByPositionVisitor.orderByPositions(ImmutableList.of(ref, new InputColumn(1), TestingHelpers.createReference("other", DataTypes.LONG)), ImmutableList.of(ref, Literal.BOOLEAN_FALSE));
}
Also used : Reference(io.crate.metadata.Reference) InputColumn(io.crate.analyze.symbol.InputColumn) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 74 with Reference

use of io.crate.metadata.Reference in project crate by crate.

the class MergeNodeTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    Reference nameRef = TestingHelpers.createReference("name", DataTypes.STRING);
    List<Symbol> keys = Collections.singletonList(nameRef);
    List<Aggregation> aggregations = Collections.singletonList(Aggregation.finalAggregation(new FunctionInfo(new FunctionIdent(CountAggregation.NAME, ImmutableList.of()), DataTypes.LONG), ImmutableList.of(), Aggregation.Step.PARTIAL));
    GroupProjection groupProjection = new GroupProjection(keys, aggregations, RowGranularity.CLUSTER);
    TopNProjection topNProjection = new TopNProjection(10, 0, InputColumn.numInputs(keys.size() + aggregations.size()));
    List<Projection> projections = Arrays.asList(groupProjection, topNProjection);
    MergePhase node = new MergePhase(UUID.randomUUID(), 0, "merge", 2, Collections.emptyList(), Arrays.<DataType>asList(DataTypes.UNDEFINED, DataTypes.STRING), projections, DistributionInfo.DEFAULT_BROADCAST, null);
    node.executionNodes(Sets.newHashSet("node1", "node2"));
    BytesStreamOutput output = new BytesStreamOutput();
    node.writeTo(output);
    StreamInput input = StreamInput.wrap(output.bytes());
    MergePhase node2 = MergePhase.FACTORY.create();
    node2.readFrom(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 : Reference(io.crate.metadata.Reference) Symbol(io.crate.analyze.symbol.Symbol) FunctionInfo(io.crate.metadata.FunctionInfo) TopNProjection(io.crate.planner.projection.TopNProjection) GroupProjection(io.crate.planner.projection.GroupProjection) Projection(io.crate.planner.projection.Projection) TopNProjection(io.crate.planner.projection.TopNProjection) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) CountAggregation(io.crate.operation.aggregation.impl.CountAggregation) Aggregation(io.crate.analyze.symbol.Aggregation) FunctionIdent(io.crate.metadata.FunctionIdent) MergePhase(io.crate.planner.node.dql.MergePhase) StreamInput(org.elasticsearch.common.io.stream.StreamInput) GroupProjection(io.crate.planner.projection.GroupProjection) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 75 with Reference

use of io.crate.metadata.Reference in project crate by crate.

the class GroupByPlannerTest method testCountDistinctWithGroupBy.

@Test
public void testCountDistinctWithGroupBy() throws Exception {
    Merge distributedGroupByMerge = e.plan("select count(distinct id), name from users group by name order by count(distinct id)");
    DistributedGroupBy distributedGroupBy = (DistributedGroupBy) distributedGroupByMerge.subPlan();
    RoutedCollectPhase collectPhase = distributedGroupBy.collectPhase();
    // collect
    assertThat(collectPhase.toCollect().get(0), instanceOf(Reference.class));
    assertThat(collectPhase.toCollect().size(), is(2));
    assertThat(((Reference) collectPhase.toCollect().get(0)).ident().columnIdent().name(), is("id"));
    assertThat(((Reference) collectPhase.toCollect().get(1)).ident().columnIdent().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));
    Aggregation aggregation = groupProjection.values().get(0);
    assertThat(aggregation.toStep(), is(Aggregation.Step.PARTIAL));
    Symbol aggregationInput = aggregation.inputs().get(0);
    assertThat(aggregationInput.symbolType(), is(SymbolType.INPUT_COLUMN));
    // reducer
    MergePhase mergePhase = distributedGroupBy.reducerMergeNode();
    assertThat(mergePhase.projections().size(), is(2));
    Projection groupProjection1 = mergePhase.projections().get(0);
    assertThat(groupProjection1, instanceOf(GroupProjection.class));
    groupProjection = (GroupProjection) groupProjection1;
    assertThat(groupProjection.keys().get(0), instanceOf(InputColumn.class));
    assertThat(((InputColumn) groupProjection.keys().get(0)).index(), is(0));
    assertThat(groupProjection.values().get(0), instanceOf(Aggregation.class));
    Aggregation aggregationStep2 = groupProjection.values().get(0);
    assertThat(aggregationStep2.toStep(), is(Aggregation.Step.FINAL));
    OrderedTopNProjection topNProjection = (OrderedTopNProjection) mergePhase.projections().get(1);
    Symbol collection_count = topNProjection.outputs().get(0);
    assertThat(collection_count, instanceOf(Function.class));
    // handler
    MergePhase localMergeNode = distributedGroupByMerge.mergePhase();
    assertThat(localMergeNode.projections(), empty());
}
Also used : CountAggregation(io.crate.operation.aggregation.impl.CountAggregation) SymbolMatchers.isAggregation(io.crate.testing.SymbolMatchers.isAggregation) MergePhase(io.crate.planner.node.dql.MergePhase) Merge(io.crate.planner.Merge) Reference(io.crate.metadata.Reference) SymbolMatchers.isReference(io.crate.testing.SymbolMatchers.isReference) DistributedGroupBy(io.crate.planner.node.dql.DistributedGroupBy) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

Reference (io.crate.metadata.Reference)84 Test (org.junit.Test)57 CrateUnitTest (io.crate.test.integration.CrateUnitTest)55 TestingHelpers.mapToSortedString (io.crate.testing.TestingHelpers.mapToSortedString)27 HashMap (java.util.HashMap)20 Map (java.util.Map)20 NestedObjectExpression (io.crate.operation.reference.NestedObjectExpression)17 ColumnIdent (io.crate.metadata.ColumnIdent)15 Symbol (io.crate.analyze.symbol.Symbol)14 BytesRef (org.apache.lucene.util.BytesRef)11 TableIdent (io.crate.metadata.TableIdent)9 ReferenceIdent (io.crate.metadata.ReferenceIdent)8 InputColumn (io.crate.analyze.symbol.InputColumn)6 RoutedCollectPhase (io.crate.planner.node.dql.RoutedCollectPhase)6 ShardId (org.elasticsearch.index.shard.ShardId)5 Routing (io.crate.metadata.Routing)4 UUID (java.util.UUID)4 OrderBy (io.crate.analyze.OrderBy)3 ShardResponse (io.crate.executor.transport.ShardResponse)3 ShardUpsertRequest (io.crate.executor.transport.ShardUpsertRequest)3