You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "jayzhan211 (via GitHub)" <gi...@apache.org> on 2024/05/24 01:30:32 UTC
[PR] Move Median to `functions-aggregate` [datafusion]
jayzhan211 opened a new pull request, #10644:
URL: https://github.com/apache/datafusion/pull/10644
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #.
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Move Median to `functions-aggregate` and Introduce Numeric signature [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #10644:
URL: https://github.com/apache/datafusion/pull/10644
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Move Median to `functions-aggregate` and Introduce Numeric signature [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #10644:
URL: https://github.com/apache/datafusion/pull/10644#issuecomment-2132160586
Thanks again @jayzhan211
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Move Median to `functions-aggregate` and Introduce Numeric signature [datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #10644:
URL: https://github.com/apache/datafusion/pull/10644#discussion_r1614640283
##########
datafusion/functions-aggregate/Cargo.toml:
##########
@@ -39,6 +39,7 @@ path = "src/lib.rs"
[dependencies]
arrow = { workspace = true }
+arrow-schema = { workspace = true }
Review Comment:
yes, some macro has inevitable `arrow-schema` dependency https://github.com/apache/arrow-rs/issues/5676.
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Move Median to `functions-aggregate` and Introduce Numeric signature [datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #10644:
URL: https://github.com/apache/datafusion/pull/10644#discussion_r1614957716
##########
datafusion/sqllogictest/test_files/aggregate.slt:
##########
@@ -871,6 +871,33 @@ select median(distinct c) from t;
statement ok
drop table t;
+# optimize distinct median to group by
+statement ok
+create table t(c int) as values (1), (1), (1), (1), (2), (2), (3), (3);
+
+query TT
+explain select median(distinct c) from t;
+----
+logical_plan
+01)Projection: MEDIAN(alias1) AS MEDIAN(DISTINCT t.c)
+02)--Aggregate: groupBy=[[]], aggr=[[MEDIAN(alias1)]]
+03)----Aggregate: groupBy=[[t.c AS alias1]], aggr=[[]]
Review Comment:
This shows distinct is optimzed
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Move Median to `functions-aggregate` and Introduce Numeric signature [datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #10644:
URL: https://github.com/apache/datafusion/pull/10644#discussion_r1613540288
##########
datafusion/functions-aggregate/src/median.rs:
##########
@@ -15,71 +15,105 @@
// specific language governing permissions and limitations
// under the License.
-//! # Median
-
-use crate::aggregate::utils::{down_cast_any_ref, Hashable};
-use crate::expressions::format_state_name;
-use crate::{AggregateExpr, PhysicalExpr};
-use arrow::array::{Array, ArrayRef};
-use arrow::datatypes::{DataType, Field};
-use arrow_array::cast::AsArray;
-use arrow_array::{downcast_integer, ArrowNativeTypeOp, ArrowNumericType};
-use arrow_buffer::ArrowNativeType;
-use datafusion_common::{DataFusionError, Result, ScalarValue};
-use datafusion_expr::Accumulator;
-use std::any::Any;
use std::collections::HashSet;
use std::fmt::Formatter;
-use std::sync::Arc;
+use std::{fmt::Debug, sync::Arc};
+
+use arrow::array::{downcast_integer, ArrowNumericType};
+use arrow::{
+ array::{ArrayRef, AsArray},
+ datatypes::{
+ DataType, Decimal128Type, Decimal256Type, Field, Float16Type, Float32Type,
+ Float64Type,
+ },
+};
+
+use arrow::array::Array;
+use arrow::array::ArrowNativeTypeOp;
+use arrow::datatypes::ArrowNativeType;
+
+use datafusion_common::{DataFusionError, Result, ScalarValue};
+use datafusion_expr::function::StateFieldsArgs;
+use datafusion_expr::{
+ function::AccumulatorArgs, utils::format_state_name, Accumulator, AggregateUDFImpl,
+ Signature, Volatility,
+};
+use datafusion_physical_expr_common::aggregate::utils::Hashable;
+
+make_udaf_expr_and_func!(
+ Median,
+ median,
+ expression,
+ "Computes the median of a set of numbers",
+ median_udaf
+);
-/// MEDIAN aggregate expression. If using the non-distinct variation, then this uses a
-/// lot of memory because all values need to be stored in memory before a result can be
-/// computed. If an approximation is sufficient then APPROX_MEDIAN provides a much more
-/// efficient solution.
-///
-/// If using the distinct variation, the memory usage will be similarly high if the
-/// cardinality is high as it stores all distinct values in memory before computing the
-/// result, but if cardinality is low then memory usage will also be lower.
-#[derive(Debug)]
pub struct Median {
- name: String,
- expr: Arc<dyn PhysicalExpr>,
- data_type: DataType,
- distinct: bool,
+ signature: Signature,
+ aliases: Vec<String>,
+}
+
+impl Debug for Median {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+ f.debug_struct("Median")
+ .field("name", &self.name())
+ .field("signature", &self.signature)
+ .finish()
+ }
+}
+
+impl Default for Median {
+ fn default() -> Self {
+ Self::new()
+ }
}
impl Median {
- /// Create a new MEDIAN aggregate function
- pub fn new(
- expr: Arc<dyn PhysicalExpr>,
- name: impl Into<String>,
- data_type: DataType,
- distinct: bool,
- ) -> Self {
+ pub fn new() -> Self {
Self {
- name: name.into(),
- expr,
- data_type,
- distinct,
+ aliases: vec!["median".to_string()],
+ signature: Signature::numeric(1, Volatility::Immutable),
Review Comment:
I introduce this signature that computes the final coerce types instead of generating tons of valid types with the possible same type. The numeric signature considers decimal types too, which is needed but not included in the Numeric types array.
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Move Median to `functions-aggregate` and Introduce Numeric signature [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #10644:
URL: https://github.com/apache/datafusion/pull/10644#discussion_r1614539590
##########
datafusion/functions-aggregate/src/median.rs:
##########
@@ -15,71 +15,105 @@
// specific language governing permissions and limitations
// under the License.
-//! # Median
-
-use crate::aggregate::utils::{down_cast_any_ref, Hashable};
-use crate::expressions::format_state_name;
-use crate::{AggregateExpr, PhysicalExpr};
-use arrow::array::{Array, ArrayRef};
-use arrow::datatypes::{DataType, Field};
-use arrow_array::cast::AsArray;
-use arrow_array::{downcast_integer, ArrowNativeTypeOp, ArrowNumericType};
-use arrow_buffer::ArrowNativeType;
-use datafusion_common::{DataFusionError, Result, ScalarValue};
-use datafusion_expr::Accumulator;
-use std::any::Any;
use std::collections::HashSet;
use std::fmt::Formatter;
-use std::sync::Arc;
+use std::{fmt::Debug, sync::Arc};
+
+use arrow::array::{downcast_integer, ArrowNumericType};
+use arrow::{
+ array::{ArrayRef, AsArray},
+ datatypes::{
+ DataType, Decimal128Type, Decimal256Type, Field, Float16Type, Float32Type,
+ Float64Type,
+ },
+};
+
+use arrow::array::Array;
+use arrow::array::ArrowNativeTypeOp;
+use arrow::datatypes::ArrowNativeType;
+
+use datafusion_common::{DataFusionError, Result, ScalarValue};
+use datafusion_expr::function::StateFieldsArgs;
+use datafusion_expr::{
+ function::AccumulatorArgs, utils::format_state_name, Accumulator, AggregateUDFImpl,
+ Signature, Volatility,
+};
+use datafusion_physical_expr_common::aggregate::utils::Hashable;
+
+make_udaf_expr_and_func!(
+ Median,
+ median,
+ expression,
+ "Computes the median of a set of numbers",
+ median_udaf
+);
-/// MEDIAN aggregate expression. If using the non-distinct variation, then this uses a
Review Comment:
I think this comment still holds and would be valuable to bring to the new `Median` UDF
Specifically that median uses substantial memory
##########
datafusion/expr/src/signature.rs:
##########
@@ -119,6 +119,8 @@ pub enum TypeSignature {
OneOf(Vec<TypeSignature>),
/// Specifies Signatures for array functions
ArraySignature(ArrayFunctionSignature),
+ /// Fixed number of arguments of numeric types
Review Comment:
Could we please document (or link to documentation) about what types are "numeric"? Is it https://docs.rs/arrow/latest/arrow/datatypes/enum.DataType.html#method.is_numeric ?
##########
datafusion/optimizer/src/single_distinct_to_groupby.rs:
##########
@@ -257,6 +257,56 @@ impl OptimizerRule for SingleDistinctToGroupBy {
)))
}
}
+ Expr::AggregateFunction(AggregateFunction {
Review Comment:
Can we please add a test (if not already done) showing that the distinct aggregate is rewritten? Or perhaps there is already a test for median 🤔
##########
datafusion/functions-aggregate/Cargo.toml:
##########
@@ -39,6 +39,7 @@ path = "src/lib.rs"
[dependencies]
arrow = { workspace = true }
+arrow-schema = { workspace = true }
Review Comment:
is the issue that not re-exported in arrow?
##########
datafusion/optimizer/src/single_distinct_to_groupby.rs:
##########
@@ -257,6 +257,56 @@ impl OptimizerRule for SingleDistinctToGroupBy {
)))
}
}
+ Expr::AggregateFunction(AggregateFunction {
Review Comment:
From what I can tell this is now a general optimization (as in it will rewrite *any* distinct user defined aggregate as well). If so, that is quite cool. Is it correct?
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Move Median to `functions-aggregate` and Introduce Numeric signature [datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #10644:
URL: https://github.com/apache/datafusion/pull/10644#discussion_r1613550658
##########
datafusion/functions-aggregate/Cargo.toml:
##########
@@ -39,6 +39,7 @@ path = "src/lib.rs"
[dependencies]
arrow = { workspace = true }
+arrow-schema = { workspace = true }
Review Comment:
for `downcast_integer`
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org