Skip to content

Commit

Permalink
[fix](Nereids) fix insert into table with null literal default value (a…
Browse files Browse the repository at this point in the history
…pache#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
  • Loading branch information
LiBinfeng-01 authored Aug 12, 2024
1 parent 4910a5a commit c47399c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -923,11 +923,15 @@ private void analyzeRow(Analyzer analyzer, List<Column> targetColumns, List<Arra
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");
Expand Down
3 changes: 3 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,9 @@ public String getDefaultValue() {
}

public Expr getDefaultValueExpr() throws AnalysisException {
if (defaultValue == null) {
return null;
}
StringLiteral defaultValueLiteral = new StringLiteral(defaultValue);
if (getDataType() == PrimitiveType.VARCHAR) {
return defaultValueLiteral;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ private static Map<String, NamedExpression> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 11 additions & 4 deletions regression-test/data/load_p0/insert/test_insert_default_value.out
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

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"
}

0 comments on commit c47399c

Please sign in to comment.