polars_core/chunked_array/ops/
any_value.rs

1#![allow(unsafe_op_in_unsafe_fn)]
2
3use crate::prelude::*;
4use crate::series::implementations::null::NullChunked;
5use crate::utils::index_to_chunked_index;
6
7/// # Safety
8/// `idx` MUST be in-bounds for `arr` and `dtype` has to match the data stored in `arr`.
9#[inline]
10#[allow(unused_variables)]
11pub(crate) unsafe fn arr_to_any_value<'a>(
12    arr: &'a dyn Array,
13    idx: usize,
14    dtype: &'a DataType,
15) -> AnyValue<'a> {
16    debug_assert!(idx < arr.len());
17    if arr.is_null(idx) {
18        return AnyValue::Null;
19    }
20
21    macro_rules! downcast_and_pack {
22        ($casttype:ident, $variant:ident) => {{
23            let arr = &*(arr as *const dyn Array as *const $casttype);
24            let v = arr.value_unchecked(idx);
25            AnyValue::$variant(v)
26        }};
27    }
28    macro_rules! downcast {
29        ($casttype:ident) => {{
30            let arr = &*(arr as *const dyn Array as *const $casttype);
31            arr.value_unchecked(idx)
32        }};
33    }
34    match dtype {
35        DataType::String => downcast_and_pack!(Utf8ViewArray, String),
36        DataType::Binary => downcast_and_pack!(BinaryViewArray, Binary),
37        DataType::Boolean => downcast_and_pack!(BooleanArray, Boolean),
38        DataType::UInt8 => downcast_and_pack!(UInt8Array, UInt8),
39        DataType::UInt16 => downcast_and_pack!(UInt16Array, UInt16),
40        DataType::UInt32 => downcast_and_pack!(UInt32Array, UInt32),
41        DataType::UInt64 => downcast_and_pack!(UInt64Array, UInt64),
42        DataType::UInt128 => downcast_and_pack!(UInt128Array, UInt128),
43        DataType::Int8 => downcast_and_pack!(Int8Array, Int8),
44        DataType::Int16 => downcast_and_pack!(Int16Array, Int16),
45        DataType::Int32 => downcast_and_pack!(Int32Array, Int32),
46        DataType::Int64 => downcast_and_pack!(Int64Array, Int64),
47        DataType::Int128 => downcast_and_pack!(Int128Array, Int128),
48        DataType::Float16 => downcast_and_pack!(Float16Array, Float16),
49        DataType::Float32 => downcast_and_pack!(Float32Array, Float32),
50        DataType::Float64 => downcast_and_pack!(Float64Array, Float64),
51        DataType::List(dt) => {
52            let v: ArrayRef = downcast!(LargeListArray);
53            if dt.is_primitive() {
54                let s = Series::from_chunks_and_dtype_unchecked(PlSmallStr::EMPTY, vec![v], dt);
55                AnyValue::List(s)
56            } else {
57                let s = Series::from_chunks_and_dtype_unchecked(
58                    PlSmallStr::EMPTY,
59                    vec![v],
60                    &dt.to_physical(),
61                )
62                .from_physical_unchecked(dt)
63                .unwrap();
64                AnyValue::List(s)
65            }
66        },
67        #[cfg(feature = "dtype-array")]
68        DataType::Array(dt, width) => {
69            let v: ArrayRef = downcast!(FixedSizeListArray);
70            if dt.is_primitive() {
71                let s = Series::from_chunks_and_dtype_unchecked(PlSmallStr::EMPTY, vec![v], dt);
72                AnyValue::Array(s, *width)
73            } else {
74                let s = Series::from_chunks_and_dtype_unchecked(
75                    PlSmallStr::EMPTY,
76                    vec![v],
77                    &dt.to_physical(),
78                )
79                .from_physical_unchecked(dt)
80                .unwrap();
81                AnyValue::Array(s, *width)
82            }
83        },
84        #[cfg(feature = "dtype-categorical")]
85        DataType::Categorical(cats, mapping) => {
86            with_match_categorical_physical_type!(cats.physical(), |$C| {
87                type A = <$C as PolarsDataType>::Array;
88                let arr = &*(arr as *const dyn Array as *const A);
89                let cat_id = arr.value_unchecked(idx).as_cat();
90                AnyValue::Categorical(cat_id, mapping)
91            })
92        },
93        #[cfg(feature = "dtype-categorical")]
94        DataType::Enum(fcats, mapping) => {
95            with_match_categorical_physical_type!(fcats.physical(), |$C| {
96                type A = <$C as PolarsDataType>::Array;
97                let arr = &*(arr as *const dyn Array as *const A);
98                let cat_id = arr.value_unchecked(idx).as_cat();
99                AnyValue::Enum(cat_id, mapping)
100            })
101        },
102        #[cfg(feature = "dtype-struct")]
103        DataType::Struct(flds) => {
104            let arr = &*(arr as *const dyn Array as *const StructArray);
105            AnyValue::Struct(idx, arr, flds)
106        },
107        #[cfg(feature = "dtype-datetime")]
108        DataType::Datetime(tu, tz) => {
109            let arr = &*(arr as *const dyn Array as *const Int64Array);
110            let v = arr.value_unchecked(idx);
111            AnyValue::Datetime(v, *tu, tz.as_ref())
112        },
113        #[cfg(feature = "dtype-date")]
114        DataType::Date => {
115            let arr = &*(arr as *const dyn Array as *const Int32Array);
116            let v = arr.value_unchecked(idx);
117            AnyValue::Date(v)
118        },
119        #[cfg(feature = "dtype-duration")]
120        DataType::Duration(tu) => {
121            let arr = &*(arr as *const dyn Array as *const Int64Array);
122            let v = arr.value_unchecked(idx);
123            AnyValue::Duration(v, *tu)
124        },
125        #[cfg(feature = "dtype-time")]
126        DataType::Time => {
127            let arr = &*(arr as *const dyn Array as *const Int64Array);
128            let v = arr.value_unchecked(idx);
129            AnyValue::Time(v)
130        },
131        #[cfg(feature = "dtype-decimal")]
132        DataType::Decimal(precision, scale) => {
133            let arr = &*(arr as *const dyn Array as *const Int128Array);
134            let v = arr.value_unchecked(idx);
135            AnyValue::Decimal(v, *precision, *scale)
136        },
137        #[cfg(feature = "dtype-extension")]
138        DataType::Extension(typ, storage) => arr_to_any_value(arr, idx, storage),
139        #[cfg(feature = "object")]
140        DataType::Object(_) => {
141            use crate::chunked_array::object::registry::get_object_array_getter;
142            get_object_array_getter()(arr, idx).unwrap()
143        },
144        DataType::Null => AnyValue::Null,
145        DataType::BinaryOffset => downcast_and_pack!(LargeBinaryArray, Binary),
146        dt => panic!("not implemented for {dt:?}"),
147    }
148}
149
150#[cfg(feature = "dtype-struct")]
151impl<'a> AnyValue<'a> {
152    pub fn _iter_struct_av(&self) -> impl Iterator<Item = AnyValue<'_>> {
153        let AnyValue::Struct(idx, arr, flds) = self else {
154            unreachable!()
155        };
156        unsafe {
157            arr.values()
158                .iter()
159                .zip(*flds)
160                .map(move |(arr, fld)| arr_to_any_value(&**arr, *idx, fld.dtype()))
161        }
162    }
163
164    pub fn _materialize_struct_av(&'a self, buf: &mut Vec<AnyValue<'a>>) {
165        let iter = self._iter_struct_av();
166        buf.extend(iter)
167    }
168}
169
170macro_rules! get_any_value_unchecked {
171    ($self:ident, $index:expr) => {{
172        let (chunk_idx, idx) = $self.index_to_chunked_index($index);
173        debug_assert!(chunk_idx < $self.chunks.len());
174        let arr = &**$self.chunks.get_unchecked(chunk_idx);
175        debug_assert!(idx < arr.len());
176        arr_to_any_value(arr, idx, $self.dtype())
177    }};
178}
179
180macro_rules! get_any_value {
181    ($self:ident, $index:expr) => {{
182        if $index >= $self.len() {
183            polars_bail!(oob = $index, $self.len());
184        }
185        // SAFETY:
186        // bounds are checked
187        Ok(unsafe { $self.get_any_value_unchecked($index) })
188    }};
189}
190
191impl<T> ChunkAnyValue for ChunkedArray<T>
192where
193    T: PolarsNumericType,
194{
195    #[inline]
196    unsafe fn get_any_value_unchecked(&self, index: usize) -> AnyValue<'_> {
197        get_any_value_unchecked!(self, index)
198    }
199
200    fn get_any_value(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
201        get_any_value!(self, index)
202    }
203}
204
205impl ChunkAnyValue for BooleanChunked {
206    #[inline]
207    unsafe fn get_any_value_unchecked(&self, index: usize) -> AnyValue<'_> {
208        get_any_value_unchecked!(self, index)
209    }
210
211    fn get_any_value(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
212        get_any_value!(self, index)
213    }
214}
215
216impl ChunkAnyValue for StringChunked {
217    #[inline]
218    unsafe fn get_any_value_unchecked(&self, index: usize) -> AnyValue<'_> {
219        get_any_value_unchecked!(self, index)
220    }
221
222    fn get_any_value(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
223        get_any_value!(self, index)
224    }
225}
226
227impl ChunkAnyValue for BinaryChunked {
228    #[inline]
229    unsafe fn get_any_value_unchecked(&self, index: usize) -> AnyValue<'_> {
230        get_any_value_unchecked!(self, index)
231    }
232
233    fn get_any_value(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
234        get_any_value!(self, index)
235    }
236}
237
238impl ChunkAnyValue for BinaryOffsetChunked {
239    #[inline]
240    unsafe fn get_any_value_unchecked(&self, index: usize) -> AnyValue<'_> {
241        get_any_value_unchecked!(self, index)
242    }
243
244    fn get_any_value(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
245        get_any_value!(self, index)
246    }
247}
248
249impl ChunkAnyValueBypassValidity for BinaryOffsetChunked {
250    #[inline]
251    unsafe fn get_any_value_bypass_validity(&self, index: usize) -> AnyValue<'_> {
252        debug_assert!(index < self.len());
253        let (chunk_idx, idx) = self.index_to_chunked_index(index);
254        debug_assert!(chunk_idx < self.chunks.len());
255        let arr = &**self.chunks.get_unchecked(chunk_idx);
256        let arr = &*(arr as *const dyn Array as *const LargeBinaryArray);
257        let v = arr.value_unchecked(idx);
258        AnyValue::Binary(v)
259    }
260}
261
262impl ChunkAnyValue for ListChunked {
263    #[inline]
264    unsafe fn get_any_value_unchecked(&self, index: usize) -> AnyValue<'_> {
265        get_any_value_unchecked!(self, index)
266    }
267
268    fn get_any_value(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
269        get_any_value!(self, index)
270    }
271}
272
273#[cfg(feature = "dtype-array")]
274impl ChunkAnyValue for ArrayChunked {
275    #[inline]
276    unsafe fn get_any_value_unchecked(&self, index: usize) -> AnyValue<'_> {
277        get_any_value_unchecked!(self, index)
278    }
279
280    fn get_any_value(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
281        get_any_value!(self, index)
282    }
283}
284
285#[cfg(feature = "object")]
286impl<T: PolarsObject> ChunkAnyValue for ObjectChunked<T> {
287    #[inline]
288    unsafe fn get_any_value_unchecked(&self, index: usize) -> AnyValue<'_> {
289        match self.get_object_unchecked(index) {
290            None => AnyValue::Null,
291            Some(v) => AnyValue::Object(v),
292        }
293    }
294
295    fn get_any_value(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
296        get_any_value!(self, index)
297    }
298}
299
300impl ChunkAnyValue for NullChunked {
301    #[inline]
302    unsafe fn get_any_value_unchecked(&self, _index: usize) -> AnyValue<'_> {
303        AnyValue::Null
304    }
305
306    fn get_any_value(&self, _index: usize) -> PolarsResult<AnyValue<'_>> {
307        Ok(AnyValue::Null)
308    }
309}
310
311#[cfg(feature = "dtype-struct")]
312impl ChunkAnyValue for StructChunked {
313    /// Gets AnyValue from LogicalType
314    fn get_any_value(&self, i: usize) -> PolarsResult<AnyValue<'_>> {
315        polars_ensure!(i < self.len(), oob = i, self.len());
316        unsafe { Ok(self.get_any_value_unchecked(i)) }
317    }
318
319    unsafe fn get_any_value_unchecked(&self, i: usize) -> AnyValue<'_> {
320        let (chunk_idx, idx) = index_to_chunked_index(self.chunks.iter().map(|c| c.len()), i);
321        if let DataType::Struct(flds) = self.dtype() {
322            // SAFETY: we already have a single chunk and we are
323            // guarded by the type system.
324            unsafe {
325                let arr = &**self.chunks.get_unchecked(chunk_idx);
326                let arr = &*(arr as *const dyn Array as *const StructArray);
327
328                if arr.is_null_unchecked(idx) {
329                    AnyValue::Null
330                } else {
331                    AnyValue::Struct(idx, arr, flds)
332                }
333            }
334        } else {
335            unreachable!()
336        }
337    }
338}