leaves12138 commented on code in PR #9197: URL: https://github.com/apache/paimon/pull/9197#discussion_r3771407392
########## paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/commands/DataEvolutionCompactMergeConflictRewriter.scala: ########## @@ -0,0 +1,303 @@ +/* + * 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.spark.commands + +import org.apache.paimon.Snapshot +import org.apache.paimon.data.BinaryRow +import org.apache.paimon.format.blob.BlobFileFormat.isBlobFile +import org.apache.paimon.io.{CompactIncrement, DataFileMeta, DataIncrement} +import org.apache.paimon.spark.util.ScanPlanHelper +import org.apache.paimon.table.{FileStoreTable, SpecialFields} +import org.apache.paimon.table.sink.{CommitMessage, CommitMessageImpl} +import org.apache.paimon.table.source.{DataSplit, IncrementalSplit} +import org.apache.paimon.types.VectorType.isVectorStoreFile +import org.apache.paimon.utils.Range + +import org.apache.spark.sql.{functions, SparkSession} +import org.apache.spark.sql.PaimonUtils.createDataset +import org.apache.spark.sql.catalyst.analysis.SimpleAnalyzer.resolver +import org.apache.spark.sql.catalyst.expressions.AttributeReference +import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation +import org.apache.spark.sql.functions.{col, udf} +import org.apache.spark.sql.paimon.shims.SparkShimLoader + +import java.util.{Collections, List => JList, Optional => JOptional} + +import scala.collection.JavaConverters._ + +/** Rebases MERGE-compatible partial-column files onto staged compact output boundaries. */ +class DataEvolutionCompactMergeConflictRewriter( + table: FileStoreTable, + targetRelation: DataSourceV2Relation) + extends ScanPlanHelper { + + import DataEvolutionCompactMergeConflictRewriter._ + + def rewrite( + sparkSession: SparkSession, + baseSnapshot: Snapshot, + latestSnapshot: Snapshot, + compactMessages: JList[CommitMessage]): JOptional[JList[CommitMessage]] = { + if ( + table.coreOptions().deletionVectorsEnabled() || + latestSnapshot.id() <= baseSnapshot.id() + ) { + return JOptional.empty() + } + + val messageImpls = compactMessages.asScala.collect { + case message: CommitMessageImpl => message + } + if (messageImpls.size != compactMessages.size()) { + return JOptional.empty() + } + + val targets = messageImpls.flatMap( + message => + normalRowIdFiles(message.compactIncrement().compactAfter().asScala) + .map(file => CompactTarget(message, file))) + if (targets.isEmpty) { + return JOptional.empty() + } + + // Snapshot.operation is optional and Python MERGE currently does not persist it. Validate + // the portable partial-column file contract below instead. + val additions = table + .newSnapshotReader() + .withSnapshot(latestSnapshot) + .readIncrementalDiff(baseSnapshot) + .splits() + .asScala + .collect { case split: IncrementalSplit => split } + .flatMap( + split => + split + .afterFiles() + .asScala + .map(file => AddedFile(split.partition(), split.bucket(), file))) + + val overlappingAdditions = additions.filter(addition => targets.exists(_.intersects(addition))) + if (overlappingAdditions.isEmpty) { + return JOptional.empty() + } + if (overlappingAdditions.exists(addition => !isRegularPartialFile(addition.file))) { + return JOptional.empty() + } + if (overlappingAdditions.exists(addition => targets.count(_.contains(addition)) != 1)) { + return JOptional.empty() + } + + val targetRewrites = targets.flatMap { + target => + val files = overlappingAdditions.filter(target.contains) + if (files.nonEmpty && files.exists(file => file.file.nonNullRowIdRange() != target.range)) { + val updatedFields = table + .rowType() + .getFieldNames + .asScala + .filter(name => files.exists(_.file.writeCols().contains(name))) + .toSeq + if (updatedFields.isEmpty) { Review Comment: This can silently lose updates when the schema evolves concurrently. If compaction starts with an old schema and a concurrent partial update writes both an existing column and a newly added column, `updatedFields` is filtered through the stale `table.rowType()`, so the new column is omitted. The rewritten compact increment then deletes the original partial file but replaces it with a file containing only the old column. Since the commit uses the latest schema, it succeeds while the new-column update is lost. I reproduced this with an old `(id, value)` compaction, concurrent `ADD COLUMN extra`, and a partial update of `(value, extra)`: before the rebase the row is `(1, 11, 99)`, but after the compact commit it becomes `(1, 11, NULL)`. Please either reject rebasing when the schema changed between the preparation and latest snapshots, or rebuild the rewrite against the latest schema while preserving every column in the concurrent partial file. -- 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]
