polars_core/chunked_array/ops/
any_value.rs

1#![allow(unsafe_op_in_unsafe_fn)]
2
3#[cfg(feature = "object")]
4use crate::chunked_array::object::extension::polars_extension::PolarsExtension;
5use crate::prelude::*;
6use crate::series::implementations::null::NullChunked;
7use crate::utils::index_to_chunked_index;
8
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::Float32 => downcast_and_pack!(Float32Array, Float32),
49        DataType::Float64 => downcast_and_pack!(Float64Array, Float64),
50        DataType::List(dt) => {
51            let v: ArrayRef = downcast!(LargeListArray);
52            if dt.is_primitive() {
53                let s = Series::from_chunks_and_dtype_unchecked(PlSmallStr::EMPTY, vec![v], dt);
54                AnyValue::List(s)
55            } else {
56                let s = Series::from_chunks_and_dtype_unchecked(
57                    PlSmallStr::EMPTY,
58                    vec![v],
59                    &dt.to_physical(),
60                )
61                .from_physical_unchecked(dt)
62                .unwrap();
63                AnyValue::List(s)
64            }
65        },
66        #[cfg(feature = "dtype-array")]
67        DataType::Array(dt, width) => {
68            let v: ArrayRef = downcast!(FixedSizeListArray);
69            if dt.is_primitive() {
70                let s = Series::from_chunks_and_dtype_unchecked(PlSmallStr::EMPTY, vec![v], dt);
71                AnyValue::Array(s, *width)
72            } else {
73                let s = Series::from_chunks_and_dtype_unchecked(
74                    PlSmallStr::EMPTY,
75                    vec![v],
76                    &dt.to_physical(),
77                )
78                .from_physical_unchecked(dt)
79                .unwrap();
80                AnyValue::Array(s, *width)
81            }
82        },
83        #[cfg(feature = "dtype-categorical")]
84        DataType::Categorical(cats, mapping) => {
85            with_match_categorical_physical_type!(cats.physical(), |$C| {
86                type A = <$C as PolarsDataType>::Array;
87                let arr = &*(arr as *const dyn Array as *const A);
88                let cat_id = arr.value_unchecked(idx).as_cat();
89                AnyValue::Categorical(cat_id, mapping)
90            })
91        },
92        #[cfg(feature = "dtype-categorical")]
93        DataType::Enum(fcats, mapping) => {
94            with_match_categorical_physical_type!(fcats.physical(), |$C| {
95                type A = <$C as PolarsDataType>::Array;
96                let arr = &*(arr as *const dyn Array as *const A);
97                let cat_id = arr.value_unchecked(idx).as_cat();
98                AnyValue::Enum(cat_id, mapping)
99            })
100        },
101        #[cfg(feature = "dtype-struct")]
102        DataType::Struct(flds) => {
103            let arr = &*(arr as *const dyn Array as *const StructArray);
104            AnyValue::Struct(idx, arr, flds)
105        },
106        #[cfg(feature = "dtype-datetime")]
107        DataType::Datetime(tu, tz) => {
108            let arr = &*(arr as *const dyn Array as *const Int64Array);
109            let v = arr.value_unchecked(idx);
110            AnyValue::Datetime(v, *tu, tz.as_ref())
111        },
112        #[cfg(feature = "dtype-date")]
113        DataType::Date => {
114            let arr = &*(arr as *const dyn Array as *const Int32Array);
115            let v = arr.value_unchecked(idx);
116            AnyValue::Date(v)
117        },
118        #[cfg(feature = "dtype-duration")]
119        DataType::Duration(tu) => {
120            let arr = &*(arr as *const dyn Array as *const Int64Array);
121            let v = arr.value_unchecked(idx);
122            AnyValue::Duration(v, *tu)
123        },
124        #[cfg(feature = "dtype-time")]
125        DataType::Time => {
126            let arr = &*(arr as *const dyn Array as *const Int64Array);
127            let v = arr.value_unchecked(idx);
128            AnyValue::Time(v)
129        },
130        #[cfg(feature = "dtype-decimal")]
131        DataType::Decimal(precision, scale) => {
132            let arr = &*(arr as *const dyn Array as *const Int128Array);
133            let v = arr.value_unchecked(idx);
134            AnyValue::Decimal(v, *precision, *scale)
135        },
136        #[cfg(feature = "object")]
137        DataType::Object(_) => {
138            // We should almost never hit this. The only known exception is when we put objects in
139            // structs. Any other hit should be considered a bug.
140            let arr = arr.as_any().downcast_ref::<FixedSizeBinaryArray>().unwrap();
141            PolarsExtension::arr_to_av(arr, idx)
142        },
143        DataType::Null => AnyValue::Null,
144        DataType::BinaryOffset => downcast_and_pack!(LargeBinaryArray, Binary),
145        dt => panic!("not implemented for {dt:?}"),
146    }
147}
148
149#[cfg(feature = "dtype-struct")]
150impl<'a> AnyValue<'a> {
151    pub fn _iter_struct_av(&self) -> impl Iterator<Item = AnyValue<'_>> {
152        let AnyValue::Struct(idx, arr, flds) = self else {
153            unreachable!()
154        };
155        unsafe {
156            arr.values()
157                .iter()
158                .zip(*flds)
159                .map(move |(arr, fld)| arr_to_any_value(&**arr, *idx, fld.dtype()))
160        }
161    }
162
163    pub fn _materialize_struct_av(&'a self, buf: &mut Vec<AnyValue<'a>>) {
164        let iter = self._iter_struct_av();
165        buf.extend(iter)
166    }
167}
168
169macro_rules! get_any_value_unchecked {
170    ($self:ident, $index:expr) => {{
171        let (chunk_idx, idx) = $self.index_to_chunked_index($index);
172        debug_assert!(chunk_idx < $self.chunks.len());
173        let arr = &**$self.chunks.get_unchecked(chunk_idx);
174        debug_assert!(idx < arr.len());
175        arr_to_any_value(arr, idx, $self.dtype())
176    }};
177}
178
179macro_rules! get_any_value {
180    ($self:ident, $index:expr) => {{
181        if $index >= $self.len() {
182            polars_bail!(oob = $index, $self.len());
183        }
184        // SAFETY:
185        // bounds are checked
186        Ok(unsafe { $self.get_any_value_unchecked($index) })
187    }};
188}
189
190impl<T> ChunkAnyValue for ChunkedArray<T>
191where
192    T: PolarsNumericType,
193{
194    #[inline]
195    unsafe fn get_any_value_unchecked(&self, index: usize) -> AnyValue<'_> {
196        get_any_value_unchecked!(self, index)
197    }
198
199    fn get_any_value(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
200        get_any_value!(self, index)
201    }
202}
203
204impl ChunkAnyValue for BooleanChunked {
205    #[inline]
206    unsafe fn get_any_value_unchecked(&self, index: usize) -> AnyValue<'_> {
207        get_any_value_unchecked!(self, index)
208    }
209
210    fn get_any_value(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
211        get_any_value!(self, index)
212    }
213}
214
215impl ChunkAnyValue for StringChunked {
216    #[inline]
217    unsafe fn get_any_value_unchecked(&self, index: usize) -> AnyValue<'_> {
218        get_any_value_unchecked!(self, index)
219    }
220
221    fn get_any_value(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
222        get_any_value!(self, index)
223    }
224}
225
226impl ChunkAnyValue for BinaryChunked {
227    #[inline]
228    unsafe fn get_any_value_unchecked(&self, index: usize) -> AnyValue<'_> {
229        get_any_value_unchecked!(self, index)
230    }
231
232    fn get_any_value(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
233        get_any_value!(self, index)
234    }
235}
236
237impl ChunkAnyValue for BinaryOffsetChunked {
238    #[inline]
239    unsafe fn get_any_value_unchecked(&self, index: usize) -> AnyValue<'_> {
240        get_any_value_unchecked!(self, index)
241    }
242
243    fn get_any_value(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
244        get_any_value!(self, index)
245    }
246}
247
248impl ChunkAnyValue for ListChunked {
249    #[inline]
250    unsafe fn get_any_value_unchecked(&self, index: usize) -> AnyValue<'_> {
251        get_any_value_unchecked!(self, index)
252    }
253
254    fn get_any_value(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
255        get_any_value!(self, index)
256    }
257}
258
259#[cfg(feature = "dtype-array")]
260impl ChunkAnyValue for ArrayChunked {
261    #[inline]
262    unsafe fn get_any_value_unchecked(&self, index: usize) -> AnyValue<'_> {
263        get_any_value_unchecked!(self, index)
264    }
265
266    fn get_any_value(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
267        get_any_value!(self, index)
268    }
269}
270
271#[cfg(feature = "object")]
272impl<T: PolarsObject> ChunkAnyValue for ObjectChunked<T> {
273    #[inline]
274    unsafe fn get_any_value_unchecked(&self, index: usize) -> AnyValue<'_> {
275        match self.get_object_unchecked(index) {
276            None => AnyValue::Null,
277            Some(v) => AnyValue::Object(v),
278        }
279    }
280
281    fn get_any_value(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
282        get_any_value!(self, index)
283    }
284}
285
286impl ChunkAnyValue for NullChunked {
287    #[inline]
288    unsafe fn get_any_value_unchecked(&self, _index: usize) -> AnyValue<'_> {
289        AnyValue::Null
290    }
291
292    fn get_any_value(&self, _index: usize) -> PolarsResult<AnyValue<'_>> {
293        Ok(AnyValue::Null)
294    }
295}
296
297#[cfg(feature = "dtype-struct")]
298impl ChunkAnyValue for StructChunked {
299    /// Gets AnyValue from LogicalType
300    fn get_any_value(&self, i: usize) -> PolarsResult<AnyValue<'_>> {
301        polars_ensure!(i < self.len(), oob = i, self.len());
302        unsafe { Ok(self.get_any_value_unchecked(i)) }
303    }
304
305    unsafe fn get_any_value_unchecked(&self, i: usize) -> AnyValue<'_> {
306        let (chunk_idx, idx) = index_to_chunked_index(self.chunks.iter().map(|c| c.len()), i);
307        if let DataType::Struct(flds) = self.dtype() {
308            // SAFETY: we already have a single chunk and we are
309            // guarded by the type system.
310            unsafe {
311                let arr = &**self.chunks.get_unchecked(chunk_idx);
312                let arr = &*(arr as *const dyn Array as *const StructArray);
313
314                if arr.is_null_unchecked(idx) {
315                    AnyValue::Null
316                } else {
317                    AnyValue::Struct(idx, arr, flds)
318                }
319            }
320        } else {
321            unreachable!()
322        }
323    }
324}