You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/09/12 05:24:17 UTC
[GitHub] [arrow-rs] liukun4515 opened a new pull request, #2703: Support bitwise and operation in the kernel
liukun4515 opened a new pull request, #2703:
URL: https://github.com/apache/arrow-rs/pull/2703
# 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.
-->
part of #2702
# 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
[GitHub] [arrow-rs] liukun4515 merged pull request #2703: Support bitwise and operation in the kernel
Posted by GitBox <gi...@apache.org>.
liukun4515 merged PR #2703:
URL: https://github.com/apache/arrow-rs/pull/2703
--
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] liukun4515 commented on a diff in pull request #2703: Support bitwise and operation in the kernel
Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2703:
URL: https://github.com/apache/arrow-rs/pull/2703#discussion_r969026323
##########
arrow/src/compute/kernels/bitwise.rs:
##########
@@ -0,0 +1,109 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::PrimitiveArray;
+use crate::compute::{binary, unary};
+use crate::datatypes::ArrowNumericType;
+use crate::error::{ArrowError, Result};
+use std::ops::BitAnd;
+
+// The helper function for bitwise operation with two array
+fn bitwise_op<T, F>(
+ left: &PrimitiveArray<T>,
+ right: &PrimitiveArray<T>,
+ op: F,
+) -> Result<PrimitiveArray<T>>
+where
+ T: ArrowNumericType,
+ F: Fn(T::Native, T::Native) -> T::Native,
+{
+ if left.len() != right.len() {
+ return Err(ArrowError::ComputeError(
+ "Cannot perform bitwise operation on arrays of different length".to_string(),
+ ));
+ }
+ Ok(binary(left, right, op))
Review Comment:
> Could we directly operate on the value buffer, as the bitwise operation is irrelevant to the data type ?
@HaoYang670 we can track a issue for this optimization, and can try it after finish the bitwise kernel
--
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] HaoYang670 commented on a diff in pull request #2703: Support bitwise and operation in the kernel
Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on code in PR #2703:
URL: https://github.com/apache/arrow-rs/pull/2703#discussion_r969048993
##########
arrow/src/compute/kernels/bitwise.rs:
##########
@@ -0,0 +1,109 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::PrimitiveArray;
+use crate::compute::{binary, unary};
+use crate::datatypes::ArrowNumericType;
+use crate::error::{ArrowError, Result};
+use std::ops::BitAnd;
+
+// The helper function for bitwise operation with two array
+fn bitwise_op<T, F>(
Review Comment:
What is the difference between this function and `math_op` (https://github.com/apache/arrow-rs/blob/master/arrow/src/compute/kernels/arithmetic.rs#L60)?
I find the 2 have same types and function body. Could we directly reuse `math_op`, such as
```rust
// The helper function for bitwise operation with two array
fn bitwise_op<T, F>(
left: &PrimitiveArray<T>,
right: &PrimitiveArray<T>,
op: F,
) -> Result<PrimitiveArray<T>>
where
T: ArrowNumericType,
F: Fn(T::Native, T::Native) -> T::Native,
{
math_op(left, right, op)
}
```
?
--
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] liukun4515 commented on a diff in pull request #2703: Support bitwise and operation in the kernel
Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2703:
URL: https://github.com/apache/arrow-rs/pull/2703#discussion_r969029051
##########
arrow/src/compute/kernels/bitwise.rs:
##########
@@ -0,0 +1,109 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::PrimitiveArray;
+use crate::compute::{binary, unary};
+use crate::datatypes::ArrowNumericType;
+use crate::error::{ArrowError, Result};
+use std::ops::BitAnd;
+
+// The helper function for bitwise operation with two array
+fn bitwise_op<T, F>(
+ left: &PrimitiveArray<T>,
+ right: &PrimitiveArray<T>,
+ op: F,
+) -> Result<PrimitiveArray<T>>
+where
+ T: ArrowNumericType,
+ F: Fn(T::Native, T::Native) -> T::Native,
+{
+ if left.len() != right.len() {
+ return Err(ArrowError::ComputeError(
+ "Cannot perform bitwise operation on arrays of different length".to_string(),
+ ));
+ }
+ Ok(binary(left, right, op))
+}
+
+/// Perform `left & right` operation on two arrays. If either left or right value is null
+/// then the result is also null.
+pub fn bitwise_and<T>(
+ left: &PrimitiveArray<T>,
+ right: &PrimitiveArray<T>,
+) -> Result<PrimitiveArray<T>>
+where
+ T: ArrowNumericType,
+ T::Native: BitAnd<Output = T::Native>,
+{
+ bitwise_op(left, right, |a, b| a & b)
+}
+
+/// Perform bitwise and every value in an array with the scalar. If any value in the array is null then the
+/// result is also null.
+pub fn bitwise_and_scalar<T>(
+ array: &PrimitiveArray<T>,
+ scalar: T::Native,
+) -> Result<PrimitiveArray<T>>
+where
+ T: ArrowNumericType,
+ T::Native: BitAnd<Output = T::Native>,
+{
+ Ok(unary(array, |value| value & scalar))
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::array::{Int32Array, UInt64Array};
+ use crate::compute::kernels::bitwise::{bitwise_and, bitwise_and_scalar};
+ use crate::error::Result;
+
+ #[test]
+ fn test_bitwise_and_array() -> Result<()> {
+ // unsigned value
+ let left = UInt64Array::from(vec![Some(1), Some(2), None, Some(4)]);
+ let right = UInt64Array::from(vec![Some(5), Some(10), Some(8), Some(12)]);
+ let expected = UInt64Array::from(vec![Some(1), Some(2), None, Some(4)]);
+ let result = bitwise_and(&left, &right)?;
+ assert_eq!(expected, result);
+
+ // signed value
+ let left = Int32Array::from(vec![Some(1), Some(2), None, Some(4)]);
+ let right = Int32Array::from(vec![Some(5), Some(10), Some(8), Some(12)]);
+ let expected = Int32Array::from(vec![Some(1), Some(2), None, Some(4)]);
+ let result = bitwise_and(&left, &right)?;
+ assert_eq!(expected, result);
+ Ok(())
+ }
+
+ #[test]
+ fn test_bitwise_and_array_scalar() -> Result<()> {
+ // unsigned value
+ let left = UInt64Array::from(vec![Some(1), Some(2), None, Some(4)]);
+ let scalar = 7;
+ let expected = UInt64Array::from(vec![Some(1), Some(2), None, Some(4)]);
Review Comment:
Done
--
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] HaoYang670 commented on a diff in pull request #2703: Support bitwise and operation in the kernel
Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on code in PR #2703:
URL: https://github.com/apache/arrow-rs/pull/2703#discussion_r968331740
##########
arrow/src/compute/kernels/bitwise.rs:
##########
@@ -0,0 +1,109 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::PrimitiveArray;
+use crate::compute::{binary, unary};
+use crate::datatypes::ArrowNumericType;
+use crate::error::{ArrowError, Result};
+use std::ops::BitAnd;
+
+// The helper function for bitwise operation with two array
+fn bitwise_op<T, F>(
+ left: &PrimitiveArray<T>,
+ right: &PrimitiveArray<T>,
+ op: F,
+) -> Result<PrimitiveArray<T>>
+where
+ T: ArrowNumericType,
+ F: Fn(T::Native, T::Native) -> T::Native,
+{
+ if left.len() != right.len() {
Review Comment:
We also check the length in `binary`, so I am afraid the checking here is somewhat redundant.
Maybe we could let `binary` to return Err when the 2 arrays have different length.
##########
arrow/src/compute/kernels/bitwise.rs:
##########
@@ -0,0 +1,109 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::PrimitiveArray;
+use crate::compute::{binary, unary};
+use crate::datatypes::ArrowNumericType;
+use crate::error::{ArrowError, Result};
+use std::ops::BitAnd;
+
+// The helper function for bitwise operation with two array
+fn bitwise_op<T, F>(
+ left: &PrimitiveArray<T>,
+ right: &PrimitiveArray<T>,
+ op: F,
+) -> Result<PrimitiveArray<T>>
+where
+ T: ArrowNumericType,
+ F: Fn(T::Native, T::Native) -> T::Native,
+{
+ if left.len() != right.len() {
+ return Err(ArrowError::ComputeError(
+ "Cannot perform bitwise operation on arrays of different length".to_string(),
+ ));
+ }
+ Ok(binary(left, right, op))
Review Comment:
Could we directly operate on the value buffer,
as the bitwise operation is irrelevant to the data type ?
--
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] ursabot commented on pull request #2703: Support bitwise and operation in the kernel
Posted by GitBox <gi...@apache.org>.
ursabot commented on PR #2703:
URL: https://github.com/apache/arrow-rs/pull/2703#issuecomment-1244847547
Benchmark runs are scheduled for baseline = 0ba5c5bf47941d0aa407b592ad594d902979ce50 and contender = 259a3028b22f47d788b5e32b4651731206b1b4b6. 259a3028b22f47d788b5e32b4651731206b1b4b6 is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/f23c78b0c0fe4b3a833e879c17b9ab22...f376922196d7478dba9a4ce3b61136be/)
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/70f3082911684aae8ded7e43c1bc877f...899f613d2214452b887c67743d0a3643/)
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/073dca82b47f4d80978f7777e87a0a6d...b50b182f37f24458a70442ede04c0b23/)
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/bf0d86f55082425db7b47a7c33ef8ffb...cfb0f5c3445144538ccbd0918d0b8fc3/)
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
--
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] liukun4515 commented on a diff in pull request #2703: Support bitwise and operation in the kernel
Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2703:
URL: https://github.com/apache/arrow-rs/pull/2703#discussion_r969067446
##########
arrow/src/compute/kernels/bitwise.rs:
##########
@@ -0,0 +1,109 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::PrimitiveArray;
+use crate::compute::{binary, unary};
+use crate::datatypes::ArrowNumericType;
+use crate::error::{ArrowError, Result};
+use std::ops::BitAnd;
+
+// The helper function for bitwise operation with two array
+fn bitwise_op<T, F>(
Review Comment:
We can use the `math_op` to replace the code body, but I concern the error message which can point out what operation is error
--
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 pull request #2703: Support bitwise and operation in the kernel
Posted by GitBox <gi...@apache.org>.
alamb commented on PR #2703:
URL: https://github.com/apache/arrow-rs/pull/2703#issuecomment-1243710447
This is great -- thank you @liukun4515
--
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] liukun4515 commented on a diff in pull request #2703: Support bitwise and operation in the kernel
Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2703:
URL: https://github.com/apache/arrow-rs/pull/2703#discussion_r969066328
##########
arrow/src/compute/kernels/bitwise.rs:
##########
@@ -0,0 +1,109 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::PrimitiveArray;
+use crate::compute::{binary, unary};
+use crate::datatypes::ArrowNumericType;
+use crate::error::{ArrowError, Result};
+use std::ops::BitAnd;
+
+// The helper function for bitwise operation with two array
+fn bitwise_op<T, F>(
Review Comment:
two difference about the `math_op`
1. in the bit wise, the left and right has the same data type of `T`, and the output data type is also `T`
2. Minor diff is the Error message.
--
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] liukun4515 commented on a diff in pull request #2703: Support bitwise and operation in the kernel
Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2703:
URL: https://github.com/apache/arrow-rs/pull/2703#discussion_r969025476
##########
arrow/src/compute/kernels/bitwise.rs:
##########
@@ -0,0 +1,109 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::PrimitiveArray;
+use crate::compute::{binary, unary};
+use crate::datatypes::ArrowNumericType;
+use crate::error::{ArrowError, Result};
+use std::ops::BitAnd;
+
+// The helper function for bitwise operation with two array
+fn bitwise_op<T, F>(
+ left: &PrimitiveArray<T>,
+ right: &PrimitiveArray<T>,
+ op: F,
+) -> Result<PrimitiveArray<T>>
+where
+ T: ArrowNumericType,
+ F: Fn(T::Native, T::Native) -> T::Native,
+{
+ if left.len() != right.len() {
Review Comment:
> We also check the length in `binary`, so I am afraid the checking here is somewhat redundant.
>
> Maybe we could let `binary` to return Err when the 2 arrays have different length.
If the binary return the `ERR` with checking the len, I can remove this.
--
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] HaoYang670 commented on a diff in pull request #2703: Support bitwise and operation in the kernel
Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on code in PR #2703:
URL: https://github.com/apache/arrow-rs/pull/2703#discussion_r969046414
##########
arrow/src/compute/kernels/bitwise.rs:
##########
@@ -0,0 +1,109 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::PrimitiveArray;
+use crate::compute::{binary, unary};
+use crate::datatypes::ArrowNumericType;
+use crate::error::{ArrowError, Result};
+use std::ops::BitAnd;
+
+// The helper function for bitwise operation with two array
+fn bitwise_op<T, F>(
+ left: &PrimitiveArray<T>,
+ right: &PrimitiveArray<T>,
+ op: F,
+) -> Result<PrimitiveArray<T>>
+where
+ T: ArrowNumericType,
+ F: Fn(T::Native, T::Native) -> T::Native,
+{
+ if left.len() != right.len() {
+ return Err(ArrowError::ComputeError(
+ "Cannot perform bitwise operation on arrays of different length".to_string(),
+ ));
+ }
+ Ok(binary(left, right, op))
Review Comment:
Agreed @liukun4515. And we should add some benchmarks to see whether we ccoul get performance improvement by doing this.
--
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 #2703: Support bitwise and operation in the kernel
Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #2703:
URL: https://github.com/apache/arrow-rs/pull/2703#discussion_r968765733
##########
arrow/src/compute/kernels/bitwise.rs:
##########
@@ -0,0 +1,109 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::PrimitiveArray;
+use crate::compute::{binary, unary};
+use crate::datatypes::ArrowNumericType;
+use crate::error::{ArrowError, Result};
+use std::ops::BitAnd;
+
+// The helper function for bitwise operation with two array
+fn bitwise_op<T, F>(
+ left: &PrimitiveArray<T>,
+ right: &PrimitiveArray<T>,
+ op: F,
+) -> Result<PrimitiveArray<T>>
+where
+ T: ArrowNumericType,
+ F: Fn(T::Native, T::Native) -> T::Native,
+{
+ if left.len() != right.len() {
+ return Err(ArrowError::ComputeError(
+ "Cannot perform bitwise operation on arrays of different length".to_string(),
+ ));
+ }
+ Ok(binary(left, right, op))
+}
+
+/// Perform `left & right` operation on two arrays. If either left or right value is null
+/// then the result is also null.
+pub fn bitwise_and<T>(
+ left: &PrimitiveArray<T>,
+ right: &PrimitiveArray<T>,
+) -> Result<PrimitiveArray<T>>
+where
+ T: ArrowNumericType,
+ T::Native: BitAnd<Output = T::Native>,
+{
+ bitwise_op(left, right, |a, b| a & b)
+}
+
+/// Perform bitwise and every value in an array with the scalar. If any value in the array is null then the
+/// result is also null.
+pub fn bitwise_and_scalar<T>(
+ array: &PrimitiveArray<T>,
+ scalar: T::Native,
+) -> Result<PrimitiveArray<T>>
+where
+ T: ArrowNumericType,
+ T::Native: BitAnd<Output = T::Native>,
+{
+ Ok(unary(array, |value| value & scalar))
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::array::{Int32Array, UInt64Array};
+ use crate::compute::kernels::bitwise::{bitwise_and, bitwise_and_scalar};
+ use crate::error::Result;
+
+ #[test]
+ fn test_bitwise_and_array() -> Result<()> {
+ // unsigned value
+ let left = UInt64Array::from(vec![Some(1), Some(2), None, Some(4)]);
+ let right = UInt64Array::from(vec![Some(5), Some(10), Some(8), Some(12)]);
+ let expected = UInt64Array::from(vec![Some(1), Some(2), None, Some(4)]);
+ let result = bitwise_and(&left, &right)?;
+ assert_eq!(expected, result);
+
+ // signed value
+ let left = Int32Array::from(vec![Some(1), Some(2), None, Some(4)]);
+ let right = Int32Array::from(vec![Some(5), Some(10), Some(8), Some(12)]);
+ let expected = Int32Array::from(vec![Some(1), Some(2), None, Some(4)]);
+ let result = bitwise_and(&left, &right)?;
+ assert_eq!(expected, result);
+ Ok(())
+ }
+
+ #[test]
+ fn test_bitwise_and_array_scalar() -> Result<()> {
+ // unsigned value
+ let left = UInt64Array::from(vec![Some(1), Some(2), None, Some(4)]);
+ let scalar = 7;
+ let expected = UInt64Array::from(vec![Some(1), Some(2), None, Some(4)]);
Review Comment:
Could we get a test where the result is changed 😄
--
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 #2703: Support bitwise and operation in the kernel
Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #2703:
URL: https://github.com/apache/arrow-rs/pull/2703#discussion_r968763271
##########
arrow/src/compute/kernels/bitwise.rs:
##########
@@ -0,0 +1,109 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::PrimitiveArray;
+use crate::compute::{binary, unary};
+use crate::datatypes::ArrowNumericType;
+use crate::error::{ArrowError, Result};
+use std::ops::BitAnd;
+
+// The helper function for bitwise operation with two array
+fn bitwise_op<T, F>(
+ left: &PrimitiveArray<T>,
+ right: &PrimitiveArray<T>,
+ op: F,
+) -> Result<PrimitiveArray<T>>
+where
+ T: ArrowNumericType,
+ F: Fn(T::Native, T::Native) -> T::Native,
+{
+ if left.len() != right.len() {
+ return Err(ArrowError::ComputeError(
+ "Cannot perform bitwise operation on arrays of different length".to_string(),
+ ));
+ }
+ Ok(binary(left, right, op))
Review Comment:
You would need to take into account both endianess, and the size of the types, but theoretically yes you could do this. I suspect it would perform worse, however, as you'll have a variable length type now
--
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 #2703: Support bitwise and operation in the kernel
Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #2703:
URL: https://github.com/apache/arrow-rs/pull/2703#discussion_r968764697
##########
arrow/src/compute/kernels/bitwise.rs:
##########
@@ -0,0 +1,109 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::PrimitiveArray;
+use crate::compute::{binary, unary};
+use crate::datatypes::ArrowNumericType;
+use crate::error::{ArrowError, Result};
+use std::ops::BitAnd;
+
+// The helper function for bitwise operation with two array
+fn bitwise_op<T, F>(
+ left: &PrimitiveArray<T>,
+ right: &PrimitiveArray<T>,
+ op: F,
+) -> Result<PrimitiveArray<T>>
+where
+ T: ArrowNumericType,
+ F: Fn(T::Native, T::Native) -> T::Native,
+{
+ if left.len() != right.len() {
Review Comment:
I think it is fine, the compiler should eliminate it, and even if it doesn't the cost will be amortised over the much more expensive code below
--
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