Xuanwo commented on code in PR #556: URL: https://github.com/apache/iceberg-rust/pull/556#discussion_r1721162878
########## bindings/python/src/transform.rs: ########## @@ -0,0 +1,142 @@ +// 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 arrow::{ + array::{make_array, Array, ArrayData, ArrayRef}, + error::ArrowError, + ffi::{from_ffi, to_ffi}, +}; +use libc::uintptr_t; +use pyo3::{exceptions::PyOSError, exceptions::PyValueError, prelude::*}; + +#[derive(Debug)] +enum PyO3ArrowError { + ArrowError(ArrowError), +} + +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_arrow_array(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; Review Comment: I'm not comfortable using libc directly. How about using `pyo3::ffi::Py_uintptr_t` instead? ########## .github/workflows/bindings_python_ci.yml: ########## @@ -78,6 +78,6 @@ jobs: shell: bash run: | set -e - pip install dist/pyiceberg_core-*.whl --force-reinstall - pip install pytest - pytest -v + pip install hatch==1.12.0 Review Comment: How about split workflow related changes to another PR? ########## bindings/python/src/lib.rs: ########## @@ -26,6 +29,8 @@ fn hello_world() -> PyResult<String> { #[pymodule] fn pyiceberg_core_rust(m: &Bound<'_, PyModule>) -> PyResult<()> { + use transform::bucket_transform; m.add_function(wrap_pyfunction!(hello_world, m)?)?; + m.add_function(wrap_pyfunction!(bucket_transform, m)?)?; Review Comment: pyo3 supports modules, how about adding a `transform` module like this? ```rust use pyo3::prelude::*; #[pymodule] fn pyiceberg_core_rust(m: &Bound<'_, PyModule>) -> PyResult<()> { let transform_module = PyModule::new_bound(parent_module.py(), "transform")?; child_module.add_function(wrap_pyfunction!(bucket_transform, &transform_module)?)?; parent_module.add_submodule(&transform_module) } ``` ########## bindings/python/src/lib.rs: ########## @@ -17,6 +17,9 @@ use iceberg::io::FileIOBuilder; use pyo3::prelude::*; +use pyo3::wrap_pyfunction; + +mod transform; Review Comment: I like this idea ########## .licenserc.yaml: ########## @@ -31,4 +31,6 @@ header: - '**/DEPENDENCIES.*.tsv' # Release distributions - 'dist/*' + # Generated content by poetry + - poetry.lock Review Comment: After switching to `hatch`, do we still need this? ########## bindings/python/src/transform.rs: ########## @@ -0,0 +1,142 @@ +// 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 arrow::{ + array::{make_array, Array, ArrayData, ArrayRef}, + error::ArrowError, + ffi::{from_ffi, to_ffi}, +}; +use libc::uintptr_t; +use pyo3::{exceptions::PyOSError, exceptions::PyValueError, prelude::*}; + +#[derive(Debug)] +enum PyO3ArrowError { Review Comment: I believe most ffi related to arrow has been handled in [`arrow-rs/pyarrow`](https://github.com/apache/arrow-rs/blob/master/arrow/src/pyarrow.rs). Would you like to take a look? Maybe we can reuse them. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org