morrySnow commented on code in PR #18869:
URL: https://github.com/apache/doris/pull/18869#discussion_r1188511006
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/NereidsPlanner.java:
##########
@@ -298,6 +298,23 @@ private void optimize() {
new CascadesOptimizer(cascadesContext).execute();
}
+ private void adjustRequiredProperties(Plan plan) {
+ PhysicalProperties properties = null;
+ if (statementContext.getParsedStatement() != null) {
+ Plan parsedStmt = ((LogicalPlanAdapter)
statementContext.getParsedStatement()).getLogicalPlan();
+ if (parsedStmt instanceof InsertIntoTableCommand) {
+ properties = ((InsertIntoTableCommand) parsedStmt)
+ .calculatePhysicalProperties(plan.getOutput());
+ } else if (parsedStmt instanceof ExplainCommand) {
+ properties = PhysicalProperties.ANY;
+ }
Review Comment:
if it is a explain command, u should use the REAL PLAN in explain to
generate required properties
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/Command.java:
##########
@@ -103,6 +103,11 @@ public boolean canBind() {
throw new RuntimeException("Command do not implement canResolve");
}
+ @Override
+ public boolean isCommand() {
+ return true;
+ }
+
Review Comment:
remove this interface
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -0,0 +1,271 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.glue.LogicalPlanAdapter;
+import org.apache.doris.nereids.properties.DistributionSpecHash;
+import org.apache.doris.nereids.properties.DistributionSpecHash.ShuffleType;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.txn.Transaction;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.planner.DataSink;
+import org.apache.doris.planner.OlapTableSink;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * insert into select command implementation
+ *
+ * insert into select command support the grammer: explain? insert into table
columns? partitions? hints? query
+ * InsertIntoTableCommand is a command to represent insert the answer of a
query into a table.
+ * class structure's:
+ * InsertIntoTableCommand(Query())
+ * InsertIntoTableCommand(ExplainCommand(Query()))
+ */
+public class InsertIntoTableCommand extends Command implements ForwardWithSync
{
+ public static final Logger LOG =
LogManager.getLogger(InsertIntoTableCommand.class);
+ private final List<String> tableName;
+ private final List<String> colNames;
+ private final LogicalPlan logicalQuery;
+ private final String labelName;
+ private Database database;
+ private Table table;
+ private NereidsPlanner planner;
+ private TupleDescriptor olapTuple;
+ private List<String> partitions;
+ private List<String> hints;
+ private List<Column> targetColumns;
+ private List<Long> partitionIds = null;
+
+ /**
+ * constructor
+ */
+ public InsertIntoTableCommand(List<String> tableName, String labelName,
List<String> colNames,
+ List<String> partitions, List<String> hints, LogicalPlan
logicalQuery) {
+ super(PlanType.INSERT_INTO_SELECT_COMMAND);
+ Preconditions.checkArgument(tableName != null, "tableName cannot be
null in insert-into-select command");
+ Preconditions.checkArgument(logicalQuery != null, "logicalQuery cannot
be null in insert-into-select command");
+ this.tableName = tableName;
+ this.labelName = labelName;
+ this.colNames = colNames;
+ this.partitions = partitions;
+ this.hints = hints;
+ this.logicalQuery = logicalQuery;
+ }
+
+ public NereidsPlanner getPlanner() {
+ return planner;
+ }
+
+ @Override
+ public void run(ConnectContext ctx, StmtExecutor executor) throws
Exception {
+ if (ctx.isTxnModel()) {
+ // in original planner and in txn model, we can execute sql like:
insert into t select 1, 2, 3
Review Comment:
```suggestion
// in original planner with txn model, we can execute sql like:
insert into t select 1, 2, 3
```
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -0,0 +1,271 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.glue.LogicalPlanAdapter;
+import org.apache.doris.nereids.properties.DistributionSpecHash;
+import org.apache.doris.nereids.properties.DistributionSpecHash.ShuffleType;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.txn.Transaction;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.planner.DataSink;
+import org.apache.doris.planner.OlapTableSink;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * insert into select command implementation
+ *
+ * insert into select command support the grammer: explain? insert into table
columns? partitions? hints? query
+ * InsertIntoTableCommand is a command to represent insert the answer of a
query into a table.
+ * class structure's:
+ * InsertIntoTableCommand(Query())
+ * InsertIntoTableCommand(ExplainCommand(Query()))
+ */
+public class InsertIntoTableCommand extends Command implements ForwardWithSync
{
+ public static final Logger LOG =
LogManager.getLogger(InsertIntoTableCommand.class);
+ private final List<String> tableName;
+ private final List<String> colNames;
+ private final LogicalPlan logicalQuery;
+ private final String labelName;
+ private Database database;
+ private Table table;
+ private NereidsPlanner planner;
+ private TupleDescriptor olapTuple;
+ private List<String> partitions;
+ private List<String> hints;
+ private List<Column> targetColumns;
+ private List<Long> partitionIds = null;
+
+ /**
+ * constructor
+ */
+ public InsertIntoTableCommand(List<String> tableName, String labelName,
List<String> colNames,
+ List<String> partitions, List<String> hints, LogicalPlan
logicalQuery) {
+ super(PlanType.INSERT_INTO_SELECT_COMMAND);
+ Preconditions.checkArgument(tableName != null, "tableName cannot be
null in insert-into-select command");
+ Preconditions.checkArgument(logicalQuery != null, "logicalQuery cannot
be null in insert-into-select command");
+ this.tableName = tableName;
+ this.labelName = labelName;
+ this.colNames = colNames;
+ this.partitions = partitions;
+ this.hints = hints;
+ this.logicalQuery = logicalQuery;
+ }
+
+ public NereidsPlanner getPlanner() {
+ return planner;
+ }
+
+ @Override
+ public void run(ConnectContext ctx, StmtExecutor executor) throws
Exception {
+ if (ctx.isTxnModel()) {
+ // in original planner and in txn model, we can execute sql like:
insert into t select 1, 2, 3
+ // but no data will be inserted, now we adjust forbid it.
+ throw new AnalysisException("insert into table command is not
supported in txn model");
+ }
+ checkDatabaseAndTable(ctx);
+ getColumns();
+ getPartition();
+
+
ctx.getStatementContext().getInsertIntoContext().setTargetSchema(targetColumns);
+
+ LogicalPlanAdapter logicalPlanAdapter = new
LogicalPlanAdapter(extractPlan(logicalQuery),
+ ctx.getStatementContext());
+ planner = new NereidsPlanner(ctx.getStatementContext());
+ planner.plan(logicalPlanAdapter, ctx.getSessionVariable().toThrift());
+
+ getTupleDesc();
+ addUnassignedColumns();
+
+ if (ctx.getMysqlChannel() != null) {
+ ctx.getMysqlChannel().reset();
+ }
+ String label = this.labelName;
+ if (label == null) {
+ label = String.format("label_%x_%x", ctx.queryId().hi,
ctx.queryId().lo);
+ }
+
+ Transaction txn;
+ PlanFragment root = planner.getFragments().get(0);
+ DataSink sink = createDataSink(ctx, root);
+ Preconditions.checkArgument(sink instanceof OlapTableSink, "olap table
sink is expected when"
+ + " running insert into select");
+ txn = new Transaction(ctx, database, table, label, planner);
+
+ OlapTableSink olapTableSink = ((OlapTableSink) sink);
+ olapTableSink.init(ctx.queryId(), txn.getTxnId(), database.getId(),
ctx.getExecTimeout(),
+ ctx.getSessionVariable().getSendBatchParallelism(), false);
+ olapTableSink.complete();
+ root.resetSink(olapTableSink);
+
+ if (isExplain()) {
+ executor.handleExplainStmt(((ExplainCommand)
logicalQuery).getExplainString(planner));
+ return;
+ }
+
+ txn.executeInsertIntoSelectCommand(executor);
+ }
+
+ private void checkDatabaseAndTable(ConnectContext ctx) {
+ List<String> qualifier = RelationUtil.getQualifierName(ctx, tableName);
+ String catalogName = qualifier.get(0);
+ String dbName = qualifier.get(1);
+ String tableName = qualifier.get(2);
+ CatalogIf catalog =
Env.getCurrentEnv().getCatalogMgr().getCatalog(catalogName);
+ if (catalog == null) {
+ throw new RuntimeException(String.format("Catalog %s does not
exist.", catalogName));
+ }
+ try {
+ database = ((Database) catalog.getDb(dbName).orElseThrow(() ->
+ new RuntimeException("Database [" + dbName + "] does not
exist.")));
+ table = database.getTable(tableName).orElseThrow(() ->
+ new RuntimeException("Table [" + tableName + "] does not
exist in database [" + dbName + "]."));
Review Comment:
```suggestion
new AnalysisException("Table [" + tableName + "] does
not exist in database [" + dbName + "]."));
```
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -0,0 +1,271 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.glue.LogicalPlanAdapter;
+import org.apache.doris.nereids.properties.DistributionSpecHash;
+import org.apache.doris.nereids.properties.DistributionSpecHash.ShuffleType;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.txn.Transaction;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.planner.DataSink;
+import org.apache.doris.planner.OlapTableSink;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * insert into select command implementation
+ *
+ * insert into select command support the grammer: explain? insert into table
columns? partitions? hints? query
+ * InsertIntoTableCommand is a command to represent insert the answer of a
query into a table.
+ * class structure's:
+ * InsertIntoTableCommand(Query())
+ * InsertIntoTableCommand(ExplainCommand(Query()))
+ */
+public class InsertIntoTableCommand extends Command implements ForwardWithSync
{
+ public static final Logger LOG =
LogManager.getLogger(InsertIntoTableCommand.class);
+ private final List<String> tableName;
+ private final List<String> colNames;
+ private final LogicalPlan logicalQuery;
+ private final String labelName;
+ private Database database;
+ private Table table;
+ private NereidsPlanner planner;
+ private TupleDescriptor olapTuple;
+ private List<String> partitions;
+ private List<String> hints;
+ private List<Column> targetColumns;
+ private List<Long> partitionIds = null;
+
+ /**
+ * constructor
+ */
+ public InsertIntoTableCommand(List<String> tableName, String labelName,
List<String> colNames,
+ List<String> partitions, List<String> hints, LogicalPlan
logicalQuery) {
+ super(PlanType.INSERT_INTO_SELECT_COMMAND);
+ Preconditions.checkArgument(tableName != null, "tableName cannot be
null in insert-into-select command");
+ Preconditions.checkArgument(logicalQuery != null, "logicalQuery cannot
be null in insert-into-select command");
+ this.tableName = tableName;
+ this.labelName = labelName;
+ this.colNames = colNames;
+ this.partitions = partitions;
+ this.hints = hints;
+ this.logicalQuery = logicalQuery;
+ }
+
+ public NereidsPlanner getPlanner() {
+ return planner;
+ }
+
+ @Override
+ public void run(ConnectContext ctx, StmtExecutor executor) throws
Exception {
+ if (ctx.isTxnModel()) {
+ // in original planner and in txn model, we can execute sql like:
insert into t select 1, 2, 3
+ // but no data will be inserted, now we adjust forbid it.
+ throw new AnalysisException("insert into table command is not
supported in txn model");
+ }
+ checkDatabaseAndTable(ctx);
+ getColumns();
+ getPartition();
+
+
ctx.getStatementContext().getInsertIntoContext().setTargetSchema(targetColumns);
+
+ LogicalPlanAdapter logicalPlanAdapter = new
LogicalPlanAdapter(extractPlan(logicalQuery),
+ ctx.getStatementContext());
+ planner = new NereidsPlanner(ctx.getStatementContext());
+ planner.plan(logicalPlanAdapter, ctx.getSessionVariable().toThrift());
+
+ getTupleDesc();
+ addUnassignedColumns();
+
+ if (ctx.getMysqlChannel() != null) {
+ ctx.getMysqlChannel().reset();
+ }
+ String label = this.labelName;
+ if (label == null) {
+ label = String.format("label_%x_%x", ctx.queryId().hi,
ctx.queryId().lo);
+ }
+
+ Transaction txn;
+ PlanFragment root = planner.getFragments().get(0);
+ DataSink sink = createDataSink(ctx, root);
+ Preconditions.checkArgument(sink instanceof OlapTableSink, "olap table
sink is expected when"
+ + " running insert into select");
+ txn = new Transaction(ctx, database, table, label, planner);
+
+ OlapTableSink olapTableSink = ((OlapTableSink) sink);
+ olapTableSink.init(ctx.queryId(), txn.getTxnId(), database.getId(),
ctx.getExecTimeout(),
+ ctx.getSessionVariable().getSendBatchParallelism(), false);
+ olapTableSink.complete();
+ root.resetSink(olapTableSink);
+
+ if (isExplain()) {
+ executor.handleExplainStmt(((ExplainCommand)
logicalQuery).getExplainString(planner));
+ return;
+ }
Review Comment:
should handle all explain in ExplainCommand
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/NereidsPlanner.java:
##########
@@ -209,6 +207,8 @@ public Plan plan(LogicalPlan plan, PhysicalProperties
requireProperties, Explain
deriveStats();
+ adjustRequiredProperties(cascadesContext.getRewritePlan());
Review Comment:
why not do it at the beginning of `plan`
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -0,0 +1,271 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.glue.LogicalPlanAdapter;
+import org.apache.doris.nereids.properties.DistributionSpecHash;
+import org.apache.doris.nereids.properties.DistributionSpecHash.ShuffleType;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.txn.Transaction;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.planner.DataSink;
+import org.apache.doris.planner.OlapTableSink;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * insert into select command implementation
+ *
+ * insert into select command support the grammer: explain? insert into table
columns? partitions? hints? query
+ * InsertIntoTableCommand is a command to represent insert the answer of a
query into a table.
+ * class structure's:
+ * InsertIntoTableCommand(Query())
+ * InsertIntoTableCommand(ExplainCommand(Query()))
+ */
+public class InsertIntoTableCommand extends Command implements ForwardWithSync
{
+ public static final Logger LOG =
LogManager.getLogger(InsertIntoTableCommand.class);
+ private final List<String> tableName;
+ private final List<String> colNames;
+ private final LogicalPlan logicalQuery;
+ private final String labelName;
+ private Database database;
+ private Table table;
+ private NereidsPlanner planner;
+ private TupleDescriptor olapTuple;
+ private List<String> partitions;
+ private List<String> hints;
+ private List<Column> targetColumns;
+ private List<Long> partitionIds = null;
+
+ /**
+ * constructor
+ */
+ public InsertIntoTableCommand(List<String> tableName, String labelName,
List<String> colNames,
+ List<String> partitions, List<String> hints, LogicalPlan
logicalQuery) {
+ super(PlanType.INSERT_INTO_SELECT_COMMAND);
+ Preconditions.checkArgument(tableName != null, "tableName cannot be
null in insert-into-select command");
+ Preconditions.checkArgument(logicalQuery != null, "logicalQuery cannot
be null in insert-into-select command");
+ this.tableName = tableName;
+ this.labelName = labelName;
+ this.colNames = colNames;
+ this.partitions = partitions;
+ this.hints = hints;
+ this.logicalQuery = logicalQuery;
+ }
+
+ public NereidsPlanner getPlanner() {
+ return planner;
+ }
+
+ @Override
+ public void run(ConnectContext ctx, StmtExecutor executor) throws
Exception {
+ if (ctx.isTxnModel()) {
+ // in original planner and in txn model, we can execute sql like:
insert into t select 1, 2, 3
+ // but no data will be inserted, now we adjust forbid it.
Review Comment:
why no data insert? find the reason and add more info in this comment
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -0,0 +1,271 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.glue.LogicalPlanAdapter;
+import org.apache.doris.nereids.properties.DistributionSpecHash;
+import org.apache.doris.nereids.properties.DistributionSpecHash.ShuffleType;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.txn.Transaction;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.planner.DataSink;
+import org.apache.doris.planner.OlapTableSink;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * insert into select command implementation
+ *
+ * insert into select command support the grammer: explain? insert into table
columns? partitions? hints? query
+ * InsertIntoTableCommand is a command to represent insert the answer of a
query into a table.
+ * class structure's:
+ * InsertIntoTableCommand(Query())
+ * InsertIntoTableCommand(ExplainCommand(Query()))
+ */
+public class InsertIntoTableCommand extends Command implements ForwardWithSync
{
+ public static final Logger LOG =
LogManager.getLogger(InsertIntoTableCommand.class);
+ private final List<String> tableName;
+ private final List<String> colNames;
+ private final LogicalPlan logicalQuery;
+ private final String labelName;
+ private Database database;
+ private Table table;
+ private NereidsPlanner planner;
+ private TupleDescriptor olapTuple;
+ private List<String> partitions;
+ private List<String> hints;
+ private List<Column> targetColumns;
+ private List<Long> partitionIds = null;
+
+ /**
+ * constructor
+ */
+ public InsertIntoTableCommand(List<String> tableName, String labelName,
List<String> colNames,
+ List<String> partitions, List<String> hints, LogicalPlan
logicalQuery) {
+ super(PlanType.INSERT_INTO_SELECT_COMMAND);
+ Preconditions.checkArgument(tableName != null, "tableName cannot be
null in insert-into-select command");
Review Comment:
```suggestion
Preconditions.checkArgument(tableName != null, "tableName cannot be
null in InsertIntoTableCommand");
```
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -0,0 +1,271 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.glue.LogicalPlanAdapter;
+import org.apache.doris.nereids.properties.DistributionSpecHash;
+import org.apache.doris.nereids.properties.DistributionSpecHash.ShuffleType;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.txn.Transaction;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.planner.DataSink;
+import org.apache.doris.planner.OlapTableSink;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * insert into select command implementation
+ *
+ * insert into select command support the grammer: explain? insert into table
columns? partitions? hints? query
+ * InsertIntoTableCommand is a command to represent insert the answer of a
query into a table.
+ * class structure's:
+ * InsertIntoTableCommand(Query())
+ * InsertIntoTableCommand(ExplainCommand(Query()))
+ */
+public class InsertIntoTableCommand extends Command implements ForwardWithSync
{
+ public static final Logger LOG =
LogManager.getLogger(InsertIntoTableCommand.class);
Review Comment:
add a blank line before and after this line
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -0,0 +1,271 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.glue.LogicalPlanAdapter;
+import org.apache.doris.nereids.properties.DistributionSpecHash;
+import org.apache.doris.nereids.properties.DistributionSpecHash.ShuffleType;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.txn.Transaction;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.planner.DataSink;
+import org.apache.doris.planner.OlapTableSink;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * insert into select command implementation
+ *
+ * insert into select command support the grammer: explain? insert into table
columns? partitions? hints? query
+ * InsertIntoTableCommand is a command to represent insert the answer of a
query into a table.
+ * class structure's:
+ * InsertIntoTableCommand(Query())
+ * InsertIntoTableCommand(ExplainCommand(Query()))
Review Comment:
ExplainCommand(InsertIntoTableCommand(Query())) is more make sense
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/txn/InsertIntoContext.java:
##########
@@ -0,0 +1,37 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.txn;
+
+import org.apache.doris.catalog.Column;
+
+import java.util.List;
+
+/**
+ * context for insert into command
+ */
+public class InsertIntoContext {
Review Comment:
maybe the better way to do that is add a new type node LogicalOlapTableSink.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -0,0 +1,271 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.glue.LogicalPlanAdapter;
+import org.apache.doris.nereids.properties.DistributionSpecHash;
+import org.apache.doris.nereids.properties.DistributionSpecHash.ShuffleType;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.txn.Transaction;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.planner.DataSink;
+import org.apache.doris.planner.OlapTableSink;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * insert into select command implementation
+ *
+ * insert into select command support the grammer: explain? insert into table
columns? partitions? hints? query
+ * InsertIntoTableCommand is a command to represent insert the answer of a
query into a table.
+ * class structure's:
+ * InsertIntoTableCommand(Query())
+ * InsertIntoTableCommand(ExplainCommand(Query()))
+ */
+public class InsertIntoTableCommand extends Command implements ForwardWithSync
{
+ public static final Logger LOG =
LogManager.getLogger(InsertIntoTableCommand.class);
+ private final List<String> tableName;
+ private final List<String> colNames;
+ private final LogicalPlan logicalQuery;
+ private final String labelName;
+ private Database database;
+ private Table table;
+ private NereidsPlanner planner;
+ private TupleDescriptor olapTuple;
+ private List<String> partitions;
+ private List<String> hints;
+ private List<Column> targetColumns;
+ private List<Long> partitionIds = null;
+
+ /**
+ * constructor
+ */
+ public InsertIntoTableCommand(List<String> tableName, String labelName,
List<String> colNames,
+ List<String> partitions, List<String> hints, LogicalPlan
logicalQuery) {
+ super(PlanType.INSERT_INTO_SELECT_COMMAND);
+ Preconditions.checkArgument(tableName != null, "tableName cannot be
null in insert-into-select command");
+ Preconditions.checkArgument(logicalQuery != null, "logicalQuery cannot
be null in insert-into-select command");
+ this.tableName = tableName;
+ this.labelName = labelName;
+ this.colNames = colNames;
+ this.partitions = partitions;
+ this.hints = hints;
+ this.logicalQuery = logicalQuery;
+ }
+
+ public NereidsPlanner getPlanner() {
+ return planner;
+ }
+
+ @Override
+ public void run(ConnectContext ctx, StmtExecutor executor) throws
Exception {
+ if (ctx.isTxnModel()) {
+ // in original planner and in txn model, we can execute sql like:
insert into t select 1, 2, 3
+ // but no data will be inserted, now we adjust forbid it.
+ throw new AnalysisException("insert into table command is not
supported in txn model");
+ }
+ checkDatabaseAndTable(ctx);
+ getColumns();
+ getPartition();
+
+
ctx.getStatementContext().getInsertIntoContext().setTargetSchema(targetColumns);
+
+ LogicalPlanAdapter logicalPlanAdapter = new
LogicalPlanAdapter(extractPlan(logicalQuery),
+ ctx.getStatementContext());
+ planner = new NereidsPlanner(ctx.getStatementContext());
+ planner.plan(logicalPlanAdapter, ctx.getSessionVariable().toThrift());
+
+ getTupleDesc();
+ addUnassignedColumns();
+
+ if (ctx.getMysqlChannel() != null) {
+ ctx.getMysqlChannel().reset();
+ }
+ String label = this.labelName;
+ if (label == null) {
+ label = String.format("label_%x_%x", ctx.queryId().hi,
ctx.queryId().lo);
+ }
+
+ Transaction txn;
+ PlanFragment root = planner.getFragments().get(0);
+ DataSink sink = createDataSink(ctx, root);
+ Preconditions.checkArgument(sink instanceof OlapTableSink, "olap table
sink is expected when"
+ + " running insert into select");
+ txn = new Transaction(ctx, database, table, label, planner);
+
+ OlapTableSink olapTableSink = ((OlapTableSink) sink);
+ olapTableSink.init(ctx.queryId(), txn.getTxnId(), database.getId(),
ctx.getExecTimeout(),
+ ctx.getSessionVariable().getSendBatchParallelism(), false);
+ olapTableSink.complete();
+ root.resetSink(olapTableSink);
+
+ if (isExplain()) {
+ executor.handleExplainStmt(((ExplainCommand)
logicalQuery).getExplainString(planner));
+ return;
+ }
+
+ txn.executeInsertIntoSelectCommand(executor);
+ }
+
+ private void checkDatabaseAndTable(ConnectContext ctx) {
+ List<String> qualifier = RelationUtil.getQualifierName(ctx, tableName);
+ String catalogName = qualifier.get(0);
+ String dbName = qualifier.get(1);
+ String tableName = qualifier.get(2);
+ CatalogIf catalog =
Env.getCurrentEnv().getCatalogMgr().getCatalog(catalogName);
+ if (catalog == null) {
+ throw new RuntimeException(String.format("Catalog %s does not
exist.", catalogName));
Review Comment:
```suggestion
throw new AnalysisException(String.format("Catalog %s does not
exist.", catalogName));
```
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -0,0 +1,271 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.glue.LogicalPlanAdapter;
+import org.apache.doris.nereids.properties.DistributionSpecHash;
+import org.apache.doris.nereids.properties.DistributionSpecHash.ShuffleType;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.txn.Transaction;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.planner.DataSink;
+import org.apache.doris.planner.OlapTableSink;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * insert into select command implementation
+ *
+ * insert into select command support the grammer: explain? insert into table
columns? partitions? hints? query
+ * InsertIntoTableCommand is a command to represent insert the answer of a
query into a table.
+ * class structure's:
+ * InsertIntoTableCommand(Query())
+ * InsertIntoTableCommand(ExplainCommand(Query()))
+ */
+public class InsertIntoTableCommand extends Command implements ForwardWithSync
{
+ public static final Logger LOG =
LogManager.getLogger(InsertIntoTableCommand.class);
+ private final List<String> tableName;
+ private final List<String> colNames;
+ private final LogicalPlan logicalQuery;
+ private final String labelName;
+ private Database database;
+ private Table table;
+ private NereidsPlanner planner;
+ private TupleDescriptor olapTuple;
+ private List<String> partitions;
+ private List<String> hints;
+ private List<Column> targetColumns;
+ private List<Long> partitionIds = null;
Review Comment:
why only this attribute init as `null`?
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -0,0 +1,271 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.glue.LogicalPlanAdapter;
+import org.apache.doris.nereids.properties.DistributionSpecHash;
+import org.apache.doris.nereids.properties.DistributionSpecHash.ShuffleType;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.txn.Transaction;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.planner.DataSink;
+import org.apache.doris.planner.OlapTableSink;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * insert into select command implementation
+ *
+ * insert into select command support the grammer: explain? insert into table
columns? partitions? hints? query
+ * InsertIntoTableCommand is a command to represent insert the answer of a
query into a table.
+ * class structure's:
+ * InsertIntoTableCommand(Query())
+ * InsertIntoTableCommand(ExplainCommand(Query()))
+ */
+public class InsertIntoTableCommand extends Command implements ForwardWithSync
{
+ public static final Logger LOG =
LogManager.getLogger(InsertIntoTableCommand.class);
+ private final List<String> tableName;
+ private final List<String> colNames;
+ private final LogicalPlan logicalQuery;
+ private final String labelName;
+ private Database database;
+ private Table table;
+ private NereidsPlanner planner;
+ private TupleDescriptor olapTuple;
+ private List<String> partitions;
+ private List<String> hints;
+ private List<Column> targetColumns;
+ private List<Long> partitionIds = null;
+
+ /**
+ * constructor
+ */
+ public InsertIntoTableCommand(List<String> tableName, String labelName,
List<String> colNames,
Review Comment:
```suggestion
public InsertIntoTableCommand(List<String> nameParts, String labelName,
List<String> colNames,
```
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -0,0 +1,271 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.glue.LogicalPlanAdapter;
+import org.apache.doris.nereids.properties.DistributionSpecHash;
+import org.apache.doris.nereids.properties.DistributionSpecHash.ShuffleType;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.txn.Transaction;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.planner.DataSink;
+import org.apache.doris.planner.OlapTableSink;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * insert into select command implementation
+ *
+ * insert into select command support the grammer: explain? insert into table
columns? partitions? hints? query
+ * InsertIntoTableCommand is a command to represent insert the answer of a
query into a table.
+ * class structure's:
+ * InsertIntoTableCommand(Query())
+ * InsertIntoTableCommand(ExplainCommand(Query()))
+ */
+public class InsertIntoTableCommand extends Command implements ForwardWithSync
{
+ public static final Logger LOG =
LogManager.getLogger(InsertIntoTableCommand.class);
+ private final List<String> tableName;
+ private final List<String> colNames;
+ private final LogicalPlan logicalQuery;
+ private final String labelName;
+ private Database database;
+ private Table table;
+ private NereidsPlanner planner;
+ private TupleDescriptor olapTuple;
+ private List<String> partitions;
+ private List<String> hints;
Review Comment:
not use?
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java:
##########
@@ -89,5 +89,6 @@ public enum PlanType {
COMMAND,
EXPLAIN_COMMAND,
- CREATE_POLICY_COMMAND
+ CREATE_POLICY_COMMAND,
+ INSERT_INTO_SELECT_COMMAND
Review Comment:
```suggestion
INSERT_INTO_TABLE_COMMAND
```
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -0,0 +1,271 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.glue.LogicalPlanAdapter;
+import org.apache.doris.nereids.properties.DistributionSpecHash;
+import org.apache.doris.nereids.properties.DistributionSpecHash.ShuffleType;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.txn.Transaction;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.planner.DataSink;
+import org.apache.doris.planner.OlapTableSink;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * insert into select command implementation
+ *
+ * insert into select command support the grammer: explain? insert into table
columns? partitions? hints? query
+ * InsertIntoTableCommand is a command to represent insert the answer of a
query into a table.
+ * class structure's:
+ * InsertIntoTableCommand(Query())
+ * InsertIntoTableCommand(ExplainCommand(Query()))
+ */
+public class InsertIntoTableCommand extends Command implements ForwardWithSync
{
+ public static final Logger LOG =
LogManager.getLogger(InsertIntoTableCommand.class);
+ private final List<String> tableName;
+ private final List<String> colNames;
+ private final LogicalPlan logicalQuery;
+ private final String labelName;
+ private Database database;
+ private Table table;
+ private NereidsPlanner planner;
+ private TupleDescriptor olapTuple;
+ private List<String> partitions;
+ private List<String> hints;
+ private List<Column> targetColumns;
+ private List<Long> partitionIds = null;
+
+ /**
+ * constructor
+ */
+ public InsertIntoTableCommand(List<String> tableName, String labelName,
List<String> colNames,
+ List<String> partitions, List<String> hints, LogicalPlan
logicalQuery) {
+ super(PlanType.INSERT_INTO_SELECT_COMMAND);
+ Preconditions.checkArgument(tableName != null, "tableName cannot be
null in insert-into-select command");
+ Preconditions.checkArgument(logicalQuery != null, "logicalQuery cannot
be null in insert-into-select command");
+ this.tableName = tableName;
+ this.labelName = labelName;
+ this.colNames = colNames;
+ this.partitions = partitions;
+ this.hints = hints;
+ this.logicalQuery = logicalQuery;
+ }
+
+ public NereidsPlanner getPlanner() {
+ return planner;
+ }
+
+ @Override
+ public void run(ConnectContext ctx, StmtExecutor executor) throws
Exception {
+ if (ctx.isTxnModel()) {
+ // in original planner and in txn model, we can execute sql like:
insert into t select 1, 2, 3
+ // but no data will be inserted, now we adjust forbid it.
+ throw new AnalysisException("insert into table command is not
supported in txn model");
+ }
+ checkDatabaseAndTable(ctx);
+ getColumns();
+ getPartition();
+
+
ctx.getStatementContext().getInsertIntoContext().setTargetSchema(targetColumns);
+
+ LogicalPlanAdapter logicalPlanAdapter = new
LogicalPlanAdapter(extractPlan(logicalQuery),
+ ctx.getStatementContext());
+ planner = new NereidsPlanner(ctx.getStatementContext());
+ planner.plan(logicalPlanAdapter, ctx.getSessionVariable().toThrift());
+
+ getTupleDesc();
+ addUnassignedColumns();
+
+ if (ctx.getMysqlChannel() != null) {
+ ctx.getMysqlChannel().reset();
+ }
+ String label = this.labelName;
+ if (label == null) {
+ label = String.format("label_%x_%x", ctx.queryId().hi,
ctx.queryId().lo);
+ }
+
+ Transaction txn;
+ PlanFragment root = planner.getFragments().get(0);
+ DataSink sink = createDataSink(ctx, root);
+ Preconditions.checkArgument(sink instanceof OlapTableSink, "olap table
sink is expected when"
+ + " running insert into select");
+ txn = new Transaction(ctx, database, table, label, planner);
+
+ OlapTableSink olapTableSink = ((OlapTableSink) sink);
+ olapTableSink.init(ctx.queryId(), txn.getTxnId(), database.getId(),
ctx.getExecTimeout(),
+ ctx.getSessionVariable().getSendBatchParallelism(), false);
+ olapTableSink.complete();
+ root.resetSink(olapTableSink);
+
+ if (isExplain()) {
+ executor.handleExplainStmt(((ExplainCommand)
logicalQuery).getExplainString(planner));
+ return;
+ }
+
+ txn.executeInsertIntoSelectCommand(executor);
+ }
+
+ private void checkDatabaseAndTable(ConnectContext ctx) {
+ List<String> qualifier = RelationUtil.getQualifierName(ctx, tableName);
Review Comment:
```suggestion
List<String> qualifiedTableName = RelationUtil.getQualifierName(ctx,
tableName);
```
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -0,0 +1,271 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.glue.LogicalPlanAdapter;
+import org.apache.doris.nereids.properties.DistributionSpecHash;
+import org.apache.doris.nereids.properties.DistributionSpecHash.ShuffleType;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.txn.Transaction;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.planner.DataSink;
+import org.apache.doris.planner.OlapTableSink;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * insert into select command implementation
+ *
+ * insert into select command support the grammer: explain? insert into table
columns? partitions? hints? query
+ * InsertIntoTableCommand is a command to represent insert the answer of a
query into a table.
+ * class structure's:
+ * InsertIntoTableCommand(Query())
+ * InsertIntoTableCommand(ExplainCommand(Query()))
+ */
+public class InsertIntoTableCommand extends Command implements ForwardWithSync
{
+ public static final Logger LOG =
LogManager.getLogger(InsertIntoTableCommand.class);
+ private final List<String> tableName;
+ private final List<String> colNames;
+ private final LogicalPlan logicalQuery;
+ private final String labelName;
+ private Database database;
+ private Table table;
+ private NereidsPlanner planner;
+ private TupleDescriptor olapTuple;
+ private List<String> partitions;
+ private List<String> hints;
+ private List<Column> targetColumns;
+ private List<Long> partitionIds = null;
+
+ /**
+ * constructor
+ */
+ public InsertIntoTableCommand(List<String> tableName, String labelName,
List<String> colNames,
+ List<String> partitions, List<String> hints, LogicalPlan
logicalQuery) {
+ super(PlanType.INSERT_INTO_SELECT_COMMAND);
+ Preconditions.checkArgument(tableName != null, "tableName cannot be
null in insert-into-select command");
+ Preconditions.checkArgument(logicalQuery != null, "logicalQuery cannot
be null in insert-into-select command");
+ this.tableName = tableName;
+ this.labelName = labelName;
+ this.colNames = colNames;
+ this.partitions = partitions;
+ this.hints = hints;
+ this.logicalQuery = logicalQuery;
+ }
+
+ public NereidsPlanner getPlanner() {
+ return planner;
+ }
+
+ @Override
+ public void run(ConnectContext ctx, StmtExecutor executor) throws
Exception {
+ if (ctx.isTxnModel()) {
+ // in original planner and in txn model, we can execute sql like:
insert into t select 1, 2, 3
+ // but no data will be inserted, now we adjust forbid it.
+ throw new AnalysisException("insert into table command is not
supported in txn model");
+ }
+ checkDatabaseAndTable(ctx);
+ getColumns();
+ getPartition();
+
+
ctx.getStatementContext().getInsertIntoContext().setTargetSchema(targetColumns);
+
+ LogicalPlanAdapter logicalPlanAdapter = new
LogicalPlanAdapter(extractPlan(logicalQuery),
+ ctx.getStatementContext());
+ planner = new NereidsPlanner(ctx.getStatementContext());
+ planner.plan(logicalPlanAdapter, ctx.getSessionVariable().toThrift());
+
+ getTupleDesc();
+ addUnassignedColumns();
+
+ if (ctx.getMysqlChannel() != null) {
+ ctx.getMysqlChannel().reset();
+ }
+ String label = this.labelName;
+ if (label == null) {
+ label = String.format("label_%x_%x", ctx.queryId().hi,
ctx.queryId().lo);
+ }
+
+ Transaction txn;
+ PlanFragment root = planner.getFragments().get(0);
+ DataSink sink = createDataSink(ctx, root);
+ Preconditions.checkArgument(sink instanceof OlapTableSink, "olap table
sink is expected when"
+ + " running insert into select");
+ txn = new Transaction(ctx, database, table, label, planner);
+
+ OlapTableSink olapTableSink = ((OlapTableSink) sink);
+ olapTableSink.init(ctx.queryId(), txn.getTxnId(), database.getId(),
ctx.getExecTimeout(),
+ ctx.getSessionVariable().getSendBatchParallelism(), false);
+ olapTableSink.complete();
+ root.resetSink(olapTableSink);
+
+ if (isExplain()) {
+ executor.handleExplainStmt(((ExplainCommand)
logicalQuery).getExplainString(planner));
+ return;
+ }
+
+ txn.executeInsertIntoSelectCommand(executor);
+ }
+
+ private void checkDatabaseAndTable(ConnectContext ctx) {
+ List<String> qualifier = RelationUtil.getQualifierName(ctx, tableName);
+ String catalogName = qualifier.get(0);
+ String dbName = qualifier.get(1);
+ String tableName = qualifier.get(2);
+ CatalogIf catalog =
Env.getCurrentEnv().getCatalogMgr().getCatalog(catalogName);
+ if (catalog == null) {
+ throw new RuntimeException(String.format("Catalog %s does not
exist.", catalogName));
+ }
+ try {
+ database = ((Database) catalog.getDb(dbName).orElseThrow(() ->
+ new RuntimeException("Database [" + dbName + "] does not
exist.")));
Review Comment:
```suggestion
new AnalysisException("Database [" + dbName + "] does
not exist.")));
```
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -0,0 +1,271 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.glue.LogicalPlanAdapter;
+import org.apache.doris.nereids.properties.DistributionSpecHash;
+import org.apache.doris.nereids.properties.DistributionSpecHash.ShuffleType;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.txn.Transaction;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.planner.DataSink;
+import org.apache.doris.planner.OlapTableSink;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * insert into select command implementation
+ *
+ * insert into select command support the grammer: explain? insert into table
columns? partitions? hints? query
+ * InsertIntoTableCommand is a command to represent insert the answer of a
query into a table.
+ * class structure's:
+ * InsertIntoTableCommand(Query())
+ * InsertIntoTableCommand(ExplainCommand(Query()))
+ */
+public class InsertIntoTableCommand extends Command implements ForwardWithSync
{
+ public static final Logger LOG =
LogManager.getLogger(InsertIntoTableCommand.class);
+ private final List<String> tableName;
+ private final List<String> colNames;
+ private final LogicalPlan logicalQuery;
+ private final String labelName;
+ private Database database;
+ private Table table;
+ private NereidsPlanner planner;
+ private TupleDescriptor olapTuple;
+ private List<String> partitions;
+ private List<String> hints;
+ private List<Column> targetColumns;
+ private List<Long> partitionIds = null;
+
+ /**
+ * constructor
+ */
+ public InsertIntoTableCommand(List<String> tableName, String labelName,
List<String> colNames,
+ List<String> partitions, List<String> hints, LogicalPlan
logicalQuery) {
+ super(PlanType.INSERT_INTO_SELECT_COMMAND);
+ Preconditions.checkArgument(tableName != null, "tableName cannot be
null in insert-into-select command");
+ Preconditions.checkArgument(logicalQuery != null, "logicalQuery cannot
be null in insert-into-select command");
+ this.tableName = tableName;
+ this.labelName = labelName;
+ this.colNames = colNames;
+ this.partitions = partitions;
+ this.hints = hints;
+ this.logicalQuery = logicalQuery;
+ }
+
+ public NereidsPlanner getPlanner() {
+ return planner;
+ }
+
+ @Override
+ public void run(ConnectContext ctx, StmtExecutor executor) throws
Exception {
+ if (ctx.isTxnModel()) {
+ // in original planner and in txn model, we can execute sql like:
insert into t select 1, 2, 3
+ // but no data will be inserted, now we adjust forbid it.
+ throw new AnalysisException("insert into table command is not
supported in txn model");
+ }
+ checkDatabaseAndTable(ctx);
+ getColumns();
+ getPartition();
+
+
ctx.getStatementContext().getInsertIntoContext().setTargetSchema(targetColumns);
+
+ LogicalPlanAdapter logicalPlanAdapter = new
LogicalPlanAdapter(extractPlan(logicalQuery),
+ ctx.getStatementContext());
+ planner = new NereidsPlanner(ctx.getStatementContext());
+ planner.plan(logicalPlanAdapter, ctx.getSessionVariable().toThrift());
+
+ getTupleDesc();
+ addUnassignedColumns();
+
+ if (ctx.getMysqlChannel() != null) {
+ ctx.getMysqlChannel().reset();
+ }
+ String label = this.labelName;
+ if (label == null) {
+ label = String.format("label_%x_%x", ctx.queryId().hi,
ctx.queryId().lo);
+ }
+
+ Transaction txn;
+ PlanFragment root = planner.getFragments().get(0);
+ DataSink sink = createDataSink(ctx, root);
+ Preconditions.checkArgument(sink instanceof OlapTableSink, "olap table
sink is expected when"
+ + " running insert into select");
+ txn = new Transaction(ctx, database, table, label, planner);
+
+ OlapTableSink olapTableSink = ((OlapTableSink) sink);
+ olapTableSink.init(ctx.queryId(), txn.getTxnId(), database.getId(),
ctx.getExecTimeout(),
+ ctx.getSessionVariable().getSendBatchParallelism(), false);
+ olapTableSink.complete();
+ root.resetSink(olapTableSink);
+
+ if (isExplain()) {
+ executor.handleExplainStmt(((ExplainCommand)
logicalQuery).getExplainString(planner));
+ return;
+ }
+
+ txn.executeInsertIntoSelectCommand(executor);
+ }
+
+ private void checkDatabaseAndTable(ConnectContext ctx) {
Review Comment:
bindTargetRelation
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -0,0 +1,271 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.glue.LogicalPlanAdapter;
+import org.apache.doris.nereids.properties.DistributionSpecHash;
+import org.apache.doris.nereids.properties.DistributionSpecHash.ShuffleType;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.txn.Transaction;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.planner.DataSink;
+import org.apache.doris.planner.OlapTableSink;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * insert into select command implementation
+ *
+ * insert into select command support the grammer: explain? insert into table
columns? partitions? hints? query
+ * InsertIntoTableCommand is a command to represent insert the answer of a
query into a table.
+ * class structure's:
+ * InsertIntoTableCommand(Query())
+ * InsertIntoTableCommand(ExplainCommand(Query()))
+ */
+public class InsertIntoTableCommand extends Command implements ForwardWithSync
{
+ public static final Logger LOG =
LogManager.getLogger(InsertIntoTableCommand.class);
+ private final List<String> tableName;
+ private final List<String> colNames;
+ private final LogicalPlan logicalQuery;
+ private final String labelName;
+ private Database database;
+ private Table table;
+ private NereidsPlanner planner;
+ private TupleDescriptor olapTuple;
+ private List<String> partitions;
+ private List<String> hints;
+ private List<Column> targetColumns;
+ private List<Long> partitionIds = null;
+
+ /**
+ * constructor
+ */
+ public InsertIntoTableCommand(List<String> tableName, String labelName,
List<String> colNames,
+ List<String> partitions, List<String> hints, LogicalPlan
logicalQuery) {
+ super(PlanType.INSERT_INTO_SELECT_COMMAND);
+ Preconditions.checkArgument(tableName != null, "tableName cannot be
null in insert-into-select command");
+ Preconditions.checkArgument(logicalQuery != null, "logicalQuery cannot
be null in insert-into-select command");
+ this.tableName = tableName;
+ this.labelName = labelName;
+ this.colNames = colNames;
+ this.partitions = partitions;
+ this.hints = hints;
+ this.logicalQuery = logicalQuery;
+ }
+
+ public NereidsPlanner getPlanner() {
+ return planner;
+ }
+
+ @Override
+ public void run(ConnectContext ctx, StmtExecutor executor) throws
Exception {
+ if (ctx.isTxnModel()) {
+ // in original planner and in txn model, we can execute sql like:
insert into t select 1, 2, 3
+ // but no data will be inserted, now we adjust forbid it.
+ throw new AnalysisException("insert into table command is not
supported in txn model");
+ }
+ checkDatabaseAndTable(ctx);
+ getColumns();
+ getPartition();
Review Comment:
put the code block back to `run` method and remove these functions
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/txn/Transaction.java:
##########
@@ -0,0 +1,243 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.txn;
+
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.catalog.TableIf.TableType;
+import org.apache.doris.common.Config;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+import org.apache.doris.common.MetaNotFoundException;
+import org.apache.doris.common.UserException;
+import org.apache.doris.common.util.DebugUtil;
+import org.apache.doris.load.EtlJobType;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.Coordinator;
+import org.apache.doris.qe.QeProcessorImpl;
+import org.apache.doris.qe.StmtExecutor;
+import org.apache.doris.service.FrontendOptions;
+import org.apache.doris.task.LoadEtlTask;
+import org.apache.doris.thrift.TQueryType;
+import org.apache.doris.transaction.TabletCommitInfo;
+import org.apache.doris.transaction.TransactionState.LoadJobSourceType;
+import org.apache.doris.transaction.TransactionState.TxnCoordinator;
+import org.apache.doris.transaction.TransactionState.TxnSourceType;
+import org.apache.doris.transaction.TransactionStatus;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+/**
+ * transaction wrapper for Nereids
+ */
+public class Transaction {
Review Comment:
why need Nereids' transaction?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]