Jackie-Jiang commented on code in PR #20363:
URL: https://github.com/apache/pinot/pull/20363#discussion_r4673831501
##########
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/ArrayFunctions.java:
##########
@@ -642,21 +642,42 @@ private static int generatedArrayLength(long start, long
end, long inc) {
}
/// Length of the `float` sequence `start, start + inc, ...` bounded by
`end`, inclusive. The step count is computed
- /// in `float` arithmetic, as the single-stage engine always did.
+ /// in `float` arithmetic, as the single-stage engine always did, and
snapped to the nearest integer when rounding
+ /// leaves `(end - start) / inc` within ULP tolerance below that integer.
private static int generatedArrayLength(float start, float end, float inc) {
checkFloatingPointArguments(start, end, inc);
- // TODO: decimal steps such as 0.1 are not exact in binary, so this can
drop the end; see apache/pinot#19814
- return checkGeneratedArrayLength((long) ((end - start) / inc + 1) - 1);
+ float rawSteps = (end - start) / inc;
+ if (Float.isFinite(rawSteps) && rawSteps < MAX_GENERATED_ARRAY_LENGTH) {
+ float nearest = (float) Math.rint(rawSteps);
+ if (nearest > rawSteps) {
+ float maxBound = Math.max(Math.abs(start), Math.abs(end));
+ float tolerance = Math.min(0.25f, 3.0f * (Math.ulp(maxBound) /
Math.abs(inc) + Math.ulp(nearest)));
+ if (nearest - rawSteps <= tolerance) {
+ return checkGeneratedArrayLength((long) nearest);
+ }
+ }
+ }
+ return checkGeneratedArrayLength((long) rawSteps);
}
- /// Length of the `double` sequence `start, start + inc, ...` bounded by
`end`, inclusive.
+ /// Length of the `double` sequence `start, start + inc, ...` bounded by
`end`, inclusive. The step count is snapped
+ /// to the nearest integer when floating-point rounding leaves `(end -
start) / inc` within ULP tolerance below that
+ /// integer.
private static int generatedArrayLength(double start, double end, double
inc) {
checkFloatingPointArguments(start, end, inc);
- // Adding 1 before truncating matches the length the single-stage engine
always computed. It absorbs part of the
- // rounding error of decimal steps, so 0.1 to 0.3 by 0.1 keeps its end.
- // TODO: it does not absorb all of it, so 0.1 to 0.7 by 0.1 still drops
its end; see apache/pinot#19814
+ double rawSteps = (end - start) / inc;
+ if (Double.isFinite(rawSteps) && rawSteps < MAX_GENERATED_ARRAY_LENGTH) {
+ double nearest = Math.rint(rawSteps);
+ if (nearest > rawSteps) {
+ double maxBound = Math.max(Math.abs(start), Math.abs(end));
+ double tolerance = Math.min(0.25, 3.0 * (Math.ulp(maxBound) /
Math.abs(inc) + Math.ulp(nearest)));
+ if (nearest - rawSteps <= tolerance) {
+ return checkGeneratedArrayLength((long) nearest);
Review Comment:
Snapping can append `Infinity` even though all arguments are finite. For
example:
```java
double inc = Math.nextUp(Math.nextUp(Double.MAX_VALUE / 2));
ArrayFunctions.generateDoubleArray(0, Double.MAX_VALUE, inc);
```
Here `rawSteps` is `1.9999999999999993`. The previous formula returns two
finite elements, but this branch rounds up to two steps and the third element,
`2 * inc`, overflows to `Infinity`. The corresponding `Float.MAX_VALUE` example
also reproduces in the float overload. This exceeds the intended small endpoint
rounding error. Please check that the newly included element remains finite and
add regression coverage for both types and directions.
##########
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/ArrayFunctions.java:
##########
@@ -642,21 +642,42 @@ private static int generatedArrayLength(long start, long
end, long inc) {
}
/// Length of the `float` sequence `start, start + inc, ...` bounded by
`end`, inclusive. The step count is computed
- /// in `float` arithmetic, as the single-stage engine always did.
+ /// in `float` arithmetic, as the single-stage engine always did, and
snapped to the nearest integer when rounding
+ /// leaves `(end - start) / inc` within ULP tolerance below that integer.
private static int generatedArrayLength(float start, float end, float inc) {
checkFloatingPointArguments(start, end, inc);
- // TODO: decimal steps such as 0.1 are not exact in binary, so this can
drop the end; see apache/pinot#19814
- return checkGeneratedArrayLength((long) ((end - start) / inc + 1) - 1);
+ float rawSteps = (end - start) / inc;
+ if (Float.isFinite(rawSteps) && rawSteps < MAX_GENERATED_ARRAY_LENGTH) {
+ float nearest = (float) Math.rint(rawSteps);
+ if (nearest > rawSteps) {
+ float maxBound = Math.max(Math.abs(start), Math.abs(end));
+ float tolerance = Math.min(0.25f, 3.0f * (Math.ulp(maxBound) /
Math.abs(inc) + Math.ulp(nearest)));
+ if (nearest - rawSteps <= tolerance) {
+ return checkGeneratedArrayLength((long) nearest);
Review Comment:
This tolerance can round a genuinely fractional step count up and reject a
valid range. `generateFloatArray(0f, 99999.96f, 1f)` previously returns exactly
100,000 elements (`0..99999`). Subtraction and division are exact here:
`rawSteps = 99999.9609375`, so the endpoint really is below the next integer
step. However, `tolerance = 0.046875` exceeds the gap of `0.0390625`, causing
this branch to request 100,000 steps and throw the size-limit exception. Please
tighten the rounding-error bound so this integer-step range remains valid, and
add a regression at this boundary.
--
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]