-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
perf: Fast decision for Parquet dictionary encoding #19256
Merged
ritchie46
merged 4 commits into
pola-rs:main
from
coastalwhite:perf/pq-write-dict-decide
Oct 16, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
@@ -0,0 +1,159 @@ | ||
use arrow::array::{ | ||
Array, BinaryArray, BinaryViewArray, BooleanArray, FixedSizeBinaryArray, PrimitiveArray, | ||
Utf8Array, Utf8ViewArray, | ||
}; | ||
use arrow::datatypes::PhysicalType; | ||
use arrow::types::Offset; | ||
use arrow::with_match_primitive_type_full; | ||
use polars_utils::total_ord::ToTotalOrd; | ||
|
||
use crate::hyperloglogplus::HyperLogLog; | ||
|
||
/// Get an estimate for the *cardinality* of the array (i.e. the number of unique values) | ||
/// | ||
/// This is not currently implemented for nested types. | ||
pub fn estimate_cardinality(array: &dyn Array) -> usize { | ||
if array.is_empty() { | ||
return 0; | ||
} | ||
|
||
if array.null_count() == array.len() { | ||
return 1; | ||
} | ||
|
||
// Estimate the cardinality with HyperLogLog | ||
use PhysicalType as PT; | ||
match array.dtype().to_physical_type() { | ||
PT::Null => 1, | ||
|
||
PT::Boolean => { | ||
let mut cardinality = 0; | ||
|
||
let array = array.as_any().downcast_ref::<BooleanArray>().unwrap(); | ||
|
||
cardinality += usize::from(array.has_nulls()); | ||
|
||
if let Some(unset_bits) = array.values().lazy_unset_bits() { | ||
cardinality += 1 + usize::from(unset_bits != array.len()); | ||
} else { | ||
cardinality += 2; | ||
} | ||
|
||
cardinality | ||
}, | ||
|
||
PT::Primitive(primitive_type) => with_match_primitive_type_full!(primitive_type, |$T| { | ||
let mut hll = HyperLogLog::new(); | ||
|
||
let array = array | ||
.as_any() | ||
.downcast_ref::<PrimitiveArray<$T>>() | ||
.unwrap(); | ||
|
||
if array.has_nulls() { | ||
for v in array.iter() { | ||
let v = v.copied().unwrap_or_default(); | ||
hll.add(&v.to_total_ord()); | ||
} | ||
} else { | ||
for v in array.values_iter() { | ||
hll.add(&v.to_total_ord()); | ||
} | ||
} | ||
|
||
hll.count() | ||
}), | ||
PT::FixedSizeBinary => { | ||
let mut hll = HyperLogLog::new(); | ||
|
||
let array = array | ||
.as_any() | ||
.downcast_ref::<FixedSizeBinaryArray>() | ||
.unwrap(); | ||
|
||
if array.has_nulls() { | ||
for v in array.iter() { | ||
let v = v.unwrap_or_default(); | ||
hll.add(v); | ||
} | ||
} else { | ||
for v in array.values_iter() { | ||
hll.add(v); | ||
} | ||
} | ||
|
||
hll.count() | ||
}, | ||
PT::Binary => { | ||
binary_offset_array_estimate(array.as_any().downcast_ref::<BinaryArray<i32>>().unwrap()) | ||
}, | ||
PT::LargeBinary => { | ||
binary_offset_array_estimate(array.as_any().downcast_ref::<BinaryArray<i64>>().unwrap()) | ||
}, | ||
PT::Utf8 => binary_offset_array_estimate( | ||
&array | ||
.as_any() | ||
.downcast_ref::<Utf8Array<i32>>() | ||
.unwrap() | ||
.to_binary(), | ||
), | ||
PT::LargeUtf8 => binary_offset_array_estimate( | ||
&array | ||
.as_any() | ||
.downcast_ref::<Utf8Array<i64>>() | ||
.unwrap() | ||
.to_binary(), | ||
), | ||
PT::BinaryView => { | ||
binary_view_array_estimate(array.as_any().downcast_ref::<BinaryViewArray>().unwrap()) | ||
}, | ||
PT::Utf8View => binary_view_array_estimate( | ||
&array | ||
.as_any() | ||
.downcast_ref::<Utf8ViewArray>() | ||
.unwrap() | ||
.to_binview(), | ||
), | ||
PT::List => unimplemented!(), | ||
PT::FixedSizeList => unimplemented!(), | ||
PT::LargeList => unimplemented!(), | ||
PT::Struct => unimplemented!(), | ||
PT::Union => unimplemented!(), | ||
PT::Map => unimplemented!(), | ||
PT::Dictionary(_) => unimplemented!(), | ||
} | ||
} | ||
|
||
fn binary_offset_array_estimate<O: Offset>(array: &BinaryArray<O>) -> usize { | ||
let mut hll = HyperLogLog::new(); | ||
|
||
if array.has_nulls() { | ||
for v in array.iter() { | ||
let v = v.unwrap_or_default(); | ||
hll.add(v); | ||
} | ||
} else { | ||
for v in array.values_iter() { | ||
hll.add(v); | ||
} | ||
} | ||
|
||
hll.count() | ||
} | ||
|
||
fn binary_view_array_estimate(array: &BinaryViewArray) -> usize { | ||
let mut hll = HyperLogLog::new(); | ||
|
||
if array.has_nulls() { | ||
for v in array.iter() { | ||
let v = v.unwrap_or_default(); | ||
hll.add(v); | ||
} | ||
} else { | ||
for v in array.values_iter() { | ||
hll.add(v); | ||
} | ||
} | ||
|
||
hll.count() | ||
} |
This file contains 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 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
Oops, something went wrong.
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.
question: Not sure how big these arrays are, but if they are full column length, does it make sense to partition the work of computing the cardinality across rows, producing a partial estimate in each thread and then using
HyperLogLog::merge
to reduce across threads?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.
These arrays are equally large as a row group in parquet.
I don't really see much benefit in parallelizing here, because we already parallelize over columns and row groups. So unless you have a short dataframe with few columns, you probably won't see any sleeping threads.
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.
Ah, sure.