zhuxiangyi commented on code in PR #10391: URL: https://github.com/apache/paimon/pull/10391#discussion_r4184460648
########## 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: Thanks, reproduced: with your schedule the shift gives {1:2, 2:3, 20:2, 21:3}. Fixed in 65c3b7b34 by no longer allocating row ids in the copy. Copied files are now committed like files written for the target: no `firstRowId`, `APPEND`, sequence numbers 0. The commit then assigns row ids from the snapshot it commits on, on every attempt, the same as for any writer. A dedicated blob or vector file is ordered after its normal file, so it gets the same range. A data-evolution column update in the source (two files with the same rows) can't be committed this way, so the copy refuses it and asks to compact the source first. Tests in `CopiedDataFilesTest`: your schedule, the same into an empty target, and a copy commit that is retried after a concurrent append. All end with four distinct row ids. -- 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]
