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
92macro_rules! polars_to_ffi {
93 ($from_ver:literal) => {
94 paste! {
95 impl Interchange {
96 #[doc = "Move Polars version `" $from_ver "` to the Arrow data interchange format."]
97 pub fn [<from_polars_ $from_ver>](df: [<polars_crate_ $from_ver>]::frame::DataFrame) -> Result<Self, InterchangeError> {
98 Ok(Self {
99 chunks_aligned: !df.should_rechunk(),
100 ffi: {
101 // Number of columns
102 let num_cols = df.width();
103
104 // Prepare ffi series vec
105 let mut ffi = Vec::with_capacity(num_cols);
106
107 // Get the series from the df
108 let series: Vec<[<polars_crate_ $from_ver>]::series::Series> = df.take_columns();
109
110 for s in series {
111 // Get arrow-type fields
112 let field = &s
113 .field()
114 .to_arrow([<polars_crate_ $from_ver>]::datatypes::CompatLevel::newest());
115
116 // Get name of column
117 let name = s.name().to_string();
118
119 // Get number of chunks in each column
120 let n_chunks = s.n_chunks();
121
122 // Prepare chunk vec
123 let mut ffi_chunk = Vec::with_capacity(num_cols);
124
125 for c in 0..n_chunks {
126
127 // Get ffi array
128 let ffi_array = [<polars_arrow_ $from_ver>]::ffi::export_array_to_c(
129 s.to_arrow(c, [<polars_crate_ $from_ver>]::datatypes::CompatLevel::newest()),
130 );
131
132 // Get ffi field
133 let ffi_field = [<polars_arrow_ $from_ver>]::ffi::export_field_to_c(field);
134
135 // Convert ffi array from polars-arrow to this crate's version of ArrowArray
136 let ffi_array = unsafe { transmute::<
137 [<polars_arrow_ $from_ver>]::ffi::ArrowArray,
138 ArrowArray,
139 >(ffi_array)};
140
141 // Convert ffi field from polars-arrow to this crate's version of ArrowField
142 let ffi_field = unsafe{transmute::<
143 [<polars_arrow_ $from_ver>]::ffi::ArrowSchema,
144 ArrowSchema,
145 >(ffi_field)};
146
147 // Create series
148 ffi_chunk.push((ffi_array, ffi_field));
149 }
150
151 ffi.push((name, ffi_chunk));
152 }
153
154 ffi
155 }
156 })
157 }
158 }
159 }
160 };
161}
162
163#[cfg(feature = "polars_0_42")]
164polars_to_ffi!("0_42");
165
166#[cfg(feature = "polars_0_43")]
167polars_to_ffi!("0_43");
168
169macro_rules! polars_to_ffi {
170 ($from_ver:literal) => {
171 paste! {
172 impl Interchange {
173 #[doc = "Move Polars version `" $from_ver "` to the Arrow data interchange format."]
174 pub fn [<from_polars_ $from_ver>](df: [<polars_crate_ $from_ver>]::frame::DataFrame) -> Result<Self, InterchangeError> {
175 Ok(Self {
176 chunks_aligned: !df.should_rechunk(),
177 ffi: {
178 // Number of columns
179 let num_cols = df.width();
180
181 // Prepare ffi series vec
182 let mut ffi = Vec::with_capacity(num_cols);
183
184 // Get column names to remove them one by one
185 let names = df.get_column_names();
186
187 // Get the series from the df
188 let series: Vec<[<polars_crate_ $from_ver>]::series::Series> = df.select_series(names)?;
189
190 for s in series {
191 // Get arrow-type fields
192 let field = &s
193 .field()
194 .to_arrow(false);
195
196 // Get name of column
197 let name = s.name().to_string();
198
199 // Get number of chunks in each column
200 let n_chunks = s.n_chunks();
201
202 // Prepare chunk vec
203 let mut ffi_chunk = Vec::with_capacity(num_cols);
204
205 for c in 0..n_chunks {
206
207 // Get ffi array
208 let ffi_array = [<polars_arrow_ $from_ver>]::ffi::export_array_to_c(
209 s.to_arrow(c, false),
210 );
211
212 // Get ffi field
213 let ffi_field = [<polars_arrow_ $from_ver>]::ffi::export_field_to_c(field);
214
215 // Convert ffi array from polars-arrow to this crate's version of ArrowArray
216 let ffi_array = unsafe { transmute::<
217 [<polars_arrow_ $from_ver>]::ffi::ArrowArray,
218 ArrowArray,
219 >(ffi_array)};
220
221 // Convert ffi field from polars-arrow to this crate's version of ArrowField
222 let ffi_field = unsafe{transmute::<
223 [<polars_arrow_ $from_ver>]::ffi::ArrowSchema,
224 ArrowSchema,
225 >(ffi_field)};
226
227 // Create series
228 ffi_chunk.push((ffi_array, ffi_field));
229 }
230
231 ffi.push((name, ffi_chunk));
232 }
233
234 ffi
235 }
236 })
237 }
238 }
239 }
240 };
241}
242
243#[cfg(feature = "polars_0_40")]
244polars_to_ffi!("0_40");
245
246#[cfg(feature = "polars_0_41")]
247polars_to_ffi!("0_41");