Deduplicate bucket computation in width_bucket_interval_mdn_exact
Is your feature request related to a problem or challenge?
width_bucket_interval_mdn_exact (spark/src/function/math/width_bucket.rs#L300-L431) contains two near-identical blocks, "only month" (about L330-L366) and "months equals" (about L368-L424). Each repeats:
- the ascending/descending out-of-range checks (bucket
0ornum_buckets + 1), - the bucket-width computation with its zero / non-finite null guard,
- the floor-plus-one bucket clamp.
They differ only in how x, l and h are projected to f64 (months vs. day/nanosecond offset relative to l). Duplicated numeric edge-case handling like this is where a boundary fix ends up in only one copy.
Describe the solution you'd like
Extract fn bucket_from_f64(x: f64, l: f64, h: f64, num_buckets: i64, ascending: bool) -> Option<i64> (returning None for null). Each branch then only computes its projected (x, l, h) and calls it. Optionally move the per-row body into a helper returning Option<i64> so the continue chains disappear. The existing width_bucket tests cover the behaviour.
Describe alternatives you've considered
None.
Additional context
This is small and self-contained, a good first issue. Line references are pinned to commit 0d79252f61bd.
Hi, I'd like to work on this. I'll submit a PR shortly.
untake
Hi @andygrove, I noticed #26307 is available again. I'd like to extract the shared bucket calculation into bucket_from_f64, preserve existing behavior, and run the width_bucket tests. Would that scope be appropriate?
I would like to take this one. I will extract the shared bucket_from_f64 helper and keep the existing width_bucket tests passing.