-
Notifications
You must be signed in to change notification settings - Fork 829
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
Add downcast macros (#2635) #2636
Conversation
@@ -358,92 +336,12 @@ fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<Array | |||
IterationStrategy::None => Ok(new_empty_array(values.data_type())), | |||
IterationStrategy::All => Ok(make_array(values.data().slice(0, predicate.count))), | |||
// actually filter | |||
_ => match values.data_type() { | |||
_ => downcast_primitive_array! { |
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.
Unfortunately rust doesn't support macros in branch position, and so this was the best I could come up with - rust-lang/rfcs#2654
f161208
to
0fdbfff
Compare
>($values); | ||
$e | ||
} | ||
$crate::datatypes::DataType::Float16 => { |
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 was actually missing before
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.
I think it looks great, personally. Only one small suggestion
/// # use arrow::array::as_string_array; | ||
/// | ||
/// fn print_primitive(array: &dyn Array) { | ||
/// downcast_primitive_array!( |
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.
❤️ that certainly will make the macros in code such as https://github.com/apache/arrow-datafusion/blob/master/datafusion/physical-expr/src/expressions/binary.rs easier to deal with ❤️
/// println!("{:?}", v); | ||
/// } | ||
/// } | ||
/// t => println!("Unsupported datatype {}", t) |
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.
I think it would be good to extend this example to show the String array as well -- not only to document how to support other types, but also because the StringArray is so common
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.
Done, and threw in a bit of TypedDictionaryArray spice as well 😆
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.
🥘 👍
@@ -358,92 +336,12 @@ fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<Array | |||
IterationStrategy::None => Ok(new_empty_array(values.data_type())), | |||
IterationStrategy::All => Ok(make_array(values.data().slice(0, predicate.count))), | |||
// actually filter | |||
_ => match values.data_type() { | |||
_ => downcast_primitive_array! { | |||
values => Ok(Arc::new(filter_primitive(values, predicate))), |
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.
👨🍳 👌
Very nice
Benchmark runs are scheduled for baseline = 2d3a350 and contender = 5a49cfe. 5a49cfe is a master commit associated with this PR. Results will be available as each benchmark for each run completes. |
Which issue does this PR close?
Closes #2635
Rationale for this change
I find myself frequently writing the same code, we can instead generate this with macros. This will also benefit downstreams and acts to reduce the current situation where we have ergonomic but limited dyn-dispatch, or significantly more verbose manual-dispatch but with full flexibility.
What changes are included in this PR?
Adds two macros
downcast_primitive
anddowncast_dict
.Are there any user-facing changes?