forked from apache/iceberg-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into feat-impl-df-filters
- Loading branch information
Showing
34 changed files
with
4,437 additions
and
505 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,24 @@ | ||
// 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 pyo3::exceptions::PyValueError; | ||
use pyo3::PyErr; | ||
|
||
/// Convert an iceberg error to a python error | ||
pub fn to_py_err(err: iceberg::Error) -> PyErr { | ||
PyValueError::new_err(err.to_string()) | ||
} |
This file contains 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 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,93 @@ | ||
// 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 arrow::array::{make_array, Array, ArrayData}; | ||
use arrow::pyarrow::{FromPyArrow, ToPyArrow}; | ||
use iceberg::spec::Transform; | ||
use iceberg::transform::create_transform_function; | ||
use pyo3::prelude::*; | ||
|
||
use crate::error::to_py_err; | ||
|
||
#[pyfunction] | ||
pub fn identity(py: Python, array: PyObject) -> PyResult<PyObject> { | ||
apply(py, array, Transform::Identity) | ||
} | ||
|
||
#[pyfunction] | ||
pub fn void(py: Python, array: PyObject) -> PyResult<PyObject> { | ||
apply(py, array, Transform::Void) | ||
} | ||
|
||
#[pyfunction] | ||
pub fn year(py: Python, array: PyObject) -> PyResult<PyObject> { | ||
apply(py, array, Transform::Year) | ||
} | ||
|
||
#[pyfunction] | ||
pub fn month(py: Python, array: PyObject) -> PyResult<PyObject> { | ||
apply(py, array, Transform::Month) | ||
} | ||
|
||
#[pyfunction] | ||
pub fn day(py: Python, array: PyObject) -> PyResult<PyObject> { | ||
apply(py, array, Transform::Day) | ||
} | ||
|
||
#[pyfunction] | ||
pub fn hour(py: Python, array: PyObject) -> PyResult<PyObject> { | ||
apply(py, array, Transform::Hour) | ||
} | ||
|
||
#[pyfunction] | ||
pub fn bucket(py: Python, array: PyObject, num_buckets: u32) -> PyResult<PyObject> { | ||
apply(py, array, Transform::Bucket(num_buckets)) | ||
} | ||
|
||
#[pyfunction] | ||
pub fn truncate(py: Python, array: PyObject, width: u32) -> PyResult<PyObject> { | ||
apply(py, array, Transform::Truncate(width)) | ||
} | ||
|
||
fn apply(py: Python, array: PyObject, transform: Transform) -> 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) | ||
} | ||
|
||
pub fn register_module(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { | ||
let this = PyModule::new_bound(py, "transform")?; | ||
|
||
this.add_function(wrap_pyfunction!(identity, &this)?)?; | ||
this.add_function(wrap_pyfunction!(void, &this)?)?; | ||
this.add_function(wrap_pyfunction!(year, &this)?)?; | ||
this.add_function(wrap_pyfunction!(month, &this)?)?; | ||
this.add_function(wrap_pyfunction!(day, &this)?)?; | ||
this.add_function(wrap_pyfunction!(hour, &this)?)?; | ||
this.add_function(wrap_pyfunction!(bucket, &this)?)?; | ||
this.add_function(wrap_pyfunction!(truncate, &this)?)?; | ||
|
||
m.add_submodule(&this)?; | ||
py.import_bound("sys")? | ||
.getattr("modules")? | ||
.set_item("pyiceberg_core.transform", this) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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,99 @@ | ||
# 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 transform | ||
|
||
|
||
def test_identity_transform(): | ||
arr = pa.array([1, 2]) | ||
result = transform.identity(arr) | ||
assert result == arr | ||
|
||
|
||
def test_bucket_transform(): | ||
arr = pa.array([1, 2]) | ||
result = transform.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", | ||
): | ||
transform.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(transform.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 = transform.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 = transform.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 = transform.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 = transform.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 = transform.truncate(arr, 5) | ||
expected = pa.array(["this ", "hi my"]) | ||
assert result == expected | ||
|
||
|
||
def test_identity_transform_with_direct_import(): | ||
from pyiceberg_core.transform import identity | ||
|
||
arr = pa.array([1, 2]) | ||
result = identity(arr) | ||
assert result == arr |
This file contains 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 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
Oops, something went wrong.