You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "tustvold (via GitHub)" <gi...@apache.org> on 2023/07/28 11:43:44 UTC
[GitHub] [arrow-rs] tustvold opened a new pull request, #4579: Cleanup take_bytes (#4576)
tustvold opened a new pull request, #4579:
URL: https://github.com/apache/arrow-rs/pull/4579
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #4576
# Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
This fixes the overflow and null checking behaviour of take_bytes. This does regress the performance slightly, but I think this is acceptable given the prior logic was incorrect.
```
take str 512 time: [3.8135 µs 3.8144 µs 3.8155 µs]
change: [+8.1351% +8.5262% +8.8652%] (p = 0.00 < 0.05)
Performance has regressed.
Found 10 outliers among 100 measurements (10.00%)
2 (2.00%) low mild
6 (6.00%) high mild
2 (2.00%) high severe
take str 1024 time: [6.6656 µs 6.6665 µs 6.6675 µs]
change: [+10.429% +10.776% +10.972%] (p = 0.00 < 0.05)
Performance has regressed.
Found 7 outliers among 100 measurements (7.00%)
6 (6.00%) high mild
1 (1.00%) high severe
take str null indices 512
time: [3.2013 µs 3.2048 µs 3.2081 µs]
change: [+6.4883% +6.7476% +7.0323%] (p = 0.00 < 0.05)
Performance has regressed.
Found 3 outliers among 100 measurements (3.00%)
2 (2.00%) high mild
1 (1.00%) high severe
take str null indices 1024
time: [6.3864 µs 6.4001 µs 6.4149 µs]
change: [+8.6045% +9.0105% +9.4405%] (p = 0.00 < 0.05)
Performance has regressed.
Found 4 outliers among 100 measurements (4.00%)
3 (3.00%) high mild
1 (1.00%) high severe
take str null values 1024
time: [6.9174 µs 6.9204 µs 6.9236 µs]
change: [-3.4004% -3.0300% -2.7115%] (p = 0.00 < 0.05)
Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) high severe
take str null values null indices 1024
time: [5.5812 µs 5.5842 µs 5.5873 µs]
change: [+2.1866% +2.5807% +2.9030%] (p = 0.00 < 0.05)
Performance has regressed.
Found 9 outliers among 100 measurements (9.00%)
6 (6.00%) high mild
3 (3.00%) high severe
```
# What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
# Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!---
If there are any breaking changes to public APIs, please add the `breaking change` label.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold commented on a diff in pull request #4579: Cleanup take_bytes (#4576)
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #4579:
URL: https://github.com/apache/arrow-rs/pull/4579#discussion_r1277444354
##########
arrow-select/src/take.rs:
##########
@@ -331,94 +331,68 @@ fn take_bytes<T: ByteArrayType, IndexType: ArrowPrimitiveType>(
let data_len = indices.len();
let bytes_offset = (data_len + 1) * std::mem::size_of::<T::Offset>();
- let mut offsets_buffer = MutableBuffer::from_len_zeroed(bytes_offset);
+ let mut offsets = MutableBuffer::new(bytes_offset);
+ offsets.push(T::Offset::default());
- let offsets = offsets_buffer.typed_data_mut();
let mut values = MutableBuffer::new(0);
- let mut length_so_far = T::Offset::from_usize(0).unwrap();
- offsets[0] = length_so_far;
let nulls;
if array.null_count() == 0 && indices.null_count() == 0 {
- for (i, offset) in offsets.iter_mut().skip(1).enumerate() {
- let index = indices.value(i).to_usize().ok_or_else(|| {
- ArrowError::ComputeError("Cast to usize failed".to_string())
- })?;
-
- let s = array.value(index);
-
- let s: &[u8] = s.as_ref();
- length_so_far += T::Offset::from_usize(s.len()).unwrap();
+ offsets.extend(indices.values().iter().map(|index| {
+ let s: &[u8] = array.value(index.as_usize()).as_ref();
values.extend_from_slice(s);
- *offset = length_so_far;
- }
+ T::Offset::usize_as(values.len())
+ }));
nulls = None
} else if indices.null_count() == 0 {
let num_bytes = bit_util::ceil(data_len, 8);
let mut null_buf = MutableBuffer::new(num_bytes).with_bitset(num_bytes, true);
let null_slice = null_buf.as_slice_mut();
-
- for (i, offset) in offsets.iter_mut().skip(1).enumerate() {
- let index = indices.value(i).to_usize().ok_or_else(|| {
- ArrowError::ComputeError("Cast to usize failed".to_string())
- })?;
-
+ offsets.extend(indices.values().iter().enumerate().map(|(i, index)| {
+ let index = index.as_usize();
if array.is_valid(index) {
let s: &[u8] = array.value(index).as_ref();
-
- length_so_far += T::Offset::from_usize(s.len()).unwrap();
values.extend_from_slice(s.as_ref());
Review Comment:
We piggy back on the fact this already does checked arithmetic
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold commented on a diff in pull request #4579: Cleanup take_bytes (#4576)
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #4579:
URL: https://github.com/apache/arrow-rs/pull/4579#discussion_r1277443850
##########
arrow-select/src/take.rs:
##########
@@ -331,94 +331,68 @@ fn take_bytes<T: ByteArrayType, IndexType: ArrowPrimitiveType>(
let data_len = indices.len();
let bytes_offset = (data_len + 1) * std::mem::size_of::<T::Offset>();
- let mut offsets_buffer = MutableBuffer::from_len_zeroed(bytes_offset);
+ let mut offsets = MutableBuffer::new(bytes_offset);
+ offsets.push(T::Offset::default());
- let offsets = offsets_buffer.typed_data_mut();
let mut values = MutableBuffer::new(0);
- let mut length_so_far = T::Offset::from_usize(0).unwrap();
- offsets[0] = length_so_far;
let nulls;
if array.null_count() == 0 && indices.null_count() == 0 {
- for (i, offset) in offsets.iter_mut().skip(1).enumerate() {
- let index = indices.value(i).to_usize().ok_or_else(|| {
- ArrowError::ComputeError("Cast to usize failed".to_string())
- })?;
-
- let s = array.value(index);
-
- let s: &[u8] = s.as_ref();
- length_so_far += T::Offset::from_usize(s.len()).unwrap();
+ offsets.extend(indices.values().iter().map(|index| {
+ let s: &[u8] = array.value(index.as_usize()).as_ref();
values.extend_from_slice(s);
- *offset = length_so_far;
- }
+ T::Offset::usize_as(values.len())
+ }));
nulls = None
} else if indices.null_count() == 0 {
let num_bytes = bit_util::ceil(data_len, 8);
let mut null_buf = MutableBuffer::new(num_bytes).with_bitset(num_bytes, true);
let null_slice = null_buf.as_slice_mut();
-
- for (i, offset) in offsets.iter_mut().skip(1).enumerate() {
- let index = indices.value(i).to_usize().ok_or_else(|| {
- ArrowError::ComputeError("Cast to usize failed".to_string())
- })?;
-
+ offsets.extend(indices.values().iter().enumerate().map(|(i, index)| {
+ let index = index.as_usize();
if array.is_valid(index) {
let s: &[u8] = array.value(index).as_ref();
-
- length_so_far += T::Offset::from_usize(s.len()).unwrap();
Review Comment:
This wasn't performing checked arithmetic and so could silently overflow
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] alamb commented on a diff in pull request #4579: Fix take_bytes Null and Overflow Handling (#4576)
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #4579:
URL: https://github.com/apache/arrow-rs/pull/4579#discussion_r1277727180
##########
arrow-select/src/take.rs:
##########
@@ -331,94 +331,68 @@ fn take_bytes<T: ByteArrayType, IndexType: ArrowPrimitiveType>(
let data_len = indices.len();
let bytes_offset = (data_len + 1) * std::mem::size_of::<T::Offset>();
- let mut offsets_buffer = MutableBuffer::from_len_zeroed(bytes_offset);
+ let mut offsets = MutableBuffer::new(bytes_offset);
+ offsets.push(T::Offset::default());
- let offsets = offsets_buffer.typed_data_mut();
let mut values = MutableBuffer::new(0);
- let mut length_so_far = T::Offset::from_usize(0).unwrap();
- offsets[0] = length_so_far;
let nulls;
if array.null_count() == 0 && indices.null_count() == 0 {
- for (i, offset) in offsets.iter_mut().skip(1).enumerate() {
- let index = indices.value(i).to_usize().ok_or_else(|| {
- ArrowError::ComputeError("Cast to usize failed".to_string())
- })?;
-
- let s = array.value(index);
-
- let s: &[u8] = s.as_ref();
- length_so_far += T::Offset::from_usize(s.len()).unwrap();
+ offsets.extend(indices.values().iter().map(|index| {
+ let s: &[u8] = array.value(index.as_usize()).as_ref();
values.extend_from_slice(s);
- *offset = length_so_far;
- }
+ T::Offset::usize_as(values.len())
+ }));
nulls = None
} else if indices.null_count() == 0 {
let num_bytes = bit_util::ceil(data_len, 8);
let mut null_buf = MutableBuffer::new(num_bytes).with_bitset(num_bytes, true);
let null_slice = null_buf.as_slice_mut();
-
- for (i, offset) in offsets.iter_mut().skip(1).enumerate() {
- let index = indices.value(i).to_usize().ok_or_else(|| {
- ArrowError::ComputeError("Cast to usize failed".to_string())
- })?;
-
+ offsets.extend(indices.values().iter().enumerate().map(|(i, index)| {
+ let index = index.as_usize();
if array.is_valid(index) {
let s: &[u8] = array.value(index).as_ref();
-
- length_so_far += T::Offset::from_usize(s.len()).unwrap();
values.extend_from_slice(s.as_ref());
} else {
bit_util::unset_bit(null_slice, i);
}
- *offset = length_so_far;
- }
+ T::Offset::usize_as(values.len())
+ }));
nulls = Some(null_buf.into());
} else if array.null_count() == 0 {
- for (i, offset) in offsets.iter_mut().skip(1).enumerate() {
+ offsets.extend(indices.values().iter().enumerate().map(|(i, index)| {
if indices.is_valid(i) {
Review Comment:
```suggestion
// check index is valid before using index. The value in
// NULL index slots may not be within bounds of array
if indices.is_valid(i) {
```
##########
arrow-select/src/take.rs:
##########
@@ -1949,4 +1923,16 @@ mod tests {
.collect::<Vec<_>>();
assert_eq!(&values, &[Some(23), Some(4), None, None])
}
+
+ #[test]
+ fn test_take_bytes_null_indices() {
+ let indices = Int32Array::new(
Review Comment:
```suggestion
// Build indices with values that out of bounds, but NULL so they
// will not be checked
let indices = Int32Array::new(
```
##########
arrow-select/src/take.rs:
##########
@@ -331,94 +331,68 @@ fn take_bytes<T: ByteArrayType, IndexType: ArrowPrimitiveType>(
let data_len = indices.len();
let bytes_offset = (data_len + 1) * std::mem::size_of::<T::Offset>();
- let mut offsets_buffer = MutableBuffer::from_len_zeroed(bytes_offset);
+ let mut offsets = MutableBuffer::new(bytes_offset);
+ offsets.push(T::Offset::default());
- let offsets = offsets_buffer.typed_data_mut();
let mut values = MutableBuffer::new(0);
- let mut length_so_far = T::Offset::from_usize(0).unwrap();
- offsets[0] = length_so_far;
let nulls;
if array.null_count() == 0 && indices.null_count() == 0 {
- for (i, offset) in offsets.iter_mut().skip(1).enumerate() {
- let index = indices.value(i).to_usize().ok_or_else(|| {
- ArrowError::ComputeError("Cast to usize failed".to_string())
- })?;
-
- let s = array.value(index);
-
- let s: &[u8] = s.as_ref();
- length_so_far += T::Offset::from_usize(s.len()).unwrap();
+ offsets.extend(indices.values().iter().map(|index| {
+ let s: &[u8] = array.value(index.as_usize()).as_ref();
values.extend_from_slice(s);
- *offset = length_so_far;
- }
+ T::Offset::usize_as(values.len())
+ }));
nulls = None
} else if indices.null_count() == 0 {
let num_bytes = bit_util::ceil(data_len, 8);
let mut null_buf = MutableBuffer::new(num_bytes).with_bitset(num_bytes, true);
let null_slice = null_buf.as_slice_mut();
-
- for (i, offset) in offsets.iter_mut().skip(1).enumerate() {
- let index = indices.value(i).to_usize().ok_or_else(|| {
- ArrowError::ComputeError("Cast to usize failed".to_string())
- })?;
-
+ offsets.extend(indices.values().iter().enumerate().map(|(i, index)| {
+ let index = index.as_usize();
if array.is_valid(index) {
let s: &[u8] = array.value(index).as_ref();
-
- length_so_far += T::Offset::from_usize(s.len()).unwrap();
values.extend_from_slice(s.as_ref());
} else {
bit_util::unset_bit(null_slice, i);
}
- *offset = length_so_far;
- }
+ T::Offset::usize_as(values.len())
+ }));
nulls = Some(null_buf.into());
} else if array.null_count() == 0 {
- for (i, offset) in offsets.iter_mut().skip(1).enumerate() {
+ offsets.extend(indices.values().iter().enumerate().map(|(i, index)| {
if indices.is_valid(i) {
- let index = indices.value(i).to_usize().ok_or_else(|| {
- ArrowError::ComputeError("Cast to usize failed".to_string())
- })?;
-
- let s: &[u8] = array.value(index).as_ref();
-
- length_so_far += T::Offset::from_usize(s.len()).unwrap();
+ let s: &[u8] = array.value(index.as_usize()).as_ref();
values.extend_from_slice(s);
}
- *offset = length_so_far;
- }
+ T::Offset::usize_as(values.len())
+ }));
nulls = indices.nulls().map(|b| b.inner().sliced());
} else {
let num_bytes = bit_util::ceil(data_len, 8);
let mut null_buf = MutableBuffer::new(num_bytes).with_bitset(num_bytes, true);
let null_slice = null_buf.as_slice_mut();
-
- for (i, offset) in offsets.iter_mut().skip(1).enumerate() {
- let index = indices.value(i).to_usize().ok_or_else(|| {
- ArrowError::ComputeError("Cast to usize failed".to_string())
- })?;
-
- if array.is_valid(index) && indices.is_valid(i) {
+ offsets.extend(indices.values().iter().enumerate().map(|(i, index)| {
+ let index = index.as_usize();
+ if indices.is_valid(i) && array.is_valid(index) {
Review Comment:
this is the actual fix for the bug, right? that `indices.is_valid` is checked prior to `array.is_valid`?
```suggestion
// check index is valid before using index. The value in
// NULL index slots may not be within bounds of array
if indices.is_valid(i) && array.is_valid(index) {
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold merged pull request #4579: Fix take_bytes Null and Overflow Handling (#4576)
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold merged PR #4579:
URL: https://github.com/apache/arrow-rs/pull/4579
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org