Skip to main content

polars_core/chunked_array/object/extension/
mod.rs

1pub(crate) mod drop;
2pub(super) mod list;
3pub(crate) mod polars_extension;
4
5use std::mem;
6
7use arrow::array::FixedSizeBinaryArray;
8use arrow::bitmap::BitmapBuilder;
9use arrow::datatypes::ExtensionType;
10use polars_buffer::Buffer;
11use polars_extension::PolarsExtension;
12use polars_utils::format_pl_smallstr;
13use polars_utils::relaxed_cell::RelaxedCell;
14
15use crate::PROCESS_ID;
16use crate::prelude::*;
17
18static POLARS_ALLOW_EXTENSION: RelaxedCell<bool> = RelaxedCell::new_bool(false);
19
20/// Control whether extension types may be created.
21///
22/// If the environment variable POLARS_ALLOW_EXTENSION is set, this function has no effect.
23pub fn set_polars_allow_extension(toggle: bool) {
24    POLARS_ALLOW_EXTENSION.store(toggle)
25}
26
27/// Invariants
28/// `ptr` must point to start a `T` allocation
29/// `n_t_vals` must represent the correct number of `T` values in that allocation
30unsafe fn create_drop<T: Sized>(mut ptr: *const u8, n_t_vals: usize) -> Box<dyn FnMut()> {
31    Box::new(move || {
32        let t_size = size_of::<T>() as isize;
33        for _ in 0..n_t_vals {
34            let _ = std::ptr::read_unaligned(ptr as *const T);
35            ptr = ptr.offset(t_size)
36        }
37    })
38}
39
40#[allow(clippy::type_complexity)]
41struct ExtensionSentinel {
42    drop_fn: Option<Box<dyn FnMut()>>,
43    // A function on the heap that take a `array: FixedSizeBinary` and a `name: PlSmallStr`
44    // and returns a `Series` of `ObjectChunked<T>`
45    pub(crate) to_series_fn: Option<Box<dyn Fn(&FixedSizeBinaryArray, &PlSmallStr) -> Series>>,
46}
47
48impl Drop for ExtensionSentinel {
49    fn drop(&mut self) {
50        if let Some(mut drop_fn) = self.drop_fn.take() {
51            (drop_fn)()
52        }
53    }
54}
55
56// https://stackoverflow.com/questions/28127165/how-to-convert-struct-to-u8d
57// not entirely sure if padding bytes in T are initialized or not.
58unsafe fn any_as_u8_slice<T: Sized>(p: &T) -> &[u8] {
59    std::slice::from_raw_parts((p as *const T) as *const u8, size_of::<T>())
60}
61
62/// Create an extension Array that can be sent to arrow and (once wrapped in [`PolarsExtension`] will
63/// also call drop on `T`, when the array is dropped.
64pub(crate) fn create_extension<I: Iterator<Item = Option<T>> + TrustedLen, T: Sized + Default>(
65    iter: I,
66) -> PolarsExtension {
67    let env = "POLARS_ALLOW_EXTENSION";
68    if !(POLARS_ALLOW_EXTENSION.load() || std::env::var(env).is_ok()) {
69        panic!("creating extension types not allowed - try setting the environment variable {env}")
70    }
71    let t_size = size_of::<T>();
72    let t_alignment = align_of::<T>();
73    let n_t_vals = iter.size_hint().1.unwrap();
74
75    let mut buf = Vec::with_capacity(n_t_vals * t_size);
76    let mut validity = BitmapBuilder::with_capacity(n_t_vals);
77
78    // when we transmute from &[u8] to T, T must be aligned correctly,
79    // so we pad with bytes until the alignment matches
80    let n_padding = (buf.as_ptr() as usize) % t_alignment;
81    buf.extend(std::iter::repeat_n(0, n_padding));
82
83    // transmute T as bytes and copy in buffer
84    for opt_t in iter.into_iter() {
85        match opt_t {
86            Some(t) => {
87                unsafe {
88                    buf.extend_from_slice(any_as_u8_slice(&t));
89                    // SAFETY: we allocated upfront
90                    validity.push_unchecked(true)
91                }
92                mem::forget(t);
93            },
94            None => {
95                unsafe {
96                    buf.extend_from_slice(any_as_u8_slice(&T::default()));
97                    // SAFETY: we allocated upfront
98                    validity.push_unchecked(false)
99                }
100            },
101        }
102    }
103
104    // We slice the buffer because we want to ignore the padding bytes from here
105    // they can be forgotten.
106    let buf: Buffer<u8> = Buffer::from_vec(buf).sliced(n_padding..);
107    // ptr to start of T, not to start of padding
108    let ptr = buf.as_slice().as_ptr();
109
110    // SAFETY: ptr and t are correct.
111    let drop_fn = unsafe { create_drop::<T>(ptr, n_t_vals) };
112    let et = Box::new(ExtensionSentinel {
113        drop_fn: Some(drop_fn),
114        to_series_fn: None,
115    });
116    let et_ptr = &*et as *const ExtensionSentinel;
117    std::mem::forget(et);
118
119    let metadata = format_pl_smallstr!("{};{}", *PROCESS_ID, et_ptr as usize);
120
121    let physical_type = ArrowDataType::FixedSizeBinary(t_size);
122    let extension_type = ArrowDataType::Extension(Box::new(ExtensionType {
123        name: PlSmallStr::from_static(POLARS_OBJECT_EXTENSION_NAME),
124        inner: physical_type,
125        metadata: Some(metadata),
126    }));
127
128    let array = FixedSizeBinaryArray::new(extension_type, buf, validity.into_opt_validity());
129
130    // SAFETY: we just heap allocated the ExtensionSentinel, so its alive.
131    unsafe { PolarsExtension::new(array) }
132}
133
134#[cfg(test)]
135mod test {
136    use std::fmt::{Display, Formatter};
137    use std::hash::{Hash, Hasher};
138
139    use polars_utils::total_ord::TotalHash;
140
141    use super::*;
142
143    #[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
144    struct Foo {
145        pub a: i32,
146        pub b: u8,
147        pub other_heap: String,
148    }
149
150    impl TotalEq for Foo {
151        fn tot_eq(&self, other: &Self) -> bool {
152            self == other
153        }
154    }
155
156    impl TotalHash for Foo {
157        fn tot_hash<H>(&self, state: &mut H)
158        where
159            H: Hasher,
160        {
161            self.hash(state);
162        }
163    }
164
165    impl Display for Foo {
166        fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
167            write!(f, "{self:?}")
168        }
169    }
170
171    impl PolarsObject for Foo {
172        fn type_name() -> &'static str {
173            "object"
174        }
175    }
176
177    #[test]
178    fn test_create_extension() {
179        set_polars_allow_extension(true);
180        // Run this under MIRI.
181        let foo = Foo {
182            a: 1,
183            b: 1,
184            other_heap: "foo".into(),
185        };
186        let foo2 = Foo {
187            a: 1,
188            b: 1,
189            other_heap: "bar".into(),
190        };
191
192        let vals = vec![Some(foo), Some(foo2)];
193        create_extension(vals.into_iter());
194    }
195}