You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "AnishMahto (via GitHub)" <gi...@apache.org> on 2026/05/20 18:38:46 UTC
[PR] [SPARK-56953][SDP] Implement SCD1 Batch Processor; foreachBatch Callback [spark]
AnishMahto opened a new pull request, #56016:
URL: https://github.com/apache/spark/pull/56016
Approved AutoCDC SPIP: https://lists.apache.org/thread/j6sj9wo9odgdpgzlxtvhoy7szs0jplf7
--------
This is a stacked PR. Review incremental diff here: https://github.com/AnishMahto/spark/compare/SPARK-56927-SCD1-merge-microbatch-onto-target...SPARK-56953-SCD1-foreachbatch-callback
--------
**Preamble:**
The SCD type 1 flow is a foreachBatch streaming query on an input change-data-feed, and is responsible for reconciling the incoming change data onto some target table that follows SCD1 replication semantics.
SCD1 flows also maintain an "auxiliary" table to keep track of early-arriving out-of-order received events state. Each microbatch will need to reconcile against this auxiliary table as well, and update the auxiliary table's state appropriately for future microbatches.
**foreachBatch Callback:**
Implementation of the actual callback that will be passed into the foreachBatch streaming query for SCD1 flows.
The callback orchestrates microbatch validation and calling all of the `Scd1BatchProcessor` helpers to execute the complete reconciliation of a change-data-feed microbatch onto the auxiliary and target tables.
Introduce Scd1ForeachBatchExecSuite to exercise E2E business logic unit tests. In the future when we hook this up to an actual flow execution, we will introduce E2E _integration_ tests that can additionally verify schema evolution, interaction with other dataflow graph entities, etc.
Additionally refactor some test helper functions into a shared `AutoCdcCatalogExecutionTestBase`.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56953][SDP] Implement SCD1 Batch Processor; foreachBatch Callback [spark]
Posted by "dbtsai (via GitHub)" <gi...@apache.org>.
dbtsai closed pull request #56016: [SPARK-56953][SDP] Implement SCD1 Batch Processor; foreachBatch Callback
URL: https://github.com/apache/spark/pull/56016
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56953][SDP] Implement SCD1 Batch Processor; foreachBatch Callback [spark]
Posted by "szehon-ho (via GitHub)" <gi...@apache.org>.
szehon-ho commented on code in PR #56016:
URL: https://github.com/apache/spark/pull/56016#discussion_r3293364524
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1ForeachBatchHandler.scala:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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.spark.sql.pipelines.autocdc
+
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.classic.DataFrame
+
+/**
+ * Exposes an API to execute one SCD Type 1 AutoCDC microbatch reconciliation on a
+ * foreachBatch streaming query.
+ */
+case class Scd1ForeachBatchHandler(
+ batchProcessor: Scd1BatchProcessor,
+ auxiliaryTableIdentifier: TableIdentifier,
+ targetTableIdentifier: TableIdentifier) {
+
+ /**
+ * Process a single CDC microbatch and merge it into the auxiliary and target tables.
+ *
+ * The body is intentionally a thin orchestration of three calls - validate, reconcile,
Review Comment:
nit: comment is a bit overly detailed, non blocking
##########
sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/autocdc/Scd1ForeachBatchHandlerSuite.scala:
##########
@@ -0,0 +1,640 @@
+/*
+ * 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.spark.sql.pipelines.autocdc
+
+import org.scalatest.BeforeAndAfter
+
+import org.apache.spark.SparkFunSuite
+import org.apache.spark.sql.{functions => F, AnalysisException, Row}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.test.SharedSparkSession
+import org.apache.spark.sql.types._
+
+/**
+ * E2E unit tests for the Scd1ForeachBatchHandler class.
+ */
+class Scd1ForeachBatchHandlerSuite
Review Comment:
nit: better to extend QueryTest
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1BatchProcessor.scala:
##########
@@ -212,6 +256,151 @@ case class Scd1BatchProcessor(
)
}
+ /**
+ * Merge the reconciled (deduplicated per key) microbatch onto the auxiliary table,
+ * advancing or deleting existing tombstones and inserting new tombstones for previously
+ * untracked keys.
+ *
+ * After the merge, the auxiliary table has the same schema as before, but with the latest
+ * tombstone data per key.
+ *
+ * @param reconciledMicrobatchDf The deduplicated microbatch.
+ * @param auxiliaryTableIdentifier The identifier (not a [[DataFrame]]) of the auxiliary
Review Comment:
nit: not a [[DataFrame]] not really necessary. Non blocking
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1BatchProcessor.scala:
##########
@@ -37,6 +38,47 @@ case class Scd1BatchProcessor(
changeArgs: ChangeArgs,
resolvedSequencingType: DataType) {
+ /**
+ * Reconcile a CDC microbatch into the canonical form that the auxiliary- and target-table
+ * merges consume. Composes the per-step transforms in the only order that produces correct
+ * SCD1 semantics:
+ *
+ * 1. [[deduplicateMicrobatch]]: collapse same-key events to the latest by sequence.
+ * 2. [[extendMicrobatchRowsWithCdcMetadata]]: project the operational `_cdc_metadata` column
+ * (must run before column selection, which may drop inputs the metadata expressions
+ * reference).
+ * 3. [[projectTargetColumnsOntoMicrobatch]]: apply the user-defined column selection while
+ * preserving the CDC metadata column.
+ * 4. [[applyTombstonesToMicrobatch]]: filter out late-arriving events superseded by
+ * tombstones already recorded in the auxiliary table.
+ *
+ * The per-step methods are kept package-visible so that focused unit tests can pin each
+ * transform's behavior independently, but production callers should always use this entry
+ * point so the ordering is enforced.
+ *
+ * Microbatch validation (orderable sequence, non-null sequence/keys) is a separate concern
+ * handled upstream by [[ScdBatchValidator.validateMicrobatch]] and is intentionally not
+ * folded into this method - it must run before any of these transforms touch the data.
+ *
+ * @param batchDf The validated incoming CDC microbatch.
+ * @param auxiliaryTableDf A snapshot of the auxiliary table for tombstone reconciliation.
+ * Must contain at minimum the key columns + `_cdc_metadata`.
+ * @return The reconciled microbatch, ready to be merged onto both tables.
+ */
+ def reconcileMicrobatch(
Review Comment:
nit: can it be private? we want always validation
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56953][SDP] Implement SCD1 Batch Processor; foreachBatch Callback [spark]
Posted by "AnishMahto (via GitHub)" <gi...@apache.org>.
AnishMahto commented on PR #56016:
URL: https://github.com/apache/spark/pull/56016#issuecomment-4523874120
I left some comments in the code explaining concepts we discussed in these threads. Accepted code suggestions.
By the way in the near future I'm hoping to merge some automated/fuzz testing for idempotency, to provide additional signal for the idempotency argument (and prevent idempotency regression).
The idea would be to compose two different `Scd1ForeachBatchHandler`, one with a stubbed faulty `Scd1BatchProcessor.mergeMicrobatchOntoAuxiliaryTable`, and the one with the regular `Scd1ForeachBatchHandler` implementation. When both handlers run on the same generated microbatch, they should produce the same output (granted the failing handler needs to run twice to recover from failure).
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56953][SDP] Implement SCD1 Batch Processor; foreachBatch Callback [spark]
Posted by "dbtsai (via GitHub)" <gi...@apache.org>.
dbtsai commented on PR #56016:
URL: https://github.com/apache/spark/pull/56016#issuecomment-4530945932
Merged into master, 4.x, 4.2. Thanks.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56953][SDP] Implement SCD1 Batch Processor; foreachBatch Callback [spark]
Posted by "AnishMahto (via GitHub)" <gi...@apache.org>.
AnishMahto commented on code in PR #56016:
URL: https://github.com/apache/spark/pull/56016#discussion_r3291856720
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1ForeachBatchExec.scala:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.spark.sql.pipelines.autocdc
+
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.classic.DataFrame
+
+/**
+ * Exposes an API to execute one SCD Type 1 AutoCDC microbatch reconciliation on a
+ * foreachBatch streaming query.
+ */
+case class Scd1ForeachBatchExec(
+ batchProcessor: Scd1BatchProcessor,
+ auxiliaryTableIdentifier: TableIdentifier,
+ targetTableIdentifier: TableIdentifier) {
+
+ /**
+ * Process a single CDC microbatch and merge it into the auxiliary and target tables.
+ */
+ def execute(batchDf: DataFrame, batchId: Long): Unit = {
+ ScdBatchValidator(
+ destinationIdentifier = targetTableIdentifier,
+ changeArgs = batchProcessor.changeArgs,
+ batchDf = batchDf,
+ batchId = batchId
+ ).validateMicrobatch()
+
+ val deduplicatedMicrobatch = batchProcessor.deduplicateMicrobatch(
+ validatedMicrobatch = batchDf
+ )
+
+ val microbatchWithCdcMetadata = batchProcessor.extendMicrobatchRowsWithCdcMetadata(
+ validatedMicrobatch = deduplicatedMicrobatch
+ )
+
+ val projectedMicrobatch = batchProcessor.projectTargetColumnsOntoMicrobatch(
+ microbatchWithCdcMetadataDf = microbatchWithCdcMetadata
+ )
+
+ val reconciledMicrobatch = batchProcessor.applyTombstonesToMicrobatch(
+ microbatchDf = projectedMicrobatch,
+ auxiliaryTableDf = batchDf.sparkSession.read.table(
+ auxiliaryTableIdentifier.quotedString
+ )
+ )
+
+ batchProcessor.mergeMicrobatchOntoAuxiliaryTable(
+ reconciledMicrobatchDf = reconciledMicrobatch,
+ auxiliaryTableIdentifier = auxiliaryTableIdentifier
+ )
+
+ batchProcessor.mergeMicrobatchOntoTarget(
+ reconciledMicrobatchDf = reconciledMicrobatch,
Review Comment:
Yeah this is still idempotent even if we fail in between the auxiliary table merge and the target merge.
In the auxiliary table merge, we only delete previous tombstones iff they are either be replaced by a newer tombstone or an upsert - in either case the implication is the microbatch must contain a newer event that renders the previous tombstone stale.
Hence on microbatch replay, it doesn't matter whether those (now stale) tombstones are still present in the auxiliary table or not. I'll leave a comment about this in the code.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56953][SDP] Implement SCD1 Batch Processor; foreachBatch Callback [spark]
Posted by "AnishMahto (via GitHub)" <gi...@apache.org>.
AnishMahto commented on code in PR #56016:
URL: https://github.com/apache/spark/pull/56016#discussion_r3291876465
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1ForeachBatchExec.scala:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.spark.sql.pipelines.autocdc
+
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.classic.DataFrame
+
+/**
+ * Exposes an API to execute one SCD Type 1 AutoCDC microbatch reconciliation on a
+ * foreachBatch streaming query.
+ */
+case class Scd1ForeachBatchExec(
+ batchProcessor: Scd1BatchProcessor,
+ auxiliaryTableIdentifier: TableIdentifier,
+ targetTableIdentifier: TableIdentifier) {
+
+ /**
+ * Process a single CDC microbatch and merge it into the auxiliary and target tables.
+ */
+ def execute(batchDf: DataFrame, batchId: Long): Unit = {
+ ScdBatchValidator(
+ destinationIdentifier = targetTableIdentifier,
+ changeArgs = batchProcessor.changeArgs,
+ batchDf = batchDf,
+ batchId = batchId
+ ).validateMicrobatch()
+
+ val deduplicatedMicrobatch = batchProcessor.deduplicateMicrobatch(
+ validatedMicrobatch = batchDf
+ )
+
+ val microbatchWithCdcMetadata = batchProcessor.extendMicrobatchRowsWithCdcMetadata(
+ validatedMicrobatch = deduplicatedMicrobatch
+ )
+
+ val projectedMicrobatch = batchProcessor.projectTargetColumnsOntoMicrobatch(
+ microbatchWithCdcMetadataDf = microbatchWithCdcMetadata
+ )
+
+ val reconciledMicrobatch = batchProcessor.applyTombstonesToMicrobatch(
+ microbatchDf = projectedMicrobatch,
+ auxiliaryTableDf = batchDf.sparkSession.read.table(
Review Comment:
Yep this is a good question. We do indeed expect the auxiliary table to be small, _especially_ for SCD1 - there can be at most one tombstone per key in the universe of possible keys, and if a row is upserted to post deletion, the tombstone is GC'd.
It _is_ worth mentioning if a row is deleted in the upstream source and never touched again (not uncommon, i.e row is permanently deleted), its tombstone will continue to live on indefinitely - necessary for correctness, but there are future paths where we can consider a TTL for tombstone rows to eventually clean them up. This would be a correctness vs time/space efficiency tradeoff.
Anyhow given that we expect the auxiliary table to generally be small, we should expect this join to typically use a broadcast join - should be relatively fast.
That being said I agree there's room for spark engine based optimization such as pruning/clustering for rarer cases where the auxiliary table does grow larger in size. I'll leave a follow up comment.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56953][SDP] Implement SCD1 Batch Processor; foreachBatch Callback [spark]
Posted by "szehon-ho (via GitHub)" <gi...@apache.org>.
szehon-ho commented on PR #56016:
URL: https://github.com/apache/spark/pull/56016#issuecomment-4523610293
**Suggestion: centralize microbatch pipeline ordering on `Scd1BatchProcessor`**
`Scd1ForeachBatchExec.execute` wires a fixed sequence of public `Scd1BatchProcessor` steps. That's clear today, but once this is hooked into a real foreachBatch callback, any caller could invoke those methods out of order (e.g. tombstones before CDC metadata, or merges before projection).
Spark-style library code usually hides that ordering behind a single entry point rather than exposing every step.
For example:
```scala
// Scd1BatchProcessor – public API
def reconcileMicrobatch(
batchDf: DataFrame,
auxiliaryTableDf: DataFrame): DataFrame = { /* validate? */ ... }
// Scd1ForeachBatchExec
def execute(batchDf: DataFrame, batchId: Long): Unit = {
ScdBatchValidator(...).validateMicrobatch()
val reconciled = batchProcessor.reconcileMicrobatch(
batchDf,
batchDf.sparkSession.read.table(auxiliaryTableIdentifier.quotedString))
batchProcessor.mergeMicrobatchOntoAuxiliaryTable(reconciled, auxiliaryTableIdentifier)
batchProcessor.mergeMicrobatchOntoTarget(reconciled, targetTableIdentifier)
}
```
1. Add `reconcileMicrobatch` on `Scd1BatchProcessor` for validate → dedupe → metadata → projection → tombstones (validation can stay in `ScdBatchValidator` and be called from the exec, as above).
2. Keep the existing step methods as `private[autocdc]` for unit tests in `Scd1BatchProcessorSuite` / merge suites.
3. Slim `Scd1ForeachBatchExec` to validate → read aux → `reconcileMicrobatch` → merge aux → merge target.
That preserves testability without exposing a composable-but-unsafe public pipeline API. Minor naming nit: `*Exec` usually means a physical operator in Spark; something like `Scd1ForeachBatchHandler` / `Scd1MicrobatchReconciler` might fit pipelines better if you're open to a rename.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56953][SDP] Implement SCD1 Batch Processor; foreachBatch Callback [spark]
Posted by "szehon-ho (via GitHub)" <gi...@apache.org>.
szehon-ho commented on code in PR #56016:
URL: https://github.com/apache/spark/pull/56016#discussion_r3293354183
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1ForeachBatchHandler.scala:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.spark.sql.pipelines.autocdc
+
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.classic.DataFrame
+
+/**
+ * Exposes an API to execute one SCD Type 1 AutoCDC microbatch reconciliation on a
+ * foreachBatch streaming query.
+ */
+case class Scd1ForeachBatchExec(
+ batchProcessor: Scd1BatchProcessor,
+ auxiliaryTableIdentifier: TableIdentifier,
+ targetTableIdentifier: TableIdentifier) {
+
+ /**
+ * Process a single CDC microbatch and merge it into the auxiliary and target tables.
+ */
+ def execute(batchDf: DataFrame, batchId: Long): Unit = {
+ ScdBatchValidator(
+ destinationIdentifier = targetTableIdentifier,
+ changeArgs = batchProcessor.changeArgs,
+ batchDf = batchDf,
+ batchId = batchId
+ ).validateMicrobatch()
+
+ val deduplicatedMicrobatch = batchProcessor.deduplicateMicrobatch(
+ validatedMicrobatch = batchDf
+ )
+
+ val microbatchWithCdcMetadata = batchProcessor.extendMicrobatchRowsWithCdcMetadata(
+ validatedMicrobatch = deduplicatedMicrobatch
+ )
+
+ val projectedMicrobatch = batchProcessor.projectTargetColumnsOntoMicrobatch(
+ microbatchWithCdcMetadataDf = microbatchWithCdcMetadata
+ )
+
+ val reconciledMicrobatch = batchProcessor.applyTombstonesToMicrobatch(
+ microbatchDf = projectedMicrobatch,
+ auxiliaryTableDf = batchDf.sparkSession.read.table(
Review Comment:
sgtm!
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56953][SDP] Implement SCD1 Batch Processor; foreachBatch Callback [spark]
Posted by "AnishMahto (via GitHub)" <gi...@apache.org>.
AnishMahto commented on code in PR #56016:
URL: https://github.com/apache/spark/pull/56016#discussion_r3291876465
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1ForeachBatchExec.scala:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.spark.sql.pipelines.autocdc
+
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.classic.DataFrame
+
+/**
+ * Exposes an API to execute one SCD Type 1 AutoCDC microbatch reconciliation on a
+ * foreachBatch streaming query.
+ */
+case class Scd1ForeachBatchExec(
+ batchProcessor: Scd1BatchProcessor,
+ auxiliaryTableIdentifier: TableIdentifier,
+ targetTableIdentifier: TableIdentifier) {
+
+ /**
+ * Process a single CDC microbatch and merge it into the auxiliary and target tables.
+ */
+ def execute(batchDf: DataFrame, batchId: Long): Unit = {
+ ScdBatchValidator(
+ destinationIdentifier = targetTableIdentifier,
+ changeArgs = batchProcessor.changeArgs,
+ batchDf = batchDf,
+ batchId = batchId
+ ).validateMicrobatch()
+
+ val deduplicatedMicrobatch = batchProcessor.deduplicateMicrobatch(
+ validatedMicrobatch = batchDf
+ )
+
+ val microbatchWithCdcMetadata = batchProcessor.extendMicrobatchRowsWithCdcMetadata(
+ validatedMicrobatch = deduplicatedMicrobatch
+ )
+
+ val projectedMicrobatch = batchProcessor.projectTargetColumnsOntoMicrobatch(
+ microbatchWithCdcMetadataDf = microbatchWithCdcMetadata
+ )
+
+ val reconciledMicrobatch = batchProcessor.applyTombstonesToMicrobatch(
+ microbatchDf = projectedMicrobatch,
+ auxiliaryTableDf = batchDf.sparkSession.read.table(
Review Comment:
Yep this is a good question. We do indeed expect the auxiliary table to be small, _especially_ for SCD1 - there can be at most one tombstone per key in the universe of possible keys, and if a row is upserted to post deletion, the tombstone is GC'd.
It _is_ worth mentioning if a row is deleted in the upstream source and never touched again (not totally uncommon, i.e row is permanently deleted), its tombstone will continue to live on indefinitely - necessary for correctness, but there are future paths where we can consider a TTL for tombstone rows to eventually clean them up. This would be a correctness vs time/space efficiency tradeoff.
Anyhow given that we expect the auxiliary table to generally be small, we should expect this join to typically use a broadcast join - should be relatively fast.
That being said I agree there's room for spark engine based optimization such as pruning/clustering for rarer cases where the auxiliary table does grow larger in size. I'll leave a follow up comment.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56953][SDP] Implement SCD1 Batch Processor; foreachBatch Callback [spark]
Posted by "AnishMahto (via GitHub)" <gi...@apache.org>.
AnishMahto commented on code in PR #56016:
URL: https://github.com/apache/spark/pull/56016#discussion_r3291876465
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1ForeachBatchExec.scala:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.spark.sql.pipelines.autocdc
+
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.classic.DataFrame
+
+/**
+ * Exposes an API to execute one SCD Type 1 AutoCDC microbatch reconciliation on a
+ * foreachBatch streaming query.
+ */
+case class Scd1ForeachBatchExec(
+ batchProcessor: Scd1BatchProcessor,
+ auxiliaryTableIdentifier: TableIdentifier,
+ targetTableIdentifier: TableIdentifier) {
+
+ /**
+ * Process a single CDC microbatch and merge it into the auxiliary and target tables.
+ */
+ def execute(batchDf: DataFrame, batchId: Long): Unit = {
+ ScdBatchValidator(
+ destinationIdentifier = targetTableIdentifier,
+ changeArgs = batchProcessor.changeArgs,
+ batchDf = batchDf,
+ batchId = batchId
+ ).validateMicrobatch()
+
+ val deduplicatedMicrobatch = batchProcessor.deduplicateMicrobatch(
+ validatedMicrobatch = batchDf
+ )
+
+ val microbatchWithCdcMetadata = batchProcessor.extendMicrobatchRowsWithCdcMetadata(
+ validatedMicrobatch = deduplicatedMicrobatch
+ )
+
+ val projectedMicrobatch = batchProcessor.projectTargetColumnsOntoMicrobatch(
+ microbatchWithCdcMetadataDf = microbatchWithCdcMetadata
+ )
+
+ val reconciledMicrobatch = batchProcessor.applyTombstonesToMicrobatch(
+ microbatchDf = projectedMicrobatch,
+ auxiliaryTableDf = batchDf.sparkSession.read.table(
Review Comment:
Yep this is a good question. We do indeed expect the auxiliary table to be small, _especially_ for SCD1 - there can be at most one tombstone per key in the universe of possible keys, and tombstones are generally expected to be both uncommon (signifies out of order delete) and expected to soon be overtaken by its matching late arriving event, at which point it is deleted.
Given that we expect the auxiliary table to be fairly small, we should generally expect this join to use a broadcast join - should be relatively fast.
That being said I agree there's room for spark engine based optimization such as pruning/clustering for rarer cases where the auxiliary table does grow larger in size. I'll leave a follow up comment.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56953][SDP] Implement SCD1 Batch Processor; foreachBatch Callback [spark]
Posted by "szehon-ho (via GitHub)" <gi...@apache.org>.
szehon-ho commented on code in PR #56016:
URL: https://github.com/apache/spark/pull/56016#discussion_r3293354437
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1ForeachBatchHandler.scala:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.spark.sql.pipelines.autocdc
+
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.classic.DataFrame
+
+/**
+ * Exposes an API to execute one SCD Type 1 AutoCDC microbatch reconciliation on a
+ * foreachBatch streaming query.
+ */
+case class Scd1ForeachBatchExec(
+ batchProcessor: Scd1BatchProcessor,
+ auxiliaryTableIdentifier: TableIdentifier,
+ targetTableIdentifier: TableIdentifier) {
+
+ /**
+ * Process a single CDC microbatch and merge it into the auxiliary and target tables.
+ */
+ def execute(batchDf: DataFrame, batchId: Long): Unit = {
+ ScdBatchValidator(
+ destinationIdentifier = targetTableIdentifier,
+ changeArgs = batchProcessor.changeArgs,
+ batchDf = batchDf,
+ batchId = batchId
+ ).validateMicrobatch()
+
+ val deduplicatedMicrobatch = batchProcessor.deduplicateMicrobatch(
+ validatedMicrobatch = batchDf
+ )
+
+ val microbatchWithCdcMetadata = batchProcessor.extendMicrobatchRowsWithCdcMetadata(
+ validatedMicrobatch = deduplicatedMicrobatch
+ )
+
+ val projectedMicrobatch = batchProcessor.projectTargetColumnsOntoMicrobatch(
+ microbatchWithCdcMetadataDf = microbatchWithCdcMetadata
+ )
+
+ val reconciledMicrobatch = batchProcessor.applyTombstonesToMicrobatch(
+ microbatchDf = projectedMicrobatch,
+ auxiliaryTableDf = batchDf.sparkSession.read.table(
+ auxiliaryTableIdentifier.quotedString
+ )
+ )
+
+ batchProcessor.mergeMicrobatchOntoAuxiliaryTable(
+ reconciledMicrobatchDf = reconciledMicrobatch,
+ auxiliaryTableIdentifier = auxiliaryTableIdentifier
+ )
+
+ batchProcessor.mergeMicrobatchOntoTarget(
+ reconciledMicrobatchDf = reconciledMicrobatch,
Review Comment:
thanks!
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56953][SDP] Implement SCD1 Batch Processor; foreachBatch Callback [spark]
Posted by "szehon-ho (via GitHub)" <gi...@apache.org>.
szehon-ho commented on code in PR #56016:
URL: https://github.com/apache/spark/pull/56016#discussion_r3291826379
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1ForeachBatchExec.scala:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.spark.sql.pipelines.autocdc
+
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.classic.DataFrame
+
+/**
+ * Exposes an API to execute one SCD Type 1 AutoCDC microbatch reconciliation on a
+ * foreachBatch streaming query.
+ */
+case class Scd1ForeachBatchExec(
+ batchProcessor: Scd1BatchProcessor,
+ auxiliaryTableIdentifier: TableIdentifier,
+ targetTableIdentifier: TableIdentifier) {
+
+ /**
+ * Process a single CDC microbatch and merge it into the auxiliary and target tables.
+ */
+ def execute(batchDf: DataFrame, batchId: Long): Unit = {
+ ScdBatchValidator(
+ destinationIdentifier = targetTableIdentifier,
+ changeArgs = batchProcessor.changeArgs,
+ batchDf = batchDf,
+ batchId = batchId
+ ).validateMicrobatch()
+
+ val deduplicatedMicrobatch = batchProcessor.deduplicateMicrobatch(
+ validatedMicrobatch = batchDf
+ )
+
+ val microbatchWithCdcMetadata = batchProcessor.extendMicrobatchRowsWithCdcMetadata(
+ validatedMicrobatch = deduplicatedMicrobatch
+ )
+
+ val projectedMicrobatch = batchProcessor.projectTargetColumnsOntoMicrobatch(
+ microbatchWithCdcMetadataDf = microbatchWithCdcMetadata
+ )
+
+ val reconciledMicrobatch = batchProcessor.applyTombstonesToMicrobatch(
+ microbatchDf = projectedMicrobatch,
+ auxiliaryTableDf = batchDf.sparkSession.read.table(
+ auxiliaryTableIdentifier.quotedString
+ )
+ )
+
+ batchProcessor.mergeMicrobatchOntoAuxiliaryTable(
+ reconciledMicrobatchDf = reconciledMicrobatch,
+ auxiliaryTableIdentifier = auxiliaryTableIdentifier
+ )
+
+ batchProcessor.mergeMicrobatchOntoTarget(
+ reconciledMicrobatchDf = reconciledMicrobatch,
Review Comment:
After `mergeMicrobatchOntoAuxiliaryTable` succeeds, a failure in `mergeMicrobatchOntoTarget` could leave aux updated while the target is not (or vice versa on partial retry, depending on foreachBatch semantics).
What is the expected recovery story here—idempotent replay of the same `batchId`, manual repair, or a future transactional wrapper? Worth a short note in scaladoc or SPIP follow-up?
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1ForeachBatchExec.scala:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.spark.sql.pipelines.autocdc
+
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.classic.DataFrame
+
+/**
+ * Exposes an API to execute one SCD Type 1 AutoCDC microbatch reconciliation on a
+ * foreachBatch streaming query.
+ */
+case class Scd1ForeachBatchExec(
+ batchProcessor: Scd1BatchProcessor,
+ auxiliaryTableIdentifier: TableIdentifier,
+ targetTableIdentifier: TableIdentifier) {
+
+ /**
+ * Process a single CDC microbatch and merge it into the auxiliary and target tables.
+ */
+ def execute(batchDf: DataFrame, batchId: Long): Unit = {
+ ScdBatchValidator(
+ destinationIdentifier = targetTableIdentifier,
+ changeArgs = batchProcessor.changeArgs,
+ batchDf = batchDf,
+ batchId = batchId
+ ).validateMicrobatch()
+
+ val deduplicatedMicrobatch = batchProcessor.deduplicateMicrobatch(
+ validatedMicrobatch = batchDf
+ )
+
+ val microbatchWithCdcMetadata = batchProcessor.extendMicrobatchRowsWithCdcMetadata(
+ validatedMicrobatch = deduplicatedMicrobatch
+ )
+
+ val projectedMicrobatch = batchProcessor.projectTargetColumnsOntoMicrobatch(
+ microbatchWithCdcMetadataDf = microbatchWithCdcMetadata
+ )
+
+ val reconciledMicrobatch = batchProcessor.applyTombstonesToMicrobatch(
+ microbatchDf = projectedMicrobatch,
+ auxiliaryTableDf = batchDf.sparkSession.read.table(
Review Comment:
This reads the full auxiliary table on every microbatch for tombstone application. Is that intentional (aux expected to stay small), or should we plan a follow-up—e.g. project to keys + `__spark_autocdc_metadata` only, or key-pruned reads as the aux table grows?
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org