dtenedor opened a new pull request, #60770:
URL: https://github.com/apache/spark/pull/60770

   ### What changes were proposed in this pull request?
   
   `uniform(min, max)` with integral bounds now returns each integer in the 
closed range `[min, max]` with equal probability.
   
   Previously, the replacement expression drew a double from `[min, max)` and 
cast it to the integral result type, which truncates toward zero. This had 
three effects:
   
   - It never returned `max`.
   - It never returned a negative `min`.
   - When the range spanned zero, `0` collected draws from both `(-1, 0)` and 
`[0, 1)`, so it came up twice as often as any other value.
   
   For example, `uniform(0, 1)` and `uniform(-1, 1)` returned `0` on every row, 
and `uniform(1, 6)` never returned `6`.
   
   For integral result types, the replacement now computes `floor(lo + (hi - lo 
+ 1) * rand(seed))`, where `lo = least(min, max)` and `hi = greatest(min, 
max)`. The result is clamped to `[lo, hi]` before the cast to the result type:
   
   - The clamp absorbs double rounding error near the ends of very wide ranges.
   - `Floor` uses `failOnError = false` (saturating), so ranges that touch the 
`BIGINT` limits cannot overflow under ANSI mode.
   - A `NULL` bound still returns `NULL`. `least` and `greatest` skip `NULL`s, 
so the bounds are checked explicitly.
   
   Floating-point and decimal results are unchanged and stay in `[min, max)`.
   
   This PR also adds the internal legacy config 
`spark.sql.legacy.uniformIntegralTruncate` (default `false`), which restores 
the previous behavior.
   
   ### Why are the changes needed?
   
   The previous behavior gives wrong results. Users expect `uniform(1, 6)` to 
behave like a die roll, and other engines treat integral bounds as inclusive. 
PostgreSQL's `random(min, max)` and DuckDB's integer range functions both 
return each integer in `[min, max]`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. `uniform` with integral bounds can now return `max` and a negative 
`min`, and its values are evenly distributed. Results for a given seed differ 
from earlier versions. The SQL migration guide documents the change and the 
legacy config.
   
   ### How was this patch tested?
   
   - Regenerated `random.sql` golden files. The new queries cover:
     - `uniform(1, 6)`, `uniform(0, 1)` and `uniform(-1, 1)` reach every value 
in the range.
     - Reversed bounds.
     - Equal bounds.
     - The full `TINYINT` range.
     - The full `BIGINT` range and the top of the `BIGINT` range.
     - A distribution balance check.
   - Added a `DataFrameFunctionsSuite` test that covers both the new behavior 
and `spark.sql.legacy.uniformIntegralTruncate=true`.
   - Ran `SQLQueryTestSuite -z random.sql`, `DataFrameFunctionsSuite -z 
uniform` and `DataFrameZipSuite`, and ran scalastyle.
   - The PySpark docstring example output was derived from the same per-row 
`rand` values. Both the old and new mappings are monotone in the draw, so the 
row order is preserved.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Isaac (Claude Opus 5.5)
   
   This pull request and its description were written by Isaac.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to