1515// specific language governing permissions and limitations
1616// under the License.
1717
18+ use arrow:: array:: { make_array, Array , ArrayData } ;
19+ use arrow:: pyarrow:: { FromPyArrow , ToPyArrow } ;
1820use iceberg:: spec:: Transform ;
1921use iceberg:: transform:: create_transform_function;
22+ use pyo3:: prelude:: * ;
2023
21- use arrow:: {
22- array:: { make_array, Array , ArrayData } ,
23- } ;
24- use arrow:: pyarrow:: { FromPyArrow , ToPyArrow } ;
25- use pyo3:: { exceptions:: PyValueError , prelude:: * } ;
24+ use crate :: error:: to_py_err;
25+
26+ #[ pyfunction]
27+ pub fn identity ( py : Python , array : PyObject ) -> PyResult < PyObject > {
28+ apply ( py, array, Transform :: Identity )
29+ }
30+
31+ #[ pyfunction]
32+ pub fn void ( py : Python , array : PyObject ) -> PyResult < PyObject > {
33+ apply ( py, array, Transform :: Void )
34+ }
35+
36+ #[ pyfunction]
37+ pub fn year ( py : Python , array : PyObject ) -> PyResult < PyObject > {
38+ apply ( py, array, Transform :: Year )
39+ }
40+
41+ #[ pyfunction]
42+ pub fn month ( py : Python , array : PyObject ) -> PyResult < PyObject > {
43+ apply ( py, array, Transform :: Month )
44+ }
2645
27- fn to_py_err ( err : iceberg:: Error ) -> PyErr {
28- PyValueError :: new_err ( err. to_string ( ) )
46+ #[ pyfunction]
47+ pub fn day ( py : Python , array : PyObject ) -> PyResult < PyObject > {
48+ apply ( py, array, Transform :: Day )
2949}
3050
31- #[ pyclass]
32- pub struct ArrowArrayTransform {
51+ #[ pyfunction]
52+ pub fn hour ( py : Python , array : PyObject ) -> PyResult < PyObject > {
53+ apply ( py, array, Transform :: Hour )
3354}
3455
35- fn apply ( array : PyObject , transform : Transform , py : Python ) -> PyResult < PyObject > {
56+ #[ pyfunction]
57+ pub fn bucket ( py : Python , array : PyObject , num_buckets : u32 ) -> PyResult < PyObject > {
58+ apply ( py, array, Transform :: Bucket ( num_buckets) )
59+ }
60+
61+ #[ pyfunction]
62+ pub fn truncate ( py : Python , array : PyObject , width : u32 ) -> PyResult < PyObject > {
63+ apply ( py, array, Transform :: Truncate ( width) )
64+ }
65+
66+ fn apply ( py : Python , array : PyObject , transform : Transform ) -> PyResult < PyObject > {
3667 // import
3768 let array = ArrayData :: from_pyarrow_bound ( array. bind ( py) ) ?;
3869 let array = make_array ( array) ;
@@ -43,45 +74,20 @@ fn apply(array: PyObject, transform: Transform, py: Python) -> PyResult<PyObject
4374 array. to_pyarrow ( py)
4475}
4576
46- #[ pymethods]
47- impl ArrowArrayTransform {
48- #[ staticmethod]
49- pub fn identity ( array : PyObject , py : Python ) -> PyResult < PyObject > {
50- apply ( array, Transform :: Identity , py)
51- }
52-
53- #[ staticmethod]
54- pub fn void ( array : PyObject , py : Python ) -> PyResult < PyObject > {
55- apply ( array, Transform :: Void , py)
56- }
57-
58- #[ staticmethod]
59- pub fn year ( array : PyObject , py : Python ) -> PyResult < PyObject > {
60- apply ( array, Transform :: Year , py)
61- }
62-
63- #[ staticmethod]
64- pub fn month ( array : PyObject , py : Python ) -> PyResult < PyObject > {
65- apply ( array, Transform :: Month , py)
66- }
67-
68- #[ staticmethod]
69- pub fn day ( array : PyObject , py : Python ) -> PyResult < PyObject > {
70- apply ( array, Transform :: Day , py)
71- }
72-
73- #[ staticmethod]
74- pub fn hour ( array : PyObject , py : Python ) -> PyResult < PyObject > {
75- apply ( array, Transform :: Hour , py)
76- }
77+ pub fn register_module ( py : Python < ' _ > , m : & Bound < ' _ , PyModule > ) -> PyResult < ( ) > {
78+ let this = PyModule :: new_bound ( py, "transform" ) ?;
7779
78- #[ staticmethod]
79- pub fn bucket ( array : PyObject , num_buckets : u32 , py : Python ) -> PyResult < PyObject > {
80- apply ( array, Transform :: Bucket ( num_buckets) , py)
81- }
80+ this. add_function ( wrap_pyfunction ! ( identity, & this) ?) ?;
81+ this. add_function ( wrap_pyfunction ! ( void, & this) ?) ?;
82+ this. add_function ( wrap_pyfunction ! ( year, & this) ?) ?;
83+ this. add_function ( wrap_pyfunction ! ( month, & this) ?) ?;
84+ this. add_function ( wrap_pyfunction ! ( day, & this) ?) ?;
85+ this. add_function ( wrap_pyfunction ! ( hour, & this) ?) ?;
86+ this. add_function ( wrap_pyfunction ! ( bucket, & this) ?) ?;
87+ this. add_function ( wrap_pyfunction ! ( truncate, & this) ?) ?;
8288
83- # [ staticmethod ]
84- pub fn truncate ( array : PyObject , width : u32 , py : Python ) -> PyResult < PyObject > {
85- apply ( array , Transform :: Truncate ( width ) , py )
86- }
89+ m . add_submodule ( & this ) ? ;
90+ py . import_bound ( "sys" ) ?
91+ . getattr ( "modules" ) ?
92+ . set_item ( "pyiceberg_core.transform" , this )
8793}
0 commit comments