Commit 11435de
authored
Core: Fix incorrect searched CASE optimization (#14349)
* Fix incorrect searched CASE optimization
There is an optimization for searched CASE where values are of boolean
type. It was converting the expression like
CASE
WHEN X THEN A
WHEN Y THEN B
..
[ ELSE D ]
END
into
(X AND A)
OR (Y AND NOT X AND B)
[ OR (NOT (X OR Y) AND D) ]
This had the following problems
- does not work for nullable conditions. If X is nullable, we cannot use
NOT (X) to compliment it. We need to use `X IS DISTINCT FROM true`
- it does not work correctly when some conditions are nullable and other
values are false. E.g. X=NULL, A=true, Y=NULL, B=true, D=false, the
CASE should return false, but the boolean expression will simplify to
`(NULL AND ..) OR (NULL AND ..) OR (false)` which is NULL, not false
- thus we use `X` for truthness check of `X`, we need to test `X IS
NOT DISTINCT FROM true`
- it did not work correctly when default D is missing, but conditions
do not evaluate to NULL. CASE's result should be NULL but was false.
This commit fixes that optimization.
* Fix complexity comment1 parent 07ee09a commit 11435de
File tree
2 files changed
+89
-29
lines changed- datafusion
- optimizer/src/simplify_expressions
- sqllogictest/test_files
2 files changed
+89
-29
lines changedLines changed: 74 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1385 | 1385 | | |
1386 | 1386 | | |
1387 | 1387 | | |
1388 | | - | |
| 1388 | + | |
1389 | 1389 | | |
1390 | 1390 | | |
1391 | | - | |
| 1391 | + | |
1392 | 1392 | | |
1393 | 1393 | | |
1394 | 1394 | | |
1395 | 1395 | | |
1396 | 1396 | | |
1397 | | - | |
1398 | | - | |
1399 | | - | |
1400 | | - | |
1401 | | - | |
| 1397 | + | |
| 1398 | + | |
| 1399 | + | |
1402 | 1400 | | |
1403 | 1401 | | |
1404 | | - | |
| 1402 | + | |
1405 | 1403 | | |
1406 | 1404 | | |
1407 | | - | |
1408 | | - | |
1409 | | - | |
1410 | | - | |
| 1405 | + | |
| 1406 | + | |
| 1407 | + | |
1411 | 1408 | | |
1412 | 1409 | | |
1413 | 1410 | | |
| |||
1881 | 1878 | | |
1882 | 1879 | | |
1883 | 1880 | | |
| 1881 | + | |
| 1882 | + | |
| 1883 | + | |
| 1884 | + | |
| 1885 | + | |
| 1886 | + | |
| 1887 | + | |
| 1888 | + | |
| 1889 | + | |
| 1890 | + | |
| 1891 | + | |
| 1892 | + | |
| 1893 | + | |
1884 | 1894 | | |
1885 | 1895 | | |
1886 | 1896 | | |
| |||
3272 | 3282 | | |
3273 | 3283 | | |
3274 | 3284 | | |
3275 | | - | |
| 3285 | + | |
3276 | 3286 | | |
3277 | 3287 | | |
3278 | | - | |
| 3288 | + | |
3279 | 3289 | | |
3280 | | - | |
| 3290 | + | |
3281 | 3291 | | |
3282 | 3292 | | |
3283 | 3293 | | |
| |||
3292 | 3302 | | |
3293 | 3303 | | |
3294 | 3304 | | |
3295 | | - | |
| 3305 | + | |
3296 | 3306 | | |
3297 | 3307 | | |
3298 | | - | |
| 3308 | + | |
3299 | 3309 | | |
3300 | | - | |
| 3310 | + | |
3301 | 3311 | | |
3302 | 3312 | | |
3303 | 3313 | | |
| |||
3328 | 3338 | | |
3329 | 3339 | | |
3330 | 3340 | | |
3331 | | - | |
3332 | | - | |
| 3341 | + | |
| 3342 | + | |
3333 | 3343 | | |
3334 | 3344 | | |
3335 | 3345 | | |
3336 | | - | |
| 3346 | + | |
3337 | 3347 | | |
3338 | 3348 | | |
3339 | 3349 | | |
| |||
3347 | 3357 | | |
3348 | 3358 | | |
3349 | 3359 | | |
3350 | | - | |
3351 | | - | |
| 3360 | + | |
| 3361 | + | |
3352 | 3362 | | |
3353 | 3363 | | |
3354 | 3364 | | |
3355 | | - | |
| 3365 | + | |
| 3366 | + | |
| 3367 | + | |
| 3368 | + | |
| 3369 | + | |
| 3370 | + | |
| 3371 | + | |
| 3372 | + | |
| 3373 | + | |
| 3374 | + | |
| 3375 | + | |
| 3376 | + | |
| 3377 | + | |
| 3378 | + | |
| 3379 | + | |
3356 | 3380 | | |
| 3381 | + | |
| 3382 | + | |
| 3383 | + | |
| 3384 | + | |
| 3385 | + | |
| 3386 | + | |
| 3387 | + | |
| 3388 | + | |
| 3389 | + | |
| 3390 | + | |
| 3391 | + | |
| 3392 | + | |
| 3393 | + | |
| 3394 | + | |
| 3395 | + | |
| 3396 | + | |
| 3397 | + | |
| 3398 | + | |
| 3399 | + | |
| 3400 | + | |
| 3401 | + | |
| 3402 | + | |
| 3403 | + | |
| 3404 | + | |
| 3405 | + | |
| 3406 | + | |
3357 | 3407 | | |
3358 | 3408 | | |
3359 | 3409 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
289 | 289 | | |
290 | 290 | | |
291 | 291 | | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
297 | 297 | | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
298 | 308 | | |
299 | 309 | | |
300 | 310 | | |
0 commit comments