IgnatiusPang commented on PR #25743:
URL: https://github.com/apache/datafusion/pull/25743#issuecomment-5835271135

   
   ```markdown
   ### 1. SQL Reproducer (Scalar vs Grouped Divergence)
   
   ```sql
   -- Query 1: Scalar evaluation on single-row NaN returns 0.0
   SELECT var_pop(val) AS v
   FROM (VALUES ('NaN'::double)) AS t(val);
   
   -- Query 2: Grouped evaluation on the EXACT SAME single-row NaN returns NaN
   SELECT grp, var_pop(val) AS v
   FROM (VALUES ('g1', 'NaN'::double)) AS t(grp, val)
   GROUP BY grp;
   ```
   
   **Current Result (unpatched DataFusion):**
   ```text
   Query 1 (Scalar):
   +-----+
   | v   |
   +-----+
   | 0.0 |  <-- Inconsistent!
   +-----+
   
   Query 2 (Grouped):
   +-----+-----+
   | grp | v   |
   +-----+-----+
   | g1  | NaN |  <-- Inconsistent!
   +-----+-----+
   ```
   
   **Expected Result:**
   Both scalar and grouped queries must return `NaN`. A population containing a 
`NaN` has an undefined/NaN variance.
   
   ---
   
   ### 2. Root Cause
   
   In `datafusion/functions-aggregate/src/variance.rs`:
   ```rust
   // VarianceAccumulator::evaluate (Scalar):
   match self.count {
       0 => None,
       1 => {
           if self.stats_type == StatsType::Population {
               Some(0.0) // <-- Hardcoded 0.0, ignores self.m2.is_nan()!
           } else {
               None
           }
       }
       _ => ...
   }
   ```
   In contrast, `VarianceGroupsAccumulator::variance_values` evaluates `m2 / 
count = NaN / 1.0 = NaN`.
   
   ### 3. Proposed Fix
   Check `if self.m2.is_nan() { Some(f64::NAN) }` before returning `Some(0.0)` 
for single-row datasets in `VarianceAccumulator` and 
`DistinctVarianceAccumulator`.
   ```


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