-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Implement special min/max accumulator for Strings and Binary (10% faster for Clickbench Q28) #12792
Changes from all commits
8bc00f8
19b3297
4b3e625
64e9861
c4f6271
c3145cd
126a9a8
a7ebb56
2d5957d
2671b2d
a43288e
043ac35
e454838
8618673
c7aa11f
8e876bf
1389fb7
558e1ba
7853d8f
81431a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,22 @@ | |
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
//! [`set_nulls`], and [`filtered_null_mask`], utilities for working with nulls | ||
//! [`set_nulls`], other utilities for working with nulls | ||
|
||
use arrow::array::{Array, ArrowNumericType, BooleanArray, PrimitiveArray}; | ||
use arrow::array::{ | ||
Array, ArrayRef, ArrowNumericType, AsArray, BinaryArray, BinaryViewArray, | ||
BooleanArray, LargeBinaryArray, LargeStringArray, PrimitiveArray, StringArray, | ||
StringViewArray, | ||
}; | ||
use arrow::buffer::NullBuffer; | ||
use arrow::datatypes::DataType; | ||
use datafusion_common::{not_impl_err, Result}; | ||
use std::sync::Arc; | ||
|
||
/// Sets the validity mask for a `PrimitiveArray` to `nulls` | ||
/// replacing any existing null mask | ||
/// | ||
/// See [`set_nulls_dyn`] for a version that works with `Array` | ||
pub fn set_nulls<T: ArrowNumericType + Send>( | ||
array: PrimitiveArray<T>, | ||
nulls: Option<NullBuffer>, | ||
|
@@ -91,3 +100,105 @@ pub fn filtered_null_mask( | |
let opt_filter = opt_filter.and_then(filter_to_nulls); | ||
NullBuffer::union(opt_filter.as_ref(), input.nulls()) | ||
} | ||
|
||
/// Applies optional filter to input, returning a new array of the same type | ||
/// with the same data, but with any values that were filtered out set to null | ||
pub fn apply_filter_as_nulls( | ||
input: &dyn Array, | ||
opt_filter: Option<&BooleanArray>, | ||
) -> Result<ArrayRef> { | ||
let nulls = filtered_null_mask(opt_filter, input); | ||
set_nulls_dyn(input, nulls) | ||
} | ||
|
||
/// Replaces the nulls in the input array with the given `NullBuffer` | ||
/// | ||
/// TODO: replace when upstreamed in arrow-rs: <https://github.com/apache/arrow-rs/issues/6528> | ||
pub fn set_nulls_dyn(input: &dyn Array, nulls: Option<NullBuffer>) -> Result<ArrayRef> { | ||
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. nit: this could be private |
||
if let Some(nulls) = nulls.as_ref() { | ||
assert_eq!(nulls.len(), input.len()); | ||
} | ||
|
||
let output: ArrayRef = match input.data_type() { | ||
DataType::Utf8 => { | ||
let input = input.as_string::<i32>(); | ||
// safety: values / offsets came from a valid string array, so are valid utf8 | ||
// and we checked nulls has the same length as values | ||
unsafe { | ||
Arc::new(StringArray::new_unchecked( | ||
input.offsets().clone(), | ||
input.values().clone(), | ||
nulls, | ||
)) | ||
} | ||
} | ||
DataType::LargeUtf8 => { | ||
let input = input.as_string::<i64>(); | ||
// safety: values / offsets came from a valid string array, so are valid utf8 | ||
// and we checked nulls has the same length as values | ||
unsafe { | ||
Arc::new(LargeStringArray::new_unchecked( | ||
input.offsets().clone(), | ||
input.values().clone(), | ||
nulls, | ||
)) | ||
} | ||
} | ||
DataType::Utf8View => { | ||
let input = input.as_string_view(); | ||
// safety: values / views came from a valid string view array, so are valid utf8 | ||
// and we checked nulls has the same length as values | ||
unsafe { | ||
Arc::new(StringViewArray::new_unchecked( | ||
input.views().clone(), | ||
input.data_buffers().to_vec(), | ||
nulls, | ||
)) | ||
} | ||
} | ||
|
||
DataType::Binary => { | ||
let input = input.as_binary::<i32>(); | ||
// safety: values / offsets came from a valid binary array | ||
// and we checked nulls has the same length as values | ||
unsafe { | ||
Arc::new(BinaryArray::new_unchecked( | ||
input.offsets().clone(), | ||
input.values().clone(), | ||
nulls, | ||
)) | ||
} | ||
} | ||
DataType::LargeBinary => { | ||
let input = input.as_binary::<i64>(); | ||
// safety: values / offsets came from a valid large binary array | ||
// and we checked nulls has the same length as values | ||
unsafe { | ||
Arc::new(LargeBinaryArray::new_unchecked( | ||
input.offsets().clone(), | ||
input.values().clone(), | ||
nulls, | ||
)) | ||
} | ||
} | ||
DataType::BinaryView => { | ||
let input = input.as_binary_view(); | ||
// safety: values / views came from a valid binary view array | ||
// and we checked nulls has the same length as values | ||
unsafe { | ||
Arc::new(BinaryViewArray::new_unchecked( | ||
input.views().clone(), | ||
input.data_buffers().to_vec(), | ||
nulls, | ||
)) | ||
} | ||
} | ||
_ => { | ||
return not_impl_err!("Applying nulls {:?}", input.data_type()); | ||
Comment on lines
+196
to
+197
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. Do we have to support this for any other data types? 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. not necessairly -- I am hoping we put this code upstream in arrow-rs and can remove it entirely from datafusion eventually |
||
} | ||
}; | ||
assert_eq!(input.len(), output.len()); | ||
assert_eq!(input.data_type(), output.data_type()); | ||
|
||
Ok(output) | ||
} |
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 is supporting code for replacing the null buffers in arrays