Skip to content

Apache DataFusion SQL Query Engine

ARROW-3687: [Rust] Anything measuring array slots should be `usize`

Paddy Horan1ca4a13ba41d
diff --git a/rust/src/array.rs b/rust/src/array.rs
index ca1d2a5cdb..51bc8d993c 100644
--- a/rust/src/array.rs
+++ b/rust/src/array.rs
@@ -47,27 +47,27 @@ pub trait Array: Send + Sync {
}
/// Returns the length (i.e., number of elements) of this array
- fn len(&self) -> i64 {
+ fn len(&self) -> usize {
self.data().len()
}
/// Returns the offset of this array
- fn offset(&self) -> i64 {
+ fn offset(&self) -> usize {
self.data().offset()
}
/// Returns whether the element at index `i` is null
- fn is_null(&self, i: i64) -> bool {
+ fn is_null(&self, i: usize) -> bool {
self.data().is_null(i)
}
/// Returns whether the element at index `i` is not null
- fn is_valid(&self, i: i64) -> bool {
+ fn is_valid(&self, i: usize) -> bool {
self.data().is_valid(i)
}
/// Returns the total number of nulls in this array
- fn null_count(&self) -> i64 {
+ fn null_count(&self) -> usize {
self.data().null_count()
}
}
@@ -158,7 +158,7 @@ impl<T: ArrowPrimitiveType> Array for PrimitiveArray<T> {
/// Implementation for primitive arrays with numeric types.
/// Boolean arrays are bit-packed and so implemented separately.
impl<T: ArrowNumericType> PrimitiveArray<T> {
- pub fn new(length: i64, values: Buffer, null_count: i64, offset: i64) -> Self {
+ pub fn new(length: usize, values: Buffer, null_count: usize, offset: usize) -> Self {
let array_data = ArrayData::builder(T::get_data_type())
.len(length)
.add_buffer(values)
@@ -176,7 +176,7 @@ impl<T: ArrowNumericType> PrimitiveArray<T> {
}
/// Returns the length of this array
- pub fn len(&self) -> i64 {
+ pub fn len(&self) -> usize {
self.data.len()
}
@@ -188,16 +188,16 @@ impl<T: ArrowNumericType> PrimitiveArray<T> {
/// Returns the primitive value at index `i`.
///
/// Note this doesn't do any bound checking, for performance reason.
- pub fn value(&self, i: i64) -> T::Native {
+ pub fn value(&self, i: usize) -> T::Native {
unsafe { *(self.raw_values().offset(i as isize)) }
}
/// Returns a slice for the given offset and length
///
/// Note this doesn't do any bound checking, for performance reason.
- pub fn value_slice(&self, offset: i64, len: i64) -> &[T::Native] {
- let raw = unsafe { std::slice::from_raw_parts(self.raw_values(), self.len() as usize) };
- &raw[offset as usize..offset as usize + len as usize]
+ pub fn value_slice(&self, offset: usize, len: usize) -> &[T::Native] {
+ let raw = unsafe { std::slice::from_raw_parts(self.raw_values(), self.len()) };
+ &raw[offset..offset + len]
}
/// Returns the minimum value in the array, according to the natural order.
@@ -220,7 +220,7 @@ impl<T: ArrowNumericType> PrimitiveArray<T> {
if data.is_null(i) {
continue;
}
- let m = self.value(i as i64);
+ let m = self.value(i);
match n {
None => n = Some(m),
Some(nn) => {
@@ -234,14 +234,14 @@ impl<T: ArrowNumericType> PrimitiveArray<T> {
}
// Returns a new primitive array builder
- pub fn builder(capacity: i64) -> PrimitiveArrayBuilder<T> {
+ pub fn builder(capacity: usize) -> PrimitiveArrayBuilder<T> {
PrimitiveArrayBuilder::<T>::new(capacity)
}
}
/// Specific implementation for Boolean arrays due to bit-packing
impl PrimitiveArray<BooleanType> {
- pub fn new(length: i64, values: Buffer, null_count: i64, offset: i64) -> Self {
+ pub fn new(length: usize, values: Buffer, null_count: usize, offset: usize) -> Self {
let array_data = ArrayData::builder(DataType::Boolean)
.len(length)
.add_buffer(values)
@@ -259,14 +259,14 @@ impl PrimitiveArray<BooleanType> {
}
/// Returns the boolean value at index `i`.
- pub fn value(&self, i: i64) -> bool {
+ pub fn value(&self, i: usize) -> bool {
let offset = i + self.offset();
assert!(offset < self.data.len());
- unsafe { bit_util::get_bit_raw(self.raw_values.get() as *const u8, offset as usize) }
+ unsafe { bit_util::get_bit_raw(self.raw_values.get() as *const u8, offset) }
}
// Returns a new primitive array builder
- pub fn builder(capacity: i64) -> BooleanBuilder {
+ pub fn builder(capacity: usize) -> BooleanBuilder {
BooleanBuilder::new(capacity)
}
}
@@ -279,7 +279,7 @@ macro_rules! def_numeric_from_vec {
impl From<Vec<$native_ty>> for PrimitiveArray<$ty> {
fn from(data: Vec<$native_ty>) -> Self {
let array_data = ArrayData::builder($ty_id)
- .len(data.len() as i64)
+ .len(data.len())
.add_buffer(Buffer::from(data.to_byte_slice()))
.build();
PrimitiveArray::from(array_data)
@@ -290,7 +290,7 @@ macro_rules! def_numeric_from_vec {
impl From<Vec<Option<$native_ty>>> for PrimitiveArray<$ty> {
fn from(data: Vec<Option<$native_ty>>) -> Self {
let data_len = data.len();
- let num_bytes = bit_util::ceil(data_len as i64, 8) as usize;
+ let num_bytes = bit_util::ceil(data_len, 8);
let mut null_buf = MutableBuffer::new(num_bytes).with_bitset(num_bytes, false);
let mut val_buf = MutableBuffer::new(data_len * mem::size_of::<$native_ty>());
@@ -310,7 +310,7 @@ macro_rules! def_numeric_from_vec {
}
let array_data = ArrayData::builder($ty_id)
- .len(data_len as i64)
+ .len(data_len)
.add_buffer(val_buf.freeze())
.null_bit_buffer(null_buf.freeze())
.build();
@@ -334,7 +334,7 @@ def_numeric_from_vec!(Float64Type, f64, DataType::Float64);
/// Constructs a boolean array from a vector. Should only be used for testing.
impl From<Vec<bool>> for BooleanArray {
fn from(data: Vec<bool>) -> Self {
- let num_byte = bit_util::ceil(data.len() as i64, 8) as usize;
+ let num_byte = bit_util::ceil(data.len(), 8);
let mut mut_buf = MutableBuffer::new(num_byte).with_bitset(num_byte, false);
{
let mut_slice = mut_buf.data_mut();
@@ -345,7 +345,7 @@ impl From<Vec<bool>> for BooleanArray {
}
}
let array_data = ArrayData::builder(DataType::Boolean)
- .len(data.len() as i64)
+ .len(data.len())
.add_buffer(mut_buf.freeze())
.build();
BooleanArray::from(array_data)
@@ -354,8 +354,8 @@ impl From<Vec<bool>> for BooleanArray {
impl From<Vec<Option<bool>>> for BooleanArray {
fn from(data: Vec<Option<bool>>) -> Self {
- let data_len = data.len() as i64;
- let num_byte = bit_util::ceil(data_len, 8) as usize;
+ let data_len = data.len();
+ let num_byte = bit_util::ceil(data_len, 8);
let mut null_buf = MutableBuffer::new(num_byte).with_bitset(num_byte, false);
let mut val_buf = MutableBuffer::new(num_byte).with_bitset(num_byte, false);
@@ -425,7 +425,7 @@ impl ListArray {
///
/// Note this doesn't do any bound checking, for performance reason.
#[inline]
- pub fn value_offset(&self, i: i64) -> i32 {
+ pub fn value_offset(&self, i: usize) -> i32 {
self.value_offset_at(self.data.offset() + i)
}
@@ -433,13 +433,13 @@ impl ListArray {
///
/// Note this doesn't do any bound checking, for performance reason.
#[inline]
- pub fn value_length(&self, mut i: i64) -> i32 {
+ pub fn value_length(&self, mut i: usize) -> i32 {
i += self.data.offset();
self.value_offset_at(i + 1) - self.value_offset_at(i)
}
#[inline]
- fn value_offset_at(&self, i: i64) -> i32 {
+ fn value_offset_at(&self, i: usize) -> i32 {
unsafe { *self.value_offsets.get().offset(i as isize) }
}
}
@@ -503,11 +503,8 @@ pub struct BinaryArray {
impl BinaryArray {
/// Returns the element at index `i` as a byte slice.
- pub fn get_value(&self, i: i64) -> &[u8] {
- assert!(
- i >= 0 && i < self.data.len(),
- "BinaryArray out of bounds access"
- );
+ pub fn get_value(&self, i: usize) -> &[u8] {
+ assert!(i < self.data.len(), "BinaryArray out of bounds access");
let offset = i.checked_add(self.data.offset()).unwrap();
unsafe {
let pos = self.value_offset_at(offset);
@@ -521,7 +518,7 @@ impl BinaryArray {
/// Returns the element at index `i` as a string.
///
/// Note this doesn't do any bound checking, for performance reason.
- pub fn get_string(&self, i: i64) -> String {
+ pub fn get_string(&self, i: usize) -> String {
let slice = self.get_value(i);
unsafe { String::from_utf8_unchecked(Vec::from(slice)) }
}
@@ -530,7 +527,7 @@ impl BinaryArray {
///
/// Note this doesn't do any bound checking, for performance reason.
#[inline]
- pub fn value_offset(&self, i: i64) -> i32 {
+ pub fn value_offset(&self, i: usize) -> i32 {
self.value_offset_at(self.data.offset() + i)
}
@@ -538,13 +535,13 @@ impl BinaryArray {
///
/// Note this doesn't do any bound checking, for performance reason.
#[inline]
- pub fn value_length(&self, mut i: i64) -> i32 {
+ pub fn value_length(&self, mut i: usize) -> i32 {
i += self.data.offset();
self.value_offset_at(i + 1) - self.value_offset_at(i)
}
#[inline]
- fn value_offset_at(&self, i: i64) -> i32 {
+ fn value_offset_at(&self, i: usize) -> i32 {
unsafe { *self.value_offsets.get().offset(i as isize) }
}
}
@@ -582,7 +579,7 @@ impl<'a> From<Vec<&'a str>> for BinaryArray {
values.extend_from_slice(s.as_bytes());
}
let array_data = ArrayData::builder(DataType::Utf8)
- .len(v.len() as i64)
+ .len(v.len())
.add_buffer(Buffer::from(offsets.to_byte_slice()))
.add_buffer(Buffer::from(&values[..]))
.build();
@@ -664,7 +661,7 @@ impl Array for StructArray {
}
/// Returns the length (i.e., number of elements) of this array
- fn len(&self) -> i64 {
+ fn len(&self) -> usize {
self.boxed_fields[0].len()
}
}
@@ -876,8 +873,8 @@ mod tests {
assert_eq!(6, list_array.value_offset(2));
assert_eq!(2, list_array.value_length(2));
for i in 0..3 {
- assert!(list_array.is_valid(i as i64));
- assert!(!list_array.is_null(i as i64));
+ assert!(list_array.is_valid(i));
+ assert!(!list_array.is_null(i));
}
// Now test with a non-zero offset
@@ -991,8 +988,8 @@ mod tests {
assert_eq!(5, binary_array.value_offset(2));
assert_eq!(7, binary_array.value_length(2));
for i in 0..3 {
- assert!(binary_array.is_valid(i as i64));
- assert!(!binary_array.is_null(i as i64));
+ assert!(binary_array.is_valid(i));
+ assert!(!binary_array.is_null(i));
}
// Test binary array with offset
diff --git a/rust/src/array_data.rs b/rust/src/array_data.rs
index b288d4a804..36a817ee57 100644
--- a/rust/src/array_data.rs
+++ b/rust/src/array_data.rs
@@ -31,13 +31,13 @@ pub struct ArrayData {
data_type: DataType,
/// The number of elements in this array data
- len: i64,
+ len: usize,
/// The number of null elements in this array data
- null_count: i64,
+ null_count: usize,
/// The offset into this array data
- offset: i64,
+ offset: usize,
/// The buffers for this array data. Note that depending on the array types, this
/// could hold different kinds of buffers (e.g., value buffer, value offset buffer)
@@ -54,25 +54,28 @@ pub struct ArrayData {
}
pub type ArrayDataRef = Arc<ArrayData>;
-pub const UNKNOWN_NULL_COUNT: i64 = -1;
impl ArrayData {
pub fn new(
data_type: DataType,
- len: i64,
- mut null_count: i64,
+ len: usize,
+ null_count: Option<usize>,
null_bit_buffer: Option<Buffer>,
- offset: i64,
+ offset: usize,
buffers: Vec<Buffer>,
child_data: Vec<ArrayDataRef>,
) -> Self {
- if null_count < 0 {
- null_count = if let Some(ref buf) = null_bit_buffer {
- len - bit_util::count_set_bits_offset(buf.data(), offset as usize)
- } else {
- 0
- };
- }
+ let null_count = match null_count {
+ None => {
+ if let Some(ref buf) = null_bit_buffer {
+ len.checked_sub(bit_util::count_set_bits_offset(buf.data(), offset))
+ .unwrap()
+ } else {
+ 0
+ }
+ }
+ Some(null_count) => null_count,
+ };
let null_bitmap = null_bit_buffer.map(Bitmap::from);
Self {
data_type,
@@ -106,7 +109,7 @@ impl ArrayData {
}
/// Returns whether the element at index `i` is null
- pub fn is_null(&self, i: i64) -> bool {
+ pub fn is_null(&self, i: usize) -> bool {
if let Some(ref b) = self.null_bitmap {
return !b.is_set(i);
}
@@ -119,7 +122,7 @@ impl ArrayData {
}
/// Returns whether the element at index `i` is not null
- pub fn is_valid(&self, i: i64) -> bool {
+ pub fn is_valid(&self, i: usize) -> bool {
if let Some(ref b) = self.null_bitmap {
return b.is_set(i);
}
@@ -127,17 +130,17 @@ impl ArrayData {
}
/// Returns the length (i.e., number of elements) of this array
- pub fn len(&self) -> i64 {
+ pub fn len(&self) -> usize {
self.len
}
/// Returns the offset of this array
- pub fn offset(&self) -> i64 {
+ pub fn offset(&self) -> usize {
self.offset
}
/// Returns the total number of nulls in this array
- pub fn null_count(&self) -> i64 {
+ pub fn null_count(&self) -> usize {
self.null_count
}
}
@@ -145,10 +148,10 @@ impl ArrayData {
/// Builder for `ArrayData` type
pub struct ArrayDataBuilder {
data_type: DataType,
- len: i64,
- null_count: i64,
+ len: usize,
+ null_count: Option<usize>,
null_bit_buffer: Option<Buffer>,
- offset: i64,
+ offset: usize,
buffers: Vec<Buffer>,
child_data: Vec<ArrayDataRef>,
}
@@ -158,7 +161,7 @@ impl ArrayDataBuilder {
Self {
data_type,
len: 0,
- null_count: UNKNOWN_NULL_COUNT,
+ null_count: None,
null_bit_buffer: None,
offset: 0,
buffers: vec![],
@@ -166,13 +169,13 @@ impl ArrayDataBuilder {
}
}
- pub fn len(mut self, n: i64) -> Self {
+ pub fn len(mut self, n: usize) -> Self {
self.len = n;
self
}
- pub fn null_count(mut self, n: i64) -> Self {
- self.null_count = n;
+ pub fn null_count(mut self, n: usize) -> Self {
+ self.null_count = Some(n);
self
}
@@ -181,7 +184,7 @@ impl ArrayDataBuilder {
self
}
- pub fn offset(mut self, n: i64) -> Self {
+ pub fn offset(mut self, n: usize) -> Self {
self.offset = n;
self
}
@@ -230,7 +233,7 @@ mod tests {
#[test]
fn test_new() {
- let arr_data = ArrayData::new(DataType::Boolean, 10, 1, None, 2, vec![], vec![]);
+ let arr_data = ArrayData::new(DataType::Boolean, 10, Some(1), None, 2, vec![], vec![]);
assert_eq!(10, arr_data.len());
assert_eq!(1, arr_data.null_count());
assert_eq!(2, arr_data.offset());
@@ -244,7 +247,7 @@ mod tests {
let child_arr_data = Arc::new(ArrayData::new(
DataType::Int32,
10,
- 0,
+ Some(0),
None,
0,
vec![],
diff --git a/rust/src/bitmap.rs b/rust/src/bitmap.rs
index 742fac5587..3d5a77f78a 100644
--- a/rust/src/bitmap.rs
+++ b/rust/src/bitmap.rs
@@ -45,9 +45,9 @@ impl Bitmap {
self.bits.len()
}
- pub fn is_set(&self, i: i64) -> bool {
- assert!(i < (self.bits.len() << 3) as i64);
- unsafe { bit_util::get_bit_raw(self.bits.raw_data(), i as usize) }
+ pub fn is_set(&self, i: usize) -> bool {
+ assert!(i < (self.bits.len() << 3));
+ unsafe { bit_util::get_bit_raw(self.bits.raw_data(), i) }
}
}
diff --git a/rust/src/buffer.rs b/rust/src/buffer.rs
index 4b7d2a0d3c..b9c159f338 100644
--- a/rust/src/buffer.rs
+++ b/rust/src/buffer.rs
@@ -49,7 +49,7 @@ impl PartialEq for BufferData {
if self.len != other.len {
return false;
}
- unsafe { memory::memcmp(self.ptr, other.ptr, self.len as usize) == 0 }
+ unsafe { memory::memcmp(self.ptr, other.ptr, self.len) == 0 }
}
}
@@ -73,7 +73,7 @@ impl Buffer {
/// Returns the number of bytes in the buffer
pub fn len(&self) -> usize {
- self.data.len - self.offset as usize
+ self.data.len - self.offset
}
/// Returns whether the buffer is empty.
@@ -128,7 +128,7 @@ impl<T: AsRef<[u8]>> From<T> for Buffer {
// allocate aligned memory buffer
let slice = p.as_ref();
let len = slice.len() * mem::size_of::<u8>();
- let buffer = memory::allocate_aligned((len) as i64).unwrap();
+ let buffer = memory::allocate_aligned(len).unwrap();
unsafe {
memory::memcpy(buffer, slice.as_ptr(), len);
}
@@ -151,12 +151,12 @@ pub struct MutableBuffer {
impl MutableBuffer {
/// Allocate a new mutable buffer with initial capacity to be `capacity`.
pub fn new(capacity: usize) -> Self {
- let new_capacity = bit_util::round_upto_multiple_of_64(capacity as i64);
+ let new_capacity = bit_util::round_upto_multiple_of_64(capacity);
let ptr = memory::allocate_aligned(new_capacity).unwrap();
Self {
data: ptr,
len: 0,
- capacity: new_capacity as usize,
+ capacity: new_capacity,
}
}
@@ -193,8 +193,8 @@ impl MutableBuffer {
/// Returns the new capacity for this buffer.
pub fn reserve(&mut self, capacity: usize) -> Result<usize> {
if capacity > self.capacity {
- let new_capacity = bit_util::round_upto_multiple_of_64(capacity as i64);
- let new_capacity = cmp::max(new_capacity, self.capacity as i64 * 2) as usize;
+ let new_capacity = bit_util::round_upto_multiple_of_64(capacity);
+ let new_capacity = cmp::max(new_capacity, self.capacity * 2);
let new_data = memory::reallocate(self.capacity, new_capacity, self.data)?;
self.data = new_data as *mut u8;
self.capacity = new_capacity;
@@ -213,7 +213,7 @@ impl MutableBuffer {
if new_len > self.len {
self.reserve(new_len)?;
} else {
- let new_capacity = bit_util::round_upto_multiple_of_64(new_len as i64) as usize;
+ let new_capacity = bit_util::round_upto_multiple_of_64(new_len);
if new_capacity < self.capacity {
let new_data = memory::reallocate(self.capacity, new_capacity, self.data)?;
self.data = new_data as *mut u8;
@@ -287,7 +287,7 @@ impl PartialEq for MutableBuffer {
if self.len != other.len {
return false;
}
- unsafe { memory::memcmp(self.data, other.data, self.len as usize) == 0 }
+ unsafe { memory::memcmp(self.data, other.data, self.len) == 0 }
}
}
diff --git a/rust/src/builder.rs b/rust/src/builder.rs
index 2cbdce0c85..fc781ffa50 100644
--- a/rust/src/builder.rs
+++ b/rust/src/builder.rs
@@ -33,7 +33,7 @@ use crate::util::bit_util;
/// Buffer builder with zero-copy build method
pub struct BufferBuilder<T: ArrowPrimitiveType> {
buffer: MutableBuffer,
- len: i64,
+ len: usize,
_marker: PhantomData<T>,
}
@@ -53,11 +53,11 @@ pub type Float64BufferBuilder = BufferBuilder<Float64Type>;
// numeric types and boolean types, while still be able to call methods on buffer builder
// with generic primitive type.
pub trait BufferBuilderTrait<T: ArrowPrimitiveType> {
- fn new(capacity: i64) -> Self;
- fn len(&self) -> i64;
- fn capacity(&self) -> i64;
- fn advance(&mut self, i: i64) -> Result<()>;
- fn reserve(&mut self, n: i64) -> Result<()>;
+ fn new(capacity: usize) -> Self;
+ fn len(&self) -> usize;
+ fn capacity(&self) -> usize;
+ fn advance(&mut self, i: usize) -> Result<()>;
+ fn reserve(&mut self, n: usize) -> Result<()>;
fn push(&mut self, v: T::Native) -> Result<()>;
fn push_slice(&mut self, slice: &[T::Native]) -> Result<()>;
fn finish(self) -> Buffer;
@@ -65,8 +65,8 @@ pub trait BufferBuilderTrait<T: ArrowPrimitiveType> {
impl<T: ArrowPrimitiveType> BufferBuilderTrait<T> for BufferBuilder<T> {
/// Creates a builder with a fixed initial capacity
- default fn new(capacity: i64) -> Self {
- let buffer = MutableBuffer::new(capacity as usize * mem::size_of::<T::Native>());
+ default fn new(capacity: usize) -> Self {
+ let buffer = MutableBuffer::new(capacity * mem::size_of::<T::Native>());
Self {
buffer,
len: 0,
@@ -75,28 +75,28 @@ impl<T: ArrowPrimitiveType> BufferBuilderTrait<T> for BufferBuilder<T> {
}
/// Returns the number of array elements (slots) in the builder
- fn len(&self) -> i64 {
+ fn len(&self) -> usize {
self.len
}
/// Returns the current capacity of the builder (number of elements)
- fn capacity(&self) -> i64 {
+ fn capacity(&self) -> usize {
let bit_capacity = self.buffer.capacity() * 8;
- (bit_capacity / T::get_bit_width()) as i64
+ (bit_capacity / T::get_bit_width())
}
// Advances the `len` of the underlying `Buffer` by `i` slots of type T
- default fn advance(&mut self, i: i64) -> Result<()> {
- let new_buffer_len = (self.len + i) as usize * mem::size_of::<T::Native>();
+ default fn advance(&mut self, i: usize) -> Result<()> {
+ let new_buffer_len = (self.len + i) * mem::size_of::<T::Native>();
self.buffer.resize(new_buffer_len)?;
self.len += i;
Ok(())
}
/// Reserves memory for `n` elements of type `T`.
- default fn reserve(&mut self, n: i64) -> Result<()> {
+ default fn reserve(&mut self, n: usize) -> Result<()> {
let new_capacity = self.len + n;
- let byte_capacity = mem::size_of::<T::Native>() * new_capacity as usize;
+ let byte_capacity = mem::size_of::<T::Native>() * new_capacity;
self.buffer.reserve(byte_capacity)?;
Ok(())
}
@@ -109,7 +109,7 @@ impl<T: ArrowPrimitiveType> BufferBuilderTrait<T> for BufferBuilder<T> {
/// Pushes a slice of type `T`, growing the internal buffer as needed.
default fn push_slice(&mut self, slice: &[T::Native]) -> Result<()> {
- let array_slots = slice.len() as i64;
+ let array_slots = slice.len();
self.reserve(array_slots)?;
self.write_bytes(slice.to_byte_slice(), array_slots)
}
@@ -124,7 +124,7 @@ impl<T: ArrowPrimitiveType> BufferBuilder<T> {
/// Writes a byte slice to the underlying buffer and updates the `len`, i.e. the number array
/// elements in the builder. Also, converts the `io::Result` required by the `Write` trait
/// to the Arrow `Result` type.
- fn write_bytes(&mut self, bytes: &[u8], len_added: i64) -> Result<()> {
+ fn write_bytes(&mut self, bytes: &[u8], len_added: usize) -> Result<()> {
let write_result = self.buffer.write(bytes);
// `io::Result` has many options one of which we use, so pattern matching is overkill here
if write_result.is_err() {
@@ -140,9 +140,9 @@ impl<T: ArrowPrimitiveType> BufferBuilder<T> {
impl BufferBuilderTrait<BooleanType> for BufferBuilder<BooleanType> {
/// Creates a builder with a fixed initial capacity.
- fn new(capacity: i64) -> Self {
+ fn new(capacity: usize) -> Self {
let byte_capacity = bit_util::ceil(capacity, 8);
- let actual_capacity = bit_util::round_upto_multiple_of_64(byte_capacity) as usize;
+ let actual_capacity = bit_util::round_upto_multiple_of_64(byte_capacity);
let mut buffer = MutableBuffer::new(actual_capacity);
buffer.set_null_bits(0, actual_capacity);
Self {
@@ -153,9 +153,9 @@ impl BufferBuilderTrait<BooleanType> for BufferBuilder<BooleanType> {
}
// Advances the `len` of the underlying `Buffer` by `i` slots of type T
- fn advance(&mut self, i: i64) -> Result<()> {
+ fn advance(&mut self, i: usize) -> Result<()> {
let new_buffer_len = bit_util::ceil(self.len + i, 8);
- self.buffer.resize(new_buffer_len as usize)?;
+ self.buffer.resize(new_buffer_len)?;
self.len += i;
Ok(())
}
@@ -167,7 +167,7 @@ impl BufferBuilderTrait<BooleanType> for BufferBuilder<BooleanType> {
// For performance the `len` of the buffer is not updated on each push but
// is updated in the `freeze` method instead.
unsafe {
- bit_util::set_bit_raw(self.buffer.raw_data() as *mut u8, (self.len) as usize);
+ bit_util::set_bit_raw(self.buffer.raw_data() as *mut u8, self.len);
}
}
self.len += 1;
@@ -184,10 +184,10 @@ impl BufferBuilderTrait<BooleanType> for BufferBuilder<BooleanType> {
}
/// Reserves memory for `n` elements of type `T`.
- fn reserve(&mut self, n: i64) -> Result<()> {
+ fn reserve(&mut self, n: usize) -> Result<()> {
let new_capacity = self.len + n;
if new_capacity > self.capacity() {
- let new_byte_capacity = bit_util::ceil(new_capacity, 8) as usize;
+ let new_byte_capacity = bit_util::ceil(new_capacity, 8);
let existing_capacity = self.buffer.capacity();
let new_capacity = self.buffer.reserve(new_byte_capacity)?;
self.buffer
@@ -199,7 +199,7 @@ impl BufferBuilderTrait<BooleanType> for BufferBuilder<BooleanType> {
/// Consumes this and returns an immutable `Buffer`.
fn finish(mut self) -> Buffer {
// `push` does not update the buffer's `len` so do it before `freeze` is called.
- let new_buffer_len = bit_util::ceil(self.len, 8) as usize;
+ let new_buffer_len = bit_util::ceil(self.len, 8);
debug_assert!(new_buffer_len >= self.buffer.len());
self.buffer.resize(new_buffer_len).unwrap();
self.buffer.freeze()
@@ -216,7 +216,7 @@ pub trait ArrayBuilder {
fn into_any(self) -> Box<Any>;
/// Returns the number of array slots in the builder
- fn len(&self) -> i64;
+ fn len(&self) -> usize;
/// Builds the array
fn finish(self) -> Self::ArrayType;
@@ -250,7 +250,7 @@ impl<T: ArrowPrimitiveType> ArrayBuilder for PrimitiveArrayBuilder<T> {
}
/// Returns the number of array slots in the builder
- fn len(&self) -> i64 {
+ fn len(&self) -> usize {
self.values_builder.len
}
@@ -270,7 +270,7 @@ impl<T: ArrowPrimitiveType> ArrayBuilder for PrimitiveArrayBuilder<T> {
impl<T: ArrowPrimitiveType> PrimitiveArrayBuilder<T> {
/// Creates a new primitive array builder
- pub fn new(capacity: i64) -> Self {
+ pub fn new(capacity: usize) -> Self {
Self {
values_builder: BufferBuilder::<T>::new(capacity),
bitmap_builder: BooleanBufferBuilder::new(capacity),
@@ -278,7 +278,7 @@ impl<T: ArrowPrimitiveType> PrimitiveArrayBuilder<T> {
}
/// Returns the capacity of this builder measured in slots of type `T`
- pub fn capacity(&self) -> i64 {
+ pub fn capacity(&self) -> usize {
self.values_builder.capacity()
}
@@ -318,7 +318,7 @@ pub struct ListArrayBuilder<T: ArrayBuilder> {
offsets_builder: Int32BufferBuilder,
bitmap_builder: BooleanBufferBuilder,
values_builder: T,
- len: i64,
+ len: usize,
}
impl<T: ArrayBuilder> ListArrayBuilder<T> {
@@ -348,7 +348,7 @@ where
}
/// Returns the number of array slots in the builder
- fn len(&self) -> i64 {
+ fn len(&self) -> usize {
self.len
}
@@ -410,7 +410,7 @@ impl ArrayBuilder for BinaryArrayBuilder {
}
/// Returns the number of array slots in the builder
- fn len(&self) -> i64 {
+ fn len(&self) -> usize {
self.builder.len()
}
@@ -422,7 +422,7 @@ impl ArrayBuilder for BinaryArrayBuilder {
impl BinaryArrayBuilder {
/// Creates a new `BinaryArrayBuilder`, `capacity` is the number of bytes in the values array
- pub fn new(capacity: i64) -> Self {
+ pub fn new(capacity: usize) -> Self {
let values_builder = UInt8Builder::new(capacity);
Self {
builder: ListArrayBuilder::new(values_builder),
@@ -736,8 +736,8 @@ mod tests {
assert_eq!(6, list_array.value_offset(2));
assert_eq!(2, list_array.value_length(2));
for i in 0..3 {
- assert!(list_array.is_valid(i as i64));
- assert!(!list_array.is_null(i as i64));
+ assert!(list_array.is_valid(i));
+ assert!(!list_array.is_null(i));
}
}
diff --git a/rust/src/csv/reader.rs b/rust/src/csv/reader.rs
index 697ace653b..956408e4a4 100644
--- a/rust/src/csv/reader.rs
+++ b/rust/src/csv/reader.rs
@@ -44,13 +44,15 @@ use std::fs::File;
use std::io::BufReader;
use std::sync::Arc;
+use csv as csv_crate;
+
use crate::array::{ArrayRef, BinaryArray};
use crate::builder::*;
use crate::datatypes::*;
use crate::error::{ArrowError, Result};
use crate::record_batch::RecordBatch;
-use csv_crate::{StringRecord, StringRecordsIntoIter};
+use self::csv_crate::{StringRecord, StringRecordsIntoIter};
/// CSV file reader
pub struct Reader {
@@ -91,7 +93,7 @@ fn build_primitive_array<T: ArrowPrimitiveType>(
rows: &[StringRecord],
col_idx: &usize,
) -> Result<ArrayRef> {
- let mut builder = PrimitiveArrayBuilder::<T>::new(rows.len() as i64);
+ let mut builder = PrimitiveArrayBuilder::<T>::new(rows.len());
for row_index in 0..rows.len() {
match rows[row_index].get(*col_idx) {
Some(s) if s.len() > 0 => match s.parse::<T::Native>() {
@@ -161,7 +163,7 @@ impl Reader {
&DataType::Float32 => build_primitive_array::<Float32Type>(rows, i),
&DataType::Float64 => build_primitive_array::<Float64Type>(rows, i),
&DataType::Utf8 => {
- let values_builder: UInt8Builder = UInt8Builder::new(rows.len() as i64);
+ let values_builder: UInt8Builder = UInt8Builder::new(rows.len());
let mut list_builder = ListArrayBuilder::new(values_builder);
for row_index in 0..rows.len() {
match rows[row_index].get(*i) {
diff --git a/rust/src/memory.rs b/rust/src/memory.rs
index 193eff12d6..763cb48f50 100644
--- a/rust/src/memory.rs
+++ b/rust/src/memory.rs
@@ -31,7 +31,7 @@ extern "C" {
}
#[cfg(windows)]
-pub fn allocate_aligned(size: i64) -> Result<*mut u8> {
+pub fn allocate_aligned(size: usize) -> Result<*mut u8> {
let page = unsafe { _aligned_malloc(size as libc::size_t, ALIGNMENT as libc::size_t) };
match page {
0 => Err(ArrowError::MemoryError(
@@ -42,10 +42,10 @@ pub fn allocate_aligned(size: i64) -> Result<*mut u8> {
}
#[cfg(not(windows))]
-pub fn allocate_aligned(size: i64) -> Result<*mut u8> {
+pub fn allocate_aligned(size: usize) -> Result<*mut u8> {
unsafe {
let mut page: *mut libc::c_void = mem::uninitialized();
- let result = libc::posix_memalign(&mut page, ALIGNMENT, size as usize);
+ let result = libc::posix_memalign(&mut page, ALIGNMENT, size);
match result {
0 => Ok(mem::transmute::<*mut libc::c_void, *mut u8>(page)),
_ => Err(ArrowError::MemoryError(
@@ -72,7 +72,7 @@ pub fn free_aligned(p: *const u8) {
pub fn reallocate(old_size: usize, new_size: usize, pointer: *const u8) -> Result<*const u8> {
unsafe {
let old_src = mem::transmute::<*const u8, *mut libc::c_void>(pointer);
- let result = allocate_aligned(new_size as i64)?;
+ let result = allocate_aligned(new_size)?;
let dst = mem::transmute::<*const u8, *mut libc::c_void>(result);
libc::memcpy(dst, old_src, cmp::min(old_size, new_size));
free_aligned(pointer);
diff --git a/rust/src/record_batch.rs b/rust/src/record_batch.rs
index 4cb5c8e7db..2666770460 100644
--- a/rust/src/record_batch.rs
+++ b/rust/src/record_batch.rs
@@ -52,7 +52,7 @@ impl RecordBatch {
self.columns.len()
}
- pub fn num_rows(&self) -> i64 {
+ pub fn num_rows(&self) -> usize {
self.columns[0].data().len()
}
diff --git a/rust/src/tensor.rs b/rust/src/tensor.rs
index ec56aeb4cc..175b68d81f 100644
--- a/rust/src/tensor.rs
+++ b/rust/src/tensor.rs
@@ -23,30 +23,30 @@ use crate::buffer::Buffer;
use crate::datatypes::*;
/// Computes the strides required assuming a row major memory layout
-fn compute_row_major_strides<T: ArrowPrimitiveType>(shape: &Vec<i64>) -> Vec<i64> {
+fn compute_row_major_strides<T: ArrowPrimitiveType>(shape: &Vec<usize>) -> Vec<usize> {
let mut remaining_bytes = mem::size_of::<T::Native>();
for i in shape {
remaining_bytes = remaining_bytes
- .checked_mul(*i as usize)
+ .checked_mul(*i)
.expect("Overflow occurred when computing row major strides.");
}
- let mut strides = Vec::<i64>::new();
+ let mut strides = Vec::<usize>::new();
for i in shape {
- remaining_bytes /= *i as usize;
- strides.push(remaining_bytes as i64);
+ remaining_bytes /= *i;
+ strides.push(remaining_bytes);
}
strides
}
/// Computes the strides required assuming a column major memory layout
-fn compute_column_major_strides<T: ArrowPrimitiveType>(shape: &Vec<i64>) -> Vec<i64> {
+fn compute_column_major_strides<T: ArrowPrimitiveType>(shape: &Vec<usize>) -> Vec<usize> {
let mut remaining_bytes = mem::size_of::<T::Native>();
- let mut strides = Vec::<i64>::new();
+ let mut strides = Vec::<usize>::new();
for i in shape {
- strides.push(remaining_bytes as i64);
+ strides.push(remaining_bytes);
remaining_bytes = remaining_bytes
- .checked_mul(*i as usize)
+ .checked_mul(*i)
.expect("Overflow occurred when computing column major strides.");
}
strides
@@ -56,8 +56,8 @@ fn compute_column_major_strides<T: ArrowPrimitiveType>(shape: &Vec<i64>) -> Vec<
pub struct Tensor<'a, T: ArrowPrimitiveType> {
data_type: DataType,
buffer: Buffer,
- shape: Option<Vec<i64>>,
- strides: Option<Vec<i64>>,
+ shape: Option<Vec<usize>>,
+ strides: Option<Vec<usize>>,
names: Option<Vec<&'a str>>,
_marker: PhantomData<T>,
}
@@ -78,8 +78,8 @@ impl<'a, T: ArrowPrimitiveType> Tensor<'a, T> {
/// Creates a new `Tensor`
pub fn new(
buffer: Buffer,
- shape: Option<Vec<i64>>,
- strides: Option<Vec<i64>>,
+ shape: Option<Vec<usize>>,
+ strides: Option<Vec<usize>>,
names: Option<Vec<&'a str>>,
) -> Self {
match &shape {
@@ -122,7 +122,7 @@ impl<'a, T: ArrowPrimitiveType> Tensor<'a, T> {
/// Creates a new Tensor using row major memory layout
pub fn new_row_major(
buffer: Buffer,
- shape: Option<Vec<i64>>,
+ shape: Option<Vec<usize>>,
names: Option<Vec<&'a str>>,
) -> Self {
let strides = match &shape {
@@ -135,7 +135,7 @@ impl<'a, T: ArrowPrimitiveType> Tensor<'a, T> {
/// Creates a new Tensor using column major memory layout
pub fn new_column_major(
buffer: Buffer,
- shape: Option<Vec<i64>>,
+ shape: Option<Vec<usize>>,
names: Option<Vec<&'a str>>,
) -> Self {
let strides = match &shape {
@@ -151,7 +151,7 @@ impl<'a, T: ArrowPrimitiveType> Tensor<'a, T> {
}
/// The sizes of the dimensions
- pub fn shape(&self) -> Option<&Vec<i64>> {
+ pub fn shape(&self) -> Option<&Vec<usize>> {
self.shape.as_ref()
}
@@ -161,7 +161,7 @@ impl<'a, T: ArrowPrimitiveType> Tensor<'a, T> {
}
/// The number of bytes between elements in each dimension
- pub fn strides(&self) -> Option<&Vec<i64>> {
+ pub fn strides(&self) -> Option<&Vec<usize>> {
self.strides.as_ref()
}
@@ -171,24 +171,24 @@ impl<'a, T: ArrowPrimitiveType> Tensor<'a, T> {
}
/// The number of dimensions
- pub fn ndim(&self) -> i64 {
+ pub fn ndim(&self) -> usize {
match &self.shape {
None => 0,
- Some(v) => v.len() as i64,
+ Some(v) => v.len(),
}
}
/// The name of dimension i
- pub fn dim_name(&self, i: i64) -> Option<&'a str> {
+ pub fn dim_name(&self, i: usize) -> Option<&'a str> {
match &self.names {
None => None,
- Some(ref names) => Some(&names[i as usize]),
+ Some(ref names) => Some(&names[i]),
}
}
/// The total number of elements in the `Tensor`
- pub fn size(&self) -> i64 {
- (self.buffer.len() / mem::size_of::<T::Native>()) as i64
+ pub fn size(&self) -> usize {
+ (self.buffer.len() / mem::size_of::<T::Native>())
}
/// Indicates if the data is laid out contiguously in memory
@@ -223,15 +223,15 @@ mod tests {
fn test_compute_row_major_strides() {
assert_eq!(
vec![48, 8],
- compute_row_major_strides::<Int64Type>(&vec![4_i64, 6])
+ compute_row_major_strides::<Int64Type>(&vec![4_usize, 6])
);
assert_eq!(
vec![24, 4],
- compute_row_major_strides::<Int32Type>(&vec![4_i64, 6])
+ compute_row_major_strides::<Int32Type>(&vec![4_usize, 6])
);
assert_eq!(
vec![6, 1],
- compute_row_major_strides::<Int8Type>(&vec![4_i64, 6])
+ compute_row_major_strides::<Int8Type>(&vec![4_usize, 6])
);
}
@@ -239,15 +239,15 @@ mod tests {
fn test_compute_column_major_strides() {
assert_eq!(
vec![8, 32],
- compute_column_major_strides::<Int64Type>(&vec![4_i64, 6])
+ compute_column_major_strides::<Int64Type>(&vec![4_usize, 6])
);
assert_eq!(
vec![4, 16],
- compute_column_major_strides::<Int32Type>(&vec![4_i64, 6])
+ compute_column_major_strides::<Int32Type>(&vec![4_usize, 6])
);
assert_eq!(
vec![1, 4],
- compute_column_major_strides::<Int8Type>(&vec![4_i64, 6])
+ compute_column_major_strides::<Int8Type>(&vec![4_usize, 6])
);
}
@@ -283,7 +283,7 @@ mod tests {
let buf = builder.finish();
let tensor = Int32Tensor::new(buf, Some(vec![2, 8]), None, None);
assert_eq!(16, tensor.size());
- assert_eq!(Some(vec![2_i64, 8]).as_ref(), tensor.shape());
+ assert_eq!(Some(vec![2_usize, 8]).as_ref(), tensor.shape());
assert_eq!(None, tensor.strides());
assert_eq!(2, tensor.ndim());
assert_eq!(None, tensor.names());
@@ -298,8 +298,8 @@ mod tests {
let buf = builder.finish();
let tensor = Int32Tensor::new_row_major(buf, Some(vec![2, 8]), None);
assert_eq!(16, tensor.size());
- assert_eq!(Some(vec![2_i64, 8]).as_ref(), tensor.shape());
- assert_eq!(Some(vec![32_i64, 4]).as_ref(), tensor.strides());
+ assert_eq!(Some(vec![2_usize, 8]).as_ref(), tensor.shape());
+ assert_eq!(Some(vec![32_usize, 4]).as_ref(), tensor.strides());
assert_eq!(None, tensor.names());
assert_eq!(2, tensor.ndim());
assert_eq!(true, tensor.is_row_major());
@@ -316,8 +316,8 @@ mod tests {
let buf = builder.finish();
let tensor = Int32Tensor::new_column_major(buf, Some(vec![2, 8]), None);
assert_eq!(16, tensor.size());
- assert_eq!(Some(vec![2_i64, 8]).as_ref(), tensor.shape());
- assert_eq!(Some(vec![4_i64, 8]).as_ref(), tensor.strides());
+ assert_eq!(Some(vec![2_usize, 8]).as_ref(), tensor.shape());
+ assert_eq!(Some(vec![4_usize, 8]).as_ref(), tensor.strides());
assert_eq!(None, tensor.names());
assert_eq!(2, tensor.ndim());
assert_eq!(false, tensor.is_row_major());
@@ -335,8 +335,8 @@ mod tests {
let names = vec!["Dim 1", "Dim 2"];
let tensor = Int64Tensor::new_column_major(buf, Some(vec![2, 4]), Some(names));
assert_eq!(8, tensor.size());
- assert_eq!(Some(vec![2_i64, 4]).as_ref(), tensor.shape());
- assert_eq!(Some(vec![8_i64, 16]).as_ref(), tensor.strides());
+ assert_eq!(Some(vec![2_usize, 4]).as_ref(), tensor.shape());
+ assert_eq!(Some(vec![8_usize, 16]).as_ref(), tensor.strides());
assert_eq!("Dim 1", tensor.dim_name(0).unwrap());
assert_eq!("Dim 2", tensor.dim_name(1).unwrap());
assert_eq!(2, tensor.ndim());
diff --git a/rust/src/util/bit_util.rs b/rust/src/util/bit_util.rs
index da6d10d269..3f7f4cb573 100644
--- a/rust/src/util/bit_util.rs
+++ b/rust/src/util/bit_util.rs
@@ -30,13 +30,13 @@ static POPCOUNT_TABLE: [u8; 256] = [
/// Returns the nearest number that is `>=` than `num` and is a multiple of 64
#[inline]
-pub fn round_upto_multiple_of_64(num: i64) -> i64 {
+pub fn round_upto_multiple_of_64(num: usize) -> usize {
round_upto_power_of_2(num, 64)
}
/// Returns the nearest multiple of `factor` that is `>=` than `num`. Here `factor` must
/// be a power of 2.
-fn round_upto_power_of_2(num: i64, factor: i64) -> i64 {
+fn round_upto_power_of_2(num: usize, factor: usize) -> usize {
debug_assert!(factor > 0 && (factor & (factor - 1)) == 0);
(num + (factor - 1)) & !(factor - 1)
}
@@ -73,20 +73,20 @@ pub unsafe fn set_bit_raw(data: *mut u8, i: usize) {
/// Returns the number of 1-bits in `data`
#[inline]
-pub fn count_set_bits(data: &[u8]) -> i64 {
- let mut count: i64 = 0;
+pub fn count_set_bits(data: &[u8]) -> usize {
+ let mut count: usize = 0;
for u in data {
- count += POPCOUNT_TABLE[*u as usize] as i64;
+ count += POPCOUNT_TABLE[*u as usize] as usize;
}
count
}
/// Returns the number of 1-bits in `data`, starting from `offset`.
#[inline]
-pub fn count_set_bits_offset(data: &[u8], offset: usize) -> i64 {
+pub fn count_set_bits_offset(data: &[u8], offset: usize) -> usize {
debug_assert!(offset <= (data.len() << 3));
- let start_byte_pos = (offset >> 3) as usize;
+ let start_byte_pos = offset >> 3;
let start_bit_pos = offset & 7;
if start_bit_pos == 0 {
@@ -95,7 +95,7 @@ pub fn count_set_bits_offset(data: &[u8], offset: usize) -> i64 {
let mut result = 0;
result += count_set_bits(&data[start_byte_pos + 1..]);
for i in start_bit_pos..8 {
- if get_bit(&data[start_byte_pos..start_byte_pos + 1], i as usize) {
+ if get_bit(&data[start_byte_pos..start_byte_pos + 1], i) {
result += 1;
}
}
@@ -105,7 +105,7 @@ pub fn count_set_bits_offset(data: &[u8], offset: usize) -> i64 {
/// Returns the ceil of `value`/`divisor`
#[inline]
-pub fn ceil(value: i64, divisor: i64) -> i64 {
+pub fn ceil(value: usize, divisor: usize) -> usize {
let mut result = value / divisor;
if value % divisor != 0 {
result += 1