From 160d83bfa1bc08b2ea4249ce19f5e51a2dd84b8f Mon Sep 17 00:00:00 2001 From: LiBinfeng <46676950+LiBinfeng-01@users.noreply.github.com> Date: Mon, 12 Aug 2024 16:32:44 +0800 Subject: [PATCH] [fix](Nereids) fix insert into table with null literal default value (#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 --- .../java/org/apache/doris/analysis/NativeInsertStmt.java | 8 ++++++-- .../src/main/java/org/apache/doris/catalog/Column.java | 3 +++ .../org/apache/doris/nereids/rules/analysis/BindSink.java | 6 ++++++ .../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 f95efcd3474c9f..b9a745dad20746 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 @@ private void analyzeRow(Analyzer analyzer, List targetColumns, List buildRules() { // 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 264891804ff85a..5d47127ea99863 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 void testScalarSubQuery() { @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;