From f698f205d5a6a0701f0bf27200579f33e5f2003c Mon Sep 17 00:00:00 2001 From: bobhan1 Date: Mon, 16 Oct 2023 14:05:06 +0800 Subject: [PATCH] [Fix](merge-on-write) throw exception when the user don't specify the insert columns in insert statement for partial update (#25437) --- .../java/org/apache/doris/analysis/NativeInsertStmt.java | 5 +++++ .../org/apache/doris/nereids/rules/analysis/BindSink.java | 6 ++++++ .../nereids_p0/insert_into_table/partial_update.groovy | 4 ++++ .../insert_into_table/partial_update_complex.groovy | 7 ++++++- .../test_partial_update_native_insert_stmt.groovy | 4 ++++ .../test_partial_update_native_insert_stmt_complex.groovy | 7 ++++++- 6 files changed, 31 insertions(+), 2 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 72035f8237..d357c2129c 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 @@ -507,6 +507,11 @@ public class NativeInsertStmt extends InsertStmt { Set mentionedColumns = Sets.newTreeSet(String.CASE_INSENSITIVE_ORDER); List realTargetColumnNames; if (targetColumnNames == null) { + if (!isFromDeleteOrUpdateStmt + && analyzer.getContext().getSessionVariable().isEnableUniqueKeyPartialUpdate()) { + throw new AnalysisException("You must explicitly specify the columns to be updated when " + + "updating partial columns using the INSERT statement."); + } // the mentioned columns are columns which are visible to user, so here we use // getBaseSchema(), not getFullSchema() for (Column col : targetTable.getBaseSchema(false)) { 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 f5998c3620..374620db27 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 @@ -81,6 +81,12 @@ public class BindSink implements AnalysisRuleFactory { boolean isNeedSequenceCol = child.getOutput().stream() .anyMatch(slot -> slot.getName().equals(Column.SEQUENCE_COL)); + if (sink.getColNames().isEmpty() && sink.isFromNativeInsertStmt() + && sink.isPartialUpdate()) { + throw new AnalysisException("You must explicitly specify the columns to be updated when " + + "updating partial columns using the INSERT statement."); + } + LogicalOlapTableSink boundSink = new LogicalOlapTableSink<>( database, table, diff --git a/regression-test/suites/nereids_p0/insert_into_table/partial_update.groovy b/regression-test/suites/nereids_p0/insert_into_table/partial_update.groovy index 697bc21880..ed4cd1a194 100644 --- a/regression-test/suites/nereids_p0/insert_into_table/partial_update.groovy +++ b/regression-test/suites/nereids_p0/insert_into_table/partial_update.groovy @@ -43,6 +43,10 @@ suite("nereids_partial_update_native_insert_stmt", "p0") { // existing rows should be updated and new rows should be inserted with unmentioned columns filled with default or null value sql """insert into ${tableName}(id,score) values(2,400),(1,200),(4,400)""" qt_1 """ select * from ${tableName} order by id; """ + test { + sql """insert into ${tableName} values(2,400),(1,200),(4,400)""" + exception "You must explicitly specify the columns to be updated when updating partial columns using the INSERT statement." + } sql "set enable_unique_key_partial_update=false;" sql "sync;" sql """ DROP TABLE IF EXISTS ${tableName} """ diff --git a/regression-test/suites/nereids_p0/insert_into_table/partial_update_complex.groovy b/regression-test/suites/nereids_p0/insert_into_table/partial_update_complex.groovy index 66945fed05..088aa7ffd0 100644 --- a/regression-test/suites/nereids_p0/insert_into_table/partial_update_complex.groovy +++ b/regression-test/suites/nereids_p0/insert_into_table/partial_update_complex.groovy @@ -77,7 +77,12 @@ suite("nereids_partial_update_native_insert_stmt_complex", "p0") { from ${tbName2} inner join ${tbName3} on ${tbName2}.id = ${tbName3}.id; """ qt_complex_update """select * from ${tbName1} order by id;""" - + test { + sql """insert into ${tbName1} + select ${tbName2}.id, ${tbName2}.c1, ${tbName2}.c3 * 100 + from ${tbName2} inner join ${tbName3} on ${tbName2}.id = ${tbName3}.id; """ + exception "You must explicitly specify the columns to be updated when updating partial columns using the INSERT statement." + } sql "truncate table ${tbName1};" sql "truncate table ${tbName2};" sql "truncate table ${tbName3};" diff --git a/regression-test/suites/unique_with_mow_p0/partial_update/test_partial_update_native_insert_stmt.groovy b/regression-test/suites/unique_with_mow_p0/partial_update/test_partial_update_native_insert_stmt.groovy index ce9b425ff9..37f0890699 100644 --- a/regression-test/suites/unique_with_mow_p0/partial_update/test_partial_update_native_insert_stmt.groovy +++ b/regression-test/suites/unique_with_mow_p0/partial_update/test_partial_update_native_insert_stmt.groovy @@ -44,6 +44,10 @@ suite("test_partial_update_native_insert_stmt", "p0") { // existing rows should be updated and new rows should be inserted with unmentioned columns filled with default or null value sql """insert into ${tableName}(id,score) values(2,400),(1,200),(4,400)""" qt_1 """ select * from ${tableName} order by id; """ + test { + sql """insert into ${tableName} values(2,400),(1,200),(4,400)""" + exception "You must explicitly specify the columns to be updated when updating partial columns using the INSERT statement." + } sql "set enable_unique_key_partial_update=false;" sql "sync;" sql """ DROP TABLE IF EXISTS ${tableName} """ diff --git a/regression-test/suites/unique_with_mow_p0/partial_update/test_partial_update_native_insert_stmt_complex.groovy b/regression-test/suites/unique_with_mow_p0/partial_update/test_partial_update_native_insert_stmt_complex.groovy index bdb3a12dc5..731156b78d 100644 --- a/regression-test/suites/unique_with_mow_p0/partial_update/test_partial_update_native_insert_stmt_complex.groovy +++ b/regression-test/suites/unique_with_mow_p0/partial_update/test_partial_update_native_insert_stmt_complex.groovy @@ -77,7 +77,12 @@ suite("test_partial_update_native_insert_stmt_complex", "p0") { from ${tbName2} inner join ${tbName3} on ${tbName2}.id = ${tbName3}.id; """ qt_complex_update """select * from ${tbName1} order by id;""" - + test { + sql """insert into ${tbName1} + select ${tbName2}.id, ${tbName2}.c1, ${tbName2}.c3 * 100 + from ${tbName2} inner join ${tbName3} on ${tbName2}.id = ${tbName3}.id; """ + exception "You must explicitly specify the columns to be updated when updating partial columns using the INSERT statement." + } sql "truncate table ${tbName1};" sql "truncate table ${tbName2};" sql "truncate table ${tbName3};"