Skip to content

Commit

Permalink
Convert bool_and & bool_or to UDAF (#11009)
Browse files Browse the repository at this point in the history
* Port `bool_and` and `bool_or` to `AggregateUDFImpl`

* Remove trait methods with default implementation

* Add `bool_or_udaf`

* Register `bool_and` and `bool_or`

* Remove from `physical-expr`

* Add expressions to logical plan roundtrip test

* minor: remove methods with default implementation

* Removes redundant tests

* Removes hard-coded function names
  • Loading branch information
jcsherin authored Jun 20, 2024
1 parent 5bfc11b commit 89def2c
Show file tree
Hide file tree
Showing 16 changed files with 362 additions and 515 deletions.
15 changes: 0 additions & 15 deletions datafusion/expr/src/aggregate_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ pub enum AggregateFunction {
Correlation,
/// Grouping
Grouping,
/// Bool And
BoolAnd,
/// Bool Or
BoolOr,
}

impl AggregateFunction {
Expand All @@ -64,8 +60,6 @@ impl AggregateFunction {
NthValue => "NTH_VALUE",
Correlation => "CORR",
Grouping => "GROUPING",
BoolAnd => "BOOL_AND",
BoolOr => "BOOL_OR",
}
}
}
Expand All @@ -82,8 +76,6 @@ impl FromStr for AggregateFunction {
Ok(match name {
// general
"avg" => AggregateFunction::Avg,
"bool_and" => AggregateFunction::BoolAnd,
"bool_or" => AggregateFunction::BoolOr,
"max" => AggregateFunction::Max,
"mean" => AggregateFunction::Avg,
"min" => AggregateFunction::Min,
Expand Down Expand Up @@ -128,9 +120,6 @@ impl AggregateFunction {
// The coerced_data_types is same with input_types.
Ok(coerced_data_types[0].clone())
}
AggregateFunction::BoolAnd | AggregateFunction::BoolOr => {
Ok(DataType::Boolean)
}
AggregateFunction::Correlation => {
correlation_return_type(&coerced_data_types[0])
}
Expand Down Expand Up @@ -179,10 +168,6 @@ impl AggregateFunction {
.collect::<Vec<_>>();
Signature::uniform(1, valid, Volatility::Immutable)
}
AggregateFunction::BoolAnd | AggregateFunction::BoolOr => {
Signature::uniform(1, vec![DataType::Boolean], Volatility::Immutable)
}

AggregateFunction::Avg => {
Signature::uniform(1, NUMERICS.to_vec(), Volatility::Immutable)
}
Expand Down
16 changes: 0 additions & 16 deletions datafusion/expr/src/type_coercion/aggregates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,6 @@ pub fn coerce_types(
};
Ok(vec![v])
}
AggregateFunction::BoolAnd | AggregateFunction::BoolOr => {
// Refer to https://www.postgresql.org/docs/8.2/functions-aggregate.html doc
// smallint, int, bigint, real, double precision, decimal, or interval.
if !is_bool_and_or_support_arg_type(&input_types[0]) {
return plan_err!(
"The function {:?} does not support inputs of type {:?}.",
agg_fun,
input_types[0]
);
}
Ok(input_types.to_vec())
}
AggregateFunction::Correlation => {
if !is_correlation_support_arg_type(&input_types[0]) {
return plan_err!(
Expand Down Expand Up @@ -319,10 +307,6 @@ pub fn avg_sum_type(arg_type: &DataType) -> Result<DataType> {
}
}

pub fn is_bool_and_or_support_arg_type(arg_type: &DataType) -> bool {
matches!(arg_type, DataType::Boolean)
}

pub fn is_sum_support_arg_type(arg_type: &DataType) -> bool {
match arg_type {
DataType::Dictionary(_, dict_value_type) => {
Expand Down
Loading

0 comments on commit 89def2c

Please sign in to comment.