Skip to content

opt: simplify leak-proof boolean CASE statements#96545

Open
DrewKimball wants to merge 1 commit intocockroachdb:masterfrom
DrewKimball:bool-case
Open

opt: simplify leak-proof boolean CASE statements#96545
DrewKimball wants to merge 1 commit intocockroachdb:masterfrom
DrewKimball:bool-case

Conversation

@DrewKimball
Copy link
Copy Markdown
Collaborator

This patch adds a normalization rule SimplifyLeakproofBooleanCase that replaces a leak-proof boolean CASE statement with an equivalent boolean expression built using AND and OR operators. This is possible when the following conditions are satisfied:

  1. The CASE statement returns a boolean value.
  2. The CASE statement is leak-proof.
  3. The CASE condition is a constant boolean value (True, False, NULL).
  4. Each WHEN branch returns a constant boolean value.

Example:

CASE WHEN a THEN False WHEN b THEN True ELSE c END
=>
((a IS NOT True) AND (b OR c))

See the ConvertCaseToCondition comment for details.

This is useful for strict UDFs, which implement the argument NULL check using a CASE statement.

Informs #96218

Release note: None

This patch adds a normalization rule `SimplifyLeakproofBooleanCase`
that replaces a leak-proof boolean CASE statement with an
equivalent boolean expression built using `AND` and `OR` operators.
This is possible when the following conditions are satisfied:
  1. The CASE statement returns a boolean value.
  2. The CASE statement is leak-proof.
  3. The CASE condition is a *constant* boolean value (True, False, NULL).
  4. Each WHEN branch returns a *constant* boolean value.

Example:
```
CASE WHEN a THEN False WHEN b THEN True ELSE c END
=>
((a IS NOT True) AND (b OR c))
```
See the `ConvertCaseToCondition` comment for details.

This is useful for strict UDFs, which implement the argument NULL
check using a CASE statement.

Informs cockroachdb#96218

Release note: None
@DrewKimball DrewKimball requested a review from mgartner February 4, 2023 11:30
@DrewKimball DrewKimball requested a review from a team as a code owner February 4, 2023 11:30
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@mgartner mgartner removed their request for review November 21, 2023 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants