kosiew commented on code in PR #27799:
URL: https://github.com/apache/datafusion/pull/27799#discussion_r4766171576


##########
datafusion/sqllogictest/test_files/aggregate.slt:
##########
@@ -1230,6 +1230,30 @@ FROM distinct_pct_nulls ORDER BY id;
 statement ok
 DROP TABLE distinct_pct_nulls;
 
+# Issue #25266: percentile_cont does not round the interpolation weight for 
floats
+query RR
+SELECT percentile_cont(x, 2.5e-7), percentile_cont(2.5e-7) WITHIN GROUP (ORDER 
BY x)
+FROM (VALUES (CAST(0.0 AS DOUBLE)), (CAST(1.0 AS DOUBLE)), (CAST(2.0 AS 
DOUBLE))) AS t(x);
+----
+0.0000005 0.0000005
+
+# Issue #25267: percentile_cont does not overflow for finite Float64 extremes
+query RR
+SELECT percentile_cont(x, 0.5), percentile_cont(0.5) WITHIN GROUP (ORDER BY x)

Review Comment:
   Could we add `median(x)` as a third column and expect `0 0 0`? This would 
cover the separate SQL entry point without duplicating the fixture.



##########
datafusion/functions-aggregate/src/percentile_cont.rs:
##########
@@ -1060,18 +1060,19 @@ where
         upper: T::Native,
         fraction: f64,
     ) -> Result<T::Native> {
-        // Linear interpolation.
-        // We compute a quantized interpolation weight using 
`FLOAT_INTERPOLATION_PRECISION` because:
-        // 1. Both values come from the input data, so (upper - lower) is 
bounded by the value range
-        // 2. fraction is between 0 and 1; quantizing it provides stable, 
predictable results
-        // 3. The result is guaranteed to be between lower_value and 
upper_value (modulo cast rounding)
-        // 4. Arithmetic is performed in f64 and cast back to avoid 
overflowing Float16 intermediates
-        let scaled = (fraction * (INTERPOLATION_PRECISION as f64)) as usize;
-        let weight = scaled as f64 / (INTERPOLATION_PRECISION as f64);
-
+        // Linear interpolation with the exact fraction. The arithmetic is 
done in f64
+        // and cast back to avoid overflowing Float16 intermediates.
         let lower_f: f64 = lower.as_();
         let upper_f: f64 = upper.as_();
-        let interpolated_f = lower_f + (upper_f - lower_f) * weight;
+        let delta = upper_f - lower_f;
+        let interpolated_f =
+            if lower_f.is_finite() && upper_f.is_finite() && 
!delta.is_finite() {
+                // Finite values of opposite sign near the f64 limits can have 
an
+                // infinite difference, so weight each endpoint separately 
instead.
+                lower_f * (1.0 - fraction) + upper_f * fraction
+            } else {
+                lower_f + delta * fraction

Review Comment:
   Could we clarify in the PR description that non-finite endpoint results may 
change despite retaining the formula? For example, interpolating (0, +Infinity) 
at 5e-7 now returns +Infinity instead of NaN. No implementation change is 
needed.



##########
datafusion/functions-aggregate/src/percentile_cont.rs:
##########
@@ -1060,18 +1060,19 @@ where
         upper: T::Native,
         fraction: f64,
     ) -> Result<T::Native> {
-        // Linear interpolation.
-        // We compute a quantized interpolation weight using 
`FLOAT_INTERPOLATION_PRECISION` because:
-        // 1. Both values come from the input data, so (upper - lower) is 
bounded by the value range
-        // 2. fraction is between 0 and 1; quantizing it provides stable, 
predictable results
-        // 3. The result is guaranteed to be between lower_value and 
upper_value (modulo cast rounding)
-        // 4. Arithmetic is performed in f64 and cast back to avoid 
overflowing Float16 intermediates
-        let scaled = (fraction * (INTERPOLATION_PRECISION as f64)) as usize;
-        let weight = scaled as f64 / (INTERPOLATION_PRECISION as f64);
-
+        // Linear interpolation with the exact fraction. The arithmetic is 
done in f64

Review Comment:
   Consider replacing 'exact fraction' with 'unquantized fraction', since f64 
rounding still applies. Please also update the PR description and the 
INTERPOLATION_PRECISION documentation at lines 58-71 to reflect that 
quantization is now decimal-only.



-- 
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