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 dc1fefdbff45a8..d478192f1b430e 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 @@ -284,6 +284,7 @@ public static Plan normalizePlan(Plan plan, TableIf table) { ((UnboundTableSink) unboundLogicalSink).setPartialUpdate(false); } else { boolean hasMissingColExceptAutoIncKey = false; + boolean hasMissingAutoIncKey = false; for (Column col : olapTable.getFullSchema()) { Optional insertCol = unboundLogicalSink.getColNames().stream() .filter(c -> c.equalsIgnoreCase(col.getName())).findFirst(); @@ -294,9 +295,18 @@ public static Plan normalizePlan(Plan plan, TableIf table) { if (!(col.isAutoInc() && col.isKey()) && !insertCol.isPresent() && col.isVisible()) { hasMissingColExceptAutoIncKey = true; } + if (col.isAutoInc() && col.isKey() && !insertCol.isPresent()) { + hasMissingAutoIncKey = true; + } } if (!hasMissingColExceptAutoIncKey) { ((UnboundTableSink) unboundLogicalSink).setPartialUpdate(false); + } else { + if (hasMissingAutoIncKey) { + // becuase of the uniqueness of genetaed value of auto-increment column, + // we convert this load to upsert when is misses auto-increment key column + ((UnboundTableSink) unboundLogicalSink).setPartialUpdate(false); + } } } } diff --git a/regression-test/data/unique_with_mow_p0/partial_update/test_partial_update_auto_inc.out b/regression-test/data/unique_with_mow_p0/partial_update/test_partial_update_auto_inc.out index d157f501a8b4b7..1bf8ea6b148636 100644 --- a/regression-test/data/unique_with_mow_p0/partial_update/test_partial_update_auto_inc.out +++ b/regression-test/data/unique_with_mow_p0/partial_update/test_partial_update_auto_inc.out @@ -35,6 +35,7 @@ doris9 3 888 888 30 4 40 40 40 +<<<<<<< HEAD -- !select_1 -- doris1 doris2 @@ -70,4 +71,13 @@ doris9 2 888 888 20 3 888 888 30 4 40 40 40 +======= +-- !sql -- +test1 15 +test2 29 +test3 49 + +-- !sql -- +3 +>>>>>>> cd0cd55f11 ([opt](auto-inc) Allow to miss auto-increment column and other value columns in partial update (#44528)) diff --git a/regression-test/suites/unique_with_mow_p0/partial_update/test_partial_update_auto_inc.groovy b/regression-test/suites/unique_with_mow_p0/partial_update/test_partial_update_auto_inc.groovy index ec46939b2f5855..13d6fd269069d9 100644 --- a/regression-test/suites/unique_with_mow_p0/partial_update/test_partial_update_auto_inc.groovy +++ b/regression-test/suites/unique_with_mow_p0/partial_update/test_partial_update_auto_inc.groovy @@ -113,6 +113,47 @@ suite("test_partial_update_auto_inc") { } order_qt_select_6 "select * from test_primary_key_partial_update_auto_inc2" sql """ DROP TABLE IF EXISTS test_primary_key_partial_update_auto_inc2 """ + + sql """ DROP TABLE IF EXISTS test_primary_key_partial_update_auto_inc3 force; """ + sql """ create table test_primary_key_partial_update_auto_inc3 + ( + `id` bigint not null AUTO_INCREMENT, + `project_code` varchar(20) not null, + `period_num` int, + `c2` int + ) unique KEY(`id`) + DISTRIBUTED BY HASH(`id`) BUCKETS auto + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1", + "enable_unique_key_merge_on_write" = "true" + ); """ + sql "set enable_unique_key_partial_update=true;" + sql "set enable_insert_strict=false;" + sql "sync;" + + sql "insert into test_primary_key_partial_update_auto_inc3(project_code,period_num) values ('test1',15),('test2',29),('test3',49);" + qt_sql "select project_code,period_num from test_primary_key_partial_update_auto_inc3 order by project_code,period_num;" + qt_sql "select count(distinct id) from test_primary_key_partial_update_auto_inc3;" + + + sql """ DROP TABLE IF EXISTS test_primary_key_partial_update_auto_inc4 """ + sql """ CREATE TABLE test_primary_key_partial_update_auto_inc4 ( + `k1` BIGINT NOT NULL AUTO_INCREMENT, + `k2` int, + `c1` int, + `c2` int, + `c3` int) + UNIQUE KEY(`k1`,`k2`) DISTRIBUTED BY HASH(`k1`,`k2`) BUCKETS 1 + PROPERTIES("replication_num" = "1", "enable_unique_key_merge_on_write" = "true"); """ + sql "set enable_unique_key_partial_update=true;" + sql "set enable_insert_strict=false;" + sql "sync;" + + test { + sql "insert into test_primary_key_partial_update_auto_inc4(c1,c2) values(1,1),(2,2),(3,3)" + exception "Partial update should include all key columns, missing: k2" + } + } } }