|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::sync::Arc; |
| 19 | + |
| 20 | +use arrow::array::{Array, ArrayRef}; |
| 21 | +use arrow::datatypes::{Field, FieldRef}; |
| 22 | +use arrow::ffi::{FFI_ArrowArray, FFI_ArrowSchema}; |
| 23 | +use arrow::pyarrow::ToPyArrow; |
| 24 | +use pyo3::prelude::{PyAnyMethods, PyCapsuleMethods}; |
| 25 | +use pyo3::types::PyCapsule; |
| 26 | +use pyo3::{pyclass, pymethods, Bound, PyAny, PyResult, Python}; |
| 27 | + |
| 28 | +use crate::errors::PyDataFusionResult; |
| 29 | +use crate::utils::validate_pycapsule; |
| 30 | + |
| 31 | +/// A Python object which implements the Arrow PyCapsule for importing |
| 32 | +/// into other libraries. |
| 33 | +#[pyclass(name = "ArrowArrayExportable", module = "datafusion", frozen)] |
| 34 | +#[derive(Clone)] |
| 35 | +pub struct PyArrowArrayExportable { |
| 36 | + array: ArrayRef, |
| 37 | + field: FieldRef, |
| 38 | +} |
| 39 | + |
| 40 | +#[pymethods] |
| 41 | +impl PyArrowArrayExportable { |
| 42 | + #[pyo3(signature = (requested_schema=None))] |
| 43 | + fn __arrow_c_array__<'py>( |
| 44 | + &'py self, |
| 45 | + py: Python<'py>, |
| 46 | + requested_schema: Option<Bound<'py, PyCapsule>>, |
| 47 | + ) -> PyDataFusionResult<(Bound<'py, PyCapsule>, Bound<'py, PyCapsule>)> { |
| 48 | + let field = if let Some(schema_capsule) = requested_schema { |
| 49 | + validate_pycapsule(&schema_capsule, "arrow_schema")?; |
| 50 | + |
| 51 | + let schema_ptr = unsafe { schema_capsule.reference::<FFI_ArrowSchema>() }; |
| 52 | + let desired_field = Field::try_from(schema_ptr)?; |
| 53 | + |
| 54 | + Arc::new(desired_field) |
| 55 | + } else { |
| 56 | + Arc::clone(&self.field) |
| 57 | + }; |
| 58 | + |
| 59 | + let ffi_schema = FFI_ArrowSchema::try_from(&field)?; |
| 60 | + let schema_capsule = PyCapsule::new(py, ffi_schema, Some(cr"arrow_schema".into()))?; |
| 61 | + |
| 62 | + let ffi_array = FFI_ArrowArray::new(&self.array.to_data()); |
| 63 | + let array_capsule = PyCapsule::new(py, ffi_array, Some(cr"arrow_array".into()))?; |
| 64 | + |
| 65 | + Ok((schema_capsule, array_capsule)) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl ToPyArrow for PyArrowArrayExportable { |
| 70 | + fn to_pyarrow<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> { |
| 71 | + let module = py.import("pyarrow")?; |
| 72 | + let method = module.getattr("array")?; |
| 73 | + let array = method.call((self.clone(),), None)?; |
| 74 | + Ok(array) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl PyArrowArrayExportable { |
| 79 | + pub fn new(array: ArrayRef, field: FieldRef) -> Self { |
| 80 | + Self { array, field } |
| 81 | + } |
| 82 | +} |
0 commit comments