Commit 50cdbde
authored
fix: preserve NULL semantics in bitwise xor simplification (#24248)
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes #123` indicates that this PR will close issue #123.
-->
- Part of #24246 .
## Rationale for this change
Bitwise XOR simplifications cancelled repeated nullable operands, which
could incorrectly produce a non-NULL result when the operand was NULL.
For example:
```sql
SELECT
i XOR i,
(i XOR 7) XOR i,
i XOR (7 XOR i)
FROM (VALUES (NULL::INT)) AS t(i);
```
These expressions should all return NULL, but simplification could
replace them with 0, 7, and 7.
<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
Please explain the problem you are trying to solve in terms of the
user-visible
behavior, rather than the implementation.
For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of
the
implementation. "COUNT(DISTINCT) returns wrong results when the column
contains
nulls" is the user-visible problem.
-->
## What changes are included in this PR?
Only cancel repeated XOR operands when the removed operand is
non-nullable.
<!--
There is no need to duplicate the description in the issue here, but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
Yes. Added sqllogictests covering results.
## Are there any user-facing changes?
Yes. Bitwise XOR expressions with repeated nullable operands now
correctly preserve NULLs.
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
If there are any breaking changes to public APIs, please add the `api
change` label.
-->1 parent 90164e1 commit 50cdbde
2 files changed
Lines changed: 73 additions & 26 deletions
File tree
- datafusion
- optimizer/src/simplify_expressions
- sqllogictest/test_files
Lines changed: 62 additions & 26 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1372 | 1372 | | |
1373 | 1373 | | |
1374 | 1374 | | |
1375 | | - | |
| 1375 | + | |
1376 | 1376 | | |
1377 | 1377 | | |
1378 | 1378 | | |
| |||
1389 | 1389 | | |
1390 | 1390 | | |
1391 | 1391 | | |
1392 | | - | |
| 1392 | + | |
1393 | 1393 | | |
1394 | 1394 | | |
1395 | 1395 | | |
| |||
3020 | 3020 | | |
3021 | 3021 | | |
3022 | 3022 | | |
3023 | | - | |
| 3023 | + | |
3024 | 3024 | | |
3025 | | - | |
3026 | | - | |
| 3025 | + | |
| 3026 | + | |
| 3027 | + | |
| 3028 | + | |
| 3029 | + | |
3027 | 3030 | | |
3028 | 3031 | | |
3029 | 3032 | | |
3030 | 3033 | | |
3031 | | - | |
3032 | | - | |
| 3034 | + | |
| 3035 | + | |
3033 | 3036 | | |
3034 | 3037 | | |
3035 | 3038 | | |
| |||
3038 | 3041 | | |
3039 | 3042 | | |
3040 | 3043 | | |
3041 | | - | |
| 3044 | + | |
3042 | 3045 | | |
3043 | | - | |
3044 | | - | |
| 3046 | + | |
| 3047 | + | |
| 3048 | + | |
| 3049 | + | |
| 3050 | + | |
| 3051 | + | |
| 3052 | + | |
| 3053 | + | |
3045 | 3054 | | |
3046 | 3055 | | |
3047 | 3056 | | |
3048 | 3057 | | |
3049 | | - | |
| 3058 | + | |
3050 | 3059 | | |
3051 | | - | |
3052 | | - | |
| 3060 | + | |
| 3061 | + | |
3053 | 3062 | | |
3054 | 3063 | | |
3055 | 3064 | | |
| |||
3060 | 3069 | | |
3061 | 3070 | | |
3062 | 3071 | | |
3063 | | - | |
3064 | | - | |
| 3072 | + | |
| 3073 | + | |
| 3074 | + | |
| 3075 | + | |
| 3076 | + | |
3065 | 3077 | | |
3066 | | - | |
| 3078 | + | |
3067 | 3079 | | |
3068 | 3080 | | |
3069 | 3081 | | |
3070 | | - | |
3071 | | - | |
| 3082 | + | |
| 3083 | + | |
3072 | 3084 | | |
3073 | 3085 | | |
3074 | 3086 | | |
| |||
3078 | 3090 | | |
3079 | 3091 | | |
3080 | 3092 | | |
3081 | | - | |
3082 | | - | |
| 3093 | + | |
| 3094 | + | |
| 3095 | + | |
| 3096 | + | |
| 3097 | + | |
| 3098 | + | |
| 3099 | + | |
| 3100 | + | |
3083 | 3101 | | |
3084 | | - | |
| 3102 | + | |
3085 | 3103 | | |
3086 | 3104 | | |
3087 | 3105 | | |
3088 | 3106 | | |
3089 | | - | |
3090 | | - | |
| 3107 | + | |
| 3108 | + | |
3091 | 3109 | | |
3092 | | - | |
| 3110 | + | |
3093 | 3111 | | |
3094 | 3112 | | |
3095 | 3113 | | |
3096 | 3114 | | |
3097 | 3115 | | |
| 3116 | + | |
| 3117 | + | |
| 3118 | + | |
| 3119 | + | |
| 3120 | + | |
| 3121 | + | |
| 3122 | + | |
| 3123 | + | |
| 3124 | + | |
| 3125 | + | |
| 3126 | + | |
| 3127 | + | |
| 3128 | + | |
| 3129 | + | |
| 3130 | + | |
| 3131 | + | |
| 3132 | + | |
| 3133 | + | |
3098 | 3134 | | |
3099 | 3135 | | |
3100 | 3136 | | |
| |||
3184 | 3220 | | |
3185 | 3221 | | |
3186 | 3222 | | |
3187 | | - | |
| 3223 | + | |
3188 | 3224 | | |
3189 | 3225 | | |
3190 | 3226 | | |
3191 | 3227 | | |
3192 | 3228 | | |
3193 | | - | |
| 3229 | + | |
3194 | 3230 | | |
3195 | 3231 | | |
3196 | 3232 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1444 | 1444 | | |
1445 | 1445 | | |
1446 | 1446 | | |
| 1447 | + | |
| 1448 | + | |
| 1449 | + | |
| 1450 | + | |
| 1451 | + | |
| 1452 | + | |
| 1453 | + | |
| 1454 | + | |
| 1455 | + | |
| 1456 | + | |
| 1457 | + | |
1447 | 1458 | | |
1448 | 1459 | | |
1449 | 1460 | | |
| |||
0 commit comments