df_interchange/
to_polars.rs1use paste::paste;
2use std::mem::transmute;
3
4use crate::{error::InterchangeError, ArrowArray, ArrowSchema, Interchange};
5
6macro_rules! ffi_to_polars {
7 ($to_ver:literal) => {
8 paste! {
9 impl Interchange {
10 #[doc = "Move Arrow data interchange format to Polars version `" $to_ver "`."]
11 pub fn [<to_polars_ $to_ver>](self) -> Result<[<polars_crate_ $to_ver>]::frame::DataFrame, InterchangeError> {
12
13 let num_cols = self.ffi.len();
15 let mut columns = Vec::with_capacity(num_cols);
16
17 for s in self.ffi {
19
20 let num_chunks = s.1.len();
22 let mut chunks = Vec::with_capacity(num_chunks);
23
24 let name = s.0;
26
27 for c in s.1 {
29
30 let ffi_array = unsafe { transmute::<ArrowArray, [<polars_arrow_ $to_ver>]::ffi::ArrowArray>(c.0) };
32
33 let ffi_field = unsafe { transmute::<ArrowSchema, [<polars_arrow_ $to_ver>]::ffi::ArrowSchema>(c.1) };
35
36 let field = unsafe {
38 [<polars_arrow_ $to_ver>]::ffi::import_field_from_c(&ffi_field)
39 }?;
40
41 let array = unsafe {
43 [<polars_arrow_ $to_ver>]::ffi::import_array_from_c(ffi_array, field.dtype().clone(),
44 )
45 }?;
46
47 chunks.push(array);
49 }
50
51 let column = [<polars_crate_ $to_ver>]::frame::column::Column::from(
53 [<polars_crate_ $to_ver>]::series::Series::from_arrow_chunks(
54 name.into(),
55 chunks,
56 )?);
57
58 columns.push(column);
60
61
62 }
63
64 Ok([<polars_crate_ $to_ver>]::frame::DataFrame::new_infer_height(columns)?)
66 }
67 }
68 }
69 };
70}
71
72#[cfg(feature = "polars_0_53")]
73ffi_to_polars!("0_53");
74
75macro_rules! ffi_to_polars {
76 ($to_ver:literal) => {
77 paste! {
78 impl Interchange {
79 #[doc = "Move Arrow data interchange format to Polars version `" $to_ver "`."]
80 pub fn [<to_polars_ $to_ver>](self) -> Result<[<polars_crate_ $to_ver>]::frame::DataFrame, InterchangeError> {
81
82 let num_cols = self.ffi.len();
84 let mut series = Vec::with_capacity(num_cols);
85
86 for s in self.ffi {
88
89 let num_chunks = s.1.len();
91 let mut chunks = Vec::with_capacity(num_chunks);
92
93 let name = s.0;
95
96 for c in s.1 {
98
99 let ffi_array = unsafe { transmute::<ArrowArray, [<polars_arrow_ $to_ver>]::ffi::ArrowArray>(c.0) };
101
102 let ffi_field = unsafe { transmute::<ArrowSchema, [<polars_arrow_ $to_ver>]::ffi::ArrowSchema>(c.1) };
104
105 let field = unsafe {
107 [<polars_arrow_ $to_ver>]::ffi::import_field_from_c(&ffi_field)
108 }?;
109
110 let array = unsafe {
112 [<polars_arrow_ $to_ver>]::ffi::import_array_from_c(ffi_array, field.dtype().clone(),
113 )
114 }?;
115
116 chunks.push(array);
118 }
119
120 series.push(
122 [<polars_crate_ $to_ver>]::series::Series::from_arrow_chunks(
123 name.into(),
124 chunks,
125 )?,
126 );
127 }
128
129 Ok([<polars_crate_ $to_ver>]::frame::DataFrame::from_iter(series))
131 }
132 }
133 }
134 };
135}
136
137#[cfg(feature = "polars_0_43")]
138ffi_to_polars!("0_43");
139
140#[cfg(feature = "polars_0_44")]
141ffi_to_polars!("0_44");
142
143#[cfg(feature = "polars_0_45")]
144ffi_to_polars!("0_45");
145
146#[cfg(feature = "polars_0_46")]
147ffi_to_polars!("0_46");
148
149#[cfg(feature = "polars_0_47")]
150ffi_to_polars!("0_47");
151
152#[cfg(feature = "polars_0_48")]
153ffi_to_polars!("0_48");
154
155#[cfg(feature = "polars_0_49")]
156ffi_to_polars!("0_49");
157
158#[cfg(feature = "polars_0_50")]
159ffi_to_polars!("0_50");
160
161#[cfg(feature = "polars_0_51")]
162ffi_to_polars!("0_51");
163
164#[cfg(feature = "polars_0_52")]
165ffi_to_polars!("0_52");
166
167macro_rules! ffi_to_polars {
168 ($to_ver:literal) => {
169 paste! {
170 impl Interchange {
171 #[doc = "Move Arrow data interchange format to Polars version `" $to_ver "`."]
172 pub fn [<to_polars_ $to_ver>](self) -> Result<[<polars_crate_ $to_ver>]::frame::DataFrame, InterchangeError> {
173
174 let num_cols = self.ffi.len();
176 let mut series = Vec::with_capacity(num_cols);
177
178 for s in self.ffi {
180
181 let num_chunks = s.1.len();
183 let mut chunks = Vec::with_capacity(num_chunks);
184
185 let name = s.0;
187
188 for c in s.1 {
190
191 let ffi_array = unsafe { transmute::<ArrowArray, [<polars_arrow_ $to_ver>]::ffi::ArrowArray>(c.0) };
193
194 let ffi_field = unsafe { transmute::<ArrowSchema, [<polars_arrow_ $to_ver>]::ffi::ArrowSchema>(c.1) };
196
197 let field = unsafe {
199 [<polars_arrow_ $to_ver>]::ffi::import_field_from_c(&ffi_field)
200 }?;
201
202 let array = unsafe {
204 [<polars_arrow_ $to_ver>]::ffi::import_array_from_c(ffi_array, field.data_type().clone(),
205 )
206 }?;
207
208 chunks.push(array);
210 }
211
212 series.push(
214 [<polars_crate_ $to_ver>]::series::Series::from_arrow_chunks(
215 &name,
216 chunks,
217 )?,
218 );
219 }
220
221 Ok([<polars_crate_ $to_ver>]::frame::DataFrame::from_iter(series))
223 }
224 }
225 }
226 };
227}
228
229#[cfg(feature = "polars_0_41")]
230ffi_to_polars!("0_41");
231
232#[cfg(feature = "polars_0_42")]
233ffi_to_polars!("0_42");
234
235macro_rules! ffi_to_polars {
236 ($to_ver:literal) => {
237 paste! {
238 impl Interchange {
239 #[doc = "Move Arrow data interchange format to Polars version `" $to_ver "`."]
240 pub fn [<to_polars_ $to_ver>](self) -> Result<[<polars_crate_ $to_ver>]::frame::DataFrame, InterchangeError> {
241
242 let num_cols = self.ffi.len();
244 let mut series = Vec::with_capacity(num_cols);
245
246 for s in self.ffi {
248
249 let num_chunks = s.1.len();
251 let mut chunks = Vec::with_capacity(num_chunks);
252
253 let name = s.0;
255
256 for c in s.1 {
258
259 let ffi_array = unsafe { transmute::<ArrowArray, [<polars_arrow_ $to_ver>]::ffi::ArrowArray>(c.0) };
261
262 let ffi_field = unsafe { transmute::<ArrowSchema, [<polars_arrow_ $to_ver>]::ffi::ArrowSchema>(c.1) };
264
265 let field = unsafe {
267 [<polars_arrow_ $to_ver>]::ffi::import_field_from_c(&ffi_field)
268 }?;
269
270 let array = unsafe {
272 [<polars_arrow_ $to_ver>]::ffi::import_array_from_c(ffi_array, field.data_type().clone(),
273 )
274 }?;
275
276 let series_chunk = [<polars_crate_ $to_ver>]::series::Series::from_arrow(
277 &name,
278 array,
279 )?;
280
281 chunks.push(series_chunk);
283 }
284
285 let mut chunk_iter = chunks.into_iter();
287 let mut full_series = chunk_iter.next().unwrap();
288 for c in chunk_iter {
289 full_series.append(&c)?;
290 }
291
292 series.push(
293 full_series,
294 );
295 }
296
297 Ok([<polars_crate_ $to_ver>]::frame::DataFrame::from_iter(series))
299 }
300 }
301 }
302 };
303}
304
305#[cfg(feature = "polars_0_40")]
306ffi_to_polars!("0_40");