Skip to content
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

Merged
merged 3 commits into from
Sep 2, 2022
Merged

Conversation

tustvold
Copy link
Contributor

@tustvold tustvold commented Sep 2, 2022

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 and downcast_dict.

Are there any user-facing changes?

@github-actions github-actions bot added the arrow Changes to the arrow crate label Sep 2, 2022
@@ -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! {
Copy link
Contributor Author

@tustvold tustvold Sep 2, 2022

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

>($values);
$e
}
$crate::datatypes::DataType::Float16 => {
Copy link
Contributor Author

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

Copy link
Contributor

@alamb alamb left a 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!(
Copy link
Contributor

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)
Copy link
Contributor

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

Copy link
Contributor Author

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 😆

Copy link
Contributor

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))),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👨‍🍳 👌

Very nice

@tustvold tustvold merged commit 5a49cfe into apache:master Sep 2, 2022
@ursabot
Copy link

ursabot commented Sep 2, 2022

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.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Macros to assist with static dispatch
3 participants