This is an automated email from the ASF dual-hosted git repository.

JingsongLi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/paimon-rust.git


The following commit(s) were added to refs/heads/main by this push:
     new bd6fb200 fix(datafusion): reject unsupported row selection in UPDATE 
and DELETE. (#1499)
bd6fb200 is described below

commit bd6fb20087f9aa87d44fe93d376b6782f5a0723e
Author: slfan1989 <[email protected]>
AuthorDate: Mon Nov 23 14:14:20 2026 +0800

    fix(datafusion): reject unsupported row selection in UPDATE and DELETE. 
(#1499)
---
 crates/integrations/datafusion/src/delete.rs       | 64 +++++++++++++++-------
 crates/integrations/datafusion/src/update.rs       | 35 ++++++++++--
 .../integrations/datafusion/tests/delete_tests.rs  | 62 +++++++++++++++++++++
 .../datafusion/tests/pk_delete_tests.rs            | 43 ++++++++++-----
 .../datafusion/tests/pk_update_tests.rs            |  4 +-
 .../integrations/datafusion/tests/update_tests.rs  | 54 ++++++++++++++++++
 6 files changed, 220 insertions(+), 42 deletions(-)

diff --git a/crates/integrations/datafusion/src/delete.rs 
b/crates/integrations/datafusion/src/delete.rs
index 49037501..a5cbec88 100644
--- a/crates/integrations/datafusion/src/delete.rs
+++ b/crates/integrations/datafusion/src/delete.rs
@@ -49,13 +49,43 @@ pub(crate) async fn execute_delete(
     let tables = match &delete.from {
         FromTable::WithFromKeyword(t) | FromTable::WithoutKeyword(t) => t,
     };
-    if let Some(first) = tables.first() {
-        if let TableFactor::Table { alias: Some(a), .. } = &first.relation {
-            return Err(DataFusionError::Plan(format!(
-                "Table alias '{}' in DELETE is not yet supported",
-                a.name.value
-            )));
-        }
+    let [target] = tables.as_slice() else {
+        return Err(DataFusionError::NotImplemented(
+            "DELETE requires a single target table".into(),
+        ));
+    };
+    if !delete.tables.is_empty() {
+        return Err(DataFusionError::NotImplemented(
+            "DELETE with an explicit target table list is not 
supported".into(),
+        ));
+    }
+    if delete.using.is_some() {
+        return Err(DataFusionError::NotImplemented(
+            "DELETE ... USING is not supported".into(),
+        ));
+    }
+    if !target.joins.is_empty() {
+        return Err(DataFusionError::NotImplemented(
+            "DELETE ... JOIN is not supported".into(),
+        ));
+    }
+    if let TableFactor::Table { alias: Some(a), .. } = &target.relation {
+        return Err(DataFusionError::Plan(format!(
+            "Table alias '{}' in DELETE is not yet supported",
+            a.name.value
+        )));
+    }
+    if matches!(&target.relation, TableFactor::Table { args: Some(_), .. }) {
+        return Err(DataFusionError::NotImplemented(
+            "DELETE on table functions is not supported".into(),
+        ));
+    }
+    if matches!(&target.relation, TableFactor::Table { sample, partitions, .. }
+        if sample.is_some() || !partitions.is_empty())
+    {
+        return Err(DataFusionError::NotImplemented(
+            "DELETE with TABLESAMPLE or PARTITION is not supported".into(),
+        ));
     }
 
     let schema = table.schema();
@@ -65,7 +95,7 @@ pub(crate) async fn execute_delete(
         return execute_data_evolution_delete(ctx, delete, &table, 
table_ref).await;
     }
     if !schema.trimmed_primary_keys().is_empty() {
-        return execute_primary_key_delete(ctx, delete, &table, 
table_ref).await;
+        return execute_primary_key_delete(ctx, delete, &target.relation, 
&table, table_ref).await;
     }
 
     execute_cow_delete(ctx, delete, &table, table_ref).await
@@ -74,6 +104,7 @@ pub(crate) async fn execute_delete(
 async fn execute_primary_key_delete(
     ctx: &SQLContext,
     delete: &Delete,
+    target: &TableFactor,
     table: &Table,
     table_ref: &str,
 ) -> DFResult<DataFrame> {
@@ -83,22 +114,15 @@ async fn execute_primary_key_delete(
             "DELETE on primary-key tables requires ignore-delete=false".into(),
         ));
     }
-    let from = match &delete.from {
-        FromTable::WithFromKeyword(tables) | FromTable::WithoutKeyword(tables) 
=> tables,
-    };
-    if from.len() != 1
-        || !from[0].joins.is_empty()
-        || !delete.tables.is_empty()
-        || delete.using.is_some()
-        || delete.returning.is_some()
+    if delete.returning.is_some()
         || delete.output.is_some()
         || !delete.order_by.is_empty()
         || delete.limit.is_some()
-        || !matches!(&from[0].relation,
+        || !matches!(target,
             TableFactor::Table {
-                args: None, version: None, with_ordinality: false,
-                json_path: None, sample: None, with_hints, partitions, 
index_hints, ..
-            } if with_hints.is_empty() && partitions.is_empty() && 
index_hints.is_empty())
+                version: None, with_ordinality: false,
+                json_path: None, with_hints, index_hints, ..
+            } if with_hints.is_empty() && index_hints.is_empty())
     {
         return Err(DataFusionError::NotImplemented(
             "Primary-key DELETE supports a single target with an optional 
WHERE clause".into(),
diff --git a/crates/integrations/datafusion/src/update.rs 
b/crates/integrations/datafusion/src/update.rs
index b5fdc6dc..4f8c0aee 100644
--- a/crates/integrations/datafusion/src/update.rs
+++ b/crates/integrations/datafusion/src/update.rs
@@ -59,6 +59,31 @@ pub(crate) async fn execute_update(
             a.name.value
         )));
     }
+    if update.from.is_some() {
+        return Err(DataFusionError::NotImplemented(
+            "UPDATE ... FROM is not supported".into(),
+        ));
+    }
+    if !update.table.joins.is_empty() {
+        return Err(DataFusionError::NotImplemented(
+            "UPDATE ... JOIN is not supported".into(),
+        ));
+    }
+    if matches!(
+        &update.table.relation,
+        TableFactor::Table { args: Some(_), .. }
+    ) {
+        return Err(DataFusionError::NotImplemented(
+            "UPDATE on table functions is not supported".into(),
+        ));
+    }
+    if matches!(&update.table.relation, TableFactor::Table { sample, 
partitions, .. }
+        if sample.is_some() || !partitions.is_empty())
+    {
+        return Err(DataFusionError::NotImplemented(
+            "UPDATE with TABLESAMPLE or PARTITION is not supported".into(),
+        ));
+    }
 
     let schema = table.schema();
     let core_options = CoreOptions::new(schema.options());
@@ -79,9 +104,7 @@ async fn execute_primary_key_update(
     enable_ident_normalization: bool,
 ) -> DFResult<DataFrame> {
     primary_key_dml::validate_table(table, "UPDATE")?;
-    if !update.table.joins.is_empty()
-        || update.from.is_some()
-        || update.returning.is_some()
+    if update.returning.is_some()
         || update.output.is_some()
         || update.or.is_some()
         || !update.optimizer_hints.is_empty()
@@ -89,9 +112,9 @@ async fn execute_primary_key_update(
         || update.limit.is_some()
         || !matches!(&update.table.relation,
             TableFactor::Table {
-                args: None, version: None, with_ordinality: false,
-                json_path: None, sample: None, with_hints, partitions, 
index_hints, ..
-            } if with_hints.is_empty() && partitions.is_empty() && 
index_hints.is_empty())
+                version: None, with_ordinality: false,
+                json_path: None, with_hints, index_hints, ..
+            } if with_hints.is_empty() && index_hints.is_empty())
     {
         return Err(DataFusionError::NotImplemented(
             "Primary-key UPDATE supports a single target, SET assignments and 
an optional WHERE clause".into(),
diff --git a/crates/integrations/datafusion/tests/delete_tests.rs 
b/crates/integrations/datafusion/tests/delete_tests.rs
index 7e0a3b74..b403a4b8 100644
--- a/crates/integrations/datafusion/tests/delete_tests.rs
+++ b/crates/integrations/datafusion/tests/delete_tests.rs
@@ -85,6 +85,68 @@ async fn query_pt(sql_context: &SQLContext) -> Vec<(i32, 
String, i32)> {
 
 // ======================= Basic DELETE =======================
 
+#[tokio::test]
+async fn test_delete_rejects_unsupported_row_selection_before_writing() {
+    for options in [
+        "",
+        "WITH (
+            'row-tracking.enabled' = 'true',
+            'data-evolution.enabled' = 'true',
+            'deletion-vectors.enabled' = 'true'
+        )",
+    ] {
+        let (_tmp, catalog) = create_test_env();
+        let sql_context = create_sql_context(catalog).await;
+        exec(&sql_context, "CREATE SCHEMA paimon.test_db").await;
+        exec(
+            &sql_context,
+            &format!(
+                "CREATE TABLE paimon.test_db.t (id INT, name VARCHAR, pt INT) 
PARTITIONED BY (pt) {options}"
+            ),
+        )
+        .await;
+        exec(
+            &sql_context,
+            "INSERT INTO paimon.test_db.t (id, name, pt) VALUES (1, 'a', 1), 
(2, 'b', 2)",
+        )
+        .await;
+        exec(
+            &sql_context,
+            "CREATE TABLE paimon.test_db.src (source_id INT)",
+        )
+        .await;
+        let before = query_pt(&sql_context).await;
+
+        for (suffix, error) in [
+            (
+                "USING paimon.test_db.src",
+                "DELETE ... USING is not supported",
+            ),
+            (
+                "JOIN paimon.test_db.src ON t.id = src.source_id",
+                "DELETE ... JOIN is not supported",
+            ),
+            (
+                ", paimon.test_db.src",
+                "DELETE requires a single target table",
+            ),
+            ("()", "DELETE on table functions is not supported"),
+            (
+                "TABLESAMPLE BERNOULLI (0)",
+                "DELETE with TABLESAMPLE or PARTITION is not supported",
+            ),
+            (
+                "PARTITION (p0)",
+                "DELETE with TABLESAMPLE or PARTITION is not supported",
+            ),
+        ] {
+            let sql = format!("DELETE FROM paimon.test_db.t {suffix}");
+            assert_sql_error(&sql_context, &sql, error).await;
+            assert_eq!(query_pt(&sql_context).await, before, "{sql}");
+        }
+    }
+}
+
 #[tokio::test]
 async fn test_delete_by_non_partition_column() {
     let (_tmp, sql_context) = setup().await;
diff --git a/crates/integrations/datafusion/tests/pk_delete_tests.rs 
b/crates/integrations/datafusion/tests/pk_delete_tests.rs
index 42733223..327bfeef 100644
--- a/crates/integrations/datafusion/tests/pk_delete_tests.rs
+++ b/crates/integrations/datafusion/tests/pk_delete_tests.rs
@@ -564,24 +564,39 @@ async fn primary_key_delete_rejects_extended_syntax() {
     let (_tmp, catalog, ctx) = setup("").await;
     exec(&ctx, "INSERT INTO paimon.test_db.t VALUES (1, 10)").await;
     let before = snapshot(&catalog).await;
-    for suffix in [
-        "RETURNING id",
-        "USING paimon.test_db.t other WHERE t.id = other.id",
-        "ORDER BY id LIMIT 1",
-        "LIMIT 1",
-        "JOIN paimon.test_db.t other ON t.id = other.id",
-        ", paimon.test_db.t",
-        "TABLESAMPLE BERNOULLI (50)",
-        "OUTPUT deleted.id",
-        "()",
-        "WITH (NOLOCK)",
-        "WITH ORDINALITY",
-        "PARTITION (p0)",
+    for (suffix, error) in [
+        ("RETURNING id", "optional WHERE"),
+        (
+            "USING paimon.test_db.t other WHERE t.id = other.id",
+            "DELETE ... USING is not supported",
+        ),
+        ("ORDER BY id LIMIT 1", "optional WHERE"),
+        ("LIMIT 1", "optional WHERE"),
+        (
+            "JOIN paimon.test_db.t other ON t.id = other.id",
+            "DELETE ... JOIN is not supported",
+        ),
+        (
+            ", paimon.test_db.t",
+            "DELETE requires a single target table",
+        ),
+        (
+            "TABLESAMPLE BERNOULLI (50)",
+            "DELETE with TABLESAMPLE or PARTITION is not supported",
+        ),
+        ("OUTPUT deleted.id", "optional WHERE"),
+        ("()", "DELETE on table functions is not supported"),
+        ("WITH (NOLOCK)", "optional WHERE"),
+        ("WITH ORDINALITY", "optional WHERE"),
+        (
+            "PARTITION (p0)",
+            "DELETE with TABLESAMPLE or PARTITION is not supported",
+        ),
     ] {
         assert_sql_error(
             &ctx,
             &format!("DELETE FROM paimon.test_db.t {suffix}"),
-            "optional WHERE",
+            error,
         )
         .await;
         assert_eq!(snapshot(&catalog).await, before, "{suffix}");
diff --git a/crates/integrations/datafusion/tests/pk_update_tests.rs 
b/crates/integrations/datafusion/tests/pk_update_tests.rs
index 8856d9b9..12de57d3 100644
--- a/crates/integrations/datafusion/tests/pk_update_tests.rs
+++ b/crates/integrations/datafusion/tests/pk_update_tests.rs
@@ -382,11 +382,11 @@ async fn 
primary_key_update_rejects_unsupported_targets_and_syntax() {
         ),
         (
             "UPDATE paimon.test_db.t SET v = 1 FROM paimon.test_db.t other 
WHERE t.id = other.id",
-            "optional WHERE",
+            "UPDATE ... FROM is not supported",
         ),
         (
             "UPDATE paimon.test_db.t JOIN paimon.test_db.t other ON t.id = 
other.id SET v = 1",
-            "optional WHERE",
+            "UPDATE ... JOIN is not supported",
         ),
         (
             "UPDATE paimon.test_db.t SET v = 1 OUTPUT inserted.id",
diff --git a/crates/integrations/datafusion/tests/update_tests.rs 
b/crates/integrations/datafusion/tests/update_tests.rs
index a06e2aed..b25bc579 100644
--- a/crates/integrations/datafusion/tests/update_tests.rs
+++ b/crates/integrations/datafusion/tests/update_tests.rs
@@ -85,6 +85,60 @@ async fn query_pt(sql_context: &SQLContext) -> Vec<(i32, 
String, i32)> {
 
 // ======================= Basic UPDATE =======================
 
+#[tokio::test]
+async fn test_update_rejects_unsupported_row_selection_before_writing() {
+    for options in [
+        "",
+        "WITH ('row-tracking.enabled' = 'true', 'data-evolution.enabled' = 
'true')",
+    ] {
+        let (_tmp, catalog) = create_test_env();
+        let sql_context = create_sql_context(catalog).await;
+        exec(&sql_context, "CREATE SCHEMA paimon.test_db").await;
+        exec(
+            &sql_context,
+            &format!("CREATE TABLE paimon.test_db.t (id INT, name VARCHAR, age 
INT) {options}"),
+        )
+        .await;
+        exec(
+            &sql_context,
+            "INSERT INTO paimon.test_db.t (id, name, age) VALUES (1, 'a', 10), 
(2, 'b', 20)",
+        )
+        .await;
+        exec(
+            &sql_context,
+            "CREATE TABLE paimon.test_db.src (source_id INT)",
+        )
+        .await;
+        let before = query(&sql_context).await;
+
+        for (sql, error) in [
+            (
+                "UPDATE paimon.test_db.t SET age = 99 FROM (SELECT 1 WHERE 
FALSE) AS empty_source",
+                "UPDATE ... FROM is not supported",
+            ),
+            (
+                "UPDATE paimon.test_db.t SET age = 99 FROM (SELECT 1 AS 
source_id) AS source WHERE id = 1",
+                "UPDATE ... FROM is not supported",
+            ),
+            (
+                "UPDATE paimon.test_db.t JOIN paimon.test_db.src ON t.id = 
src.source_id SET age = 99",
+                "UPDATE ... JOIN is not supported",
+            ),
+            (
+                "UPDATE paimon.test_db.t TABLESAMPLE BERNOULLI (0) SET age = 
99",
+                "UPDATE with TABLESAMPLE or PARTITION is not supported",
+            ),
+            (
+                "UPDATE paimon.test_db.t() SET age = 99",
+                "UPDATE on table functions is not supported",
+            ),
+        ] {
+            assert_sql_error(&sql_context, sql, error).await;
+            assert_eq!(query(&sql_context).await, before, "{sql}");
+        }
+    }
+}
+
 #[tokio::test]
 async fn test_update_with_where() {
     let (_tmp, sql_context) = setup().await;

Reply via email to