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


##########
paimon-core/src/main/java/org/apache/paimon/migrate/CopiedDataFiles.java:
##########
@@ -0,0 +1,198 @@
+/*
+ * 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.migrate;
+
+import org.apache.paimon.Snapshot;
+import org.apache.paimon.io.DataFileMeta;
+import org.apache.paimon.table.FileStoreTable;
+import org.apache.paimon.table.SpecialFields;
+
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Map;
+import java.util.TreeSet;
+
+/**
+ * Adapts the metadata of data files copied from another table, as {@code 
sys.copy} does, to the
+ * table they are committed into.
+ *
+ * <ul>
+ *   <li>Row ids: a copied file keeps the first row id it had in the source 
table. When the target
+ *       has given out those row ids already, for example to rows of a 
partition the copy does not
+ *       overwrite, the copied rows would share row ids with them. All copied 
row ids are then
+ *       shifted by the same offset, so that they follow the row ids of the 
target and keep their
+ *       layout: files that update columns of the same rows keep sharing a row 
id range.
+ *   <li>Sequence numbers: a data-evolution read takes each column from the 
file with the highest
+ *       sequence number, and a commit stamps new files with its snapshot id. 
Copied files keep the
+ *       sequence numbers of the source, which can exceed the snapshot ids of 
the target, so later
+ *       updates in the target would be ignored. On a data-evolution target, 
the copied sequence
+ *       numbers are mapped, in order, to values up to the snapshot id of the 
copy: the newest
+ *       becomes 0, which the commit stamps with its snapshot id, older ones 
-1, -2 and so on.
+ * </ul>
+ */
+public final class CopiedDataFiles {
+
+    private CopiedDataFiles() {}
+
+    /** Replaces, in place, every copied file by its metadata for {@code 
target}. */
+    public static void adaptToTarget(
+            FileStoreTable target, Collection<List<DataFileMeta>> 
copiedFilesByBucket) {
+        List<DataFileMeta> copied = new ArrayList<>();
+        copiedFilesByBucket.forEach(copied::addAll);
+        if (copied.isEmpty()) {
+            return;
+        }
+        long rowIdShift = rowIdShift(target, copied);

Review Comment:
   [P1] Allocate copied row IDs against the snapshot being committed
   
   This shift is computed once before CopyFilesCommitOperator creates its 
overwrite committer, so another writer can allocate the chosen range before the 
copy commits. A real-file reproducer starts with source p1 IDs [0,2) and target 
p1 IDs [0,2). adaptToTarget shifts the copy to [2,4); a competing append into 
target p2 then successfully takes [2,4). The copy overwrite also succeeds and 
keeps p2, yielding rows {1:2, 2:3, 20:2, 21:3}: four rows but two distinct 
_ROW_IDs. The same schedule without this new adaptation retains source IDs 
[0,2), giving four distinct IDs. assignRowTracking preserves the preassigned 
IDs, and append conflict detection does not reject their overlap. This breaks 
row identity used by later updates/merges and indexes. Recompute the common 
shift from the latest snapshot inside each optimistic commit attempt, or 
fence/reject and retry an allocation when the target snapshot changes; keep 
overlapping DE column files together.



-- 
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