You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "viirya (via GitHub)" <gi...@apache.org> on 2024/09/15 00:43:16 UTC
[PR] fix: binary_mut should work if only one input array has null buffer [arrow-rs]
viirya opened a new pull request, #6396:
URL: https://github.com/apache/arrow-rs/pull/6396
# 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 #6374.
# 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.
-->
# 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
Re: [PR] fix: binary_mut should work if only one input array has null buffer [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #6396:
URL: https://github.com/apache/arrow-rs/pull/6396
--
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
Re: [PR] fix: binary_mut should work if only one input array has null buffer [arrow-rs]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #6396:
URL: https://github.com/apache/arrow-rs/pull/6396#discussion_r1759926697
##########
arrow-arith/src/arity.rs:
##########
@@ -313,7 +313,7 @@ where
))));
}
- let nulls = NullBuffer::union(a.logical_nulls().as_ref(), b.logical_nulls().as_ref());
Review Comment:
If only `a` has null buffer, `NullBuffer.union` will clone it which shares the underlying buffer that fails `into_mutable` call on the null buffer later.
--
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
Re: [PR] fix: binary_mut should work if only one input array has null buffer [arrow-rs]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #6396:
URL: https://github.com/apache/arrow-rs/pull/6396#discussion_r1764467841
##########
arrow-arith/src/arity.rs:
##########
@@ -313,7 +313,7 @@ where
))));
}
- let nulls = NullBuffer::union(a.logical_nulls().as_ref(), b.logical_nulls().as_ref());
Review Comment:
Hmm, for `binary_mut`, it is okay to defer the computation of union of two null buffers. It is because that we don't need to own the null buffers before invoking `op` on the two arrays.
Since the null buffer is not changed on the builder, we can compute the union after finishing the builder. So for `binary_mut`, we can avoid copying null buffer.
But for `try_binary_mut`, because we need to get the union of two null buffers before invoking `op` on the two arrays, we still need to copy it as this PR does.
--
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
Re: [PR] fix: binary_mut should work if only one input array has null buffer [arrow-rs]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #6396:
URL: https://github.com/apache/arrow-rs/pull/6396#discussion_r1762071146
##########
arrow-arith/src/arity.rs:
##########
@@ -435,6 +436,22 @@ where
}
}
+/// Computes the union of the nulls in two optional [`NullBuffer`] which
+/// is not shared with the input buffers.
+///
+/// The union of the nulls is the same as `NullBuffer::union(lhs, rhs)` but
+/// it does not increase the reference count of the null buffer.
+fn create_union_null_buffer(
+ lhs: Option<&NullBuffer>,
+ rhs: Option<&NullBuffer>,
+) -> Option<NullBuffer> {
+ match (lhs, rhs) {
+ (Some(lhs), Some(rhs)) => Some(NullBuffer::new(lhs.inner() & rhs.inner())),
+ (Some(n), None) | (None, Some(n)) => Some(NullBuffer::new(n.inner() & n.inner())),
Review Comment:
Yes. Previously it did a clone that shares the underlying buffer.
--
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
Re: [PR] fix: binary_mut should work if only one input array has null buffer [arrow-rs]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #6396:
URL: https://github.com/apache/arrow-rs/pull/6396#discussion_r1764470344
##########
arrow-arith/src/arity.rs:
##########
@@ -313,7 +313,7 @@ where
))));
}
- let nulls = NullBuffer::union(a.logical_nulls().as_ref(), b.logical_nulls().as_ref());
Review Comment:
So I only avoid copying null buffer in `binary_mut`.
--
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
Re: [PR] fix: binary_mut should work if only one input array has null buffer [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #6396:
URL: https://github.com/apache/arrow-rs/pull/6396#discussion_r1761795643
##########
arrow-arith/src/arity.rs:
##########
@@ -435,6 +436,22 @@ where
}
}
+/// Computes the union of the nulls in two optional [`NullBuffer`] which
+/// is not shared with the input buffers.
+///
+/// The union of the nulls is the same as `NullBuffer::union(lhs, rhs)` but
+/// it does not increase the reference count of the null buffer.
+fn create_union_null_buffer(
+ lhs: Option<&NullBuffer>,
+ rhs: Option<&NullBuffer>,
+) -> Option<NullBuffer> {
+ match (lhs, rhs) {
+ (Some(lhs), Some(rhs)) => Some(NullBuffer::new(lhs.inner() & rhs.inner())),
+ (Some(n), None) | (None, Some(n)) => Some(NullBuffer::new(n.inner() & n.inner())),
Review Comment:
If I understand this correctly, it forces a copy of the null buffer's contents?
##########
arrow-arith/src/arity.rs:
##########
@@ -313,7 +313,7 @@ where
))));
}
- let nulls = NullBuffer::union(a.logical_nulls().as_ref(), b.logical_nulls().as_ref());
Review Comment:
Since we have `a` by value, perhaps we could add a `PrimitiveArray::into_parts` that would deconstruct the `PrimitiveArray` into its underlying `NullBuffer` and values 🤔
Following the model of https://docs.rs/arrow/latest/arrow/array/struct.GenericByteArray.html#method.into_parts
That way we could directly use the `NullBuffer ` and avoid the copy in `create_union_null_buffer`
--
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
Re: [PR] fix: binary_mut should work if only one input array has null buffer [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #6396:
URL: https://github.com/apache/arrow-rs/pull/6396#discussion_r1764844394
##########
arrow-arith/src/arity.rs:
##########
@@ -557,6 +577,24 @@ mod tests {
assert_eq!(c, expected);
}
+ #[test]
+ fn test_binary_mut_null_buffer() {
+ let a = Int32Array::from(vec![Some(3), Some(4), Some(5), Some(6), None]);
+
+ let b = Int32Array::from(vec![Some(10), Some(11), Some(12), Some(13), Some(14)]);
+
+ let r1 = binary_mut(a, &b, |a, b| a + b).unwrap();
+
+ let a = Int32Array::from(vec![Some(3), Some(4), Some(5), Some(6), None]);
+ let b = Int32Array::new(
+ vec![10, 11, 12, 13, 14].into(),
+ Some(vec![true, true, true, true, true].into()),
+ );
+
+ let r2 = binary_mut(a, &b, |a, b| a + b).unwrap();
Review Comment:
```suggestion
// unwrap here means that no copying occured
let r2 = binary_mut(a, &b, |a, b| a + b).unwrap();
```
##########
arrow-arith/src/arity.rs:
##########
@@ -313,7 +313,7 @@ where
))));
}
- let nulls = NullBuffer::union(a.logical_nulls().as_ref(), b.logical_nulls().as_ref());
Review Comment:
It still feels to me that we could remove the copy on try_binary_mut, but this seems like an improvement to me 🚀
##########
arrow-arith/src/arity.rs:
##########
@@ -587,6 +625,24 @@ mod tests {
.expect_err("should got error");
}
+ #[test]
+ fn test_try_binary_mut_null_buffer() {
+ let a = Int32Array::from(vec![Some(3), Some(4), Some(5), Some(6), None]);
+
+ let b = Int32Array::from(vec![Some(10), Some(11), Some(12), Some(13), Some(14)]);
+
+ let r1 = try_binary_mut(a, &b, |a, b| Ok(a + b)).unwrap();
+
+ let a = Int32Array::from(vec![Some(3), Some(4), Some(5), Some(6), None]);
+ let b = Int32Array::new(
+ vec![10, 11, 12, 13, 14].into(),
+ Some(vec![true, true, true, true, true].into()),
+ );
+
+ let r2 = try_binary_mut(a, &b, |a, b| Ok(a + b)).unwrap();
Review Comment:
```suggestion
// unwrap here means that no copying occured
let r2 = try_binary_mut(a, &b, |a, b| Ok(a + b)).unwrap();
```
--
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
Re: [PR] fix: binary_mut should work if only one input array has null buffer [arrow-rs]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on PR #6396:
URL: https://github.com/apache/arrow-rs/pull/6396#issuecomment-2359034359
Thanks @alamb @MaxenceMaire
--
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
Re: [PR] fix: binary_mut should work if only one input array has null buffer [arrow-rs]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #6396:
URL: https://github.com/apache/arrow-rs/pull/6396#discussion_r1764467841
##########
arrow-arith/src/arity.rs:
##########
@@ -313,7 +313,7 @@ where
))));
}
- let nulls = NullBuffer::union(a.logical_nulls().as_ref(), b.logical_nulls().as_ref());
Review Comment:
Hmm, for `binary_mut`, it is okay to defer the computation of union of two null buffers. It is because that we don't need to own the null buffers before invoking `op` on the two arrays.
Since the null buffer is not changed on the builder, we can compute the union after finishing the builder. So for `binary_mut`, we can avoid copying null buffer.
But for `try_binary_mut`, because we need to get the union of two null buffers before invoking `op` on the two arrays, we still need to copy it as this PR does.
`PrimitiveArray::into_parts` (it already exists) also cannot help on it because the builder needs to own the null buffer.
--
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
Re: [PR] fix: binary_mut should work if only one input array has null buffer [arrow-rs]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #6396:
URL: https://github.com/apache/arrow-rs/pull/6396#discussion_r1762071299
##########
arrow-arith/src/arity.rs:
##########
@@ -313,7 +313,7 @@ where
))));
}
- let nulls = NullBuffer::union(a.logical_nulls().as_ref(), b.logical_nulls().as_ref());
Review Comment:
Hmm, let me see if it works.
--
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
Re: [PR] fix: binary_mut should work if only one input array has null buffer [arrow-rs]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on PR #6396:
URL: https://github.com/apache/arrow-rs/pull/6396#issuecomment-2359487908
Thank you @alamb
--
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
Re: [PR] fix: binary_mut should work if only one input array has null buffer [arrow-rs]
Posted by "MaxenceMaire (via GitHub)" <gi...@apache.org>.
MaxenceMaire commented on PR #6396:
URL: https://github.com/apache/arrow-rs/pull/6396#issuecomment-2358101694
Nice work, thanks @viirya and @alamb for looking into it!
--
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