Search in sources :

Example 1 with ColumnIndexWriterProjection

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

the class InsertFromSubQueryPlanner method plan.

public static LogicalPlan plan(AnalyzedInsertStatement statement, PlannerContext plannerContext, LogicalPlanner logicalPlanner, SubqueryPlanner subqueryPlanner) {
    if (statement.outputs() != null && !plannerContext.clusterState().getNodes().getMinNodeVersion().onOrAfter(Version.V_4_2_0)) {
        throw new UnsupportedFeatureException(RETURNING_VERSION_ERROR_MSG);
    }
    List<Reference> targetColsExclPartitionCols = new ArrayList<>(statement.columns().size() - statement.tableInfo().partitionedBy().size());
    for (Reference column : statement.columns()) {
        if (statement.tableInfo().partitionedBy().contains(column.column())) {
            continue;
        }
        targetColsExclPartitionCols.add(column);
    }
    List<Symbol> columnSymbols = InputColumns.create(targetColsExclPartitionCols, new InputColumns.SourceSymbols(statement.columns()));
    // if fields are null default to number of rows imported
    var outputs = statement.outputs() == null ? List.of(new InputColumn(0, DataTypes.LONG)) : statement.outputs();
    ColumnIndexWriterProjection indexWriterProjection = new ColumnIndexWriterProjection(statement.tableInfo().ident(), null, statement.tableInfo().primaryKey(), statement.columns(), targetColsExclPartitionCols, columnSymbols, statement.isIgnoreDuplicateKeys(), statement.onDuplicateKeyAssignments(), statement.primaryKeySymbols(), statement.partitionedBySymbols(), statement.tableInfo().clusteredBy(), statement.clusteredBySymbol(), Settings.EMPTY, statement.tableInfo().isPartitioned(), outputs, statement.outputs() == null ? List.of() : statement.outputs());
    LogicalPlan plannedSubQuery = logicalPlanner.plan(statement.subQueryRelation(), plannerContext, subqueryPlanner, true);
    EvalProjection castOutputs = EvalProjection.castValues(Symbols.typeView(statement.columns()), plannedSubQuery.outputs());
    return new Insert(plannedSubQuery, indexWriterProjection, castOutputs);
}
Also used : InputColumns(io.crate.execution.dsl.projection.builder.InputColumns) UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) Reference(io.crate.metadata.Reference) Symbol(io.crate.expression.symbol.Symbol) ArrayList(java.util.ArrayList) ColumnIndexWriterProjection(io.crate.execution.dsl.projection.ColumnIndexWriterProjection) Insert(io.crate.planner.operators.Insert) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) InputColumn(io.crate.expression.symbol.InputColumn) LogicalPlan(io.crate.planner.operators.LogicalPlan)

Example 2 with ColumnIndexWriterProjection

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

the class InsertPlannerTest method testInsertFromSubQueryDistributedGroupByWithoutLimit.

@Test
public void testInsertFromSubQueryDistributedGroupByWithoutLimit() {
    Merge planNode = e.plan("insert into users (id, name) (select name, count(*) from users group by name)");
    Merge groupBy = (Merge) planNode.subPlan();
    MergePhase mergePhase = groupBy.mergePhase();
    assertThat(mergePhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(EvalProjection.class), instanceOf(ColumnIndexWriterProjection.class)));
    ColumnIndexWriterProjection projection = (ColumnIndexWriterProjection) mergePhase.projections().get(2);
    assertThat(projection.primaryKeys().size(), is(1));
    assertThat(projection.primaryKeys().get(0).fqn(), is("id"));
    assertThat(projection.columnReferencesExclPartition().size(), is(2));
    assertThat(projection.columnReferencesExclPartition().get(0).column().fqn(), is("id"));
    assertThat(projection.columnReferencesExclPartition().get(1).column().fqn(), is("name"));
    assertNotNull(projection.clusteredByIdent());
    assertThat(projection.clusteredByIdent().fqn(), is("id"));
    assertThat(projection.tableIdent().fqn(), is("doc.users"));
    assertThat(projection.partitionedBySymbols().isEmpty(), is(true));
    MergePhase localMergeNode = planNode.mergePhase();
    assertThat(localMergeNode.projections().size(), is(1));
    assertThat(localMergeNode.projections().get(0), instanceOf(MergeCountProjection.class));
    assertThat(localMergeNode.finalProjection().get().outputs().size(), is(1));
}
Also used : MergeCountProjection(io.crate.execution.dsl.projection.MergeCountProjection) MergePhase(io.crate.execution.dsl.phases.MergePhase) ColumnIndexWriterProjection(io.crate.execution.dsl.projection.ColumnIndexWriterProjection) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 3 with ColumnIndexWriterProjection

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

the class InsertPlannerTest method testInsertFromSubQueryJoin.

@Test
public void testInsertFromSubQueryJoin() {
    Join join = e.plan("insert into users (id, name) (select u1.id, u2.name from users u1 CROSS JOIN users u2)");
    assertThat(join.joinPhase().projections(), contains(instanceOf(EvalProjection.class), instanceOf(ColumnIndexWriterProjection.class)));
    ColumnIndexWriterProjection projection = (ColumnIndexWriterProjection) join.joinPhase().projections().get(1);
    assertThat(projection.columnReferencesExclPartition().size(), is(2));
    assertThat(projection.columnReferencesExclPartition().get(0).column().fqn(), is("id"));
    assertThat(projection.columnReferencesExclPartition().get(1).column().fqn(), is("name"));
    assertThat(((InputColumn) projection.ids().get(0)).index(), is(0));
    assertThat(((InputColumn) projection.clusteredBy()).index(), is(0));
    assertThat(projection.partitionedBySymbols().isEmpty(), is(true));
}
Also used : Join(io.crate.planner.node.dql.join.Join) ColumnIndexWriterProjection(io.crate.execution.dsl.projection.ColumnIndexWriterProjection) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 4 with ColumnIndexWriterProjection

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

the class InsertPlannerTest method testInsertFromSubQueryGlobalAggregate.

@Test
public void testInsertFromSubQueryGlobalAggregate() {
    Merge globalAggregate = e.plan("insert into users (name, id) (select arbitrary(name), count(*) from users)");
    MergePhase mergePhase = globalAggregate.mergePhase();
    assertThat(mergePhase.projections(), contains(instanceOf(AggregationProjection.class), instanceOf(ColumnIndexWriterProjection.class)));
    assertThat(mergePhase.projections().get(1), instanceOf(ColumnIndexWriterProjection.class));
    ColumnIndexWriterProjection projection = (ColumnIndexWriterProjection) mergePhase.projections().get(1);
    assertThat(projection.columnReferencesExclPartition().size(), is(2));
    assertThat(projection.columnReferencesExclPartition().get(0).column().fqn(), is("name"));
    assertThat(projection.columnReferencesExclPartition().get(1).column().fqn(), is("id"));
    assertThat(projection.columnSymbolsExclPartition().size(), is(2));
    assertThat(((InputColumn) projection.columnSymbolsExclPartition().get(0)).index(), is(0));
    assertThat(((InputColumn) projection.columnSymbolsExclPartition().get(1)).index(), is(1));
    assertNotNull(projection.clusteredByIdent());
    assertThat(projection.clusteredByIdent().fqn(), is("id"));
    assertThat(projection.tableIdent().fqn(), is("doc.users"));
    assertThat(projection.partitionedBySymbols().isEmpty(), is(true));
}
Also used : MergePhase(io.crate.execution.dsl.phases.MergePhase) ColumnIndexWriterProjection(io.crate.execution.dsl.projection.ColumnIndexWriterProjection) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 5 with ColumnIndexWriterProjection

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

the class InsertPlannerTest method testInsertFromSubQueryReduceOnCollectorGroupByWithCast.

@Test
public void testInsertFromSubQueryReduceOnCollectorGroupByWithCast() {
    Merge merge = e.plan("insert into users (id, name) (select id, count(*) from users group by id)");
    Collect nonDistributedGroupBy = (Collect) merge.subPlan();
    RoutedCollectPhase collectPhase = ((RoutedCollectPhase) nonDistributedGroupBy.collectPhase());
    assertThat(collectPhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(EvalProjection.class), instanceOf(ColumnIndexWriterProjection.class)));
    EvalProjection collectTopN = (EvalProjection) collectPhase.projections().get(1);
    assertThat(collectTopN.outputs(), contains(isInputColumn(0), isFunction(ImplicitCastFunction.NAME, List.of(DataTypes.LONG, DataTypes.STRING))));
    ColumnIndexWriterProjection columnIndexWriterProjection = (ColumnIndexWriterProjection) collectPhase.projections().get(2);
    assertThat(columnIndexWriterProjection.columnReferencesExclPartition(), contains(isReference("id"), isReference("name")));
    MergePhase mergePhase = merge.mergePhase();
    assertThat(mergePhase.projections(), contains(instanceOf(MergeCountProjection.class)));
}
Also used : MergePhase(io.crate.execution.dsl.phases.MergePhase) Collect(io.crate.planner.node.dql.Collect) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) ColumnIndexWriterProjection(io.crate.execution.dsl.projection.ColumnIndexWriterProjection) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Aggregations

ColumnIndexWriterProjection (io.crate.execution.dsl.projection.ColumnIndexWriterProjection)8 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)7 Test (org.junit.Test)7 MergePhase (io.crate.execution.dsl.phases.MergePhase)5 Collect (io.crate.planner.node.dql.Collect)3 RoutedCollectPhase (io.crate.execution.dsl.phases.RoutedCollectPhase)2 EvalProjection (io.crate.execution.dsl.projection.EvalProjection)2 MergeCountProjection (io.crate.execution.dsl.projection.MergeCountProjection)2 UnsupportedFeatureException (io.crate.exceptions.UnsupportedFeatureException)1 PKLookupPhase (io.crate.execution.dsl.phases.PKLookupPhase)1 InputColumns (io.crate.execution.dsl.projection.builder.InputColumns)1 InputColumn (io.crate.expression.symbol.InputColumn)1 Symbol (io.crate.expression.symbol.Symbol)1 Reference (io.crate.metadata.Reference)1 Join (io.crate.planner.node.dql.join.Join)1 Insert (io.crate.planner.operators.Insert)1 LogicalPlan (io.crate.planner.operators.LogicalPlan)1 ArrayList (java.util.ArrayList)1