-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add customizable equality and hash functions to UDFs #11392
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,17 @@ | |
|
||
//! [`AggregateUDF`]: User Defined Aggregate Functions | ||
|
||
use std::any::Any; | ||
use std::fmt::{self, Debug, Formatter}; | ||
use std::hash::{DefaultHasher, Hash, Hasher}; | ||
use std::sync::Arc; | ||
use std::vec; | ||
|
||
use arrow::datatypes::{DataType, Field}; | ||
use sqlparser::ast::NullTreatment; | ||
|
||
use datafusion_common::{exec_err, not_impl_err, plan_err, Result}; | ||
|
||
use crate::expr::AggregateFunction; | ||
use crate::function::{ | ||
AccumulatorArgs, AggregateFunctionSimplification, StateFieldsArgs, | ||
|
@@ -26,13 +37,6 @@ use crate::utils::format_state_name; | |
use crate::utils::AggregateOrderSensitivity; | ||
use crate::{Accumulator, Expr}; | ||
use crate::{AccumulatorFactoryFunction, ReturnTypeFunction, Signature}; | ||
use arrow::datatypes::{DataType, Field}; | ||
use datafusion_common::{exec_err, not_impl_err, plan_err, Result}; | ||
use sqlparser::ast::NullTreatment; | ||
use std::any::Any; | ||
use std::fmt::{self, Debug, Formatter}; | ||
use std::sync::Arc; | ||
use std::vec; | ||
|
||
/// Logical representation of a user-defined [aggregate function] (UDAF). | ||
/// | ||
|
@@ -72,20 +76,19 @@ pub struct AggregateUDF { | |
|
||
impl PartialEq for AggregateUDF { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.name() == other.name() && self.signature() == other.signature() | ||
self.inner.equals(other.inner.as_ref()) | ||
} | ||
} | ||
|
||
impl Eq for AggregateUDF {} | ||
|
||
impl std::hash::Hash for AggregateUDF { | ||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { | ||
self.name().hash(state); | ||
self.signature().hash(state); | ||
impl Hash for AggregateUDF { | ||
fn hash<H: Hasher>(&self, state: &mut H) { | ||
self.inner.hash_value().hash(state) | ||
} | ||
} | ||
|
||
impl std::fmt::Display for AggregateUDF { | ||
impl fmt::Display for AggregateUDF { | ||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
write!(f, "{}", self.name()) | ||
} | ||
|
@@ -280,7 +283,7 @@ where | |
/// #[derive(Debug, Clone)] | ||
/// struct GeoMeanUdf { | ||
/// signature: Signature | ||
/// }; | ||
/// } | ||
/// | ||
/// impl GeoMeanUdf { | ||
/// fn new() -> Self { | ||
|
@@ -507,6 +510,33 @@ pub trait AggregateUDFImpl: Debug + Send + Sync { | |
fn coerce_types(&self, _arg_types: &[DataType]) -> Result<Vec<DataType>> { | ||
not_impl_err!("Function {} does not implement coerce_types", self.name()) | ||
} | ||
|
||
/// Return true if this aggregate UDF is equal to the other. | ||
/// | ||
/// Allows customizing the equality of aggregate UDFs. | ||
/// Must be consistent with [`Self::hash_value`] and follow the same rules as [`Eq`]: | ||
/// | ||
/// - reflexive: `a.equals(a)`; | ||
/// - symmetric: `a.equals(b)` implies `b.equals(a)`; | ||
/// - transitive: `a.equals(b)` and `b.equals(c)` implies `a.equals(c)`. | ||
/// | ||
/// By default, compares [`Self::name`] and [`Self::signature`]. | ||
fn equals(&self, other: &dyn AggregateUDFImpl) -> bool { | ||
self.name() == other.name() && self.signature() == other.signature() | ||
} | ||
|
||
/// Returns a hash value for this aggregate UDF. | ||
/// | ||
/// Allows customizing the hash code of aggregate UDFs. Similarly to [`Hash`] and [`Eq`], | ||
/// if [`Self::equals`] returns true for two UDFs, their `hash_value`s must be the same. | ||
/// | ||
/// By default, hashes [`Self::name`] and [`Self::signature`]. | ||
fn hash_value(&self) -> u64 { | ||
let hasher = &mut DefaultHasher::new(); | ||
self.name().hash(hasher); | ||
self.signature().hash(hasher); | ||
hasher.finish() | ||
} | ||
} | ||
|
||
pub enum ReversedUDAF { | ||
|
@@ -562,6 +592,21 @@ impl AggregateUDFImpl for AliasedAggregateUDFImpl { | |
fn aliases(&self) -> &[String] { | ||
&self.aliases | ||
} | ||
|
||
fn equals(&self, other: &dyn AggregateUDFImpl) -> bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this makes sense to me as the name and signature are the same as the inner |
||
if let Some(other) = other.as_any().downcast_ref::<AliasedAggregateUDFImpl>() { | ||
self.inner.equals(other.inner.as_ref()) && self.aliases == other.aliases | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
fn hash_value(&self) -> u64 { | ||
let hasher = &mut DefaultHasher::new(); | ||
self.inner.hash_value().hash(hasher); | ||
self.aliases.hash(hasher); | ||
hasher.finish() | ||
} | ||
} | ||
|
||
/// Implementation of [`AggregateUDFImpl`] that wraps the function style pointers | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without the changes in this PR are the expressions combined by CSE or something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This particular case is deduplicated in
PushDownFilter
: