codewithsruthi commented on code in PR #20363:
URL: https://github.com/apache/pinot/pull/20363#discussion_r4673979544
##########
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:
Thanks for catching this! Guarded snapping with `Double.isFinite(start +
nearest * inc)` (and `Float.isFinite(start + nearest * inc)` in the `float`
overload) and added regression coverage for both `double` and `float` in
ascending and descending 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:
Good catch! Tightened the tolerance to `(Math.ulp(start) + Math.ulp(end) +
nearest * Math.ulp(inc)) / Math.abs(inc)` so `generateFloatArray(0f, 99999.96f,
1f)` (and descending `(99999.96f, 0f, -1f)`) keeps its 100,000 elements without
snapping past the limit, and added regression tests for both.
--
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]