Repository navigation
Commit 05b7e11
fix: avoid overflow in join cardinality estimation (#23788)
## Which issue does this PR close?
- Closes #23787.
## Rationale for this change
Join cardinality estimation multiplies two `usize` row counts before
applying the join-key NDV divisor. A large Cartesian-product
intermediate can therefore overflow and panic even when the normalized
cardinality is representable. Saturating the product before division
would avoid the panic but produce an inaccurate estimate.
## What changes are included in this PR?
- Compute the Cartesian-product intermediate with `u128` before applying
the NDV divisor.
- Preserve exact estimates when the normalized result fits in `usize`.
- Cap an unrepresentable final result at `usize::MAX` and mark it
inexact, consistent with existing statistics arithmetic.
- Add regression coverage for exact and inexact statistics and for
final-result overflow.
## Are these changes tested?
Yes.
## Are there any user-facing changes?
Queries with very large join statistics no longer panic during
cardinality estimation. There are no public API changes.
Co-authored-by: Qi Zhu <[email protected]>1 parent 39e2f23 commit 05b7e11
1 file changed
Lines changed: 55 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
846 | 846 | | |
847 | 847 | | |
848 | 848 | | |
849 | | - | |
850 | | - | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
851 | 856 | | |
852 | | - | |
853 | | - | |
854 | | - | |
855 | | - | |
856 | | - | |
857 | | - | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
858 | 865 | | |
859 | 866 | | |
860 | 867 | | |
| |||
3071 | 3078 | | |
3072 | 3079 | | |
3073 | 3080 | | |
| 3081 | + | |
| 3082 | + | |
| 3083 | + | |
| 3084 | + | |
| 3085 | + | |
| 3086 | + | |
| 3087 | + | |
| 3088 | + | |
| 3089 | + | |
| 3090 | + | |
| 3091 | + | |
| 3092 | + | |
| 3093 | + | |
| 3094 | + | |
| 3095 | + | |
| 3096 | + | |
| 3097 | + | |
| 3098 | + | |
| 3099 | + | |
| 3100 | + | |
| 3101 | + | |
| 3102 | + | |
| 3103 | + | |
| 3104 | + | |
| 3105 | + | |
| 3106 | + | |
| 3107 | + | |
| 3108 | + | |
| 3109 | + | |
| 3110 | + | |
| 3111 | + | |
| 3112 | + | |
| 3113 | + | |
| 3114 | + | |
| 3115 | + | |
| 3116 | + | |
| 3117 | + | |
| 3118 | + | |
| 3119 | + | |
| 3120 | + | |
3074 | 3121 | | |
3075 | 3122 | | |
3076 | 3123 | | |
| |||
0 commit comments