jayzhan211 commented on code in PR #10834:
URL: https://github.com/apache/datafusion/pull/10834#discussion_r1632136912


##########
datafusion/functions-aggregate/src/stddev.rs:
##########
@@ -0,0 +1,380 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Defines physical expressions that can evaluated at runtime during query 
execution
+
+use std::any::Any;
+use std::fmt::{Debug, Formatter};
+
+use arrow::{array::ArrayRef, datatypes::DataType, datatypes::Field};
+
+use datafusion_common::{internal_err, not_impl_err, Result};
+use datafusion_common::{plan_err, ScalarValue};
+use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs};
+use datafusion_expr::utils::format_state_name;
+use datafusion_expr::{Accumulator, AggregateUDFImpl, Signature, Volatility};
+use datafusion_physical_expr_common::aggregate::stats::StatsType;
+
+use crate::variance::VarianceAccumulator;
+
+make_udaf_expr_and_func!(
+    Stddev,
+    stddev,
+    expression,
+    "Compute the standard deviation of a set of numbers",
+    stddev_udaf
+);
+
+/// STDDEV and STDDEV_SAMP (standard deviation) aggregate expression
+pub struct Stddev {
+    signature: Signature,
+    alias: Vec<String>,
+}
+
+impl Debug for Stddev {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        f.debug_struct("Stddev")
+            .field("name", &self.name())
+            .field("signature", &self.signature)
+            .finish()
+    }
+}
+
+impl Default for Stddev {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl Stddev {
+    /// Create a new STDDEV aggregate function
+    pub fn new() -> Self {
+        Self {
+            signature: Signature::numeric(1, Volatility::Immutable),
+            alias: vec!["stddev_samp".to_string()],
+        }
+    }
+}
+
+impl AggregateUDFImpl for Stddev {
+    /// Return a reference to Any that can be used for downcasting
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn name(&self) -> &str {
+        "stddev"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+        if !arg_types[0].is_numeric() {
+            return plan_err!("Stddev requires numeric input types");
+        }
+
+        Ok(DataType::Float64)
+    }
+
+    fn state_fields(&self, args: StateFieldsArgs) -> Result<Vec<Field>> {
+        Ok(vec![
+            Field::new(
+                format_state_name(args.name, "count"),
+                DataType::UInt64,
+                true,
+            ),
+            Field::new(
+                format_state_name(args.name, "mean"),
+                DataType::Float64,
+                true,
+            ),
+            Field::new(format_state_name(args.name, "m2"), DataType::Float64, 
true),
+        ])
+    }
+
+    fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn 
Accumulator>> {
+        if acc_args.is_distinct {
+            return not_impl_err!("STDDEV_POP(DISTINCT) aggregations are not 
available");
+        }
+        Ok(Box::new(StddevAccumulator::try_new(StatsType::Sample)?))
+    }
+
+    fn create_sliding_accumulator(

Review Comment:
   
https://github.com/apache/datafusion/blob/6b7021479c956ba3ca2a04fff81487cd57f80624/datafusion/expr/src/udaf.rs#L414-L419



##########
datafusion/functions-aggregate/src/stddev.rs:
##########
@@ -0,0 +1,380 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Defines physical expressions that can evaluated at runtime during query 
execution
+
+use std::any::Any;
+use std::fmt::{Debug, Formatter};
+
+use arrow::{array::ArrayRef, datatypes::DataType, datatypes::Field};
+
+use datafusion_common::{internal_err, not_impl_err, Result};
+use datafusion_common::{plan_err, ScalarValue};
+use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs};
+use datafusion_expr::utils::format_state_name;
+use datafusion_expr::{Accumulator, AggregateUDFImpl, Signature, Volatility};
+use datafusion_physical_expr_common::aggregate::stats::StatsType;
+
+use crate::variance::VarianceAccumulator;
+
+make_udaf_expr_and_func!(
+    Stddev,
+    stddev,
+    expression,
+    "Compute the standard deviation of a set of numbers",
+    stddev_udaf
+);
+
+/// STDDEV and STDDEV_SAMP (standard deviation) aggregate expression
+pub struct Stddev {
+    signature: Signature,
+    alias: Vec<String>,
+}
+
+impl Debug for Stddev {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        f.debug_struct("Stddev")
+            .field("name", &self.name())
+            .field("signature", &self.signature)
+            .finish()
+    }
+}
+
+impl Default for Stddev {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl Stddev {
+    /// Create a new STDDEV aggregate function
+    pub fn new() -> Self {
+        Self {
+            signature: Signature::numeric(1, Volatility::Immutable),
+            alias: vec!["stddev_samp".to_string()],
+        }
+    }
+}
+
+impl AggregateUDFImpl for Stddev {
+    /// Return a reference to Any that can be used for downcasting
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn name(&self) -> &str {
+        "stddev"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+        if !arg_types[0].is_numeric() {
+            return plan_err!("Stddev requires numeric input types");
+        }
+
+        Ok(DataType::Float64)
+    }
+
+    fn state_fields(&self, args: StateFieldsArgs) -> Result<Vec<Field>> {
+        Ok(vec![
+            Field::new(
+                format_state_name(args.name, "count"),
+                DataType::UInt64,
+                true,
+            ),
+            Field::new(
+                format_state_name(args.name, "mean"),
+                DataType::Float64,
+                true,
+            ),
+            Field::new(format_state_name(args.name, "m2"), DataType::Float64, 
true),
+        ])
+    }
+
+    fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn 
Accumulator>> {
+        if acc_args.is_distinct {
+            return not_impl_err!("STDDEV_POP(DISTINCT) aggregations are not 
available");
+        }
+        Ok(Box::new(StddevAccumulator::try_new(StatsType::Sample)?))
+    }
+
+    fn create_sliding_accumulator(

Review Comment:
   I think we could rely on the default impl, since they are equivalent



##########
datafusion/functions-aggregate/src/stddev.rs:
##########
@@ -0,0 +1,380 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Defines physical expressions that can evaluated at runtime during query 
execution
+
+use std::any::Any;
+use std::fmt::{Debug, Formatter};
+
+use arrow::{array::ArrayRef, datatypes::DataType, datatypes::Field};
+
+use datafusion_common::{internal_err, not_impl_err, Result};
+use datafusion_common::{plan_err, ScalarValue};
+use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs};
+use datafusion_expr::utils::format_state_name;
+use datafusion_expr::{Accumulator, AggregateUDFImpl, Signature, Volatility};
+use datafusion_physical_expr_common::aggregate::stats::StatsType;
+
+use crate::variance::VarianceAccumulator;
+
+make_udaf_expr_and_func!(
+    Stddev,
+    stddev,
+    expression,
+    "Compute the standard deviation of a set of numbers",
+    stddev_udaf
+);
+
+/// STDDEV and STDDEV_SAMP (standard deviation) aggregate expression
+pub struct Stddev {
+    signature: Signature,
+    alias: Vec<String>,
+}
+
+impl Debug for Stddev {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        f.debug_struct("Stddev")
+            .field("name", &self.name())
+            .field("signature", &self.signature)
+            .finish()
+    }
+}
+
+impl Default for Stddev {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl Stddev {
+    /// Create a new STDDEV aggregate function
+    pub fn new() -> Self {
+        Self {
+            signature: Signature::numeric(1, Volatility::Immutable),
+            alias: vec!["stddev_samp".to_string()],
+        }
+    }
+}
+
+impl AggregateUDFImpl for Stddev {
+    /// Return a reference to Any that can be used for downcasting
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn name(&self) -> &str {
+        "stddev"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+        if !arg_types[0].is_numeric() {
+            return plan_err!("Stddev requires numeric input types");
+        }
+
+        Ok(DataType::Float64)
+    }
+
+    fn state_fields(&self, args: StateFieldsArgs) -> Result<Vec<Field>> {
+        Ok(vec![
+            Field::new(
+                format_state_name(args.name, "count"),
+                DataType::UInt64,
+                true,
+            ),
+            Field::new(
+                format_state_name(args.name, "mean"),
+                DataType::Float64,
+                true,
+            ),
+            Field::new(format_state_name(args.name, "m2"), DataType::Float64, 
true),
+        ])
+    }
+
+    fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn 
Accumulator>> {
+        if acc_args.is_distinct {
+            return not_impl_err!("STDDEV_POP(DISTINCT) aggregations are not 
available");
+        }
+        Ok(Box::new(StddevAccumulator::try_new(StatsType::Sample)?))
+    }
+
+    fn create_sliding_accumulator(
+        &self,
+        args: AccumulatorArgs,
+    ) -> Result<Box<dyn Accumulator>> {
+        self.accumulator(args)
+    }
+
+    fn aliases(&self) -> &[String] {
+        &self.alias
+    }
+}
+
+make_udaf_expr_and_func!(
+    StddevPop,
+    stddev_pop,
+    expression,
+    "Compute the population standard deviation of a set of numbers",
+    stddev_pop_udaf
+);
+
+/// STDDEV_POP population aggregate expression
+pub struct StddevPop {
+    signature: Signature,
+}
+
+impl Debug for StddevPop {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        f.debug_struct("StddevPop")
+            .field("name", &self.name())
+            .field("signature", &self.signature)
+            .finish()
+    }
+}
+
+impl Default for StddevPop {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl StddevPop {
+    /// Create a new STDDEV_POP aggregate function
+    pub fn new() -> Self {
+        Self {
+            signature: Signature::numeric(1, Volatility::Immutable),
+        }
+    }
+}
+
+impl AggregateUDFImpl for StddevPop {
+    /// Return a reference to Any that can be used for downcasting
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn name(&self) -> &str {
+        "stddev_pop"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn state_fields(&self, args: StateFieldsArgs) -> Result<Vec<Field>> {
+        Ok(vec![
+            Field::new(
+                format_state_name(args.name, "count"),
+                DataType::UInt64,
+                true,
+            ),
+            Field::new(
+                format_state_name(args.name, "mean"),
+                DataType::Float64,
+                true,
+            ),
+            Field::new(format_state_name(args.name, "m2"), DataType::Float64, 
true),
+        ])
+    }
+
+    fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn 
Accumulator>> {
+        if acc_args.is_distinct {
+            return not_impl_err!("STDDEV_POP(DISTINCT) aggregations are not 
available");
+        }
+        Ok(Box::new(StddevAccumulator::try_new(StatsType::Population)?))
+    }
+
+    fn create_sliding_accumulator(
+        &self,
+        args: AccumulatorArgs,
+    ) -> Result<Box<dyn Accumulator>> {
+        self.accumulator(args)

Review Comment:
   same here



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to