Skip to content
Closed
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
23 changes: 23 additions & 0 deletions common/utils/src/main/resources/error/error-conditions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3310,6 +3310,29 @@
},
"sqlState" : "42K03"
},
"INVALID_CHANGELOG_SCHEMA" : {
"message" : [
"The Change Data Capture (CDC) schema returned by connector <changelogName> is invalid."
],
"subClass" : {
"INVALID_COLUMN_TYPE" : {
"message" : [
"Column `<columnName>` has type <actualType>, expected <expectedType>."
]
},
"MISSING_COLUMN" : {
"message" : [
"Required column `<columnName>` is missing."
]
},
"MISSING_ROW_ID" : {
"message" : [
"Connector advertises one or more post-processing properties (`containsCarryoverRows`, `representsUpdateAsDeleteAndInsert`, `containsIntermediateChanges`) that require row identity, but `Changelog.rowId()` returned an empty array."
]
}
},
"sqlState" : "42K03"
},
"INVALID_CLONE_SESSION_REQUEST" : {
"message" : [
"Invalid session clone request."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3881,6 +3881,35 @@ private[sql] object QueryCompilationErrors extends QueryErrorsBase with Compilat
messageParameters = Map("changelogName" -> changelogName))
}

def changelogMissingColumnError(
changelogName: String, columnName: String): AnalysisException = {
new AnalysisException(
errorClass = "INVALID_CHANGELOG_SCHEMA.MISSING_COLUMN",
messageParameters = Map(
"changelogName" -> changelogName,
"columnName" -> columnName))
}

def changelogInvalidColumnTypeError(
changelogName: String,
columnName: String,
expectedType: String,
actualType: String): AnalysisException = {
new AnalysisException(
errorClass = "INVALID_CHANGELOG_SCHEMA.INVALID_COLUMN_TYPE",
messageParameters = Map(
"changelogName" -> changelogName,
"columnName" -> columnName,
"expectedType" -> expectedType,
"actualType" -> actualType))
}

def changelogMissingRowIdError(changelogName: String): AnalysisException = {
new AnalysisException(
errorClass = "INVALID_CHANGELOG_SCHEMA.MISSING_ROW_ID",
messageParameters = Map("changelogName" -> changelogName))
}

def invalidCdcOptionConflictingRangeTypes(): Throwable = {
new AnalysisException(
errorClass = "INVALID_CDC_OPTION.CONFLICTING_RANGE_TYPES",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import java.util.{EnumSet => JEnumSet, Set => JSet}
import org.apache.spark.sql.connector.catalog.{Changelog, ChangelogInfo, Column, SupportsRead, Table, TableCapability}
import org.apache.spark.sql.connector.catalog.TableCapability.{BATCH_READ, MICRO_BATCH_READ}
import org.apache.spark.sql.connector.read.ScanBuilder
import org.apache.spark.sql.errors.QueryCompilationErrors
import org.apache.spark.sql.types.{DataType, StringType, TimestampType}
import org.apache.spark.sql.util.CaseInsensitiveStringMap

/**
Expand All @@ -36,6 +38,11 @@ case class ChangelogTable(
changelogInfo: ChangelogInfo,
resolved: Boolean = false) extends Table with SupportsRead {
Comment thread
gengliangwang marked this conversation as resolved.

// Validate that the connector returned a schema with the required CDC metadata columns
// and correct types. `_commit_version` is connector-defined per the Changelog contract,
// so its type is not checked.
ChangelogTable.validateSchema(changelog)

override def name: String = changelog.name

override def columns: Array[Column] = changelog.columns
Expand All @@ -46,3 +53,39 @@ case class ChangelogTable(

override def capabilities: JSet[TableCapability] = JEnumSet.of(BATCH_READ, MICRO_BATCH_READ)
}

object ChangelogTable {

private[v2] def validateSchema(cl: Changelog): Unit = {
val byName = cl.columns.map(c => c.name -> c).toMap
def check(name: String, expected: DataType*): Unit = {
val col = byName.getOrElse(name,
throw QueryCompilationErrors.changelogMissingColumnError(cl.name, name))
if (expected.nonEmpty && col.dataType != expected.head) {
throw QueryCompilationErrors.changelogInvalidColumnTypeError(
cl.name, name, expected.head.sql, col.dataType.sql)
}
}
check("_change_type", StringType)
check("_commit_version") // connector-defined, any type accepted
check("_commit_timestamp", TimestampType)

// Only call `rowId()` / `rowVersion()` when a capability requires them; a connector
// that advertises a capability without overriding the method surfaces the default
// UnsupportedOperationException directly.
val needsRowId = cl.containsCarryoverRows() ||
cl.representsUpdateAsDeleteAndInsert() ||
cl.containsIntermediateChanges()
if (needsRowId) {
val rowIds = cl.rowId()
if (rowIds == null || rowIds.isEmpty) {
throw QueryCompilationErrors.changelogMissingRowIdError(cl.name)
}
}

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.

rowId columns are not checked for non-nullability, even though (a) the Changelog.rowId() Javadoc requires "Each referenced column must be non-nullable", and (b) the peer row-level-operations path validates this via RewriteRowLevelCommand.resolveRowIdAttrs with NULLABLE_ROW_ID_ATTRIBUTES. Consider adding a parallel NULLABLE_ROW_ID sub-class (or at least stating explicitly that rowId column validation is deferred to a later PR). As written, rowVersion gets nullability + top-level-ness but rowId gets presence only.

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.

Done, using your option 2 from the NULL-safety thread on #55426. Added count(rowVersion) to the carry-over Window as a third aggregate alongside min and max (no extra Window operator, no additional shuffle). The filter now requires _rv_cnt = 2 AND _min_rv = _max_rv. A NULL rowVersion on either side fails the count check and the pair falls through as raw delete+insert instead of being silently dropped. Nesting-agnostic. Implementation and regression test ("NULL rowVersion on one side is NOT silently dropped as carry-over") in #55508.

On the rowId asymmetry: rowId nullability is not schema-checked. An analogous silent-drop path exists (multiple NULL-rowId rows collapse into one Window partition via SQL NULL-group semantics), but the trigger surface is narrower than for rowVersion and a count()=2-style runtime guard does not port cleanly.

A top-level-only schema check would cover id but miss, for example, Delta's nested _metadata.row_id. This asymmetric coverage feels worse than no coverage at all.
We can

  • either do a full schema walk through metadata columns covering both top-level and nested (how deep do we go, I think all the way, right?),
  • or leave it unenforced and trust the Javadoc contract.

Currently, it's implemented for the latter. Open to the recursive column check but could need some input here. What do you think @gengliangwang?

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.

For now I'd defer to a later PR. Is that fine?

val needsRowVersion = cl.containsCarryoverRows() ||
cl.representsUpdateAsDeleteAndInsert()
if (needsRowVersion) {
cl.rowVersion()
}
}
}
Loading