From 3f4eb842f46accaf0d9bf9f547412c7aaaea510b Mon Sep 17 00:00:00 2001 From: rich7420 Date: Sat, 24 Oct 2026 02:55:35 +0800 Subject: [PATCH 1/4] perf: add grouped accumulators for regression aggregates Reuse the grouped variance, covariance and Pearson kernels while preserving Spark regression state layouts, arithmetic and version-dependent null/error behavior. Add grouped update/merge/emission coverage and focused native and Spark benchmarks for issue #6886. --- native/spark-expr/benches/aggregate_stats.rs | 150 ++- .../spark-expr/src/agg_funcs/correlation.rs | 136 ++- native/spark-expr/src/agg_funcs/regr.rs | 851 ++++++++++++++++-- native/spark-expr/src/agg_funcs/welford.rs | 41 +- .../sql-tests/expressions/aggregate/regr.sql | 34 + .../CometAggregateExpressionBenchmark.scala | 37 +- 6 files changed, 1131 insertions(+), 118 deletions(-) diff --git a/native/spark-expr/benches/aggregate_stats.rs b/native/spark-expr/benches/aggregate_stats.rs index c18b16810a9..d60e4461e63 100644 --- a/native/spark-expr/benches/aggregate_stats.rs +++ b/native/spark-expr/benches/aggregate_stats.rs @@ -30,6 +30,7 @@ use arrow::datatypes::{DataType, Field, Schema}; use criterion::{criterion_group, criterion_main, Criterion}; use datafusion::datasource::memory::MemorySourceConfig; use datafusion::datasource::source::DataSourceExec; +use datafusion::execution::context::SessionConfig; use datafusion::execution::TaskContext; use datafusion::logical_expr::AggregateUDF; use datafusion::physical_expr::aggregate::AggregateExprBuilder; @@ -38,8 +39,8 @@ use datafusion::physical_expr::PhysicalExpr; use datafusion::physical_plan::aggregates::{AggregateExec, AggregateMode, PhysicalGroupBy}; use datafusion::physical_plan::ExecutionPlan; use datafusion_comet_spark_expr::{ - ApproxPercentile, Avg, Correlation, Covariance, EvalMode, HllPlusPlus, SparkPercentile, - Variance, + ApproxPercentile, Avg, Correlation, Covariance, EvalMode, HllPlusPlus, Regr, RegrType, + SparkPercentile, Variance, }; use futures::StreamExt; use std::hint::black_box; @@ -172,6 +173,131 @@ fn criterion_benchmark(c: &mut Criterion) { group.finish(); } +// Measure Partial aggregation (update + state emission), including the DataFusion +// grouping path. Full-query Spark benchmarks also cover finalization and shuffle. +fn regression_benchmark(c: &mut Criterion) { + let rt = Runtime::new().unwrap(); + let mut group = c.benchmark_group("regr_partial"); + for (groups, null_every) in [(1024, 0), (1024, 101), (1024, 2), (65536, 0), (1, 0)] { + let batches = regression_batches(groups, null_every); + let partitions = &[batches]; + for (name, kind) in [ + ("regr_sxx", RegrType::SXX), + ("regr_syy", RegrType::SYY), + ("regr_sxy", RegrType::SXY), + ("regr_slope", RegrType::Slope), + ("regr_intercept", RegrType::Intercept), + ("regr_r2", RegrType::R2), + ] { + // Cover every function with and without nulls, and the two compound + // state families for the high-cardinality and single-group controls. + if groups != 1024 && !matches!(kind, RegrType::Slope | RegrType::R2) { + continue; + } + let udf = Arc::new(AggregateUDF::new_from_impl(Regr::new( + kind, + name, + true, + true, + EvalMode::Legacy, + None, + datafusion_comet_spark_expr::create_query_context_map(), + ))); + let group_col: Arc = Arc::new(Column::new("c0", 0)); + // Spark rewrites SXX/SYY to the pair-null-filtered selected column, + // which Comet's serde passes twice to the native moment accumulator. + let indices = match kind { + RegrType::SXX => [4, 4], + RegrType::SYY => [3, 3], + _ => [1, 2], + }; + let inputs: Vec> = indices + .map(|i| Arc::new(Column::new(&format!("c{i}"), i)) as Arc) + .to_vec(); + // Check actual cardinality outside timing. Batches use successive row + // offsets, so the high-cardinality case really contains 64k groups. + let (rows, aggregate) = rt.block_on(agg_test( + partitions, + group_col.clone(), + inputs.clone(), + udf.clone(), + name, + )); + assert_eq!(rows, groups); + let skipped = aggregate + .metrics() + .unwrap() + .sum_by_name("skipped_aggregation_rows") + .map(|metric| metric.as_usize()) + .unwrap_or(0); + assert_eq!(skipped, 0); + let nulls = match null_every { + 0 => "no_null", + 2 => "half_null", + _ => "sparse_null", + }; + group.bench_function(format!("{name}/{groups}_groups/{nulls}"), |b| { + b.to_async(&rt).iter(|| { + agg_test( + partitions, + group_col.clone(), + inputs.clone(), + udf.clone(), + name, + ) + }) + }); + } + } + group.finish(); +} + +fn regression_batches(groups: usize, null_every: usize) -> Vec { + let schema = Arc::new(Schema::new(vec![ + Field::new("c0", DataType::Utf8, false), + Field::new("c1", DataType::Float64, true), + Field::new("c2", DataType::Float64, false), + Field::new("c3", DataType::Float64, true), + Field::new("c4", DataType::Float64, true), + ])); + (0..32) + .map(|batch| { + let mut keys = StringBuilder::new(); + let mut y = Float64Builder::new(); + let mut x = Float64Builder::new(); + let mut paired_x = Float64Builder::new(); + for row in 0..8192 { + let i = batch * 8192 + row; + let xv = i as f64 * 0.25 + 1.0; + let yv = xv * 1.5 + (i % 17) as f64; + let valid = match null_every { + 0 => true, + 2 => !(i / groups).is_multiple_of(2), + // Coprime to the group count and batch size: every batch + // contains sparse nulls and every group still has valid rows. + _ => !i.is_multiple_of(null_every), + }; + keys.append_value(format!("group_{}", i % groups)); + y.append_option(valid.then_some(yv)); + x.append_value(xv); + paired_x.append_option(valid.then_some(xv)); + } + let y: ArrayRef = Arc::new(y.finish()); + RecordBatch::try_new( + schema.clone(), + vec![ + Arc::new(keys.finish()), + y.clone(), + Arc::new(x.finish()), + y, + Arc::new(paired_x.finish()), + ], + ) + .unwrap() + }) + .collect() +} + fn comet_variance(stats_type: StatsType) -> Arc { Arc::new(AggregateUDF::new_from_impl(Variance::new( "variance", @@ -207,18 +333,26 @@ async fn agg_test( input_exprs: Vec>, aggregate_udf: Arc, alias: &str, -) { +) -> (usize, Arc) { let schema = &partitions[0][0].schema(); let scan: Arc = Arc::new(DataSourceExec::new(Arc::new( MemorySourceConfig::try_new(partitions, Arc::clone(schema), None).unwrap(), ))); let aggregate = create_aggregate(scan, group_col, input_exprs, schema, aggregate_udf, alias); - let mut stream = aggregate - .execute(0, Arc::new(TaskContext::default())) - .unwrap(); + // Match Comet's eligibility gate: regression aggregates cannot skip partial + // aggregation (convert_to_state is unsupported by the grouped kernels). + let mut config = SessionConfig::new(); + config + .options_mut() + .execution + .skip_partial_aggregation_probe_ratio_threshold = 1.1; + let context = TaskContext::default().with_session_config(config); + let mut stream = aggregate.execute(0, Arc::new(context)).unwrap(); + let mut rows = 0; while let Some(batch) = stream.next().await { - let _batch = batch.unwrap(); + rows += black_box(batch.unwrap()).num_rows(); } + (rows, aggregate) } fn create_aggregate( @@ -286,6 +420,6 @@ fn config() -> Criterion { criterion_group! { name = benches; config = config(); - targets = criterion_benchmark + targets = criterion_benchmark, regression_benchmark } criterion_main!(benches); diff --git a/native/spark-expr/src/agg_funcs/correlation.rs b/native/spark-expr/src/agg_funcs/correlation.rs index 56108bfa6b0..49ee8aa4c1f 100644 --- a/native/spark-expr/src/agg_funcs/correlation.rs +++ b/native/spark-expr/src/agg_funcs/correlation.rs @@ -23,7 +23,7 @@ use std::sync::Arc; use crate::agg_funcs::covariance::{CovarianceAccumulator, CovarianceGroupsAccumulator}; use crate::agg_funcs::stddev::StddevAccumulator; use crate::agg_funcs::variance::VarianceGroupsAccumulator; -use crate::agg_funcs::welford::ZeroDivisor; +use crate::agg_funcs::welford::{r2_value, ZeroDivisor}; use crate::{EvalMode, QueryContextMap}; use arrow::compute::filter; use datafusion::common::{not_impl_err, Result, ScalarValue}; @@ -136,7 +136,7 @@ impl AggregateUDFImpl for Correlation { &self, _args: AccumulatorArgs, ) -> Result> { - Ok(Box::new(CorrelationGroupsAccumulator::new( + Ok(Box::new(CorrelationGroupsAccumulator::new_corr( self.null_on_divide_by_zero, self.zero_divisor.clone(), ))) @@ -278,33 +278,63 @@ impl Accumulator for CorrelationAccumulator { } } -/// Grouped correlation accumulator. Mirrors the per-row `CorrelationAccumulator` +#[derive(Debug, Clone, Copy)] +enum PearsonResult { + Corr { + null_on_divide_by_zero: bool, + }, + RegrR2 { + constant_dependent_is_perfect_fit: bool, + }, +} + +/// Grouped correlation/R2 accumulator. Mirrors the per-row `CorrelationAccumulator` /// composition: one population covariance + two population variance sub- /// accumulators for the per-column m2 values. Combined filter handles /// "skip rows where either input is null" without filtering `group_indices`. #[derive(Debug)] -struct CorrelationGroupsAccumulator { +pub(super) struct CorrelationGroupsAccumulator { covar: CovarianceGroupsAccumulator, var1: VarianceGroupsAccumulator, var2: VarianceGroupsAccumulator, - null_on_divide_by_zero: bool, + result: PearsonResult, zero_divisor: ZeroDivisor, } impl CorrelationGroupsAccumulator { - fn new(null_on_divide_by_zero: bool, zero_divisor: ZeroDivisor) -> Self { + pub(super) fn new_corr(null_on_divide_by_zero: bool, zero_divisor: ZeroDivisor) -> Self { + Self::new( + PearsonResult::Corr { + null_on_divide_by_zero, + }, + zero_divisor, + ) + } + + pub(super) fn new_regr_r2( + constant_dependent_is_perfect_fit: bool, + zero_divisor: ZeroDivisor, + ) -> Self { + Self::new( + PearsonResult::RegrR2 { + constant_dependent_is_perfect_fit, + }, + zero_divisor, + ) + } + + fn new(result: PearsonResult, zero_divisor: ZeroDivisor) -> Self { // Children run with StatsType::Population, which never hits the // count == 1 sample-divide-by-zero branch, so the children's - // null_on_divide_by_zero is dead code. The top-level evaluate() - // applies the count <= 1 rule. Pass `false` to children to keep - // that intent explicit. + // null_on_divide_by_zero is dead code. The top-level result mode + // applies the appropriate degenerate-case rules. Self { covar: CovarianceGroupsAccumulator::new(StatsType::Population, false), var1: VarianceGroupsAccumulator::new(StatsType::Population, false) .with_pearson_update(), var2: VarianceGroupsAccumulator::new(StatsType::Population, false) .with_pearson_update(), - null_on_divide_by_zero, + result, zero_divisor, } } @@ -410,35 +440,39 @@ impl GroupsAccumulator for CorrelationGroupsAccumulator { let mut values = Vec::with_capacity(n); let mut validity = Vec::with_capacity(n); for i in 0..n { - let count = counts[i]; - if count == 0.0 { - values.push(0.0); - validity.push(false); - continue; - } - if count == 1.0 { - if self.null_on_divide_by_zero { - values.push(0.0); - validity.push(false); - } else { - values.push(f64::NAN); - validity.push(true); - } - continue; - } - // Match Spark's raw-moment product, including overflow, underflow - // and NaN from a zero moment paired with infinity. - let m2_product = m2_1s[i] * m2_2s[i]; - if m2_product == 0.0 { - if let Some(error) = self.zero_divisor.error() { - return Err(error); + let value = match self.result { + PearsonResult::Corr { + null_on_divide_by_zero, + } => { + if counts[i] == 0.0 { + None + } else if counts[i] == 1.0 { + (!null_on_divide_by_zero).then_some(f64::NAN) + } else { + // Preserve Spark's raw-moment overflow/underflow behavior. + let product = m2_1s[i] * m2_2s[i]; + if product == 0.0 { + if let Some(error) = self.zero_divisor.error() { + return Err(error); + } + None + } else { + Some(algo_consts[i] / product.sqrt()) + } + } } - values.push(0.0); - validity.push(false); - continue; - } - values.push(algo_consts[i] / m2_product.sqrt()); - validity.push(true); + PearsonResult::RegrR2 { + constant_dependent_is_perfect_fit, + } => r2_value( + algo_consts[i], + m2_1s[i], + m2_2s[i], + constant_dependent_is_perfect_fit, + &self.zero_divisor, + )?, + }; + values.push(value.unwrap_or_default()); + validity.push(value.is_some()); } Ok(Arc::new(Float64Array::new( @@ -494,7 +528,7 @@ mod groups_tests { fn acc(legacy: bool) -> CorrelationGroupsAccumulator { // null_on_divide_by_zero = !legacy - CorrelationGroupsAccumulator::new(!legacy, zero_divisor(EvalMode::Legacy)) + CorrelationGroupsAccumulator::new_corr(!legacy, zero_divisor(EvalMode::Legacy)) } fn evaluate(a: &mut CorrelationGroupsAccumulator) -> Vec> { @@ -535,7 +569,7 @@ mod groups_tests { zero_divisor(EvalMode::Legacy), ) .unwrap(); - let mut grouped = CorrelationGroupsAccumulator::new( + let mut grouped = CorrelationGroupsAccumulator::new_corr( null_on_divide_by_zero, zero_divisor(EvalMode::Legacy), ); @@ -552,7 +586,7 @@ mod groups_tests { zero_divisor(EvalMode::Legacy), ) .unwrap(); - let mut merged_grouped = CorrelationGroupsAccumulator::new( + let mut merged_grouped = CorrelationGroupsAccumulator::new_corr( null_on_divide_by_zero, zero_divisor(EvalMode::Legacy), ); @@ -580,7 +614,8 @@ mod groups_tests { ]; let mut scalar = CorrelationAccumulator::try_new(true, zero_divisor(EvalMode::Legacy)).unwrap(); - let mut grouped = CorrelationGroupsAccumulator::new(true, zero_divisor(EvalMode::Legacy)); + let mut grouped = + CorrelationGroupsAccumulator::new_corr(true, zero_divisor(EvalMode::Legacy)); scalar.update_batch(&input).unwrap(); grouped.update_batch(&input, &[0, 0], None, 1).unwrap(); let state = scalar @@ -592,7 +627,7 @@ mod groups_tests { let mut merged_scalar = CorrelationAccumulator::try_new(true, zero_divisor(EvalMode::Legacy)).unwrap(); let mut merged_grouped = - CorrelationGroupsAccumulator::new(true, zero_divisor(EvalMode::Legacy)); + CorrelationGroupsAccumulator::new_corr(true, zero_divisor(EvalMode::Legacy)); merged_scalar.merge_batch(&state).unwrap(); merged_grouped.merge_batch(&state, &[0], 1).unwrap(); for result in [ @@ -612,7 +647,7 @@ mod groups_tests { let mut scalar = CorrelationAccumulator::try_new(true, zero_divisor(EvalMode::Legacy)).unwrap(); let mut grouped = - CorrelationGroupsAccumulator::new(true, zero_divisor(EvalMode::Legacy)); + CorrelationGroupsAccumulator::new_corr(true, zero_divisor(EvalMode::Legacy)); let counts = if empty_first { [0.0, 100.0] } else { @@ -681,7 +716,7 @@ mod groups_tests { // count == 1 branch reads, does not matter. let mut scalar = CorrelationAccumulator::try_new(true, zero_divisor(eval_mode)).unwrap(); - let mut grouped = CorrelationGroupsAccumulator::new(true, zero_divisor(eval_mode)); + let mut grouped = CorrelationGroupsAccumulator::new_corr(true, zero_divisor(eval_mode)); scalar.update_batch(&input(0..6)).unwrap(); grouped .update_batch(&input(0..6), &[0; 6], None, 1) @@ -692,7 +727,8 @@ mod groups_tests { for ranges in [[0..3, 3..6], [3..6, 0..3]] { let mut scalar = CorrelationAccumulator::try_new(true, zero_divisor(eval_mode)).unwrap(); - let mut grouped = CorrelationGroupsAccumulator::new(true, zero_divisor(eval_mode)); + let mut grouped = + CorrelationGroupsAccumulator::new_corr(true, zero_divisor(eval_mode)); for range in ranges { let state = partial_state(&input(range)); scalar.merge_batch(&state).unwrap(); @@ -732,7 +768,7 @@ mod groups_tests { Arc::new(Float64Array::from(vec![0.1, 0.1, 0.1])), ]; let mut scalar = CorrelationAccumulator::try_new(true, zero_divisor.clone()).unwrap(); - let mut grouped = CorrelationGroupsAccumulator::new(true, zero_divisor); + let mut grouped = CorrelationGroupsAccumulator::new_corr(true, zero_divisor); scalar.update_batch(&input).unwrap(); grouped.update_batch(&input, &[0, 0, 0], None, 1).unwrap(); for result in [ @@ -756,7 +792,7 @@ mod groups_tests { zero_divisor(EvalMode::Ansi), ) .unwrap(); - let mut grouped = CorrelationGroupsAccumulator::new( + let mut grouped = CorrelationGroupsAccumulator::new_corr( null_on_divide_by_zero, zero_divisor(EvalMode::Ansi), ); @@ -792,7 +828,7 @@ mod groups_tests { zero_divisor(EvalMode::Ansi), ) .unwrap(); - let mut grouped = CorrelationGroupsAccumulator::new( + let mut grouped = CorrelationGroupsAccumulator::new_corr( null_on_divide_by_zero, zero_divisor(EvalMode::Ansi), ); @@ -810,7 +846,7 @@ mod groups_tests { Arc::new(Float64Array::from(vec![1.0, 2.0, 1.0, 2.0])), Arc::new(Float64Array::from(vec![2.0, 4.0, 3.0, 3.0])), ]; - let mut grouped = CorrelationGroupsAccumulator::new( + let mut grouped = CorrelationGroupsAccumulator::new_corr( null_on_divide_by_zero, zero_divisor(EvalMode::Ansi), ); diff --git a/native/spark-expr/src/agg_funcs/regr.rs b/native/spark-expr/src/agg_funcs/regr.rs index b8b7f27e74f..2ba0e4c8cda 100644 --- a/native/spark-expr/src/agg_funcs/regr.rs +++ b/native/spark-expr/src/agg_funcs/regr.rs @@ -32,20 +32,27 @@ //! | `regr_sxy` | `Covariance` | 4 | //! | `regr_r2` | `PearsonCorrelation` | 6 | //! | `regr_slope`/`_intercept` | composite cov + var | 7 | +//! +//! Grouped aggregation reuses [`VarianceGroupsAccumulator`], +//! [`CovarianceGroupsAccumulator`], and [`CorrelationGroupsAccumulator`], +//! preserving the same partial-state layouts. -use arrow::array::{Array, ArrayRef}; +use arrow::array::{Array, ArrayRef, BooleanArray, Float64Array}; use arrow::compute::{and, filter, is_not_null}; use arrow::datatypes::{DataType, Field, FieldRef}; -use datafusion::common::{Result, ScalarValue}; +use datafusion::common::{not_impl_err, Result, ScalarValue}; use datafusion::logical_expr::function::{AccumulatorArgs, StateFieldsArgs}; -use datafusion::logical_expr::{Accumulator, AggregateUDFImpl, Signature, Volatility}; +use datafusion::logical_expr::{ + Accumulator, AggregateUDFImpl, EmitTo, GroupsAccumulator, Signature, Volatility, +}; use datafusion::physical_expr::expressions::format_state_name; use datafusion::physical_expr::expressions::StatsType; use std::sync::Arc; -use crate::agg_funcs::covariance::CovarianceAccumulator; -use crate::agg_funcs::variance::VarianceAccumulator; -use crate::agg_funcs::welford::ZeroDivisor; +use crate::agg_funcs::correlation::CorrelationGroupsAccumulator; +use crate::agg_funcs::covariance::{CovarianceAccumulator, CovarianceGroupsAccumulator}; +use crate::agg_funcs::variance::{VarianceAccumulator, VarianceGroupsAccumulator}; +use crate::agg_funcs::welford::{r2_value, ZeroDivisor}; use crate::{EvalMode, QueryContextMap}; /// The kind of linear-regression statistic to compute. @@ -162,6 +169,34 @@ impl AggregateUDFImpl for Regr { Ok(acc) } + fn groups_accumulator_supported(&self, _args: AccumulatorArgs) -> bool { + true + } + + fn create_groups_accumulator( + &self, + _args: AccumulatorArgs, + ) -> Result> { + Ok(match self.regr_type { + RegrType::SXX | RegrType::SYY => Box::new(RegrMomentGroupsAccumulator { + var: VarianceGroupsAccumulator::new(StatsType::Population, false), + }), + RegrType::SXY => Box::new(RegrCovGroupsAccumulator { + covar: CovarianceGroupsAccumulator::new(StatsType::Population, false), + }), + RegrType::Slope | RegrType::Intercept => Box::new(RegrLineGroupsAccumulator { + covar: CovarianceGroupsAccumulator::new(StatsType::Population, false), + var_x: VarianceGroupsAccumulator::new(StatsType::Population, false), + intercept: self.regr_type == RegrType::Intercept, + filter_var_by_pair_nulls: self.filter_var_by_pair_nulls, + }), + RegrType::R2 => Box::new(CorrelationGroupsAccumulator::new_regr_r2( + self.r2_constant_dependent_is_perfect_fit, + self.zero_divisor.clone(), + )), + }) + } + fn state_fields(&self, args: StateFieldsArgs) -> Result> { let names: &[&str] = match self.num_state_fields() { 3 => &["count", "mean", "m2"], @@ -376,39 +411,13 @@ impl Accumulator for RegrR2Accumulator { } fn evaluate(&mut self) -> Result { - let m2_x = self.var_x.get_m2(); - let m2_y = self.var_y.get_m2(); - // The two degenerate cases (constant dependent y, constant independent x) - // return null and 1.0 respectively, but SPARK-55969 swapped which is which. - let (null_case, perfect_fit_case) = if self.constant_dependent_is_perfect_fit { - // After SPARK-55969: constant x -> null, constant y -> 1.0. - (m2_x == 0.0, m2_y == 0.0) - } else { - // Before SPARK-55969: constant y -> null, constant x -> 1.0. - (m2_y == 0.0, m2_x == 0.0) - }; - if null_case { - Ok(ScalarValue::Float64(None)) - } else if perfect_fit_case { - Ok(ScalarValue::Float64(Some(1.0))) - } else { - // Nonzero moments can still have a product that underflows to zero. - // Spark's Divide handles this only after the constant-input guards. - let product = m2_y * m2_x; - if product == 0.0 { - return match self.zero_divisor.error() { - Some(error) => Err(error), - None => Ok(ScalarValue::Float64(None)), - }; - } - // Mirror Spark's exact evaluation order (corr = ck / sqrt(m2_y * m2_x); - // corr * corr) so the last-ULP rounding matches bit-for-bit. Writing - // it as (ck * ck) / (m2_x * m2_y) is mathematically equal but rounds - // differently. - let ck = self.covar.get_algo_const(); - let corr = ck / product.sqrt(); - Ok(ScalarValue::Float64(Some(corr * corr))) - } + Ok(ScalarValue::Float64(r2_value( + self.covar.get_algo_const(), + self.var_y.get_m2(), + self.var_x.get_m2(), + self.constant_dependent_is_perfect_fit, + &self.zero_divisor, + )?)) } fn size(&self) -> usize { @@ -492,20 +501,13 @@ impl Accumulator for RegrLineAccumulator { } fn evaluate(&mut self) -> Result { - let m2_x = self.var_x.get_m2(); - if m2_x == 0.0 { - // independent variable has no spread (also covers count <= 1) - return Ok(ScalarValue::Float64(None)); - } - let slope = self.covar.get_algo_const() / m2_x; - if self.intercept { - // mean(y) - slope * mean(x); covar fed (x, y) => mean1 = mean(x), mean2 = mean(y) - let mean_x = self.covar.get_mean1(); - let mean_y = self.covar.get_mean2(); - Ok(ScalarValue::Float64(Some(mean_y - slope * mean_x))) - } else { - Ok(ScalarValue::Float64(Some(slope))) - } + Ok(ScalarValue::Float64(line_value( + self.covar.get_algo_const(), + self.var_x.get_m2(), + self.covar.get_mean1(), + self.covar.get_mean2(), + self.intercept, + ))) } fn size(&self) -> usize { @@ -515,6 +517,219 @@ impl Accumulator for RegrLineAccumulator { } } +fn line_value(ck: f64, m2_x: f64, mean_x: f64, mean_y: f64, intercept: bool) -> Option { + if m2_x == 0.0 { + // The independent variable has no spread (also covers count <= 1). + None + } else { + let slope = ck / m2_x; + Some(if intercept { + mean_y - slope * mean_x + } else { + slope + }) + } +} + +/// Grouped raw central moment. Spark has already pair-filtered the selected child. +#[derive(Debug)] +struct RegrMomentGroupsAccumulator { + var: VarianceGroupsAccumulator, +} + +impl GroupsAccumulator for RegrMomentGroupsAccumulator { + fn update_batch( + &mut self, + values: &[ArrayRef], + group_indices: &[usize], + opt_filter: Option<&BooleanArray>, + total_num_groups: usize, + ) -> Result<()> { + self.var + .update_batch(&values[0..1], group_indices, opt_filter, total_num_groups) + } + + fn merge_batch( + &mut self, + values: &[ArrayRef], + group_indices: &[usize], + total_num_groups: usize, + ) -> Result<()> { + self.var + .merge_batch(values, group_indices, total_num_groups) + } + + fn evaluate(&mut self, emit_to: EmitTo) -> Result { + let counts = emit_to.take_needed(&mut self.var.counts); + let _ = emit_to.take_needed(&mut self.var.means); + let m2s = emit_to.take_needed(&mut self.var.m2s); + Ok(Arc::new(Float64Array::from_iter( + counts + .into_iter() + .zip(m2s) + .map(|(count, m2)| (count != 0.0).then_some(m2)), + ))) + } + + fn state(&mut self, emit_to: EmitTo) -> Result> { + self.var.state(emit_to) + } + + fn convert_to_state( + &self, + _values: &[ArrayRef], + _opt_filter: Option<&BooleanArray>, + ) -> Result> { + not_impl_err!("Input batch conversion to state not implemented") + } + + fn size(&self) -> usize { + self.var.size() + } +} + +/// Grouped raw co-moment, with the same four state fields as covariance. +#[derive(Debug)] +struct RegrCovGroupsAccumulator { + covar: CovarianceGroupsAccumulator, +} + +impl GroupsAccumulator for RegrCovGroupsAccumulator { + fn update_batch( + &mut self, + values: &[ArrayRef], + group_indices: &[usize], + opt_filter: Option<&BooleanArray>, + total_num_groups: usize, + ) -> Result<()> { + self.covar + .update_batch(values, group_indices, opt_filter, total_num_groups) + } + + fn merge_batch( + &mut self, + values: &[ArrayRef], + group_indices: &[usize], + total_num_groups: usize, + ) -> Result<()> { + self.covar + .merge_batch(values, group_indices, total_num_groups) + } + + fn evaluate(&mut self, emit_to: EmitTo) -> Result { + let counts = emit_to.take_needed(&mut self.covar.counts); + let _ = emit_to.take_needed(&mut self.covar.mean1s); + let _ = emit_to.take_needed(&mut self.covar.mean2s); + let cs = emit_to.take_needed(&mut self.covar.algo_consts); + Ok(Arc::new(Float64Array::from_iter( + counts + .into_iter() + .zip(cs) + .map(|(count, c)| (count != 0.0).then_some(c)), + ))) + } + + fn state(&mut self, emit_to: EmitTo) -> Result> { + self.covar.state(emit_to) + } + + fn convert_to_state( + &self, + _values: &[ArrayRef], + _opt_filter: Option<&BooleanArray>, + ) -> Result> { + not_impl_err!("Input batch conversion to state not implemented") + } + + fn size(&self) -> usize { + self.covar.size() + } +} + +/// The covariance and x variance keep independent counts: before SPARK-48719, +/// the variance includes x even when y is null. Preserve the seven-field state. +#[derive(Debug)] +struct RegrLineGroupsAccumulator { + covar: CovarianceGroupsAccumulator, + var_x: VarianceGroupsAccumulator, + intercept: bool, + filter_var_by_pair_nulls: bool, +} + +impl GroupsAccumulator for RegrLineGroupsAccumulator { + fn update_batch( + &mut self, + values: &[ArrayRef], + group_indices: &[usize], + opt_filter: Option<&BooleanArray>, + total_num_groups: usize, + ) -> Result<()> { + let cov_input = [Arc::clone(&values[1]), Arc::clone(&values[0])]; + self.covar + .update_batch(&cov_input, group_indices, opt_filter, total_num_groups)?; + // The variance child already skips null x. Modern Spark additionally + // excludes null y; preserve row positions so group_indices still align. + let combined = if self.filter_var_by_pair_nulls && values[0].null_count() != 0 { + let valid_y = is_not_null(&values[0])?; + Some(match opt_filter { + Some(filter) => and(filter, &valid_y)?, + None => valid_y, + }) + } else { + None + }; + self.var_x.update_batch( + &values[1..2], + group_indices, + combined.as_ref().or(opt_filter), + total_num_groups, + ) + } + + fn merge_batch( + &mut self, + values: &[ArrayRef], + group_indices: &[usize], + total_num_groups: usize, + ) -> Result<()> { + self.covar + .merge_batch(&values[0..4], group_indices, total_num_groups)?; + self.var_x + .merge_batch(&values[4..7], group_indices, total_num_groups) + } + + fn evaluate(&mut self, emit_to: EmitTo) -> Result { + let _ = emit_to.take_needed(&mut self.covar.counts); + let means_x = emit_to.take_needed(&mut self.covar.mean1s); + let means_y = emit_to.take_needed(&mut self.covar.mean2s); + let cs = emit_to.take_needed(&mut self.covar.algo_consts); + let _ = emit_to.take_needed(&mut self.var_x.counts); + let _ = emit_to.take_needed(&mut self.var_x.means); + let m2s = emit_to.take_needed(&mut self.var_x.m2s); + Ok(Arc::new(Float64Array::from_iter((0..cs.len()).map(|i| { + line_value(cs[i], m2s[i], means_x[i], means_y[i], self.intercept) + })))) + } + + fn state(&mut self, emit_to: EmitTo) -> Result> { + let mut state = self.covar.state(emit_to)?; + state.extend(self.var_x.state(emit_to)?); + Ok(state) + } + + fn convert_to_state( + &self, + _values: &[ArrayRef], + _opt_filter: Option<&BooleanArray>, + ) -> Result> { + not_impl_err!("Input batch conversion to state not implemented") + } + + fn size(&self) -> usize { + self.covar.size() + self.var_x.size() + } +} + #[cfg(test)] mod tests { use super::*; @@ -954,3 +1169,533 @@ mod tests { } } } + +#[cfg(test)] +mod groups_tests { + use super::*; + use crate::{create_query_context_map, Correlation, QueryContext, SparkErrorWithContext}; + use arrow::array::AsArray; + use arrow::datatypes::{Float64Type, Schema}; + use datafusion::common::DataFusionError; + use datafusion::logical_expr::AggregateUDF; + use datafusion::physical_expr::aggregate::{AggregateExprBuilder, AggregateFunctionExpr}; + use datafusion::physical_expr::expressions::Column; + use datafusion_comet_common::SparkError; + use std::mem::size_of; + + const KINDS: [RegrType; 6] = [ + RegrType::SXX, + RegrType::SYY, + RegrType::SXY, + RegrType::Slope, + RegrType::Intercept, + RegrType::R2, + ]; + + fn physical(udf: AggregateUDF) -> AggregateFunctionExpr { + let schema = Arc::new(Schema::new(vec![ + Field::new("y", DataType::Float64, true), + Field::new("x", DataType::Float64, true), + ])); + let expr = AggregateExprBuilder::new( + Arc::new(udf), + vec![Arc::new(Column::new("y", 0)), Arc::new(Column::new("x", 1))], + ) + .schema(schema) + .alias("stat") + .build() + .unwrap(); + assert!(expr.groups_accumulator_supported()); + expr + } + + fn expression(kind: RegrType, modern: bool) -> AggregateFunctionExpr { + physical(AggregateUDF::new_from_impl(Regr::new( + kind, + "regr", + modern, + modern, + EvalMode::Legacy, + None, + create_query_context_map(), + ))) + } + + fn inputs(kind: RegrType, y: &[Option], x: &[Option]) -> Vec { + if matches!(kind, RegrType::SXX | RegrType::SYY) { + let selected = y.iter().zip(x).map(|(&y, &x)| { + y.zip(x) + .map(|(y, x)| if kind == RegrType::SXX { x } else { y }) + }); + let column: ArrayRef = Arc::new(Float64Array::from_iter(selected)); + vec![Arc::clone(&column), column] + } else { + vec![ + Arc::new(Float64Array::from(y.to_vec())), + Arc::new(Float64Array::from(x.to_vec())), + ] + } + } + + fn same_value(actual: Option, expected: Option) { + match (actual, expected) { + (Some(a), Some(b)) if b.is_nan() => assert!(a.is_nan()), + (Some(a), Some(b)) => assert_eq!(a.to_bits(), b.to_bits(), "{a:?} != {b:?}"), + _ => assert_eq!(actual, expected), + } + } + + fn scalar_value(acc: &mut dyn Accumulator) -> Option { + let ScalarValue::Float64(value) = acc.evaluate().unwrap() else { + panic!("expected f64") + }; + value + } + + fn update_scalars( + scalars: &mut [Box], + values: &[ArrayRef], + groups: &[usize], + predicate: Option<&BooleanArray>, + ) { + for (group, scalar) in scalars.iter_mut().enumerate() { + let mask = BooleanArray::from_iter(groups.iter().enumerate().map(|(i, &g)| { + Some(g == group && predicate.is_none_or(|p| p.is_valid(i) && p.value(i))) + })); + let selected = values + .iter() + .map(|a| filter(a, &mask).unwrap()) + .collect::>(); + scalar.update_batch(&selected).unwrap(); + } + } + + fn same_state(state: &[ArrayRef], group: usize, scalar: &mut dyn Accumulator) { + let expected = scalar.state().unwrap(); + assert_eq!(state.len(), expected.len()); + for (actual, expected) in state.iter().zip(expected) { + let ScalarValue::Float64(expected) = expected else { + panic!("expected f64 state") + }; + let actual = actual.as_primitive::(); + assert!(actual.is_valid(group)); + same_value(Some(actual.value(group)), expected); + } + } + + fn scalar_state(scalar: &mut dyn Accumulator) -> Vec { + scalar + .state() + .unwrap() + .iter() + .map(|v| v.to_array_of_size(1).unwrap()) + .collect() + } + + #[test] + fn grouped_updates_preserve_scalar_state_and_results() { + for kind in KINDS { + for modern in [false, true] { + for nullable in [false, true] { + for filtered in [false, true] { + let expr = expression(kind, modern); + let mut scalars = (0..6) + .map(|_| expr.create_accumulator().unwrap()) + .collect::>(); + let mut grouped = expr.create_groups_accumulator().unwrap(); + let mut for_state = expr.create_groups_accumulator().unwrap(); + let y: Vec<_> = (0..35) + .map(|i| { + (i != 0 && i != 34 && (!nullable || i % 7 != 0)) + .then_some(i as f64 * 1.5 + 1.0) + }) + .collect(); + let x: Vec<_> = (0..35) + .map(|i| { + (i != 0 && i != 34 && (!nullable || i % 11 != 0)) + .then_some(i as f64 * 0.75 - 3.0) + }) + .collect(); + let values = inputs(kind, &y, &x); + // Sliced Arrow inputs and predicates retain nonzero offsets. + // The dense case has NULLs only outside the sliced range. + for start in [1, 12, 23] { + let chunk: Vec<_> = values.iter().map(|a| a.slice(start, 11)).collect(); + let indices: Vec<_> = (start..start + 11).map(|i| i % 5).collect(); + let predicate = BooleanArray::from_iter((0..35).map(|i| { + if i % 13 == 0 { + None + } else { + Some(i % 3 != 0) + } + })) + .slice(start, 11); + let filter = filtered.then_some(&predicate); + // Increase the allocated group count without adding a value + // for group 5. FILTER can also leave existing groups empty. + let total = if start == 1 { 5 } else { 6 }; + grouped + .update_batch(&chunk, &indices, filter, total) + .unwrap(); + for_state + .update_batch(&chunk, &indices, filter, total) + .unwrap(); + update_scalars(&mut scalars, &chunk, &indices, filter); + } + let states = for_state.state(EmitTo::All).unwrap(); + let result = grouped.evaluate(EmitTo::All).unwrap(); + let result = result.as_primitive::(); + assert_eq!(result.len(), scalars.len()); + for (i, scalar) in scalars.iter_mut().enumerate() { + same_state(&states, i, scalar.as_mut()); + same_value( + result.is_valid(i).then(|| result.value(i)), + scalar_value(scalar.as_mut()), + ); + } + assert_eq!(grouped.size(), 0); + assert_eq!(for_state.size(), 0); + + // Final aggregation receives several state rows at once, + // with repeated and non-monotonic destination groups. + let destinations = [2, 0, 2, 1, 0, 1]; + let mut merged = expr.create_groups_accumulator().unwrap(); + let mut expected = (0..3) + .map(|_| expr.create_accumulator().unwrap()) + .collect::>(); + for (start, len) in [(0, 6), (1, 4)] { + let batch: Vec<_> = + states.iter().map(|a| a.slice(start, len)).collect(); + let indices = &destinations[start..start + len]; + merged.merge_batch(&batch, indices, 3).unwrap(); + for_state.merge_batch(&batch, indices, 3).unwrap(); + for (row, &group) in indices.iter().enumerate() { + let state: Vec<_> = batch.iter().map(|a| a.slice(row, 1)).collect(); + expected[group].merge_batch(&state).unwrap(); + } + } + let state = for_state.state(EmitTo::All).unwrap(); + let result = merged.evaluate(EmitTo::All).unwrap(); + let result = result.as_primitive::(); + assert_eq!(result.len(), expected.len()); + for (i, scalar) in expected.iter_mut().enumerate() { + same_state(&state, i, scalar.as_mut()); + same_value( + result.is_valid(i).then(|| result.value(i)), + scalar_value(scalar.as_mut()), + ); + } + } + } + } + } + } + + #[test] + fn grouped_state_round_trips_in_both_directions() { + for kind in KINDS { + for modern in [false, true] { + // Include large adjacent values, signed zero, infinities/NaNs, + // fractional constants and raw moment underflow/overflow. + for (y, x) in [ + ( + vec![Some(1e16), Some(1e16 + 2.0), None, Some(-0.0)], + vec![Some(3.0), Some(9.0), Some(20.0), Some(0.0)], + ), + ( + vec![ + Some(1e200), + Some(-1e200), + Some(f64::NAN), + Some(f64::INFINITY), + ], + vec![Some(0.1); 4], + ), + ( + vec![Some(1e-100), Some(2e-100), None, Some(0.0)], + vec![Some(2e-100), Some(-1e-100), Some(1e100), None], + ), + ] { + let expr = expression(kind, modern); + let mut grouped = expr.create_groups_accumulator().unwrap(); + let mut expected = expr.create_accumulator().unwrap(); + let mut from_grouped = expr.create_accumulator().unwrap(); + let values = inputs(kind, &y, &x); + // Empty partials before and after nonempty states must execute + // the merge arithmetic, including its NaN propagation. + for (start, len) in [(0, 0), (0, 2), (2, 2), (4, 0)] { + let chunk = values + .iter() + .map(|a| a.slice(start, len)) + .collect::>(); + let mut partial = expr.create_accumulator().unwrap(); + partial.update_batch(&chunk).unwrap(); + let mut grouped_partial = expr.create_groups_accumulator().unwrap(); + grouped_partial + .update_batch(&chunk, &vec![0; len], None, 1) + .unwrap(); + let gs = grouped_partial.state(EmitTo::All).unwrap(); + same_state(&gs, 0, partial.as_mut()); + let ss = scalar_state(partial.as_mut()); + expected.merge_batch(&ss).unwrap(); + grouped.merge_batch(&ss, &[0], 1).unwrap(); + from_grouped.merge_batch(&gs).unwrap(); + } + let states = grouped.state(EmitTo::All).unwrap(); + same_state(&states, 0, expected.as_mut()); + same_value( + scalar_value(from_grouped.as_mut()), + scalar_value(expected.as_mut()), + ); + // Evaluate after one identical additional merge on both sides. + grouped.merge_batch(&states, &[0], 1).unwrap(); + let mut merged = expr.create_accumulator().unwrap(); + merged.merge_batch(&states).unwrap(); + let actual = grouped.evaluate(EmitTo::All).unwrap(); + same_value( + actual.as_primitive::().iter().next().unwrap(), + scalar_value(merged.as_mut()), + ); + } + } + } + } + + #[test] + fn grouped_emission_preserves_remaining_groups() { + for kind in KINDS { + for modern in [false, true] { + let expr = expression(kind, modern); + let mut grouped = expr.create_groups_accumulator().unwrap(); + let mut scalars = (0..4) + .map(|_| expr.create_accumulator().unwrap()) + .collect::>(); + let values = inputs( + kind, + &[ + Some(1.0), + Some(4.0), + None, + Some(9.0), + Some(3.0), + Some(8.0), + Some(5.0), + Some(14.0), + ], + &[ + Some(2.0), + Some(1.0), + Some(3.0), + None, + Some(6.0), + Some(4.0), + Some(7.0), + Some(5.0), + ], + ); + let groups = [0, 1, 2, 3, 0, 1, 2, 3]; + grouped.update_batch(&values, &groups, None, 4).unwrap(); + update_scalars(&mut scalars, &values, &groups, None); + let vectors = match kind { + RegrType::SXX | RegrType::SYY => 3, + RegrType::SXY => 4, + RegrType::Slope | RegrType::Intercept => 7, + RegrType::R2 => 10, + }; + let allocated = grouped.size(); + assert!(allocated >= 4 * vectors * size_of::()); + assert!(grouped + .state(EmitTo::First(0)) + .unwrap() + .iter() + .all(|a| a.is_empty())); + assert_eq!(grouped.evaluate(EmitTo::First(0)).unwrap().len(), 0); + let first = grouped.evaluate(EmitTo::First(1)).unwrap(); + same_value( + first.as_primitive::().iter().next().unwrap(), + scalar_value(scalars.remove(0).as_mut()), + ); + // EmitTo keeps the backing allocation when draining a small + // prefix. Accounting must reflect capacity, not the new length. + assert_eq!(grouped.size(), allocated); + let state = grouped.state(EmitTo::First(1)).unwrap(); + same_state(&state, 0, scalars.remove(0).as_mut()); + assert!(grouped.size() >= 2 * vectors * size_of::()); + scalars.push(expr.create_accumulator().unwrap()); + let more = inputs( + kind, + &[Some(8.0), None, Some(17.0), Some(12.0)], + &[Some(6.0), Some(7.0), Some(9.0), Some(3.0)], + ); + grouped.update_batch(&more, &[0, 2, 1, 2], None, 3).unwrap(); + update_scalars(&mut scalars, &more, &[0, 2, 1, 2], None); + let mut partial = expr.create_accumulator().unwrap(); + partial.update_batch(&more).unwrap(); + let partial_state = scalar_state(partial.as_mut()); + grouped.merge_batch(&partial_state, &[1], 3).unwrap(); + scalars[1].merge_batch(&partial_state).unwrap(); + // A majority prefix exercises EmitTo's other allocation path. + let first = grouped.evaluate(EmitTo::First(2)).unwrap(); + let last = grouped.evaluate(EmitTo::All).unwrap(); + assert_eq!(first.len(), 2); + assert_eq!(last.len(), 1); + for (actual, scalar) in first + .as_primitive::() + .iter() + .chain(last.as_primitive::().iter()) + .zip(&mut scalars) + { + same_value(actual, scalar_value(scalar.as_mut())); + } + assert_eq!(grouped.size(), 0); + // All resets the accumulator so group zero can be used again. + grouped.update_batch(&more, &[0; 4], None, 1).unwrap(); + same_state(&grouped.state(EmitTo::All).unwrap(), 0, partial.as_mut()); + } + } + } + + #[test] + fn r2_factory_selects_regression_result_rules() { + for modern in [false, true] { + let expr = expression(RegrType::R2, modern); + for (y, x, expected) in [ + (vec![Some(1.0)], vec![Some(2.0)], None), + ( + vec![Some(f64::INFINITY)], + vec![Some(f64::INFINITY)], + Some(f64::NAN), + ), + ( + vec![Some(7.0); 2], + vec![Some(1.0), Some(3.0)], + modern.then_some(1.0), + ), + ( + vec![Some(1.0), Some(3.0)], + vec![Some(7.0); 2], + (!modern).then_some(1.0), + ), + ] { + let mut grouped = expr.create_groups_accumulator().unwrap(); + grouped + .update_batch(&inputs(RegrType::R2, &y, &x), &vec![0; y.len()], None, 1) + .unwrap(); + same_value( + grouped + .evaluate(EmitTo::All) + .unwrap() + .as_primitive::() + .iter() + .next() + .unwrap(), + expected, + ); + } + // Synthetic partial state checks that R2 does not inherit Corr's + // count==0 shortcut. Empty-partial merge arithmetic produces NaN M2. + let state: Vec = [0.0, f64::NAN, f64::NAN, 0.0, 0.0, 0.0] + .into_iter() + .map(|v| Arc::new(Float64Array::from(vec![v])) as ArrayRef) + .collect(); + let mut grouped = expr.create_groups_accumulator().unwrap(); + grouped.merge_batch(&state, &[0], 1).unwrap(); + same_value( + grouped + .evaluate(EmitTo::All) + .unwrap() + .as_primitive::() + .iter() + .next() + .unwrap(), + Some(f64::NAN), + ); + } + let corr = physical(AggregateUDF::new_from_impl(Correlation::new( + "corr", + DataType::Float64, + false, + EvalMode::Legacy, + None, + create_query_context_map(), + ))); + let mut corr = corr.create_groups_accumulator().unwrap(); + corr.update_batch( + &inputs(RegrType::R2, &[Some(1.0)], &[Some(2.0)]), + &[0], + None, + 1, + ) + .unwrap(); + same_value( + corr.evaluate(EmitTo::All) + .unwrap() + .as_primitive::() + .iter() + .next() + .unwrap(), + Some(f64::NAN), + ); + } + + #[test] + fn grouped_r2_underflow_preserves_captured_error_context() { + let sql = "SELECT regr_r2(y, x) FROM t GROUP BY g"; + for mode in [EvalMode::Legacy, EvalMode::Ansi] { + for modern in [false, true] { + for merge in [false, true] { + let registry = create_query_context_map(); + registry.register( + 7, + QueryContext::new(sql.to_string(), 7, 19, None, None, 1, 7), + ); + let expr = physical(AggregateUDF::new_from_impl(Regr::new( + RegrType::R2, + "regr_r2", + true, + modern, + mode, + Some(7), + registry, + ))); + let mut grouped = expr.create_groups_accumulator().unwrap(); + let values = inputs( + RegrType::R2, + &[Some(1e-100), None, Some(2e-100)], + &[Some(-1e-100), Some(1e200), Some(-2e-100)], + ); + if merge { + for (start, len) in [(0, 2), (2, 1)] { + let mut partial = expr.create_accumulator().unwrap(); + partial + .update_batch( + &values + .iter() + .map(|a| a.slice(start, len)) + .collect::>(), + ) + .unwrap(); + grouped + .merge_batch(&scalar_state(partial.as_mut()), &[0], 1) + .unwrap(); + } + } else { + grouped.update_batch(&values, &[0; 3], None, 1).unwrap(); + } + if mode == EvalMode::Ansi { + let DataFusionError::External(error) = + grouped.evaluate(EmitTo::All).unwrap_err() + else { + panic!("expected structured Spark error") + }; + let error = error.downcast_ref::().unwrap(); + assert!(matches!(error.error, SparkError::DivideByZero)); + assert_eq!(error.context.as_ref().unwrap().sql_text.as_str(), sql); + } else { + assert_eq!(grouped.evaluate(EmitTo::All).unwrap().null_count(), 1); + } + } + } + } + } +} diff --git a/native/spark-expr/src/agg_funcs/welford.rs b/native/spark-expr/src/agg_funcs/welford.rs index 44c096f5223..ff4645dab76 100644 --- a/native/spark-expr/src/agg_funcs/welford.rs +++ b/native/spark-expr/src/agg_funcs/welford.rs @@ -15,13 +15,13 @@ // specific language governing permissions and limitations // under the License. -//! Welford-style online update/merge math shared by the per-row +//! Statistical update, merge and finalization shared by the per-row //! `Accumulator` and the vectorized `GroupsAccumulator` implementations of -//! variance/stddev/covariance/correlation. Counts are `f64` to match +//! variance/stddev/covariance/correlation/regression. Counts are `f64` to match //! Spark's wire-format state. use arrow::buffer::NullBuffer; -use datafusion::common::DataFusionError; +use datafusion::common::{DataFusionError, Result}; use datafusion::physical_expr::expressions::StatsType; use std::hash::{Hash, Hasher}; use std::sync::Arc; @@ -251,3 +251,38 @@ pub(crate) fn covariance_merge( let new_c = c_a + c_b + delta1 * delta2_n * count_a * count_b; (new_count, new_mean1, new_mean2, new_c) } + +/// Spark RegrR2's result rule, shared by scalar and grouped accumulation. +/// SPARK-55969 swapped the constant-x/constant-y cases. Their guards precede +/// division, including in ANSI mode; Corr's count guards do not apply here. +pub(super) fn r2_value( + ck: f64, + m2_y: f64, + m2_x: f64, + constant_dependent_is_perfect_fit: bool, + zero_divisor: &ZeroDivisor, +) -> Result> { + let (null_case, perfect_fit_case) = if constant_dependent_is_perfect_fit { + (m2_x == 0.0, m2_y == 0.0) + } else { + (m2_y == 0.0, m2_x == 0.0) + }; + if null_case { + Ok(None) + } else if perfect_fit_case { + Ok(Some(1.0)) + } else { + // Nonzero moments can still have a product that underflows to zero. + let product = m2_y * m2_x; + if product == 0.0 { + return match zero_divisor.error() { + Some(error) => Err(error), + None => Ok(None), + }; + } + // Preserve Spark's rounding and overflow behavior: dividing first and + // then squaring differs from (ck * ck) / (m2_y * m2_x). + let corr = ck / product.sqrt(); + Ok(Some(corr * corr)) + } +} diff --git a/spark/src/test/resources/sql-tests/expressions/aggregate/regr.sql b/spark/src/test/resources/sql-tests/expressions/aggregate/regr.sql index e17268eccbd..191c0a8f3a1 100644 --- a/spark/src/test/resources/sql-tests/expressions/aggregate/regr.sql +++ b/spark/src/test/resources/sql-tests/expressions/aggregate/regr.sql @@ -174,6 +174,34 @@ SELECT regr_slope(y, x), regr_intercept(y, x), regr_r2(y, x) FROM test_regr_cons query SELECT regr_sxx(y, x), regr_syy(y, x), regr_sxy(y, x) FROM test_regr_const_y +-- Nullable aggregate FILTERs must preserve group positions and combine with +-- each function's pair-null rules. Group 1 exists but has no qualifying rows; +-- group 2 exercises the historical slope/intercept variance rule for null y. +statement +CREATE TABLE test_regr_filtered(g int, y double, x double, keep boolean) USING parquet + +statement +INSERT INTO test_regr_filtered VALUES + (0, 3.0, 1.0, true), (0, 9.0, 4.0, true), (0, 8.0, NULL, true), + (0, NULL, 20.0, true), (0, 90.0, 8.0, false), (0, 70.0, 9.0, NULL), + (1, 2.0, 5.0, false), (1, 8.0, 9.0, NULL), + (2, NULL, 1.0, true), (2, NULL, 4.0, true), + (3, 7.0, 1.0, true), (3, 7.0, 4.0, true) + +query +SELECT g, + regr_sxx(y, x) FILTER (WHERE keep), regr_syy(y, x) FILTER (WHERE keep), + regr_sxy(y, x) FILTER (WHERE keep), regr_slope(y, x) FILTER (WHERE keep), + regr_intercept(y, x) FILTER (WHERE keep), regr_r2(y, x) FILTER (WHERE keep) +FROM test_regr_filtered GROUP BY g ORDER BY g + +query +SELECT g, + regr_sxx(x, y) FILTER (WHERE keep), regr_syy(x, y) FILTER (WHERE keep), + regr_sxy(x, y) FILTER (WHERE keep), regr_slope(x, y) FILTER (WHERE keep), + regr_intercept(x, y) FILTER (WHERE keep), regr_r2(x, y) FILTER (WHERE keep) +FROM test_regr_filtered GROUP BY g ORDER BY g + -- Both moments are nonzero, but their product underflows to zero. statement CREATE TABLE test_regr_underflow(v double, grp int) USING parquet @@ -202,3 +230,9 @@ SELECT regr_r2(y, x) FROM test_regr_const_x query SELECT regr_r2(y, x) FROM test_regr_const_y + +query +SELECT x, regr_r2(y, x) FROM test_regr_const_x GROUP BY x + +query +SELECT y, regr_r2(y, x) FROM test_regr_const_y GROUP BY y diff --git a/spark/src/test/scala/org/apache/spark/sql/benchmark/CometAggregateExpressionBenchmark.scala b/spark/src/test/scala/org/apache/spark/sql/benchmark/CometAggregateExpressionBenchmark.scala index 950ebaab7aa..d9ff9a66e2c 100644 --- a/spark/src/test/scala/org/apache/spark/sql/benchmark/CometAggregateExpressionBenchmark.scala +++ b/spark/src/test/scala/org/apache/spark/sql/benchmark/CometAggregateExpressionBenchmark.scala @@ -74,7 +74,25 @@ object CometAggregateExpressionBenchmark extends CometBenchmarkBase { AggExprConfig( "covar_pop", "SELECT COVAR_POP(c_double, c_double2) FROM parquetV1Table GROUP BY grp"), - AggExprConfig("corr", "SELECT CORR(c_double, c_double2) FROM parquetV1Table GROUP BY grp")) + AggExprConfig("corr", "SELECT CORR(c_double, c_double2) FROM parquetV1Table GROUP BY grp"), + AggExprConfig( + "regr_sxx", + "SELECT regr_sxx(c_double, c_double2) FROM parquetV1Table GROUP BY grp"), + AggExprConfig( + "regr_syy", + "SELECT regr_syy(c_double, c_double2) FROM parquetV1Table GROUP BY grp"), + AggExprConfig( + "regr_sxy", + "SELECT regr_sxy(c_double, c_double2) FROM parquetV1Table GROUP BY grp"), + AggExprConfig( + "regr_slope", + "SELECT regr_slope(c_double, c_double2) FROM parquetV1Table GROUP BY grp"), + AggExprConfig( + "regr_intercept", + "SELECT regr_intercept(c_double, c_double2) FROM parquetV1Table GROUP BY grp"), + AggExprConfig( + "regr_r2", + "SELECT regr_r2(c_double, c_double2) FROM parquetV1Table GROUP BY grp")) private val bitwiseAggregates = List( AggExprConfig("bit_and", "SELECT BIT_AND(c_long) FROM parquetV1Table GROUP BY grp"), @@ -225,9 +243,20 @@ object CometAggregateExpressionBenchmark extends CometBenchmarkBase { highCardinalityAggregates ++ percentileAggregates ++ approxPercentileAggregates ++ approxCountDistinctAggregates ++ maxMinByAggregates - allAggregates.foreach { config => - runExpressionBenchmark(config.name, v.toLong, config.query, config.extraCometConfigs) - } + require( + mainArgs.forall(name => allAggregates.exists(_.name == name)), + s"Unknown aggregate name in: ${mainArgs.mkString(", ")}") + // Optional names allow focused comparisons. With SPARK_GENERATE_BENCHMARK_FILES=1, + // a filtered run replaces the results file with only the selected aggregates. + allAggregates + .filter(config => mainArgs.isEmpty || mainArgs.contains(config.name)) + .foreach { config => + runExpressionBenchmark( + config.name, + v.toLong, + config.query, + config.extraCometConfigs) + } } } } From 4e4b58ad2c4fd5681fc3b364a408020237cff28d Mon Sep 17 00:00:00 2001 From: rich7420 Date: Sat, 24 Oct 2026 13:56:02 +0800 Subject: [PATCH 2/4] bench: cover clustered regression aggregate inputs Compare interleaved keys with long same-key runs and exercise all six regression functions in the single-group control. Preserve the existing benchmark names and cardinality/partial-skip checks. --- native/spark-expr/benches/aggregate_stats.rs | 39 ++++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/native/spark-expr/benches/aggregate_stats.rs b/native/spark-expr/benches/aggregate_stats.rs index d60e4461e63..3281f9d0f76 100644 --- a/native/spark-expr/benches/aggregate_stats.rs +++ b/native/spark-expr/benches/aggregate_stats.rs @@ -173,13 +173,24 @@ fn criterion_benchmark(c: &mut Criterion) { group.finish(); } +const REGRESSION_BATCHES: usize = 32; +const REGRESSION_BATCH_SIZE: usize = 8192; +const REGRESSION_ROWS: usize = REGRESSION_BATCHES * REGRESSION_BATCH_SIZE; + // Measure Partial aggregation (update + state emission), including the DataFusion // grouping path. Full-query Spark benchmarks also cover finalization and shuffle. fn regression_benchmark(c: &mut Criterion) { let rt = Runtime::new().unwrap(); let mut group = c.benchmark_group("regr_partial"); - for (groups, null_every) in [(1024, 0), (1024, 101), (1024, 2), (65536, 0), (1, 0)] { - let batches = regression_batches(groups, null_every); + for (groups, null_every, clustered) in [ + (1024, 0, false), + (1024, 101, false), + (1024, 2, false), + (65536, 0, false), + (1, 0, false), + (1024, 0, true), + ] { + let batches = regression_batches(groups, null_every, clustered); let partitions = &[batches]; for (name, kind) in [ ("regr_sxx", RegrType::SXX), @@ -189,9 +200,9 @@ fn regression_benchmark(c: &mut Criterion) { ("regr_intercept", RegrType::Intercept), ("regr_r2", RegrType::R2), ] { - // Cover every function with and without nulls, and the two compound - // state families for the high-cardinality and single-group controls. - if groups != 1024 && !matches!(kind, RegrType::Slope | RegrType::R2) { + // Cover every function with and without nulls, clustered keys, and + // one group. Limit the high-cardinality control to compound states. + if groups == 65536 && !matches!(kind, RegrType::Slope | RegrType::R2) { continue; } let udf = Arc::new(AggregateUDF::new_from_impl(Regr::new( @@ -236,7 +247,8 @@ fn regression_benchmark(c: &mut Criterion) { 2 => "half_null", _ => "sparse_null", }; - group.bench_function(format!("{name}/{groups}_groups/{nulls}"), |b| { + let layout = if clustered { "/clustered" } else { "" }; + group.bench_function(format!("{name}/{groups}_groups/{nulls}{layout}"), |b| { b.to_async(&rt).iter(|| { agg_test( partitions, @@ -252,7 +264,7 @@ fn regression_benchmark(c: &mut Criterion) { group.finish(); } -fn regression_batches(groups: usize, null_every: usize) -> Vec { +fn regression_batches(groups: usize, null_every: usize, clustered: bool) -> Vec { let schema = Arc::new(Schema::new(vec![ Field::new("c0", DataType::Utf8, false), Field::new("c1", DataType::Float64, true), @@ -260,14 +272,14 @@ fn regression_batches(groups: usize, null_every: usize) -> Vec { Field::new("c3", DataType::Float64, true), Field::new("c4", DataType::Float64, true), ])); - (0..32) + (0..REGRESSION_BATCHES) .map(|batch| { let mut keys = StringBuilder::new(); let mut y = Float64Builder::new(); let mut x = Float64Builder::new(); let mut paired_x = Float64Builder::new(); - for row in 0..8192 { - let i = batch * 8192 + row; + for row in 0..REGRESSION_BATCH_SIZE { + let i = batch * REGRESSION_BATCH_SIZE + row; let xv = i as f64 * 0.25 + 1.0; let yv = xv * 1.5 + (i % 17) as f64; let valid = match null_every { @@ -277,7 +289,12 @@ fn regression_batches(groups: usize, null_every: usize) -> Vec { // contains sparse nulls and every group still has valid rows. _ => !i.is_multiple_of(null_every), }; - keys.append_value(format!("group_{}", i % groups)); + let group_index = if clustered { + i / (REGRESSION_ROWS / groups) + } else { + i % groups + }; + keys.append_value(format!("group_{group_index}")); y.append_option(valid.then_some(yv)); x.append_value(xv); paired_x.append_option(valid.then_some(xv)); From 426cdd92965f44281198c773d815086db2ba5451 Mon Sep 17 00:00:00 2001 From: rich7420 Date: Wed, 18 Nov 2026 15:46:59 +0800 Subject: [PATCH 3/4] perf: update grouped regr aggregates in one pass per row Composing the grouped variance, covariance and correlation accumulators made grouped regr_* slower than the per-group accumulators when rows of one group arrive together: 9-23% for regr_sxy, regr_slope, regr_intercept and regr_r2 with a single group, and 11% for regr_r2 with 256-row runs. Replace the composition with one accumulator that keeps a state row per group in Spark's partial-buffer layout and folds every moment of a statistic in a single pass. It carries the state in a local while consecutive rows share a group. The arithmetic and its order are those of the per-row accumulators, so results stay bit-identical; the existing grouped tests compare the two. This leaves the shared variance, covariance and correlation kernels as they are on main. Also drop the test assertion that depended on how DataFusion splits a Vec on EmitTo::First, and the benchmark changes that the regr cases do not need. --- native/spark-expr/benches/aggregate_stats.rs | 36 +- .../spark-expr/src/agg_funcs/correlation.rs | 136 +++--- native/spark-expr/src/agg_funcs/regr.rs | 413 ++++++++++-------- native/spark-expr/src/agg_funcs/welford.rs | 41 +- .../CometAggregateExpressionBenchmark.scala | 17 +- 5 files changed, 286 insertions(+), 357 deletions(-) diff --git a/native/spark-expr/benches/aggregate_stats.rs b/native/spark-expr/benches/aggregate_stats.rs index 9dbc8c775ea..1e46f5c962e 100644 --- a/native/spark-expr/benches/aggregate_stats.rs +++ b/native/spark-expr/benches/aggregate_stats.rs @@ -30,7 +30,6 @@ use arrow::datatypes::{DataType, Field, Schema}; use criterion::{criterion_group, criterion_main, Criterion}; use datafusion::datasource::memory::MemorySourceConfig; use datafusion::datasource::source::DataSourceExec; -use datafusion::execution::context::SessionConfig; use datafusion::execution::TaskContext; use datafusion::logical_expr::AggregateUDF; use datafusion::physical_expr::aggregate::AggregateExprBuilder; @@ -232,23 +231,6 @@ fn regression_benchmark(c: &mut Criterion) { let inputs: Vec> = indices .map(|i| Arc::new(Column::new(&format!("c{i}"), i)) as Arc) .to_vec(); - // Check actual cardinality outside timing. Batches use successive row - // offsets, so the high-cardinality case really contains 64k groups. - let (rows, aggregate) = rt.block_on(agg_test( - partitions, - group_col.clone(), - inputs.clone(), - udf.clone(), - name, - )); - assert_eq!(rows, groups); - let skipped = aggregate - .metrics() - .unwrap() - .sum_by_name("skipped_aggregation_rows") - .map(|metric| metric.as_usize()) - .unwrap_or(0); - assert_eq!(skipped, 0); let nulls = match null_every { 0 => "no_null", 2 => "half_null", @@ -357,26 +339,18 @@ async fn agg_test( input_exprs: Vec>, aggregate_udf: Arc, alias: &str, -) -> (usize, Arc) { +) { let schema = &partitions[0][0].schema(); let scan: Arc = Arc::new(DataSourceExec::new(Arc::new( MemorySourceConfig::try_new(partitions, Arc::clone(schema), None).unwrap(), ))); let aggregate = create_aggregate(scan, group_col, input_exprs, schema, aggregate_udf, alias); - // Match Comet's eligibility gate: regression aggregates cannot skip partial - // aggregation (convert_to_state is unsupported by the grouped kernels). - let mut config = SessionConfig::new(); - config - .options_mut() - .execution - .skip_partial_aggregation_probe_ratio_threshold = 1.1; - let context = TaskContext::default().with_session_config(config); - let mut stream = aggregate.execute(0, Arc::new(context)).unwrap(); - let mut rows = 0; + let mut stream = aggregate + .execute(0, Arc::new(TaskContext::default())) + .unwrap(); while let Some(batch) = stream.next().await { - rows += black_box(batch.unwrap()).num_rows(); + let _batch = batch.unwrap(); } - (rows, aggregate) } fn create_aggregate( diff --git a/native/spark-expr/src/agg_funcs/correlation.rs b/native/spark-expr/src/agg_funcs/correlation.rs index 49ee8aa4c1f..56108bfa6b0 100644 --- a/native/spark-expr/src/agg_funcs/correlation.rs +++ b/native/spark-expr/src/agg_funcs/correlation.rs @@ -23,7 +23,7 @@ use std::sync::Arc; use crate::agg_funcs::covariance::{CovarianceAccumulator, CovarianceGroupsAccumulator}; use crate::agg_funcs::stddev::StddevAccumulator; use crate::agg_funcs::variance::VarianceGroupsAccumulator; -use crate::agg_funcs::welford::{r2_value, ZeroDivisor}; +use crate::agg_funcs::welford::ZeroDivisor; use crate::{EvalMode, QueryContextMap}; use arrow::compute::filter; use datafusion::common::{not_impl_err, Result, ScalarValue}; @@ -136,7 +136,7 @@ impl AggregateUDFImpl for Correlation { &self, _args: AccumulatorArgs, ) -> Result> { - Ok(Box::new(CorrelationGroupsAccumulator::new_corr( + Ok(Box::new(CorrelationGroupsAccumulator::new( self.null_on_divide_by_zero, self.zero_divisor.clone(), ))) @@ -278,63 +278,33 @@ impl Accumulator for CorrelationAccumulator { } } -#[derive(Debug, Clone, Copy)] -enum PearsonResult { - Corr { - null_on_divide_by_zero: bool, - }, - RegrR2 { - constant_dependent_is_perfect_fit: bool, - }, -} - -/// Grouped correlation/R2 accumulator. Mirrors the per-row `CorrelationAccumulator` +/// Grouped correlation accumulator. Mirrors the per-row `CorrelationAccumulator` /// composition: one population covariance + two population variance sub- /// accumulators for the per-column m2 values. Combined filter handles /// "skip rows where either input is null" without filtering `group_indices`. #[derive(Debug)] -pub(super) struct CorrelationGroupsAccumulator { +struct CorrelationGroupsAccumulator { covar: CovarianceGroupsAccumulator, var1: VarianceGroupsAccumulator, var2: VarianceGroupsAccumulator, - result: PearsonResult, + null_on_divide_by_zero: bool, zero_divisor: ZeroDivisor, } impl CorrelationGroupsAccumulator { - pub(super) fn new_corr(null_on_divide_by_zero: bool, zero_divisor: ZeroDivisor) -> Self { - Self::new( - PearsonResult::Corr { - null_on_divide_by_zero, - }, - zero_divisor, - ) - } - - pub(super) fn new_regr_r2( - constant_dependent_is_perfect_fit: bool, - zero_divisor: ZeroDivisor, - ) -> Self { - Self::new( - PearsonResult::RegrR2 { - constant_dependent_is_perfect_fit, - }, - zero_divisor, - ) - } - - fn new(result: PearsonResult, zero_divisor: ZeroDivisor) -> Self { + fn new(null_on_divide_by_zero: bool, zero_divisor: ZeroDivisor) -> Self { // Children run with StatsType::Population, which never hits the // count == 1 sample-divide-by-zero branch, so the children's - // null_on_divide_by_zero is dead code. The top-level result mode - // applies the appropriate degenerate-case rules. + // null_on_divide_by_zero is dead code. The top-level evaluate() + // applies the count <= 1 rule. Pass `false` to children to keep + // that intent explicit. Self { covar: CovarianceGroupsAccumulator::new(StatsType::Population, false), var1: VarianceGroupsAccumulator::new(StatsType::Population, false) .with_pearson_update(), var2: VarianceGroupsAccumulator::new(StatsType::Population, false) .with_pearson_update(), - result, + null_on_divide_by_zero, zero_divisor, } } @@ -440,39 +410,35 @@ impl GroupsAccumulator for CorrelationGroupsAccumulator { let mut values = Vec::with_capacity(n); let mut validity = Vec::with_capacity(n); for i in 0..n { - let value = match self.result { - PearsonResult::Corr { - null_on_divide_by_zero, - } => { - if counts[i] == 0.0 { - None - } else if counts[i] == 1.0 { - (!null_on_divide_by_zero).then_some(f64::NAN) - } else { - // Preserve Spark's raw-moment overflow/underflow behavior. - let product = m2_1s[i] * m2_2s[i]; - if product == 0.0 { - if let Some(error) = self.zero_divisor.error() { - return Err(error); - } - None - } else { - Some(algo_consts[i] / product.sqrt()) - } - } + let count = counts[i]; + if count == 0.0 { + values.push(0.0); + validity.push(false); + continue; + } + if count == 1.0 { + if self.null_on_divide_by_zero { + values.push(0.0); + validity.push(false); + } else { + values.push(f64::NAN); + validity.push(true); } - PearsonResult::RegrR2 { - constant_dependent_is_perfect_fit, - } => r2_value( - algo_consts[i], - m2_1s[i], - m2_2s[i], - constant_dependent_is_perfect_fit, - &self.zero_divisor, - )?, - }; - values.push(value.unwrap_or_default()); - validity.push(value.is_some()); + continue; + } + // Match Spark's raw-moment product, including overflow, underflow + // and NaN from a zero moment paired with infinity. + let m2_product = m2_1s[i] * m2_2s[i]; + if m2_product == 0.0 { + if let Some(error) = self.zero_divisor.error() { + return Err(error); + } + values.push(0.0); + validity.push(false); + continue; + } + values.push(algo_consts[i] / m2_product.sqrt()); + validity.push(true); } Ok(Arc::new(Float64Array::new( @@ -528,7 +494,7 @@ mod groups_tests { fn acc(legacy: bool) -> CorrelationGroupsAccumulator { // null_on_divide_by_zero = !legacy - CorrelationGroupsAccumulator::new_corr(!legacy, zero_divisor(EvalMode::Legacy)) + CorrelationGroupsAccumulator::new(!legacy, zero_divisor(EvalMode::Legacy)) } fn evaluate(a: &mut CorrelationGroupsAccumulator) -> Vec> { @@ -569,7 +535,7 @@ mod groups_tests { zero_divisor(EvalMode::Legacy), ) .unwrap(); - let mut grouped = CorrelationGroupsAccumulator::new_corr( + let mut grouped = CorrelationGroupsAccumulator::new( null_on_divide_by_zero, zero_divisor(EvalMode::Legacy), ); @@ -586,7 +552,7 @@ mod groups_tests { zero_divisor(EvalMode::Legacy), ) .unwrap(); - let mut merged_grouped = CorrelationGroupsAccumulator::new_corr( + let mut merged_grouped = CorrelationGroupsAccumulator::new( null_on_divide_by_zero, zero_divisor(EvalMode::Legacy), ); @@ -614,8 +580,7 @@ mod groups_tests { ]; let mut scalar = CorrelationAccumulator::try_new(true, zero_divisor(EvalMode::Legacy)).unwrap(); - let mut grouped = - CorrelationGroupsAccumulator::new_corr(true, zero_divisor(EvalMode::Legacy)); + let mut grouped = CorrelationGroupsAccumulator::new(true, zero_divisor(EvalMode::Legacy)); scalar.update_batch(&input).unwrap(); grouped.update_batch(&input, &[0, 0], None, 1).unwrap(); let state = scalar @@ -627,7 +592,7 @@ mod groups_tests { let mut merged_scalar = CorrelationAccumulator::try_new(true, zero_divisor(EvalMode::Legacy)).unwrap(); let mut merged_grouped = - CorrelationGroupsAccumulator::new_corr(true, zero_divisor(EvalMode::Legacy)); + CorrelationGroupsAccumulator::new(true, zero_divisor(EvalMode::Legacy)); merged_scalar.merge_batch(&state).unwrap(); merged_grouped.merge_batch(&state, &[0], 1).unwrap(); for result in [ @@ -647,7 +612,7 @@ mod groups_tests { let mut scalar = CorrelationAccumulator::try_new(true, zero_divisor(EvalMode::Legacy)).unwrap(); let mut grouped = - CorrelationGroupsAccumulator::new_corr(true, zero_divisor(EvalMode::Legacy)); + CorrelationGroupsAccumulator::new(true, zero_divisor(EvalMode::Legacy)); let counts = if empty_first { [0.0, 100.0] } else { @@ -716,7 +681,7 @@ mod groups_tests { // count == 1 branch reads, does not matter. let mut scalar = CorrelationAccumulator::try_new(true, zero_divisor(eval_mode)).unwrap(); - let mut grouped = CorrelationGroupsAccumulator::new_corr(true, zero_divisor(eval_mode)); + let mut grouped = CorrelationGroupsAccumulator::new(true, zero_divisor(eval_mode)); scalar.update_batch(&input(0..6)).unwrap(); grouped .update_batch(&input(0..6), &[0; 6], None, 1) @@ -727,8 +692,7 @@ mod groups_tests { for ranges in [[0..3, 3..6], [3..6, 0..3]] { let mut scalar = CorrelationAccumulator::try_new(true, zero_divisor(eval_mode)).unwrap(); - let mut grouped = - CorrelationGroupsAccumulator::new_corr(true, zero_divisor(eval_mode)); + let mut grouped = CorrelationGroupsAccumulator::new(true, zero_divisor(eval_mode)); for range in ranges { let state = partial_state(&input(range)); scalar.merge_batch(&state).unwrap(); @@ -768,7 +732,7 @@ mod groups_tests { Arc::new(Float64Array::from(vec![0.1, 0.1, 0.1])), ]; let mut scalar = CorrelationAccumulator::try_new(true, zero_divisor.clone()).unwrap(); - let mut grouped = CorrelationGroupsAccumulator::new_corr(true, zero_divisor); + let mut grouped = CorrelationGroupsAccumulator::new(true, zero_divisor); scalar.update_batch(&input).unwrap(); grouped.update_batch(&input, &[0, 0, 0], None, 1).unwrap(); for result in [ @@ -792,7 +756,7 @@ mod groups_tests { zero_divisor(EvalMode::Ansi), ) .unwrap(); - let mut grouped = CorrelationGroupsAccumulator::new_corr( + let mut grouped = CorrelationGroupsAccumulator::new( null_on_divide_by_zero, zero_divisor(EvalMode::Ansi), ); @@ -828,7 +792,7 @@ mod groups_tests { zero_divisor(EvalMode::Ansi), ) .unwrap(); - let mut grouped = CorrelationGroupsAccumulator::new_corr( + let mut grouped = CorrelationGroupsAccumulator::new( null_on_divide_by_zero, zero_divisor(EvalMode::Ansi), ); @@ -846,7 +810,7 @@ mod groups_tests { Arc::new(Float64Array::from(vec![1.0, 2.0, 1.0, 2.0])), Arc::new(Float64Array::from(vec![2.0, 4.0, 3.0, 3.0])), ]; - let mut grouped = CorrelationGroupsAccumulator::new_corr( + let mut grouped = CorrelationGroupsAccumulator::new( null_on_divide_by_zero, zero_divisor(EvalMode::Ansi), ); diff --git a/native/spark-expr/src/agg_funcs/regr.rs b/native/spark-expr/src/agg_funcs/regr.rs index 2ba0e4c8cda..5026d0411c3 100644 --- a/native/spark-expr/src/agg_funcs/regr.rs +++ b/native/spark-expr/src/agg_funcs/regr.rs @@ -33,26 +33,28 @@ //! | `regr_r2` | `PearsonCorrelation` | 6 | //! | `regr_slope`/`_intercept` | composite cov + var | 7 | //! -//! Grouped aggregation reuses [`VarianceGroupsAccumulator`], -//! [`CovarianceGroupsAccumulator`], and [`CorrelationGroupsAccumulator`], -//! preserving the same partial-state layouts. +//! Grouped aggregation runs [`RegrGroupsAccumulator`], which keeps the same partial-state +//! layouts in one row per group and updates every moment of a statistic in a single pass. -use arrow::array::{Array, ArrayRef, BooleanArray, Float64Array}; +use arrow::array::{Array, ArrayRef, AsArray, BooleanArray, Float64Array}; use arrow::compute::{and, filter, is_not_null}; -use arrow::datatypes::{DataType, Field, FieldRef}; -use datafusion::common::{not_impl_err, Result, ScalarValue}; +use arrow::datatypes::{DataType, Field, FieldRef, Float64Type}; +use datafusion::common::{downcast_value, not_impl_err, Result, ScalarValue}; use datafusion::logical_expr::function::{AccumulatorArgs, StateFieldsArgs}; use datafusion::logical_expr::{ Accumulator, AggregateUDFImpl, EmitTo, GroupsAccumulator, Signature, Volatility, }; use datafusion::physical_expr::expressions::format_state_name; use datafusion::physical_expr::expressions::StatsType; +use std::fmt::Debug; use std::sync::Arc; -use crate::agg_funcs::correlation::CorrelationGroupsAccumulator; -use crate::agg_funcs::covariance::{CovarianceAccumulator, CovarianceGroupsAccumulator}; -use crate::agg_funcs::variance::{VarianceAccumulator, VarianceGroupsAccumulator}; -use crate::agg_funcs::welford::{r2_value, ZeroDivisor}; +use crate::agg_funcs::covariance::CovarianceAccumulator; +use crate::agg_funcs::variance::VarianceAccumulator; +use crate::agg_funcs::welford::{ + covariance_merge, covariance_update, variance_merge, variance_update, VarianceUpdate, + ZeroDivisor, +}; use crate::{EvalMode, QueryContextMap}; /// The kind of linear-regression statistic to compute. @@ -178,22 +180,16 @@ impl AggregateUDFImpl for Regr { _args: AccumulatorArgs, ) -> Result> { Ok(match self.regr_type { - RegrType::SXX | RegrType::SYY => Box::new(RegrMomentGroupsAccumulator { - var: VarianceGroupsAccumulator::new(StatsType::Population, false), - }), - RegrType::SXY => Box::new(RegrCovGroupsAccumulator { - covar: CovarianceGroupsAccumulator::new(StatsType::Population, false), - }), - RegrType::Slope | RegrType::Intercept => Box::new(RegrLineGroupsAccumulator { - covar: CovarianceGroupsAccumulator::new(StatsType::Population, false), - var_x: VarianceGroupsAccumulator::new(StatsType::Population, false), + RegrType::SXX | RegrType::SYY => RegrGroupsAccumulator::boxed(MomentKernel), + RegrType::SXY => RegrGroupsAccumulator::boxed(CovKernel), + RegrType::Slope | RegrType::Intercept => RegrGroupsAccumulator::boxed(LineKernel { intercept: self.regr_type == RegrType::Intercept, filter_var_by_pair_nulls: self.filter_var_by_pair_nulls, }), - RegrType::R2 => Box::new(CorrelationGroupsAccumulator::new_regr_r2( - self.r2_constant_dependent_is_perfect_fit, - self.zero_divisor.clone(), - )), + RegrType::R2 => RegrGroupsAccumulator::boxed(R2Kernel { + constant_dependent_is_perfect_fit: self.r2_constant_dependent_is_perfect_fit, + zero_divisor: self.zero_divisor.clone(), + }), }) } @@ -531,132 +527,185 @@ fn line_value(ck: f64, m2_x: f64, mean_x: f64, mean_y: f64, intercept: bool) -> } } -/// Grouped raw central moment. Spark has already pair-filtered the selected child. -#[derive(Debug)] -struct RegrMomentGroupsAccumulator { - var: VarianceGroupsAccumulator, +/// Spark RegrR2's result rule, shared by scalar and grouped accumulation. +/// SPARK-55969 swapped the constant-x/constant-y cases. Their guards precede +/// division, including in ANSI mode; Corr's count guards do not apply here. +fn r2_value( + ck: f64, + m2_y: f64, + m2_x: f64, + constant_dependent_is_perfect_fit: bool, + zero_divisor: &ZeroDivisor, +) -> Result> { + let (null_case, perfect_fit_case) = if constant_dependent_is_perfect_fit { + (m2_x == 0.0, m2_y == 0.0) + } else { + (m2_y == 0.0, m2_x == 0.0) + }; + if null_case { + Ok(None) + } else if perfect_fit_case { + Ok(Some(1.0)) + } else { + // Nonzero moments can still have a product that underflows to zero. + let product = m2_y * m2_x; + if product == 0.0 { + return match zero_divisor.error() { + Some(error) => Err(error), + None => Ok(None), + }; + } + // Preserve Spark's rounding and overflow behavior: dividing first and + // then squaring differs from (ck * ck) / (m2_y * m2_x). + let corr = ck / product.sqrt(); + Ok(Some(corr * corr)) + } } -impl GroupsAccumulator for RegrMomentGroupsAccumulator { - fn update_batch( - &mut self, - values: &[ArrayRef], - group_indices: &[usize], - opt_filter: Option<&BooleanArray>, - total_num_groups: usize, - ) -> Result<()> { - self.var - .update_batch(&values[0..1], group_indices, opt_filter, total_num_groups) +/// One `regr_*` statistic over a group's state row, whose `N` fields are the partial-state +/// columns in order. Each method repeats the arithmetic of the per-row accumulators, in the +/// same order, so grouped and ungrouped aggregation agree bit for bit. +trait RegrKernel: Debug + Send + Sync + 'static { + /// Folds one row into `state`, leaving it unchanged when the row does not take part. + /// `None` is a NULL input. + fn update(&self, state: &mut [f64; N], y: Option, x: Option); + + /// Merges a partial state into `state`. + fn merge(&self, state: &mut [f64; N], partial: [f64; N]); + + /// The statistic for a final state. + fn finish(&self, state: &[f64; N]) -> Result>; +} + +/// `regr_sxx` / `regr_syy`: count, mean, m2 of the pair-filtered child Spark passes twice. +#[derive(Debug)] +struct MomentKernel; + +impl RegrKernel<3> for MomentKernel { + #[inline] + fn update(&self, s: &mut [f64; 3], y: Option, _x: Option) { + if let Some(y) = y { + (s[0], s[1], s[2]) = + variance_update(s[0], s[1], s[2], y, VarianceUpdate::CentralMoment); + } } - fn merge_batch( - &mut self, - values: &[ArrayRef], - group_indices: &[usize], - total_num_groups: usize, - ) -> Result<()> { - self.var - .merge_batch(values, group_indices, total_num_groups) + fn merge(&self, s: &mut [f64; 3], p: [f64; 3]) { + (s[0], s[1], s[2]) = variance_merge(s[0], s[1], s[2], p[0], p[1], p[2]); } - fn evaluate(&mut self, emit_to: EmitTo) -> Result { - let counts = emit_to.take_needed(&mut self.var.counts); - let _ = emit_to.take_needed(&mut self.var.means); - let m2s = emit_to.take_needed(&mut self.var.m2s); - Ok(Arc::new(Float64Array::from_iter( - counts - .into_iter() - .zip(m2s) - .map(|(count, m2)| (count != 0.0).then_some(m2)), - ))) + fn finish(&self, s: &[f64; 3]) -> Result> { + Ok((s[0] != 0.0).then_some(s[2])) } +} - fn state(&mut self, emit_to: EmitTo) -> Result> { - self.var.state(emit_to) +/// `regr_sxy`: count, mean(y), mean(x), co-moment. +#[derive(Debug)] +struct CovKernel; + +impl RegrKernel<4> for CovKernel { + #[inline] + fn update(&self, s: &mut [f64; 4], y: Option, x: Option) { + if let (Some(y), Some(x)) = (y, x) { + (s[0], s[1], s[2], s[3]) = covariance_update(s[0], s[1], s[2], s[3], y, x); + } } - fn convert_to_state( - &self, - _values: &[ArrayRef], - _opt_filter: Option<&BooleanArray>, - ) -> Result> { - not_impl_err!("Input batch conversion to state not implemented") + fn merge(&self, s: &mut [f64; 4], p: [f64; 4]) { + (s[0], s[1], s[2], s[3]) = covariance_merge(s[0], s[1], s[2], s[3], p[0], p[1], p[2], p[3]); } - fn size(&self) -> usize { - self.var.size() + fn finish(&self, s: &[f64; 4]) -> Result> { + Ok((s[0] != 0.0).then_some(s[3])) } } -/// Grouped raw co-moment, with the same four state fields as covariance. +/// `regr_slope` / `regr_intercept`: the covariance of (x, y) followed by the variance of x. +/// The two keep their own counts, because before SPARK-48719 the variance also counts x +/// where y is null. #[derive(Debug)] -struct RegrCovGroupsAccumulator { - covar: CovarianceGroupsAccumulator, +struct LineKernel { + intercept: bool, + filter_var_by_pair_nulls: bool, } -impl GroupsAccumulator for RegrCovGroupsAccumulator { - fn update_batch( - &mut self, - values: &[ArrayRef], - group_indices: &[usize], - opt_filter: Option<&BooleanArray>, - total_num_groups: usize, - ) -> Result<()> { - self.covar - .update_batch(values, group_indices, opt_filter, total_num_groups) +impl RegrKernel<7> for LineKernel { + #[inline] + fn update(&self, s: &mut [f64; 7], y: Option, x: Option) { + let Some(x) = x else { + return; + }; + if let Some(y) = y { + (s[0], s[1], s[2], s[3]) = covariance_update(s[0], s[1], s[2], s[3], x, y); + } + if y.is_some() || !self.filter_var_by_pair_nulls { + (s[4], s[5], s[6]) = + variance_update(s[4], s[5], s[6], x, VarianceUpdate::CentralMoment); + } } - fn merge_batch( - &mut self, - values: &[ArrayRef], - group_indices: &[usize], - total_num_groups: usize, - ) -> Result<()> { - self.covar - .merge_batch(values, group_indices, total_num_groups) + fn merge(&self, s: &mut [f64; 7], p: [f64; 7]) { + (s[0], s[1], s[2], s[3]) = covariance_merge(s[0], s[1], s[2], s[3], p[0], p[1], p[2], p[3]); + (s[4], s[5], s[6]) = variance_merge(s[4], s[5], s[6], p[4], p[5], p[6]); } - fn evaluate(&mut self, emit_to: EmitTo) -> Result { - let counts = emit_to.take_needed(&mut self.covar.counts); - let _ = emit_to.take_needed(&mut self.covar.mean1s); - let _ = emit_to.take_needed(&mut self.covar.mean2s); - let cs = emit_to.take_needed(&mut self.covar.algo_consts); - Ok(Arc::new(Float64Array::from_iter( - counts - .into_iter() - .zip(cs) - .map(|(count, c)| (count != 0.0).then_some(c)), - ))) + fn finish(&self, s: &[f64; 7]) -> Result> { + Ok(line_value(s[3], s[6], s[1], s[2], self.intercept)) } +} - fn state(&mut self, emit_to: EmitTo) -> Result> { - self.covar.state(emit_to) +/// `regr_r2`: count, mean(y), mean(x), co-moment, m2(y), m2(x). The two moments share the +/// covariance's count and means, as they do in Spark's `PearsonCorrelation` buffer. +#[derive(Debug)] +struct R2Kernel { + constant_dependent_is_perfect_fit: bool, + zero_divisor: ZeroDivisor, +} + +impl RegrKernel<6> for R2Kernel { + #[inline] + fn update(&self, s: &mut [f64; 6], y: Option, x: Option) { + if let (Some(y), Some(x)) = (y, x) { + (_, _, s[4]) = variance_update(s[0], s[1], s[4], y, VarianceUpdate::Pearson); + (_, _, s[5]) = variance_update(s[0], s[2], s[5], x, VarianceUpdate::Pearson); + (s[0], s[1], s[2], s[3]) = covariance_update(s[0], s[1], s[2], s[3], y, x); + } } - fn convert_to_state( - &self, - _values: &[ArrayRef], - _opt_filter: Option<&BooleanArray>, - ) -> Result> { - not_impl_err!("Input batch conversion to state not implemented") + fn merge(&self, s: &mut [f64; 6], p: [f64; 6]) { + (_, _, s[4]) = variance_merge(s[0], s[1], s[4], p[0], p[1], p[4]); + (_, _, s[5]) = variance_merge(s[0], s[2], s[5], p[0], p[2], p[5]); + (s[0], s[1], s[2], s[3]) = covariance_merge(s[0], s[1], s[2], s[3], p[0], p[1], p[2], p[3]); } - fn size(&self) -> usize { - self.covar.size() + fn finish(&self, s: &[f64; 6]) -> Result> { + r2_value( + s[3], + s[4], + s[5], + self.constant_dependent_is_perfect_fit, + &self.zero_divisor, + ) } } -/// The covariance and x variance keep independent counts: before SPARK-48719, -/// the variance includes x even when y is null. Preserve the seven-field state. +/// Grouped `regr_*` accumulator: one state row per group, updated in a single pass. #[derive(Debug)] -struct RegrLineGroupsAccumulator { - covar: CovarianceGroupsAccumulator, - var_x: VarianceGroupsAccumulator, - intercept: bool, - filter_var_by_pair_nulls: bool, +struct RegrGroupsAccumulator { + kernel: K, + states: Vec<[f64; N]>, } -impl GroupsAccumulator for RegrLineGroupsAccumulator { +impl, const N: usize> RegrGroupsAccumulator { + fn boxed(kernel: K) -> Box { + Box::new(Self { + kernel, + states: Vec::new(), + }) + } +} + +impl, const N: usize> GroupsAccumulator for RegrGroupsAccumulator { fn update_batch( &mut self, values: &[ArrayRef], @@ -664,26 +713,35 @@ impl GroupsAccumulator for RegrLineGroupsAccumulator { opt_filter: Option<&BooleanArray>, total_num_groups: usize, ) -> Result<()> { - let cov_input = [Arc::clone(&values[1]), Arc::clone(&values[0])]; - self.covar - .update_batch(&cov_input, group_indices, opt_filter, total_num_groups)?; - // The variance child already skips null x. Modern Spark additionally - // excludes null y; preserve row positions so group_indices still align. - let combined = if self.filter_var_by_pair_nulls && values[0].null_count() != 0 { - let valid_y = is_not_null(&values[0])?; - Some(match opt_filter { - Some(filter) => and(filter, &valid_y)?, - None => valid_y, - }) - } else { - None - }; - self.var_x.update_batch( - &values[1..2], - group_indices, - combined.as_ref().or(opt_filter), - total_num_groups, - ) + let y = values[0].as_primitive::(); + let x = values[1].as_primitive::(); + self.states.resize(total_num_groups, [0.0; N]); + + // Carry the state in a local while consecutive rows share a group, and reload it + // only when the group changes. Every row still writes it back, so input clustered + // by key updates the local without waiting on its own stores. + let mut current = usize::MAX; + let mut state = [0.0; N]; + let rows = group_indices.iter().zip(y.values()).zip(x.values()); + for (idx, ((&group_index, &yv), &xv)) in rows.enumerate() { + if let Some(f) = opt_filter { + if !f.is_valid(idx) || !f.value(idx) { + continue; + } + } + let slot = &mut self.states[group_index]; + if current != group_index { + current = group_index; + state = *slot; + } + self.kernel.update( + &mut state, + y.is_valid(idx).then_some(yv), + x.is_valid(idx).then_some(xv), + ); + *slot = state; + } + Ok(()) } fn merge_batch( @@ -692,29 +750,35 @@ impl GroupsAccumulator for RegrLineGroupsAccumulator { group_indices: &[usize], total_num_groups: usize, ) -> Result<()> { - self.covar - .merge_batch(&values[0..4], group_indices, total_num_groups)?; - self.var_x - .merge_batch(&values[4..7], group_indices, total_num_groups) + let mut columns = Vec::with_capacity(N); + for value in &values[..N] { + columns.push(downcast_value!(value, Float64Array)); + } + self.states.resize(total_num_groups, [0.0; N]); + for (row, &group_index) in group_indices.iter().enumerate() { + let partial = std::array::from_fn(|field| columns[field].value(row)); + self.kernel.merge(&mut self.states[group_index], partial); + } + Ok(()) } fn evaluate(&mut self, emit_to: EmitTo) -> Result { - let _ = emit_to.take_needed(&mut self.covar.counts); - let means_x = emit_to.take_needed(&mut self.covar.mean1s); - let means_y = emit_to.take_needed(&mut self.covar.mean2s); - let cs = emit_to.take_needed(&mut self.covar.algo_consts); - let _ = emit_to.take_needed(&mut self.var_x.counts); - let _ = emit_to.take_needed(&mut self.var_x.means); - let m2s = emit_to.take_needed(&mut self.var_x.m2s); - Ok(Arc::new(Float64Array::from_iter((0..cs.len()).map(|i| { - line_value(cs[i], m2s[i], means_x[i], means_y[i], self.intercept) - })))) + let states = emit_to.take_needed(&mut self.states); + let values = states + .iter() + .map(|state| self.kernel.finish(state)) + .collect::>()?; + Ok(Arc::new(values)) } fn state(&mut self, emit_to: EmitTo) -> Result> { - let mut state = self.covar.state(emit_to)?; - state.extend(self.var_x.state(emit_to)?); - Ok(state) + let states = emit_to.take_needed(&mut self.states); + Ok((0..N) + .map(|field| { + let column = states.iter().map(|state| state[field]); + Arc::new(Float64Array::from_iter_values(column)) as ArrayRef + }) + .collect()) } fn convert_to_state( @@ -726,7 +790,7 @@ impl GroupsAccumulator for RegrLineGroupsAccumulator { } fn size(&self) -> usize { - self.covar.size() + self.var_x.size() + self.states.capacity() * size_of::<[f64; N]>() } } @@ -1173,7 +1237,7 @@ mod tests { #[cfg(test)] mod groups_tests { use super::*; - use crate::{create_query_context_map, Correlation, QueryContext, SparkErrorWithContext}; + use crate::{create_query_context_map, QueryContext, SparkErrorWithContext}; use arrow::array::AsArray; use arrow::datatypes::{Float64Type, Schema}; use datafusion::common::DataFusionError; @@ -1321,7 +1385,9 @@ mod groups_tests { // The dense case has NULLs only outside the sliced range. for start in [1, 12, 23] { let chunk: Vec<_> = values.iter().map(|a| a.slice(start, 11)).collect(); - let indices: Vec<_> = (start..start + 11).map(|i| i % 5).collect(); + // Pairs of rows share a group, so runs meet NULLs, FILTER and batch + // boundaries, and a group comes back after other groups. + let indices: Vec<_> = (start..start + 11).map(|i| i / 2 % 5).collect(); let predicate = BooleanArray::from_iter((0..35).map(|i| { if i % 13 == 0 { None @@ -1500,10 +1566,9 @@ mod groups_tests { RegrType::SXX | RegrType::SYY => 3, RegrType::SXY => 4, RegrType::Slope | RegrType::Intercept => 7, - RegrType::R2 => 10, + RegrType::R2 => 6, }; - let allocated = grouped.size(); - assert!(allocated >= 4 * vectors * size_of::()); + assert!(grouped.size() >= 4 * vectors * size_of::()); assert!(grouped .state(EmitTo::First(0)) .unwrap() @@ -1515,9 +1580,6 @@ mod groups_tests { first.as_primitive::().iter().next().unwrap(), scalar_value(scalars.remove(0).as_mut()), ); - // EmitTo keeps the backing allocation when draining a small - // prefix. Accounting must reflect capacity, not the new length. - assert_eq!(grouped.size(), allocated); let state = grouped.state(EmitTo::First(1)).unwrap(); same_state(&state, 0, scalars.remove(0).as_mut()); assert!(grouped.size() >= 2 * vectors * size_of::()); @@ -1592,8 +1654,8 @@ mod groups_tests { expected, ); } - // Synthetic partial state checks that R2 does not inherit Corr's - // count==0 shortcut. Empty-partial merge arithmetic produces NaN M2. + // R2 has no count == 0 shortcut: merging empty partials can leave a NaN + // moment, which Spark's result rule turns into NaN. let state: Vec = [0.0, f64::NAN, f64::NAN, 0.0, 0.0, 0.0] .into_iter() .map(|v| Arc::new(Float64Array::from(vec![v])) as ArrayRef) @@ -1611,31 +1673,6 @@ mod groups_tests { Some(f64::NAN), ); } - let corr = physical(AggregateUDF::new_from_impl(Correlation::new( - "corr", - DataType::Float64, - false, - EvalMode::Legacy, - None, - create_query_context_map(), - ))); - let mut corr = corr.create_groups_accumulator().unwrap(); - corr.update_batch( - &inputs(RegrType::R2, &[Some(1.0)], &[Some(2.0)]), - &[0], - None, - 1, - ) - .unwrap(); - same_value( - corr.evaluate(EmitTo::All) - .unwrap() - .as_primitive::() - .iter() - .next() - .unwrap(), - Some(f64::NAN), - ); } #[test] diff --git a/native/spark-expr/src/agg_funcs/welford.rs b/native/spark-expr/src/agg_funcs/welford.rs index ff4645dab76..44c096f5223 100644 --- a/native/spark-expr/src/agg_funcs/welford.rs +++ b/native/spark-expr/src/agg_funcs/welford.rs @@ -15,13 +15,13 @@ // specific language governing permissions and limitations // under the License. -//! Statistical update, merge and finalization shared by the per-row +//! Welford-style online update/merge math shared by the per-row //! `Accumulator` and the vectorized `GroupsAccumulator` implementations of -//! variance/stddev/covariance/correlation/regression. Counts are `f64` to match +//! variance/stddev/covariance/correlation. Counts are `f64` to match //! Spark's wire-format state. use arrow::buffer::NullBuffer; -use datafusion::common::{DataFusionError, Result}; +use datafusion::common::DataFusionError; use datafusion::physical_expr::expressions::StatsType; use std::hash::{Hash, Hasher}; use std::sync::Arc; @@ -251,38 +251,3 @@ pub(crate) fn covariance_merge( let new_c = c_a + c_b + delta1 * delta2_n * count_a * count_b; (new_count, new_mean1, new_mean2, new_c) } - -/// Spark RegrR2's result rule, shared by scalar and grouped accumulation. -/// SPARK-55969 swapped the constant-x/constant-y cases. Their guards precede -/// division, including in ANSI mode; Corr's count guards do not apply here. -pub(super) fn r2_value( - ck: f64, - m2_y: f64, - m2_x: f64, - constant_dependent_is_perfect_fit: bool, - zero_divisor: &ZeroDivisor, -) -> Result> { - let (null_case, perfect_fit_case) = if constant_dependent_is_perfect_fit { - (m2_x == 0.0, m2_y == 0.0) - } else { - (m2_y == 0.0, m2_x == 0.0) - }; - if null_case { - Ok(None) - } else if perfect_fit_case { - Ok(Some(1.0)) - } else { - // Nonzero moments can still have a product that underflows to zero. - let product = m2_y * m2_x; - if product == 0.0 { - return match zero_divisor.error() { - Some(error) => Err(error), - None => Ok(None), - }; - } - // Preserve Spark's rounding and overflow behavior: dividing first and - // then squaring differs from (ck * ck) / (m2_y * m2_x). - let corr = ck / product.sqrt(); - Ok(Some(corr * corr)) - } -} diff --git a/spark/src/test/scala/org/apache/spark/sql/benchmark/CometAggregateExpressionBenchmark.scala b/spark/src/test/scala/org/apache/spark/sql/benchmark/CometAggregateExpressionBenchmark.scala index 468df35c775..f789e3cecd5 100644 --- a/spark/src/test/scala/org/apache/spark/sql/benchmark/CometAggregateExpressionBenchmark.scala +++ b/spark/src/test/scala/org/apache/spark/sql/benchmark/CometAggregateExpressionBenchmark.scala @@ -261,20 +261,9 @@ object CometAggregateExpressionBenchmark extends CometBenchmarkBase { highCardinalityAggregates ++ percentileAggregates ++ approxPercentileAggregates ++ approxCountDistinctAggregates ++ maxMinByAggregates - require( - mainArgs.forall(name => allAggregates.exists(_.name == name)), - s"Unknown aggregate name in: ${mainArgs.mkString(", ")}") - // Optional names allow focused comparisons. With SPARK_GENERATE_BENCHMARK_FILES=1, - // a filtered run replaces the results file with only the selected aggregates. - allAggregates - .filter(config => mainArgs.isEmpty || mainArgs.contains(config.name)) - .foreach { config => - runExpressionBenchmark( - config.name, - v.toLong, - config.query, - config.extraCometConfigs) - } + allAggregates.foreach { config => + runExpressionBenchmark(config.name, v.toLong, config.query, config.extraCometConfigs) + } } } } From 761f56e722a69b26d8c42d4d82b95387e2215dd6 Mon Sep 17 00:00:00 2001 From: rich7420 Date: Wed, 18 Nov 2026 17:14:09 +0800 Subject: [PATCH 4/4] test: rename regr grouped test items after the single-pass accumulator --- native/spark-expr/src/agg_funcs/regr.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/native/spark-expr/src/agg_funcs/regr.rs b/native/spark-expr/src/agg_funcs/regr.rs index 4433d3773e2..e763b578069 100644 --- a/native/spark-expr/src/agg_funcs/regr.rs +++ b/native/spark-expr/src/agg_funcs/regr.rs @@ -1562,13 +1562,13 @@ mod groups_tests { let groups = [0, 1, 2, 3, 0, 1, 2, 3]; grouped.update_batch(&values, &groups, None, 4).unwrap(); update_scalars(&mut scalars, &values, &groups, None); - let vectors = match kind { + let fields = match kind { RegrType::SXX | RegrType::SYY => 3, RegrType::SXY => 4, RegrType::Slope | RegrType::Intercept => 7, RegrType::R2 => 6, }; - assert!(grouped.size() >= 4 * vectors * size_of::()); + assert!(grouped.size() >= 4 * fields * size_of::()); assert!(grouped .state(EmitTo::First(0)) .unwrap() @@ -1582,7 +1582,7 @@ mod groups_tests { ); let state = grouped.state(EmitTo::First(1)).unwrap(); same_state(&state, 0, scalars.remove(0).as_mut()); - assert!(grouped.size() >= 2 * vectors * size_of::()); + assert!(grouped.size() >= 2 * fields * size_of::()); scalars.push(expr.create_accumulator().unwrap()); let more = inputs( kind, @@ -1618,7 +1618,7 @@ mod groups_tests { } #[test] - fn r2_factory_selects_regression_result_rules() { + fn grouped_r2_applies_regression_result_rules() { for modern in [false, true] { let expr = expression(RegrType::R2, modern); for (y, x, expected) in [