-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[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
Open
AnishMahto
wants to merge
25
commits into
apache:master
Choose a base branch
from
AnishMahto:SPARK-56870-extend-microbatch-with-cdc-metadata
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
8b08cbe
Introduce ChangeArgs
AnishMahto 202f3a5
linting
AnishMahto 4ac75e7
reorder error condition
AnishMahto 11606c5
PR feedback
AnishMahto d1a38e6
linting
AnishMahto bbe5335
PR feedback
AnishMahto 95ca0e1
buff error message and revert to case class
AnishMahto 481ca9f
test UnqualifiedColumnName('`col`')
AnishMahto 0126659
minor test buff
AnishMahto ac15be5
address PR feedbak
AnishMahto 436ff0a
PR feedback
AnishMahto 875f0b1
Implement deduplicateMicrobatch
AnishMahto 08ea9f4
indenting cleanup
AnishMahto cf3ec82
schema comment
AnishMahto 21d4ffe
casing
AnishMahto 2ff07f4
linting
AnishMahto 76d775d
PR feedback
AnishMahto 8790a2d
use reserved __spark_autocdc* prefix
AnishMahto 5c0c0f8
Add deduplicate test when row contains nested columns
AnishMahto 1a640d1
validation
AnishMahto 88e9c1d
buff scaladoc
AnishMahto fd631ad
use spark resolver
AnishMahto 2a886af
lingint
AnishMahto e8df6f0
rebase conflict
AnishMahto f9c2aed
PR feedback
AnishMahto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
159 changes: 159 additions & 0 deletions
159
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/ChangeArgs.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 |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| /* | ||
| * 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.{AnalysisException, Column} | ||
| import org.apache.spark.sql.catalyst.parser.CatalystSqlParser | ||
| import org.apache.spark.sql.catalyst.util.QuotingUtils | ||
| import org.apache.spark.sql.types.StructType | ||
|
|
||
| /** | ||
| * A single, unqualified column identifier (no nested path or table/alias qualifier). Backticks | ||
| * are consumed: "`a.b`" is stored as "a.b" in [[name]]. Use [[name]] for direct schema-fieldName | ||
| * comparison and [[quoted]] for APIs that re-parse identifier strings. | ||
| */ | ||
| case class UnqualifiedColumnName private (name: String) { | ||
| def quoted: String = QuotingUtils.quoteIdentifier(name) | ||
| } | ||
|
|
||
| object UnqualifiedColumnName { | ||
| def apply(input: String): UnqualifiedColumnName = { | ||
| val nameParts = CatalystSqlParser.parseMultipartIdentifier(input) | ||
| if (nameParts.length != 1) { | ||
| throw multipartColumnIdentifierError(input, nameParts) | ||
| } | ||
| new UnqualifiedColumnName(nameParts.head) | ||
| } | ||
|
|
||
| private def multipartColumnIdentifierError( | ||
| columnName: String, | ||
| nameParts: Seq[String] | ||
| ): AnalysisException = | ||
| new AnalysisException( | ||
| errorClass = "AUTOCDC_MULTIPART_COLUMN_IDENTIFIER", | ||
| messageParameters = Map( | ||
| "columnName" -> columnName, | ||
| "nameParts" -> nameParts.mkString(", ") | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| sealed trait ColumnSelection | ||
| object ColumnSelection { | ||
|
|
||
| case class IncludeColumns(columns: Seq[UnqualifiedColumnName]) extends ColumnSelection | ||
| case class ExcludeColumns(columns: Seq[UnqualifiedColumnName]) | ||
| extends ColumnSelection | ||
|
|
||
| /** | ||
| * Applies [[ColumnSelection]] to a [[StructType]] and returns the filtered schema. Field order | ||
| * follows the original schema; only matching fields are retained in the returned schema. | ||
| * | ||
| * @param schemaName Logical name of the schema being filtered, surfaced in error messages | ||
| * when columns are not found (e.g. "microbatch", "target"). | ||
| * @param schema The schema to filter. | ||
| * @param columnSelection The user-provided selection. `None` is a no-op and returns `schema` | ||
| * unchanged. | ||
| * @param caseSensitive Whether to match column names case-sensitively against the schema. | ||
| * Callers should derive this from the session, e.g. | ||
| * `session.sessionState.conf.caseSensitiveAnalysis`, so column matching | ||
| * stays consistent with `spark.sql.caseSensitive`. | ||
| */ | ||
| def applyToSchema( | ||
| schemaName: String, | ||
| schema: StructType, | ||
| columnSelection: Option[ColumnSelection], | ||
| caseSensitive: Boolean): StructType = columnSelection match { | ||
| case None => | ||
| // A None column selection is interpreted as a no-op. | ||
| schema | ||
| case Some(IncludeColumns(cols)) => | ||
| val keepIndices = lookupFieldIndices(schemaName, schema, cols, caseSensitive) | ||
| StructType(schema.fields.zipWithIndex.collect { | ||
| case (field, idx) if keepIndices.contains(idx) => field | ||
| }) | ||
| case Some(ExcludeColumns(cols)) => | ||
| val dropIndices = lookupFieldIndices(schemaName, schema, cols, caseSensitive) | ||
| StructType(schema.fields.zipWithIndex.collect { | ||
| case (field, idx) if !dropIndices.contains(idx) => field | ||
| }) | ||
| } | ||
|
|
||
| private def lookupFieldIndices( | ||
| schemaName: String, | ||
| schema: StructType, | ||
| fields: Seq[UnqualifiedColumnName], | ||
| caseSensitive: Boolean): Set[Int] = { | ||
| val caseAwareGetFieldIndex: String => Option[Int] = | ||
| if (caseSensitive) schema.getFieldIndex else schema.getFieldIndexCaseInsensitive | ||
|
|
||
| val fieldIndexResolutions = fields.map(f => f -> caseAwareGetFieldIndex(f.name)) | ||
| val missingFieldNames = fieldIndexResolutions.collect { case (f, None) => f.name }.distinct | ||
| if (missingFieldNames.nonEmpty) { | ||
| throw new AnalysisException( | ||
| errorClass = "AUTOCDC_COLUMNS_NOT_FOUND_IN_SCHEMA", | ||
| messageParameters = Map( | ||
| "caseSensitivity" -> CaseSensitivityLabels.of(caseSensitive), | ||
| "schemaName" -> schemaName, | ||
| "missingColumns" -> missingFieldNames.mkString(", "), | ||
| "availableColumns" -> schema.fieldNames.mkString(", ") | ||
| ) | ||
| ) | ||
| } | ||
| fieldIndexResolutions.flatMap { case (_, idx) => idx }.toSet | ||
| } | ||
| } | ||
|
|
||
| /** User-facing case-sensitivity labels surfaced in AutoCDC error messages. */ | ||
| private[autocdc] object CaseSensitivityLabels { | ||
| val CaseSensitive: String = "case-sensitive" | ||
| val CaseInsensitive: String = "case-insensitive" | ||
|
|
||
| def of(caseSensitive: Boolean): String = | ||
| if (caseSensitive) CaseSensitive else CaseInsensitive | ||
| } | ||
|
|
||
| /** The SCD (Slowly Changing Dimension) strategy for a CDC flow. */ | ||
| sealed trait ScdType | ||
|
|
||
| object ScdType { | ||
| /** Representation for the standard SCD1 strategy. */ | ||
| case object Type1 extends ScdType | ||
| /** Representation for the standard SCD2 strategy. */ | ||
| case object Type2 extends ScdType | ||
| } | ||
|
|
||
| /** | ||
| * Configuration for an AutoCDC flow. | ||
| * | ||
| * @param keys The column(s) that uniquely identify a row in the source data. | ||
| * @param sequencing Expression ordering CDC events to correctly resolve out-of-order | ||
| * arrivals. Must be a sortable type. | ||
| * @param deleteCondition Expression that marks a source row as a DELETE. When None, all | ||
| * rows are treated as upserts. | ||
| * @param storedAsScdType The SCD strategy these args should be applied to. | ||
| * @param columnSelection Which source columns to select in the target table. None means | ||
| * all columns. | ||
| */ | ||
| case class ChangeArgs( | ||
| keys: Seq[UnqualifiedColumnName], | ||
| sequencing: Column, | ||
| storedAsScdType: ScdType, | ||
| deleteCondition: Option[Column] = None, | ||
| columnSelection: Option[ColumnSelection] = None | ||
| ) |
175 changes: 175 additions & 0 deletions
175
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 |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| /* | ||
| * 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.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, | ||
| resolvedSequencingType: DataType) { | ||
|
|
||
| /** | ||
| * Deduplicate the incoming CDC microbatch by key, keeping the most recent event per key | ||
| * as ordered by [[ChangeArgs.sequencing]]. | ||
| * | ||
| * For SCD1 we only care about the most recent (by sequence value) event per key. When | ||
| * multiple events share the same key and the same sequence value, the row selected is | ||
| * non-deterministic and undefined. | ||
| * | ||
| * @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 schema of the returned dataframe matches the schema of the microbatch exactly. | ||
| */ | ||
| def deduplicateMicrobatch(validatedMicrobatch: DataFrame): DataFrame = { | ||
| // The `max_by` API can only return a single column, so pack/unpack the entire row into a | ||
| // temporary column before and after the `max_by` operation. | ||
| val winningRowCol = Scd1BatchProcessor.winningRowColName | ||
|
|
||
| val allMicrobatchColumns = | ||
| validatedMicrobatch.columns | ||
| .map(colName => F.col(QuotingUtils.quoteIdentifier(colName))) | ||
| .toImmutableArraySeq | ||
|
|
||
| validatedMicrobatch | ||
| .groupBy(changeArgs.keys.map(k => F.col(k.quoted)): _*) | ||
| .agg( | ||
| F.max_by(F.struct(allMicrobatchColumns: _*), changeArgs.sequencing) | ||
| .as(winningRowCol) | ||
| ) | ||
| .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. | ||
| * | ||
| * The returned dataframe has all of the columns in the input microbatch + the CDC metadata | ||
| * column. | ||
| */ | ||
| def extendMicrobatchRowsWithCdcMetadata(microbatchDf: DataFrame): DataFrame = { | ||
|
AnishMahto marked this conversation as resolved.
|
||
| // Proactively validate the reserved CDC metadata column does not exist in the microbatch. | ||
| validateCdcMetadataColumnNotPresent(microbatchDf) | ||
|
|
||
| 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.
|
||
|
|
||
| microbatchDf.withColumn( | ||
| Scd1BatchProcessor.cdcMetadataColName, | ||
| Scd1BatchProcessor.constructCdcMetadataCol( | ||
| deleteSequence = rowDeleteSequence, | ||
| upsertSequence = rowUpsertSequence, | ||
| sequencingType = resolvedSequencingType | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| private def validateCdcMetadataColumnNotPresent(microbatchDf: DataFrame): Unit = { | ||
| val microbatchSqlConf = microbatchDf.sparkSession.sessionState.conf | ||
| val resolver = microbatchSqlConf.resolver | ||
|
|
||
| microbatchDf.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: 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.
|
||
| 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.
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.