[GitHub] [arrow] vertexclique commented on a change in pull request #8664: ARROW-10588: [Rust] Safe bit operations for Arrow

2020-11-18 Thread GitBox


vertexclique commented on a change in pull request #8664:
URL: https://github.com/apache/arrow/pull/8664#discussion_r526421965



##
File path: rust/arrow/src/util/bit_ops.rs
##
@@ -0,0 +1,407 @@
+// 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::buffer::Buffer;
+
+use bitvec::prelude::*;
+use bitvec::slice::ChunksExact;
+
+use std::fmt::Debug;
+
+///
+/// Immutable bit slice representation of buffer data
+#[derive(Debug)]
+pub struct BufferBitSlice<'a> {
+buffer_data: &'a [u8],
+bit_slice: &'a BitSlice,
+}
+
+impl<'a> BufferBitSlice<'a> {
+///
+/// Creates a immutable bit slice over the given data
+#[inline]
+pub fn new(buffer_data: &'a [u8]) -> Self {
+let bit_slice = BitSlicefrom_slice(buffer_data).unwrap();
+
+BufferBitSlice {
+buffer_data,
+bit_slice: _slice,
+}
+}
+
+///
+/// Returns immutable view with the given offset in bits and length in 
bits.
+/// This view have zero-copy representation over the actual data.
+#[inline]
+pub fn view(, offset_in_bits: usize, len_in_bits: usize) -> Self {
+Self {
+buffer_data: self.buffer_data,
+bit_slice: _slice[offset_in_bits..offset_in_bits + 
len_in_bits],
+}
+}
+
+///
+/// Returns bit chunks in native 64-bit allocation size.
+/// Native representations in Arrow follows 64-bit convention.
+/// Chunks can still be reinterpreted in any primitive type lower than u64.
+#[inline]
+pub fn chunks() -> BufferBitChunksExact
+where
+T: BitMemory,
+{
+let offset_size_in_bits = 8 * std::mem::size_of::();
+let chunks_exact = self.bit_slice.chunks_exact(offset_size_in_bits);
+let remainder_bits = chunks_exact.remainder();
+let remainder: T = if remainder_bits.len() == 0 {
+T::default()
+} else {
+remainder_bits.load::()
+};
+BufferBitChunksExact {
+chunks_exact,
+remainder,
+remainder_len_in_bits: remainder_bits.len(),
+}
+}
+
+///
+/// Converts the bit view into the Buffer.
+/// Buffer is always byte-aligned and well-aligned.

Review comment:
   Reworded both occurrences with that.





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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [arrow] vertexclique commented on a change in pull request #8664: ARROW-10588: [Rust] Safe bit operations for Arrow

2020-11-18 Thread GitBox


vertexclique commented on a change in pull request #8664:
URL: https://github.com/apache/arrow/pull/8664#discussion_r526390939



##
File path: rust/arrow/src/util/bit_ops.rs
##
@@ -0,0 +1,407 @@
+// 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::buffer::Buffer;
+
+use bitvec::prelude::*;
+use bitvec::slice::ChunksExact;
+
+use std::fmt::Debug;
+
+///
+/// Immutable bit slice representation of buffer data
+#[derive(Debug)]
+pub struct BufferBitSlice<'a> {
+buffer_data: &'a [u8],
+bit_slice: &'a BitSlice,
+}
+
+impl<'a> BufferBitSlice<'a> {
+///
+/// Creates a immutable bit slice over the given data
+#[inline]
+pub fn new(buffer_data: &'a [u8]) -> Self {
+let bit_slice = BitSlicefrom_slice(buffer_data).unwrap();
+
+BufferBitSlice {
+buffer_data,
+bit_slice: _slice,
+}
+}
+
+///
+/// Returns immutable view with the given offset in bits and length in 
bits.
+/// This view have zero-copy representation over the actual data.
+#[inline]
+pub fn view(, offset_in_bits: usize, len_in_bits: usize) -> Self {
+Self {
+buffer_data: self.buffer_data,
+bit_slice: _slice[offset_in_bits..offset_in_bits + 
len_in_bits],
+}
+}
+
+///
+/// Returns bit chunks in native 64-bit allocation size.
+/// Native representations in Arrow follows 64-bit convention.
+/// Chunks can still be reinterpreted in any primitive type lower than u64.
+#[inline]
+pub fn chunks() -> BufferBitChunksExact
+where
+T: BitMemory,
+{
+let offset_size_in_bits = 8 * std::mem::size_of::();
+let chunks_exact = self.bit_slice.chunks_exact(offset_size_in_bits);
+let remainder_bits = chunks_exact.remainder();
+let remainder: T = if remainder_bits.len() == 0 {
+T::default()
+} else {
+remainder_bits.load::()
+};
+BufferBitChunksExact {
+chunks_exact,
+remainder,
+remainder_len_in_bits: remainder_bits.len(),
+}
+}
+
+///
+/// Converts the bit view into the Buffer.
+/// Buffer is always byte-aligned and well-aligned.

Review comment:
   Yes that's what I meant. 





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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [arrow] vertexclique commented on a change in pull request #8664: ARROW-10588: [Rust] Safe bit operations for Arrow

2020-11-18 Thread GitBox


vertexclique commented on a change in pull request #8664:
URL: https://github.com/apache/arrow/pull/8664#discussion_r526358664



##
File path: rust/arrow/src/buffer.rs
##
@@ -258,39 +259,52 @@ impl Buffer {
 /// Returns a slice of this buffer starting at a certain bit offset.
 /// If the offset is byte-aligned the returned buffer is a shallow clone,
 /// otherwise a new buffer is allocated and filled with a copy of the bits 
in the range.
-pub fn bit_slice(, offset: usize, len: usize) -> Self {
-if offset % 8 == 0 && len % 8 == 0 {
-return self.slice(offset / 8);
+#[inline]
+pub fn bit_view(, offset_in_bits: usize, len_in_bits: usize) -> Self {

Review comment:
   I am unsure about this suggestion tbh. Since it is giving a range and it 
is not important to the user how it gives. It feels like it is important to 
give a subview from the user's point of view rather than how it gives. Since 
most of our code is relying on aligned access. Feels like the second paragraph 
of the doc is mostly for us, arrow committers.





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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [arrow] vertexclique commented on a change in pull request #8664: ARROW-10588: [Rust] Safe bit operations for Arrow

2020-11-18 Thread GitBox


vertexclique commented on a change in pull request #8664:
URL: https://github.com/apache/arrow/pull/8664#discussion_r526309397



##
File path: rust/arrow/src/util/bit_ops.rs
##
@@ -0,0 +1,407 @@
+// 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::buffer::Buffer;
+
+use bitvec::prelude::*;
+use bitvec::slice::ChunksExact;
+
+use std::fmt::Debug;
+
+///
+/// Immutable bit slice representation of buffer data
+#[derive(Debug)]
+pub struct BufferBitSlice<'a> {
+buffer_data: &'a [u8],
+bit_slice: &'a BitSlice,
+}
+
+impl<'a> BufferBitSlice<'a> {
+///
+/// Creates a immutable bit slice over the given data
+#[inline]
+pub fn new(buffer_data: &'a [u8]) -> Self {
+let bit_slice = BitSlicefrom_slice(buffer_data).unwrap();
+
+BufferBitSlice {
+buffer_data,
+bit_slice: _slice,
+}
+}
+
+///
+/// Returns immutable view with the given offset in bits and length in 
bits.
+/// This view have zero-copy representation over the actual data.
+#[inline]
+pub fn view(, offset_in_bits: usize, len_in_bits: usize) -> Self {
+Self {
+buffer_data: self.buffer_data,
+bit_slice: _slice[offset_in_bits..offset_in_bits + 
len_in_bits],
+}
+}
+
+///
+/// Returns bit chunks in native 64-bit allocation size.
+/// Native representations in Arrow follows 64-bit convention.
+/// Chunks can still be reinterpreted in any primitive type lower than u64.
+#[inline]
+pub fn chunks() -> BufferBitChunksExact
+where
+T: BitMemory,
+{
+let offset_size_in_bits = 8 * std::mem::size_of::();
+let chunks_exact = self.bit_slice.chunks_exact(offset_size_in_bits);
+let remainder_bits = chunks_exact.remainder();
+let remainder: T = if remainder_bits.len() == 0 {
+T::default()
+} else {
+remainder_bits.load::()
+};
+BufferBitChunksExact {
+chunks_exact,
+remainder,
+remainder_len_in_bits: remainder_bits.len(),
+}
+}
+
+///
+/// Converts the bit view into the Buffer.
+/// Buffer is always byte-aligned and well-aligned.
+#[inline]
+pub fn as_buffer() -> Buffer {
+Buffer::from(self.bit_slice.as_slice())
+}
+
+///
+/// Count ones in the given bit view
+#[inline]
+pub fn count_ones() -> usize {
+self.bit_slice.count_ones()
+}
+
+///
+/// Count zeros in the given bit view
+#[inline]
+pub fn count_zeros() -> usize {
+self.bit_slice.count_zeros()
+}
+
+///
+/// Get bit value at the given index in this bit view
+#[inline]
+pub fn get_bit(, index: usize) -> bool {
+*unsafe { self.bit_slice.get_unchecked(index) }
+}
+
+///
+/// Get bits in this view as vector of booleans
+#[inline]
+pub fn typed_bits() -> Vec {
+self.bit_slice.iter().map(|e| *e).collect()
+}
+
+///
+/// Get manipulated data as byte slice
+#[inline]
+pub fn to_slice() -> &[u8] {
+self.bit_slice.as_slice()
+}
+}
+
+impl<'a> PartialEq for BufferBitSlice<'a> {
+fn eq(, other: ) -> bool {
+self.bit_slice == other.bit_slice
+}
+}
+
+///
+/// Conversion from mutable slice to immutable bit slice
+impl<'a> From<&'a [u8]> for BufferBitSlice<'a> {
+fn from(data: &'a [u8]) -> Self {
+BufferBitSlice::new(data)
+}
+}
+
+///
+/// Mutable bit slice representation of buffer data
+#[derive(Debug)]
+pub struct BufferBitSliceMut<'a> {
+bit_slice: &'a mut BitSlice,
+}
+
+impl<'a> BufferBitSliceMut<'a> {
+///
+/// Creates a mutable bit slice over the given data
+#[inline]
+pub fn new(buffer_data: &'a mut [u8]) -> Self {
+let bit_slice = BitSlicefrom_slice_mut(buffer_data).unwrap();
+
+BufferBitSliceMut { bit_slice }
+}
+
+///
+/// Returns mutable view with the given offset in bits and length in bits.
+/// This view have zero-copy representation over the actual data.
+#[inline]
+pub fn view(&'a mut self, offset_in_bits: usize, len_in_bits: usize) -> 
Self {
+Self {
+  

[GitHub] [arrow] vertexclique commented on a change in pull request #8664: ARROW-10588: [Rust] Safe bit operations for Arrow

2020-11-18 Thread GitBox


vertexclique commented on a change in pull request #8664:
URL: https://github.com/apache/arrow/pull/8664#discussion_r526161079



##
File path: rust/arrow/src/compute/kernels/aggregate.rs
##
@@ -219,24 +223,27 @@ where
 let data_chunks = data.chunks_exact(64);
 let remainder = data_chunks.remainder();
 
-let bit_chunks = buffer.bit_chunks(array.offset(), array.len());
+let bit_slice = buffer.bit_slice().view(array.offset(), 
array.len());
+let bit_chunks = bit_slice.chunks::();
 let remainder_bits = bit_chunks.remainder_bits();
 
-data_chunks.zip(bit_chunks).for_each(|(chunk, mut mask)| {
-// split chunks further into slices corresponding to the 
vector length
-// the compiler is able to unroll this inner loop and remove 
bounds checks
-// since the outer chunk size (64) is always a multiple of the 
number of lanes
-chunk.chunks_exact(T::lanes()).for_each(|chunk| {
-let zero = T::init(T::default_value());
-let vecmask = T::mask_from_u64(mask);
-let chunk = T::load();
-let blended = T::mask_select(vecmask, chunk, zero);
-
-vector_sum = vector_sum + blended;
-
-mask = mask >> T::lanes();
+data_chunks
+.zip(bit_chunks.interpret())

Review comment:
   Addressed.

##
File path: rust/arrow/src/compute/kernels/comparison.rs
##
@@ -570,27 +570,26 @@ where
 ));
 }
 
-let num_bytes = bit_util::ceil(left_len, 8);
+let num_bytes = utils::ceil(left_len, 8);
 
 let not_both_null_bit_buffer =
 match combine_option_bitmap(left.data_ref(), right.data_ref(), 
left_len)? {
 Some(buff) => buff,
 None => new_all_set_buffer(num_bytes),
 };
-let not_both_null_bitmap = not_both_null_bit_buffer.data();
+let _not_both_null_bitmap = not_both_null_bit_buffer.data();

Review comment:
   Addressed too.





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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [arrow] vertexclique commented on a change in pull request #8664: ARROW-10588: [Rust] Safe bit operations for Arrow

2020-11-18 Thread GitBox


vertexclique commented on a change in pull request #8664:
URL: https://github.com/apache/arrow/pull/8664#discussion_r526145990



##
File path: rust/arrow/src/array/array_list.rs
##
@@ -711,8 +722,9 @@ mod tests {
 assert_eq!(1, sliced_array.offset());
 assert_eq!(2, sliced_array.null_count());
 
+let null_bit_slice = BufferBitSliceMut::new( null_bits);

Review comment:
   Nice catch :+1: 





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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [arrow] vertexclique commented on a change in pull request #8664: ARROW-10588: [Rust] Safe bit operations for Arrow

2020-11-18 Thread GitBox


vertexclique commented on a change in pull request #8664:
URL: https://github.com/apache/arrow/pull/8664#discussion_r526143753



##
File path: rust/arrow/src/util/utils.rs
##
@@ -0,0 +1,119 @@
+// 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.
+
+//! Utils for working with bits
+
+#[cfg(feature = "simd")]
+use packed_simd::u8x64;
+
+/// Returns the nearest number that is `>=` than `num` and is a multiple of 64
+#[inline]
+pub fn round_upto_multiple_of_64(num: usize) -> usize {

Review comment:
   Forgot to remove it :man_facepalming: 





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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [arrow] vertexclique commented on a change in pull request #8664: ARROW-10588: [Rust] Safe bit operations for Arrow

2020-11-18 Thread GitBox


vertexclique commented on a change in pull request #8664:
URL: https://github.com/apache/arrow/pull/8664#discussion_r526137607



##
File path: rust/arrow/src/util/bit_ops.rs
##
@@ -0,0 +1,407 @@
+// 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::buffer::Buffer;
+
+use bitvec::prelude::*;
+use bitvec::slice::ChunksExact;
+
+use std::fmt::Debug;
+
+///
+/// Immutable bit slice representation of buffer data
+#[derive(Debug)]
+pub struct BufferBitSlice<'a> {
+buffer_data: &'a [u8],
+bit_slice: &'a BitSlice,
+}
+
+impl<'a> BufferBitSlice<'a> {
+///
+/// Creates a immutable bit slice over the given data
+#[inline]
+pub fn new(buffer_data: &'a [u8]) -> Self {
+let bit_slice = BitSlicefrom_slice(buffer_data).unwrap();
+
+BufferBitSlice {
+buffer_data,
+bit_slice: _slice,
+}
+}
+
+///
+/// Returns immutable view with the given offset in bits and length in 
bits.
+/// This view have zero-copy representation over the actual data.
+#[inline]
+pub fn view(, offset_in_bits: usize, len_in_bits: usize) -> Self {
+Self {
+buffer_data: self.buffer_data,
+bit_slice: _slice[offset_in_bits..offset_in_bits + 
len_in_bits],
+}
+}
+
+///
+/// Returns bit chunks in native 64-bit allocation size.
+/// Native representations in Arrow follows 64-bit convention.
+/// Chunks can still be reinterpreted in any primitive type lower than u64.
+#[inline]
+pub fn chunks() -> BufferBitChunksExact
+where
+T: BitMemory,
+{
+let offset_size_in_bits = 8 * std::mem::size_of::();
+let chunks_exact = self.bit_slice.chunks_exact(offset_size_in_bits);
+let remainder_bits = chunks_exact.remainder();
+let remainder: T = if remainder_bits.len() == 0 {
+T::default()
+} else {
+remainder_bits.load::()
+};
+BufferBitChunksExact {
+chunks_exact,
+remainder,
+remainder_len_in_bits: remainder_bits.len(),
+}
+}
+
+///
+/// Converts the bit view into the Buffer.
+/// Buffer is always byte-aligned and well-aligned.
+#[inline]
+pub fn as_buffer() -> Buffer {
+Buffer::from(self.bit_slice.as_slice())
+}
+
+///
+/// Count ones in the given bit view
+#[inline]
+pub fn count_ones() -> usize {
+self.bit_slice.count_ones()
+}
+
+///
+/// Count zeros in the given bit view
+#[inline]
+pub fn count_zeros() -> usize {
+self.bit_slice.count_zeros()
+}
+
+///
+/// Get bit value at the given index in this bit view
+#[inline]
+pub fn get_bit(, index: usize) -> bool {
+*unsafe { self.bit_slice.get_unchecked(index) }
+}
+
+///
+/// Get bits in this view as vector of booleans
+#[inline]
+pub fn typed_bits() -> Vec {
+self.bit_slice.iter().map(|e| *e).collect()
+}
+
+///
+/// Get manipulated data as byte slice
+#[inline]
+pub fn to_slice() -> &[u8] {
+self.bit_slice.as_slice()
+}
+}
+
+impl<'a> PartialEq for BufferBitSlice<'a> {
+fn eq(, other: ) -> bool {
+self.bit_slice == other.bit_slice
+}
+}
+
+///
+/// Conversion from mutable slice to immutable bit slice
+impl<'a> From<&'a [u8]> for BufferBitSlice<'a> {
+fn from(data: &'a [u8]) -> Self {
+BufferBitSlice::new(data)
+}
+}
+
+///
+/// Mutable bit slice representation of buffer data
+#[derive(Debug)]
+pub struct BufferBitSliceMut<'a> {
+bit_slice: &'a mut BitSlice,
+}
+
+impl<'a> BufferBitSliceMut<'a> {
+///
+/// Creates a mutable bit slice over the given data
+#[inline]
+pub fn new(buffer_data: &'a mut [u8]) -> Self {
+let bit_slice = BitSlicefrom_slice_mut(buffer_data).unwrap();
+
+BufferBitSliceMut { bit_slice }
+}
+
+///
+/// Returns mutable view with the given offset in bits and length in bits.
+/// This view have zero-copy representation over the actual data.
+#[inline]
+pub fn view(&'a mut self, offset_in_bits: usize, len_in_bits: usize) -> 
Self {
+Self {
+  

[GitHub] [arrow] vertexclique commented on a change in pull request #8664: ARROW-10588: [Rust] Safe bit operations for Arrow

2020-11-18 Thread GitBox


vertexclique commented on a change in pull request #8664:
URL: https://github.com/apache/arrow/pull/8664#discussion_r526122655



##
File path: rust/arrow/src/util/bit_ops.rs
##
@@ -0,0 +1,407 @@
+// 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::buffer::Buffer;
+
+use bitvec::prelude::*;
+use bitvec::slice::ChunksExact;
+
+use std::fmt::Debug;
+
+///
+/// Immutable bit slice representation of buffer data
+#[derive(Debug)]
+pub struct BufferBitSlice<'a> {
+buffer_data: &'a [u8],
+bit_slice: &'a BitSlice,
+}
+
+impl<'a> BufferBitSlice<'a> {
+///
+/// Creates a immutable bit slice over the given data
+#[inline]
+pub fn new(buffer_data: &'a [u8]) -> Self {
+let bit_slice = BitSlicefrom_slice(buffer_data).unwrap();
+
+BufferBitSlice {
+buffer_data,
+bit_slice: _slice,
+}
+}
+
+///
+/// Returns immutable view with the given offset in bits and length in 
bits.
+/// This view have zero-copy representation over the actual data.
+#[inline]
+pub fn view(, offset_in_bits: usize, len_in_bits: usize) -> Self {
+Self {
+buffer_data: self.buffer_data,
+bit_slice: _slice[offset_in_bits..offset_in_bits + 
len_in_bits],
+}
+}
+
+///
+/// Returns bit chunks in native 64-bit allocation size.
+/// Native representations in Arrow follows 64-bit convention.
+/// Chunks can still be reinterpreted in any primitive type lower than u64.
+#[inline]
+pub fn chunks() -> BufferBitChunksExact
+where
+T: BitMemory,
+{
+let offset_size_in_bits = 8 * std::mem::size_of::();
+let chunks_exact = self.bit_slice.chunks_exact(offset_size_in_bits);
+let remainder_bits = chunks_exact.remainder();
+let remainder: T = if remainder_bits.len() == 0 {
+T::default()
+} else {
+remainder_bits.load::()
+};
+BufferBitChunksExact {
+chunks_exact,
+remainder,
+remainder_len_in_bits: remainder_bits.len(),
+}
+}
+
+///
+/// Converts the bit view into the Buffer.
+/// Buffer is always byte-aligned and well-aligned.
+#[inline]
+pub fn as_buffer() -> Buffer {
+Buffer::from(self.bit_slice.as_slice())
+}
+
+///
+/// Count ones in the given bit view
+#[inline]
+pub fn count_ones() -> usize {
+self.bit_slice.count_ones()
+}
+
+///
+/// Count zeros in the given bit view
+#[inline]
+pub fn count_zeros() -> usize {
+self.bit_slice.count_zeros()
+}
+
+///
+/// Get bit value at the given index in this bit view
+#[inline]
+pub fn get_bit(, index: usize) -> bool {
+*unsafe { self.bit_slice.get_unchecked(index) }
+}
+
+///
+/// Get bits in this view as vector of booleans
+#[inline]
+pub fn typed_bits() -> Vec {
+self.bit_slice.iter().map(|e| *e).collect()
+}
+
+///
+/// Get manipulated data as byte slice
+#[inline]
+pub fn to_slice() -> &[u8] {
+self.bit_slice.as_slice()
+}
+}
+
+impl<'a> PartialEq for BufferBitSlice<'a> {
+fn eq(, other: ) -> bool {
+self.bit_slice == other.bit_slice
+}
+}
+
+///
+/// Conversion from mutable slice to immutable bit slice
+impl<'a> From<&'a [u8]> for BufferBitSlice<'a> {
+fn from(data: &'a [u8]) -> Self {
+BufferBitSlice::new(data)
+}
+}
+
+///
+/// Mutable bit slice representation of buffer data
+#[derive(Debug)]
+pub struct BufferBitSliceMut<'a> {
+bit_slice: &'a mut BitSlice,
+}
+
+impl<'a> BufferBitSliceMut<'a> {
+///
+/// Creates a mutable bit slice over the given data
+#[inline]
+pub fn new(buffer_data: &'a mut [u8]) -> Self {
+let bit_slice = BitSlicefrom_slice_mut(buffer_data).unwrap();
+
+BufferBitSliceMut { bit_slice }
+}
+
+///
+/// Returns mutable view with the given offset in bits and length in bits.
+/// This view have zero-copy representation over the actual data.
+#[inline]
+pub fn view(&'a mut self, offset_in_bits: usize, len_in_bits: usize) -> 
Self {
+Self {
+  

[GitHub] [arrow] vertexclique commented on a change in pull request #8664: ARROW-10588: [Rust] Safe bit operations for Arrow

2020-11-18 Thread GitBox


vertexclique commented on a change in pull request #8664:
URL: https://github.com/apache/arrow/pull/8664#discussion_r526122170



##
File path: rust/arrow/src/util/bit_ops.rs
##
@@ -0,0 +1,407 @@
+// 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::buffer::Buffer;
+
+use bitvec::prelude::*;
+use bitvec::slice::ChunksExact;
+
+use std::fmt::Debug;
+
+///
+/// Immutable bit slice representation of buffer data
+#[derive(Debug)]
+pub struct BufferBitSlice<'a> {
+buffer_data: &'a [u8],

Review comment:
   Addressed.





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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [arrow] vertexclique commented on a change in pull request #8664: ARROW-10588: [Rust] Safe bit operations for Arrow

2020-11-18 Thread GitBox


vertexclique commented on a change in pull request #8664:
URL: https://github.com/apache/arrow/pull/8664#discussion_r526122010



##
File path: rust/arrow/src/util/bit_ops.rs
##
@@ -0,0 +1,407 @@
+// 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::buffer::Buffer;
+
+use bitvec::prelude::*;
+use bitvec::slice::ChunksExact;
+
+use std::fmt::Debug;
+
+///
+/// Immutable bit slice representation of buffer data
+#[derive(Debug)]
+pub struct BufferBitSlice<'a> {
+buffer_data: &'a [u8],
+bit_slice: &'a BitSlice,
+}
+
+impl<'a> BufferBitSlice<'a> {
+///
+/// Creates a immutable bit slice over the given data
+#[inline]
+pub fn new(buffer_data: &'a [u8]) -> Self {
+let bit_slice = BitSlicefrom_slice(buffer_data).unwrap();
+
+BufferBitSlice {
+buffer_data,
+bit_slice: _slice,
+}
+}
+
+///
+/// Returns immutable view with the given offset in bits and length in 
bits.
+/// This view have zero-copy representation over the actual data.
+#[inline]
+pub fn view(, offset_in_bits: usize, len_in_bits: usize) -> Self {
+Self {
+buffer_data: self.buffer_data,
+bit_slice: _slice[offset_in_bits..offset_in_bits + 
len_in_bits],
+}
+}
+
+///
+/// Returns bit chunks in native 64-bit allocation size.
+/// Native representations in Arrow follows 64-bit convention.
+/// Chunks can still be reinterpreted in any primitive type lower than u64.
+#[inline]

Review comment:
   Addressed, I've added a doctest that is also explaining the rationale 
behind this. I hope you like 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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [arrow] vertexclique commented on a change in pull request #8664: ARROW-10588: [Rust] Safe bit operations for Arrow

2020-11-18 Thread GitBox


vertexclique commented on a change in pull request #8664:
URL: https://github.com/apache/arrow/pull/8664#discussion_r526121662



##
File path: rust/arrow/src/util/bit_ops.rs
##
@@ -0,0 +1,407 @@
+// 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::buffer::Buffer;
+
+use bitvec::prelude::*;
+use bitvec::slice::ChunksExact;
+
+use std::fmt::Debug;
+
+///
+/// Immutable bit slice representation of buffer data
+#[derive(Debug)]
+pub struct BufferBitSlice<'a> {
+buffer_data: &'a [u8],
+bit_slice: &'a BitSlice,
+}
+
+impl<'a> BufferBitSlice<'a> {
+///
+/// Creates a immutable bit slice over the given data
+#[inline]
+pub fn new(buffer_data: &'a [u8]) -> Self {
+let bit_slice = BitSlicefrom_slice(buffer_data).unwrap();
+
+BufferBitSlice {
+buffer_data,
+bit_slice: _slice,
+}
+}
+
+///
+/// Returns immutable view with the given offset in bits and length in 
bits.
+/// This view have zero-copy representation over the actual data.
+#[inline]
+pub fn view(, offset_in_bits: usize, len_in_bits: usize) -> Self {

Review comment:
   Addressed.





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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [arrow] vertexclique commented on a change in pull request #8664: ARROW-10588: [Rust] Safe bit operations for Arrow

2020-11-18 Thread GitBox


vertexclique commented on a change in pull request #8664:
URL: https://github.com/apache/arrow/pull/8664#discussion_r525984998



##
File path: rust/arrow/src/compute/kernels/comparison.rs
##
@@ -570,27 +570,26 @@ where
 ));
 }
 
-let num_bytes = bit_util::ceil(left_len, 8);
+let num_bytes = utils::ceil(left_len, 8);
 
 let not_both_null_bit_buffer =
 match combine_option_bitmap(left.data_ref(), right.data_ref(), 
left_len)? {
 Some(buff) => buff,
 None => new_all_set_buffer(num_bytes),
 };
-let not_both_null_bitmap = not_both_null_bit_buffer.data();
+let _not_both_null_bitmap = not_both_null_bit_buffer.data();

Review comment:
   Things that I forgot along the lines.





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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [arrow] vertexclique commented on a change in pull request #8664: ARROW-10588: [Rust] Safe bit operations for Arrow

2020-11-18 Thread GitBox


vertexclique commented on a change in pull request #8664:
URL: https://github.com/apache/arrow/pull/8664#discussion_r525984128



##
File path: rust/arrow/src/util/bit_ops.rs
##
@@ -0,0 +1,407 @@
+// 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::buffer::Buffer;
+
+use bitvec::prelude::*;
+use bitvec::slice::ChunksExact;
+
+use std::fmt::Debug;
+
+///
+/// Immutable bit slice representation of buffer data
+#[derive(Debug)]
+pub struct BufferBitSlice<'a> {
+buffer_data: &'a [u8],
+bit_slice: &'a BitSlice,
+}
+
+impl<'a> BufferBitSlice<'a> {
+///
+/// Creates a immutable bit slice over the given data
+#[inline]
+pub fn new(buffer_data: &'a [u8]) -> Self {
+let bit_slice = BitSlicefrom_slice(buffer_data).unwrap();
+
+BufferBitSlice {
+buffer_data,
+bit_slice: _slice,
+}
+}
+
+///
+/// Returns immutable view with the given offset in bits and length in 
bits.
+/// This view have zero-copy representation over the actual data.
+#[inline]
+pub fn view(, offset_in_bits: usize, len_in_bits: usize) -> Self {
+Self {
+buffer_data: self.buffer_data,
+bit_slice: _slice[offset_in_bits..offset_in_bits + 
len_in_bits],
+}
+}
+
+///
+/// Returns bit chunks in native 64-bit allocation size.
+/// Native representations in Arrow follows 64-bit convention.
+/// Chunks can still be reinterpreted in any primitive type lower than u64.
+#[inline]
+pub fn chunks() -> BufferBitChunksExact
+where
+T: BitMemory,
+{
+let offset_size_in_bits = 8 * std::mem::size_of::();
+let chunks_exact = self.bit_slice.chunks_exact(offset_size_in_bits);
+let remainder_bits = chunks_exact.remainder();
+let remainder: T = if remainder_bits.len() == 0 {
+T::default()
+} else {
+remainder_bits.load::()
+};
+BufferBitChunksExact {
+chunks_exact,
+remainder,
+remainder_len_in_bits: remainder_bits.len(),
+}
+}
+
+///
+/// Converts the bit view into the Buffer.
+/// Buffer is always byte-aligned and well-aligned.

Review comment:
   Ok, I was thinking that you will tell me something about the explanation 
since it is not that clear. I know it is not clear totally but by byte-aligned 
I meant(also everyone meant) byte-alignment on structure level:
   
https://en.wikipedia.org/wiki/Data_structure_alignment#Typical_alignment_of_C_structs_on_x86
   and well-aligned I meant pointer to the memory location:
   
https://en.wikipedia.org/wiki/Data_structure_alignment#Hardware_significance_of_alignment_requirements
 
   
   I am ok with any wording that will explain this briefly. I am totally ok 
with removing it too, but IMO it is important to tell to people that both 
things are aligned in Arrow Rust impl.





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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [arrow] vertexclique commented on a change in pull request #8664: ARROW-10588: [Rust] Safe bit operations for Arrow

2020-11-18 Thread GitBox


vertexclique commented on a change in pull request #8664:
URL: https://github.com/apache/arrow/pull/8664#discussion_r525980049



##
File path: rust/arrow/src/util/bit_ops.rs
##
@@ -0,0 +1,407 @@
+// 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::buffer::Buffer;
+
+use bitvec::prelude::*;
+use bitvec::slice::ChunksExact;
+
+use std::fmt::Debug;
+
+///
+/// Immutable bit slice representation of buffer data
+#[derive(Debug)]
+pub struct BufferBitSlice<'a> {
+buffer_data: &'a [u8],

Review comment:
   It was my thinking about it, I was skeptical about dispensing a new 
shared slice struct or carry on with the underlying one. Seems like carrying on 
with the underlying one makes life way easier.





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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org