andygrove commented on issue #179: URL: https://github.com/apache/datafusion-spark/issues/179#issuecomment-7017282982
I prototyped this in Comet (apache/datafusion-comet#4818) and am closing that PR in favour of doing it here, so that `kurtosis` and `skewness` can share one implementation and Comet can use it. The prototype branch is `feat/kurtosis-native-support` on `andygrove/datafusion-comet` (head `4d4e573afe`). The code is in `native/spark-expr/src/agg_funcs/kurtosis.rs` and `welford.rs`. Review found several things that are easy to get wrong, so I'm recording them here. **Layout that worked** - The state is `[n, avg, m2, m3, m4]`, all `Float64`, which is Spark's `CentralMomentAgg` buffer. Keeping that layout and exposing it as the partial state is what would let a Spark partial and a native final exchange bytes. The update and merge kernels are direct ports of Spark's `updateExpressionsDef` and `mergeExpressions`, in the same operation order, so they match bit-for-bit. - The recurrences are order-4 helpers (`moments4_update`, `moments4_merge`) beside the existing variance and covariance helpers. The order-4 recurrence subsumes the order-2 one, so `skewness` (`momentOrder = 3`) is only a different `evaluate` over the same state. That is why #180 should share the accumulator rather than copy it. **Semantics that review caught** - **The divisor is `m2 * m2`, not `m2`.** Spark guards on `m2 == 0`, but `evaluateExpression` divides by `m2 * m2`. For inputs `1e-100` and `2e-100`, `m2` is non-zero but its square underflows to exactly `0.0`. Spark's `Divide` then applies its own zero-divisor rule: `NULL` with ANSI off, `DIVIDE_BY_ZERO` with ANSI on. A plain IEEE divide returns `NaN`. This is a different switch from `spark.sql.legacy.statisticalAggregate`, which governs only the `m2 == 0` branch. - **The ANSI mode is the one captured when the aggregate was built, not the session's current setting.** The `Divide` inside `Kurtosis.evaluateExpression` captures its eval mode at analysis time. A temporary view analyzed with ANSI off and queried with it on returns `NULL`. The reverse raises `DIVIDE_BY_ZERO`. Reading the current session setting gets both wrong. In Comet the serde reads the captured mode, so the function needs an input for the eval mode and cannot read a session flag. - **An empty partial must be merged, not skipped.** Skipping `n2 == 0` in `merge_batch` gives a different answer when other partials overflow to `NaN`. With values near `5e77` in one file and an all-NULL second file, Spark returns `NaN` and a version that skips the empty partial returns `-1.22`. The merge has to run Spark's `mergeExpressions` for every partial, including empty ones. - The ANSI error should carry the `kurtosis(...)` query context, as Spark's does. - A `NaN` input yields `NaN`. A single finite row or all-equal values yield `NULL`, or `NaN` under the legacy flag. A single infinite row yields `NaN`, because `inf - inf` poisons `m2`. **Tests worth porting** - Both of Spark's own `ExpressionDescription` examples (`-0.7014368047529627` and `0.19432323191699075`). - Merge across partitions, compared against Spark rather than a tolerance. A merge-order change in `moments4_merge` is only caught by a bit-exact comparison over several files. The `1e-9` tolerance in the Rust merge test would not catch one. - The two-file empty-partial case above. - The saved-view case in both directions: analyzed with ANSI off and queried with it on, then the reverse. - Use plain queries in the SQL fixtures, not answer-only checks, so a fixture fails if the function silently falls back to Spark. **Performance gap** - The prototype had no `GroupsAccumulator`, so grouped `kurtosis` ran through DataFusion's generic `GroupsAccumulatorAdapter`, which costs one boxed `Accumulator` and a `ScalarValue` round trip per group per batch. `VarianceGroupsAccumulator` in Comet keeps flat `Vec<f64>` state and is the model. The vectorized version should be written once for `kurtosis` and `skewness` against the shared state. I did not benchmark either. **Open question** - Window use still fell back in Comet. If this lands here, the sliding-frame question in the implementation notes (`retract_batch` or recomputation) decides whether windows can run natively. Thank you to @mbutrovich, @rich7420, @sunchao and @comphead for the reviews on the Comet PR. Most of the list above is theirs. -- 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]
