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