Commit b426232
authored
feat: fix windows decimal casting frame (apache#22174)
## 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 apache#123` indicates that this PR will close issue apache#123.
-->
- Closes apache#22113 .
## Rationale for this change
`RANGE` window frames with a `DECIMAL` `ORDER BY` column crash at
runtime:
```sql
SELECT COUNT(*) OVER (
PARTITION BY 1
ORDER BY cast(1 as decimal(10, 0)) DESC
RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING
) FROM (SELECT 1);
-- Internal error: Uncomparable values: Decimal128(Some(0),11,0),
Decimal128(Some(1),10,0).
```
Frame-bound arithmetic (`value ± delta`) widens the decimal result
precision by 1 (`Decimal(10,0) ± Decimal(10,0) → Decimal(11,0)`).
`search_in_slice` then compares the widened target against the
original `ORDER BY` column, and `ScalarValue::partial_cmp` rejects
decimals whose precision differs — even when the scale matches and the
underlying integer representation is directly comparable.
That precision-equality gate is also inconsistent with SQL semantics:
`DEC(10,0) 1` and `DEC(20,0) 1` represent the same number and should
compare equal.
## What changes are included in this PR?
**`datafusion/common/src/scalar/mod.rs`**
- `ScalarValue::partial_cmp` for `Decimal32` / `Decimal64` /
`Decimal128` / `Decimal256`: compare underlying values whenever scales
match, regardless of declared precision. Different scales still
return `None` (rescaling would be required).
**`datafusion/sqllogictest/test_files/window.slt`**
- Regression block covering the reporter query verbatim plus
`ASC`/`DESC` × `PRECEDING`/`FOLLOWING`, symmetric `N PRECEDING AND N
FOLLOWING`, and a non-zero-scale `DECIMAL(10,2)` case over multi-row
partitions.1 parent 6a57b22 commit b426232
2 files changed
Lines changed: 124 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
592 | 592 | | |
593 | 593 | | |
594 | 594 | | |
595 | | - | |
596 | | - | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
597 | 602 | | |
598 | 603 | | |
599 | | - | |
600 | 604 | | |
601 | 605 | | |
602 | 606 | | |
603 | 607 | | |
604 | | - | |
605 | | - | |
| 608 | + | |
| 609 | + | |
606 | 610 | | |
607 | 611 | | |
608 | | - | |
609 | 612 | | |
610 | 613 | | |
611 | 614 | | |
612 | 615 | | |
613 | | - | |
614 | | - | |
| 616 | + | |
| 617 | + | |
615 | 618 | | |
616 | 619 | | |
617 | | - | |
618 | 620 | | |
619 | 621 | | |
620 | 622 | | |
621 | 623 | | |
622 | | - | |
623 | | - | |
| 624 | + | |
| 625 | + | |
624 | 626 | | |
625 | 627 | | |
626 | | - | |
627 | 628 | | |
628 | 629 | | |
629 | 630 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6431 | 6431 | | |
6432 | 6432 | | |
6433 | 6433 | | |
| 6434 | + | |
| 6435 | + | |
| 6436 | + | |
| 6437 | + | |
| 6438 | + | |
| 6439 | + | |
| 6440 | + | |
| 6441 | + | |
| 6442 | + | |
| 6443 | + | |
| 6444 | + | |
| 6445 | + | |
| 6446 | + | |
| 6447 | + | |
| 6448 | + | |
| 6449 | + | |
| 6450 | + | |
| 6451 | + | |
| 6452 | + | |
| 6453 | + | |
| 6454 | + | |
| 6455 | + | |
| 6456 | + | |
| 6457 | + | |
| 6458 | + | |
| 6459 | + | |
| 6460 | + | |
| 6461 | + | |
| 6462 | + | |
| 6463 | + | |
| 6464 | + | |
| 6465 | + | |
| 6466 | + | |
| 6467 | + | |
| 6468 | + | |
| 6469 | + | |
| 6470 | + | |
| 6471 | + | |
| 6472 | + | |
| 6473 | + | |
| 6474 | + | |
| 6475 | + | |
| 6476 | + | |
| 6477 | + | |
| 6478 | + | |
| 6479 | + | |
| 6480 | + | |
| 6481 | + | |
| 6482 | + | |
| 6483 | + | |
| 6484 | + | |
| 6485 | + | |
| 6486 | + | |
| 6487 | + | |
| 6488 | + | |
| 6489 | + | |
| 6490 | + | |
| 6491 | + | |
| 6492 | + | |
| 6493 | + | |
| 6494 | + | |
| 6495 | + | |
| 6496 | + | |
| 6497 | + | |
| 6498 | + | |
| 6499 | + | |
| 6500 | + | |
| 6501 | + | |
| 6502 | + | |
| 6503 | + | |
| 6504 | + | |
| 6505 | + | |
| 6506 | + | |
| 6507 | + | |
| 6508 | + | |
| 6509 | + | |
| 6510 | + | |
| 6511 | + | |
| 6512 | + | |
| 6513 | + | |
| 6514 | + | |
| 6515 | + | |
| 6516 | + | |
| 6517 | + | |
| 6518 | + | |
| 6519 | + | |
| 6520 | + | |
| 6521 | + | |
| 6522 | + | |
| 6523 | + | |
| 6524 | + | |
| 6525 | + | |
| 6526 | + | |
| 6527 | + | |
| 6528 | + | |
| 6529 | + | |
| 6530 | + | |
| 6531 | + | |
| 6532 | + | |
| 6533 | + | |
| 6534 | + | |
| 6535 | + | |
| 6536 | + | |
| 6537 | + | |
| 6538 | + | |
| 6539 | + | |
| 6540 | + | |
| 6541 | + | |
| 6542 | + | |
| 6543 | + | |
| 6544 | + | |
6434 | 6545 | | |
6435 | 6546 | | |
6436 | 6547 | | |
| |||
0 commit comments