@@ -45,7 +45,8 @@ use std::sync::Arc;
4545
4646use crate :: agg_funcs:: covariance:: CovarianceAccumulator ;
4747use crate :: agg_funcs:: variance:: VarianceAccumulator ;
48- use crate :: { divide_by_zero_error, EvalMode } ;
48+ use crate :: agg_funcs:: welford:: ZeroDivisor ;
49+ use crate :: { EvalMode , QueryContextMap } ;
4950
5051/// The kind of linear-regression statistic to compute.
5152///
@@ -82,7 +83,7 @@ pub struct Regr {
8283 /// independent variable to `null`. When `false`, those two cases are reversed.
8384 r2_constant_dependent_is_perfect_fit : bool ,
8485 /// Only consulted for `R2` when the raw moment product underflows to zero.
85- eval_mode : EvalMode ,
86+ zero_divisor : ZeroDivisor ,
8687}
8788
8889impl Regr {
@@ -92,6 +93,8 @@ impl Regr {
9293 filter_var_by_pair_nulls : bool ,
9394 r2_constant_dependent_is_perfect_fit : bool ,
9495 eval_mode : EvalMode ,
96+ expr_id : Option < u64 > ,
97+ registry : Arc < QueryContextMap > ,
9598 ) -> Self {
9699 Self {
97100 name : name. into ( ) ,
@@ -102,7 +105,7 @@ impl Regr {
102105 regr_type,
103106 filter_var_by_pair_nulls,
104107 r2_constant_dependent_is_perfect_fit,
105- eval_mode,
108+ zero_divisor : ZeroDivisor :: new ( eval_mode, expr_id , registry ) ,
106109 }
107110 }
108111
@@ -145,7 +148,7 @@ impl AggregateUDFImpl for Regr {
145148 RegrType :: SXY => Box :: new ( RegrCovAccumulator :: try_new ( ) ?) ,
146149 RegrType :: R2 => Box :: new ( RegrR2Accumulator :: try_new (
147150 self . r2_constant_dependent_is_perfect_fit ,
148- self . eval_mode ,
151+ self . zero_divisor . clone ( ) ,
149152 ) ?) ,
150153 RegrType :: Slope => Box :: new ( RegrLineAccumulator :: try_new (
151154 false ,
@@ -306,19 +309,19 @@ struct RegrR2Accumulator {
306309 /// When `true` (after SPARK-55969), a constant dependent variable yields
307310 /// `1.0` and a constant independent variable yields `null`; reversed otherwise.
308311 constant_dependent_is_perfect_fit : bool ,
309- eval_mode : EvalMode ,
312+ zero_divisor : ZeroDivisor ,
310313}
311314
312315impl RegrR2Accumulator {
313- fn try_new ( constant_dependent_is_perfect_fit : bool , eval_mode : EvalMode ) -> Result < Self > {
316+ fn try_new ( constant_dependent_is_perfect_fit : bool , zero_divisor : ZeroDivisor ) -> Result < Self > {
314317 Ok ( Self {
315318 covar : CovarianceAccumulator :: try_new ( StatsType :: Population , false ) ?,
316319 var_y : VarianceAccumulator :: try_new ( StatsType :: Population , false ) ?
317320 . with_pearson_update ( ) ,
318321 var_x : VarianceAccumulator :: try_new ( StatsType :: Population , false ) ?
319322 . with_pearson_update ( ) ,
320323 constant_dependent_is_perfect_fit,
321- eval_mode ,
324+ zero_divisor ,
322325 } )
323326 }
324327}
@@ -393,10 +396,9 @@ impl Accumulator for RegrR2Accumulator {
393396 // Spark's Divide handles this only after the constant-input guards.
394397 let product = m2_y * m2_x;
395398 if product == 0.0 {
396- return if self . eval_mode == EvalMode :: Ansi {
397- Err ( divide_by_zero_error ( ) . into ( ) )
398- } else {
399- Ok ( ScalarValue :: Float64 ( None ) )
399+ return match self . zero_divisor . error ( ) {
400+ Some ( error) => Err ( error) ,
401+ None => Ok ( ScalarValue :: Float64 ( None ) ) ,
400402 } ;
401403 }
402404 // Mirror Spark's exact evaluation order (corr = ck / sqrt(m2_y * m2_x);
@@ -516,16 +518,23 @@ impl Accumulator for RegrLineAccumulator {
516518#[ cfg( test) ]
517519mod tests {
518520 use super :: * ;
521+ use crate :: { create_query_context_map, QueryContext , SparkErrorWithContext } ;
519522 use arrow:: array:: Float64Array ;
520523 use datafusion:: common:: DataFusionError ;
521524 use datafusion_comet_common:: SparkError ;
522525
526+ fn zero_divisor ( eval_mode : EvalMode ) -> ZeroDivisor {
527+ ZeroDivisor :: new ( eval_mode, None , create_query_context_map ( ) )
528+ }
529+
523530 fn acc ( regr_type : RegrType ) -> Box < dyn Accumulator > {
524531 match regr_type {
525532 RegrType :: SXX | RegrType :: SYY => Box :: new ( RegrMomentAccumulator :: try_new ( ) . unwrap ( ) ) ,
526533 RegrType :: SXY => Box :: new ( RegrCovAccumulator :: try_new ( ) . unwrap ( ) ) ,
527534 // Default to the post-SPARK-55969 degenerate-case semantics.
528- RegrType :: R2 => Box :: new ( RegrR2Accumulator :: try_new ( true , EvalMode :: Legacy ) . unwrap ( ) ) ,
535+ RegrType :: R2 => {
536+ Box :: new ( RegrR2Accumulator :: try_new ( true , zero_divisor ( EvalMode :: Legacy ) ) . unwrap ( ) )
537+ }
529538 // Existing tests exercise the Spark 3.5+ both-non-null semantics.
530539 RegrType :: Slope => Box :: new ( RegrLineAccumulator :: try_new ( false , true ) . unwrap ( ) ) ,
531540 RegrType :: Intercept => Box :: new ( RegrLineAccumulator :: try_new ( true , true ) . unwrap ( ) ) ,
@@ -636,20 +645,50 @@ mod tests {
636645 }
637646 }
638647
648+ #[ test]
649+ fn r2_ansi_error_carries_query_context ( ) {
650+ let sql = "SELECT regr_r2(v, v) FROM t" ;
651+ let registry = create_query_context_map ( ) ;
652+ registry. register (
653+ 7 ,
654+ QueryContext :: new ( sql. to_string ( ) , 7 , 19 , None , None , 1 , 7 ) ,
655+ ) ;
656+ for perfect_dep in [ false , true ] {
657+ let mut a = RegrR2Accumulator :: try_new (
658+ perfect_dep,
659+ ZeroDivisor :: new ( EvalMode :: Ansi , Some ( 7 ) , Arc :: clone ( & registry) ) ,
660+ )
661+ . unwrap ( ) ;
662+ let v = vec ! [ Some ( 1e-100 ) , Some ( 2e-100 ) ] ;
663+ a. update_batch ( & cols ( v. clone ( ) , v) ) . unwrap ( ) ;
664+ let DataFusionError :: External ( error) = a. evaluate ( ) . unwrap_err ( ) else {
665+ panic ! ( "expected a Spark error with query context" ) ;
666+ } ;
667+ let error = error. downcast_ref :: < SparkErrorWithContext > ( ) . unwrap ( ) ;
668+ assert ! ( matches!( error. error, SparkError :: DivideByZero ) ) ;
669+ assert_eq ! ( error. context. as_ref( ) . unwrap( ) . sql_text. as_str( ) , sql) ;
670+ }
671+ }
672+
639673 #[ test]
640674 fn r2_moment_product_underflow ( ) {
641675 for eval_mode in [ EvalMode :: Legacy , EvalMode :: Ansi ] {
642676 for perfect_dep in [ false , true ] {
643677 for sign in [ -1.0 , 1.0 ] {
644678 for merge in [ false , true ] {
645- let mut a = RegrR2Accumulator :: try_new ( perfect_dep, eval_mode) . unwrap ( ) ;
679+ let mut a =
680+ RegrR2Accumulator :: try_new ( perfect_dep, zero_divisor ( eval_mode) )
681+ . unwrap ( ) ;
646682 let y = vec ! [ Some ( 1e-100 ) , None , Some ( 2e-100 ) ] ;
647683 let x = vec ! [ Some ( sign * 1e-100 ) , Some ( 1e200 ) , Some ( sign * 2e-100 ) ] ;
648684 if merge {
649685 // Merge two single-row partials, including an unpaired null.
650686 for range in [ 0 ..2 , 2 ..3 ] {
651- let mut partial =
652- RegrR2Accumulator :: try_new ( perfect_dep, eval_mode) . unwrap ( ) ;
687+ let mut partial = RegrR2Accumulator :: try_new (
688+ perfect_dep,
689+ zero_divisor ( eval_mode) ,
690+ )
691+ . unwrap ( ) ;
653692 partial
654693 . update_batch ( & cols (
655694 y[ range. clone ( ) ] . to_vec ( ) ,
@@ -678,7 +717,8 @@ mod tests {
678717 fn r2_zero_covariance_with_underflowed_product ( ) {
679718 for eval_mode in [ EvalMode :: Legacy , EvalMode :: Ansi ] {
680719 for perfect_dep in [ false , true ] {
681- let mut a = RegrR2Accumulator :: try_new ( perfect_dep, eval_mode) . unwrap ( ) ;
720+ let mut a =
721+ RegrR2Accumulator :: try_new ( perfect_dep, zero_divisor ( eval_mode) ) . unwrap ( ) ;
682722 // A zero co-moment still divides by zero, rather than returning NaN.
683723 let state = [ 4.0 , 0.0 , 0.0 , 0.0 , 4e-200 , 4e-200 ]
684724 . into_iter ( )
@@ -697,7 +737,9 @@ mod tests {
697737 for sign in [ -1.0 , 1.0 ] {
698738 for ( values, nan) in [ ( vec ! [ 1e100 , 2e100 ] , false ) , ( vec ! [ 1e200 , -1e200 ] , true ) ]
699739 {
700- let mut a = RegrR2Accumulator :: try_new ( perfect_dep, eval_mode) . unwrap ( ) ;
740+ let mut a =
741+ RegrR2Accumulator :: try_new ( perfect_dep, zero_divisor ( eval_mode) )
742+ . unwrap ( ) ;
701743 a. update_batch ( & cols (
702744 values. iter ( ) . copied ( ) . map ( Some ) . collect ( ) ,
703745 values. iter ( ) . map ( |v| Some ( sign * v) ) . collect ( ) ,
@@ -739,7 +781,8 @@ mod tests {
739781
740782 for eval_mode in [ EvalMode :: Legacy , EvalMode :: Ansi ] {
741783 let r2 = |perfect_dep : bool , y : Vec < Option < f64 > > , x : Vec < Option < f64 > > | {
742- let mut a = RegrR2Accumulator :: try_new ( perfect_dep, eval_mode) . unwrap ( ) ;
784+ let mut a =
785+ RegrR2Accumulator :: try_new ( perfect_dep, zero_divisor ( eval_mode) ) . unwrap ( ) ;
743786 a. update_batch ( & cols ( y, x) ) . unwrap ( ) ;
744787 match a. evaluate ( ) . unwrap ( ) {
745788 ScalarValue :: Float64 ( v) => v,
0 commit comments