This is an automated email from the ASF dual-hosted git repository.
morrysnow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new c47399cc0d0 [fix](Nereids) fix insert into table with null literal
default value (#39122)
c47399cc0d0 is described below
commit c47399cc0d0dbd0e97ed31810b8c777577a72f96
Author: LiBinfeng <[email protected]>
AuthorDate: Mon Aug 12 16:32:44 2024 +0800
[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
---
.../apache/doris/analysis/NativeInsertStmt.java | 8 +++-
.../main/java/org/apache/doris/catalog/Column.java | 3 ++
.../doris/nereids/rules/analysis/BindSink.java | 2 +-
.../trees/plans/commands/insert/InsertUtils.java | 4 +-
.../load_p0/insert/test_insert_default_value.out | 15 +++++--
.../insert/test_insert_default_value.groovy | 48 +++++++++++++++++++++-
6 files changed, 71 insertions(+), 9 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 ae3c2371e73..64ab872ea8b 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
@@ -923,11 +923,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 86cbf203a53..d7965470e64 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
@@ -518,6 +518,9 @@ public class Column implements 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 d21490aa76d..f7ff692cdbe 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
@@ -337,7 +337,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 67374254c8a..03ca58e99d1 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
@@ -427,7 +427,9 @@ public class InsertUtils {
return new Alias(new
NullLiteral(DataType.fromCatalogType(column.getType())), column.getName());
}
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(
diff --git a/regression-test/data/load_p0/insert/test_insert_default_value.out
b/regression-test/data/load_p0/insert/test_insert_default_value.out
index 7fc34c82fda..c63e8496bfa 100644
--- a/regression-test/data/load_p0/insert/test_insert_default_value.out
+++ b/regression-test/data/load_p0/insert/test_insert_default_value.out
@@ -1,8 +1,15 @@
-- This file is automatically generated. You should know what you did if you
want to edit this
-- !select1 --
-10 10000 10000000 92233720368547758 19223372036854775807
10.3 10.3
-10 10000 10000000 92233720368547758 19223372036854775807
10.3 10.3
+10 10000 10000000 92233720368547758 19223372036854775807
10.30 10.3
+10 10000 10000000 92233720368547758 19223372036854775807
10.30 10.3
-- !select2 --
-true 10 10000 10000000 92233720368547758
19223372036854775807 3.14159 hello world, today is 15/06/2023
2023-06-15 2023-06-15T16:10:15 10.3
-true 10 10000 10000000 92233720368547758
19223372036854775807 3.14159 hello world, today is 15/06/2023
2023-06-15 2023-06-15T16:10:15 10.3
+true 10 10000 10000000 92233720368547758
19223372036854775807 3.14159 hello world, today is 15/06/2023
2023-06-15 2023-06-15T16:10:15 10.30
+true 10 10000 10000000 92233720368547758
19223372036854775807 3.14159 hello world, today is 15/06/2023
2023-06-15 2023-06-15T16:10:15 10.30
+
+-- !select3 --
+1 2 test 0 0 0 \N
0.0 0 0 0 \N \N
+
+-- !select4 --
+1 2 test 0 0 0 \N
0.0 0 0 0 \N \N
+
diff --git
a/regression-test/suites/load_p0/insert/test_insert_default_value.groovy
b/regression-test/suites/load_p0/insert/test_insert_default_value.groovy
index 1e894196e28..38b512370b4 100644
--- a/regression-test/suites/load_p0/insert/test_insert_default_value.groovy
+++ b/regression-test/suites/load_p0/insert/test_insert_default_value.groovy
@@ -82,4 +82,50 @@ suite("test_insert_default_value") {
qt_select2 """ select k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11 from
test_insert_dft_tbl """
sql "drop table test_insert_dft_tbl"
-}
\ No newline at end of file
+
+ sql "drop table if exists test_insert_default_null"
+ sql """
+ CREATE TABLE `test_insert_default_null` (
+ `gz_organization_id` int(11) DEFAULT '1',
+ `company_id` int(11) NOT NULL,
+ `material_id` varchar(120) NOT NULL COMMENT '素材id',
+ `material_info_type` varchar(40) DEFAULT '',
+ `signature` varchar(260) DEFAULT '' COMMENT 'md5',
+ `size` int(11) DEFAULT '0' COMMENT '大小',
+ `width` int(11) DEFAULT '0' COMMENT '宽',
+ `height` int(11) DEFAULT '0' COMMENT '高',
+ `format` varchar(80) DEFAULT '' COMMENT '格式',
+ `upload_time` datetime DEFAULT NULL COMMENT '上传时间',
+ `filename` varchar(500) DEFAULT '' COMMENT '名字',
+ `duration` decimal(10,1) DEFAULT '0' COMMENT '视频时长',
+ `producer_name` varchar(200) DEFAULT '',
+ `producer_id` int(11) DEFAULT '0',
+ `producer_department_path` varchar(100) DEFAULT '',
+ `producer_special_id` int(11) DEFAULT '0',
+ `producer_node_id` int(11) DEFAULT '0',
+ `update_time` datetime DEFAULT null,
+ `create_time` datetime DEFAULT null,
+ INDEX idx_filename(filename) USING INVERTED PROPERTIES("parser" =
"chinese"),
+ ) ENGINE=OLAP
+ UNIQUE KEY(`gz_organization_id`, `company_id`, `material_id`)
+ DISTRIBUTED BY HASH(`material_id`) BUCKETS 3
+ PROPERTIES (
+ "store_row_column" = "true",
+ "enable_unique_key_merge_on_write" = "true",
+ "replication_num" = "1"
+ );
+ """
+
+ sql """ set enable_nereids_planner=true """
+ sql """ set enable_nereids_dml=true """
+ sql """ INSERT INTO `test_insert_default_null` (gz_organization_id,
`company_id`, `material_id`, create_time) VALUES ('1', '2', 'test', DEFAULT);
"""
+ qt_select3 """ select * from test_insert_default_null;"""
+ sql """ truncate table test_insert_default_null;"""
+
+ sql """ set enable_nereids_planner=false """
+ sql """ set enable_nereids_dml=false """
+ sql """ INSERT INTO `test_insert_default_null` (gz_organization_id,
`company_id`, `material_id`, create_time) VALUES ('1', '2', 'test', DEFAULT);
"""
+
+ qt_select4 """ select * from test_insert_default_null;"""
+ sql "drop table if exists test_insert_default_null"
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]