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

Casting generic binary to generic string #3607

Merged
merged 3 commits into from
Jan 28, 2023
Merged

Conversation

viirya
Copy link
Member

@viirya viirya commented Jan 26, 2023

Which issue does this PR close?

Closes #3606.

Rationale for this change

What changes are included in this PR?

Are there any user-facing changes?

@github-actions github-actions bot added the arrow Changes to the arrow crate label Jan 26, 2023
Copy link
Contributor

@tustvold tustvold left a comment

Choose a reason for hiding this comment

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

I wonder if we could do this in two passes

  • Convert offsets
  • Convert to string

This will be significantly faster than the approach here as it doesn't copy any string data and performs UTF-8 validation in a single pass (not to mention less code)

.iter()
.map(|maybe_value| match maybe_value {
Some(value) => {
let result = std::str::from_utf8(value);
Copy link
Contributor

Choose a reason for hiding this comment

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

The performance of this will be pretty poor, it would be better to cast the offsets and then use the optimised routine for UTF-8 validation

Copy link
Member Author

@viirya viirya Jan 27, 2023

Choose a reason for hiding this comment

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

This will be significantly faster than the approach here as it doesn't copy any string data and performs UTF-8 validation in a single pass (not to mention less code)

This can only be applied if CastOptions.safe as false. If CastOptions.safe as true, we still need to iterate and validate each value (because it will be null instead returning Err directly).

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I keep forgetting about that option, it seems to be some thing we keep for legacy compatibility

Copy link
Contributor

@tustvold tustvold left a comment

Choose a reason for hiding this comment

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

Looks good to me, I still wonder if there is some way to avoid re-encoding the offsets when not necessary - e.g. the common case of Binary to Utf8. Perhaps by separating offset conversion from value conversion, possibly something for a future PR

arrow-cast/src/cast.rs Show resolved Hide resolved
let offsets = array.value_offsets();
let values = array.value_data();

// We only need to validate that all values are valid UTF-8
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems a shame to duplicate this logic, but I guess it is hard to avoid whilst having this type signature

arrow-cast/src/cast.rs Outdated Show resolved Hide resolved
@viirya viirya merged commit 8cc8327 into apache:master Jan 28, 2023
@ursabot
Copy link

ursabot commented Jan 28, 2023

Benchmark runs are scheduled for baseline = eeecbe5 and contender = 8cc8327. 8cc8327 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.

Casting generic binary to generic string
3 participants