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

feat: expose centroids in approx_percentile_cont fluent api #11878

Merged
merged 3 commits into from
Aug 8, 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
19 changes: 17 additions & 2 deletions datafusion/core/tests/dataframe/dataframe_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ async fn test_fn_approx_median() -> Result<()> {

#[tokio::test]
async fn test_fn_approx_percentile_cont() -> Result<()> {
let expr = approx_percentile_cont(col("b"), lit(0.5));
let expr = approx_percentile_cont(col("b"), lit(0.5), None);

let expected = [
"+---------------------------------------------+",
Expand All @@ -381,7 +381,7 @@ async fn test_fn_approx_percentile_cont() -> Result<()> {
None::<&str>,
"arg_2".to_string(),
));
let expr = approx_percentile_cont(col("b"), alias_expr);
let expr = approx_percentile_cont(col("b"), alias_expr, None);
let df = create_test_table().await?;
let expected = [
"+--------------------------------------+",
Expand All @@ -394,6 +394,21 @@ async fn test_fn_approx_percentile_cont() -> Result<()> {

assert_batches_eq!(expected, &batches);

// with number of centroids set
let expr = approx_percentile_cont(col("b"), lit(0.5), Some(lit(2)));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

NOTE: I don't have SQL Server, so I did not verify that it produces this result.

I chose a choice for centroids that produced a different result than the default.

let expected = [
"+------------------------------------------------------+",
"| approx_percentile_cont(test.b,Float64(0.5),Int32(2)) |",
"+------------------------------------------------------+",
"| 30 |",
"+------------------------------------------------------+",
];

let df = create_test_table().await?;
let batches = df.aggregate(vec![], vec![expr]).unwrap().collect().await?;

assert_batches_eq!(expected, &batches);

Ok(())
}

Expand Down
22 changes: 15 additions & 7 deletions datafusion/functions-aggregate/src/approx_percentile_cont.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,21 @@ use datafusion_physical_expr_common::aggregate::tdigest::{
};
use datafusion_physical_expr_common::utils::limited_convert_logical_expr_to_physical_expr_with_dfschema;

make_udaf_expr_and_func!(
ApproxPercentileCont,
approx_percentile_cont,
expression percentile,
"Computes the approximate percentile continuous of a set of numbers",
approx_percentile_cont_udaf
);
create_func!(ApproxPercentileCont, approx_percentile_cont_udaf);

/// Computes the approximate percentile continuous of a set of numbers
pub fn approx_percentile_cont(
expression: Expr,
percentile: Expr,
centroids: Option<Expr>,
) -> Expr {
let args = if let Some(centroids) = centroids {
vec![expression, percentile, centroids]
} else {
vec![expression, percentile]
};
approx_percentile_cont_udaf().call(args)
}

pub struct ApproxPercentileCont {
signature: Signature,
Expand Down
3 changes: 2 additions & 1 deletion datafusion/proto/tests/cases/roundtrip_logical_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,8 @@ async fn roundtrip_expr_api() -> Result<()> {
stddev_pop(lit(2.2)),
approx_distinct(lit(2)),
approx_median(lit(2)),
approx_percentile_cont(lit(2), lit(0.5)),
approx_percentile_cont(lit(2), lit(0.5), None),
approx_percentile_cont(lit(2), lit(0.5), Some(lit(50))),
approx_percentile_cont_with_weight(lit(2), lit(1), lit(0.5)),
grouping(lit(1)),
bit_and(lit(2)),
Expand Down