Commit 292ff08
fix(vortex-row): decimal sort keys are not memcmp-comparable across chunks (#8937)
## The bug
`ORDER BY` / top-k on a decimal column silently returns wrongly ordered
rows whenever the column spans chunks whose physical value widths
differ. No error is raised; the output looks plausible.
### Example
Take a `DECIMAL(7, 5)` tip column and `ORDER BY tip DESC`. Two rows land
in different chunks, and compression picks a different physical width
for each chunk:
| row | unscaled value | chunk's physical type |
|---|---|---|
| `tip = 0.00484` | `484` | **i16** (484 doesn't fit i8) |
| `tip = 0.00091` | `91` | **i8** (every value in its chunk fits i8) |
A descending sort key is built per chunk: write the value big-endian,
flip the sign bit, invert the value bytes (for DESC), and prefix the
non-null sentinel `0xFE`. Before this fix, the number of value bytes
came from the *chunk's* type:
```
tip = 0.00484, encoded at its chunk's width (i16 → 2 value bytes)
big-endian bytes 01 E4
flip sign bit 81 E4
invert for DESC 7E 1B
prepend sentinel FE 7E 1B ← 3-byte key
tip = 0.00091, encoded at its chunk's width (i8 → 1 value byte)
big-endian bytes 5B
flip sign bit DB
invert for DESC 24
prepend sentinel FE 24 ← 2-byte key
```
The sorter compares keys with `memcmp`:
```
key(0.00484) = FE 7E 1B
key(0.00091) = FE 24
─┬ ─┬
│ └─ byte 1 decides: 0x24 < 0x7E, so key(0.00091) is the smaller key
└─ byte 0: equal
```
But byte 1 means different things in the two keys: in the 3-byte key it
is the *high-order* byte of a two-byte number, while in the 2-byte key
it is the *only* byte of a one-byte number. The comparison is
meaningless — and DESC encoding promises **bigger value ⇒ smaller key**,
so the smaller key wins: the sorter ranks `0.00091` as a larger tip than
`0.00484`. Ascending breaks symmetrically.
With this fix, both chunks encode at the width the *declared* dtype
implies (`DECIMAL(7,5)` → i32 → 4 value bytes), whatever their physical
storage:
```
key(0.00484) = FE 7F FF FE 1B
key(0.00091) = FE 7F FF FF A4
─┬
└─ byte 3 decides: 0xFE < 0xFF, so key(0.00484) is smaller
```
Equal-length keys, every byte position aligned — `0.00484` correctly
sorts first in the descending order.
## The fix
Derive the key width from the declared dtype, so every chunk of a column
encodes identical-length keys:
- `decimal_key_type` (vortex-row): the dtype-derived width, used by both
the sizing pass and the encode dispatch.
- `converted_buffer<W>` (vortex-array, next to `widened_buffer`):
returns a chunk's values at exactly width `W`. Zero-copy when the chunk
is already stored at `W` (the common case), lossless widening otherwise,
and an error for any value that violates its declared precision, such
values previously encoded a garbage key silently.
The encode loop itself is unchanged; only where it reads its values from
changed.
---------
Signed-off-by: Nemo Yu <zyu379@wisc.edu>
Co-authored-by: Robert Kruszewski <github@robertk.io>1 parent d9619b8 commit 292ff08
3 files changed
Lines changed: 157 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
8 | 11 | | |
9 | 12 | | |
10 | 13 | | |
| |||
28 | 31 | | |
29 | 32 | | |
30 | 33 | | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
31 | 91 | | |
32 | 92 | | |
33 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
| 37 | + | |
36 | 38 | | |
37 | 39 | | |
38 | 40 | | |
39 | 41 | | |
| 42 | + | |
40 | 43 | | |
41 | 44 | | |
42 | 45 | | |
| |||
183 | 186 | | |
184 | 187 | | |
185 | 188 | | |
186 | | - | |
| 189 | + | |
187 | 190 | | |
188 | 191 | | |
189 | 192 | | |
| |||
385 | 388 | | |
386 | 389 | | |
387 | 390 | | |
388 | | - | |
| 391 | + | |
389 | 392 | | |
390 | 393 | | |
391 | 394 | | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
392 | 404 | | |
393 | 405 | | |
394 | 406 | | |
| |||
634 | 646 | | |
635 | 647 | | |
636 | 648 | | |
637 | | - | |
| 649 | + | |
638 | 650 | | |
639 | 651 | | |
640 | 652 | | |
| |||
654 | 666 | | |
655 | 667 | | |
656 | 668 | | |
657 | | - | |
658 | 669 | | |
659 | 670 | | |
660 | 671 | | |
| |||
664 | 675 | | |
665 | 676 | | |
666 | 677 | | |
667 | | - | |
| 678 | + | |
| 679 | + | |
668 | 680 | | |
669 | 681 | | |
670 | 682 | | |
671 | 683 | | |
672 | 684 | | |
673 | 685 | | |
674 | | - | |
| 686 | + | |
675 | 687 | | |
676 | 688 | | |
677 | 689 | | |
| |||
685 | 697 | | |
686 | 698 | | |
687 | 699 | | |
| 700 | + | |
688 | 701 | | |
689 | 702 | | |
690 | 703 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
| 25 | + | |
| 26 | + | |
23 | 27 | | |
24 | 28 | | |
25 | 29 | | |
| |||
614 | 618 | | |
615 | 619 | | |
616 | 620 | | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
0 commit comments