Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port kurtosis_pop UDAF #7

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,4 @@ SELECT min_by(x, y) FROM VALUES (1, 10), (2, 5), (3, 15), (4, 8) as tab(x, y);
- [x] `mode(expression) -> scalar` - Returns the most frequent (mode) value from a column of data.
- [x] `max_by(expression1, expression2) -> scalar` - Returns the value of `expression1` associated with the maximum value of `expression2`.
- [x] `min_by(expression1, expression2) -> scalar` - Returns the value of `expression1` associated with the minimum value of `expression2`.
- [x] `kurtois_pop(expression) -> scalar` - Computes the excess kurtosis (Fisher’s definition) without bias correction.
188 changes: 188 additions & 0 deletions src/kurtosis_pop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// 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.

// Copired from `datafusion/functions-aggregate/src/kurtosis_pop.rs`
// Originally authored by goldmedal

use arrow::array::{Array, ArrayRef, Float64Array, UInt64Array};
use datafusion::arrow::datatypes::{DataType, Field};
use datafusion::common::cast::as_float64_array;
use datafusion::common::{downcast_value, DataFusionError, Result, ScalarValue};
use datafusion::logical_expr::function::{AccumulatorArgs, StateFieldsArgs};
use datafusion::logical_expr::{Accumulator, AggregateUDFImpl, Signature, Volatility};
use std::any::Any;
use std::fmt::Debug;

make_udaf_expr_and_func!(
KurtosisPopFunction,
kurtosis_pop,
x,
"Calculates the excess kurtosis (Fisher’s definition) without bias correction.",
kurtosis_pop_udaf
);

pub struct KurtosisPopFunction {
signature: Signature,
}

impl Debug for KurtosisPopFunction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("KurtosisPopFunction")
.field("signature", &self.signature)
.finish()
}
}

impl Default for KurtosisPopFunction {
fn default() -> Self {
Self::new()
}
}

impl KurtosisPopFunction {
pub fn new() -> Self {
Self {
signature: Signature::coercible(vec![DataType::Float64], Volatility::Immutable),
}
}
}

impl AggregateUDFImpl for KurtosisPopFunction {
fn as_any(&self) -> &dyn Any {
self
}

fn name(&self) -> &str {
"kurtosis_pop"
}

fn signature(&self) -> &Signature {
&self.signature
}

fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
Ok(DataType::Float64)
}

fn state_fields(&self, _args: StateFieldsArgs) -> Result<Vec<Field>> {
Ok(vec![
Field::new("count", DataType::UInt64, true),
Field::new("sum", DataType::Float64, true),
Field::new("sum_sqr", DataType::Float64, true),
Field::new("sum_cub", DataType::Float64, true),
Field::new("sum_four", DataType::Float64, true),
])
}

fn accumulator(&self, _acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> {
Ok(Box::new(KurtosisPopAccumulator::new()))
}
}

/// Accumulator for calculating the excess kurtosis (Fisher’s definition) without bias correction.
/// This implementation follows the [DuckDB implementation]:
/// <https://github.com/duckdb/duckdb/blob/main/src/core_functions/aggregate/distributive/kurtosis.cpp>
#[derive(Debug, Default)]
pub struct KurtosisPopAccumulator {
count: u64,
sum: f64,
sum_sqr: f64,
sum_cub: f64,
sum_four: f64,
}

impl KurtosisPopAccumulator {
pub fn new() -> Self {
Self {
count: 0,
sum: 0.0,
sum_sqr: 0.0,
sum_cub: 0.0,
sum_four: 0.0,
}
}
}

impl Accumulator for KurtosisPopAccumulator {
fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
let array = as_float64_array(&values[0])?;
for value in array.iter().flatten() {
self.count += 1;
self.sum += value;
self.sum_sqr += value.powi(2);
self.sum_cub += value.powi(3);
self.sum_four += value.powi(4);
}
Ok(())
}

fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {
let counts = downcast_value!(states[0], UInt64Array);
let sums = downcast_value!(states[1], Float64Array);
let sum_sqrs = downcast_value!(states[2], Float64Array);
let sum_cubs = downcast_value!(states[3], Float64Array);
let sum_fours = downcast_value!(states[4], Float64Array);

for i in 0..counts.len() {
let c = counts.value(i);
if c == 0 {
continue;
}
self.count += c;
self.sum += sums.value(i);
self.sum_sqr += sum_sqrs.value(i);
self.sum_cub += sum_cubs.value(i);
self.sum_four += sum_fours.value(i);
}

Ok(())
}

fn evaluate(&mut self) -> Result<ScalarValue> {
if self.count < 1 {
return Ok(ScalarValue::Float64(None));
}

let count_64 = 1_f64 / self.count as f64;
let m4 = count_64
* (self.sum_four - 4.0 * self.sum_cub * self.sum * count_64
+ 6.0 * self.sum_sqr * self.sum.powi(2) * count_64.powi(2)
- 3.0 * self.sum.powi(4) * count_64.powi(3));

let m2 = (self.sum_sqr - self.sum.powi(2) * count_64) * count_64;
if m2 <= 0.0 {
return Ok(ScalarValue::Float64(None));
}

let target = m4 / (m2.powi(2)) - 3.0;
Ok(ScalarValue::Float64(Some(target)))
}

fn size(&self) -> usize {
std::mem::size_of_val(self)
}

fn state(&mut self) -> Result<Vec<ScalarValue>> {
Ok(vec![
ScalarValue::from(self.count),
ScalarValue::from(self.sum),
ScalarValue::from(self.sum_sqr),
ScalarValue::from(self.sum_cub),
ScalarValue::from(self.sum_four),
])
}
}
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,23 @@ use datafusion::logical_expr::AggregateUDF;
#[macro_use]
pub mod macros;
pub mod common;
pub mod kurtosis_pop;
pub mod max_min_by;
pub mod mode;
pub mod expr_extra_fn {
pub use super::kurtosis_pop::kurtosis_pop;
pub use super::max_min_by::max_by;
pub use super::max_min_by::min_by;
pub use super::mode::mode;
}

pub fn all_extra_aggregate_functions() -> Vec<Arc<AggregateUDF>> {
vec![mode_udaf(), max_min_by::max_by_udaf(), max_min_by::min_by_udaf()]
vec![
mode_udaf(),
max_min_by::max_by_udaf(),
max_min_by::min_by_udaf(),
kurtosis_pop::kurtosis_pop_udaf(),
]
}

/// Registers all enabled packages with a [`FunctionRegistry`]
Expand Down
60 changes: 60 additions & 0 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,63 @@ async fn test_max_by_and_min_by() {
- +---------------------+
"###);
}

#[tokio::test]
async fn test_kurtosis_pop() {
let mut execution = TestExecution::new().await.unwrap().with_setup(TEST_TABLE).await;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were additional tests too in apache/datafusion#12647

https://github.com/apache/datafusion/pull/12647/files#diff-a60d69cf923e18b0532e7ba3470c4b728e6b00230b3ea242f3fbe95e899c5ffeL5866

Maybe it is time to consider adding sqllogictest to this repo too 🤔 (not as part of this PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 I was thinking the same. Plus it kind of hazzle to assert those table output snapshots.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to work on adding sqllogictest to this project

@dharanad @alamb


// Test with int64
let actual = execution
.run_and_format("SELECT kurtosis_pop(int64_col) FROM test_table")
.await;

insta::assert_yaml_snapshot!(actual, @r###"
- +------------------------------------+
- "| kurtosis_pop(test_table.int64_col) |"
- +------------------------------------+
- "| -0.9599999999999755 |"
- +------------------------------------+
"###);

// Test with float64
let actual = execution
.run_and_format("SELECT kurtosis_pop(float64_col) FROM test_table")
.await;

insta::assert_yaml_snapshot!(actual, @r###"
- +--------------------------------------+
- "| kurtosis_pop(test_table.float64_col) |"
- +--------------------------------------+
- "| -0.9599999999999755 |"
- +--------------------------------------+
"###);

let actual = execution
.run_and_format("SELECT kurtosis_pop(col) FROM VALUES (1.0) as tab(col)")
.await;
insta::assert_yaml_snapshot!(actual, @r###"
- +-----------------------+
- "| kurtosis_pop(tab.col) |"
- +-----------------------+
- "| |"
- +-----------------------+
"###);

let actual = execution.run_and_format("SELECT kurtosis_pop(1.0)").await;
insta::assert_yaml_snapshot!(actual, @r###"
- +--------------------------+
- "| kurtosis_pop(Float64(1)) |"
- +--------------------------+
- "| |"
- +--------------------------+
"###);

let actual = execution.run_and_format("SELECT kurtosis_pop(null)").await;
insta::assert_yaml_snapshot!(actual, @r###"
- +--------------------+
- "| kurtosis_pop(NULL) |"
- +--------------------+
- "| |"
- +--------------------+
"###);
}
Loading