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 series = 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 series.push(
53 [<polars_crate_ $to_ver>]::series::Series::from_arrow_chunks(
54 name.into(),
55 chunks,
56 )?,
57 );
58 }
59
60 Ok([<polars_crate_ $to_ver>]::frame::DataFrame::from_iter(series))
62 }
63 }
64 }
65 };
66}
67
68#[cfg(feature = "polars_0_43")]
69ffi_to_polars!("0_43");
70
71#[cfg(feature = "polars_0_44")]
72ffi_to_polars!("0_44");
73
74#[cfg(feature = "polars_0_45")]
75ffi_to_polars!("0_45");
76
77#[cfg(feature = "polars_0_46")]
78ffi_to_polars!("0_46");
79
80#[cfg(feature = "polars_0_47")]
81ffi_to_polars!("0_47");
82
83#[cfg(feature = "polars_0_48")]
84ffi_to_polars!("0_48");
85
86#[cfg(feature = "polars_0_49")]
87ffi_to_polars!("0_49");
88
89#[cfg(feature = "polars_0_50")]
90ffi_to_polars!("0_50");
91
92#[cfg(feature = "polars_0_51")]
93ffi_to_polars!("0_51");
94
95macro_rules! ffi_to_polars {
96 ($to_ver:literal) => {
97 paste! {
98 impl Interchange {
99 #[doc = "Move Arrow data interchange format to Polars version `" $to_ver "`."]
100 pub fn [<to_polars_ $to_ver>](self) -> Result<[<polars_crate_ $to_ver>]::frame::DataFrame, InterchangeError> {
101
102 let num_cols = self.ffi.len();
104 let mut series = Vec::with_capacity(num_cols);
105
106 for s in self.ffi {
108
109 let num_chunks = s.1.len();
111 let mut chunks = Vec::with_capacity(num_chunks);
112
113 let name = s.0;
115
116 for c in s.1 {
118
119 let ffi_array = unsafe { transmute::<ArrowArray, [<polars_arrow_ $to_ver>]::ffi::ArrowArray>(c.0) };
121
122 let ffi_field = unsafe { transmute::<ArrowSchema, [<polars_arrow_ $to_ver>]::ffi::ArrowSchema>(c.1) };
124
125 let field = unsafe {
127 [<polars_arrow_ $to_ver>]::ffi::import_field_from_c(&ffi_field)
128 }?;
129
130 let array = unsafe {
132 [<polars_arrow_ $to_ver>]::ffi::import_array_from_c(ffi_array, field.data_type().clone(),
133 )
134 }?;
135
136 chunks.push(array);
138 }
139
140 series.push(
142 [<polars_crate_ $to_ver>]::series::Series::from_arrow_chunks(
143 &name,
144 chunks,
145 )?,
146 );
147 }
148
149 Ok([<polars_crate_ $to_ver>]::frame::DataFrame::from_iter(series))
151 }
152 }
153 }
154 };
155}
156
157#[cfg(feature = "polars_0_41")]
158ffi_to_polars!("0_41");
159
160#[cfg(feature = "polars_0_42")]
161ffi_to_polars!("0_42");
162
163macro_rules! ffi_to_polars {
164 ($to_ver:literal) => {
165 paste! {
166 impl Interchange {
167 #[doc = "Move Arrow data interchange format to Polars version `" $to_ver "`."]
168 pub fn [<to_polars_ $to_ver>](self) -> Result<[<polars_crate_ $to_ver>]::frame::DataFrame, InterchangeError> {
169
170 let num_cols = self.ffi.len();
172 let mut series = Vec::with_capacity(num_cols);
173
174 for s in self.ffi {
176
177 let num_chunks = s.1.len();
179 let mut chunks = Vec::with_capacity(num_chunks);
180
181 let name = s.0;
183
184 for c in s.1 {
186
187 let ffi_array = unsafe { transmute::<ArrowArray, [<polars_arrow_ $to_ver>]::ffi::ArrowArray>(c.0) };
189
190 let ffi_field = unsafe { transmute::<ArrowSchema, [<polars_arrow_ $to_ver>]::ffi::ArrowSchema>(c.1) };
192
193 let field = unsafe {
195 [<polars_arrow_ $to_ver>]::ffi::import_field_from_c(&ffi_field)
196 }?;
197
198 let array = unsafe {
200 [<polars_arrow_ $to_ver>]::ffi::import_array_from_c(ffi_array, field.data_type().clone(),
201 )
202 }?;
203
204 let series_chunk = [<polars_crate_ $to_ver>]::series::Series::from_arrow(
205 &name,
206 array,
207 )?;
208
209 chunks.push(series_chunk);
211 }
212
213 let mut chunk_iter = chunks.into_iter();
215 let mut full_series = chunk_iter.next().unwrap();
216 for c in chunk_iter {
217 full_series.append(&c)?;
218 }
219
220 series.push(
221 full_series,
222 );
223 }
224
225 Ok([<polars_crate_ $to_ver>]::frame::DataFrame::from_iter(series))
227 }
228 }
229 }
230 };
231}
232
233#[cfg(feature = "polars_0_40")]
234ffi_to_polars!("0_40");