@@ -43,6 +43,14 @@ make_udaf_expr_and_func!(
4343 covar_samp_udaf
4444) ;
4545
46+ make_udaf_expr_and_func ! (
47+ CovariancePopulation ,
48+ covar_pop,
49+ y x,
50+ "Computes the population covariance." ,
51+ covar_pop_udaf
52+ ) ;
53+
4654pub struct CovarianceSample {
4755 signature : Signature ,
4856 aliases : Vec < String > ,
@@ -120,6 +128,79 @@ impl AggregateUDFImpl for CovarianceSample {
120128 }
121129}
122130
131+ pub struct CovariancePopulation {
132+ signature : Signature ,
133+ }
134+
135+ impl Debug for CovariancePopulation {
136+ fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
137+ f. debug_struct ( "CovariancePopulation" )
138+ . field ( "name" , & self . name ( ) )
139+ . field ( "signature" , & self . signature )
140+ . finish ( )
141+ }
142+ }
143+
144+ impl Default for CovariancePopulation {
145+ fn default ( ) -> Self {
146+ Self :: new ( )
147+ }
148+ }
149+
150+ impl CovariancePopulation {
151+ pub fn new ( ) -> Self {
152+ Self {
153+ signature : Signature :: uniform ( 2 , NUMERICS . to_vec ( ) , Volatility :: Immutable ) ,
154+ }
155+ }
156+ }
157+
158+ impl AggregateUDFImpl for CovariancePopulation {
159+ fn as_any ( & self ) -> & dyn std:: any:: Any {
160+ self
161+ }
162+
163+ fn name ( & self ) -> & str {
164+ "covar_pop"
165+ }
166+
167+ fn signature ( & self ) -> & Signature {
168+ & self . signature
169+ }
170+
171+ fn return_type ( & self , arg_types : & [ DataType ] ) -> Result < DataType > {
172+ if !arg_types[ 0 ] . is_numeric ( ) {
173+ return plan_err ! ( "Covariance requires numeric input types" ) ;
174+ }
175+
176+ Ok ( DataType :: Float64 )
177+ }
178+
179+ fn state_fields (
180+ & self ,
181+ name : & str ,
182+ _value_type : DataType ,
183+ _ordering_fields : Vec < Field > ,
184+ ) -> Result < Vec < Field > > {
185+ Ok ( vec ! [
186+ Field :: new( format_state_name( name, "count" ) , DataType :: UInt64 , true ) ,
187+ Field :: new( format_state_name( name, "mean1" ) , DataType :: Float64 , true ) ,
188+ Field :: new( format_state_name( name, "mean2" ) , DataType :: Float64 , true ) ,
189+ Field :: new(
190+ format_state_name( name, "algo_const" ) ,
191+ DataType :: Float64 ,
192+ true ,
193+ ) ,
194+ ] )
195+ }
196+
197+ fn accumulator ( & self , _acc_args : AccumulatorArgs ) -> Result < Box < dyn Accumulator > > {
198+ Ok ( Box :: new ( CovarianceAccumulator :: try_new (
199+ StatsType :: Population ,
200+ ) ?) )
201+ }
202+ }
203+
123204/// An accumulator to compute covariance
124205/// The algorithm used is an online implementation and numerically stable. It is derived from the following paper
125206/// for calculating variance:
0 commit comments