Skip to content

[SPARK-52439][SQL] Support creating check constraint with NULL #51146

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 6 commits into
base: master
Choose a base branch
from

Conversation

gengliangwang
Copy link
Member

What changes were proposed in this pull request?

  • Support check constraint with NULL value
  • Add test cases for constraints with constant expressions.
  • Add test for verifying table delete with table constraints

Why are the changes needed?

Before the changes, there will be complation error if creating a check constraint with null check(null). This PR fixes the issue.

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 Jun 10, 2025
@gengliangwang gengliangwang requested a review from cloud-fan June 10, 2025 23:07
@gengliangwang
Copy link
Member Author

cc @aokolnychyi as well

@gengliangwang gengliangwang changed the title [SPARK-52439][SQL] Support check constraint with null value [SPARK-52439][SQL] Support creating check constraint with NULL Jun 10, 2025
@@ -78,6 +78,7 @@ class V2ExpressionBuilder(e: Expression, isPredicate: Boolean = false) extends L
expr: Expression, isPredicate: Boolean = false): Option[V2Expression] = expr match {
case Literal(true, BooleanType) => Some(new AlwaysTrue())
case Literal(false, BooleanType) => Some(new AlwaysFalse())
case Cast(Literal(null, NullType), BooleanType, _, _) if isPredicate => Some(new AlwaysNull())
Copy link
Contributor

Choose a reason for hiding this comment

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

why is it not constant folded to Literal(null, BooleanType)?

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 happens in analyzer rule ResolveTableSpec.
If we proactively constant folding it, the current-like expressions won't be resolved

* @since 4.1.0
*/
@Evolving
public final class AlwaysNull extends Predicate implements Literal<Boolean> {
Copy link
Contributor

Choose a reason for hiding this comment

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

do we have to add it? can v2 Literal do the work?

Copy link
Member Author

Choose a reason for hiding this comment

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

It has to be a V2 Predicate:
https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/V2ExpressionBuilder.scala#L58

Also, having AlwaysNull besides AlwaysTrue and AlwaysFalse is reasonable.

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.

2 participants