Skip to content

Apache DataFusion SQL Query Engine

ARROW-12123: [Rust][DataFusion] Use smallvec for indices for better join performance

Heres, Danielaff5e358c2ba
diff --git a/rust/datafusion/Cargo.toml b/rust/datafusion/Cargo.toml
index 3236a97124..f78550e77c 100644
--- a/rust/datafusion/Cargo.toml
+++ b/rust/datafusion/Cargo.toml
@@ -70,6 +70,7 @@ ordered-float = "2.0"
unicode-segmentation = { version = "^1.7.1", optional = true }
regex = { version = "^1.4.3", optional = true }
lazy_static = { version = "^1.4.0", optional = true }
+smallvec = { version = "1.6", features = ["union"] }
[dev-dependencies]
rand = "0.8"
diff --git a/rust/datafusion/src/physical_plan/hash_join.rs b/rust/datafusion/src/physical_plan/hash_join.rs
index 7ca769a530..964eb13fd0 100644
--- a/rust/datafusion/src/physical_plan/hash_join.rs
+++ b/rust/datafusion/src/physical_plan/hash_join.rs
@@ -30,6 +30,7 @@ use arrow::{
compute,
datatypes::{TimeUnit, UInt32Type, UInt64Type},
};
+use smallvec::{smallvec, SmallVec};
use std::time::Instant;
use std::{any::Any, collections::HashSet};
use std::{hash::Hasher, sync::Arc};
@@ -64,7 +65,7 @@ use log::debug;
// Maps a `u64` hash value based on the left ["on" values] to a list of indices with this key's value.
// E.g. 1 -> [3, 6, 8] indicates that the column values map to rows 3, 6 and 8 for hash value 1
// As the key is a hash value, we need to check possible hash collisions in the probe stage
-type JoinHashMap = HashMap<u64, Vec<u64>, IdHashBuilder>;
+type JoinHashMap = HashMap<u64, SmallVec<[u64; 1]>, IdHashBuilder>;
type JoinLeftData = Arc<(JoinHashMap, RecordBatch)>;
/// join execution plan executes partitions in parallel and combines them into a set of
@@ -335,7 +336,7 @@ fn update_hash(
hash.raw_entry_mut()
.from_key_hashed_nocheck(*hash_value, hash_value)
.and_modify(|_, v| v.push((row + offset) as u64))
- .or_insert_with(|| (*hash_value, vec![(row + offset) as u64]));
+ .or_insert_with(|| (*hash_value, smallvec![(row + offset) as u64]));
}
Ok(())
}
@@ -1178,8 +1179,8 @@ mod tests {
create_hashes(&[left.columns()[0].clone()], &random_state, hashes_buff)?;
// Create hash collisions
- hashmap_left.insert(hashes[0], vec![0, 1]);
- hashmap_left.insert(hashes[1], vec![0, 1]);
+ hashmap_left.insert(hashes[0], smallvec![0, 1]);
+ hashmap_left.insert(hashes[1], smallvec![0, 1]);
let right = build_table_i32(
("a", &vec![10, 20]),