-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy patharrow.rs
More file actions
345 lines (297 loc) · 12.3 KB
/
arrow.rs
File metadata and controls
345 lines (297 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// SPDX-FileCopyrightText: 2016-2025 Copyright The Apache Software Foundation
// SPDX-FileCopyrightText: 2025 Copyright the Vortex contributors
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileComment: Derived from upstream file arrow-pyarrow/src/main at commit 549709fb at https://github.com/apache/arrow-rs
// SPDX-FileNotice: https://github.com/apache/arrow-rs/blob/549709fbdf91cd1f6c263a7e4540c542b6fecf6b/NOTICE.txt
#![allow(clippy::same_name_method)]
use std::convert::From;
use std::convert::TryFrom;
use std::ffi::CStr;
use std::ptr::addr_of;
use std::sync::Arc;
use arrow_array::RecordBatch;
use arrow_array::RecordBatchIterator;
use arrow_array::RecordBatchOptions;
use arrow_array::RecordBatchReader;
use arrow_array::StructArray;
use arrow_array::ffi;
use arrow_array::ffi::FFI_ArrowArray;
use arrow_array::ffi::FFI_ArrowSchema;
use arrow_array::ffi_stream::ArrowArrayStreamReader;
use arrow_array::ffi_stream::FFI_ArrowArrayStream;
use arrow_data::ArrayData;
use arrow_schema::ArrowError;
use arrow_schema::DataType;
use arrow_schema::Field;
use arrow_schema::Schema;
use pyo3::exceptions::PyTypeError;
use pyo3::exceptions::PyValueError;
use pyo3::ffi::Py_uintptr_t;
use pyo3::ffi::c_str;
use pyo3::import_exception;
use pyo3::intern;
use pyo3::prelude::*;
use pyo3::types::PyCapsule;
use pyo3::types::PyTuple;
use crate::classes::array_class;
use crate::classes::data_type_class;
use crate::classes::field_class;
use crate::classes::record_batch_reader_class;
use crate::classes::schema_class;
const SCHEMA_NAME: &CStr = c_str!("arrow_schema");
const ARRAY_NAME: &CStr = c_str!("arrow_array");
const ARRAY_STREAM_NAME: &CStr = c_str!("arrow_array_stream");
import_exception!(pyarrow, ArrowException);
/// Represents an exception raised by PyArrow.
pub type PyArrowException = ArrowException;
fn to_py_err(err: ArrowError) -> PyErr {
PyArrowException::new_err(err.to_string())
}
/// Trait for converting Python objects to arrow-rs types.
pub trait FromPyArrow<'a, 'py>: Sized {
/// Convert a Python object to an arrow-rs type.
///
/// Takes a GIL-bound value from Python and returns a result with the arrow-rs type.
fn from_pyarrow(value: &Borrowed<'a, 'py, PyAny>) -> PyResult<Self>;
}
/// Create a new PyArrow object from a arrow-rs type.
pub trait ToPyArrow {
/// Convert the implemented type into a Python object without consuming it.
fn to_pyarrow(&self, py: Python) -> PyResult<Py<PyAny>>;
}
/// Convert an arrow-rs type into a PyArrow object.
pub trait IntoPyArrow {
/// Convert the implemented type into a Python object while consuming it.
fn into_pyarrow(self, py: Python) -> PyResult<Py<PyAny>>;
}
impl<'py> FromPyArrow<'_, 'py> for DataType {
fn from_pyarrow(value: &Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
let py = value.py();
if !value.hasattr(intern!(py, "__arrow_c_schema__"))? {
return Err(PyValueError::new_err(
"Expected __arrow_c_schema__ attribute to be set.",
));
}
let capsule = value.getattr(intern!(py, "__arrow_c_schema__"))?.call0()?;
let capsule = capsule.cast::<PyCapsule>()?;
let schema_ptr = unsafe {
capsule
.pointer_checked(Some(SCHEMA_NAME))?
.cast::<FFI_ArrowSchema>()
.as_ref()
};
DataType::try_from(schema_ptr).map_err(to_py_err)
}
}
impl ToPyArrow for DataType {
fn to_pyarrow(&self, py: Python) -> PyResult<Py<PyAny>> {
let c_schema = FFI_ArrowSchema::try_from(self).map_err(to_py_err)?;
let dtype = data_type_class(py)?.call_method1(
intern!(py, "_import_from_c"),
(&raw const c_schema as Py_uintptr_t,),
)?;
Ok(dtype.into())
}
}
impl<'py> FromPyArrow<'_, 'py> for Field {
fn from_pyarrow(value: &Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
let py = value.py();
if !value.hasattr(intern!(py, "__arrow_c_schema__"))? {
return Err(PyValueError::new_err(
"Expected __arrow_c_schema__ attribute to be set.",
));
}
let capsule = value.getattr(intern!(py, "__arrow_c_schema__"))?.call0()?;
let capsule = capsule.cast::<PyCapsule>()?;
let schema_ptr = unsafe {
capsule
.pointer_checked(Some(SCHEMA_NAME))?
.cast::<FFI_ArrowSchema>()
.as_ref()
};
let field = Field::try_from(schema_ptr).map_err(to_py_err)?;
Ok(field)
}
}
impl ToPyArrow for Field {
fn to_pyarrow(&self, py: Python) -> PyResult<Py<PyAny>> {
let c_schema = FFI_ArrowSchema::try_from(self).map_err(to_py_err)?;
let dtype = field_class(py)?.call_method1(
intern!(py, "_import_from_c"),
(&raw const c_schema as Py_uintptr_t,),
)?;
Ok(dtype.into())
}
}
impl<'py> FromPyArrow<'_, 'py> for Schema {
fn from_pyarrow(value: &Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
let py = value.py();
if !value.hasattr(intern!(py, "__arrow_c_schema__"))? {
return Err(PyValueError::new_err(
"Expected __arrow_c_schema__ attribute to be set.",
));
}
let capsule = value.getattr(intern!(py, "__arrow_c_schema__"))?.call0()?;
let capsule = capsule.cast::<PyCapsule>()?;
let schema_ptr = unsafe {
capsule
.pointer_checked(Some(SCHEMA_NAME))?
.cast::<FFI_ArrowSchema>()
.as_ref()
};
let schema = Schema::try_from(schema_ptr).map_err(to_py_err)?;
Ok(schema)
}
}
impl ToPyArrow for Schema {
fn to_pyarrow(&self, py: Python) -> PyResult<Py<PyAny>> {
let c_schema = FFI_ArrowSchema::try_from(self).map_err(to_py_err)?;
let schema = schema_class(py)?.call_method1(
intern!(py, "_import_from_c"),
(&raw const c_schema as Py_uintptr_t,),
)?;
Ok(schema.into())
}
}
impl<'py> FromPyArrow<'_, 'py> for ArrayData {
fn from_pyarrow(value: &Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
let py = value.py();
if !value.hasattr(intern!(py, "__arrow_c_array__"))? {
return Err(PyValueError::new_err(
"Expected __arrow_c_array__ attribute to be set.",
));
}
let tuple = value.getattr(intern!(py, "__arrow_c_array__"))?.call0()?;
if !tuple.is_instance_of::<PyTuple>() {
return Err(PyTypeError::new_err(
"Expected __arrow_c_array__ to return a tuple.",
));
}
let schema_capsule = tuple.get_item(0)?;
let schema_capsule = schema_capsule.cast::<PyCapsule>()?;
let array_capsule = tuple.get_item(1)?;
let array_capsule = array_capsule.cast::<PyCapsule>()?;
let schema_ptr = unsafe {
schema_capsule
.pointer_checked(Some(SCHEMA_NAME))?
.cast::<FFI_ArrowSchema>()
.as_ref()
};
let array_ptr = array_capsule
.pointer_checked(Some(ARRAY_NAME))?
.cast::<FFI_ArrowArray>()
.as_ptr();
let array = unsafe { FFI_ArrowArray::from_raw(array_ptr) };
unsafe { ffi::from_ffi(array, schema_ptr) }.map_err(to_py_err)
}
}
impl ToPyArrow for ArrayData {
fn to_pyarrow(&self, py: Python) -> PyResult<Py<PyAny>> {
let array = FFI_ArrowArray::new(self);
let schema = FFI_ArrowSchema::try_from(self.data_type()).map_err(to_py_err)?;
let array = array_class(py)?.call_method1(
intern!(py, "_import_from_c"),
(
addr_of!(array) as Py_uintptr_t,
addr_of!(schema) as Py_uintptr_t,
),
)?;
Ok(array.unbind())
}
}
impl<'py> FromPyArrow<'_, 'py> for RecordBatch {
fn from_pyarrow(value: &Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
let py = value.py();
if !value.hasattr(intern!(py, "__arrow_c_array__"))? {
return Err(PyValueError::new_err(
"Expected __arrow_c_array__ attribute to be set.",
));
}
let tuple = value.getattr(intern!(py, "__arrow_c_array__"))?.call0()?;
if !tuple.is_instance_of::<PyTuple>() {
return Err(PyTypeError::new_err(
"Expected __arrow_c_array__ to return a tuple.",
));
}
let schema_capsule = tuple.get_item(0)?;
let schema_capsule = schema_capsule.cast::<PyCapsule>()?;
let array_capsule = tuple.get_item(1)?;
let array_capsule = array_capsule.cast::<PyCapsule>()?;
let schema_ptr = unsafe {
schema_capsule
.pointer_checked(Some(SCHEMA_NAME))?
.cast::<FFI_ArrowSchema>()
.as_ref()
};
let array_ptr = array_capsule
.pointer_checked(Some(ARRAY_NAME))?
.cast::<FFI_ArrowArray>()
.as_ptr();
let ffi_array = unsafe { FFI_ArrowArray::from_raw(array_ptr) };
let mut array_data = unsafe { ffi::from_ffi(ffi_array, schema_ptr) }.map_err(to_py_err)?;
if !matches!(array_data.data_type(), DataType::Struct(_)) {
return Err(PyTypeError::new_err(
"Expected Struct type from __arrow_c_array.",
));
}
let options = RecordBatchOptions::default().with_row_count(Some(array_data.len()));
// Ensure data is aligned (by potentially copying the buffers).
// This is needed because some python code (for example the
// python flight client) produces unaligned buffers
// See https://github.com/apache/arrow/issues/43552 for details
array_data.align_buffers();
let array = StructArray::from(array_data);
// StructArray does not embed metadata from schema. We need to override
// the output schema with the schema from the capsule.
let schema = Arc::new(Schema::try_from(schema_ptr).map_err(to_py_err)?);
let (_fields, columns, nulls) = array.into_parts();
assert_eq!(
nulls.map(|n| n.null_count()).unwrap_or_default(),
0,
"Cannot convert nullable StructArray to RecordBatch, see StructArray documentation"
);
RecordBatch::try_new_with_options(schema, columns, &options).map_err(to_py_err)
}
}
impl ToPyArrow for RecordBatch {
fn to_pyarrow(&self, py: Python) -> PyResult<Py<PyAny>> {
// Workaround apache/arrow#37669 by returning RecordBatchIterator
let reader = RecordBatchIterator::new(vec![Ok(self.clone())], self.schema());
let reader: Box<dyn RecordBatchReader + Send> = Box::new(reader);
let py_reader = reader.into_pyarrow(py)?;
py_reader.call_method0(py, intern!(py, "read_next_batch"))
}
}
/// Supports conversion from `pyarrow.RecordBatchReader` to [ArrowArrayStreamReader].
impl<'py> FromPyArrow<'_, 'py> for ArrowArrayStreamReader {
fn from_pyarrow(value: &Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
let py = value.py();
if !value.hasattr(intern!(py, "__arrow_c_stream__"))? {
return Err(PyValueError::new_err(
"Expected __arrow_c_stream__ attribute to be set.",
));
}
let capsule = value.getattr(intern!(py, "__arrow_c_stream__"))?.call0()?;
let capsule = capsule.cast::<PyCapsule>()?;
let array_ptr = capsule
.pointer_checked(Some(ARRAY_STREAM_NAME))?
.cast::<FFI_ArrowArrayStream>()
.as_ptr();
let stream = unsafe { FFI_ArrowArrayStream::from_raw(array_ptr) };
let stream_reader = ArrowArrayStreamReader::try_new(stream)
.map_err(|err| PyValueError::new_err(err.to_string()))?;
Ok(stream_reader)
}
}
/// Convert a [`RecordBatchReader`] into a `pyarrow.RecordBatchReader`.
impl IntoPyArrow for Box<dyn RecordBatchReader + Send> {
// We can't implement `ToPyArrow` for `T: RecordBatchReader + Send` because
// there is already a blanket implementation for `T: ToPyArrow`.
fn into_pyarrow(self, py: Python) -> PyResult<Py<PyAny>> {
let mut stream = FFI_ArrowArrayStream::new(self);
let args = PyTuple::new(py, [&raw mut stream as Py_uintptr_t])?;
let reader =
record_batch_reader_class(py)?.call_method1(intern!(py, "_import_from_c"), args)?;
Ok(Py::from(reader))
}
}