Skip to content
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-51771][SQL] Add DSv2 APIs for ALTER TABLE ADD/DROP CONSTRAINT #50561

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

gengliangwang
Copy link
Member

What changes were proposed in this pull request?

This PR adds the following DSv2 TableChange as per SPIP doc:

  • AddConstraint
  • DropConstraint

Why are the changes needed?

For constraints support in Spark

Does this PR introduce any user-facing change?

No

How was this patch tested?

New unit tests

Was this patch authored or co-authored using generative AI tooling?

No

@gengliangwang
Copy link
Member Author

cc @aokolnychyi as well

@@ -536,6 +536,8 @@ class DataSourceV2Strategy(session: SparkSession) extends Strategy with Predicat

case a: AlterTableCommand if a.table.resolved =>
val table = a.table.asInstanceOf[ResolvedTable]
ResolveTableConstraints.validateCatalogForTableChange(
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: there will be new tests for this one when the alter table with constraint is supported end-to-end.

val newConstraint = add.getConstraint
val existingConstraint = findExistingConstraint(newConstraint.name)
if (existingConstraint.isDefined) {
throw new AnalysisException(
Copy link
Contributor

Choose a reason for hiding this comment

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

When do we define dedicated error methods in classes like QueryComplicationErrors? Do we encourage external connectors to use built-in error classes in Spark?

Copy link
Member Author

Choose a reason for hiding this comment

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

All the spark errors are with error classes. This is a utility method for external developers to use directly.

/** A TableChange to alter table and add a constraint. */
final class AddConstraint implements TableChange {
private final Constraint constraint;
private final boolean validate;
Copy link
Contributor

Choose a reason for hiding this comment

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

During the discussion, the SPIP design evolved to be like this:

class AddConstraint implements TableChange {
  private final Constraint constraint;
  private final String validatedTableVersion; // if known and was validated 
  …
}

If Spark validated the constraint, it should set the validation status in the constraint to VALID. The table version will be there, if known.

interface Table {
  …

  // reports the current table version, if supported
  default String currentVersion() { return null; }

  …
}

Copy link
Member Author

@gengliangwang gengliangwang Apr 11, 2025

Choose a reason for hiding this comment

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

TBH I am a bit confused on this one. Shall we make it simple and let the external table catalog to track the validatedTableVersion?

Copy link
Contributor

@aokolnychyi aokolnychyi Apr 12, 2025

Choose a reason for hiding this comment

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

The problem is that Spark will construct a scan to do the validation. We need a way to pass which version of the table was scanned/validated back to the connector so that the connector can check changes that happened concurrently.

@@ -811,6 +811,20 @@
},
"sqlState" : "XX000"
},
"CONSTRAINT_ALREADY_EXISTS" : {
"message" : [
"Constraint '<constraintName>' already exists. Please delete the existing constraint first.",
Copy link
Contributor

Choose a reason for hiding this comment

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

is constraint name scoped to table? wondering should we also put tableName like the other message?

Copy link
Member Author

Choose a reason for hiding this comment

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

This error is from alter table statement, which is usually simple and straightforward. I prefer keeping it as it is now

@Udaundo1973
Copy link

James Channel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants