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 a236831b feat(datafusion): support WHEN MATCHED AND in data evolution 
MERGE (#1376)
a236831b is described below

commit a236831bdcf363bd5ca97b13c26a7a3168a97be1
Author: QuakeWang <[email protected]>
AuthorDate: Mon Nov 16 13:25:13 2026 +0800

    feat(datafusion): support WHEN MATCHED AND in data evolution MERGE (#1376)
---
 crates/integrations/datafusion/src/merge_into.rs   |  5 --
 .../datafusion/tests/merge_into_tests.rs           | 88 +++++++++++++++++++++-
 .../integrations/datafusion/tests/native_merge.rs  | 38 ++++------
 docs/src/sql.md                                    | 18 +++--
 4 files changed, 111 insertions(+), 38 deletions(-)

diff --git a/crates/integrations/datafusion/src/merge_into.rs 
b/crates/integrations/datafusion/src/merge_into.rs
index 79c13952..8ad794f1 100644
--- a/crates/integrations/datafusion/src/merge_into.rs
+++ b/crates/integrations/datafusion/src/merge_into.rs
@@ -199,11 +199,6 @@ pub(crate) fn plan_merge(
                         "Multiple WHEN MATCHED clauses are not yet 
supported".into(),
                     ));
                 }
-                if de && clause.predicate.is_some() {
-                    return Err(DataFusionError::NotImplemented(
-                        "WHEN MATCHED AND <predicate> is not yet 
supported".into(),
-                    ));
-                }
                 matched_count += 1;
             }
             _ => {}
diff --git a/crates/integrations/datafusion/tests/merge_into_tests.rs 
b/crates/integrations/datafusion/tests/merge_into_tests.rs
index 1a79cb3b..3280f59c 100644
--- a/crates/integrations/datafusion/tests/merge_into_tests.rs
+++ b/crates/integrations/datafusion/tests/merge_into_tests.rs
@@ -737,6 +737,92 @@ async fn test_merge_into_update_and_insert() {
     );
 }
 
+#[tokio::test]
+async fn test_conditional_matched_update_and_insert() {
+    let (_tmp, sql_context) = setup_conditional_merge().await;
+
+    assert_eq!(
+        common::dml_count(
+            &sql_context,
+            "MERGE INTO paimon.test_db.target t USING 
paimon.test_db.src_conditional s ON t.id = s.id \
+             WHEN MATCHED AND s.version > t.value THEN UPDATE SET name = 
s.name \
+             WHEN NOT MATCHED THEN INSERT (id, name, value) VALUES (s.id, 
s.name, 40)",
+        )
+        .await,
+        2,
+    );
+    assert_eq!(
+        collect_rows_3col(
+            &sql_context,
+            "SELECT id, name, value FROM paimon.test_db.target ORDER BY id",
+        )
+        .await,
+        vec![
+            (1, "ALICE".to_string(), 10),
+            (2, "bob".to_string(), 20),
+            (3, "charlie".to_string(), 30),
+            (4, "dave".to_string(), 40),
+            (5, "eve".to_string(), 50),
+        ],
+    );
+}
+
+#[tokio::test]
+async fn test_conditional_matched_delete_and_insert() {
+    let (_tmp, sql_context) = setup_conditional_merge().await;
+    common::exec(
+        &sql_context,
+        "ALTER TABLE paimon.test_db.target SET 
TBLPROPERTIES('deletion-vectors.enabled' = 'true')",
+    )
+    .await;
+
+    assert_eq!(
+        common::dml_count(
+            &sql_context,
+            "MERGE INTO paimon.test_db.target t USING 
paimon.test_db.src_conditional s ON t.id = s.id \
+             WHEN MATCHED AND s.version > t.value THEN DELETE \
+             WHEN NOT MATCHED THEN INSERT (id, name, value) VALUES (s.id, 
s.name, 40)",
+        )
+        .await,
+        2,
+    );
+    assert_eq!(
+        collect_rows_3col(
+            &sql_context,
+            "SELECT id, name, value FROM paimon.test_db.target ORDER BY id",
+        )
+        .await,
+        vec![
+            (2, "bob".to_string(), 20),
+            (3, "charlie".to_string(), 30),
+            (4, "dave".to_string(), 40),
+            (5, "eve".to_string(), 50),
+        ],
+    );
+}
+
+async fn setup_conditional_merge() -> (TempDir, SQLContext) {
+    let (tmp, catalog) = create_test_env();
+    let sql_context = create_sql_context(catalog).await;
+    setup_data_evolution_table(&sql_context).await;
+    common::exec(
+        &sql_context,
+        "INSERT INTO paimon.test_db.target VALUES \
+         (1, 'alice', 10), (2, 'bob', 20), (3, 'charlie', 30), (5, 'eve', 50)",
+    )
+    .await;
+    enable_data_evolution(&sql_context).await;
+    // The first three rows match ON and produce TRUE, FALSE, and NULL, 
respectively.
+    register_source(
+        &sql_context,
+        "CREATE TEMPORARY TABLE paimon.test_db.src_conditional AS SELECT * 
FROM \
+         (VALUES (1, 'ALICE', 11), (2, 'BOB', 19), (3, 'CHARLIE', NULL), (4, 
'dave', 41)) \
+         AS s(id, name, version)",
+    )
+    .await;
+    (tmp, sql_context)
+}
+
 #[tokio::test]
 async fn test_merge_into_insert_only() {
     let (_tmp, catalog) = create_test_env();
@@ -1103,7 +1189,7 @@ async fn test_rejects_multiple_when_matched() {
         "MERGE INTO paimon.test_db.target t USING paimon.test_db.src_multi s 
ON t.id = s.id \
          WHEN MATCHED AND t.id = 1 THEN UPDATE SET name = s.name \
          WHEN MATCHED THEN UPDATE SET name = 'default'",
-        "WHEN MATCHED AND <predicate> is not yet supported",
+        "Multiple WHEN MATCHED clauses are not yet supported",
     )
     .await;
 }
diff --git a/crates/integrations/datafusion/tests/native_merge.rs 
b/crates/integrations/datafusion/tests/native_merge.rs
index bdd0ac4b..bb816ceb 100644
--- a/crates/integrations/datafusion/tests/native_merge.rs
+++ b/crates/integrations/datafusion/tests/native_merge.rs
@@ -368,20 +368,12 @@ async fn 
native_merge_retains_session_variable_providers() {
             "INSERT INTO paimon.test_db.target (id, value) VALUES (1, 10)",
         )
         .await;
-        // Data-evolution tables only support unconditional matched clauses.
-        let matched_predicate = if options.is_empty() {
-            " AND @delta = 7"
-        } else {
-            ""
-        };
         assert_eq!(
             common::dml_count(
                 &ctx,
-                &format!(
-                    "MERGE INTO paimon.test_db.target t USING (VALUES (1)) AS 
s(id) \
+                "MERGE INTO paimon.test_db.target t USING (VALUES (1)) AS 
s(id) \
              ON t.id = s.id AND @@flag = 7 \
-             WHEN MATCHED{matched_predicate} THEN UPDATE SET value = @delta"
-                )
+             WHEN MATCHED AND @delta = 7 THEN UPDATE SET value = @delta"
             )
             .await,
             1
@@ -401,21 +393,19 @@ async fn 
native_merge_retains_session_variable_providers() {
             common::collect_id_value(&ctx, "SELECT id, value FROM 
paimon.test_db.target").await,
             vec![(1, 7), (2, 7)]
         );
-        if options.is_empty() {
-            assert_eq!(
-                common::dml_count(
-                    &ctx,
-                    "MERGE INTO paimon.test_db.target t USING (VALUES (1)) AS 
s(id) \
+        assert_eq!(
+            common::dml_count(
+                &ctx,
+                "MERGE INTO paimon.test_db.target t USING (VALUES (1)) AS 
s(id) \
              ON t.id = s.id WHEN MATCHED AND @delta <> 7 THEN UPDATE SET value 
= 0"
-                )
-                .await,
-                0
-            );
-            assert_eq!(
-                common::collect_id_value(&ctx, "SELECT id, value FROM 
paimon.test_db.target").await,
-                vec![(1, 7), (2, 7)]
-            );
-        }
+            )
+            .await,
+            0
+        );
+        assert_eq!(
+            common::collect_id_value(&ctx, "SELECT id, value FROM 
paimon.test_db.target").await,
+            vec![(1, 7), (2, 7)]
+        );
     }
 }
 
diff --git a/docs/src/sql.md b/docs/src/sql.md
index 713dd370..6fc491f4 100644
--- a/docs/src/sql.md
+++ b/docs/src/sql.md
@@ -1056,14 +1056,16 @@ are published in one commit. Multiple source rows 
selecting conflicting actions
 for the same target row fail without publishing any actions. A sole 
unconditional
 matched DELETE collapses duplicate matches and counts each deleted target once.
 
-Append-only tables support multiple conditional matched clauses with the first
-true clause taking effect; NULL predicates fall through. Data-evolution tables
-retain the restriction of one unconditional matched clause. Both support
-conditional not-matched inserts. `NOT MATCHED BY SOURCE` and subqueries inside
-`ON` or action expressions are not supported by the Paimon executor. Native
-planner restrictions also apply, including `UPDATE SET *`, `INSERT *`, 
`OUTPUT`,
-and action-level `WHERE` predicates. After declaring a table alias, use that
-alias in column references.
+Append-only tables support multiple matched clauses and execute the first
+applicable action. Data-evolution tables support a single matched `UPDATE` or
+`DELETE` with an optional predicate. A clause predicate must evaluate to `TRUE`
+for its action to apply. If no matched action applies, the target row is
+preserved. Both table types support conditional `WHEN NOT MATCHED` inserts,
+which apply only to source rows without an `ON` match. The Paimon executor
+does not support `NOT MATCHED BY SOURCE` or subqueries inside `ON` or action
+expressions. Native planner restrictions also apply, including `UPDATE SET *`,
+`INSERT *`, `OUTPUT`, and action-level `WHERE` predicates. After declaring a
+table alias, use that alias in column references.
 
 `SQLContext` expands positional MERGE inserts to persisted column names for
 data-evolution tables, excluding the virtual `_ROW_ID`. When using a raw

Reply via email to