diff --git a/paimon-core/src/main/java/org/apache/paimon/mergetree/compact/LookupMergeFunction.java b/paimon-core/src/main/java/org/apache/paimon/mergetree/compact/LookupMergeFunction.java index 7fc3e1a11977..71425ef50dc2 100644 --- a/paimon-core/src/main/java/org/apache/paimon/mergetree/compact/LookupMergeFunction.java +++ b/paimon-core/src/main/java/org/apache/paimon/mergetree/compact/LookupMergeFunction.java @@ -25,6 +25,7 @@ import javax.annotation.Nullable; +import java.util.Iterator; import java.util.LinkedList; /** @@ -58,6 +59,21 @@ public void add(KeyValue kv) { @Override public KeyValue getResult() { + // 1. Find the latest high level record + Iterator descending = candidates.descendingIterator(); + KeyValue highLevel = null; + while (descending.hasNext()) { + KeyValue kv = descending.next(); + if (kv.level() > 0) { + if (highLevel != null) { + descending.remove(); + } else { + highLevel = kv; + } + } + } + + // 2. Do the merge for inputs mergeFunction.reset(); candidates.forEach(mergeFunction::add); return mergeFunction.getResult();