Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 29.4k
[SPARK-56870][SDP] Implement SCD1 Batch Processor; Extend Microbatch with CDC Metadata#55970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
AnishMahto
wants to merge
9
commits into
apache:master
from
AnishMahto:SPARK-56870-extend-microbatch-with-cdc-metadata
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
88aa032
validation
AnishMahto f8d2690
buff scaladoc
AnishMahto 27aa75b
use spark resolver
AnishMahto c155259
lingint
AnishMahto ca4e971
rebase conflict
AnishMahto 801a8b7
PR feedback
AnishMahto c36f910
rebase conflicts
AnishMahto 25387c3
PR feedback
AnishMahto be4b09c
scalalint LOCALE
AnishMahto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 115 additions & 3 deletions
118 sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1BatchProcessor.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -17,16 +17,26 @@ | ||
| package org.apache.spark.sql.pipelines.autocdc | ||
| import org.apache.spark.sql.{functions => F} | ||
| import org.apache.spark.SparkException | ||
| import org.apache.spark.sql.{functions => F, AnalysisException} | ||
| import org.apache.spark.sql.Column | ||
| import org.apache.spark.sql.catalyst.util.QuotingUtils | ||
| import org.apache.spark.sql.classic.DataFrame | ||
| import org.apache.spark.sql.types.{DataType, StructField, StructType} | ||
| import org.apache.spark.util.ArrayImplicits._ | ||
| /** | ||
| * Per-microbatch processor for SCD Type 1 AutoCDC flows, complying to the specified [[changeArgs]] | ||
| * configuration. | ||
| * | ||
| * @param changeArgs The CDC flow configuration. | ||
| * @param resolvedSequencingType The post-analysis [[DataType]] of the sequencing column, derived | ||
| * from the flow's resolved DataFrame at flow setup time. | ||
| */ | ||
| case class Scd1BatchProcessor(changeArgs: ChangeArgs) { | ||
| case class Scd1BatchProcessor( | ||
| changeArgs: ChangeArgs, | ||
| resolvedSequencingType: DataType) { | ||
| /** | ||
| * Deduplicate the incoming CDC microbatch by key, keeping the most recent event per key | ||
| * as ordered by [[ChangeArgs.sequencing]]. | ||
| @@ -59,9 +69,111 @@ case class Scd1BatchProcessor(changeArgs: ChangeArgs) { | ||
| ) | ||
| .select(F.col(s"$winningRowCol.*")) | ||
| } | ||
| /** | ||
| * Project the CDC metadata column onto the microbatch. | ||
| * | ||
| * This must run before any column selection is applied to the microbatch. The | ||
| * [[ChangeArgs.deleteCondition]] and [[ChangeArgs.sequencing]] expressions are evaluated against | ||
| * the current microbatch schema, and column selection may drop inputs required by those | ||
| * expressions. | ||
| * | ||
| * Rows are classified as deletes only when [[ChangeArgs.deleteCondition]] evaluates to true. A | ||
| * false or null delete condition classifies the row as an upsert. | ||
| * | ||
| * @param validatedMicrobatch A microbatch that has already been validated such that the | ||
| * sequencing column should not contain null values, and its data type | ||
| * should support ordering. | ||
| * | ||
| * The returned dataframe has all of the columns in the input microbatch + the CDC metadata | ||
| * column. | ||
| */ | ||
| def extendMicrobatchRowsWithCdcMetadata(validatedMicrobatch: DataFrame): DataFrame = { | ||
| // Proactively validate the reserved CDC metadata column does not exist in the microbatch. | ||
| validateCdcMetadataColumnNotPresent(validatedMicrobatch) | ||
| val rowDeleteSequence: Column = changeArgs.deleteCondition match { | ||
| case Some(deleteCondition) => | ||
| F.when(deleteCondition, changeArgs.sequencing).otherwise(F.lit(null)) | ||
| case None => | ||
| F.lit(null) | ||
| } | ||
| val rowUpsertSequence: Column = | ||
| // A row that is not a delete must be an upsert, these are mutually exclusive and a complete | ||
| // set of CDC event types. | ||
| F.when(rowDeleteSequence.isNull, changeArgs.sequencing).otherwise(F.lit(null)) | ||
AnishMahto marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| validatedMicrobatch.withColumn( | ||
| Scd1BatchProcessor.cdcMetadataColName, | ||
| Scd1BatchProcessor.constructCdcMetadataCol( | ||
| deleteSequence = rowDeleteSequence, | ||
| upsertSequence = rowUpsertSequence, | ||
| sequencingType = resolvedSequencingType | ||
| ) | ||
| ) | ||
| } | ||
| private def validateCdcMetadataColumnNotPresent(microbatch: DataFrame): Unit = { | ||
| val microbatchSqlConf = microbatch.sparkSession.sessionState.conf | ||
| val resolver = microbatchSqlConf.resolver | ||
| microbatch.schema.fieldNames | ||
| .find(resolver(_, Scd1BatchProcessor.cdcMetadataColName)) | ||
| .foreach { conflictingColumnName => | ||
| throw new AnalysisException( | ||
| errorClass = "AUTOCDC_RESERVED_COLUMN_NAME_CONFLICT", | ||
| messageParameters = Map( | ||
| "caseSensitivity" -> CaseSensitivityLabels.of(microbatchSqlConf.caseSensitiveAnalysis), | ||
| "columnName" -> conflictingColumnName, | ||
| "schemaName" -> "microbatch", | ||
| "reservedColumnName" -> Scd1BatchProcessor.cdcMetadataColName | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| object Scd1BatchProcessor { | ||
| // Columns prefixed with `__spark_autocdc_` are reserved for internal SDP AutoCDC processing. | ||
| private[autocdc] val winningRowColName = "__spark_autocdc_winning_row" | ||
| private[autocdc] val winningRowColName: String = "__spark_autocdc_winning_row" | ||
| private[autocdc] val cdcMetadataColName: String = "__spark_autocdc_metadata" | ||
| private[autocdc] val cdcDeleteSequenceFieldName: String = "deleteSequence" | ||
AnishMahto marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| private[autocdc] val cdcUpsertSequenceFieldName: String = "upsertSequence" | ||
| /** | ||
| * Schema of the CDC metadata struct column for SCD1. | ||
| */ | ||
| private def cdcMetadataColSchema(sequencingType: DataType): StructType = | ||
| StructType( | ||
| Seq( | ||
| // The sequencing of the event if it represents a delete, null otherwise. | ||
| StructField(cdcDeleteSequenceFieldName, sequencingType, nullable = true), | ||
| // The sequencing of the event if it represents an upsert, null otherwise. | ||
| StructField(cdcUpsertSequenceFieldName, sequencingType, nullable = true) | ||
| ) | ||
| ) | ||
| /** | ||
| * Construct the CDC metadata struct column for SCD1, following the exact schema and field | ||
| * ordering defined by [[cdcMetadataColSchema]]. | ||
| */ | ||
| private[autocdc] def constructCdcMetadataCol( | ||
| deleteSequence: Column, | ||
| upsertSequence: Column, | ||
| sequencingType: DataType): Column = { | ||
| val cdcMetadataFieldsInOrder = cdcMetadataColSchema(sequencingType).fields.map { field => | ||
| val value = field.name match { | ||
| case `cdcDeleteSequenceFieldName` => deleteSequence | ||
| case `cdcUpsertSequenceFieldName` => upsertSequence | ||
| case other => | ||
| throw SparkException.internalError( | ||
| s"Unable to construct SCD1 CDC metadata column due to unknown `${other}` field." | ||
| ) | ||
| } | ||
| value.cast(field.dataType).as(field.name) | ||
| } | ||
| F.struct(cdcMetadataFieldsInOrder.toImmutableArraySeq: _*) | ||
| } | ||
| } | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.