Skip to content

Commit c47399c

Browse files
authored
[fix](Nereids) fix insert into table with null literal default value (apache#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
1 parent 4910a5a commit c47399c

File tree

6 files changed

+71
-9
lines changed

6 files changed

+71
-9
lines changed

fe/fe-core/src/main/java/org/apache/doris/analysis/NativeInsertStmt.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -923,11 +923,15 @@ private void analyzeRow(Analyzer analyzer, List<Column> targetColumns, List<Arra
923923
Column col = targetColumns.get(i);
924924

925925
if (expr instanceof DefaultValueExpr) {
926-
if (targetColumns.get(i).getDefaultValue() == null) {
926+
if (targetColumns.get(i).getDefaultValue() == null && !targetColumns.get(i).isAllowNull()) {
927927
throw new AnalysisException("Column has no default value, column="
928928
+ targetColumns.get(i).getName());
929929
}
930-
expr = new StringLiteral(targetColumns.get(i).getDefaultValue());
930+
if (targetColumns.get(i).getDefaultValue() == null) {
931+
expr = new NullLiteral();
932+
} else {
933+
expr = new StringLiteral(targetColumns.get(i).getDefaultValue());
934+
}
931935
}
932936
if (expr instanceof Subquery) {
933937
throw new AnalysisException("Insert values can not be query");

fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java

+3
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,9 @@ public String getDefaultValue() {
518518
}
519519

520520
public Expr getDefaultValueExpr() throws AnalysisException {
521+
if (defaultValue == null) {
522+
return null;
523+
}
521524
StringLiteral defaultValueLiteral = new StringLiteral(defaultValue);
522525
if (getDataType() == PrimitiveType.VARCHAR) {
523526
return defaultValueLiteral;

fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ private static Map<String, NamedExpression> getColumnToOutput(
337337
} else if (column.getDefaultValue() == null) {
338338
// throw exception if explicitly use Default value but no default value present
339339
// insert into table t values(DEFAULT)
340-
if (columnToChildOutput.get(column) instanceof DefaultValueSlot) {
340+
if (columnToChildOutput.get(column) instanceof DefaultValueSlot && !column.isAllowNull()) {
341341
throw new AnalysisException("Column has no default value,"
342342
+ " column=" + column.getName());
343343
}

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,9 @@ private static NamedExpression generateDefaultExpression(Column column) {
427427
return new Alias(new NullLiteral(DataType.fromCatalogType(column.getType())), column.getName());
428428
}
429429
if (column.getDefaultValue() == null) {
430-
throw new AnalysisException("Column has no default value, column=" + column.getName());
430+
if (!column.isAllowNull()) {
431+
throw new AnalysisException("Column has no default value, column=" + column.getName());
432+
}
431433
}
432434
if (column.getDefaultValueExpr() != null) {
433435
Expression defualtValueExpression = new NereidsParser().parseExpression(
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
-- This file is automatically generated. You should know what you did if you want to edit this
22
-- !select1 --
3-
10 10000 10000000 92233720368547758 19223372036854775807 10.3 10.3
4-
10 10000 10000000 92233720368547758 19223372036854775807 10.3 10.3
3+
10 10000 10000000 92233720368547758 19223372036854775807 10.30 10.3
4+
10 10000 10000000 92233720368547758 19223372036854775807 10.30 10.3
55

66
-- !select2 --
7-
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
8-
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
7+
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
8+
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
9+
10+
-- !select3 --
11+
1 2 test 0 0 0 \N 0.0 0 0 0 \N \N
12+
13+
-- !select4 --
14+
1 2 test 0 0 0 \N 0.0 0 0 0 \N \N
15+

regression-test/suites/load_p0/insert/test_insert_default_value.groovy

+47-1
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,50 @@ suite("test_insert_default_value") {
8282
qt_select2 """ select k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11 from test_insert_dft_tbl """
8383

8484
sql "drop table test_insert_dft_tbl"
85-
}
85+
86+
sql "drop table if exists test_insert_default_null"
87+
sql """
88+
CREATE TABLE `test_insert_default_null` (
89+
`gz_organization_id` int(11) DEFAULT '1',
90+
`company_id` int(11) NOT NULL,
91+
`material_id` varchar(120) NOT NULL COMMENT '素材id',
92+
`material_info_type` varchar(40) DEFAULT '',
93+
`signature` varchar(260) DEFAULT '' COMMENT 'md5',
94+
`size` int(11) DEFAULT '0' COMMENT '大小',
95+
`width` int(11) DEFAULT '0' COMMENT '宽',
96+
`height` int(11) DEFAULT '0' COMMENT '高',
97+
`format` varchar(80) DEFAULT '' COMMENT '格式',
98+
`upload_time` datetime DEFAULT NULL COMMENT '上传时间',
99+
`filename` varchar(500) DEFAULT '' COMMENT '名字',
100+
`duration` decimal(10,1) DEFAULT '0' COMMENT '视频时长',
101+
`producer_name` varchar(200) DEFAULT '',
102+
`producer_id` int(11) DEFAULT '0',
103+
`producer_department_path` varchar(100) DEFAULT '',
104+
`producer_special_id` int(11) DEFAULT '0',
105+
`producer_node_id` int(11) DEFAULT '0',
106+
`update_time` datetime DEFAULT null,
107+
`create_time` datetime DEFAULT null,
108+
INDEX idx_filename(filename) USING INVERTED PROPERTIES("parser" = "chinese"),
109+
) ENGINE=OLAP
110+
UNIQUE KEY(`gz_organization_id`, `company_id`, `material_id`)
111+
DISTRIBUTED BY HASH(`material_id`) BUCKETS 3
112+
PROPERTIES (
113+
"store_row_column" = "true",
114+
"enable_unique_key_merge_on_write" = "true",
115+
"replication_num" = "1"
116+
);
117+
"""
118+
119+
sql """ set enable_nereids_planner=true """
120+
sql """ set enable_nereids_dml=true """
121+
sql """ INSERT INTO `test_insert_default_null` (gz_organization_id, `company_id`, `material_id`, create_time) VALUES ('1', '2', 'test', DEFAULT); """
122+
qt_select3 """ select * from test_insert_default_null;"""
123+
sql """ truncate table test_insert_default_null;"""
124+
125+
sql """ set enable_nereids_planner=false """
126+
sql """ set enable_nereids_dml=false """
127+
sql """ INSERT INTO `test_insert_default_null` (gz_organization_id, `company_id`, `material_id`, create_time) VALUES ('1', '2', 'test', DEFAULT); """
128+
129+
qt_select4 """ select * from test_insert_default_null;"""
130+
sql "drop table if exists test_insert_default_null"
131+
}

0 commit comments

Comments
 (0)