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

morrysnow pushed a commit to branch branch-3.1
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-3.1 by this push:
     new 7bbcb6d7ccd Branch 3.1: [fix](nereids) fix insert into values throw 
'Invalid call to sql on unbound object' and 'Insert has filtered data in strict 
mode' exception #52802 (#53032)
7bbcb6d7ccd is described below

commit 7bbcb6d7ccd0a3452432077645856b45c6de683e
Author: 924060929 <[email protected]>
AuthorDate: Thu Jul 10 17:29:13 2025 +0800

    Branch 3.1: [fix](nereids) fix insert into values throw 'Invalid call to 
sql on unbound object' and 'Insert has filtered data in strict mode' exception 
#52802 (#53032)
    
    cherry pick from #52802
---
 .../doris/nereids/rules/analysis/BindSink.java     |  11 +++++-
 .../trees/plans/commands/insert/InsertUtils.java   |  42 ++++++++++++---------
 .../nereids_p0/insert_into_table/insert_values.out | Bin 2495 -> 2621 bytes
 .../insert_into_table/insert_values.groovy         |  34 +++++++++++++++++
 4 files changed, 68 insertions(+), 19 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java
index 575fdb2473c..760783359d0 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java
@@ -94,6 +94,15 @@ import java.util.stream.Collectors;
  * bind an unbound logicalTableSink represent the target table of an insert 
command
  */
 public class BindSink implements AnalysisRuleFactory {
+    public boolean needTruncateStringWhenInsert;
+
+    public BindSink() {
+        this(true);
+    }
+
+    public BindSink(boolean needTruncateStringWhenInsert) {
+        this.needTruncateStringWhenInsert = needTruncateStringWhenInsert;
+    }
 
     @Override
     public List<Rule> buildRules() {
@@ -262,7 +271,7 @@ public class BindSink implements AnalysisRuleFactory {
                 int targetLength = ((CharacterType) targetType).getLen();
                 if (sourceLength == targetLength) {
                     castExpr = TypeCoercionUtils.castIfNotSameType(castExpr, 
targetType);
-                } else if (sourceLength > targetLength && targetLength >= 0) {
+                } else if (needTruncateStringWhenInsert && sourceLength > 
targetLength && targetLength >= 0) {
                     castExpr = new Substring(castExpr, Literal.of(1), 
Literal.of(targetLength));
                 } else if (targetType.isStringType()) {
                     castExpr = new Cast(castExpr, StringType.INSTANCE);
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 e3ea8e8ba2b..6aa208e8a4c 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
@@ -381,7 +381,7 @@ public class InsertUtils {
                 for (int i = 0; i < columns.size(); i++) {
                     Column column = columns.get(i);
                     NamedExpression defaultExpression = 
generateDefaultExpression(column);
-                    addColumnValue(analyzer, optimizedRowConstructor, 
defaultExpression);
+                    addColumnValue(analyzer, optimizedRowConstructor, 
defaultExpression, null, rewriteContext);
                 }
             } else {
                 if 
(CollectionUtils.isNotEmpty(unboundLogicalSink.getColNames())) {
@@ -408,14 +408,14 @@ public class InsertUtils {
                         }
                         if (values.get(i) instanceof DefaultValueSlot) {
                             NamedExpression defaultExpression = 
generateDefaultExpression(sameNameColumn);
-                            addColumnValue(analyzer, optimizedRowConstructor, 
defaultExpression);
+                            addColumnValue(
+                                    analyzer, optimizedRowConstructor, 
defaultExpression, null, rewriteContext
+                            );
                         } else {
                             DataType targetType = 
DataType.fromCatalogType(sameNameColumn.getType());
-                            Expression castValue = castValue(values.get(i), 
targetType);
-                            castValue = rewriteContext == null
-                                    ? castValue
-                                    : FoldConstantRuleOnFE.evaluate(castValue, 
rewriteContext);
-                            addColumnValue(analyzer, optimizedRowConstructor, 
(NamedExpression) castValue);
+                            addColumnValue(
+                                    analyzer, optimizedRowConstructor, 
values.get(i), targetType, rewriteContext
+                            );
                         }
                     }
                 } else {
@@ -431,14 +431,14 @@ public class InsertUtils {
                         }
                         if (values.get(i) instanceof DefaultValueSlot) {
                             NamedExpression defaultExpression = 
generateDefaultExpression(columns.get(i));
-                            addColumnValue(analyzer, optimizedRowConstructor, 
defaultExpression);
+                            addColumnValue(
+                                    analyzer, optimizedRowConstructor, 
defaultExpression, null, rewriteContext
+                            );
                         } else {
                             DataType targetType = 
DataType.fromCatalogType(columns.get(i).getType());
-                            Expression castValue = castValue(values.get(i), 
targetType);
-                            castValue = rewriteContext == null
-                                    ? castValue
-                                    : FoldConstantRuleOnFE.evaluate(castValue, 
rewriteContext);
-                            addColumnValue(analyzer, optimizedRowConstructor, 
(NamedExpression) castValue);
+                            addColumnValue(
+                                    analyzer, optimizedRowConstructor, 
values.get(i), targetType, rewriteContext
+                            );
                         }
                     }
                 }
@@ -518,26 +518,32 @@ public class InsertUtils {
     private static void addColumnValue(
             Optional<ExpressionAnalyzer> analyzer,
             ImmutableList.Builder<NamedExpression> optimizedRowConstructor,
-            NamedExpression value) {
+            NamedExpression value, DataType targetType, 
ExpressionRewriteContext rewriteContext) {
+        if (targetType != null) {
+            value = castValue(value, targetType);
+        }
         if (analyzer.isPresent() && !(value instanceof Alias && value.child(0) 
instanceof Literal)) {
             ExpressionAnalyzer expressionAnalyzer = analyzer.get();
             value = (NamedExpression) expressionAnalyzer.analyze(
-                value, new 
ExpressionRewriteContext(expressionAnalyzer.getCascadesContext())
+                    value, new 
ExpressionRewriteContext(expressionAnalyzer.getCascadesContext())
             );
+            value = rewriteContext == null
+                    ? value
+                    : (NamedExpression) FoldConstantRuleOnFE.evaluate(value, 
rewriteContext);
         }
         optimizedRowConstructor.add(value);
     }
 
-    private static Alias castValue(Expression value, DataType targetType) {
+    private static NamedExpression castValue(Expression value, DataType 
targetType) {
         if (value instanceof Alias) {
             Expression oldChild = value.child(0);
             Expression newChild = TypeCoercionUtils.castUnbound(oldChild, 
targetType);
             return (Alias) (oldChild == newChild ? value : 
value.withChildren(newChild));
         } else if (value instanceof UnboundAlias) {
             UnboundAlias unboundAlias = (UnboundAlias) value;
-            return new 
Alias(TypeCoercionUtils.castUnbound(unboundAlias.child(), targetType));
+            return new 
UnboundAlias(TypeCoercionUtils.castUnbound(unboundAlias.child(), targetType));
         } else {
-            return new Alias(TypeCoercionUtils.castUnbound(value, targetType));
+            return new UnboundAlias(TypeCoercionUtils.castUnbound(value, 
targetType));
         }
     }
 
diff --git 
a/regression-test/data/nereids_p0/insert_into_table/insert_values.out 
b/regression-test/data/nereids_p0/insert_into_table/insert_values.out
index 36d51ffcb2a..62d824e5e6f 100644
Binary files 
a/regression-test/data/nereids_p0/insert_into_table/insert_values.out and 
b/regression-test/data/nereids_p0/insert_into_table/insert_values.out differ
diff --git 
a/regression-test/suites/nereids_p0/insert_into_table/insert_values.groovy 
b/regression-test/suites/nereids_p0/insert_into_table/insert_values.groovy
index 84461ca2ba8..d6da58ea4fd 100644
--- a/regression-test/suites/nereids_p0/insert_into_table/insert_values.groovy
+++ b/regression-test/suites/nereids_p0/insert_into_table/insert_values.groovy
@@ -143,4 +143,38 @@ suite('nereids_insert_into_values') {
     sql "insert into agg_have_dup_base_value values (-4, -4, -4, 'd')"
     sql "sync"
     qt_mv "select * from agg_have_dup_base_value"
+
+    multi_sql """
+            DROP TABLE IF EXISTS test_insert_cast_interval;
+            CREATE TABLE test_insert_cast_interval (
+              `id` int NULL,
+              `dt` date NULL
+            ) ENGINE=OLAP
+            DUPLICATE KEY(`id`, `dt`)
+            DISTRIBUTED BY HASH(`id`) BUCKETS 10
+            PROPERTIES (
+                "replication_allocation" = "tag.location.default: 1"
+            );
+            
+            INSERT INTO test_insert_cast_interval values(1, 
date_floor('2020-02-02', interval 1 second));
+        """
+
+    qt_select_test_insert_cast_interval "select * from 
test_insert_cast_interval"
+
+    multi_sql """
+        drop table if exists test_insert_more_string;
+        CREATE TABLE test_insert_more_string (
+            `r_regionkey` int NULL,
+            `r_name` varchar(25) NULL,
+            `r_comment` varchar(152) NULL
+        )
+        DUPLICATE KEY(`r_regionkey`)
+        DISTRIBUTED BY HASH(`r_regionkey`)
+        BUCKETS 1 PROPERTIES (
+            "replication_allocation" = "tag.location.default: 1"
+        );
+        insert into test_insert_more_string values (3, 
"akljalkjbalkjsldkrjewokjfalksdjflaksjfdlaskjfalsdkfjalsdfjkasfdl", "aa")
+        """
+
+    qt_select_test_insert_more_string "select * from test_insert_more_string"
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to