Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions common/utils/src/main/resources/error/error-conditions.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@
],
"sqlState" : "42703"
},
"AUTOCDC_RESERVED_COLUMN_NAME_CONFLICT" : {
"message" : [
"Using <caseSensitivity> column name comparison, the column `<columnName>` in the <schemaName> schema conflicts with the reserved AutoCDC column name `<reservedColumnName>`. Rename or remove the column."
],
"sqlState" : "42710"
},
"AVRO_CANNOT_WRITE_NULL_FIELD" : {
"message" : [
"Cannot write null value for field <name> defined as non-null Avro data type <dataType>.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]].
Expand Down Expand Up @@ -59,9 +69,163 @@ 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))

validatedMicrobatch.withColumn(
Scd1BatchProcessor.cdcMetadataColName,
Scd1BatchProcessor.constructCdcMetadataCol(
deleteSequence = rowDeleteSequence,
upsertSequence = rowUpsertSequence,
sequencingType = resolvedSequencingType
)
)
}

/**
* Project the user-defined column selection onto the microbatch. By this point the input
* microbatch should already have projected its CDC metadata, because it's possible that the
* user-defined column selection drops columns that are otherwise necessary to compute the
* CDC metadata.
*
* Returned dataframe's schema is: all of the user-selected columns in the input dataframe as per
* [[ChangeArgs.columnSelection]] + the CDC metadata column.
*/
def projectTargetColumnsOntoMicrobatch(microbatchWithCdcMetadataDf: DataFrame): DataFrame = {
val caseSensitiveColumnComparison =
microbatchWithCdcMetadataDf.sparkSession.sessionState.conf.caseSensitiveAnalysis

// The user schema is the microbatch schema after dropping the system CDC metadata column.
// We project out the system column before applying user selection and project it back in
// afterwards, so that users cannot control whether this [necessary] column shows up in the
// target table.
val userColumnsInMicrobatchSchema = ColumnSelection.applyToSchema(
schemaName = "microbatch",
schema = microbatchWithCdcMetadataDf.schema,
columnSelection = Some(
ColumnSelection.ExcludeColumns(
Seq(UnqualifiedColumnName(Scd1BatchProcessor.cdcMetadataColName))
)
),
caseSensitive = caseSensitiveColumnComparison
)

val userSelectedColumnsInMicrobatchSchema =
ColumnSelection.applyToSchema(
schemaName = "microbatch",
schema = userColumnsInMicrobatchSchema,
columnSelection = changeArgs.columnSelection,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: columnSelection can remove key columns (e.g. ExcludeColumns on a key, or a narrow IncludeColumns that omits keys). Will a later merge step still need those columns on this DataFrame?

If keys must remain until after merge, we should validate here (or when constructing ChangeArgs) that changeArgs.keys are not dropped. If merge runs before projection, or keys are re-injected elsewhere, could you add a brief note in the scaladoc on the expected pipeline order?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep we do require all keys remain in the column selection.

I added that validation in this PR during flow analysis time (well before flow execution, which is when this would actually be called) - see requireKeysPresentInSelectedSchema.

Flow analysis must always be done before flow execution, so there's no need to do additional user-friendly validation in this internal flow execution step. For unit testing purposes if a test is incorrectly setup, spark will just throw an unresolved column exception.

caseSensitive = caseSensitiveColumnComparison
)

// In addition to the explicit user-selected columns, re-project the operational CDC metadata
// column as the last column.
val finalColumnsInMicrobatchToSelect =
userSelectedColumnsInMicrobatchSchema.fieldNames.map(colName => {
// Spark drops backticks in the schema, quote all identifiers for safety before executing
// select. Identifiers could have special characters such as '.'.
F.col(QuotingUtils.quoteIdentifier(colName))
}) :+ F.col(
Scd1BatchProcessor.cdcMetadataColName
)

microbatchWithCdcMetadataDf.select(
finalColumnsInMicrobatchToSelect.toImmutableArraySeq: _*
)
}

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"
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: _*)
}
}
Loading