-
Notifications
You must be signed in to change notification settings - Fork 261
Expose Transforms to Python Binding #556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9229a88
bucket transform rust binding
sungwy 3106792
format
sungwy 1930df2
poetry x maturin
sungwy 7ebcf5c
ignore poetry.lock in license check
sungwy 683a9cc
update bindings_python_ci to use makefile
sungwy 5a5bff5
newline
sungwy 7f6c653
https://github.com/python-poetry/poetry/pull/9135
sungwy 2ccbffe
use hatch instead of poetry
sungwy fce09ae
refactor
sungwy 2a3bdb3
revert licenserc change
sungwy 3308626
adopt review feedback
sungwy 6dadb98
Merge branch 'main' into pybind-bucket-transform
sungwy 3bc9a01
Merge branch 'main' into pybind-bucket-transform
sungwy 3425277
comments
sungwy fdff7e9
unused dependency
sungwy 6e1b411
adopt review comment
sungwy 1f8eebc
newline
sungwy ac4c42d
I like this approach a lot better
sungwy 743f161
more tests
sungwy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,5 @@ dist/* | |
**/venv | ||
*.so | ||
*.pyc | ||
*.whl | ||
*.tar.gz |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// 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 iceberg::spec::Transform; | ||
use iceberg::transform::create_transform_function; | ||
|
||
use arrow::{ | ||
array::{make_array, Array, ArrayData}, | ||
}; | ||
use arrow::pyarrow::{FromPyArrow, ToPyArrow}; | ||
use pyo3::{exceptions::PyValueError, prelude::*}; | ||
|
||
fn to_py_err(err: iceberg::Error) -> PyErr { | ||
PyValueError::new_err(err.to_string()) | ||
} | ||
|
||
#[pyclass] | ||
pub struct ArrowArrayTransform { | ||
} | ||
|
||
fn apply(array: PyObject, transform: Transform, py: Python) -> PyResult<PyObject> { | ||
// import | ||
let array = ArrayData::from_pyarrow_bound(array.bind(py))?; | ||
let array = make_array(array); | ||
let transform_function = create_transform_function(&transform).map_err(to_py_err)?; | ||
let array = transform_function.transform(array).map_err(to_py_err)?; | ||
// export | ||
let array = array.into_data(); | ||
array.to_pyarrow(py) | ||
} | ||
|
||
#[pymethods] | ||
impl ArrowArrayTransform { | ||
#[staticmethod] | ||
pub fn identity(array: PyObject, py: Python) -> PyResult<PyObject> { | ||
apply(array, Transform::Identity, py) | ||
} | ||
|
||
#[staticmethod] | ||
pub fn void(array: PyObject, py: Python) -> PyResult<PyObject> { | ||
apply(array, Transform::Void, py) | ||
} | ||
|
||
#[staticmethod] | ||
pub fn year(array: PyObject, py: Python) -> PyResult<PyObject> { | ||
apply(array, Transform::Year, py) | ||
} | ||
|
||
#[staticmethod] | ||
pub fn month(array: PyObject, py: Python) -> PyResult<PyObject> { | ||
apply(array, Transform::Month, py) | ||
} | ||
|
||
#[staticmethod] | ||
pub fn day(array: PyObject, py: Python) -> PyResult<PyObject> { | ||
apply(array, Transform::Day, py) | ||
} | ||
|
||
#[staticmethod] | ||
pub fn hour(array: PyObject, py: Python) -> PyResult<PyObject> { | ||
apply(array, Transform::Hour, py) | ||
} | ||
|
||
#[staticmethod] | ||
pub fn bucket(array: PyObject, num_buckets: u32, py: Python) -> PyResult<PyObject> { | ||
apply(array, Transform::Bucket(num_buckets), py) | ||
} | ||
|
||
#[staticmethod] | ||
pub fn truncate(array: PyObject, width: u32, py: Python) -> PyResult<PyObject> { | ||
apply(array, Transform::Truncate(width), py) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# 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. | ||
|
||
from datetime import date, datetime | ||
|
||
import pyarrow as pa | ||
import pytest | ||
from pyiceberg_core import ArrowArrayTransform | ||
|
||
|
||
def test_identity_transform(): | ||
arr = pa.array([1, 2]) | ||
result = ArrowArrayTransform.identity(arr) | ||
assert result == arr | ||
|
||
|
||
def test_bucket_transform(): | ||
arr = pa.array([1, 2]) | ||
result = ArrowArrayTransform.bucket(arr, 10) | ||
expected = pa.array([6, 2], type=pa.int32()) | ||
assert result == expected | ||
|
||
|
||
def test_bucket_transform_fails_for_list_type_input(): | ||
arr = pa.array([[1, 2], [3, 4]]) | ||
with pytest.raises( | ||
ValueError, | ||
match=r"FeatureUnsupported => Unsupported data type for bucket transform", | ||
): | ||
ArrowArrayTransform.bucket(arr, 10) | ||
|
||
|
||
def test_bucket_chunked_array(): | ||
chunked = pa.chunked_array([pa.array([1, 2]), pa.array([3, 4])]) | ||
result_chunks = [] | ||
for arr in chunked.iterchunks(): | ||
result_chunks.append(ArrowArrayTransform.bucket(arr, 10)) | ||
|
||
expected = pa.chunked_array( | ||
[pa.array([6, 2], type=pa.int32()), pa.array([5, 0], type=pa.int32())] | ||
) | ||
assert pa.chunked_array(result_chunks).equals(expected) | ||
|
||
|
||
def test_year_transform(): | ||
arr = pa.array([date(1970, 1, 1), date(2000, 1, 1)]) | ||
result = ArrowArrayTransform.year(arr) | ||
expected = pa.array([0, 30], type=pa.int32()) | ||
assert result == expected | ||
|
||
|
||
def test_month_transform(): | ||
arr = pa.array([date(1970, 1, 1), date(2000, 4, 1)]) | ||
result = ArrowArrayTransform.month(arr) | ||
expected = pa.array([0, 30 * 12 + 3], type=pa.int32()) | ||
assert result == expected | ||
|
||
|
||
def test_day_transform(): | ||
arr = pa.array([date(1970, 1, 1), date(2000, 4, 1)]) | ||
result = ArrowArrayTransform.day(arr) | ||
expected = pa.array([0, 11048], type=pa.int32()) | ||
assert result == expected | ||
|
||
|
||
def test_hour_transform(): | ||
arr = pa.array([datetime(1970, 1, 1, 19, 1, 23), datetime(2000, 3, 1, 12, 1, 23)]) | ||
result = ArrowArrayTransform.hour(arr) | ||
expected = pa.array([19, 264420], type=pa.int32()) | ||
assert result == expected | ||
|
||
|
||
def test_truncate_transform(): | ||
arr = pa.array(["this is a long string", "hi my name is sung"]) | ||
result = ArrowArrayTransform.truncate(arr, 5) | ||
expected = pa.array(["this ", "hi my"]) | ||
assert result == expected |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.