Search in sources :

Example 16 with Collect

use of io.crate.planner.node.dql.Collect in project crate by crate.

the class CopyToPlannerTest method testCopyToWithPartitionedGeneratedColumn.

@Test
public void testCopyToWithPartitionedGeneratedColumn() throws Exception {
    // test that generated partition column is NOT exported
    Merge plan = e.plan("copy parted_generated to directory '/tmp'");
    Collect innerPlan = (Collect) plan.subPlan();
    RoutedCollectPhase node = ((RoutedCollectPhase) innerPlan.collectPhase());
    WriterProjection projection = (WriterProjection) node.projections().get(0);
    assertThat(projection.overwrites().size(), is(0));
}
Also used : WriterProjection(io.crate.planner.projection.WriterProjection) Collect(io.crate.planner.node.dql.Collect) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 17 with Collect

use of io.crate.planner.node.dql.Collect in project crate by crate.

the class CopyToPlannerTest method testCopyToWithColumnsReferenceRewrite.

@Test
public void testCopyToWithColumnsReferenceRewrite() throws Exception {
    Merge plan = e.plan("copy users (name) to directory '/tmp'");
    Collect innerPlan = (Collect) plan.subPlan();
    RoutedCollectPhase node = ((RoutedCollectPhase) innerPlan.collectPhase());
    Reference nameRef = (Reference) node.toCollect().get(0);
    assertThat(nameRef.ident().columnIdent().name(), is(DocSysColumns.DOC.name()));
    assertThat(nameRef.ident().columnIdent().path().get(0), is("name"));
}
Also used : Collect(io.crate.planner.node.dql.Collect) Reference(io.crate.metadata.Reference) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 18 with Collect

use of io.crate.planner.node.dql.Collect in project crate by crate.

the class CopyStatementPlanner method planCopyFrom.

public Plan planCopyFrom(CopyFromAnalyzedStatement analysis, Planner.Context context) {
    /**
         * copy from has two "modes":
         *
         * 1: non-partitioned tables or partitioned tables with partition ident --> import into single es index
         *    -> collect raw source and import as is
         *
         * 2: partitioned table without partition ident
         *    -> collect document and partition by values
         *    -> exclude partitioned by columns from document
         *    -> insert into es index (partition determined by partition by value)
         */
    DocTableInfo table = analysis.table();
    int clusteredByPrimaryKeyIdx = table.primaryKey().indexOf(analysis.table().clusteredBy());
    List<String> partitionedByNames;
    String partitionIdent = null;
    List<BytesRef> partitionValues;
    if (analysis.partitionIdent() == null) {
        if (table.isPartitioned()) {
            partitionedByNames = Lists.newArrayList(Lists.transform(table.partitionedBy(), ColumnIdent::fqn));
        } else {
            partitionedByNames = Collections.emptyList();
        }
        partitionValues = ImmutableList.of();
    } else {
        assert table.isPartitioned() : "table must be partitioned if partitionIdent is set";
        // partitionIdent is present -> possible to index raw source into concrete es index
        partitionValues = PartitionName.decodeIdent(analysis.partitionIdent());
        partitionIdent = analysis.partitionIdent();
        partitionedByNames = Collections.emptyList();
    }
    SourceIndexWriterProjection sourceIndexWriterProjection = new SourceIndexWriterProjection(table.ident(), partitionIdent, table.getReference(DocSysColumns.RAW), table.primaryKey(), table.partitionedBy(), partitionValues, table.clusteredBy(), clusteredByPrimaryKeyIdx, analysis.settings(), null, partitionedByNames.size() > 0 ? partitionedByNames.toArray(new String[partitionedByNames.size()]) : null, // autoCreateIndices
    table.isPartitioned());
    List<Projection> projections = Collections.<Projection>singletonList(sourceIndexWriterProjection);
    partitionedByNames.removeAll(Lists.transform(table.primaryKey(), ColumnIdent::fqn));
    int referencesSize = table.primaryKey().size() + partitionedByNames.size() + 1;
    referencesSize = clusteredByPrimaryKeyIdx == -1 ? referencesSize + 1 : referencesSize;
    List<Symbol> toCollect = new ArrayList<>(referencesSize);
    // add primaryKey columns
    for (ColumnIdent primaryKey : table.primaryKey()) {
        toCollect.add(table.getReference(primaryKey));
    }
    // add partitioned columns (if not part of primaryKey)
    Set<Reference> referencedReferences = new HashSet<>();
    for (String partitionedColumn : partitionedByNames) {
        Reference reference = table.getReference(ColumnIdent.fromPath(partitionedColumn));
        Symbol symbol;
        if (reference instanceof GeneratedReference) {
            symbol = ((GeneratedReference) reference).generatedExpression();
            referencedReferences.addAll(((GeneratedReference) reference).referencedReferences());
        } else {
            symbol = reference;
        }
        toCollect.add(symbol);
    }
    // add clusteredBy column (if not part of primaryKey)
    if (clusteredByPrimaryKeyIdx == -1 && table.clusteredBy() != null && !DocSysColumns.ID.equals(table.clusteredBy())) {
        toCollect.add(table.getReference(table.clusteredBy()));
    }
    // add _raw or _doc
    if (table.isPartitioned() && analysis.partitionIdent() == null) {
        toCollect.add(table.getReference(DocSysColumns.DOC));
    } else {
        toCollect.add(table.getReference(DocSysColumns.RAW));
    }
    // add columns referenced by generated columns which are used as partitioned by column
    for (Reference reference : referencedReferences) {
        if (!toCollect.contains(reference)) {
            toCollect.add(reference);
        }
    }
    DiscoveryNodes allNodes = clusterService.state().nodes();
    FileUriCollectPhase collectPhase = new FileUriCollectPhase(context.jobId(), context.nextExecutionPhaseId(), "copyFrom", getExecutionNodes(allNodes, analysis.settings().getAsInt("num_readers", allNodes.getSize()), analysis.nodePredicate()), analysis.uri(), toCollect, projections, analysis.settings().get("compression", null), analysis.settings().getAsBoolean("shared", null));
    Collect collect = new Collect(collectPhase, TopN.NO_LIMIT, 0, 1, 1, null);
    return Merge.ensureOnHandler(collect, context, Collections.singletonList(MergeCountProjection.INSTANCE));
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) GeneratedReference(io.crate.metadata.GeneratedReference) Collect(io.crate.planner.node.dql.Collect) Symbol(io.crate.analyze.symbol.Symbol) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference) SourceIndexWriterProjection(io.crate.planner.projection.SourceIndexWriterProjection) WriterProjection(io.crate.planner.projection.WriterProjection) MergeCountProjection(io.crate.planner.projection.MergeCountProjection) SourceIndexWriterProjection(io.crate.planner.projection.SourceIndexWriterProjection) Projection(io.crate.planner.projection.Projection) FileUriCollectPhase(io.crate.planner.node.dql.FileUriCollectPhase) ColumnIdent(io.crate.metadata.ColumnIdent) BytesRef(org.apache.lucene.util.BytesRef) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 19 with Collect

use of io.crate.planner.node.dql.Collect in project crate by crate.

the class InsertPlannerTest method testInsertFromSubQueryReduceOnCollectorGroupBy.

@Test
public void testInsertFromSubQueryReduceOnCollectorGroupBy() throws Exception {
    Merge merge = e.plan("insert into users (id, name) (select id, arbitrary(name) from users group by id)");
    Collect collect = (Collect) merge.subPlan();
    RoutedCollectPhase collectPhase = ((RoutedCollectPhase) collect.collectPhase());
    assertThat(collectPhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(EvalProjection.class), instanceOf(ColumnIndexWriterProjection.class)));
    ColumnIndexWriterProjection columnIndexWriterProjection = (ColumnIndexWriterProjection) collectPhase.projections().get(2);
    assertThat(columnIndexWriterProjection.columnReferences(), contains(isReference("id"), isReference("name")));
    MergePhase mergePhase = merge.mergePhase();
    assertThat(mergePhase.projections(), contains(instanceOf(MergeCountProjection.class)));
}
Also used : MergePhase(io.crate.planner.node.dql.MergePhase) Collect(io.crate.planner.node.dql.Collect) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 20 with Collect

use of io.crate.planner.node.dql.Collect in project crate by crate.

the class InsertPlannerTest method testInsertFromSubQueryWithoutLimit.

@Test
public void testInsertFromSubQueryWithoutLimit() throws Exception {
    Merge planNode = e.plan("insert into users (id, name) (select id, name from users)");
    Collect collect = (Collect) planNode.subPlan();
    RoutedCollectPhase collectPhase = ((RoutedCollectPhase) collect.collectPhase());
    assertThat(collectPhase.projections().size(), is(1));
    assertThat(collectPhase.projections().get(0), instanceOf(ColumnIndexWriterProjection.class));
    MergePhase localMergeNode = planNode.mergePhase();
    assertThat(localMergeNode.projections().size(), is(1));
    assertThat(localMergeNode.projections().get(0), instanceOf(MergeCountProjection.class));
}
Also used : MergePhase(io.crate.planner.node.dql.MergePhase) Collect(io.crate.planner.node.dql.Collect) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

Collect (io.crate.planner.node.dql.Collect)21 RoutedCollectPhase (io.crate.planner.node.dql.RoutedCollectPhase)18 CrateUnitTest (io.crate.test.integration.CrateUnitTest)17 Test (org.junit.Test)17 MergePhase (io.crate.planner.node.dql.MergePhase)9 Merge (io.crate.planner.Merge)8 Reference (io.crate.metadata.Reference)6 Symbol (io.crate.analyze.symbol.Symbol)3 InputColumn (io.crate.analyze.symbol.InputColumn)2 PositionalOrderBy (io.crate.planner.PositionalOrderBy)2 DeleteProjection (io.crate.planner.projection.DeleteProjection)2 MergeCountProjection (io.crate.planner.projection.MergeCountProjection)2 Projection (io.crate.planner.projection.Projection)2 WriterProjection (io.crate.planner.projection.WriterProjection)2 Function (io.crate.analyze.symbol.Function)1 ColumnIdent (io.crate.metadata.ColumnIdent)1 GeneratedReference (io.crate.metadata.GeneratedReference)1 ReferenceIdent (io.crate.metadata.ReferenceIdent)1 Routing (io.crate.metadata.Routing)1 DocTableInfo (io.crate.metadata.doc.DocTableInfo)1