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
86macro_rules! ffi_to_polars {
87 ($to_ver:literal) => {
88 paste! {
89 impl Interchange {
90 #[doc = "Move Arrow data interchange format to Polars version `" $to_ver "`."]
91 pub fn [<to_polars_ $to_ver>](self) -> Result<[<polars_crate_ $to_ver>]::frame::DataFrame, InterchangeError> {
92
93 let num_cols = self.ffi.len();
95 let mut series = Vec::with_capacity(num_cols);
96
97 for s in self.ffi {
99
100 let num_chunks = s.1.len();
102 let mut chunks = Vec::with_capacity(num_chunks);
103
104 let name = s.0;
106
107 for c in s.1 {
109
110 let ffi_array = unsafe { transmute::<ArrowArray, [<polars_arrow_ $to_ver>]::ffi::ArrowArray>(c.0) };
112
113 let ffi_field = unsafe { transmute::<ArrowSchema, [<polars_arrow_ $to_ver>]::ffi::ArrowSchema>(c.1) };
115
116 let field = unsafe {
118 [<polars_arrow_ $to_ver>]::ffi::import_field_from_c(&ffi_field)
119 }?;
120
121 let array = unsafe {
123 [<polars_arrow_ $to_ver>]::ffi::import_array_from_c(ffi_array, field.data_type().clone(),
124 )
125 }?;
126
127 chunks.push(array);
129 }
130
131 series.push(
133 [<polars_crate_ $to_ver>]::series::Series::from_arrow_chunks(
134 &name,
135 chunks,
136 )?,
137 );
138 }
139
140 Ok([<polars_crate_ $to_ver>]::frame::DataFrame::from_iter(series))
142 }
143 }
144 }
145 };
146}
147
148#[cfg(feature = "polars_0_41")]
149ffi_to_polars!("0_41");
150
151#[cfg(feature = "polars_0_42")]
152ffi_to_polars!("0_42");
153
154macro_rules! ffi_to_polars {
155 ($to_ver:literal) => {
156 paste! {
157 impl Interchange {
158 #[doc = "Move Arrow data interchange format to Polars version `" $to_ver "`."]
159 pub fn [<to_polars_ $to_ver>](self) -> Result<[<polars_crate_ $to_ver>]::frame::DataFrame, InterchangeError> {
160
161 let num_cols = self.ffi.len();
163 let mut series = Vec::with_capacity(num_cols);
164
165 for s in self.ffi {
167
168 let num_chunks = s.1.len();
170 let mut chunks = Vec::with_capacity(num_chunks);
171
172 let name = s.0;
174
175 for c in s.1 {
177
178 let ffi_array = unsafe { transmute::<ArrowArray, [<polars_arrow_ $to_ver>]::ffi::ArrowArray>(c.0) };
180
181 let ffi_field = unsafe { transmute::<ArrowSchema, [<polars_arrow_ $to_ver>]::ffi::ArrowSchema>(c.1) };
183
184 let field = unsafe {
186 [<polars_arrow_ $to_ver>]::ffi::import_field_from_c(&ffi_field)
187 }?;
188
189 let array = unsafe {
191 [<polars_arrow_ $to_ver>]::ffi::import_array_from_c(ffi_array, field.data_type().clone(),
192 )
193 }?;
194
195 let series_chunk = [<polars_crate_ $to_ver>]::series::Series::from_arrow(
196 &name,
197 array,
198 )?;
199
200 chunks.push(series_chunk);
202 }
203
204 let mut chunk_iter = chunks.into_iter();
206 let mut full_series = chunk_iter.next().unwrap();
207 for c in chunk_iter {
208 full_series.append(&c)?;
209 }
210
211 series.push(
212 full_series,
213 );
214 }
215
216 Ok([<polars_crate_ $to_ver>]::frame::DataFrame::from_iter(series))
218 }
219 }
220 }
221 };
222}
223
224#[cfg(feature = "polars_0_40")]
225ffi_to_polars!("0_40");