forked from apache/iceberg-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
157 lines (136 loc) · 5.04 KB
/
lib.rs
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use std::{error, fmt, sync::Arc};
use iceberg::spec::Transform;
use iceberg::transform::create_transform_function;
use iceberg::Error;
use iceberg::io::FileIOBuilder;
use arrow::{
array::{make_array, Array, ArrayData, ArrayRef},
error::ArrowError,
ffi::{from_ffi, to_ffi},
};
use libc::uintptr_t;
use pyo3::wrap_pyfunction;
use pyo3::{exceptions::PyOSError, exceptions::PyValueError, prelude::*};
#[derive(Debug)]
enum PyO3ArrowError {
ArrowError(ArrowError),
}
#[pyfunction]
fn hello_world() -> PyResult<String> {
let _ = FileIOBuilder::new_fs_io().build().unwrap();
Ok("Hello, world!".to_string())
}
impl fmt::Display for PyO3ArrowError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
PyO3ArrowError::ArrowError(ref e) => e.fmt(f),
}
}
}
impl error::Error for PyO3ArrowError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
// The cause is the underlying implementation error type. Is implicitly
// cast to the trait object `&error::Error`. This works because the
// underlying type already implements the `Error` trait.
PyO3ArrowError::ArrowError(ref e) => Some(e),
}
}
}
impl From<ArrowError> for PyO3ArrowError {
fn from(err: ArrowError) -> PyO3ArrowError {
PyO3ArrowError::ArrowError(err)
}
}
impl From<PyO3ArrowError> for PyErr {
fn from(err: PyO3ArrowError) -> PyErr {
PyOSError::new_err(err.to_string())
}
}
#[derive(Debug)]
enum PyO3IcebergError {
Error(Error),
}
impl fmt::Display for PyO3IcebergError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
PyO3IcebergError::Error(ref e) => e.fmt(f),
}
}
}
impl error::Error for PyO3IcebergError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
// The cause is the underlying implementation error type. Is implicitly
// cast to the trait object `&error::Error`. This works because the
// underlying type already implements the `Error` trait.
PyO3IcebergError::Error(ref e) => Some(e),
}
}
}
impl From<Error> for PyO3IcebergError {
fn from(err: Error) -> PyO3IcebergError {
PyO3IcebergError::Error(err)
}
}
impl From<PyO3IcebergError> for PyErr {
fn from(err: PyO3IcebergError) -> PyErr {
PyValueError::new_err(err.to_string())
}
}
fn to_rust(ob: PyObject, py: Python) -> PyResult<ArrayRef> {
// prepare a pointer to receive the Array struct
let (array, schema) = to_ffi(&ArrayData::new_empty(&arrow::datatypes::DataType::Null))
.map_err(PyO3ArrowError::from)?;
let array_pointer = &array as *const _ as uintptr_t;
let schema_pointer = &schema as *const _ as uintptr_t;
// make the conversion through PyArrow's private API
// this changes the pointer's memory and is thus unsafe. In particular, `_export_to_c` can go out of bounds
ob.call_method1(py, "_export_to_c", (array_pointer, schema_pointer))?;
let array = unsafe { from_ffi(array, &schema) }.map_err(PyO3ArrowError::from)?;
let array = make_array(array);
Ok(array)
}
fn to_py(array: ArrayRef, py: Python) -> PyResult<PyObject> {
let (array, schema) = to_ffi(&array.to_data()).map_err(PyO3ArrowError::from)?;
let array_pointer = &array as *const _ as uintptr_t;
let schema_pointer = &schema as *const _ as uintptr_t;
let pa = py.import_bound("pyarrow")?;
let array = pa.getattr("Array")?.call_method1(
"_import_from_c",
(array_pointer as uintptr_t, schema_pointer as uintptr_t),
)?;
Ok(array.to_object(py))
}
#[pyfunction]
fn bucket_transform(array: PyObject, num_buckets: u32, py: Python) -> PyResult<PyObject> {
// import
let array = to_rust(array, py)?;
let bucket = create_transform_function(&Transform::Bucket(num_buckets)).map_err(PyO3IcebergError::from)?;
let array = bucket.transform(array).map_err(PyO3IcebergError::from)?;
let array = Arc::new(array);
// export
to_py(array, py)
}
#[pymodule]
fn pyiceberg_core_rust(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(hello_world, m)?)?;
m.add_function(wrap_pyfunction!(bucket_transform, m)?)?;
Ok(())
}