Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Removed unneeded unsafe (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao authored Jan 11, 2022
1 parent 2b0e807 commit 79bb05c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
27 changes: 16 additions & 11 deletions src/compute/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use crate::{
array::*,
buffer::Buffer,
datatypes::*,
error::{ArrowError, Result},
};
Expand Down Expand Up @@ -290,9 +289,12 @@ fn cast_list<O: Offset>(
}

fn cast_list_to_large_list(array: &ListArray<i32>, to_type: &DataType) -> ListArray<i64> {
let offsets = array.offsets();
let offsets = offsets.iter().map(|x| *x as i64);
let offets = Buffer::from_trusted_len_iter(offsets);
let offets = array
.offsets()
.iter()
.map(|x| *x as i64)
.collect::<Vec<_>>()
.into();

ListArray::<i64>::from_data(
to_type.clone(),
Expand All @@ -303,13 +305,16 @@ fn cast_list_to_large_list(array: &ListArray<i32>, to_type: &DataType) -> ListAr
}

fn cast_large_to_list(array: &ListArray<i64>, to_type: &DataType) -> ListArray<i32> {
let offsets = array.offsets();
let offsets = offsets.iter().map(|x| *x as i32);
let offets = Buffer::from_trusted_len_iter(offsets);
let offsets = array
.offsets()
.iter()
.map(|x| *x as i32)
.collect::<Vec<_>>()
.into();

ListArray::<i32>::from_data(
to_type.clone(),
offets,
offsets,
array.values().clone(),
array.validity().cloned(),
)
Expand Down Expand Up @@ -385,10 +390,10 @@ pub fn cast(array: &dyn Array, to_type: &DataType, options: CastOptions) -> Resu
// cast primitive to list's primitive
let values = cast(array, &to.data_type, options)?.into();
// create offsets, where if array.len() = 2, we have [0,1,2]
let offsets =
unsafe { Buffer::from_trusted_len_iter_unchecked(0..=array.len() as i32) };
let offsets = (0..=array.len() as i32).collect::<Vec<_>>();

let list_array = ListArray::<i32>::from_data(to_type.clone(), offsets, values, None);
let list_array =
ListArray::<i32>::from_data(to_type.clone(), offsets.into(), values, None);

Ok(Box::new(list_array))
}
Expand Down
32 changes: 21 additions & 11 deletions src/compute/cast/utf8_to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use std::convert::TryFrom;

use chrono::Datelike;

use crate::{array::*, buffer::Buffer, datatypes::DataType, types::NativeType};
use crate::{
array::*,
datatypes::DataType,
error::{ArrowError, Result},
temporal_conversions::{
utf8_to_naive_timestamp_ns as utf8_to_naive_timestamp_ns_,
utf8_to_timestamp_ns as utf8_to_timestamp_ns_, EPOCH_DAYS_FROM_CE,
},
types::NativeType,
};

use super::CastOptions;
Expand Down Expand Up @@ -145,24 +147,32 @@ pub fn utf8_to_timestamp_ns<O: Offset>(
/// Conversion of utf8
pub fn utf8_to_large_utf8(from: &Utf8Array<i32>) -> Utf8Array<i64> {
let data_type = Utf8Array::<i64>::default_data_type();
let validity = from.validity().cloned();
let values = from.values().clone();
let offsets = from.offsets().iter().map(|x| *x as i64);
let offsets = Buffer::from_trusted_len_iter(offsets);
unsafe {
Utf8Array::<i64>::from_data_unchecked(data_type, offsets, values, from.validity().cloned())
}
let offsets = from
.offsets()
.iter()
.map(|x| *x as i64)
.collect::<Vec<_>>()
.into();
// Safety: sound because `offsets` fulfills the same invariants as `from.offsets()`
unsafe { Utf8Array::<i64>::from_data_unchecked(data_type, offsets, values, validity) }
}

/// Conversion of utf8
pub fn utf8_large_to_utf8(from: &Utf8Array<i64>) -> Result<Utf8Array<i32>> {
let data_type = Utf8Array::<i32>::default_data_type();
let validity = from.validity().cloned();
let values = from.values().clone();
let _ =
i32::try_from(*from.offsets().last().unwrap()).map_err(ArrowError::from_external_error)?;

let offsets = from.offsets().iter().map(|x| *x as i32);
let offsets = Buffer::from_trusted_len_iter(offsets);
Ok(unsafe {
Utf8Array::<i32>::from_data_unchecked(data_type, offsets, values, from.validity().cloned())
})
let offsets = from
.offsets()
.iter()
.map(|x| *x as i32)
.collect::<Vec<_>>()
.into();
// Safety: sound because `offsets` fulfills the same invariants as `from.offsets()`
Ok(unsafe { Utf8Array::<i32>::from_data_unchecked(data_type, offsets, values, validity) })
}

0 comments on commit 79bb05c

Please sign in to comment.