use minarrow::{Array, Field, FieldArray, SuperArray, SuperTable, Table};
use pyo3::prelude::*;
use std::sync::Arc;
use crate::ffi::{to_py, to_rust};
#[repr(transparent)]
#[derive(Debug, Clone)]
pub struct PyArray(pub FieldArray);
impl PyArray {
pub fn new(field_array: FieldArray) -> Self {
Self(field_array)
}
pub fn inner(&self) -> &Array {
&self.0.array
}
pub fn field_array(&self) -> &FieldArray {
&self.0
}
pub fn field(&self) -> &Field {
&self.0.field
}
pub fn into_inner(self) -> FieldArray {
self.0
}
}
impl From<FieldArray> for PyArray {
fn from(field_array: FieldArray) -> Self {
Self(field_array)
}
}
impl From<Arc<Array>> for PyArray {
fn from(array: Arc<Array>) -> Self {
let field = Field::from_array("", &array, None);
Self(FieldArray::new(field, (*array).clone()))
}
}
impl From<Array> for PyArray {
fn from(array: Array) -> Self {
let field = Field::from_array("", &array, None);
Self(FieldArray::new(field, array))
}
}
impl From<PyArray> for FieldArray {
fn from(value: PyArray) -> Self {
value.0
}
}
impl From<PyArray> for Arc<Array> {
fn from(value: PyArray) -> Self {
Arc::new(value.0.array)
}
}
impl AsRef<Array> for PyArray {
fn as_ref(&self) -> &Array {
&self.0.array
}
}
impl<'py> FromPyObject<'py> for PyArray {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
let field_array = to_rust::array_to_rust(ob)?;
Ok(PyArray(field_array))
}
}
impl<'py> IntoPyObject<'py> for PyArray {
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
to_py::array_to_py(Arc::new(self.0.array), &self.0.field, py)
}
}
#[repr(transparent)]
#[derive(Debug, Clone)]
pub struct PyRecordBatch(pub Table);
impl PyRecordBatch {
pub fn new(table: Table) -> Self {
Self(table)
}
pub fn inner(&self) -> &Table {
&self.0
}
pub fn into_inner(self) -> Table {
self.0
}
}
impl From<Table> for PyRecordBatch {
fn from(table: Table) -> Self {
Self(table)
}
}
impl From<PyRecordBatch> for Table {
fn from(value: PyRecordBatch) -> Self {
value.0
}
}
impl AsRef<Table> for PyRecordBatch {
fn as_ref(&self) -> &Table {
&self.0
}
}
impl<'py> FromPyObject<'py> for PyRecordBatch {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
let table = to_rust::record_batch_to_rust(ob)?;
Ok(PyRecordBatch(table))
}
}
impl<'py> IntoPyObject<'py> for PyRecordBatch {
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
to_py::table_to_py(&self.0, py)
}
}
#[repr(transparent)]
#[derive(Debug, Clone)]
pub struct PyField(pub Field);
impl PyField {
pub fn new(field: Field) -> Self {
Self(field)
}
pub fn inner(&self) -> &Field {
&self.0
}
pub fn into_inner(self) -> Field {
self.0
}
}
impl From<Field> for PyField {
fn from(field: Field) -> Self {
Self(field)
}
}
impl From<PyField> for Field {
fn from(value: PyField) -> Self {
value.0
}
}
impl AsRef<Field> for PyField {
fn as_ref(&self) -> &Field {
&self.0
}
}
#[repr(transparent)]
#[derive(Debug, Clone)]
pub struct PyTable(pub SuperTable);
impl PyTable {
pub fn new(table: SuperTable) -> Self {
Self(table)
}
pub fn inner(&self) -> &SuperTable {
&self.0
}
pub fn into_inner(self) -> SuperTable {
self.0
}
}
impl From<SuperTable> for PyTable {
fn from(table: SuperTable) -> Self {
Self(table)
}
}
impl From<PyTable> for SuperTable {
fn from(value: PyTable) -> Self {
value.0
}
}
impl AsRef<SuperTable> for PyTable {
fn as_ref(&self) -> &SuperTable {
&self.0
}
}
impl<'py> FromPyObject<'py> for PyTable {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
let table = to_rust::table_to_rust(ob)?;
Ok(PyTable(table))
}
}
impl<'py> IntoPyObject<'py> for PyTable {
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
to_py::super_table_to_py(&self.0, py)
}
}
#[repr(transparent)]
#[derive(Debug, Clone)]
pub struct PyChunkedArray(pub SuperArray);
impl PyChunkedArray {
pub fn new(array: SuperArray) -> Self {
Self(array)
}
pub fn inner(&self) -> &SuperArray {
&self.0
}
pub fn into_inner(self) -> SuperArray {
self.0
}
}
impl From<SuperArray> for PyChunkedArray {
fn from(array: SuperArray) -> Self {
Self(array)
}
}
impl From<PyChunkedArray> for SuperArray {
fn from(value: PyChunkedArray) -> Self {
value.0
}
}
impl AsRef<SuperArray> for PyChunkedArray {
fn as_ref(&self) -> &SuperArray {
&self.0
}
}
impl<'py> FromPyObject<'py> for PyChunkedArray {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
let array = to_rust::chunked_array_to_rust(ob)?;
Ok(PyChunkedArray(array))
}
}
impl<'py> IntoPyObject<'py> for PyChunkedArray {
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
to_py::super_array_to_py(&self.0, py)
}
}