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

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


The following commit(s) were added to refs/heads/branch-2.0 by this push:
     new e02f583f996 [fix](Nereids) fix insert into table with null literal 
default value (#39122) (#39673)
e02f583f996 is described below

commit e02f583f99688b2c96dab6fa8b763e7cb1b85772
Author: LiBinfeng <46676950+libinfeng...@users.noreply.github.com>
AuthorDate: Fri Aug 23 15:01:49 2024 +0800

    [fix](Nereids) fix insert into table with null literal default value 
(#39122) (#39673)
    
    cherry-pick from master #39122
    
    Problem:
    when use insert with default value null, it can not be insert
    successfully
    Solved:
    when column is allow to be null, it can be null in create table with
    null default value
---
 .../src/main/java/org/apache/doris/analysis/NativeInsertStmt.java | 8 ++++++--
 fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java     | 3 +++
 .../java/org/apache/doris/nereids/rules/analysis/BindSink.java    | 6 ++++++
 .../src/test/java/org/apache/doris/nereids/util/ReadLockTest.java | 3 ++-
 4 files changed, 17 insertions(+), 3 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/analysis/NativeInsertStmt.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/NativeInsertStmt.java
index f95efcd3474..b9a745dad20 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/NativeInsertStmt.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/NativeInsertStmt.java
@@ -853,11 +853,15 @@ public class NativeInsertStmt extends InsertStmt {
             Column col = targetColumns.get(i);
 
             if (expr instanceof DefaultValueExpr) {
-                if (targetColumns.get(i).getDefaultValue() == null) {
+                if (targetColumns.get(i).getDefaultValue() == null && 
!targetColumns.get(i).isAllowNull()) {
                     throw new AnalysisException("Column has no default value, 
column="
                             + targetColumns.get(i).getName());
                 }
-                expr = new 
StringLiteral(targetColumns.get(i).getDefaultValue());
+                if (targetColumns.get(i).getDefaultValue() == null) {
+                    expr = new NullLiteral();
+                } else {
+                    expr = new 
StringLiteral(targetColumns.get(i).getDefaultValue());
+                }
             }
             if (expr instanceof Subquery) {
                 throw new AnalysisException("Insert values can not be query");
diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java 
b/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java
index 69d7e238af0..3dc7da3e0a0 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java
@@ -450,6 +450,9 @@ public class Column implements Writable, 
GsonPostProcessable {
     }
 
     public Expr getDefaultValueExpr() throws AnalysisException {
+        if (defaultValue == null) {
+            return null;
+        }
         StringLiteral defaultValueLiteral = new StringLiteral(defaultValue);
         if (getDataType() == PrimitiveType.VARCHAR) {
             return defaultValueLiteral;
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 ddcb3ca3164..0e12b8128e7 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
@@ -217,6 +217,12 @@ public class BindSink implements AnalysisRuleFactory {
                                             // should not contain these 
unmentioned columns, so we just skip them.
                                             continue;
                                         } else if (column.getDefaultValue() == 
null) {
+                                            // throw exception if explicitly 
use Default value null when not nullable
+                                            // insert into table t 
values(DEFAULT)
+                                            if (!column.isAllowNull()) {
+                                                throw new 
AnalysisException("Column has no default value,"
+                                                    + " column=" + 
column.getName());
+                                            }
                                             // Otherwise, the unmentioned 
columns should be filled with default values
                                             // or null values
                                             
columnToOutput.put(column.getName(), new Alias(
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/util/ReadLockTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/util/ReadLockTest.java
index 264891804ff..5d47127ea99 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/nereids/util/ReadLockTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/util/ReadLockTest.java
@@ -109,7 +109,8 @@ public class ReadLockTest extends SSBTestBase {
 
     @Test
     public void testInsertInto() {
-        String sql = "INSERT INTO supplier(s_suppkey) SELECT lo_orderkey FROM 
lineorder";
+        String sql = "INSERT INTO supplier(s_suppkey, s_name, s_address, 
s_city, s_nation, s_region, s_phone) "
+                + "SELECT lo_orderkey, '', '', '', '', '', '' FROM lineorder";
         StatementContext statementContext = 
MemoTestUtils.createStatementContext(connectContext, sql);
         boolean originalDML = 
connectContext.getSessionVariable().enableNereidsDML;
         connectContext.getSessionVariable().enableNereidsDML = true;


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

Reply via email to