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 ca9e50e49db [fix](Nereids) fix insert into table with null literal 
default value (#39122) (#39669)
ca9e50e49db is described below

commit ca9e50e49dbf7c2899b573230150ac99dd859e3c
Author: LiBinfeng <[email protected]>
AuthorDate: Thu Aug 22 10:37:50 2024 +0800

    [fix](Nereids) fix insert into table with null literal default value 
(#39122) (#39669)
    
    cherry-pick: #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
    
    ## Proposed changes
    
    Issue Number: close #xxx
    
    <!--Describe your changes.-->
---
 .../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    | 2 +-
 .../doris/nereids/trees/plans/commands/insert/InsertUtils.java    | 4 +++-
 4 files changed, 13 insertions(+), 4 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 3f948aefa75..e9d9c152aa1 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
@@ -920,11 +920,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 b600f31ca20..80c5b739e04 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
@@ -498,6 +498,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 4c00db457f4..0bda233e6f3 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
@@ -344,7 +344,7 @@ public class BindSink implements AnalysisRuleFactory {
                 } else if (column.getDefaultValue() == null) {
                     // throw exception if explicitly use Default value but no 
default value present
                     // insert into table t values(DEFAULT)
-                    if (columnToChildOutput.get(column) instanceof 
DefaultValueSlot) {
+                    if (columnToChildOutput.get(column) instanceof 
DefaultValueSlot && !column.isAllowNull()) {
                         throw new AnalysisException("Column has no default 
value,"
                                 + " column=" + column.getName());
                     }
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 34c43b3ec84..6a283ca023a 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
@@ -397,7 +397,9 @@ public class InsertUtils {
     private static NamedExpression generateDefaultExpression(Column column) {
         try {
             if (column.getDefaultValue() == null) {
-                throw new AnalysisException("Column has no default value, 
column=" + column.getName());
+                if (!column.isAllowNull()) {
+                    throw new AnalysisException("Column has no default value, 
column=" + column.getName());
+                }
             }
             if (column.getDefaultValueExpr() != null) {
                 Expression defualtValueExpression = new 
NereidsParser().parseExpression(


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

Reply via email to