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

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


The following commit(s) were added to refs/heads/branch-2.1 by this push:
     new 46cbbee12ef branch-2.1: [feat](nereids) Add session variable 
enable_insert_value_auto_cast for insert value truncate long string #55325 
(#55427)
46cbbee12ef is described below

commit 46cbbee12ef11f0e7149d71ebf90c61b302963c6
Author: yujun <[email protected]>
AuthorDate: Fri Aug 29 09:52:45 2025 +0800

    branch-2.1: [feat](nereids) Add session variable 
enable_insert_value_auto_cast for insert value truncate long string #55325 
(#55427)
    
    cherry pick from #55325
---
 .../doris/nereids/rules/analysis/BindSink.java     |   4 +-
 .../java/org/apache/doris/qe/SessionVariable.java  |  12 +++++
 .../nereids_p0/insert_into_table/insert_values.out | Bin 2495 -> 2606 bytes
 .../insert_into_table/insert_values.groovy         |  54 +++++++++++++++++++--
 4 files changed, 66 insertions(+), 4 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 acb9663caac..c7c143e4e99 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
@@ -238,6 +238,8 @@ public class BindSink implements AnalysisRuleFactory {
 
         // add cast project
         List<NamedExpression> castExprs = Lists.newArrayList();
+        ConnectContext connCtx = ConnectContext.get();
+        final boolean truncateString = connCtx == null || 
connCtx.getSessionVariable().enableInsertValueAutoCast;
         for (int i = 0; i < tableSchema.size(); ++i) {
             Column col = tableSchema.get(i);
             NamedExpression expr = columnToOutput.get(col.getName());
@@ -257,7 +259,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 (truncateString && 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/qe/SessionVariable.java 
b/fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java
index 4d4c427128c..47b3a8f692a 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java
@@ -133,6 +133,7 @@ public class SessionVariable implements Serializable, 
Writable {
     public static final String MAX_INSTANCE_NUM = "max_instance_num";
     public static final String DML_PLAN_RETRY_TIMES = "DML_PLAN_RETRY_TIMES";
     public static final String ENABLE_INSERT_STRICT = "enable_insert_strict";
+    public static final String ENABLE_INSERT_VALUE_AUTO_CAST = 
"enable_insert_value_auto_cast";
     public static final String INSERT_MAX_FILTER_RATIO = 
"insert_max_filter_ratio";
     public static final String ENABLE_SPILLING = "enable_spilling";
     public static final String ENABLE_SHORT_CIRCUIT_QUERY = 
"enable_short_circuit_point_query";
@@ -992,6 +993,13 @@ public class SessionVariable implements Serializable, 
Writable {
     @VariableMgr.VarAttr(name = ENABLE_INSERT_STRICT, needForward = true)
     public boolean enableInsertStrict = true;
 
+    @VariableMgr.VarAttr(name = ENABLE_INSERT_VALUE_AUTO_CAST, needForward = 
true, description = {
+            "INSERT VALUE 语句是否自动类型转换。当前只针对长字符串自动截短。默认开。",
+            "INSERT VALUE statement whether to automatically type cast. Only 
use for truncate long string. "
+                    + "ON by default."
+    })
+    public boolean enableInsertValueAutoCast = true;
+
     @VariableMgr.VarAttr(name = INSERT_MAX_FILTER_RATIO, needForward = true)
     public double insertMaxFilterRatio = 1.0;
 
@@ -2965,6 +2973,10 @@ public class SessionVariable implements Serializable, 
Writable {
         this.enableInsertStrict = enableInsertStrict;
     }
 
+    public boolean getEnableInsertValueAutoCast() {
+        return enableInsertValueAutoCast;
+    }
+
     public double getInsertMaxFilterRatio() {
         return insertMaxFilterRatio;
     }
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..59ba6ff19ac 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..42aa9bd7111 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
@@ -21,8 +21,6 @@ suite('nereids_insert_into_values') {
     sql 'set enable_nereids_dml=true'
     sql 'set enable_strict_consistency_dml=true'
 
-    sql 'use nereids_insert_into_table_test'
-
     def t1 = 'value_t1'
     def t2 = 'value_t2'
     def t3 = 'value_t3'
@@ -143,4 +141,54 @@ 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"
-}
\ No newline at end of file
+
+    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(4) NULL
+        )
+        DUPLICATE KEY(`r_regionkey`)
+        DISTRIBUTED BY HASH(`r_regionkey`)
+        BUCKETS 1 PROPERTIES (
+            "replication_allocation" = "tag.location.default: 1"
+        );
+        """
+
+    // shorter varchar is ok
+    sql "insert into test_insert_more_string values (1, 'ab')"
+
+    // set enable_insert_value_auto_cast = true
+    // longer varchar will truncate
+    sql "insert into test_insert_more_string values (2, 'abcdefg')"
+
+    // when disable string auto cast and in insert strict mode, insert will 
failed
+    sql 'set enable_insert_value_auto_cast = false'
+    test {
+        sql "insert into test_insert_more_string values (3, 'hi'), (4, 
'jklmn')"
+        exception 'Insert has filtered data in strict mode'
+    }
+
+    // when disable insert strict, the longer varchar row will be filtered, 
other rows will succ
+    sql 'set enable_insert_strict = false'
+    sql "insert into test_insert_more_string values (5, 'o'), (6, 'pqrst')"
+
+    order_qt_select_test_insert_more_string "select * from 
test_insert_more_string"
+}


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

Reply via email to