df_interchange/from_polars.rs
1use paste::paste;
2use std::mem::transmute;
3
4use crate::{error::InterchangeError, ArrowArray, ArrowSchema, Interchange};
5
6macro_rules! polars_to_ffi {
7 ($from_ver:literal) => {
8 paste! {
9 impl Interchange {
10 #[doc = "Move Polars version `" $from_ver "` to the Arrow data interchange format."]
11 pub fn [<from_polars_ $from_ver>](df: [<polars_crate_ $from_ver>]::frame::DataFrame) -> Result<Self, InterchangeError> {
12 Ok(Self {
13 chunks_aligned: !df.should_rechunk(),
14 ffi: {
15 // Number of columns
16 let num_cols = df.width();
17
18 // Prepare ffi series vec
19 let mut ffi = Vec::with_capacity(num_cols);
20
21 // Get the columns from the df, as series
22 let series: Vec<[<polars_crate_ $from_ver>]::series::Series> = df
23 .take_columns()
24 .into_iter()
25 .map(|s| s.take_materialized_series())
26 .collect();
27
28 for s in series {
29 // Get arrow-type fields
30 let field = &s
31 .field()
32 .to_arrow([<polars_crate_ $from_ver>]::datatypes::CompatLevel::newest());
33
34 // Get name of column
35 let name = s.name().to_string();
36
37 // Get number of chunks in each column
38 let n_chunks = s.n_chunks();
39
40 // Prepare chunk vec
41 let mut ffi_chunk = Vec::with_capacity(num_cols);
42
43 for c in 0..n_chunks {
44
45 // Get ffi array
46 let ffi_array = [<polars_arrow_ $from_ver>]::ffi::export_array_to_c(
47 s.to_arrow(c, [<polars_crate_ $from_ver>]::datatypes::CompatLevel::newest()),
48 );
49
50 // Get ffi field
51 let ffi_field = [<polars_arrow_ $from_ver>]::ffi::export_field_to_c(field);
52
53 // Convert ffi array from polars-arrow to this crate's version of ArrowArray
54 let ffi_array = unsafe { transmute::<
55 [<polars_arrow_ $from_ver>]::ffi::ArrowArray,
56 ArrowArray,
57 >(ffi_array)};
58
59 // Convert ffi field from polars-arrow to this crate's version of ArrowField
60 let ffi_field = unsafe{transmute::<
61 [<polars_arrow_ $from_ver>]::ffi::ArrowSchema,
62 ArrowSchema,
63 >(ffi_field)};
64
65 // Create series
66 ffi_chunk.push((ffi_array, ffi_field));
67 }
68
69 ffi.push((name, ffi_chunk));
70 }
71
72 ffi
73 }
74 })
75 }
76 }
77 }
78 };
79}
80#[cfg(feature = "polars_0_44")]
81polars_to_ffi!("0_44");
82
83#[cfg(feature = "polars_0_45")]
84polars_to_ffi!("0_45");
85
86#[cfg(feature = "polars_0_46")]
87polars_to_ffi!("0_46");
88
89#[cfg(feature = "polars_0_47")]
90polars_to_ffi!("0_47");
91
92#[cfg(feature = "polars_0_48")]
93polars_to_ffi!("0_48");
94
95#[cfg(feature = "polars_0_49")]
96polars_to_ffi!("0_49");
97
98#[cfg(feature = "polars_0_50")]
99polars_to_ffi!("0_50");
100
101macro_rules! polars_to_ffi {
102 ($from_ver:literal) => {
103 paste! {
104 impl Interchange {
105 #[doc = "Move Polars version `" $from_ver "` to the Arrow data interchange format."]
106 pub fn [<from_polars_ $from_ver>](df: [<polars_crate_ $from_ver>]::frame::DataFrame) -> Result<Self, InterchangeError> {
107 Ok(Self {
108 chunks_aligned: !df.should_rechunk(),
109 ffi: {
110 // Number of columns
111 let num_cols = df.width();
112
113 // Prepare ffi series vec
114 let mut ffi = Vec::with_capacity(num_cols);
115
116 // Get the series from the df
117 let series: Vec<[<polars_crate_ $from_ver>]::series::Series> = df.take_columns();
118
119 for s in series {
120 // Get arrow-type fields
121 let field = &s
122 .field()
123 .to_arrow([<polars_crate_ $from_ver>]::datatypes::CompatLevel::newest());
124
125 // Get name of column
126 let name = s.name().to_string();
127
128 // Get number of chunks in each column
129 let n_chunks = s.n_chunks();
130
131 // Prepare chunk vec
132 let mut ffi_chunk = Vec::with_capacity(num_cols);
133
134 for c in 0..n_chunks {
135
136 // Get ffi array
137 let ffi_array = [<polars_arrow_ $from_ver>]::ffi::export_array_to_c(
138 s.to_arrow(c, [<polars_crate_ $from_ver>]::datatypes::CompatLevel::newest()),
139 );
140
141 // Get ffi field
142 let ffi_field = [<polars_arrow_ $from_ver>]::ffi::export_field_to_c(field);
143
144 // Convert ffi array from polars-arrow to this crate's version of ArrowArray
145 let ffi_array = unsafe { transmute::<
146 [<polars_arrow_ $from_ver>]::ffi::ArrowArray,
147 ArrowArray,
148 >(ffi_array)};
149
150 // Convert ffi field from polars-arrow to this crate's version of ArrowField
151 let ffi_field = unsafe{transmute::<
152 [<polars_arrow_ $from_ver>]::ffi::ArrowSchema,
153 ArrowSchema,
154 >(ffi_field)};
155
156 // Create series
157 ffi_chunk.push((ffi_array, ffi_field));
158 }
159
160 ffi.push((name, ffi_chunk));
161 }
162
163 ffi
164 }
165 })
166 }
167 }
168 }
169 };
170}
171
172#[cfg(feature = "polars_0_42")]
173polars_to_ffi!("0_42");
174
175#[cfg(feature = "polars_0_43")]
176polars_to_ffi!("0_43");
177
178macro_rules! polars_to_ffi {
179 ($from_ver:literal) => {
180 paste! {
181 impl Interchange {
182 #[doc = "Move Polars version `" $from_ver "` to the Arrow data interchange format."]
183 pub fn [<from_polars_ $from_ver>](df: [<polars_crate_ $from_ver>]::frame::DataFrame) -> Result<Self, InterchangeError> {
184 Ok(Self {
185 chunks_aligned: !df.should_rechunk(),
186 ffi: {
187 // Number of columns
188 let num_cols = df.width();
189
190 // Prepare ffi series vec
191 let mut ffi = Vec::with_capacity(num_cols);
192
193 // Get column names to remove them one by one
194 let names = df.get_column_names();
195
196 // Get the series from the df
197 let series: Vec<[<polars_crate_ $from_ver>]::series::Series> = df.select_series(names)?;
198
199 for s in series {
200 // Get arrow-type fields
201 let field = &s
202 .field()
203 .to_arrow(false);
204
205 // Get name of column
206 let name = s.name().to_string();
207
208 // Get number of chunks in each column
209 let n_chunks = s.n_chunks();
210
211 // Prepare chunk vec
212 let mut ffi_chunk = Vec::with_capacity(num_cols);
213
214 for c in 0..n_chunks {
215
216 // Get ffi array
217 let ffi_array = [<polars_arrow_ $from_ver>]::ffi::export_array_to_c(
218 s.to_arrow(c, false),
219 );
220
221 // Get ffi field
222 let ffi_field = [<polars_arrow_ $from_ver>]::ffi::export_field_to_c(field);
223
224 // Convert ffi array from polars-arrow to this crate's version of ArrowArray
225 let ffi_array = unsafe { transmute::<
226 [<polars_arrow_ $from_ver>]::ffi::ArrowArray,
227 ArrowArray,
228 >(ffi_array)};
229
230 // Convert ffi field from polars-arrow to this crate's version of ArrowField
231 let ffi_field = unsafe{transmute::<
232 [<polars_arrow_ $from_ver>]::ffi::ArrowSchema,
233 ArrowSchema,
234 >(ffi_field)};
235
236 // Create series
237 ffi_chunk.push((ffi_array, ffi_field));
238 }
239
240 ffi.push((name, ffi_chunk));
241 }
242
243 ffi
244 }
245 })
246 }
247 }
248 }
249 };
250}
251
252#[cfg(feature = "polars_0_40")]
253polars_to_ffi!("0_40");
254
255#[cfg(feature = "polars_0_41")]
256polars_to_ffi!("0_41");