@@ -23,12 +23,37 @@ use iceberg::spec::{
23
23
ManifestList , ManifestStatus , PrimitiveLiteral ,
24
24
} ;
25
25
use pyo3:: prelude:: * ;
26
+ use pyo3:: IntoPyObjectExt ;
27
+ use pyo3:: types:: { PyBytes } ;
28
+
26
29
27
- #[ pyclass]
28
30
#[ allow( dead_code) ]
29
31
pub struct PyPrimitiveLiteral {
30
- inner : Option < PrimitiveLiteral > ,
32
+ inner : PrimitiveLiteral
31
33
}
34
+ //
35
+ // impl<'py> IntoPyObject<'py> for PyPrimitiveLiteral {
36
+ // type Target = PyAny; // the Python type
37
+ // type Output = Bound<'py, Self::Target>; // in most cases this will be `Bound`
38
+ // type Error = std::convert::Infallible; // the conversion error type, has to be convertable to `PyErr`
39
+ //
40
+ fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
41
+ match self . inner {
42
+ PrimitiveLiteral :: Boolean ( v) => Ok ( v. into_py_any ( py) ) ,
43
+ PrimitiveLiteral :: Int ( v) => Ok ( v. into_py_any ( py) ) ,
44
+ PrimitiveLiteral :: Long ( v) =>Ok ( v. into_py_any ( py) ) ,
45
+ PrimitiveLiteral :: Float ( v) => Ok ( v. 0 . into_py_any ( py) ) , // unwrap OrderedFloat
46
+ PrimitiveLiteral :: Double ( v) =>Ok ( v. 0 . into_py_any ( py) ) ,
47
+ PrimitiveLiteral :: String ( v) =>Ok ( v. into_py_any ( py) ) ,
48
+ PrimitiveLiteral :: Binary ( v) =>Ok ( PyBytes :: new ( py, & v) . into_py_any ( py) ) ,
49
+ PrimitiveLiteral :: Int128 ( v) => Ok ( v. into_py_any ( py) ) , // Python handles big ints
50
+ PrimitiveLiteral :: UInt128 ( v) =>Ok ( v. into_py_any ( py) ) ,
51
+ PrimitiveLiteral :: AboveMax => Err ( "AboveMax is not supported" ) ,
52
+ PrimitiveLiteral :: BelowMin => Err ( "BelowMin is not supported" ) ,
53
+ }
54
+ }
55
+ // }
56
+
32
57
33
58
#[ pyclass]
34
59
pub struct PyDataFile {
@@ -57,15 +82,33 @@ impl PyDataFile {
57
82
}
58
83
}
59
84
85
+ fn into_pyobject ( self , lit : PrimitiveLiteral , py : Python < ' py > ) -> Option < PyObject > {
86
+ match lit {
87
+ PrimitiveLiteral :: Boolean ( v) => v,
88
+ PrimitiveLiteral :: Int ( v) => v,
89
+ PrimitiveLiteral :: Long ( v) => Some ( v. into_py_any ( py) ) ,
90
+ PrimitiveLiteral :: Float ( v) => Some ( v. 0 . into_py_any ( py) ) , // unwrap OrderedFloat
91
+ PrimitiveLiteral :: Double ( v) =>Some ( v. 0 . into_py_any ( py) ) ,
92
+ PrimitiveLiteral :: String ( v) =>Some ( v. into_py_any ( py) ) ,
93
+ PrimitiveLiteral :: Binary ( v) =>Some ( PyBytes :: new ( py, & v) . into_py_any ( py) ) ,
94
+ PrimitiveLiteral :: Int128 ( v) => Some ( v. into_py_any ( py) ) , // Python handles big ints
95
+ PrimitiveLiteral :: UInt128 ( v) => Some ( v. into_py_any ( py) ) ,
96
+ PrimitiveLiteral :: AboveMax => None ,
97
+ PrimitiveLiteral :: BelowMin => None ,
98
+ }
99
+ }
100
+
60
101
#[ getter]
61
102
fn partition ( & self ) -> Vec < PyPrimitiveLiteral > {
62
- self . inner
63
- . partition ( )
64
- . iter ( )
65
- . map ( |lit| PyPrimitiveLiteral {
66
- inner : lit. map ( |l| l. as_primitive_literal ( ) . unwrap ( ) ) ,
67
- } )
68
- . collect ( )
103
+ Python :: with_gil ( |py| {
104
+ self . inner
105
+ . partition ( )
106
+ . iter ( )
107
+ . map ( |lit|
108
+ lit. map ( |l| into_pyobject ( l) )
109
+ )
110
+ . collect ( )
111
+ } )
69
112
}
70
113
71
114
#[ getter]
@@ -269,7 +312,7 @@ impl crate::manifest::PyManifestFile {
269
312
}
270
313
271
314
#[ getter]
272
- fn key_metadata ( & self ) -> Vec < u8 > {
315
+ fn key_metadata ( & self ) -> Option < Vec < u8 > > {
273
316
self . inner . key_metadata . clone ( )
274
317
}
275
318
}
0 commit comments