Skip to content

[SPARK-51984][SQL] Support Check Constraint enforcement in UpdateTable and MergeIntoTable #50943

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

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

Conversation

gengliangwang
Copy link
Member

@gengliangwang gengliangwang commented May 19, 2025

What changes were proposed in this pull request?

  • When a DSV2 table contains check constraints, validate the input rows before Update/Merge Into the table.
  • Add test cases for Update/Merge Into/Delete tables with constraints

Why are the changes needed?

Support check constraint enforcement on table update/merge into.

Does this PR introduce any user-facing change?

No

How was this patch tested?

New UT

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

No

@github-actions github-actions bot added the SQL label May 19, 2025
// applied before the rewrite rules. So we need to apply `ResolveEncodersInUDF` before the
// rewrite rules.
// applied before the rewrite rules. So we need to apply the rewrite rules after
// `ResolveEncodersInUDF`
Batch("DML rewrite", fixedPoint,
Copy link
Member Author

Choose a reason for hiding this comment

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

Context of why this batch is not in the main resolution batch: fab6d83

// Combine the check invariants into a single expression using conjunctive AND.
checkInvariants.reduceOption(And).fold(v2Write)(
condition => v2Write.withNewQuery(Filter(condition, v2Write.query)))
case r: DataSourceV2Relation =>
Copy link
Member Author

Choose a reason for hiding this comment

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

The changes in this file is purely code clean up. cc @aokolnychyi

@gengliangwang
Copy link
Member Author

I will improve the rule to exclude adding FILTER for DELETE and add tests.

@gengliangwang gengliangwang requested review from viirya and cloud-fan May 19, 2025 19:02
@gengliangwang
Copy link
Member Author

cc @aokolnychyi

@@ -419,6 +419,10 @@ class Analyzer(override val catalogManager: CatalogManager) extends RuleExecutor
ResolveRowLevelCommandAssignments ::
MoveParameterizedQueriesDown ::
BindParameters ::
ResolveEncodersInUDF ::
Copy link
Member Author

@gengliangwang gengliangwang May 19, 2025

Choose a reason for hiding this comment

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

I am moving the rules in to the main resolution batch, so that UDF encoders and table constraints can be correctly handled in DML writes.
cc @viirya @Ngone51 @aokolnychyi

Copy link
Member Author

Choose a reason for hiding this comment

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

Reverted this one. It ends up failing many tests: https://github.com/gengliangwang/spark/runs/42517140406

case r: DataSourceV2Relation =>
buildCheckCondition(r.table).map { condition =>
val filter = Filter(condition, v2Write.query)
// Resolve attribute references in the filter condition only, not the entire query.
Copy link
Member Author

Choose a reason for hiding this comment

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

See https://github.com/gengliangwang/spark/actions/runs/15130563620/job/42530638818 for details.

=== Result of Batch DML rewrite ===
!'Sort [(cast(x#26 as bigint) + cast('c.y as bigint)) ASC NULLS FIRST], true   Sort [(cast(x#26 as bigint) + cast(tempresolvedcolumn(c#24.y, c, y, false) as bigint)) ASC NULLS FIRST], true
 +- Aggregate [c#24.x], [c#24.x AS x#26]                                       +- Aggregate [c#24.x], [c#24.x AS x#26]
    +- SubqueryAlias t                                                            +- SubqueryAlias t
       +- LocalRelation [c#24]                                                       +- LocalRelation [c#24]
          ,{})

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.

1 participant