-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-55951][SQL] Add ChangelogTable schema validation and INVALID_CHANGELOG_SCHEMA error class #55507
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
[SPARK-55951][SQL] Add ChangelogTable schema validation and INVALID_CHANGELOG_SCHEMA error class #55507
Changes from all commits
a769114
b5c012f
b961af5
fe53ca0
a33c931
a61dcd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
||
| /** | ||
|
|
@@ -36,6 +38,11 @@ case class ChangelogTable( | |
| changelogInfo: ChangelogInfo, | ||
| resolved: Boolean = false) extends Table with SupportsRead { | ||
|
|
||
| // 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 | ||
|
|
@@ -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) | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 A top-level-only schema check would cover
Currently, it's implemented for the latter. Open to the recursive column check but could need some input here. What do you think @gengliangwang?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.