This is an automated email from the ASF dual-hosted git repository.

huajianlan pushed a commit to branch opt_insert_into_values
in repository https://gitbox.apache.org/repos/asf/doris.git

commit dd4c526422438f01dbf4e6756c669bd179332cd3
Author: 924060929 <lanhuaj...@selectdb.com>
AuthorDate: Mon Dec 9 14:57:51 2024 +0800

    fix
---
 .../insert/BatchInsertIntoTableCommand.java        |  6 ++-
 .../commands/insert/InsertIntoTableCommand.java    | 35 ++++++++++-----
 .../insert/InsertOverwriteTableCommand.java        |  5 ++-
 .../trees/plans/commands/insert/InsertUtils.java   | 52 ++++++++++++++++++----
 4 files changed, 73 insertions(+), 25 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/BatchInsertIntoTableCommand.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/BatchInsertIntoTableCommand.java
index b4a7a9eee3a..a588d61a330 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/BatchInsertIntoTableCommand.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/BatchInsertIntoTableCommand.java
@@ -78,7 +78,7 @@ public class BatchInsertIntoTableCommand extends Command 
implements NoForward, E
 
     @Override
     public Plan getExplainPlan(ConnectContext ctx) throws Exception {
-        return InsertUtils.getPlanForExplain(ctx, this.logicalQuery);
+        return InsertUtils.getPlanForExplain(ctx, Optional.empty(), 
this.logicalQuery);
     }
 
     @Override
@@ -98,7 +98,9 @@ public class BatchInsertIntoTableCommand extends Command 
implements NoForward, E
         TableIf targetTableIf = InsertUtils.getTargetTable(logicalQuery, ctx);
         targetTableIf.readLock();
         try {
-            this.logicalQuery = (LogicalPlan) 
InsertUtils.normalizePlan(logicalQuery, targetTableIf, Optional.empty());
+            this.logicalQuery = (LogicalPlan) InsertUtils.normalizePlan(
+                logicalQuery, targetTableIf, Optional.empty(), Optional.empty()
+            );
             LogicalPlanAdapter logicalPlanAdapter = new 
LogicalPlanAdapter(logicalQuery, ctx.getStatementContext());
             NereidsPlanner planner = new 
NereidsPlanner(ctx.getStatementContext());
             planner.plan(logicalPlanAdapter, 
ctx.getSessionVariable().toThrift());
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertIntoTableCommand.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertIntoTableCommand.java
index 525c380e3f1..f689ef02b77 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertIntoTableCommand.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertIntoTableCommand.java
@@ -30,6 +30,7 @@ import 
org.apache.doris.datasource.iceberg.IcebergExternalTable;
 import org.apache.doris.datasource.jdbc.JdbcExternalTable;
 import org.apache.doris.load.loadv2.LoadStatistic;
 import org.apache.doris.mysql.privilege.PrivPredicate;
+import org.apache.doris.nereids.CascadesContext;
 import org.apache.doris.nereids.NereidsPlanner;
 import org.apache.doris.nereids.StatementContext;
 import org.apache.doris.nereids.analyzer.UnboundTableSink;
@@ -103,6 +104,7 @@ public class InsertIntoTableCommand extends Command 
implements ForwardWithSync,
     public static final Logger LOG = 
LogManager.getLogger(InsertIntoTableCommand.class);
 
     private LogicalPlan logicalQuery;
+    private Optional<CascadesContext> analyzeContext;
     private Optional<String> labelName;
     /**
      * When source it's from job scheduler,it will be set.
@@ -179,8 +181,14 @@ public class InsertIntoTableCommand extends Command 
implements ForwardWithSync,
         // should lock target table until we begin transaction.
         targetTableIf.readLock();
         try {
+            this.analyzeContext = Optional.of(
+                    CascadesContext.initContext(ctx.getStatementContext(), 
logicalQuery, PhysicalProperties.ANY)
+            );
+
             // 1. process inline table (default values, empty values)
-            this.logicalQuery = (LogicalPlan) 
InsertUtils.normalizePlan(logicalQuery, targetTableIf, insertCtx);
+            this.logicalQuery = (LogicalPlan) InsertUtils.normalizePlan(
+                logicalQuery, targetTableIf, analyzeContext, insertCtx
+            );
             if (cte.isPresent()) {
                 this.logicalQuery = ((LogicalPlan) 
cte.get().withChildren(logicalQuery));
             }
@@ -396,7 +404,7 @@ public class InsertIntoTableCommand extends Command 
implements ForwardWithSync,
 
     @Override
     public Plan getExplainPlan(ConnectContext ctx) {
-        return InsertUtils.getPlanForExplain(ctx, this.logicalQuery);
+        return InsertUtils.getPlanForExplain(ctx, this.analyzeContext, 
this.logicalQuery);
     }
 
     @Override
@@ -497,14 +505,15 @@ public class InsertIntoTableCommand extends Command 
implements ForwardWithSync,
                         UnboundTableSink<? extends Plan> olapTableSink, Void 
context) {
                     olapTableSink =
                             (UnboundTableSink<? extends Plan>) 
super.visitUnboundTableSink(olapTableSink, context);
-                    return new BindSink()
-                            .buildRules()
-                            .stream()
-                            .filter(rule -> rule.getRuleType() == 
RuleType.BINDING_INSERT_TARGET_TABLE)
-                            .findFirst()
-                            .get()
-                            .transform(olapTableSink, getCascadesContext())
-                            .get(0);
+
+                    return (LogicalOlapTableSink<?>) new BindSink()
+                        .buildRules()
+                        .stream()
+                        .filter(rule -> rule.getRuleType() == 
RuleType.BINDING_INSERT_TARGET_TABLE)
+                        .findFirst()
+                        .get()
+                        .transform(olapTableSink, getCascadesContext())
+                        .get(0);
                 }
 
                 @Override
@@ -517,9 +526,11 @@ public class InsertIntoTableCommand extends Command 
implements ForwardWithSync,
                         outputs.add(new SlotReference(slot.getName(), 
slot.getDataType(), slot.nullable()));
                     }
 
-                    return new LogicalUnion(
-                            Qualifier.ALL, 
logicalInlineTable.getConstantExprsList(), ImmutableList.of()
+                    LogicalUnion union = new LogicalUnion(
+                        Qualifier.ALL, 
logicalInlineTable.getConstantExprsList(), ImmutableList.of()
                     ).withNewOutputs(outputs);
+
+                    return union;
                 }
             };
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertOverwriteTableCommand.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertOverwriteTableCommand.java
index c89a4fc7be9..ed0718caf39 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertOverwriteTableCommand.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertOverwriteTableCommand.java
@@ -122,7 +122,8 @@ public class InsertOverwriteTableCommand extends Command 
implements ForwardWithS
         if (targetTableIf instanceof MTMV && 
!MTMVUtil.allowModifyMTMVData(ctx)) {
             throw new AnalysisException("Not allowed to perform current 
operation on async materialized view");
         }
-        this.logicalQuery = (LogicalPlan) 
InsertUtils.normalizePlan(logicalQuery, targetTableIf, Optional.empty());
+        this.logicalQuery = (LogicalPlan) InsertUtils.normalizePlan(
+            logicalQuery, targetTableIf, Optional.empty(), Optional.empty());
         if (cte.isPresent()) {
             this.logicalQuery = (LogicalPlan) 
logicalQuery.withChildren(cte.get().withChildren(
                     this.logicalQuery.child(0)));
@@ -362,7 +363,7 @@ public class InsertOverwriteTableCommand extends Command 
implements ForwardWithS
 
     @Override
     public Plan getExplainPlan(ConnectContext ctx) {
-        return InsertUtils.getPlanForExplain(ctx, this.logicalQuery);
+        return InsertUtils.getPlanForExplain(ctx, Optional.empty(), 
this.logicalQuery);
     }
 
     @Override
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java
index b09b2429941..271ce2e0126 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java
@@ -30,6 +30,7 @@ import org.apache.doris.common.FormatOptions;
 import org.apache.doris.datasource.hive.HMSExternalTable;
 import org.apache.doris.datasource.jdbc.JdbcExternalTable;
 import org.apache.doris.nereids.CascadesContext;
+import org.apache.doris.nereids.analyzer.Scope;
 import org.apache.doris.nereids.analyzer.UnboundAlias;
 import org.apache.doris.nereids.analyzer.UnboundHiveTableSink;
 import org.apache.doris.nereids.analyzer.UnboundIcebergTableSink;
@@ -38,6 +39,7 @@ import org.apache.doris.nereids.analyzer.UnboundTableSink;
 import org.apache.doris.nereids.exceptions.AnalysisException;
 import org.apache.doris.nereids.parser.NereidsParser;
 import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.rules.analysis.ExpressionAnalyzer;
 import org.apache.doris.nereids.rules.expression.ExpressionRewriteContext;
 import org.apache.doris.nereids.rules.expression.rules.FoldConstantRuleOnFE;
 import org.apache.doris.nereids.trees.expressions.Alias;
@@ -262,7 +264,9 @@ public class InsertUtils {
     /**
      * normalize plan to let it could be process correctly by nereids
      */
-    public static Plan normalizePlan(Plan plan, TableIf table, 
Optional<InsertCommandContext> insertCtx) {
+    public static Plan normalizePlan(LogicalPlan plan, TableIf table,
+                                     Optional<CascadesContext> analyzeContext,
+                                     Optional<InsertCommandContext> insertCtx) 
{
         UnboundLogicalSink<? extends Plan> unboundLogicalSink = 
(UnboundLogicalSink<? extends Plan>) plan;
         if (table instanceof HMSExternalTable) {
             HMSExternalTable hiveTable = (HMSExternalTable) table;
@@ -355,6 +359,10 @@ public class InsertUtils {
             );
         }
 
+        Optional<ExpressionAnalyzer> analyzer = analyzeContext.map(
+            cascadesContext -> buildAnalyzer(plan, cascadesContext)
+        );
+
         Boolean[] outputSlotNullables = new 
Boolean[logicalInlineTable.getConstantExprsList().get(0).size()];
         Arrays.fill(outputSlotNullables, false);
 
@@ -368,7 +376,7 @@ public class InsertUtils {
                     Column column = columns.get(i);
                     NamedExpression defaultExpression = 
generateDefaultExpression(column);
                     addColumnValue(
-                            optimizedRowConstructor, outputSlotNullables, i, 
defaultExpression
+                        analyzer, optimizedRowConstructor, 
outputSlotNullables, i, defaultExpression
                     );
                 }
             } else {
@@ -397,7 +405,8 @@ public class InsertUtils {
                         if (values.get(i) instanceof DefaultValueSlot) {
                             NamedExpression defaultExpression = 
generateDefaultExpression(sameNameColumn);
                             addColumnValue(
-                                    optimizedRowConstructor, 
outputSlotNullables, i, defaultExpression
+                                analyzer, optimizedRowConstructor,
+                                outputSlotNullables, i, defaultExpression
                             );
                         } else {
                             DataType targetType = 
DataType.fromCatalogType(sameNameColumn.getType());
@@ -405,8 +414,8 @@ public class InsertUtils {
                             castValue = rewriteContext == null
                                     ? castValue
                                     : FoldConstantRuleOnFE.evaluate(castValue, 
rewriteContext);
-                            addColumnValue(
-                                    optimizedRowConstructor, 
outputSlotNullables, i, (NamedExpression) castValue
+                            addColumnValue(analyzer,
+                                optimizedRowConstructor, outputSlotNullables, 
i, (NamedExpression) castValue
                             );
                         }
                     }
@@ -424,7 +433,8 @@ public class InsertUtils {
                         if (values.get(i) instanceof DefaultValueSlot) {
                             NamedExpression defaultExpression = 
generateDefaultExpression(columns.get(i));
                             addColumnValue(
-                                    optimizedRowConstructor, 
outputSlotNullables, i, defaultExpression
+                                analyzer, optimizedRowConstructor,
+                                outputSlotNullables, i, defaultExpression
                             );
                         } else {
                             DataType targetType = 
DataType.fromCatalogType(columns.get(i).getType());
@@ -433,7 +443,8 @@ public class InsertUtils {
                                     ? castValue
                                     : FoldConstantRuleOnFE.evaluate(castValue, 
rewriteContext);
                             addColumnValue(
-                                    optimizedRowConstructor, 
outputSlotNullables, i, (NamedExpression) castValue
+                                analyzer, optimizedRowConstructor,
+                                outputSlotNullables, i, (NamedExpression) 
castValue
                             );
                         }
                     }
@@ -447,9 +458,30 @@ public class InsertUtils {
         )));
     }
 
+    private static ExpressionAnalyzer buildAnalyzer(LogicalPlan plan, 
CascadesContext analyzeContext) {
+        return new ExpressionAnalyzer(plan, new Scope(ImmutableList.of()),
+            analyzeContext, false, false) {
+            @Override
+            public Expression visitCast(Cast cast, ExpressionRewriteContext 
context) {
+                Expression expr = super.visitCast(cast, context);
+                if (expr instanceof Cast) {
+                    expr = FoldConstantRuleOnFE.evaluate(expr, context);
+                }
+                return expr;
+            }
+        };
+    }
+
     private static void addColumnValue(
+            Optional<ExpressionAnalyzer> analyzer,
             ImmutableList.Builder<NamedExpression> optimizedRowConstructor,
             Boolean[] nullable, int index, NamedExpression value) {
+        if (analyzer.isPresent() && !(value instanceof Alias && value.child(0) 
instanceof Literal)) {
+            ExpressionAnalyzer expressionAnalyzer = analyzer.get();
+            value = (NamedExpression) expressionAnalyzer.analyze(
+                value, new 
ExpressionRewriteContext(expressionAnalyzer.getCascadesContext())
+            );
+        }
         optimizedRowConstructor.add(value);
         nullable[index] |= value.nullable();
     }
@@ -519,8 +551,10 @@ public class InsertUtils {
     /**
      * get plan for explain.
      */
-    public static Plan getPlanForExplain(ConnectContext ctx, LogicalPlan 
logicalQuery) {
-        return InsertUtils.normalizePlan(logicalQuery, 
InsertUtils.getTargetTable(logicalQuery, ctx), Optional.empty());
+    public static Plan getPlanForExplain(
+            ConnectContext ctx, Optional<CascadesContext> analyzeContext, 
LogicalPlan logicalQuery) {
+        return InsertUtils.normalizePlan(
+            logicalQuery, InsertUtils.getTargetTable(logicalQuery, ctx), 
analyzeContext, Optional.empty());
     }
 
     // check for insert into t1(a,b,gen_col) select 1,2,3;


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to