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
18 changes: 18 additions & 0 deletions common/utils/src/main/resources/error/error-conditions.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,30 @@
],
"sqlState" : "42703"
},
"AUTOCDC_EMPTY_KEYS" : {
"message" : [
"AutoCDC requires at least one key column to identify rows, but received an empty key set."
],
"sqlState" : "22023"
},
"AUTOCDC_MULTIPART_COLUMN_IDENTIFIER" : {
"message" : [
"Expected a single column identifier; got the multi-part identifier <columnName> (parts: <nameParts>)."
],
"sqlState" : "42703"
},
"AUTOCDC_KEY_NOT_IN_SELECTED_SCHEMA" : {
"message" : [
"Using <caseSensitivity> column name comparison, the AutoCDC key column `<keyColumnName>` is not present in the flow's selected source schema. AutoCDC requires every key column to be present in the source change-data feed and retained by any configured column selection."
],
"sqlState" : "22023"
},
"AUTOCDC_RESERVED_COLUMN_NAME_PREFIX_CONFLICT" : {
"message" : [
"The column `<columnName>` in the <schemaName> schema collides with the reserved AutoCDC column name prefix `<reservedColumnNamePrefix>` (using <caseSensitivity> column name comparison). 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 @@ -120,7 +120,7 @@ object ColumnSelection {
}

/** User-facing case-sensitivity labels surfaced in AutoCDC error messages. */
private[autocdc] object CaseSensitivityLabels {
private[pipelines] object CaseSensitivityLabels {
val CaseSensitive: String = "case-sensitive"
val CaseInsensitive: String = "case-insensitive"

Expand Down Expand Up @@ -156,4 +156,22 @@ case class ChangeArgs(
storedAsScdType: ScdType,
deleteCondition: Option[Column] = None,
columnSelection: Option[ColumnSelection] = None
)
) {
ChangeArgs.validateNonEmptyKeys(keys)
}

object ChangeArgs {
/**
* Validates that [[ChangeArgs.keys]] is non-empty. Both SCD1 and SCD2 semantics require at
* least one key column to identify rows; rejecting empty key sets at construction lets
* downstream consumers rely on `keys.nonEmpty` without re-validating.
*/
private def validateNonEmptyKeys(keys: Seq[UnqualifiedColumnName]): Unit = {
if (keys.isEmpty) {
throw new AnalysisException(
errorClass = "AUTOCDC_EMPTY_KEYS",
messageParameters = Map.empty
)
}
}
}
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.SparkException
import org.apache.spark.sql.{functions => F}
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 = {
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))

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

object Scd1BatchProcessor {
/**
* Reserved column-name prefix for internal SDP AutoCDC processing. Source change-data-feed
* dataframes must not contain any columns starting with this prefix; the invariant is
* enforced at [[org.apache.spark.sql.pipelines.graph.AutoCdcMergeFlow]] construction.
*/
private[pipelines] val reservedColumnNamePrefix: String = "__spark_autocdc_"

private[autocdc] val winningRowColName: String = s"${reservedColumnNamePrefix}winning_row"
private[pipelines] val cdcMetadataColName: String = s"${reservedColumnNamePrefix}metadata"

private[autocdc] val cdcDeleteSequenceFieldName: String = "deleteSequence"
private[autocdc] val cdcUpsertSequenceFieldName: String = "upsertSequence"

/**
* Schema of the CDC metadata struct column for SCD1.
*/
private[pipelines] 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: _*)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private class FlowResolver(rawGraph: DataflowGraph) {
} else {
f
}
convertResolvedToTypedFlow(flowToResolve, maybeNewFuncResult)
transformUnresolvedFlowToResolvedFlow(flowToResolve, maybeNewFuncResult)

// If the flow failed due to an UnresolvedDatasetException, it means that one of the
// flow's inputs wasn't available. After other flows are resolved, these inputs
Expand All @@ -199,9 +199,18 @@ private class FlowResolver(rawGraph: DataflowGraph) {
}
}

private def convertResolvedToTypedFlow(
private def transformUnresolvedFlowToResolvedFlow(
flow: UnresolvedFlow,
funcResult: FlowFunctionResult): ResolvedFlow = {
flow match {
case acf: AutoCdcFlow => new AutoCdcMergeFlow(acf, funcResult)
case utf: UntypedFlow => transformUntypedFlowToResolvedFlow(utf, funcResult)
}
}

private def transformUntypedFlowToResolvedFlow(
flow: UntypedFlow,
funcResult: FlowFunctionResult): ResolvedFlow = {
flow match {
case _ if flow.once => new AppendOnceFlow(flow, funcResult)
case _ if funcResult.dataFrame.get.isStreaming =>
Expand All @@ -210,7 +219,7 @@ private class FlowResolver(rawGraph: DataflowGraph) {
// then get their results overwritten.
val mustBeAppend = rawGraph.flowsTo(flow.destinationIdentifier).size > 1
new StreamingFlow(flow, funcResult, mustBeAppend = mustBeAppend)
case _: UnresolvedFlow => new CompleteFlow(flow, funcResult)
case _ => new CompleteFlow(flow, funcResult)
}
}
}
Loading