From 5a2b7bc63f96359b410568f8006a303ba8b27f2b Mon Sep 17 00:00:00 2001 From: Anish Mahto Date: Thu, 21 May 2026 01:26:59 +0000 Subject: [PATCH 1/7] Introduce AutoCdcFlow and AutoCdcMergeFlow --- .../resources/error/error-conditions.json | 10 +- .../sql/pipelines/autocdc/ChangeArgs.scala | 2 +- .../autocdc/Scd1BatchProcessor.scala | 38 +- .../spark/sql/pipelines/graph/Flow.scala | 160 ++++- .../graph/GraphRegistrationContext.scala | 2 +- .../graph/SqlGraphRegistrationContext.scala | 10 +- .../pipelines/autocdc/AutoCdcFlowSuite.scala | 550 ++++++++++++++++++ .../autocdc/Scd1BatchProcessorSuite.scala | 77 --- .../utils/TestGraphRegistrationContext.scala | 8 +- 9 files changed, 737 insertions(+), 120 deletions(-) create mode 100644 sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/autocdc/AutoCdcFlowSuite.scala diff --git a/common/utils/src/main/resources/error/error-conditions.json b/common/utils/src/main/resources/error/error-conditions.json index 9274c17500a75..d67a7ec5af01a 100644 --- a/common/utils/src/main/resources/error/error-conditions.json +++ b/common/utils/src/main/resources/error/error-conditions.json @@ -209,9 +209,15 @@ ], "sqlState" : "42703" }, - "AUTOCDC_RESERVED_COLUMN_NAME_CONFLICT" : { + "AUTOCDC_KEY_NOT_IN_SELECTED_SCHEMA" : { "message" : [ - "Using column name comparison, the column `` in the schema conflicts with the reserved AutoCDC column name ``. Rename or remove the column." + "Using column name comparison, the AutoCDC key column `` 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 `` in the schema collides with the reserved AutoCDC column name prefix `` (using column name comparison). Rename or remove the column." ], "sqlState" : "42710" }, diff --git a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/ChangeArgs.scala b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/ChangeArgs.scala index c17c89967baa5..b975e06807f57 100644 --- a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/ChangeArgs.scala +++ b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/ChangeArgs.scala @@ -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" diff --git a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1BatchProcessor.scala b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1BatchProcessor.scala index 03aaf284f070f..89242d400bb41 100644 --- a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1BatchProcessor.scala +++ b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1BatchProcessor.scala @@ -18,7 +18,7 @@ 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.{functions => F} import org.apache.spark.sql.Column import org.apache.spark.sql.catalyst.util.QuotingUtils import org.apache.spark.sql.classic.DataFrame @@ -89,9 +89,6 @@ case class Scd1BatchProcessor( * 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)) @@ -165,31 +162,18 @@ case class Scd1BatchProcessor( 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: String = "__spark_autocdc_winning_row" - private[autocdc] val cdcMetadataColName: String = "__spark_autocdc_metadata" + /** + * 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" @@ -197,7 +181,7 @@ object Scd1BatchProcessor { /** * Schema of the CDC metadata struct column for SCD1. */ - private def cdcMetadataColSchema(sequencingType: DataType): StructType = + private[pipelines] def cdcMetadataColSchema(sequencingType: DataType): StructType = StructType( Seq( // The sequencing of the event if it represents a delete, null otherwise. diff --git a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala index e329308502f0d..9cc8817e70891 100644 --- a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala +++ b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala @@ -20,11 +20,19 @@ package org.apache.spark.sql.pipelines.graph import scala.util.Try import org.apache.spark.internal.Logging +import org.apache.spark.sql.AnalysisException import org.apache.spark.sql.catalyst.{AliasIdentifier, TableIdentifier} import org.apache.spark.sql.classic.DataFrame import org.apache.spark.sql.pipelines.AnalysisWarning +import org.apache.spark.sql.pipelines.autocdc.{ + CaseSensitivityLabels, + ChangeArgs, + ColumnSelection, + Scd1BatchProcessor, + ScdType +} import org.apache.spark.sql.pipelines.util.InputReadOptions -import org.apache.spark.sql.types.StructType +import org.apache.spark.sql.types.{StructField, StructType} /** * Contains the catalog and database context information for query execution. @@ -121,7 +129,21 @@ case class FlowFunctionResult( } /** A [[Flow]] whose output schema and dependencies aren't known. */ -case class UnresolvedFlow( +sealed trait UnresolvedFlow extends Flow { + /** Returns a copy of this flow with the given SQL confs overriding the existing ones. */ + def withSqlConf(newSqlConf: Map[String, String]): UnresolvedFlow +} + +/** + * An [[UnresolvedFlow]] whose execution-type has not yet been determined. + * + * In some cases, we know the execution-type for an [[UnresolvedFlow]] even before flow analysis + * and resolution. For example an AutoCDCFlow is a special unresolved-but-typed flow; we know a + * flow will be an AutoCDC flow immediately on construction, because it has its own special + * registration API. Such flows are considered "typed flows", but there isn't any semantic reason + * yet to explicitly introduce a `TypedFlow` trait/class. + */ +case class UntypedFlow( identifier: TableIdentifier, destinationIdentifier: TableIdentifier, func: FlowFunction, @@ -129,7 +151,34 @@ case class UnresolvedFlow( sqlConf: Map[String, String], override val once: Boolean, override val origin: QueryOrigin -) extends Flow +) extends UnresolvedFlow { + override def withSqlConf(newSqlConf: Map[String, String]): UntypedFlow = + copy(sqlConf = newSqlConf) +} + +/** + * An unresolved but typed that applies a CDC event stream to a target table via MERGE. + * + * [[AutoCdcFlow]] is a typed flow because it is only supported for streaming, and not as a once + * flow. Therefore by definition it is a streaming-type flow. + * + * In the future once-support for [[AutoCdcFlow]] may be added. + */ +case class AutoCdcFlow( + identifier: TableIdentifier, + destinationIdentifier: TableIdentifier, + func: FlowFunction, + queryContext: QueryContext, + sqlConf: Map[String, String] = Map.empty, + comment: Option[String] = None, + override val origin: QueryOrigin, + changeArgs: ChangeArgs +) extends UnresolvedFlow { + override val once: Boolean = false + + override def withSqlConf(newSqlConf: Map[String, String]): AutoCdcFlow = + copy(sqlConf = newSqlConf) +} /** * A [[Flow]] whose flow function has been invoked, meaning either: @@ -194,3 +243,108 @@ class AppendOnceFlow( override val once = true } + +/** + * A resolved flow that applies a CDC event stream to a target table via MERGE, in accordance to + * the configured [[flow.changeArgs]]. + */ +class AutoCdcMergeFlow( + val flow: AutoCdcFlow, + val funcResult: FlowFunctionResult +) extends ResolvedFlow { + requireReservedPrefixAbsentInSourceColumns() + + def changeArgs: ChangeArgs = flow.changeArgs + + /** + * Returns the augmented output schema of this flow, which can differ from the schema of the + * source change-data-feed dataframe. + * + * The source dataframe's schema describes the incoming CDC events; the augmented schema here + * applies the user-specified [[ColumnSelection]] and appends the SCD-specific metadata + * columns that the AutoCDC MERGE engine projects onto the target table. Downstream + * dependencies in the pipeline see this augmented schema. + */ + override val schema: StructType = { + val userSelectedSchema = ColumnSelection.applyToSchema( + schemaName = "changeDataFeed", + schema = df.schema, + columnSelection = changeArgs.columnSelection, + caseSensitive = spark.sessionState.conf.caseSensitiveAnalysis + ) + + // AutoCDC flows require all key columns to be present in the target table, to adhere to SCD + // semantics. + requireKeysPresentInSelectedSchema(userSelectedSchema) + + changeArgs.storedAsScdType match { + case ScdType.Type1 => + // SCD1 produces a target table with all the user-selected output columns and a projected + // CDC operational metadata column at the end. + StructType( + userSelectedSchema.fields :+ StructField( + Scd1BatchProcessor.cdcMetadataColName, + Scd1BatchProcessor.cdcMetadataColSchema( + sequencingType = df.select(changeArgs.sequencing).schema.head.dataType + ), + nullable = false + ) + ) + case ScdType.Type2 => + throw new UnsupportedOperationException( + "AutoCDC flows do not currently support SCD Type 2 transformations." + ) + } + } + + /** + * Validate that the resolved source dataframe for the AutoCDC flow does not contain any column + * names that use the reserved Spark AutoCDC prefix. + */ + private def requireReservedPrefixAbsentInSourceColumns(): Unit = { + val resolver = spark.sessionState.conf.resolver + val reservedPrefix = Scd1BatchProcessor.reservedColumnNamePrefix + + def nameContainsReservedPrefix(name: String): Boolean = { + name.length >= reservedPrefix.length && resolver( + name.substring(0, reservedPrefix.length), + reservedPrefix + ) + } + + df.schema.fieldNames.find(nameContainsReservedPrefix).foreach { conflictingColumnName => + throw new AnalysisException( + errorClass = "AUTOCDC_RESERVED_COLUMN_NAME_PREFIX_CONFLICT", + messageParameters = Map( + "caseSensitivity" -> CaseSensitivityLabels.of( + spark.sessionState.conf.caseSensitiveAnalysis + ), + "columnName" -> conflictingColumnName, + "schemaName" -> "changeDataFeed", + "reservedColumnNamePrefix" -> reservedPrefix + ) + ) + } + } + + /** + * Validate all keys specified in changeArgs are actually present in the user-selected schema. + */ + private def requireKeysPresentInSelectedSchema(selectedSchema: StructType): Unit = { + val resolver = spark.sessionState.conf.resolver + + changeArgs.keys + .find(key => !selectedSchema.fieldNames.exists(name => resolver(name, key.name))) + .foreach { missingKey => + throw new AnalysisException( + errorClass = "AUTOCDC_KEY_NOT_IN_SELECTED_SCHEMA", + messageParameters = Map( + "caseSensitivity" -> CaseSensitivityLabels.of( + spark.sessionState.conf.caseSensitiveAnalysis + ), + "keyColumnName" -> missingKey.name + ) + ) + } + } +} diff --git a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/GraphRegistrationContext.scala b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/GraphRegistrationContext.scala index dadda0561b19f..970fdb4b70e94 100644 --- a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/GraphRegistrationContext.scala +++ b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/GraphRegistrationContext.scala @@ -59,7 +59,7 @@ class GraphRegistrationContext( } def registerFlow(flowDef: UnresolvedFlow): Unit = { - flows += flowDef.copy(sqlConf = defaultSqlConf ++ flowDef.sqlConf) + flows += flowDef.withSqlConf(defaultSqlConf ++ flowDef.sqlConf) } private def isEmpty: Boolean = { diff --git a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/SqlGraphRegistrationContext.scala b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/SqlGraphRegistrationContext.scala index 829179142dc5c..4dfd096935781 100644 --- a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/SqlGraphRegistrationContext.scala +++ b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/SqlGraphRegistrationContext.scala @@ -237,7 +237,7 @@ class SqlGraphRegistrationContext( // Register flow that backs this streaming table. graphRegistrationContext.registerFlow( - UnresolvedFlow( + UntypedFlow( identifier = stIdentifier, destinationIdentifier = stIdentifier, func = FlowAnalysis.createFlowFunctionFromLogicalPlan(cst.query), @@ -288,7 +288,7 @@ class SqlGraphRegistrationContext( // Register flow that backs this materialized view. graphRegistrationContext.registerFlow( - UnresolvedFlow( + UntypedFlow( identifier = mvIdentifier, destinationIdentifier = mvIdentifier, func = FlowAnalysis.createFlowFunctionFromLogicalPlan(cmv.query), @@ -331,7 +331,7 @@ class SqlGraphRegistrationContext( // Register flow that backs this persisted view. graphRegistrationContext.registerFlow( - UnresolvedFlow( + UntypedFlow( identifier = viewIdentifier, destinationIdentifier = viewIdentifier, func = FlowAnalysis.createFlowFunctionFromLogicalPlan(cv.query), @@ -375,7 +375,7 @@ class SqlGraphRegistrationContext( // Register flow definition that backs this temporary view. graphRegistrationContext.registerFlow( - UnresolvedFlow( + UntypedFlow( identifier = viewIdentifier, destinationIdentifier = viewIdentifier, func = FlowAnalysis.createFlowFunctionFromLogicalPlan(cvc.plan), @@ -451,7 +451,7 @@ class SqlGraphRegistrationContext( .identifier graphRegistrationContext.registerFlow( - UnresolvedFlow( + UntypedFlow( identifier = flowIdentifier, destinationIdentifier = qualifiedDestinationIdentifier, func = FlowAnalysis.createFlowFunctionFromLogicalPlan(flowQueryLogicalPlan), diff --git a/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/autocdc/AutoCdcFlowSuite.scala b/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/autocdc/AutoCdcFlowSuite.scala new file mode 100644 index 0000000000000..c741062c1b6c6 --- /dev/null +++ b/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/autocdc/AutoCdcFlowSuite.scala @@ -0,0 +1,550 @@ +/* + * 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 java.util.Locale + +import scala.util.Success + +import org.apache.spark.sql.{functions => F, AnalysisException, Column, QueryTest} +import org.apache.spark.sql.catalyst.TableIdentifier +import org.apache.spark.sql.classic.DataFrame +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.pipelines.graph.{ + AutoCdcFlow, + AutoCdcMergeFlow, + FlowFunction, + FlowFunctionResult, + Input, + QueryContext, + QueryOrigin +} +import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types.{DataType, IntegerType, LongType, StringType, StructField, StructType} + +/** + * Unit tests for the [[AutoCdcFlow]] data class and the augmented schema computed by + * [[AutoCdcMergeFlow]]. The tests stop at the data-class / schema surface; they do not + * exercise the full pipeline-graph resolution machinery (which is not yet wired up to AutoCDC + * flows). + */ +class AutoCdcFlowSuite extends QueryTest with SharedSparkSession { + + private val testIdentifier = TableIdentifier("cdc_target", Some("db")) + + /** A no-op [[FlowFunction]] that throws if invoked; AutoCdcFlow tests should never call it. */ + private val noOpFlowFunction: FlowFunction = new FlowFunction { + override def call( + allInputs: Set[TableIdentifier], + availableInputs: Seq[Input], + configuration: Map[String, String], + queryContext: QueryContext, + queryOrigin: QueryOrigin): FlowFunctionResult = + throw new UnsupportedOperationException( + "noOpFlowFunction.call should not be invoked from AutoCdcFlowSuite tests" + ) + } + + private val testQueryContext = + QueryContext(currentCatalog = Some("test_catalog"), currentDatabase = Some("test_db")) + + private val testChangeArgs = ChangeArgs( + keys = Seq(UnqualifiedColumnName("id")), + sequencing = F.col("seq"), + storedAsScdType = ScdType.Type1 + ) + + private def newAutoCdcFlow( + identifier: TableIdentifier = testIdentifier, + destinationIdentifier: TableIdentifier = testIdentifier, + func: FlowFunction = noOpFlowFunction, + queryContext: QueryContext = testQueryContext, + sqlConf: Map[String, String] = Map.empty, + comment: Option[String] = None, + origin: QueryOrigin = QueryOrigin.empty, + changeArgs: ChangeArgs = testChangeArgs): AutoCdcFlow = { + AutoCdcFlow( + identifier = identifier, + destinationIdentifier = destinationIdentifier, + func = func, + queryContext = queryContext, + sqlConf = sqlConf, + comment = comment, + origin = origin, + changeArgs = changeArgs + ) + } + + test("AutoCdcFlow exposes its constructor fields") { + val flow = newAutoCdcFlow( + sqlConf = Map("spark.sql.shuffle.partitions" -> "8"), + comment = Some("my CDC flow") + ) + + assert(flow.identifier == testIdentifier) + assert(flow.destinationIdentifier == testIdentifier) + assert(flow.func eq noOpFlowFunction) + assert(flow.queryContext == testQueryContext) + assert(flow.sqlConf == Map("spark.sql.shuffle.partitions" -> "8")) + assert(flow.comment.contains("my CDC flow")) + assert(flow.origin == QueryOrigin.empty) + assert(flow.changeArgs == testChangeArgs) + } + + test("AutoCdcFlow defaults sqlConf to empty and comment to None") { + // Confirms the case-class default values match the documented contract; downstream + // registration code relies on `sqlConf` being a non-null empty map by default so that + // `defaultSqlConf ++ flowDef.sqlConf` is well-defined in [[GraphRegistrationContext]]. + val flow = AutoCdcFlow( + identifier = testIdentifier, + destinationIdentifier = testIdentifier, + func = noOpFlowFunction, + queryContext = testQueryContext, + origin = QueryOrigin.empty, + changeArgs = testChangeArgs + ) + + assert(flow.sqlConf.isEmpty) + assert(flow.comment.isEmpty) + } + + test("AutoCdcFlow.once is always false") { + // AutoCDC flows are streaming-only and must run on every batch trigger, never as a + // one-shot full-refresh-style flow. Locking this in so a future refactor doesn't + // accidentally make `once` configurable. + + // In the future we may intentionally add [[once]] support for AutoCDC flows, at which point + // this test can safely be removed. + val flow = newAutoCdcFlow() + assert(!flow.once) + } + + test("AutoCdcFlow.withSqlConf returns a new instance with the updated sqlConf") { + val original = newAutoCdcFlow(sqlConf = Map("a" -> "1")) + val updated = original.withSqlConf(Map("b" -> "2")) + + assert(updated.sqlConf == Map("b" -> "2")) + // All other fields should be preserved verbatim. + assert(updated.identifier == original.identifier) + assert(updated.destinationIdentifier == original.destinationIdentifier) + assert(updated.func eq original.func) + assert(updated.queryContext == original.queryContext) + assert(updated.comment == original.comment) + assert(updated.origin == original.origin) + assert(updated.changeArgs == original.changeArgs) + // The original must not be mutated. + assert(original.sqlConf == Map("a" -> "1")) + } + + // =========================================================================================== + // AutoCdcMergeFlow.schema tests + // =========================================================================================== + + /** Materializes a successful [[FlowFunctionResult]] backed by the given source dataframe. */ + private def successfulFuncResult(sourceDf: DataFrame): FlowFunctionResult = + FlowFunctionResult( + requestedInputs = Set.empty, + batchInputs = Set.empty, + streamingInputs = Set.empty, + usedExternalInputs = Set.empty, + dataFrame = Success(sourceDf), + sqlConf = Map.empty + ) + + /** Builds a [[AutoCdcMergeFlow]] over the given source dataframe + change args. */ + private def newAutoCdcMergeFlow( + sourceDf: DataFrame, + keys: Seq[UnqualifiedColumnName] = Seq(UnqualifiedColumnName("id")), + sequencing: Column = F.col("seq"), + storedAsScdType: ScdType = ScdType.Type1, + columnSelection: Option[ColumnSelection] = None): AutoCdcMergeFlow = { + val flow = newAutoCdcFlow( + changeArgs = ChangeArgs( + keys = keys, + sequencing = sequencing, + storedAsScdType = storedAsScdType, + columnSelection = columnSelection + ) + ) + new AutoCdcMergeFlow(flow, successfulFuncResult(sourceDf)) + } + + /** A stable 3-column source CDF schema used across most schema tests. */ + private def threeColumnSourceDf(): DataFrame = { + val schema = new StructType() + .add("id", IntegerType, nullable = false) + .add("name", StringType) + .add("seq", LongType) + spark.createDataFrame(spark.sparkContext.emptyRDD[org.apache.spark.sql.Row], schema) + } + + /** Convenience to extract the [[StructType]] of the projected `_cdc_metadata` column. */ + private def cdcMetadataStruct(schema: StructType): StructType = + schema(Scd1BatchProcessor.cdcMetadataColName).dataType.asInstanceOf[StructType] + + test( + "AutoCdcMergeFlow.schema appends _cdc_metadata to the source schema when no " + + "columnSelection is set" + ) { + val resolvedFlow = newAutoCdcMergeFlow(threeColumnSourceDf()) + + val expected = new StructType() + .add("id", IntegerType, nullable = false) + .add("name", StringType) + .add("seq", LongType) + .add( + StructField( + Scd1BatchProcessor.cdcMetadataColName, + Scd1BatchProcessor.cdcMetadataColSchema(LongType), + nullable = false + ) + ) + assert(resolvedFlow.schema == expected) + } + + test("AutoCdcMergeFlow.schema applies an IncludeColumns selection") { + val resolvedFlow = newAutoCdcMergeFlow( + sourceDf = threeColumnSourceDf(), + columnSelection = Some( + ColumnSelection.IncludeColumns( + Seq(UnqualifiedColumnName("id"), UnqualifiedColumnName("seq")) + ) + ) + ) + + val expected = new StructType() + .add("id", IntegerType, nullable = false) + .add("seq", LongType) + .add( + StructField( + Scd1BatchProcessor.cdcMetadataColName, + Scd1BatchProcessor.cdcMetadataColSchema(LongType), + nullable = false + ) + ) + assert(resolvedFlow.schema == expected) + } + + test("AutoCdcMergeFlow.schema applies an ExcludeColumns selection") { + val resolvedFlow = newAutoCdcMergeFlow( + sourceDf = threeColumnSourceDf(), + columnSelection = Some( + ColumnSelection.ExcludeColumns(Seq(UnqualifiedColumnName("name"))) + ) + ) + + val expected = new StructType() + .add("id", IntegerType, nullable = false) + .add("seq", LongType) + .add( + StructField( + Scd1BatchProcessor.cdcMetadataColName, + Scd1BatchProcessor.cdcMetadataColSchema(LongType), + nullable = false + ) + ) + assert(resolvedFlow.schema == expected) + } + + test( + "AutoCdcMergeFlow.schema's _cdc_metadata struct uses the resolved sequencing data type" + ) { + // Source has a Long `seq` column; sequencing is `cast(seq as int)`, so the projected + // `_cdc_metadata` fields should be Int (not Long), demonstrating that the sequencing + // expression's *resolved* type drives the metadata schema. + val resolvedFlow = newAutoCdcMergeFlow( + sourceDf = threeColumnSourceDf(), + sequencing = F.col("seq").cast(IntegerType) + ) + + val metaStruct = cdcMetadataStruct(resolvedFlow.schema) + assert(metaStruct == Scd1BatchProcessor.cdcMetadataColSchema(IntegerType)) + } + + test("AutoCdcMergeFlow.schema's _cdc_metadata field is non-null with nullable inner fields") { + val resolvedFlow = newAutoCdcMergeFlow(threeColumnSourceDf()) + + val metaField = resolvedFlow.schema(Scd1BatchProcessor.cdcMetadataColName) + assert(!metaField.nullable, "_cdc_metadata column itself must be non-null") + + val metaStruct = metaField.dataType.asInstanceOf[StructType] + assert(metaStruct(Scd1BatchProcessor.cdcDeleteSequenceFieldName).nullable) + assert(metaStruct(Scd1BatchProcessor.cdcUpsertSequenceFieldName).nullable) + } + + test("AutoCdcMergeFlow.schema is stable across reads") { + // The schema computation calls `df.select(sequencing).schema`, which triggers Spark + // analysis. The eagerly-initialized `val` caches the result so downstream consumers get + // a stable schema instance across reads. + val resolvedFlow = newAutoCdcMergeFlow(threeColumnSourceDf()) + val first = resolvedFlow.schema + val second = resolvedFlow.schema + assert(first eq second, "schema should be cached as a val and return the same instance") + } + + test("AutoCdcMergeFlow rejects SCD2 at construction with UnsupportedOperationException") { + // Constructing the flow forces the resolved schema, which is unsupported for SCD2 today. + // Failing eagerly (rather than deferring to the first downstream `schema` read) is the + // intended UX -- pipeline graph analysis should not be able to register an SCD2 AutoCDC + // flow at all. + val ex = intercept[UnsupportedOperationException] { + newAutoCdcMergeFlow( + sourceDf = threeColumnSourceDf(), + storedAsScdType = ScdType.Type2 + ) + } + assert( + ex.getMessage.contains("AutoCDC flows do not currently support SCD Type 2 transformations.") + ) + } + + // =========================================================================================== + // AutoCdcMergeFlow reserved-prefix validation tests + // + // The two "contract:" tests below lock in the high-level invariant that no reserved-prefix + // column name can be referenced anywhere -- not in the source change-data feed schema, and + // not in user-supplied [[ChangeArgs]] (keys or columnSelection). Together they ensure that + // (a) users cannot opt out of the reserved CDC metadata column by omitting it from the + // selected schema, and (b) users cannot opt in to (or out of) any other reserved-prefix + // name we may reserve in the future for an internal CDC concern. + // + // The remaining tests pin down case-sensitivity nuances of the source-schema validator. + // =========================================================================================== + + /** Builds an empty source df with `id` + `seq` + the supplied extra columns. */ + private def sourceDfWithExtraColumns(extraColumns: (String, DataType)*): DataFrame = { + val schema = extraColumns.foldLeft( + new StructType().add("id", IntegerType, nullable = false).add("seq", LongType) + ) { case (acc, (name, dt)) => acc.add(name, dt) } + spark.createDataFrame(spark.sparkContext.emptyRDD[org.apache.spark.sql.Row], schema) + } + + test( + "Contract: a source df column with the reserved AutoCDC prefix is rejected at flow " + + "construction" + ) { + val conflictingName = s"${Scd1BatchProcessor.reservedColumnNamePrefix}foo" + val sourceDf = sourceDfWithExtraColumns(conflictingName -> StringType) + + checkError( + exception = intercept[AnalysisException] { + newAutoCdcMergeFlow(sourceDf) + }, + condition = "AUTOCDC_RESERVED_COLUMN_NAME_PREFIX_CONFLICT", + sqlState = "42710", + parameters = Map( + "caseSensitivity" -> CaseSensitivityLabels.CaseInsensitive, + "columnName" -> conflictingName, + "schemaName" -> "changeDataFeed", + "reservedColumnNamePrefix" -> Scd1BatchProcessor.reservedColumnNamePrefix + ) + ) + } + + test( + "Contract: ChangeArgs referencing a reserved-prefix column is rejected even when the " + + "source df is clean" + ) { + // The source df has no reserved-prefix columns, but referencing a reserved-prefix column + // from any ChangeArgs path still fails at construction with a different error. The + // reservation is on the name itself, not on its presence in the source feed. + val cleanSourceDf = threeColumnSourceDf() + val reservedName = s"${Scd1BatchProcessor.reservedColumnNamePrefix}foo" + + val keysEx = intercept[AnalysisException] { + newAutoCdcMergeFlow( + sourceDf = cleanSourceDf, + keys = Seq(UnqualifiedColumnName(reservedName)) + ) + } + assert(keysEx.getCondition == "AUTOCDC_KEY_NOT_IN_SELECTED_SCHEMA") + + val includeEx = intercept[AnalysisException] { + newAutoCdcMergeFlow( + sourceDf = cleanSourceDf, + columnSelection = Some( + ColumnSelection.IncludeColumns( + Seq(UnqualifiedColumnName("id"), UnqualifiedColumnName(reservedName)) + ) + ) + ) + } + assert(includeEx.getCondition == "AUTOCDC_COLUMNS_NOT_FOUND_IN_SCHEMA") + + val excludeEx = intercept[AnalysisException] { + newAutoCdcMergeFlow( + sourceDf = cleanSourceDf, + columnSelection = Some( + ColumnSelection.ExcludeColumns(Seq(UnqualifiedColumnName(reservedName))) + ) + ) + } + assert(excludeEx.getCondition == "AUTOCDC_COLUMNS_NOT_FOUND_IN_SCHEMA") + } + + test("AutoCdcMergeFlow rejects a source df column starting with the reserved prefix") { + // Default Spark session is case-insensitive (`spark.sql.caseSensitive = false`). + val conflictingName = s"${Scd1BatchProcessor.reservedColumnNamePrefix}foo" + val sourceDf = sourceDfWithExtraColumns(conflictingName -> StringType) + + checkError( + exception = intercept[AnalysisException] { + newAutoCdcMergeFlow(sourceDf) + }, + condition = "AUTOCDC_RESERVED_COLUMN_NAME_PREFIX_CONFLICT", + sqlState = "42710", + parameters = Map( + "caseSensitivity" -> CaseSensitivityLabels.CaseInsensitive, + "columnName" -> conflictingName, + "schemaName" -> "changeDataFeed", + "reservedColumnNamePrefix" -> Scd1BatchProcessor.reservedColumnNamePrefix + ) + ) + } + + test( + "AutoCdcMergeFlow rejects a source df column whose name equals the reserved CDC " + + "metadata column" + ) { + // Locks in the previous engine-level guard (Scd1BatchProcessor.extendMicrobatchRowsWith + // CdcMetadata) at flow-construction time. Any future regression where a user-supplied + // CDC stream carries the reserved metadata column name should fail eagerly here. + val sourceDf = sourceDfWithExtraColumns(Scd1BatchProcessor.cdcMetadataColName -> StringType) + + checkError( + exception = intercept[AnalysisException] { + newAutoCdcMergeFlow(sourceDf) + }, + condition = "AUTOCDC_RESERVED_COLUMN_NAME_PREFIX_CONFLICT", + sqlState = "42710", + parameters = Map( + "caseSensitivity" -> CaseSensitivityLabels.CaseInsensitive, + "columnName" -> Scd1BatchProcessor.cdcMetadataColName, + "schemaName" -> "changeDataFeed", + "reservedColumnNamePrefix" -> Scd1BatchProcessor.reservedColumnNamePrefix + ) + ) + } + + test( + "AutoCdcMergeFlow rejects an uppercase reserved-prefix column when caseSensitive=false" + ) { + withSQLConf(SQLConf.CASE_SENSITIVE.key -> "false") { + val conflictingName = + s"${Scd1BatchProcessor.reservedColumnNamePrefix}foo".toUpperCase(Locale.ROOT) + val sourceDf = sourceDfWithExtraColumns(conflictingName -> StringType) + + checkError( + exception = intercept[AnalysisException] { + newAutoCdcMergeFlow(sourceDf) + }, + condition = "AUTOCDC_RESERVED_COLUMN_NAME_PREFIX_CONFLICT", + sqlState = "42710", + parameters = Map( + "caseSensitivity" -> CaseSensitivityLabels.CaseInsensitive, + "columnName" -> conflictingName, + "schemaName" -> "changeDataFeed", + "reservedColumnNamePrefix" -> Scd1BatchProcessor.reservedColumnNamePrefix + ) + ) + } + } + + test( + "AutoCdcMergeFlow allows an uppercase reserved-prefix column when caseSensitive=true" + ) { + // Under case-sensitive analysis, the uppercase variant is a distinct identifier and does + // not collide with the lowercase reserved namespace. Locks in that the validation respects + // `spark.sql.caseSensitive`, consistent with the schema-augmentation logic in this class. + withSQLConf(SQLConf.CASE_SENSITIVE.key -> "true") { + val nonConflictingName = + s"${Scd1BatchProcessor.reservedColumnNamePrefix}foo".toUpperCase(Locale.ROOT) + val sourceDf = sourceDfWithExtraColumns(nonConflictingName -> StringType) + + // No exception expected: construction succeeds. + newAutoCdcMergeFlow(sourceDf) + } + } + + // =========================================================================================== + // AutoCdcMergeFlow keys-presence validation tests (requireKeysPresentInSelectedSchema) + // =========================================================================================== + + test("AutoCdcMergeFlow rejects a key that is not present in the source change-data feed") { + // No columnSelection: the post-selection schema equals the source schema. The key `id` + // is absent from the source df entirely, so the validator must surface a CDC-specific + // error rather than deferring to Spark's generic UNRESOLVED_COLUMN. + val schema = new StructType() + .add("name", StringType) + .add("seq", LongType) + val sourceDf = + spark.createDataFrame(spark.sparkContext.emptyRDD[org.apache.spark.sql.Row], schema) + + checkError( + exception = intercept[AnalysisException] { + newAutoCdcMergeFlow(sourceDf) + }, + condition = "AUTOCDC_KEY_NOT_IN_SELECTED_SCHEMA", + sqlState = "22023", + parameters = Map( + "caseSensitivity" -> CaseSensitivityLabels.CaseInsensitive, + "keyColumnName" -> "id" + ) + ) + } + + test("AutoCdcMergeFlow rejects a key dropped by an IncludeColumns selection") { + checkError( + exception = intercept[AnalysisException] { + newAutoCdcMergeFlow( + sourceDf = threeColumnSourceDf(), + columnSelection = Some( + ColumnSelection.IncludeColumns( + Seq(UnqualifiedColumnName("name"), UnqualifiedColumnName("seq")) + ) + ) + ) + }, + condition = "AUTOCDC_KEY_NOT_IN_SELECTED_SCHEMA", + sqlState = "22023", + parameters = Map( + "caseSensitivity" -> CaseSensitivityLabels.CaseInsensitive, + "keyColumnName" -> "id" + ) + ) + } + + test("AutoCdcMergeFlow rejects a key dropped by an ExcludeColumns selection") { + checkError( + exception = intercept[AnalysisException] { + newAutoCdcMergeFlow( + sourceDf = threeColumnSourceDf(), + columnSelection = Some( + ColumnSelection.ExcludeColumns(Seq(UnqualifiedColumnName("id"))) + ) + ) + }, + condition = "AUTOCDC_KEY_NOT_IN_SELECTED_SCHEMA", + sqlState = "22023", + parameters = Map( + "caseSensitivity" -> CaseSensitivityLabels.CaseInsensitive, + "keyColumnName" -> "id" + ) + ) + } +} diff --git a/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/autocdc/Scd1BatchProcessorSuite.scala b/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/autocdc/Scd1BatchProcessorSuite.scala index a49c89e357555..0d7fa497f194e 100644 --- a/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/autocdc/Scd1BatchProcessorSuite.scala +++ b/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/autocdc/Scd1BatchProcessorSuite.scala @@ -17,8 +17,6 @@ package org.apache.spark.sql.pipelines.autocdc -import java.util.Locale - import org.apache.spark.sql.{functions => F, AnalysisException, QueryTest, Row} import org.apache.spark.sql.classic.DataFrame import org.apache.spark.sql.internal.SQLConf @@ -655,81 +653,6 @@ class Scd1BatchProcessorSuite extends QueryTest with SharedSparkSession { assert(ex.getCondition == "DATATYPE_MISMATCH.CAST_WITHOUT_SUGGESTION") } - test("extendMicrobatchRowsWithCdcMetadata rejects a microbatch that already contains the " + - "reserved CDC metadata column") { - withSQLConf(SQLConf.CASE_SENSITIVE.key -> "true") { - val schema = new StructType() - .add("id", IntegerType) - .add("seq", LongType) - .add(Scd1BatchProcessor.cdcMetadataColName, StringType) - - val batch = microbatchOf(schema)( - Row(1, 10L, "user-supplied") - ) - - val processor = Scd1BatchProcessor( - changeArgs = ChangeArgs( - keys = Seq(UnqualifiedColumnName("id")), - sequencing = F.col("seq"), - storedAsScdType = ScdType.Type1 - ), - resolvedSequencingType = LongType - ) - - checkError( - exception = intercept[AnalysisException] { - processor.extendMicrobatchRowsWithCdcMetadata(batch) - }, - condition = "AUTOCDC_RESERVED_COLUMN_NAME_CONFLICT", - sqlState = "42710", - parameters = Map( - "caseSensitivity" -> CaseSensitivityLabels.CaseSensitive, - "columnName" -> Scd1BatchProcessor.cdcMetadataColName, - "schemaName" -> "microbatch", - "reservedColumnName" -> Scd1BatchProcessor.cdcMetadataColName - ) - ) - } - } - - test("extendMicrobatchRowsWithCdcMetadata rejects reserved CDC metadata column " + - "case-insensitively") { - withSQLConf(SQLConf.CASE_SENSITIVE.key -> "false") { - val conflictingColumnName = Scd1BatchProcessor.cdcMetadataColName.toUpperCase(Locale.ROOT) - val schema = new StructType() - .add("id", IntegerType) - .add("seq", LongType) - .add(conflictingColumnName, StringType) - - val batch = microbatchOf(schema)( - Row(1, 10L, "user-supplied") - ) - - val processor = Scd1BatchProcessor( - changeArgs = ChangeArgs( - keys = Seq(UnqualifiedColumnName("id")), - sequencing = F.col("seq"), - storedAsScdType = ScdType.Type1 - ), - resolvedSequencingType = LongType - ) - - checkError( - exception = intercept[AnalysisException] { - processor.extendMicrobatchRowsWithCdcMetadata(batch) - }, - condition = "AUTOCDC_RESERVED_COLUMN_NAME_CONFLICT", - sqlState = "42710", - parameters = Map( - "caseSensitivity" -> CaseSensitivityLabels.CaseInsensitive, - "columnName" -> conflictingColumnName, - "schemaName" -> "microbatch", - "reservedColumnName" -> Scd1BatchProcessor.cdcMetadataColName - ) - ) - } - } - test("projectTargetColumnsOntoMicrobatch keeps every user column and the CDC metadata column " + "when columnSelection is None") { val batch = microbatchOf(microbatchWithCdcMetadataSchema)( diff --git a/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/utils/TestGraphRegistrationContext.scala b/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/utils/TestGraphRegistrationContext.scala index 9ff92ee895b1d..f5bdf87a6cc62 100644 --- a/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/utils/TestGraphRegistrationContext.scala +++ b/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/utils/TestGraphRegistrationContext.scala @@ -21,7 +21,7 @@ import org.apache.spark.sql.SQLContext import org.apache.spark.sql.catalyst.TableIdentifier import org.apache.spark.sql.catalyst.analysis.{LocalTempView, PersistedView => PersistedViewType, UnresolvedRelation, ViewType} import org.apache.spark.sql.classic.{DataFrame, SparkSession} -import org.apache.spark.sql.pipelines.graph.{DataflowGraph, FlowAnalysis, FlowFunction, GraphIdentifierManager, GraphRegistrationContext, PersistedView, QueryContext, QueryOrigin, QueryOriginType, Sink, SinkImpl, Table, TemporaryView, UnresolvedFlow} +import org.apache.spark.sql.pipelines.graph.{DataflowGraph, FlowAnalysis, FlowFunction, GraphIdentifierManager, GraphRegistrationContext, PersistedView, QueryContext, QueryOrigin, QueryOriginType, Sink, SinkImpl, Table, TemporaryView, UntypedFlow} import org.apache.spark.sql.types.StructType import org.apache.spark.sql.util.CaseInsensitiveStringMap @@ -176,7 +176,7 @@ class TestGraphRegistrationContext( if (query.isDefined) { registerFlow( - new UnresolvedFlow( + UntypedFlow( identifier = qualifiedIdentifier, destinationIdentifier = qualifiedIdentifier, func = query.get, @@ -267,7 +267,7 @@ class TestGraphRegistrationContext( ) registerFlow( - new UnresolvedFlow( + UntypedFlow( identifier = viewIdentifier, destinationIdentifier = viewIdentifier, func = query, @@ -339,7 +339,7 @@ class TestGraphRegistrationContext( } registerFlow( - new UnresolvedFlow( + UntypedFlow( identifier = flowIdentifier, destinationIdentifier = flowDestinationIdentifier, func = query, From 7bef34a0db31c57bd4ec5b6872634d931b651d4e Mon Sep 17 00:00:00 2001 From: Anish Mahto Date: Thu, 21 May 2026 16:10:27 +0000 Subject: [PATCH 2/7] add AutoCdcFlow -> AutoCdcMergeFlow resolution --- .../graph/CoreDataflowNodeProcessor.scala | 15 +++++++-- .../spark/sql/pipelines/graph/Flow.scala | 2 +- .../graph/ConnectValidPipelineSuite.scala | 33 +++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/CoreDataflowNodeProcessor.scala b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/CoreDataflowNodeProcessor.scala index 38fde0bfec4a1..1d2b4ef8a0be5 100644 --- a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/CoreDataflowNodeProcessor.scala +++ b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/CoreDataflowNodeProcessor.scala @@ -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 @@ -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 => @@ -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) } } } diff --git a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala index 9cc8817e70891..8cf436693c60a 100644 --- a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala +++ b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala @@ -136,7 +136,7 @@ sealed trait UnresolvedFlow extends Flow { /** * An [[UnresolvedFlow]] whose execution-type has not yet been determined. - * + * * In some cases, we know the execution-type for an [[UnresolvedFlow]] even before flow analysis * and resolution. For example an AutoCDCFlow is a special unresolved-but-typed flow; we know a * flow will be an AutoCDC flow immediately on construction, because it has its own special diff --git a/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/graph/ConnectValidPipelineSuite.scala b/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/graph/ConnectValidPipelineSuite.scala index 3ac3c09017506..3c7db2cca889e 100644 --- a/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/graph/ConnectValidPipelineSuite.scala +++ b/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/graph/ConnectValidPipelineSuite.scala @@ -21,6 +21,7 @@ import org.apache.spark.sql.catalyst.TableIdentifier import org.apache.spark.sql.catalyst.analysis.UnresolvedRelation import org.apache.spark.sql.catalyst.plans.logical.Union import org.apache.spark.sql.execution.streaming.runtime.MemoryStream +import org.apache.spark.sql.pipelines.autocdc.{ChangeArgs, ScdType, UnqualifiedColumnName} import org.apache.spark.sql.pipelines.utils.{PipelineTest, TestGraphRegistrationContext} import org.apache.spark.sql.test.SharedSparkSession import org.apache.spark.sql.types._ @@ -509,6 +510,38 @@ class ConnectValidPipelineSuite extends PipelineTest with SharedSparkSession { assert(g.flow(TableIdentifier("sink_flow")).isInstanceOf[StreamingFlow]) } + test("AutoCdcFlow registers and resolves to AutoCdcMergeFlow") { + val session = spark + import session.implicits._ + + val P = new TestGraphRegistrationContext(spark) { + val mem = MemoryStream[Int] + val cdcEvents = mem.toDF().select($"value" as "id", $"value" as "seq") + registerTable("target") + registerFlow( + AutoCdcFlow( + identifier = fullyQualifiedIdentifier("auto_cdc_flow"), + destinationIdentifier = fullyQualifiedIdentifier("target"), + func = dfFlowFunc(cdcEvents), + queryContext = QueryContext( + currentCatalog = Some(TestGraphRegistrationContext.DEFAULT_CATALOG), + currentDatabase = Some(TestGraphRegistrationContext.DEFAULT_DATABASE) + ), + origin = QueryOrigin.empty, + changeArgs = ChangeArgs( + keys = Seq(UnqualifiedColumnName("id")), + sequencing = $"seq", + storedAsScdType = ScdType.Type1 + ) + ) + ) + } + val g = P.resolveToDataflowGraph() + assert( + g.flow(fullyQualifiedIdentifier("auto_cdc_flow")).isInstanceOf[AutoCdcMergeFlow] + ) + } + /** Verifies the [[DataflowGraph]] has the specified [[Flow]] with the specified schema. */ private def verifyFlowSchema( pipeline: DataflowGraph, From 153d5be4f8dce1e1c54fb01797a9d4a1ad80fc73 Mon Sep 17 00:00:00 2001 From: Anish Mahto Date: Thu, 21 May 2026 18:38:23 +0000 Subject: [PATCH 3/7] disallow multiflow for AutoCDC --- .../resources/error/error-conditions.json | 6 +++ .../pipelines/graph/GraphValidations.scala | 25 ++++++++++ .../graph/ConnectInvalidPipelineSuite.scala | 49 +++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/common/utils/src/main/resources/error/error-conditions.json b/common/utils/src/main/resources/error/error-conditions.json index d67a7ec5af01a..64599c9d0e036 100644 --- a/common/utils/src/main/resources/error/error-conditions.json +++ b/common/utils/src/main/resources/error/error-conditions.json @@ -209,6 +209,12 @@ ], "sqlState" : "42703" }, + "AUTOCDC_MULTIPLE_FLOWS_TO_TARGET" : { + "message" : [ + "Invalid AutoCDC destination with multiple flows: . An AutoCDC target table must have exactly one flow writing to it." + ], + "sqlState" : "42000" + }, "AUTOCDC_KEY_NOT_IN_SELECTED_SCHEMA" : { "message" : [ "Using column name comparison, the AutoCDC key column `` 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." diff --git a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/GraphValidations.scala b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/GraphValidations.scala index ade6ce0bad3c3..7433102594471 100644 --- a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/GraphValidations.scala +++ b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/GraphValidations.scala @@ -34,6 +34,24 @@ trait GraphValidations extends Logging { */ protected[pipelines] def validateMultiQueryTables(): Map[TableIdentifier, Seq[Flow]] = { val multiQueryTables = flowsTo.filter(_._2.size > 1) + + // A multiflow table may not have an AutoCDC flow; AutoCDC flow targets must be single query. + multiQueryTables + .find { case (_, flows) => flows.exists(isAutoCdcFlow) } + .foreach { + case (dest, flows) => + throw new AnalysisException( + "AUTOCDC_MULTIPLE_FLOWS_TO_TARGET", + Map( + "tableName" -> dest.unquotedString, + "flows" -> flows + .map(_.displayName) + .sorted + .mkString(", ") + ) + ) + } + // Non-streaming tables do not support multiflow. multiQueryTables .find { @@ -58,6 +76,13 @@ trait GraphValidations extends Logging { multiQueryTables } + /** Returns true iff the given flow is an [[AutoCdcFlow]] (resolved or not). */ + private def isAutoCdcFlow(f: Flow): Boolean = f match { + case _: AutoCdcFlow => true + case rcf: ResolutionCompletedFlow => rcf.flow.isInstanceOf[AutoCdcFlow] + case _ => false + } + /** * Validate that each resolved flow is correctly either a streaming flow or non-streaming flow, * depending on the flow type (ex. once flow vs non-once flow) and the dataset type the flow diff --git a/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/graph/ConnectInvalidPipelineSuite.scala b/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/graph/ConnectInvalidPipelineSuite.scala index f37716b4a24d3..73703b974bf96 100644 --- a/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/graph/ConnectInvalidPipelineSuite.scala +++ b/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/graph/ConnectInvalidPipelineSuite.scala @@ -19,6 +19,7 @@ package org.apache.spark.sql.pipelines.graph import org.apache.spark.sql.AnalysisException import org.apache.spark.sql.execution.streaming.runtime.MemoryStream +import org.apache.spark.sql.pipelines.autocdc.{ChangeArgs, ScdType, UnqualifiedColumnName} import org.apache.spark.sql.pipelines.utils.{PipelineTest, TestGraphRegistrationContext} import org.apache.spark.sql.test.SharedSparkSession import org.apache.spark.sql.types.{IntegerType, StructType} @@ -547,4 +548,52 @@ class ConnectInvalidPipelineSuite extends PipelineTest with SharedSparkSession { assert(!ex1.getMessage.contains(streamingTableHint)) assert(ex2.getMessage.contains(streamingTableHint)) } + + test("A multiquery table cannot have an AutoCDC query input") { + val session = spark + import session.implicits._ + + val graph = new TestGraphRegistrationContext(spark) { + val cdcEvents = MemoryStream[Int].toDF().select($"value" as "id", $"value" as "seq") + registerTable("target") + registerFlow( + AutoCdcFlow( + identifier = fullyQualifiedIdentifier("auto_cdc_flow"), + destinationIdentifier = fullyQualifiedIdentifier("target"), + func = dfFlowFunc(cdcEvents), + queryContext = QueryContext( + currentCatalog = Some(TestGraphRegistrationContext.DEFAULT_CATALOG), + currentDatabase = Some(TestGraphRegistrationContext.DEFAULT_DATABASE) + ), + origin = QueryOrigin.empty, + changeArgs = ChangeArgs( + keys = Seq(UnqualifiedColumnName("id")), + sequencing = $"seq", + storedAsScdType = ScdType.Type1 + ) + ) + ) + registerFlow( + destinationName = "target", + name = "extra_flow", + query = dfFlowFunc(MemoryStream[Int].toDF().select($"value" as "id", $"value" as "seq")) + ) + }.resolveToDataflowGraph() + + val ex = intercept[AnalysisException] { + graph.validate() + } + + checkError( + exception = ex, + condition = "AUTOCDC_MULTIPLE_FLOWS_TO_TARGET", + parameters = Map( + "tableName" -> fullyQualifiedIdentifier("target").unquotedString, + "flows" -> Seq( + fullyQualifiedIdentifier("auto_cdc_flow").unquotedString, + fullyQualifiedIdentifier("extra_flow").unquotedString + ).sorted.mkString(", ") + ) + ) + } } From 5415e00cd66703c71a87acbd7ba1b34a3c424726 Mon Sep 17 00:00:00 2001 From: Anish Mahto Date: Fri, 22 May 2026 18:18:52 +0000 Subject: [PATCH 4/7] linting --- .../main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala index 8cf436693c60a..13d0df59dee74 100644 --- a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala +++ b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala @@ -141,7 +141,7 @@ sealed trait UnresolvedFlow extends Flow { * and resolution. For example an AutoCDCFlow is a special unresolved-but-typed flow; we know a * flow will be an AutoCDC flow immediately on construction, because it has its own special * registration API. Such flows are considered "typed flows", but there isn't any semantic reason - * yet to explicitly introduce a `TypedFlow` trait/class. + * yet to explicitly introduce a `TypedFlow` trait/class. */ case class UntypedFlow( identifier: TableIdentifier, From bc0c1d8fa8c3ed56ab530022b146457d7a97de85 Mon Sep 17 00:00:00 2001 From: Anish Mahto Date: Fri, 22 May 2026 20:22:43 +0000 Subject: [PATCH 5/7] reorder error conditions alphabetically --- .../src/main/resources/error/error-conditions.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/common/utils/src/main/resources/error/error-conditions.json b/common/utils/src/main/resources/error/error-conditions.json index 64599c9d0e036..dd1b12a0dc9df 100644 --- a/common/utils/src/main/resources/error/error-conditions.json +++ b/common/utils/src/main/resources/error/error-conditions.json @@ -203,6 +203,12 @@ ], "sqlState" : "22023" }, + "AUTOCDC_KEY_NOT_IN_SELECTED_SCHEMA" : { + "message" : [ + "Using column name comparison, the AutoCDC key column `` 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_MULTIPART_COLUMN_IDENTIFIER" : { "message" : [ "Expected a single column identifier; got the multi-part identifier (parts: )." @@ -215,12 +221,6 @@ ], "sqlState" : "42000" }, - "AUTOCDC_KEY_NOT_IN_SELECTED_SCHEMA" : { - "message" : [ - "Using column name comparison, the AutoCDC key column `` 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 `` in the schema collides with the reserved AutoCDC column name prefix `` (using column name comparison). Rename or remove the column." From 61165a46ab52e9ca74b08cd43d00885fd717f953 Mon Sep 17 00:00:00 2001 From: Anish Mahto Date: Fri, 22 May 2026 21:24:39 +0000 Subject: [PATCH 6/7] fix compile error --- .../apache/spark/sql/connect/pipelines/PipelinesHandler.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sql/connect/server/src/main/scala/org/apache/spark/sql/connect/pipelines/PipelinesHandler.scala b/sql/connect/server/src/main/scala/org/apache/spark/sql/connect/pipelines/PipelinesHandler.scala index ee19b83c51623..04dbc1a455063 100644 --- a/sql/connect/server/src/main/scala/org/apache/spark/sql/connect/pipelines/PipelinesHandler.scala +++ b/sql/connect/server/src/main/scala/org/apache/spark/sql/connect/pipelines/PipelinesHandler.scala @@ -34,7 +34,7 @@ import org.apache.spark.sql.connect.service.SessionHolder import org.apache.spark.sql.execution.command.{ShowCatalogsCommand, ShowNamespacesCommand} import org.apache.spark.sql.pipelines.Language.Python import org.apache.spark.sql.pipelines.common.RunState.{CANCELED, FAILED} -import org.apache.spark.sql.pipelines.graph.{AllTables, FlowAnalysis, GraphIdentifierManager, GraphRegistrationContext, IdentifierHelper, NoTables, PipelineUpdateContextImpl, QueryContext, QueryOrigin, QueryOriginType, Sink, SinkImpl, SomeTables, SqlGraphRegistrationContext, Table, TableFilter, TemporaryView, UnresolvedFlow} +import org.apache.spark.sql.pipelines.graph.{AllTables, FlowAnalysis, GraphIdentifierManager, GraphRegistrationContext, IdentifierHelper, NoTables, PipelineUpdateContextImpl, QueryContext, QueryOrigin, QueryOriginType, Sink, SinkImpl, SomeTables, SqlGraphRegistrationContext, Table, TableFilter, TemporaryView, UntypedFlow} import org.apache.spark.sql.pipelines.logging.{PipelineEvent, RunProgress} import org.apache.spark.sql.types.StructType @@ -371,7 +371,7 @@ private[connect] object PipelinesHandler extends Logging { case proto.PipelineCommand.DefineFlow.DetailsCase.RELATION_FLOW_DETAILS => val relationFlowDetails = flow.getRelationFlowDetails graphElementRegistry.registerFlow( - UnresolvedFlow( + UntypedFlow( identifier = flowIdentifier, destinationIdentifier = destinationIdentifier, func = FlowAnalysis.createFlowFunctionFromLogicalPlan( From d8410ec47b632d5973e7744f831715611260d9f2 Mon Sep 17 00:00:00 2001 From: Anish Mahto Date: Fri, 22 May 2026 22:37:28 +0000 Subject: [PATCH 7/7] PR feedback --- .../resources/error/error-conditions.json | 6 +++ .../graph/CoreDataflowNodeProcessor.scala | 4 +- .../spark/sql/pipelines/graph/Flow.scala | 7 ++-- .../pipelines/graph/GraphValidations.scala | 3 +- .../pipelines/autocdc/AutoCdcFlowSuite.scala | 40 +++++-------------- 5 files changed, 24 insertions(+), 36 deletions(-) diff --git a/common/utils/src/main/resources/error/error-conditions.json b/common/utils/src/main/resources/error/error-conditions.json index dd1b12a0dc9df..811b1719f68ab 100644 --- a/common/utils/src/main/resources/error/error-conditions.json +++ b/common/utils/src/main/resources/error/error-conditions.json @@ -227,6 +227,12 @@ ], "sqlState" : "42710" }, + "AUTOCDC_SCD2_NOT_SUPPORTED" : { + "message" : [ + "AutoCDC flows do not currently support SCD Type 2 transformations." + ], + "sqlState" : "0A000" + }, "AVRO_CANNOT_WRITE_NULL_FIELD" : { "message" : [ "Cannot write null value for field defined as non-null Avro data type .", diff --git a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/CoreDataflowNodeProcessor.scala b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/CoreDataflowNodeProcessor.scala index 1d2b4ef8a0be5..66f2995ee02d9 100644 --- a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/CoreDataflowNodeProcessor.scala +++ b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/CoreDataflowNodeProcessor.scala @@ -176,7 +176,7 @@ private class FlowResolver(rawGraph: DataflowGraph) { } else { f } - transformUnresolvedFlowToResolvedFlow(flowToResolve, maybeNewFuncResult) + resolveFlow(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 @@ -199,7 +199,7 @@ private class FlowResolver(rawGraph: DataflowGraph) { } } - private def transformUnresolvedFlowToResolvedFlow( + private def resolveFlow( flow: UnresolvedFlow, funcResult: FlowFunctionResult): ResolvedFlow = { flow match { diff --git a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala index 13d0df59dee74..eb28e5c6ee5a6 100644 --- a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala +++ b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/Flow.scala @@ -157,7 +157,7 @@ case class UntypedFlow( } /** - * An unresolved but typed that applies a CDC event stream to a target table via MERGE. + * An unresolved but typed flow that applies a CDC event stream to a target table via MERGE. * * [[AutoCdcFlow]] is a typed flow because it is only supported for streaming, and not as a once * flow. Therefore by definition it is a streaming-type flow. @@ -291,8 +291,9 @@ class AutoCdcMergeFlow( ) ) case ScdType.Type2 => - throw new UnsupportedOperationException( - "AutoCDC flows do not currently support SCD Type 2 transformations." + throw new AnalysisException( + errorClass = "AUTOCDC_SCD2_NOT_SUPPORTED", + messageParameters = Map.empty ) } } diff --git a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/GraphValidations.scala b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/GraphValidations.scala index 7433102594471..0439603b7a001 100644 --- a/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/GraphValidations.scala +++ b/sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/GraphValidations.scala @@ -78,8 +78,7 @@ trait GraphValidations extends Logging { /** Returns true iff the given flow is an [[AutoCdcFlow]] (resolved or not). */ private def isAutoCdcFlow(f: Flow): Boolean = f match { - case _: AutoCdcFlow => true - case rcf: ResolutionCompletedFlow => rcf.flow.isInstanceOf[AutoCdcFlow] + case _: AutoCdcFlow | _: AutoCdcMergeFlow => true case _ => false } diff --git a/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/autocdc/AutoCdcFlowSuite.scala b/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/autocdc/AutoCdcFlowSuite.scala index c741062c1b6c6..c1de14287366f 100644 --- a/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/autocdc/AutoCdcFlowSuite.scala +++ b/sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/autocdc/AutoCdcFlowSuite.scala @@ -297,19 +297,21 @@ class AutoCdcFlowSuite extends QueryTest with SharedSparkSession { assert(first eq second, "schema should be cached as a val and return the same instance") } - test("AutoCdcMergeFlow rejects SCD2 at construction with UnsupportedOperationException") { + test("AutoCdcMergeFlow rejects SCD2 at construction with AUTOCDC_SCD2_NOT_SUPPORTED") { // Constructing the flow forces the resolved schema, which is unsupported for SCD2 today. // Failing eagerly (rather than deferring to the first downstream `schema` read) is the // intended UX -- pipeline graph analysis should not be able to register an SCD2 AutoCDC // flow at all. - val ex = intercept[UnsupportedOperationException] { - newAutoCdcMergeFlow( - sourceDf = threeColumnSourceDf(), - storedAsScdType = ScdType.Type2 - ) - } - assert( - ex.getMessage.contains("AutoCDC flows do not currently support SCD Type 2 transformations.") + checkError( + exception = intercept[AnalysisException] { + newAutoCdcMergeFlow( + sourceDf = threeColumnSourceDf(), + storedAsScdType = ScdType.Type2 + ) + }, + condition = "AUTOCDC_SCD2_NOT_SUPPORTED", + sqlState = "0A000", + parameters = Map.empty ) } @@ -397,26 +399,6 @@ class AutoCdcFlowSuite extends QueryTest with SharedSparkSession { assert(excludeEx.getCondition == "AUTOCDC_COLUMNS_NOT_FOUND_IN_SCHEMA") } - test("AutoCdcMergeFlow rejects a source df column starting with the reserved prefix") { - // Default Spark session is case-insensitive (`spark.sql.caseSensitive = false`). - val conflictingName = s"${Scd1BatchProcessor.reservedColumnNamePrefix}foo" - val sourceDf = sourceDfWithExtraColumns(conflictingName -> StringType) - - checkError( - exception = intercept[AnalysisException] { - newAutoCdcMergeFlow(sourceDf) - }, - condition = "AUTOCDC_RESERVED_COLUMN_NAME_PREFIX_CONFLICT", - sqlState = "42710", - parameters = Map( - "caseSensitivity" -> CaseSensitivityLabels.CaseInsensitive, - "columnName" -> conflictingName, - "schemaName" -> "changeDataFeed", - "reservedColumnNamePrefix" -> Scd1BatchProcessor.reservedColumnNamePrefix - ) - ) - } - test( "AutoCdcMergeFlow rejects a source df column whose name equals the reserved CDC " + "metadata column"