Skip to content

Commit

Permalink
feat: add "future" versioning (#17421)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruihe774 committed Jul 7, 2024
1 parent e7915d8 commit ad836bd
Show file tree
Hide file tree
Showing 79 changed files with 501 additions and 265 deletions.
10 changes: 7 additions & 3 deletions crates/polars-core/src/chunked_array/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ impl ArrayChunked {
) -> PolarsResult<ArrayChunked> {
// Rechunk or the generated Series will have wrong length.
let ca = self.rechunk();
let field = self.inner_dtype().to_arrow_field("item", true);
let field = self
.inner_dtype()
.to_arrow_field("item", CompatLevel::newest());

let chunks = ca.downcast_iter().map(|arr| {
let elements = unsafe {
Expand All @@ -66,8 +68,10 @@ impl ArrayChunked {
let out = out.rechunk();
let values = out.chunks()[0].clone();

let inner_dtype =
FixedSizeListArray::default_datatype(out.dtype().to_arrow(true), ca.width());
let inner_dtype = FixedSizeListArray::default_datatype(
out.dtype().to_arrow(CompatLevel::newest()),
ca.width(),
);
let arr = FixedSizeListArray::new(inner_dtype, values, arr.validity().cloned());
Ok(arr)
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl FixedSizeListBuilder for AnonymousOwnedFixedSizeListBuilder {
.finish(
self.inner_dtype
.as_ref()
.map(|dt| dt.to_arrow(true))
.map(|dt| dt.to_arrow(CompatLevel::newest()))
.as_ref(),
)
.unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl<'a> AnonymousListBuilder<'a> {

let inner_dtype_physical = inner_dtype
.as_ref()
.map(|dt| dt.to_physical().to_arrow(true));
.map(|dt| dt.to_physical().to_arrow(CompatLevel::newest()));
let arr = slf.builder.finish(inner_dtype_physical.as_ref()).unwrap();

let list_dtype_logical = match inner_dtype {
Expand Down Expand Up @@ -157,7 +157,7 @@ impl ListBuilderTrait for AnonymousOwnedListBuilder {
let slf = std::mem::take(self);
let inner_dtype_physical = inner_dtype
.as_ref()
.map(|dt| dt.to_physical().to_arrow(true));
.map(|dt| dt.to_physical().to_arrow(CompatLevel::newest()));
let arr = slf.builder.finish(inner_dtype_physical.as_ref()).unwrap();

let list_dtype_logical = match inner_dtype {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ where
) -> Self {
let values = MutablePrimitiveArray::<T::Native>::with_capacity_from(
values_capacity,
values_type.to_arrow(true),
values_type.to_arrow(CompatLevel::newest()),
);
let builder = LargePrimitiveBuilder::<T::Native>::new_with_capacity(values, capacity);
let field = Field::new(name, DataType::List(Box::new(logical_type)));
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/chunked_array/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ where
T: PolarsNumericType,
{
fn from_slice(name: &str, v: &[T::Native]) -> Self {
let arr = PrimitiveArray::from_slice(v).to(T::get_dtype().to_arrow(true));
let arr = PrimitiveArray::from_slice(v).to(T::get_dtype().to_arrow(CompatLevel::newest()));
ChunkedArray::with_chunk(name, arr)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/chunked_array/builder/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
{
pub fn new(name: &str, capacity: usize) -> Self {
let array_builder = MutablePrimitiveArray::<T::Native>::with_capacity(capacity)
.to(T::get_dtype().to_arrow(true));
.to(T::get_dtype().to_arrow(CompatLevel::newest()));

PrimitiveChunkedBuilder {
array_builder,
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/chunked_array/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub(crate) fn cast_chunks(
let check_nulls = matches!(options, CastOptions::Strict);
let options = options.into();

let arrow_dtype = dtype.try_to_arrow(true)?;
let arrow_dtype = dtype.try_to_arrow(CompatLevel::newest())?;
chunks
.iter()
.map(|arr| {
Expand Down
19 changes: 11 additions & 8 deletions crates/polars-core/src/chunked_array/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::chunked_array::ChunkedArray;
use crate::datatypes::{
ArrayCollectIterExt, ArrayFromIter, ArrayFromIterDtype, DataType, Field, PolarsDataType,
};
use crate::prelude::CompatLevel;

pub trait ChunkedCollectIterExt<T: PolarsDataType>: Iterator + Sized {
#[inline]
Expand All @@ -26,7 +27,7 @@ pub trait ChunkedCollectIterExt<T: PolarsDataType>: Iterator + Sized {
T::Array: ArrayFromIterDtype<Self::Item>,
{
let field = Arc::new(Field::new(name, dtype.clone()));
let arr = self.collect_arr_with_dtype(field.dtype.to_arrow(true));
let arr = self.collect_arr_with_dtype(field.dtype.to_arrow(CompatLevel::newest()));
ChunkedArray::from_chunk_iter_and_field(field, [arr])
}

Expand All @@ -36,7 +37,7 @@ pub trait ChunkedCollectIterExt<T: PolarsDataType>: Iterator + Sized {
T::Array: ArrayFromIterDtype<Self::Item>,
{
let field = Arc::clone(&name_dtype_src.field);
let arr = self.collect_arr_with_dtype(field.dtype.to_arrow(true));
let arr = self.collect_arr_with_dtype(field.dtype.to_arrow(CompatLevel::newest()));
ChunkedArray::from_chunk_iter_and_field(field, [arr])
}

Expand All @@ -47,7 +48,7 @@ pub trait ChunkedCollectIterExt<T: PolarsDataType>: Iterator + Sized {
Self: TrustedLen,
{
let field = Arc::new(Field::new(name, dtype.clone()));
let arr = self.collect_arr_trusted_with_dtype(field.dtype.to_arrow(true));
let arr = self.collect_arr_trusted_with_dtype(field.dtype.to_arrow(CompatLevel::newest()));
ChunkedArray::from_chunk_iter_and_field(field, [arr])
}

Expand All @@ -58,7 +59,7 @@ pub trait ChunkedCollectIterExt<T: PolarsDataType>: Iterator + Sized {
Self: TrustedLen,
{
let field = Arc::clone(&name_dtype_src.field);
let arr = self.collect_arr_trusted_with_dtype(field.dtype.to_arrow(true));
let arr = self.collect_arr_trusted_with_dtype(field.dtype.to_arrow(CompatLevel::newest()));
ChunkedArray::from_chunk_iter_and_field(field, [arr])
}

Expand All @@ -73,7 +74,7 @@ pub trait ChunkedCollectIterExt<T: PolarsDataType>: Iterator + Sized {
Self: Iterator<Item = Result<U, E>>,
{
let field = Arc::new(Field::new(name, dtype.clone()));
let arr = self.try_collect_arr_with_dtype(field.dtype.to_arrow(true))?;
let arr = self.try_collect_arr_with_dtype(field.dtype.to_arrow(CompatLevel::newest()))?;
Ok(ChunkedArray::from_chunk_iter_and_field(field, [arr]))
}

Expand All @@ -87,7 +88,7 @@ pub trait ChunkedCollectIterExt<T: PolarsDataType>: Iterator + Sized {
Self: Iterator<Item = Result<U, E>>,
{
let field = Arc::clone(&name_dtype_src.field);
let arr = self.try_collect_arr_with_dtype(field.dtype.to_arrow(true))?;
let arr = self.try_collect_arr_with_dtype(field.dtype.to_arrow(CompatLevel::newest()))?;
Ok(ChunkedArray::from_chunk_iter_and_field(field, [arr]))
}

Expand All @@ -102,7 +103,8 @@ pub trait ChunkedCollectIterExt<T: PolarsDataType>: Iterator + Sized {
Self: Iterator<Item = Result<U, E>> + TrustedLen,
{
let field = Arc::new(Field::new(name, dtype.clone()));
let arr = self.try_collect_arr_trusted_with_dtype(field.dtype.to_arrow(true))?;
let arr =
self.try_collect_arr_trusted_with_dtype(field.dtype.to_arrow(CompatLevel::newest()))?;
Ok(ChunkedArray::from_chunk_iter_and_field(field, [arr]))
}

Expand All @@ -116,7 +118,8 @@ pub trait ChunkedCollectIterExt<T: PolarsDataType>: Iterator + Sized {
Self: Iterator<Item = Result<U, E>> + TrustedLen,
{
let field = Arc::clone(&name_dtype_src.field);
let arr = self.try_collect_arr_trusted_with_dtype(field.dtype.to_arrow(true))?;
let arr =
self.try_collect_arr_trusted_with_dtype(field.dtype.to_arrow(CompatLevel::newest()))?;
Ok(ChunkedArray::from_chunk_iter_and_field(field, [arr]))
}
}
Expand Down
10 changes: 8 additions & 2 deletions crates/polars-core/src/chunked_array/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ where
#[cfg(debug_assertions)]
{
if !chunks.is_empty() && !chunks[0].is_empty() && dtype.is_primitive() {
assert_eq!(chunks[0].data_type(), &dtype.to_arrow(true))
assert_eq!(
chunks[0].data_type(),
&dtype.to_arrow(CompatLevel::newest())
)
}
}
let field = Arc::new(Field::new(name, dtype));
Expand All @@ -234,7 +237,10 @@ where
}

pub fn full_null_like(ca: &Self, length: usize) -> Self {
let chunks = std::iter::once(T::Array::full_null(length, ca.dtype().to_arrow(true)));
let chunks = std::iter::once(T::Array::full_null(
length,
ca.dtype().to_arrow(CompatLevel::newest()),
));
Self::from_chunk_iter_like(ca, chunks)
}
}
Expand Down
26 changes: 13 additions & 13 deletions crates/polars-core/src/chunked_array/logical/categorical/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ use arrow::datatypes::IntegerType;

use super::*;

fn convert_values(arr: &Utf8ViewArray, pl_flavor: bool) -> ArrayRef {
if pl_flavor {
fn convert_values(arr: &Utf8ViewArray, compat_level: CompatLevel) -> ArrayRef {
if compat_level.0 >= 1 {
arr.clone().boxed()
} else {
utf8view_to_utf8::<i64>(arr).boxed()
}
}

impl CategoricalChunked {
pub fn to_arrow(&self, pl_flavor: bool, as_i64: bool) -> ArrayRef {
pub fn to_arrow(&self, compat_level: CompatLevel, as_i64: bool) -> ArrayRef {
if as_i64 {
self.to_i64(pl_flavor).boxed()
self.to_i64(compat_level).boxed()
} else {
self.to_u32(pl_flavor).boxed()
self.to_u32(compat_level).boxed()
}
}

fn to_u32(&self, pl_flavor: bool) -> DictionaryArray<u32> {
let values_dtype = if pl_flavor {
fn to_u32(&self, compat_level: CompatLevel) -> DictionaryArray<u32> {
let values_dtype = if compat_level.0 >= 1 {
ArrowDataType::Utf8View
} else {
ArrowDataType::LargeUtf8
Expand All @@ -32,7 +32,7 @@ impl CategoricalChunked {
let dtype = ArrowDataType::Dictionary(IntegerType::UInt32, Box::new(values_dtype), false);
match map {
RevMapping::Local(arr, _) => {
let values = convert_values(arr, pl_flavor);
let values = convert_values(arr, compat_level);

// SAFETY:
// the keys are in bounds
Expand All @@ -44,7 +44,7 @@ impl CategoricalChunked {
.map(|opt_k| opt_k.map(|k| *reverse_map.get(k).unwrap()));
let keys = PrimitiveArray::from_trusted_len_iter(iter);

let values = convert_values(values, pl_flavor);
let values = convert_values(values, compat_level);

// SAFETY:
// the keys are in bounds
Expand All @@ -53,8 +53,8 @@ impl CategoricalChunked {
}
}

fn to_i64(&self, pl_flavor: bool) -> DictionaryArray<i64> {
let values_dtype = if pl_flavor {
fn to_i64(&self, compat_level: CompatLevel) -> DictionaryArray<i64> {
let values_dtype = if compat_level.0 >= 1 {
ArrowDataType::Utf8View
} else {
ArrowDataType::LargeUtf8
Expand All @@ -65,7 +65,7 @@ impl CategoricalChunked {
let dtype = ArrowDataType::Dictionary(IntegerType::Int64, Box::new(values_dtype), false);
match map {
RevMapping::Local(arr, _) => {
let values = convert_values(arr, pl_flavor);
let values = convert_values(arr, compat_level);

// SAFETY:
// the keys are in bounds
Expand All @@ -89,7 +89,7 @@ impl CategoricalChunked {
.map(|opt_k| opt_k.map(|k| *reverse_map.get(k).unwrap() as i64));
let keys = PrimitiveArray::from_trusted_len_iter(iter);

let values = convert_values(values, pl_flavor);
let values = convert_values(values, compat_level);

// SAFETY:
// the keys are in bounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ mod test {
let ca = ca.cast(&DataType::Categorical(None, Default::default()))?;
let ca = ca.categorical().unwrap();

let arr = ca.to_arrow(true, false);
let arr = ca.to_arrow(CompatLevel::newest(), false);
let s = Series::try_from(("foo", arr))?;
assert!(matches!(s.dtype(), &DataType::Categorical(_, _)));
assert_eq!(s.null_count(), 1);
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/chunked_array/logical/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Int128Chunked {
let (_, values, validity) = default.into_inner();

*arr = PrimitiveArray::new(
DataType::Decimal(precision, Some(scale)).to_arrow(true),
DataType::Decimal(precision, Some(scale)).to_arrow(CompatLevel::newest()),
values,
validity,
);
Expand Down
10 changes: 5 additions & 5 deletions crates/polars-core/src/chunked_array/logical/struct_/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ fn fields_to_struct_array(fields: &[Series], physical: bool) -> (ArrayRef, Vec<S
let s = s.rechunk();
match s.dtype() {
#[cfg(feature = "object")]
DataType::Object(_, _) => s.to_arrow(0, true),
DataType::Object(_, _) => s.to_arrow(0, CompatLevel::newest()),
_ => {
if physical {
s.chunks()[0].clone()
} else {
s.to_arrow(0, true)
s.to_arrow(0, CompatLevel::newest())
}
},
}
Expand Down Expand Up @@ -145,7 +145,7 @@ impl StructChunked {
.iter()
.map(|s| match s.dtype() {
#[cfg(feature = "object")]
DataType::Object(_, _) => s.to_arrow(i, true),
DataType::Object(_, _) => s.to_arrow(i, CompatLevel::newest()),
_ => s.chunks()[i].clone(),
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -295,11 +295,11 @@ impl StructChunked {
self.into()
}

pub(crate) fn to_arrow(&self, i: usize, pl_flavor: bool) -> ArrayRef {
pub(crate) fn to_arrow(&self, i: usize, compat_level: CompatLevel) -> ArrayRef {
let values = self
.fields
.iter()
.map(|s| s.to_arrow(i, pl_flavor))
.map(|s| s.to_arrow(i, compat_level))
.collect::<Vec<_>>();

// we determine fields from arrays as there might be object arrays
Expand Down
6 changes: 5 additions & 1 deletion crates/polars-core/src/chunked_array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,11 @@ pub(crate) fn to_primitive<T: PolarsNumericType>(
values: Vec<T::Native>,
validity: Option<Bitmap>,
) -> PrimitiveArray<T::Native> {
PrimitiveArray::new(T::get_dtype().to_arrow(true), values.into(), validity)
PrimitiveArray::new(
T::get_dtype().to_arrow(CompatLevel::newest()),
values.into(),
validity,
)
}

pub(crate) fn to_array<T: PolarsNumericType>(
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-core/src/chunked_array/ops/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ where
let out: U::Array = arr
.values_iter()
.map(&mut op)
.collect_arr_with_dtype(dtype.to_arrow(true));
.collect_arr_with_dtype(dtype.to_arrow(CompatLevel::newest()));
out.with_validity_typed(arr.validity().cloned())
} else {
let out: U::Array = arr
.iter()
.map(|opt| opt.map(&mut op))
.collect_arr_with_dtype(dtype.to_arrow(true));
.collect_arr_with_dtype(dtype.to_arrow(CompatLevel::newest()));
out.with_validity_typed(arr.validity().cloned())
}
});
Expand Down Expand Up @@ -133,7 +133,7 @@ where
drop(arr);

let compute_immutable = |arr: &PrimitiveArray<S::Native>| {
arrow::compute::arity::unary(arr, f, S::get_dtype().to_arrow(true))
arrow::compute::arity::unary(arr, f, S::get_dtype().to_arrow(CompatLevel::newest()))
};

if owned_arr.values().is_sliced() {
Expand Down
Loading

0 comments on commit ad836bd

Please sign in to comment.