JustinBinber commented on code in PR #11322:
URL: https://github.com/apache/paimon/pull/11322#discussion_r4667057157


##########
paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/commands/MergeIntoPaimonDataEvolutionTable.scala:
##########
@@ -1432,6 +1497,77 @@ object MergeIntoPaimonDataEvolutionTable {
   final private val MERGE_DELETED_NAME = "_PAIMON_MERGE_INTO_DELETED"
   final private val RAW_BLOB_PLACEHOLDER_MARKER_PREFIX = 
"__paimon_raw_blob_placeholder_"
 
+  private[commands] def pinNarrowSource(
+      plan: LogicalPlan,
+      targetRelation: DataSourceV2Relation): Option[LogicalPlan] = {
+    def identity(table: FileStoreTable): (String, String) =
+      (
+        table.location().toString,
+        BranchManager.normalizeBranch(CoreOptions.branch(table.options())))
+
+    val target = targetRelation.table match {
+      case sparkTable: SparkTable => 
sparkTable.getTable.asInstanceOf[FileStoreTable]
+      case _ => return None
+    }
+
+    def pin(current: LogicalPlan): Option[LogicalPlan] = current match {
+      case alias: SubqueryAlias => pin(alias.child).map(child => 
alias.copy(child = child))
+      case project: Project if project.projectList.forall {
+            case _: AttributeReference => true
+            case Alias(_: AttributeReference, _) => true
+            case _ => false
+          } =>
+        pin(project.child).map(child => project.copy(child = child))
+      case relation: DataSourceV2Relation =>
+        relation.table match {
+          case sparkTable: SparkTable =>
+            sparkTable.getTable match {
+              // Pinning one branch would time-travel the other branch as well.
+              case _: FallbackReadFileStoreTable => None
+              case source: FileStoreTable
+                  if source.primaryKeys().isEmpty && identity(source) != 
identity(target) &&
+                    Set(
+                      CoreOptions.StartupMode.LATEST_FULL,
+                      CoreOptions.StartupMode.LATEST,
+                      CoreOptions.StartupMode.FROM_TIMESTAMP,
+                      CoreOptions.StartupMode.FROM_SNAPSHOT,
+                      CoreOptions.StartupMode.FROM_SNAPSHOT_FULL
+                    )
+                      .contains(source.coreOptions().startupMode()) =>
+                // Resolving scan.version can mutate options; never resolve it 
on the input table.
+                val copied = source.copy(new JHashMap[String, String]())
+                Option(TimeTravelUtil.tryTravelOrLatest(copied))
+                  .filter(snapshot => 
copied.snapshotManager().snapshotExists(snapshot.id()))
+                  .flatMap {
+                    snapshot =>
+                      val options = new JHashMap[String, String]()
+                      timeTravelOptionKeys.foreach(options.put(_, null))
+                      options.put(CoreOptions.SCAN_SNAPSHOT_ID.key(), 
snapshot.id().toString)

Review Comment:
   Thanks for the reproducer. I reproduced the numeric pin resolving the 
replacement snapshot after rollback, even though its schema ID was unchanged.
   
   Fixed in `afbe64ba66` and `d201e73cc5`. Both MERGE reads now resolve through 
a private cache seeded with the original Snapshot object, so they use the same 
manifest references. If rollback removes the original files, the reread fails 
before target commit. Sources configured to skip lost or corrupt files use the 
existing shared path. The Spark 4.0 mirror uses the same binding.
   
   The regression performs a rollback and a new commit with the same snapshot 
ID and schema. It checks that the pinned reread returns the original rows or 
reports missing original files. The rollback regression and the skip-files 
fallback regression both reproduced on the corresponding previous code and 
passed after the fixes on Spark 3.5.9.
   
   The repository-wide JDK setup retry is reverted in `3dc98d1fbb`. Neither 
workflow file is part of the PR diff.
   
   Local Spark 4.1.4 verification is pending due to a parser tool/runtime 
mismatch in the local build. The latest revision is running through 
[CI](https://github.com/apache/paimon/actions/runs/45192977580).
   



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