codewithsruthi opened a new pull request, #20363:
URL: https://github.com/apache/pinot/pull/20363
## Summary
Fixes #19814.
In `ArrayFunctions.generatedArrayLength(float, float, float)` and
`ArrayFunctions.generatedArrayLength(double, double, double)`, the step count
was previously computed via raw truncation:
```java
return checkGeneratedArrayLength((long) ((end - start) / inc + 1) - 1);
```
Because binary floating-point arithmetic cannot represent many decimal
fractions (such as `0.1` or `0.7`) exactly, `(end - start) / inc` can evaluate
to a value just below an integer (for example, `(0.7 - 0.1) / 0.1 ==
5.999999999999999` in `double` and `(0.7f - 0.1f) / 0.1f == 5.9999995f` in
`float`). Casting to `long` truncates toward zero, dropping the final element
`end` from the generated sequence.
Since both the single-stage engine (`GenerateArrayTransformFunction`) and
the multi-stage engine (`GenerateArrayScalarFunction`) delegate sequence
generation to `ArrayFunctions`, fixing `ArrayFunctions` resolves the issue
across both engines.
## Changes
1. **ULP-Aware Step Count Snapping (`ArrayFunctions`)**:
- Compute `rawSteps = (end - start) / inc` in `float` / `double`
precision respectively.
- When `rawSteps` is finite and `< MAX_GENERATED_ARRAY_LENGTH`, compare
`rawSteps` against `nearest = Math.rint(rawSteps)`.
- When `nearest > rawSteps` and `nearest - rawSteps` is within a
ULP-scaled rounding tolerance (`Math.min(0.25, 3.0 * (Math.ulp(maxBound) /
Math.abs(inc) + Math.ulp(nearest)))`, and the `float` equivalent), snap the
step count to `(long) nearest` before passing to `checkGeneratedArrayLength`.
- Preserve element computation `arr[i] = start + i * inc` to avoid
cumulative drift.
- Remove the `#19814` `TODO` comments and update method Javadocs.
2. **Unit Tests (`GenerateArrayFunctionsTest`)**:
- Expanded `testFloatingPointStepKeepsInexactEnd` to cover previously
failing decimal step sequences (`0.1` to `0.7` by `0.1`, descending `0.7` to
`0.1` by `-0.1`, negative ranges `-0.7` to `-0.1`, increments `0.2` and `0.05`,
tiny magnitudes `0.0` to `1e-10` by `1e-11`, and large magnitudes `1e6 + 0.1`
to `1e6 + 0.7` by `0.1`) for both `double` and `float`.
- Expanded `testFloatingPointEndBetweenStepsIsExcluded` to verify that
bounds genuinely short of a step (e.g., `(0.1, 0.6999, 0.1)`, `(0, 0.999999,
0.1)`) do not overshoot.
- Added `MAX_GENERATED_ARRAY_LENGTH` boundary tests with decimal step
snapping and wide floating-point overflow span tests (`-Double.MAX_VALUE` to
`Double.MAX_VALUE`, `-Float.MAX_VALUE` to `Float.MAX_VALUE`).
## Testing Done
- `./mvnw -pl pinot-common,pinot-core -am
'-Dtest=GenerateArrayFunctionsTest,org.apache.pinot.common.function.scalar.ArrayFunctionsTest,GenerateArrayTransformFunctionTest,ScalarTransformFunctionWrapperTest#testGenerateArrayOnColumnsTransformFunction'
-Dsurefire.failIfNoSpecifiedTests=false test`
- `./mvnw -pl pinot-common spotless:apply license:format checkstyle:check
license:check`
--
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]