anew commented on code in PR #57365:
URL: https://github.com/apache/spark/pull/57365#discussion_r3627452064


##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd2BatchProcessor.scala:
##########
@@ -811,6 +811,93 @@ case class Scd2BatchProcessor(
     staged.select(outputColumns.toImmutableArraySeq: _*)
   }
 
+  /**
+   * Drop delete-encoded rows (tombstones and decomposition tails) that became 
redundant after
+   * reconciliation.
+   *
+   * A tombstone or decomposition tail is redundant when the immediately 
preceding row's reconciled
+   * [[endAtColName]] equals its own sequence: the preceding upsert already 
encodes the delete
+   * boundary, so the standalone delete-encoded row should no longer be routed 
to aux.
+   *
+   * For example, an open upsert `[startAt=10, endAt=null)` followed by a 
tombstone at `15`
+   * reconciles into a closed upsert `[10, 15)`, making the tombstone 
redundant. Likewise, an
+   * existing closed `[10, 20)` bisected by an event at `15` reconciles the 
event into `[15, 20)`,
+   * making the `[null, 20)` decomposition tail redundant.
+   */
+  private[autocdc] def dropLeftoverDeletesPostReconciliation(
+      reconciledDf: DataFrame): DataFrame = {
+    val recordStartAt =
+      
Scd2BatchProcessor.recordStartAtOf(F.col(AutoCdcReservedNames.cdcMetadataColName))
+    val startAt = F.col(Scd2BatchProcessor.startAtColName)
+    val endAt = F.col(Scd2BatchProcessor.endAtColName)
+
+    // Both tombstones and decomposition tails encode a delete boundary in 
their `endAt`. Either
+    // becomes redundant when the immediately preceding upsert was reconciled 
to close exactly on
+    // that boundary, since the resulting closed upsert already carries it.
+    val row = Scd2IntervalColumns(recordStartAt, startAt, endAt)
+    val isTombstone = RowClassifier.isTombstone(row)
+    val isDecompositionTail = RowClassifier.isDecompositionTail(row)
+    val isDeleteEncodedRow = isTombstone || isDecompositionTail
+
+    val withWindowCols = reconciledDf
+      .withColumn(
+        Scd2BatchProcessor.previousEndAtColName,
+        F.lag(endAt, 1).over(orderChronologicallyPerKeyWindow)
+      )
+      .withColumn(
+        Scd2BatchProcessor.isRedundantDeleteEncodingColName,
+        isDeleteEncodedRow && (F.col(Scd2BatchProcessor.previousEndAtColName) 
<=> endAt)
+      )
+
+    withWindowCols
+      .filter(!F.col(Scd2BatchProcessor.isRedundantDeleteEncodingColName))
+      .drop(
+        Scd2BatchProcessor.previousEndAtColName,
+        Scd2BatchProcessor.isRedundantDeleteEncodingColName
+      )
+  }
+
+  /**
+   * Convert surviving decomposition tails into tombstones.

Review Comment:
    Agreed this is worth a look on the full-algorithm pass. They're handled 
identically here (both are just delete-boundary encodings), and a decomposition 
tail is essentially the within-batch analog of a tombstone — the main 
distinction today is provenance (a tail is synthesized during bisection and 
carries a null recordStartAt, a tombstone comes from an actual delete event). 
Whether they can be collapsed into one representation is a good question to 
settle when we review the algorithm end-to-end; but I think it s outside of 
this PR's scope.
   



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to