Skip to content

Apache DataFusion SQL Query Engine

Feature: Improve hash Expr performance (#16977)

Tobias Schwarzinger494851a2bd9d
diff --git a/datafusion/expr/src/udaf.rs b/datafusion/expr/src/udaf.rs
index 15c0dd57ad..984c21d581 100644
--- a/datafusion/expr/src/udaf.rs
+++ b/datafusion/expr/src/udaf.rs
@@ -942,13 +942,12 @@ pub trait AggregateUDFImpl: Debug + Send + Sync {
/// Similarly to [`Hash`] and [`Eq`], if [`Self::equals`] returns true for two UDFs,
/// their `hash_value`s must be the same.
///
- /// By default, it is consistent with default implementation of [`Self::equals`].
+ /// By default, it only hashes the type. The other fields are not hashed, as usually the
+ /// name, signature, and aliases are implied by the UDF type. Recall that UDFs with state
+ /// (and thus possibly changing fields) must override [`Self::equals`] and [`Self::hash_value`].
fn hash_value(&self) -> u64 {
let hasher = &mut DefaultHasher::new();
self.as_any().type_id().hash(hasher);
- self.name().hash(hasher);
- self.aliases().hash(hasher);
- self.signature().hash(hasher);
hasher.finish()
}
diff --git a/datafusion/expr/src/udf.rs b/datafusion/expr/src/udf.rs
index 171c4e041f..c3d09d920c 100644
--- a/datafusion/expr/src/udf.rs
+++ b/datafusion/expr/src/udf.rs
@@ -720,13 +720,12 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
/// Similarly to [`Hash`] and [`Eq`], if [`Self::equals`] returns true for two UDFs,
/// their `hash_value`s must be the same.
///
- /// By default, it is consistent with default implementation of [`Self::equals`].
+ /// By default, it only hashes the type. The other fields are not hashed, as usually the
+ /// name, signature, and aliases are implied by the UDF type. Recall that UDFs with state
+ /// (and thus possibly changing fields) must override [`Self::equals`] and [`Self::hash_value`].
fn hash_value(&self) -> u64 {
let hasher = &mut DefaultHasher::new();
self.as_any().type_id().hash(hasher);
- self.name().hash(hasher);
- self.aliases().hash(hasher);
- self.signature().hash(hasher);
hasher.finish()
}
diff --git a/datafusion/expr/src/udwf.rs b/datafusion/expr/src/udwf.rs
index b19a083bef..1e20c9d4a6 100644
--- a/datafusion/expr/src/udwf.rs
+++ b/datafusion/expr/src/udwf.rs
@@ -385,13 +385,12 @@ pub trait WindowUDFImpl: Debug + Send + Sync {
/// Similarly to [`Hash`] and [`Eq`], if [`Self::equals`] returns true for two UDFs,
/// their `hash_value`s must be the same.
///
- /// By default, it is consistent with default implementation of [`Self::equals`].
+ /// By default, it only hashes the type. The other fields are not hashed, as usually the
+ /// name, signature, and aliases are implied by the UDF type. Recall that UDFs with state
+ /// (and thus possibly changing fields) must override [`Self::equals`] and [`Self::hash_value`].
fn hash_value(&self) -> u64 {
let hasher = &mut DefaultHasher::new();
self.as_any().type_id().hash(hasher);
- self.name().hash(hasher);
- self.aliases().hash(hasher);
- self.signature().hash(hasher);
hasher.finish()
}