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 72035f823773f43..7abaa5e2f11e694 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 @@ private void analyzeSubquery(Analyzer analyzer, boolean skipCheck) throws UserEx Set mentionedColumns = Sets.newTreeSet(String.CASE_INSENSITIVE_ORDER); List realTargetColumnNames; if (targetColumnNames == null) { + if (!isFromDeleteOrUpdateStmt + && analyzer.getContext().getSessionVariable().isEnableUniqueKeyPartialUpdate()) { + throw new AnalysisException("using native insert statement to do " + + "partial update should specify the insert columns."); + } // 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 f5998c36202ee0c..40656865fdbc864 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 List buildRules() { boolean isNeedSequenceCol = child.getOutput().stream() .anyMatch(slot -> slot.getName().equals(Column.SEQUENCE_COL)); + if (sink.getColNames().isEmpty() && sink.isFromNativeInsertStmt() + && sink.isPartialUpdate()) { + throw new AnalysisException("using native insert statement to do " + + "partial update should specify the insert columns."); + } + 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 697bc21880929f7..b854ebbf1653515 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 "using native insert statement to do partial update should specify the insert columns." + } 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 66945fed053f387..422ef591ca9ba88 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 "using native insert statement to do partial update should specify the insert columns." + } 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 ce9b425ff9db62f..74b379a4e09cf90 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 "using native insert statement to do partial update should specify the insert columns." + } 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 bdb3a12dc56192d..51d80f3139836d7 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}(id, c1, c3) + select ${tbName2}.id, ${tbName2}.c1, ${tbName2}.c3 * 100 + from ${tbName2} inner join ${tbName3} on ${tbName2}.id = ${tbName3}.id; """ + exception "using native insert statement to do partial update should specify the insert columns." + } sql "truncate table ${tbName1};" sql "truncate table ${tbName2};" sql "truncate table ${tbName3};"