From 5101b1d1792906d51f0fac7d0a6e71f751727926 Mon Sep 17 00:00:00 2001 From: LiBinfeng <1204975323@qq.com> Date: Wed, 7 Aug 2024 19:34:57 +0800 Subject: [PATCH] fix insert into table with null literal default value --- .../doris/analysis/NativeInsertStmt.java | 8 +++- .../java/org/apache/doris/catalog/Column.java | 3 ++ .../nereids/rules/analysis/BindSink.java | 2 +- .../plans/commands/insert/InsertUtils.java | 4 +- .../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 ae3c2371e73c01..64ab872ea8b906 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 @@ private void analyzeRow(Analyzer analyzer, List targetColumns, List getColumnToOutput( } 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 67374254c8a6f2..03ca58e99d103a 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 @@ private static NamedExpression generateDefaultExpression(Column column) { 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 7fc34c82fda0ca..c63e8496bfab36 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 1e894196e2898e..38b512370b4207 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" +}