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

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


The following commit(s) were added to refs/heads/master by this push:
     new fd41783f25 [flink] Honor table retention in expire_changelogs (#10968)
fd41783f25 is described below

commit fd41783f25c2d156f8263d6106fa488d9c748eb6
Author: csurong <[email protected]>
AuthorDate: Wed Nov 4 14:30:53 2026 +0800

    [flink] Honor table retention in expire_changelogs (#10968)
---
 docs/docs/flink/procedures/snapshots.md            |  14 ++
 .../flink/procedure/ExpireChangelogsProcedure.java |  40 +++--
 .../procedure/ExpireChangelogsProcedureITCase.java | 167 ++++++++++++++++++++-
 3 files changed, 193 insertions(+), 28 deletions(-)

diff --git a/docs/docs/flink/procedures/snapshots.md 
b/docs/docs/flink/procedures/snapshots.md
index 07903186e2..4ec45dd0cb 100644
--- a/docs/docs/flink/procedures/snapshots.md
+++ b/docs/docs/flink/procedures/snapshots.md
@@ -101,6 +101,20 @@ To expire changelogs. Argument:
 
 - `delete_all`: whether to delete all separated changelogs.
 
+For Flink 1.19 and later, explicit arguments override table retention options 
for this invocation
+without changing the stored table options. Omitted arguments use the table 
options:
+`retain_max` uses `changelog.num-retained.max`, `retain_min` uses 
`changelog.num-retained.min`,
+`older_than` uses `changelog.time-retained`, and `max_deletes` uses 
`snapshot.expire.limit`.
+Unset changelog retention options follow the corresponding snapshot retention 
options.
+This also applies to the `expire_changelogs` Flink action. Calling with only a 
table identifier
+therefore expires changelogs according to the table's retention policy. An 
explicitly supplied
+`older_than` must not be empty or whitespace-only, including when `delete_all` 
is true.
+
+Previously, omitted arguments used internal defaults instead of table options. 
Existing calls
+may now expire changelogs that were previously retained, or fail if an 
explicit maximum conflicts
+with the table's minimum retention. Specify both bounds when overriding them 
together.
+The `delete_all` path is unchanged and does not use these retention arguments.
+
 **Syntax**
 
 ```sql
diff --git 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/procedure/ExpireChangelogsProcedure.java
 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/procedure/ExpireChangelogsProcedure.java
index 38491567d5..da369cfce9 100644
--- 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/procedure/ExpireChangelogsProcedure.java
+++ 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/procedure/ExpireChangelogsProcedure.java
@@ -21,15 +21,17 @@ package org.apache.paimon.flink.procedure;
 import org.apache.paimon.catalog.Catalog;
 import org.apache.paimon.options.ExpireConfig;
 import org.apache.paimon.table.ExpireChangelogImpl;
-import org.apache.paimon.utils.DateTimeUtils;
+import org.apache.paimon.table.FileStoreTable;
+import org.apache.paimon.table.Table;
+import org.apache.paimon.utils.ProcedureUtils;
+import org.apache.paimon.utils.StringUtils;
 
 import org.apache.flink.table.annotation.ArgumentHint;
 import org.apache.flink.table.annotation.DataTypeHint;
 import org.apache.flink.table.annotation.ProcedureHint;
 import org.apache.flink.table.procedure.ProcedureContext;
 
-import java.time.Duration;
-import java.util.TimeZone;
+import static org.apache.paimon.utils.Preconditions.checkArgument;
 
 /** A procedure to expire changelogs. */
 public class ExpireChangelogsProcedure extends ProcedureBase {
@@ -72,26 +74,18 @@ public class ExpireChangelogsProcedure extends 
ProcedureBase {
             Integer maxDeletes,
             Boolean deleteAll)
             throws Catalog.TableNotExistException {
-        ExpireChangelogImpl expireChangelogs =
-                (ExpireChangelogImpl) table(tableId).newExpireChangelog();
-        ExpireConfig.Builder builder = ExpireConfig.builder();
-        if (retainMax != null) {
-            builder.changelogRetainMax(retainMax);
-        }
-        if (retainMin != null) {
-            builder.changelogRetainMin(retainMin);
-        }
-        if (olderThanStr != null) {
-            builder.changelogTimeRetain(
-                    Duration.ofMillis(
-                            System.currentTimeMillis()
-                                    - DateTimeUtils.parseTimestampData(
-                                                    olderThanStr, 3, 
TimeZone.getDefault())
-                                            .getMillisecond()));
-        }
-        if (maxDeletes != null) {
-            builder.changelogMaxDeletes(maxDeletes);
-        }
+        checkArgument(
+                olderThanStr == null || 
!StringUtils.isNullOrWhitespaceOnly(olderThanStr),
+                "older_than must not be empty or whitespace-only when 
specified.");
+        Table table = table(tableId);
+        ExpireChangelogImpl expireChangelogs = (ExpireChangelogImpl) 
table.newExpireChangelog();
+        ExpireConfig.Builder builder =
+                ProcedureUtils.fillInChangelogOptions(
+                        ((FileStoreTable) table).store().options(),
+                        retainMax,
+                        retainMin,
+                        olderThanStr,
+                        maxDeletes);
         if (deleteAll != null && deleteAll) {
             expireChangelogs.expireAll();
             return new String[] {"Delete all separated changelogs success."};
diff --git 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/procedure/ExpireChangelogsProcedureITCase.java
 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/procedure/ExpireChangelogsProcedureITCase.java
index 314b58136e..113d7e8a28 100644
--- 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/procedure/ExpireChangelogsProcedureITCase.java
+++ 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/procedure/ExpireChangelogsProcedureITCase.java
@@ -41,6 +41,7 @@ import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 /** IT Case for {@link ExpireChangelogsProcedure}. */
 public class ExpireChangelogsProcedureITCase extends CatalogITCaseBase {
@@ -69,8 +70,9 @@ public class ExpireChangelogsProcedureITCase extends 
CatalogITCaseBase {
         // expected changelogs (1, 2, 3, 4, 5, 6)
         checkChangelogs(changelogManager, 1, 6);
 
-        // retain_max => 8, expected changelogs (3, 4, 5, 6)
-        sql("CALL sys.expire_changelogs(`table` => 'default.word_count', 
retain_max => 8)");
+        // Override both bounds because the table minimum is 10. Expected 
changelogs (3, 4, 5, 6).
+        sql(
+                "CALL sys.expire_changelogs(`table` => 'default.word_count', 
retain_max => 8, retain_min => 1)");
         checkChangelogs(changelogManager, 3, 6);
 
         // older_than => timestamp of snapshot 10, max_deletes => 1, expected 
snapshots (3, 4, 5, 6)
@@ -78,7 +80,7 @@ public class ExpireChangelogsProcedureITCase extends 
CatalogITCaseBase {
         sql(
                 "CALL sys.expire_changelogs(`table` => 'default.word_count', 
older_than => '"
                         + ts7
-                        + "', max_deletes => 1)");
+                        + "', max_deletes => 1, retain_min => 1)");
         checkChangelogs(changelogManager, 4, 6);
 
         // older_than => timestamp of snapshot 7, retain_min => 6, expected 
snapshots (5, 6)
@@ -92,7 +94,7 @@ public class ExpireChangelogsProcedureITCase extends 
CatalogITCaseBase {
         sql(
                 "CALL sys.expire_changelogs(`table`  => 'default.word_count', 
older_than => '"
                         + ts7
-                        + "')");
+                        + "', retain_min => 1)");
         checkChangelogs(changelogManager, 6, 6);
 
         // prepare 2 more snapshots
@@ -106,7 +108,8 @@ public class ExpireChangelogsProcedureITCase extends 
CatalogITCaseBase {
 
         // retain_max => 4, same as snapshot.num-retained.max
         // expected changelogs (8), retain 1 latest changelog at least
-        sql("CALL sys.expire_changelogs(`table` => 'default.word_count', 
retain_max => 4)");
+        sql(
+                "CALL sys.expire_changelogs(`table` => 'default.word_count', 
retain_max => 4, retain_min => 1)");
         checkChangelogs(changelogManager, 8, 8);
         checkBatchRead(12);
 
@@ -257,6 +260,160 @@ public class ExpireChangelogsProcedureITCase extends 
CatalogITCaseBase {
         checkAllDeleted(changelogManager);
     }
 
+    @ParameterizedTest
+    @ValueSource(booleans = {false, true})
+    public void testExpireChangelogsUsesTableRetention(boolean action) throws 
Exception {
+        ChangelogManager cm = prepareRetainedChangelogs();
+        sql(
+                "ALTER TABLE word_count SET ('changelog.num-retained.min'='4', 
"
+                        + "'changelog.num-retained.max'='8')");
+        if (action) {
+            createAction(
+                            ExpireChangelogsAction.class,
+                            "expire_changelogs",
+                            "--warehouse",
+                            path,
+                            "--database",
+                            "default",
+                            "--table",
+                            "word_count")
+                    .run();
+        } else {
+            assertThat(sql("CALL sys.expire_changelogs(`table` => 
'default.word_count')"))
+                    .containsExactly(Row.of("2"));
+        }
+        checkChangelogs(cm, 3, 6);
+        assertThat(cm.safelyGetAllChangelogs()).hasSize(4);
+        checkBatchRead(10);
+    }
+
+    @Test
+    public void testExpireChangelogsPartialOverrides() throws Exception {
+        ChangelogManager cm = prepareRetainedChangelogs();
+        sql(
+                "ALTER TABLE word_count SET ('changelog.num-retained.min'='8', 
"
+                        + "'snapshot.expire.limit'='1')");
+        String olderThan =
+                new Timestamp(
+                                paimonTable("word_count")
+                                                .snapshotManager()
+                                                .latestSnapshot()
+                                                .timeMillis()
+                                        + 1000)
+                        .toString();
+        // Explicit max overrides table max=10; omitted min=8 and delete 
limit=1 are inherited.
+        sql(
+                "CALL sys.expire_changelogs(`table` => 'default.word_count', 
retain_max => 8, "
+                        + "older_than => '"
+                        + olderThan
+                        + "')");
+        checkChangelogs(cm, 2, 6);
+        // Override the delete limit, but still respect the table's minimum 
retention.
+        sql(
+                "CALL sys.expire_changelogs(`table` => 'default.word_count', 
retain_max => 8, "
+                        + "older_than => '"
+                        + olderThan
+                        + "', max_deletes => 10)");
+        checkChangelogs(cm, 3, 6);
+        // Inheriting min=8 makes an explicit max=7 invalid.
+        assertThatThrownBy(
+                        () ->
+                                sql(
+                                        "CALL sys.expire_changelogs(`table` => 
'default.word_count', retain_max => 7)"))
+                .hasStackTraceContaining("retainMax (7) must not be less than 
retainMin (8)");
+        checkChangelogs(cm, 3, 6);
+        // Explicit min and max override the table values for this invocation 
only.
+        sql(
+                "CALL sys.expire_changelogs(`table` => 'default.word_count', 
retain_max => 4, "
+                        + "retain_min => 1, older_than => '"
+                        + olderThan
+                        + "', max_deletes => 10)");
+        checkChangelogs(cm, 6, 6);
+        assertThat(paimonTable("word_count").options())
+                .containsEntry("changelog.num-retained.min", "8")
+                .containsEntry("changelog.num-retained.max", "10");
+        checkBatchRead(10);
+    }
+
+    @Test
+    public void testExpireChangelogsUsesTableTimeRetention() throws Exception {
+        ChangelogManager cm = prepareRetainedChangelogs();
+        sql(
+                "ALTER TABLE word_count SET ('changelog.num-retained.min'='4', 
"
+                        + "'changelog.time-retained'='0 ms')");
+        // max=10 alone would retain all ten versions; time retention must 
trigger cleanup.
+        sql("CALL sys.expire_changelogs(`table` => 'default.word_count')");
+        checkChangelogs(cm, 6, 6);
+        assertThat(cm.safelyGetAllChangelogs()).hasSize(1);
+        checkBatchRead(10);
+    }
+
+    @ParameterizedTest
+    @ValueSource(booleans = {false, true})
+    public void testExpireChangelogsRejectsBlankOlderThan(boolean action) 
throws Exception {
+        ChangelogManager cm = prepareRetainedChangelogs();
+        // Make logs eligible for deletion so treating blank as omitted would 
be destructive.
+        sql(
+                "ALTER TABLE word_count SET ('changelog.num-retained.min'='4', 
"
+                        + "'changelog.num-retained.max'='8')");
+        for (String olderThan : new String[] {"", "   ", "\t\n"}) {
+            for (boolean deleteAll : new boolean[] {false, true}) {
+                assertThatThrownBy(
+                                () -> {
+                                    if (action) {
+                                        createAction(
+                                                        
ExpireChangelogsAction.class,
+                                                        "expire_changelogs",
+                                                        "--warehouse",
+                                                        path,
+                                                        "--database",
+                                                        "default",
+                                                        "--table",
+                                                        "word_count",
+                                                        "--older_than",
+                                                        olderThan,
+                                                        "--delete_all",
+                                                        
Boolean.toString(deleteAll))
+                                                .run();
+                                    } else {
+                                        sql(
+                                                "CALL 
sys.expire_changelogs(`table` => 'default.word_count', older_than => '"
+                                                        + olderThan
+                                                        + "', delete_all => "
+                                                        + deleteAll
+                                                        + ")");
+                                    }
+                                })
+                        .hasStackTraceContaining(
+                                "older_than must not be empty or 
whitespace-only when specified.");
+                checkChangelogs(cm, 1, 6);
+                assertThat(cm.safelyGetAllChangelogs()).hasSize(6);
+                for (long id = 1; id <= 6; id++) {
+                    assertThat(cm.longLivedChangelogExists(id)).isTrue();
+                }
+            }
+        }
+        checkSnapshots(paimonTable("word_count").snapshotManager(), 7, 10);
+        checkBatchRead(10);
+    }
+
+    private ChangelogManager prepareRetainedChangelogs() throws Exception {
+        sql(
+                "CREATE TABLE word_count (word STRING PRIMARY KEY NOT 
ENFORCED, cnt INT)"
+                        + " WITH ('num-sorted-run.compaction-trigger'='9999', 
'changelog-producer'='input',"
+                        + " 'snapshot.num-retained.min'='4', 
'snapshot.num-retained.max'='4',"
+                        + " 'changelog.num-retained.min'='10', 
'changelog.num-retained.max'='10')");
+        for (int i = 0; i < 10; i++) {
+            sql("INSERT INTO word_count VALUES ('" + i + "', " + i + ")");
+        }
+        FileStoreTable table = paimonTable("word_count");
+        checkSnapshots(table.snapshotManager(), 7, 10);
+        ChangelogManager cm = table.changelogManager();
+        checkChangelogs(cm, 1, 6);
+        assertThat(cm.safelyGetAllChangelogs()).hasSize(6);
+        return cm;
+    }
+
     private void checkSnapshots(SnapshotManager sm, int earliest, int latest) 
throws IOException {
         assertThat(sm.snapshotCount()).isEqualTo(latest - earliest + 1);
         assertThat(sm.earliestSnapshotId()).isEqualTo(earliest);

Reply via email to