Repository navigation
Commit 833f499
[dbolympics] fix: cast outer projection expr to declared field type in SingleDistinctToGroupBy (#26211)
## Which issue does this PR close?
Closes #26193.
Closes #17699.
## Rationale for this change
When the SingleDistinctToGroupBy optimizer rule rewrites an aggregate
combining DISTINCT and non-distinct expressions such as sum over
Decimal128, the non-distinct expression is split into inner and outer
phases. The outer phase evaluates sum over the already-widened Decimal
output of the inner aggregation, which widens precision a second time
and causes the resulting schema to diverge from the original aggregate
schema. This type divergence also triggers runtime comparison errors in
subsequent expressions like nullif or DB Olympics query q158.
## What changes are included in this PR?
The outer projection construction in SingleDistinctToGroupBy checks
whether the data type produced by outer_aggr differs from the field type
declared in the original schema. When a mismatch occurs, the projected
expression is wrapped with a cast back to the declared field type,
ensuring schema consistency across optimizer passes.
## Are these changes tested?
Covered by unit tests in single_distinct_to_groupby.rs and end-to-end
sqllogictests in single_distinct_to_groupby.slt and aggregate.slt
pinning the returned batch types, EXPLAIN plan with CAST, and
multi-aggregate group-by behavior.
## Are there any user-facing changes?
Yes, aggregate queries rewritten by SingleDistinctToGroupBy containing
Decimal expressions (such as sum over Decimal128) now retain their
original declared Decimal precision and scale rather than producing an
unintentionally expanded Decimal type.
Co-authored-by: Andrew Lamb <[email protected]>1 parent 0442090 commit 833f499
3 files changed
Lines changed: 83 additions & 6 deletions
File tree
- datafusion
- optimizer/src
- sqllogictest/test_files
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| |||
402 | 402 | | |
403 | 403 | | |
404 | 404 | | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
405 | 411 | | |
406 | 412 | | |
407 | 413 | | |
| |||
417 | 423 | | |
418 | 424 | | |
419 | 425 | | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
420 | 432 | | |
421 | 433 | | |
422 | 434 | | |
423 | 435 | | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | 436 | | |
430 | 437 | | |
431 | 438 | | |
| |||
1310 | 1317 | | |
1311 | 1318 | | |
1312 | 1319 | | |
| 1320 | + | |
| 1321 | + | |
| 1322 | + | |
| 1323 | + | |
| 1324 | + | |
| 1325 | + | |
| 1326 | + | |
| 1327 | + | |
| 1328 | + | |
| 1329 | + | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + | |
| 1333 | + | |
| 1334 | + | |
| 1335 | + | |
| 1336 | + | |
| 1337 | + | |
| 1338 | + | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
| 1343 | + | |
| 1344 | + | |
| 1345 | + | |
1313 | 1346 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10629 | 10629 | | |
10630 | 10630 | | |
10631 | 10631 | | |
| 10632 | + | |
| 10633 | + | |
| 10634 | + | |
| 10635 | + | |
| 10636 | + | |
| 10637 | + | |
| 10638 | + | |
| 10639 | + | |
| 10640 | + | |
| 10641 | + | |
| 10642 | + | |
| 10643 | + | |
| 10644 | + | |
| 10645 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
453 | 453 | | |
454 | 454 | | |
455 | 455 | | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
0 commit comments