You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "sungwy (via GitHub)" <gi...@apache.org> on 2024/08/16 15:02:38 UTC

[PR] Expose Bucket Transform to Python Binding [iceberg-rust]

sungwy opened a new pull request, #556:
URL: https://github.com/apache/iceberg-rust/pull/556

   (no comment)


-- 
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-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Expose Transforms to Python Binding [iceberg-rust]

Posted by "sungwy (via GitHub)" <gi...@apache.org>.
sungwy commented on PR #556:
URL: https://github.com/apache/iceberg-rust/pull/556#issuecomment-2311260788

   > Thanks @sungwy for this pr, LGTM!
   
   Thank you @liurenjie1024 !! 🙌


-- 
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-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Expose Transforms to Python Binding [iceberg-rust]

Posted by "sungwy (via GitHub)" <gi...@apache.org>.
sungwy commented on PR #556:
URL: https://github.com/apache/iceberg-rust/pull/556#issuecomment-2312483025

   Thank you @Xuanwo for all your help!


-- 
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-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Expose Bucket Transform to Python Binding [iceberg-rust]

Posted by "Xuanwo (via GitHub)" <gi...@apache.org>.
Xuanwo commented on code in PR #556:
URL: https://github.com/apache/iceberg-rust/pull/556#discussion_r1724448667


##########
bindings/python/src/transform.rs:
##########
@@ -0,0 +1,75 @@
+// 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};
+use iceberg::spec::Transform;
+use iceberg::transform::create_transform_function;
+use iceberg::Error;
+
+use arrow::{
+    array::{make_array, Array, ArrayData},
+};
+use arrow::pyarrow::{FromPyArrow, ToPyArrow};
+use pyo3::{exceptions::PyValueError, prelude::*};
+
+#[derive(Debug)]
+enum PyO3IcebergError {

Review Comment:
   How about implementing a simple `fn to_py_err(err: iceberg::Error) -> PyError`. So we can use:
   
   ```rust
   let bucket = create_transform_function(&Transform::Bucket(num_buckets)).map_err(to_py_err)?;
   ```
   
   But no need for implementing `Display`, `From` by hand.



##########
bindings/python/src/lib.rs:
##########
@@ -25,7 +28,25 @@ fn hello_world() -> PyResult<String> {
 }
 
 #[pymodule]
-fn pyiceberg_core_rust(m: &Bound<'_, PyModule>) -> PyResult<()> {
+fn submodule(_py: Python, module: &Bound<'_, PyModule>) -> PyResult<()> {
+    use transform::bucket_transform;
+
+    module.add_wrapped(wrap_pyfunction!(bucket_transform))?;
+    Ok(())
+}
+
+#[pymodule]
+fn pyiceberg_core_rust(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
     m.add_function(wrap_pyfunction!(hello_world, m)?)?;
+
+    // https://github.com/PyO3/pyo3/issues/759

Review Comment:
   I will explore better ways for this. Not a blocker of this PR.



-- 
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-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Expose Bucket Transform to Python Binding [iceberg-rust]

Posted by "sungwy (via GitHub)" <gi...@apache.org>.
sungwy commented on code in PR #556:
URL: https://github.com/apache/iceberg-rust/pull/556#discussion_r1720136976


##########
bindings/python/pyproject.toml:
##########
@@ -31,8 +31,30 @@ classifiers = [
   "Programming Language :: Python :: 3.12",
 ]
 
-[project.optional-dependencies]
-test = ["pytest"]
+# Duplicate section required: https://github.com/python-poetry/poetry/pull/9135

Review Comment:
   There's an open item to allow poetry to use [project] section instead of [tool.poetry]. I'm exploring using poetry for dependency management because even installing test dependencies is starting to feel a little strange with a simple pyproject.toml file, since we need to maintain a separate list of dependencies in `.github/workflows/bindings_python_ci.yml` as well as in bindings/python/pyproject.toml (there's no way to install just the optional dependencies using pip or maturin, it only supports editable install of the root project in addition to the extra dependencies)
   
   We could also exploring using other python dependency managers as well.



-- 
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-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Expose Transforms to Python Binding [iceberg-rust]

Posted by "Xuanwo (via GitHub)" <gi...@apache.org>.
Xuanwo merged PR #556:
URL: https://github.com/apache/iceberg-rust/pull/556


-- 
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-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Expose Transforms to Python Binding [iceberg-rust]

Posted by "liurenjie1024 (via GitHub)" <gi...@apache.org>.
liurenjie1024 commented on PR #556:
URL: https://github.com/apache/iceberg-rust/pull/556#issuecomment-2309627634

   Waiting for @Xuanwo 's second review.


-- 
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-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Expose Bucket Transform to Python Binding [iceberg-rust]

Posted by "sungwy (via GitHub)" <gi...@apache.org>.
sungwy commented on code in PR #556:
URL: https://github.com/apache/iceberg-rust/pull/556#discussion_r1725497415


##########
bindings/python/src/transform.rs:
##########
@@ -0,0 +1,75 @@
+// 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};
+use iceberg::spec::Transform;
+use iceberg::transform::create_transform_function;
+use iceberg::Error;
+
+use arrow::{
+    array::{make_array, Array, ArrayData},
+};
+use arrow::pyarrow::{FromPyArrow, ToPyArrow};
+use pyo3::{exceptions::PyValueError, prelude::*};
+
+#[derive(Debug)]
+enum PyO3IcebergError {

Review Comment:
   That worked like a charm! Thanks @Xuanwo 



-- 
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-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Expose Bucket Transform to Python Binding [iceberg-rust]

Posted by "sungwy (via GitHub)" <gi...@apache.org>.
sungwy commented on code in PR #556:
URL: https://github.com/apache/iceberg-rust/pull/556#discussion_r1720136976


##########
bindings/python/pyproject.toml:
##########
@@ -31,8 +31,30 @@ classifiers = [
   "Programming Language :: Python :: 3.12",
 ]
 
-[project.optional-dependencies]
-test = ["pytest"]
+# Duplicate section required: https://github.com/python-poetry/poetry/pull/9135

Review Comment:
   There's an open item to allow poetry to use [project] section instead of [tool.poetry]
   
    I'm exploring using poetry for dependency management because even installing test dependencies is starting to feel a little strange with a simple pyproject.toml file, since we need to maintain a separate list of dependencies in `.github/workflows/bindings_python_ci.yml` as well as in bindings/python/pyproject.toml (there's no way to install just the optional dependencies using pip or maturin, it only supports editable install of the root project in addition to the extra dependencies). But this duplication of the two very similar sections ([project] for maturin, and [tool.poetry] for poetry) also feels suboptimal...
   
   We could also exploring using other python dependency managers as well.



-- 
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-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Expose Bucket Transform to Python Binding [iceberg-rust]

Posted by "sungwy (via GitHub)" <gi...@apache.org>.
sungwy commented on code in PR #556:
URL: https://github.com/apache/iceberg-rust/pull/556#discussion_r1720311292


##########
bindings/python/pyproject.toml:
##########
@@ -31,8 +31,30 @@ classifiers = [
   "Programming Language :: Python :: 3.12",
 ]
 
-[project.optional-dependencies]
-test = ["pytest"]
+# Duplicate section required: https://github.com/python-poetry/poetry/pull/9135

Review Comment:
   Taking my own advice - using pypa/hatch instead: https://hatch.pypa.io/latest/



-- 
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-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Expose Bucket Transform to Python Binding [iceberg-rust]

Posted by "sungwy (via GitHub)" <gi...@apache.org>.
sungwy commented on code in PR #556:
URL: https://github.com/apache/iceberg-rust/pull/556#discussion_r1722066450


##########
.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:
   Agreed. I put up this PR as a follow up: https://github.com/apache/iceberg-rust/pull/572



-- 
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-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Expose Bucket Transform to Python Binding [iceberg-rust]

Posted by "Xuanwo (via GitHub)" <gi...@apache.org>.
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-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Expose Bucket Transform to Python Binding [iceberg-rust]

Posted by "sungwy (via GitHub)" <gi...@apache.org>.
sungwy commented on PR #556:
URL: https://github.com/apache/iceberg-rust/pull/556#issuecomment-2302976618

   > Thanks! Most changes look good to me. Here are some nits.
   
   Hi @Xuanwo - Given that there's an outstanding issue with the submodules, I decided to just implement a class and staticmethods instead so that we can make use of the usage pattern proposed in the tests. Please let me know what you think.
   
   I've added the conversions for all of the existing transforms for completeness.
   
   The test suite prioritizes verifying the integration, rather than the correctness of the transformed values which is already verified in the rust based test suites.


-- 
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-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Expose Bucket Transform to Python Binding [iceberg-rust]

Posted by "sungwy (via GitHub)" <gi...@apache.org>.
sungwy commented on PR #556:
URL: https://github.com/apache/iceberg-rust/pull/556#issuecomment-2297298410

   Thank you for the reviews @Xuanwo - I've taken all of your feedback and refactored the code to use the arrow::pyarrow crate


-- 
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-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org