Skip to content

Commit 63918be

Browse files
committed
WIP
1 parent 6b62d04 commit 63918be

File tree

1 file changed

+53
-10
lines changed

1 file changed

+53
-10
lines changed

bindings/python/src/manifest.rs

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,37 @@ use iceberg::spec::{
2323
ManifestList, ManifestStatus, PrimitiveLiteral,
2424
};
2525
use pyo3::prelude::*;
26+
use pyo3::IntoPyObjectExt;
27+
use pyo3::types::{PyBytes};
28+
2629

27-
#[pyclass]
2830
#[allow(dead_code)]
2931
pub struct PyPrimitiveLiteral {
30-
inner: Option<PrimitiveLiteral>,
32+
inner: PrimitiveLiteral
3133
}
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+
3257

3358
#[pyclass]
3459
pub struct PyDataFile {
@@ -57,15 +82,33 @@ impl PyDataFile {
5782
}
5883
}
5984

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+
60101
#[getter]
61102
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+
})
69112
}
70113

71114
#[getter]
@@ -269,7 +312,7 @@ impl crate::manifest::PyManifestFile {
269312
}
270313

271314
#[getter]
272-
fn key_metadata(&self) -> Vec<u8> {
315+
fn key_metadata(&self) -> Option<Vec<u8>> {
273316
self.inner.key_metadata.clone()
274317
}
275318
}

0 commit comments

Comments
 (0)