JingsongLi commented on code in PR #8880:
URL: https://github.com/apache/paimon/pull/8880#discussion_r3662498907


##########
paimon-core/src/main/java/org/apache/paimon/lookup/local/LocalKvListMergeOperator.java:
##########
@@ -0,0 +1,63 @@
+/*
+ * 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.lookup.local;
+
+import org.apache.paimon.lookup.sort.db.LocalKvDb;
+import org.apache.paimon.memory.MemorySlice;
+
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * Combines ListState fragments with the same logical key when writing SST 
files.
+ *
+ * <p>When TTL is enabled, merging includes expired fragments and re-encodes 
the result with a new
+ * expiration time, matching RocksDB's TTL merge behavior.
+ */
+final class LocalKvListMergeOperator implements LocalKvDb.MergeOperator {
+
+    private final LocalKvValueCodec valueCodec;
+    private final ThreadLocal<LocalKvListValueCodec> listValueCodec =
+            ThreadLocal.withInitial(LocalKvListValueCodec::new);
+
+    LocalKvListMergeOperator(LocalKvValueCodec valueCodec) {
+        this.valueCodec = valueCodec;
+    }
+
+    @Override
+    public boolean canMerge(MemorySlice firstKey, MemorySlice nextKey) {

Review Comment:
   Thanks, you are right. The intended behavior is to match RocksDB's 
`TtlMergeOperator`: it strips the TTL timestamp from the existing value and 
merge operands, invokes the user merge operator, and then appends the current 
timestamp to the merged result ([RocksDB 6.20.3 
source](https://github.com/facebook/rocksdb/blob/v6.20.3/utilities/ttl/db_ttl_impl.h#L214-L320)).
   
   I reproduced your repeated-flush sequence locally; before the fix it 
returned only `[30]`. The synthetic tombstone gap prevented LocalKv from 
reaching the merge that should refresh the whole list, so the implementation 
did not actually preserve the intended RocksDB behavior in this case.
   
   Fixed in `df92a1e00a`: `MergeOperator` now has an opt-in `canMergeTombstone` 
hook which remains `false` by default, while `LocalKvListMergeOperator` opts in 
for tombstones belonging to the same logical list. `RecordCombiningWriter` 
keeps those synthetic tombstone keys in the pending group without passing 
tombstone values to the merge operator, allowing all list fragments to merge 
before TTL filtering while preserving the default tombstone boundary for other 
operators.
   
   Added both `testCompactionMergesAcrossAbsorbedTombstones` and the 
repeated-flush TTL regression 
`testListStateTtlIsRefreshedAcrossRepeatedFlushes`. Full `LocalKvDbTest` (68 
tests) and `LocalKvStateFactoryTest` (14 tests) pass.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to