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

lzljs3620320 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new 3cefe74fe [flink] integrate DELETE filter pushdown for flink-1.17 
(#1434)
3cefe74fe is described below

commit 3cefe74fe7fb6533d7233417dde20990798f1fdb
Author: legendtkl <[email protected]>
AuthorDate: Mon Jul 3 13:26:04 2023 +0800

    [flink] integrate DELETE filter pushdown for flink-1.17 (#1434)
---
 .../DeletePushDownPartitionKeyVisitor.java         |  99 ++++++++
 .../predicate/DeletePushDownPrimaryKeyVisitor.java | 114 +++++++++
 .../predicate/DeletePushDownVisitorTest.java       | 260 +++++++++++++++++++++
 .../java/org/apache/paimon/table/TableUtils.java   |   7 +-
 .../apache/paimon/flink/sink/FlinkTableSink.java   |  94 +++++++-
 .../apache/paimon/flink/ReadWriteTableITCase.java  | 207 ++++++++++++++++
 6 files changed, 777 insertions(+), 4 deletions(-)

diff --git 
a/paimon-common/src/main/java/org/apache/paimon/predicate/DeletePushDownPartitionKeyVisitor.java
 
b/paimon-common/src/main/java/org/apache/paimon/predicate/DeletePushDownPartitionKeyVisitor.java
new file mode 100644
index 000000000..ca77d72a1
--- /dev/null
+++ 
b/paimon-common/src/main/java/org/apache/paimon/predicate/DeletePushDownPartitionKeyVisitor.java
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.paimon.predicate;
+
+import java.util.List;
+
+/**
+ * DeletePushDownPartitionKeyVisitor visit the predicate and check if it only 
contains partition
+ * keys and can be push down.
+ */
+public class DeletePushDownPartitionKeyVisitor implements 
FunctionVisitor<Boolean> {
+
+    private final List<String> partitionKeys;
+
+    public DeletePushDownPartitionKeyVisitor(List<String> partitionKeys) {
+        this.partitionKeys = partitionKeys;
+    }
+
+    @Override
+    public Boolean visitIsNotNull(FieldRef fieldRef) {
+        return false;
+    }
+
+    @Override
+    public Boolean visitIsNull(FieldRef fieldRef) {
+        return false;
+    }
+
+    @Override
+    public Boolean visitStartsWith(FieldRef fieldRef, Object literal) {
+        return false;
+    }
+
+    @Override
+    public Boolean visitLessThan(FieldRef fieldRef, Object literal) {
+        return false;
+    }
+
+    @Override
+    public Boolean visitGreaterOrEqual(FieldRef fieldRef, Object literal) {
+        return false;
+    }
+
+    @Override
+    public Boolean visitNotEqual(FieldRef fieldRef, Object literal) {
+        return false;
+    }
+
+    @Override
+    public Boolean visitLessOrEqual(FieldRef fieldRef, Object literal) {
+        return false;
+    }
+
+    @Override
+    public Boolean visitEqual(FieldRef fieldRef, Object literal) {
+        return partitionKeys.contains(fieldRef.name());
+    }
+
+    @Override
+    public Boolean visitGreaterThan(FieldRef fieldRef, Object literal) {
+        return false;
+    }
+
+    @Override
+    public Boolean visitIn(FieldRef fieldRef, List<Object> literals) {
+        return false;
+    }
+
+    @Override
+    public Boolean visitNotIn(FieldRef fieldRef, List<Object> literals) {
+        return false;
+    }
+
+    @Override
+    public Boolean visitAnd(List<Boolean> children) {
+        return children.stream().reduce((first, second) -> first && 
second).get();
+    }
+
+    @Override
+    public Boolean visitOr(List<Boolean> children) {
+        return false;
+    }
+}
diff --git 
a/paimon-common/src/main/java/org/apache/paimon/predicate/DeletePushDownPrimaryKeyVisitor.java
 
b/paimon-common/src/main/java/org/apache/paimon/predicate/DeletePushDownPrimaryKeyVisitor.java
new file mode 100644
index 000000000..dcf4cc75c
--- /dev/null
+++ 
b/paimon-common/src/main/java/org/apache/paimon/predicate/DeletePushDownPrimaryKeyVisitor.java
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.paimon.predicate;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * DeletePushDownPrimaryKeyVisitor visit the predicate and check if it only 
contains primary keys
+ * and can be push down.
+ *
+ * <p>We will check: 1. all the primary keys are in the predicate with equal 
operator. 2. if step1
+ * is satisfied, the other fields in the predicate could be ignored (e.g. 
return true).
+ */
+public class DeletePushDownPrimaryKeyVisitor implements 
FunctionVisitor<Boolean> {
+
+    private final List<String> primaryKeys;
+
+    private Map<String, Boolean> hit;
+
+    public DeletePushDownPrimaryKeyVisitor(List<String> primaryKeys) {
+        this.primaryKeys = primaryKeys;
+        this.hit = new HashMap<>();
+    }
+
+    public boolean isHitAll() {
+        return hit.size() == primaryKeys.size();
+    }
+
+    @Override
+    public Boolean visitIsNotNull(FieldRef fieldRef) {
+        return !primaryKeys.contains(fieldRef.name());
+    }
+
+    @Override
+    public Boolean visitIsNull(FieldRef fieldRef) {
+        return !primaryKeys.contains(fieldRef.name());
+    }
+
+    @Override
+    public Boolean visitStartsWith(FieldRef fieldRef, Object literal) {
+        return !primaryKeys.contains(fieldRef.name());
+    }
+
+    @Override
+    public Boolean visitLessThan(FieldRef fieldRef, Object literal) {
+        return !primaryKeys.contains(fieldRef.name());
+    }
+
+    @Override
+    public Boolean visitGreaterOrEqual(FieldRef fieldRef, Object literal) {
+        return !primaryKeys.contains(fieldRef.name());
+    }
+
+    @Override
+    public Boolean visitNotEqual(FieldRef fieldRef, Object literal) {
+        return !primaryKeys.contains(fieldRef.name());
+    }
+
+    @Override
+    public Boolean visitLessOrEqual(FieldRef fieldRef, Object literal) {
+        return !primaryKeys.contains(fieldRef.name());
+    }
+
+    @Override
+    public Boolean visitEqual(FieldRef fieldRef, Object literal) {
+        if (primaryKeys.contains(fieldRef.name())) {
+            hit.put(fieldRef.name(), true);
+        }
+        return true;
+    }
+
+    @Override
+    public Boolean visitGreaterThan(FieldRef fieldRef, Object literal) {
+        return !primaryKeys.contains(fieldRef.name());
+    }
+
+    @Override
+    public Boolean visitIn(FieldRef fieldRef, List<Object> literals) {
+        return !primaryKeys.contains(fieldRef.name());
+    }
+
+    @Override
+    public Boolean visitNotIn(FieldRef fieldRef, List<Object> literals) {
+        return !primaryKeys.contains(fieldRef.name());
+    }
+
+    @Override
+    public Boolean visitAnd(List<Boolean> children) {
+        return children.stream().reduce((x, y) -> x && y).get();
+    }
+
+    @Override
+    public Boolean visitOr(List<Boolean> children) {
+        return children.stream().reduce((x, y) -> x || y).get();
+    }
+}
diff --git 
a/paimon-common/src/test/java/org/apache/paimon/predicate/DeletePushDownVisitorTest.java
 
b/paimon-common/src/test/java/org/apache/paimon/predicate/DeletePushDownVisitorTest.java
new file mode 100644
index 000000000..623f2c3bc
--- /dev/null
+++ 
b/paimon-common/src/test/java/org/apache/paimon/predicate/DeletePushDownVisitorTest.java
@@ -0,0 +1,260 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.paimon.predicate;
+
+import org.apache.paimon.types.DataTypes;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/* test cases for DeleteFilter Push Down, suppose primaryKeys = (a,b,c,d), 
partitionKeys=(b,c,d), other fields = (e,f),
+ * the following cases are tested:
+ *
+ * 1. where a=1 and b=2 and c=3 and d=4, push down
+ * 2. where a=1 and b=2 and c=3 and d=4 and e is not null, push down
+ * 3. where a=1 and b=2 and c=3 and d=4 and f=6, push down
+ * 4. where a=1 and b=2 and c=3 and d=4 and e is not null and f=6, push down
+ * 5. where a in (1,2) and b=2 and c=3 and d=4, push down
+ * 6. where a=1 and b=1 and c is not null and d=4, do not push down
+ * 7. where a=1, do not push down
+ * 8. where a=1 and b=2 and d=4, do not push down
+ * 9. where a=1 and c=3 and d=4, do not push down
+ * 10. where b=2 and c=3 and d=4 and f=6, not push down
+ *
+ * 11. where b=2 and c=3 and d=4, push down
+ * 12. where b=2 and c=3, push down
+ * 13. where b=2 and d=4, push down
+ * 14. where b=2 and c=3 and d=4 and e=5, do not push down
+ * 15. where b=2 and c=3 or d=4, do not push down
+ * 16. where b=2 and c=3 and d>5, do not push down
+ */
+/** DeletePushDownVisitorTest tests the DeletePushDownVisitor. */
+public class DeletePushDownVisitorTest {
+
+    @Test
+    public void testPrimaryKeyPushDown() {
+        List<String> primaryKeys = Arrays.asList("a", "b", "c", "d");
+
+        Predicate predicateA =
+                new LeafPredicate(
+                        Equal.INSTANCE, DataTypes.INT(), 1, "a", 
Collections.singletonList(1));
+
+        Predicate predicateA1 =
+                new LeafPredicate(
+                        Equal.INSTANCE, DataTypes.INT(), 1, "a", 
Collections.singletonList(2));
+
+        Predicate predicateB =
+                new LeafPredicate(
+                        Equal.INSTANCE, DataTypes.INT(), 2, "b", 
Collections.singletonList(2));
+
+        Predicate predicateC =
+                new LeafPredicate(
+                        Equal.INSTANCE, DataTypes.INT(), 3, "c", 
Collections.singletonList(3));
+
+        Predicate predicateCIsNotNull =
+                new LeafPredicate(
+                        IsNotNull.INSTANCE, DataTypes.INT(), 3, "c", 
Collections.singletonList(3));
+
+        Predicate predicateD =
+                new LeafPredicate(
+                        Equal.INSTANCE, DataTypes.INT(), 4, "d", 
Collections.singletonList(4));
+
+        // non-primary key's isNotNull filter
+        Predicate predicateE =
+                new LeafPredicate(
+                        IsNotNull.INSTANCE, DataTypes.INT(), 5, "e", 
Collections.singletonList(5));
+
+        Predicate predicateF =
+                new LeafPredicate(
+                        IsNotNull.INSTANCE, DataTypes.INT(), 6, "f", 
Collections.singletonList(6));
+
+        /** filters contain all the primary keys with AND of Equal *********** 
*/
+
+        // where a=1 and b=2 and c=3 and d=4, push down
+        DeletePushDownPrimaryKeyVisitor visitor = new 
DeletePushDownPrimaryKeyVisitor(primaryKeys);
+        Predicate compoundPredicate =
+                PredicateBuilder.and(Arrays.asList(predicateA, predicateB, 
predicateC, predicateD));
+        Boolean visitResult = compoundPredicate.visit(visitor);
+        Boolean hit = visitor.isHitAll();
+        assertThat(visitResult).isTrue();
+        assertThat(hit).isTrue();
+
+        // where a=1 and b=2 and c=3 and d=4 and e is not null, push down
+        DeletePushDownPrimaryKeyVisitor visitor1 = new 
DeletePushDownPrimaryKeyVisitor(primaryKeys);
+        Predicate predicateABCDE =
+                PredicateBuilder.and(
+                        Arrays.asList(predicateA, predicateB, predicateC, 
predicateD, predicateE));
+        assertThat(predicateABCDE.visit(visitor1)).isTrue();
+        assertThat(visitor1.isHitAll()).isTrue();
+
+        // where a=1 and b=2 and c=3 and d=4 and f=6, push down
+        DeletePushDownPrimaryKeyVisitor visitor2 = new 
DeletePushDownPrimaryKeyVisitor(primaryKeys);
+        Predicate predicateABCDF =
+                PredicateBuilder.and(
+                        Arrays.asList(predicateA, predicateB, predicateC, 
predicateD, predicateF));
+        assertThat(predicateABCDF.visit(visitor2)).isTrue();
+        assertThat(visitor2.isHitAll()).isTrue();
+
+        // where a=1 and b=2 and c=3 and d=4 and e is not null and f=6, push 
down
+        DeletePushDownPrimaryKeyVisitor visitor3 = new 
DeletePushDownPrimaryKeyVisitor(primaryKeys);
+        Predicate predicateABCDEF =
+                PredicateBuilder.and(
+                        Arrays.asList(
+                                predicateA,
+                                predicateB,
+                                predicateC,
+                                predicateD,
+                                predicateE,
+                                predicateF));
+        assertThat(predicateABCDEF.visit(visitor3)).isTrue();
+        assertThat(visitor3.isHitAll()).isTrue();
+
+        // where a in (1,2) and b=2 and c=3 and d=4, push down
+        DeletePushDownPrimaryKeyVisitor visitor4 = new 
DeletePushDownPrimaryKeyVisitor(primaryKeys);
+        Predicate predicateAABCD =
+                PredicateBuilder.and(
+                        Arrays.asList(
+                                PredicateBuilder.or(predicateA, predicateA1),
+                                predicateB,
+                                predicateC,
+                                predicateD,
+                                predicateE));
+        assertThat(predicateAABCD.visit(visitor4)).isTrue();
+        assertThat(visitor4.isHitAll()).isTrue();
+
+        /** not all the primary keys filters are of Equal func ************* */
+
+        // where a=1 and b=1 and c is not null and d=4, do not push down
+        DeletePushDownPrimaryKeyVisitor visitor5 = new 
DeletePushDownPrimaryKeyVisitor(primaryKeys);
+        Predicate predicateABCNotNull =
+                PredicateBuilder.and(
+                        Arrays.asList(predicateA, predicateB, 
predicateCIsNotNull, predicateD));
+        assertThat(predicateABCNotNull.visit(visitor5)).isFalse();
+        assertThat(visitor5.isHitAll()).isFalse();
+
+        /** filters not contain all the primary keys ****************** */
+
+        // where a=1, do not push down
+        DeletePushDownPrimaryKeyVisitor visitor6 = new 
DeletePushDownPrimaryKeyVisitor(primaryKeys);
+        PredicateBuilder.and(Arrays.asList(predicateA)).visit(visitor6);
+        assertThat(visitor6.isHitAll()).isFalse();
+
+        // where a=1 and b=2 and d=4, do not push down
+        DeletePushDownPrimaryKeyVisitor visitor7 = new 
DeletePushDownPrimaryKeyVisitor(primaryKeys);
+        PredicateBuilder.and(Arrays.asList(predicateA, predicateB, 
predicateD)).visit(visitor7);
+        assertThat(visitor7.isHitAll()).isFalse();
+
+        // where a=1 and c=3 and d=4, do not push down
+        DeletePushDownPrimaryKeyVisitor visitor8 = new 
DeletePushDownPrimaryKeyVisitor(primaryKeys);
+        PredicateBuilder.and(Arrays.asList(predicateA, predicateC, 
predicateD)).visit(visitor8);
+        assertThat(visitor8.isHitAll()).isFalse();
+
+        // where b=2 and c=3 and d=4 and f=6, not push down
+        DeletePushDownPrimaryKeyVisitor visitor9 = new 
DeletePushDownPrimaryKeyVisitor(primaryKeys);
+        PredicateBuilder.and(Arrays.asList(predicateB, predicateC, predicateD, 
predicateF))
+                .visit(visitor9);
+        assertThat(visitor9.isHitAll()).isFalse();
+    }
+
+    @Test
+    public void testPartitionKeyNotPushDown() {
+        List<String> partitionKeys = Arrays.asList("b", "c", "d");
+
+        Predicate predicateB =
+                new LeafPredicate(
+                        Equal.INSTANCE, DataTypes.INT(), 2, "b", 
Collections.singletonList(2));
+
+        Predicate predicateC =
+                new LeafPredicate(
+                        Equal.INSTANCE, DataTypes.INT(), 3, "c", 
Collections.singletonList(3));
+
+        Predicate predicateD =
+                new LeafPredicate(
+                        Equal.INSTANCE, DataTypes.INT(), 4, "d", 
Collections.singletonList(4));
+
+        Predicate predicateDgreater =
+                new LeafPredicate(
+                        GreaterThan.INSTANCE,
+                        DataTypes.INT(),
+                        4,
+                        "d",
+                        Collections.singletonList(5));
+
+        Predicate predicateE =
+                new LeafPredicate(
+                        Equal.INSTANCE, DataTypes.INT(), 5, "e", 
Collections.singletonList(5));
+
+        /** filters only contain partition keys with func Equal 
****************** */
+
+        // where b=2 and c=3 and d=4, push down
+        DeletePushDownPartitionKeyVisitor visitor =
+                new DeletePushDownPartitionKeyVisitor(partitionKeys);
+        assertThat(
+                        PredicateBuilder.and(Arrays.asList(predicateB, 
predicateC, predicateD))
+                                .visit(visitor))
+                .isTrue();
+
+        // where b=2 and c=3, push down
+        DeletePushDownPartitionKeyVisitor visitor1 =
+                new DeletePushDownPartitionKeyVisitor(partitionKeys);
+        assertThat(PredicateBuilder.and(Arrays.asList(predicateB, 
predicateC)).visit(visitor1))
+                .isTrue();
+
+        // where b=2 and d=4, push down
+        DeletePushDownPartitionKeyVisitor visitor2 =
+                new DeletePushDownPartitionKeyVisitor(partitionKeys);
+        assertThat(PredicateBuilder.and(Arrays.asList(predicateB, 
predicateD)).visit(visitor2))
+                .isTrue();
+
+        // where b=2 and c=3 and d=4 and e=5, do not push down
+        DeletePushDownPartitionKeyVisitor visitor3 =
+                new DeletePushDownPartitionKeyVisitor(partitionKeys);
+        assertThat(
+                        PredicateBuilder.and(
+                                        Arrays.asList(
+                                                predicateB, predicateC, 
predicateD, predicateE))
+                                .visit(visitor3))
+                .isFalse();
+
+        // where b=2 and c=3 or d=4, do not push down
+        DeletePushDownPartitionKeyVisitor visitor4 =
+                new DeletePushDownPartitionKeyVisitor(partitionKeys);
+        assertThat(
+                        PredicateBuilder.and(
+                                        Arrays.asList(
+                                                
PredicateBuilder.or(predicateB, predicateD),
+                                                
PredicateBuilder.or(predicateC, predicateD)))
+                                .visit(visitor4))
+                .isFalse();
+
+        // where b=2 and c=3 and d>5, do not push down
+        DeletePushDownPartitionKeyVisitor visitor5 =
+                new DeletePushDownPartitionKeyVisitor(partitionKeys);
+        assertThat(
+                        PredicateBuilder.and(
+                                        Arrays.asList(predicateB, predicateC, 
predicateDgreater))
+                                .visit(visitor5))
+                .isFalse();
+    }
+}
diff --git a/paimon-core/src/main/java/org/apache/paimon/table/TableUtils.java 
b/paimon-core/src/main/java/org/apache/paimon/table/TableUtils.java
index d6deee22f..00bf5541d 100644
--- a/paimon-core/src/main/java/org/apache/paimon/table/TableUtils.java
+++ b/paimon-core/src/main/java/org/apache/paimon/table/TableUtils.java
@@ -39,11 +39,14 @@ public class TableUtils {
      * Delete according to filters.
      *
      * <p>NOTE: This method is only suitable for deletion of small amount of 
data.
+     *
+     * @return the number of deleted records
      */
-    public static void deleteWhere(Table table, List<Predicate> filters) {
+    public static long deleteWhere(Table table, List<Predicate> filters) {
         ReadBuilder readBuilder = table.newReadBuilder().withFilter(filters);
         BatchWriteBuilder writeBuilder = table.newBatchWriteBuilder();
         List<Split> splits = readBuilder.newScan().plan().splits();
+        long hit = 0;
         try (RecordReader<InternalRow> reader = 
readBuilder.newRead().createReader(splits);
                 BatchTableWrite write = writeBuilder.newWrite();
                 BatchTableCommit commit = writeBuilder.newCommit()) {
@@ -52,12 +55,14 @@ public class TableUtils {
             while (iterator.hasNext()) {
                 InternalRow row = iterator.next();
                 if (filter.test(row)) {
+                    hit++;
                     row.setRowKind(RowKind.DELETE);
                     write.write(row);
                 }
             }
 
             commit.commit(write.prepareCommit());
+            return hit;
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
diff --git 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/FlinkTableSink.java
 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/FlinkTableSink.java
index c497c4470..c2afaf0d3 100644
--- 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/FlinkTableSink.java
+++ 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/FlinkTableSink.java
@@ -19,32 +19,58 @@
 package org.apache.paimon.flink.sink;
 
 import org.apache.paimon.CoreOptions.MergeEngine;
+import org.apache.paimon.flink.LogicalTypeConversion;
+import org.apache.paimon.flink.PredicateConverter;
 import org.apache.paimon.flink.log.LogStoreTableFactory;
+import org.apache.paimon.operation.FileStoreCommit;
 import org.apache.paimon.options.Options;
+import org.apache.paimon.predicate.DeletePushDownPartitionKeyVisitor;
+import org.apache.paimon.predicate.DeletePushDownPrimaryKeyVisitor;
+import org.apache.paimon.predicate.LeafPredicate;
+import org.apache.paimon.predicate.Predicate;
+import org.apache.paimon.predicate.PredicateBuilder;
+import org.apache.paimon.table.AbstractFileStoreTable;
 import org.apache.paimon.table.AppendOnlyFileStoreTable;
 import org.apache.paimon.table.ChangelogValueCountFileStoreTable;
 import org.apache.paimon.table.ChangelogWithKeyFileStoreTable;
 import org.apache.paimon.table.Table;
+import org.apache.paimon.table.TableUtils;
+import org.apache.paimon.table.sink.BatchWriteBuilder;
 
 import org.apache.flink.table.catalog.Column;
 import org.apache.flink.table.catalog.ObjectIdentifier;
 import org.apache.flink.table.connector.RowLevelModificationScanContext;
+import org.apache.flink.table.connector.sink.abilities.SupportsDeletePushDown;
 import org.apache.flink.table.connector.sink.abilities.SupportsRowLevelDelete;
 import org.apache.flink.table.connector.sink.abilities.SupportsRowLevelUpdate;
+import org.apache.flink.table.expressions.ResolvedExpression;
 import org.apache.flink.table.factories.DynamicTableFactory;
+import org.apache.flink.table.types.logical.RowType;
 
 import javax.annotation.Nullable;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
+import java.util.UUID;
 import java.util.stream.Collectors;
 
 import static org.apache.paimon.CoreOptions.MERGE_ENGINE;
 
 /** Table sink to create sink. */
 public class FlinkTableSink extends FlinkTableSinkBase
-        implements SupportsRowLevelUpdate, SupportsRowLevelDelete {
+        implements SupportsRowLevelUpdate, SupportsRowLevelDelete, 
SupportsDeletePushDown {
+
+    @Nullable protected List<Predicate> predicates;
+    @Nullable protected Predicate predicate;
+    private boolean isDeleteFilterPartitionKey = false;
+    private boolean isDeleteFilterPrimaryKey = false;
+
+    private static final String TABLE_PATH_KEY = "path";
 
     public FlinkTableSink(
             ObjectIdentifier tableIdentifier,
@@ -112,17 +138,70 @@ public class FlinkTableSink extends FlinkTableSinkBase
     @Override
     public RowLevelDeleteInfo applyRowLevelDelete(
             @Nullable RowLevelModificationScanContext 
rowLevelModificationScanContext) {
+        checkDeletable();
+        return new RowLevelDeleteInfo() {};
+    }
+
+    // supported filters push down please refer DeletePushDownVisitorTest
+
+    @Override
+    public boolean applyDeleteFilters(List<ResolvedExpression> list) {
+        checkDeletable();
+        if (list.size() == 0) {
+            return false;
+        }
+
+        predicates = new ArrayList<>();
+        RowType rowType = LogicalTypeConversion.toLogicalType(table.rowType());
+        for (ResolvedExpression filter : list) {
+            Optional<Predicate> predicate = 
PredicateConverter.convert(rowType, filter);
+            if (predicate.isPresent()) {
+                predicates.add(predicate.get());
+            } else {
+                // convert failed, leave it to flink
+                return false;
+            }
+        }
+        predicate = predicates.isEmpty() ? null : 
PredicateBuilder.and(predicates);
+        return canPushDownDeleteFilter(predicate);
+    }
+
+    @Override
+    public Optional<Long> executeDeletion() {
+        // delete partition
+        if (isDeleteFilterPartitionKey) {
+            Map<String, String> partitions =
+                    predicates.stream()
+                            .map(p -> (LeafPredicate) p)
+                            .collect(
+                                    Collectors.toMap(
+                                            LeafPredicate::fieldName,
+                                            x -> 
String.valueOf(x.literals().get(0))));
+
+            FileStoreCommit commit =
+                    ((AbstractFileStoreTable) table)
+                            .store()
+                            .newCommit(UUID.randomUUID().toString());
+            commit.dropPartitions(Arrays.asList(partitions), 
BatchWriteBuilder.COMMIT_IDENTIFIER);
+            return Optional.empty();
+        }
+
+        // delete primary key related data
+        return Optional.of(TableUtils.deleteWhere(table, predicates));
+    }
+
+    private void checkDeletable() {
         if (table instanceof ChangelogWithKeyFileStoreTable) {
             Options options = Options.fromMap(table.options());
             if (options.get(MERGE_ENGINE) == MergeEngine.DEDUPLICATE) {
-                return new RowLevelDeleteInfo() {};
+                return;
             }
             throw new UnsupportedOperationException(
                     String.format(
                             "merge engine '%s' can not support delete, 
currently only %s can support delete.",
                             options.get(MERGE_ENGINE), 
MergeEngine.DEDUPLICATE));
         } else if (table instanceof ChangelogValueCountFileStoreTable) {
-            return new RowLevelDeleteInfo() {};
+            return;
         } else if (table instanceof AppendOnlyFileStoreTable) {
             throw new UnsupportedOperationException(
                     String.format(
@@ -135,4 +214,13 @@ public class FlinkTableSink extends FlinkTableSinkBase
                             table.getClass().getName()));
         }
     }
+
+    private boolean canPushDownDeleteFilter(Predicate predicate) {
+        DeletePushDownPrimaryKeyVisitor pkVisitor =
+                new DeletePushDownPrimaryKeyVisitor(table.primaryKeys());
+        isDeleteFilterPrimaryKey = predicate.visit(pkVisitor) && 
pkVisitor.isHitAll();
+        isDeleteFilterPartitionKey =
+                predicate.visit(new 
DeletePushDownPartitionKeyVisitor(table.partitionKeys()));
+        return isDeleteFilterPartitionKey || isDeleteFilterPrimaryKey;
+    }
 }
diff --git 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/ReadWriteTableITCase.java
 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/ReadWriteTableITCase.java
index 6b9973eb3..ca94bde9c 100644
--- 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/ReadWriteTableITCase.java
+++ 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/ReadWriteTableITCase.java
@@ -1665,6 +1665,213 @@ public class ReadWriteTableITCase extends 
AbstractTestBase {
         }
     }
 
+    @ParameterizedTest
+    @EnumSource(CoreOptions.MergeEngine.class)
+    public void testDeletePushDownWithPrimaryKey(CoreOptions.MergeEngine 
mergeEngine)
+            throws Exception {
+        Set<CoreOptions.MergeEngine> supportUpdateEngines = new HashSet<>();
+        supportUpdateEngines.add(CoreOptions.MergeEngine.DEDUPLICATE);
+
+        // Step1: define table schema
+        Map<String, String> options = new HashMap<>();
+        options.put(WRITE_MODE.key(), WriteMode.CHANGE_LOG.toString());
+        options.put(MERGE_ENGINE.key(), mergeEngine.toString());
+        String table =
+                createTable(
+                        Arrays.asList(
+                                "id BIGINT NOT NULL",
+                                "currency STRING",
+                                "rate BIGINT",
+                                "dt String"),
+                        Arrays.asList("id", "dt"),
+                        Collections.singletonList("dt"),
+                        options);
+
+        // Step2: batch write some historical data
+        insertInto(
+                table,
+                "(1, 'US Dollar', 114, '2022-01-01')",
+                "(2, 'UNKNOWN', -1, '2022-01-01')",
+                "(3, 'Euro', 119, '2022-01-02')",
+                "(4, 'CNY', 119, '2022-01-02')",
+                "(5, 'HKD', 119, '2022-01-03')",
+                "(6, 'CAD', 119, '2022-01-03')",
+                "(7, 'INR', 119, '2022-01-03')",
+                "(8, 'MOP', 119, '2022-01-03')");
+
+        // Step3: prepare delete statement 'where pk = x'
+        String deleteStatement =
+                String.format("DELETE FROM %s WHERE id = 2 and dt = 
'2022-01-01'", table);
+
+        // Step4: execute delete statement and verify result
+        List<Row> expectedRecords =
+                Arrays.asList(
+                        changelogRow("+I", 1L, "US Dollar", 114L, 
"2022-01-01"),
+                        changelogRow("+I", 3L, "Euro", 119L, "2022-01-02"),
+                        changelogRow("+I", 4L, "CNY", 119L, "2022-01-02"),
+                        changelogRow("+I", 5L, "HKD", 119L, "2022-01-03"),
+                        changelogRow("+I", 6L, "CAD", 119L, "2022-01-03"),
+                        changelogRow("+I", 7L, "INR", 119L, "2022-01-03"),
+                        changelogRow("+I", 8L, "MOP", 119L, "2022-01-03"));
+        if (supportUpdateEngines.contains(mergeEngine)) {
+            bEnv.executeSql(deleteStatement).await();
+            String querySql = String.format("SELECT * FROM %s", table);
+            testBatchRead(querySql, expectedRecords);
+        } else {
+            assertThatThrownBy(() -> bEnv.executeSql(deleteStatement).await())
+                    
.satisfies(AssertionUtils.anyCauseMatches(UnsupportedOperationException.class));
+        }
+
+        // Step5: prepare delete statement 'where pk = x or pk = y'
+        String deleteStatement1 =
+                String.format("DELETE FROM %s WHERE id = 3 or id = 4 and dt = 
'2022-01-02'", table);
+        List<Row> expectedRecords1 =
+                Arrays.asList(
+                        changelogRow("+I", 1L, "US Dollar", 114L, 
"2022-01-01"),
+                        changelogRow("+I", 5L, "HKD", 119L, "2022-01-03"),
+                        changelogRow("+I", 6L, "CAD", 119L, "2022-01-03"),
+                        changelogRow("+I", 7L, "INR", 119L, "2022-01-03"),
+                        changelogRow("+I", 8L, "MOP", 119L, "2022-01-03"));
+        if (supportUpdateEngines.contains(mergeEngine)) {
+            bEnv.executeSql(deleteStatement1).await();
+            String querySql = String.format("SELECT * FROM %s", table);
+            testBatchRead(querySql, expectedRecords1);
+        } else {
+            assertThatThrownBy(() -> bEnv.executeSql(deleteStatement1).await())
+                    
.satisfies(AssertionUtils.anyCauseMatches(UnsupportedOperationException.class));
+        }
+
+        // Step5: prepare delete statement 'where pk in (x, y, z)'
+        String deleteStatement2 =
+                String.format(
+                        "DELETE FROM %s WHERE id in (5, 6, 7, 8) and dt = 
'2022-01-03'", table);
+        List<Row> expectedRecords2 =
+                Arrays.asList(changelogRow("+I", 1L, "US Dollar", 114L, 
"2022-01-01"));
+        if (supportUpdateEngines.contains(mergeEngine)) {
+            bEnv.executeSql(deleteStatement2).await();
+            String querySql = String.format("SELECT * FROM %s", table);
+            testBatchRead(querySql, expectedRecords2);
+        } else {
+            assertThatThrownBy(() -> bEnv.executeSql(deleteStatement2).await())
+                    
.satisfies(AssertionUtils.anyCauseMatches(UnsupportedOperationException.class));
+        }
+    }
+
+    @ParameterizedTest
+    @EnumSource(CoreOptions.MergeEngine.class)
+    public void testDeletePushDownWithPartitionKey(CoreOptions.MergeEngine 
mergeEngine)
+            throws Exception {
+        Set<CoreOptions.MergeEngine> supportUpdateEngines = new HashSet<>();
+        supportUpdateEngines.add(CoreOptions.MergeEngine.DEDUPLICATE);
+
+        // Step1: define table schema
+        Map<String, String> options = new HashMap<>();
+        options.put(WRITE_MODE.key(), WriteMode.CHANGE_LOG.toString());
+        options.put(MERGE_ENGINE.key(), mergeEngine.toString());
+        String table =
+                createTable(
+                        Arrays.asList(
+                                "id BIGINT NOT NULL",
+                                "currency STRING",
+                                "rate BIGINT",
+                                "dt String",
+                                "hh String"),
+                        Arrays.asList("id", "dt", "hh"),
+                        Arrays.asList("dt", "hh"),
+                        options);
+
+        // Step2: batch write some historical data
+        insertInto(
+                table,
+                "(1, 'US Dollar', 114, '2022-01-01', '11')",
+                "(2, 'UNKNOWN', -1, '2022-01-01', '12')",
+                "(3, 'Euro', 119, '2022-01-02', '13')",
+                "(4, 'CNY', 119, '2022-01-03', '14')",
+                "(5, 'HKD', 119, '2022-01-03', '15')",
+                "(6, 'CAD', 119, '2022-01-03', '16')",
+                "(7, 'INR', 119, '2022-01-03', '17')",
+                "(8, 'MOP', 119, '2022-01-03', '18')");
+
+        // Step3: partition key not delete push down
+        String deleteStatement =
+                String.format("DELETE FROM %s WHERE dt = '2022-01-03' AND 
currency = 'CNY'", table);
+
+        // Step4: execute delete statement and verify result
+        List<Row> expectedRecords =
+                Arrays.asList(
+                        changelogRow("+I", 1L, "US Dollar", 114L, 
"2022-01-01", "11"),
+                        changelogRow("+I", 2L, "UNKNOWN", -1L, "2022-01-01", 
"12"),
+                        changelogRow("+I", 3L, "Euro", 119L, "2022-01-02", 
"13"),
+                        changelogRow("+I", 5L, "HKD", 119L, "2022-01-03", 
"15"),
+                        changelogRow("+I", 6L, "CAD", 119L, "2022-01-03", 
"16"),
+                        changelogRow("+I", 7L, "INR", 119L, "2022-01-03", 
"17"),
+                        changelogRow("+I", 8L, "MOP", 119L, "2022-01-03", 
"18"));
+        if (supportUpdateEngines.contains(mergeEngine)) {
+            bEnv.executeSql(deleteStatement).await();
+            String querySql = String.format("SELECT * FROM %s", table);
+            testBatchRead(querySql, expectedRecords);
+        } else {
+            assertThatThrownBy(() -> bEnv.executeSql(deleteStatement).await())
+                    
.satisfies(AssertionUtils.anyCauseMatches(UnsupportedOperationException.class));
+        }
+
+        // Step5: partition key not push down
+        String deleteStatement1 =
+                String.format("DELETE FROM %s WHERE dt = '2022-01-02' or hh = 
'15'", table);
+        List<Row> expectedRecords1 =
+                Arrays.asList(
+                        changelogRow("+I", 1L, "US Dollar", 114L, 
"2022-01-01", "11"),
+                        changelogRow("+I", 2L, "UNKNOWN", -1L, "2022-01-01", 
"12"),
+                        changelogRow("+I", 6L, "CAD", 119L, "2022-01-03", 
"16"),
+                        changelogRow("+I", 7L, "INR", 119L, "2022-01-03", 
"17"),
+                        changelogRow("+I", 8L, "MOP", 119L, "2022-01-03", 
"18"));
+        if (supportUpdateEngines.contains(mergeEngine)) {
+            bEnv.executeSql(deleteStatement1).await();
+            String querySql = String.format("SELECT * FROM %s", table);
+            testBatchRead(querySql, expectedRecords1);
+        } else {
+            assertThatThrownBy(() -> bEnv.executeSql(deleteStatement1).await())
+                    
.satisfies(AssertionUtils.anyCauseMatches(UnsupportedOperationException.class));
+        }
+
+        // Step6: partition key delete push down
+        String deleteStatement2 =
+                String.format("DELETE FROM %s WHERE dt = '2022-01-03' and hh = 
'16'", table);
+
+        // Step7: execute delete statement and verify result
+        List<Row> expectedRecords2 =
+                Arrays.asList(
+                        changelogRow("+I", 1L, "US Dollar", 114L, 
"2022-01-01", "11"),
+                        changelogRow("+I", 2L, "UNKNOWN", -1L, "2022-01-01", 
"12"),
+                        changelogRow("+I", 7L, "INR", 119L, "2022-01-03", 
"17"),
+                        changelogRow("+I", 8L, "MOP", 119L, "2022-01-03", 
"18"));
+        if (supportUpdateEngines.contains(mergeEngine)) {
+            bEnv.executeSql(deleteStatement2).await();
+            String querySql = String.format("SELECT * FROM %s", table);
+            testBatchRead(querySql, expectedRecords2);
+        } else {
+            assertThatThrownBy(() -> bEnv.executeSql(deleteStatement2).await())
+                    
.satisfies(AssertionUtils.anyCauseMatches(UnsupportedOperationException.class));
+        }
+
+        // Step8: partition key delete push down
+        String deleteStatement3 = String.format("DELETE FROM %s WHERE dt = 
'2022-01-03'", table);
+
+        // Step9: execute delete statement and verify result
+        List<Row> expectedRecords3 =
+                Arrays.asList(
+                        changelogRow("+I", 1L, "US Dollar", 114L, 
"2022-01-01", "11"),
+                        changelogRow("+I", 2L, "UNKNOWN", -1L, "2022-01-01", 
"12"));
+        if (supportUpdateEngines.contains(mergeEngine)) {
+            bEnv.executeSql(deleteStatement3).await();
+            String querySql = String.format("SELECT * FROM %s", table);
+            testBatchRead(querySql, expectedRecords3);
+        } else {
+            assertThatThrownBy(() -> bEnv.executeSql(deleteStatement3).await())
+                    
.satisfies(AssertionUtils.anyCauseMatches(UnsupportedOperationException.class));
+        }
+    }
+
     // 
----------------------------------------------------------------------------------------------------------------
     // Tools
     // 
----------------------------------------------------------------------------------------------------------------


Reply via email to