@@ -9,8 +9,8 @@ use lance::dataset::transaction::{
9
9
use lance:: datatypes:: Schema ;
10
10
use lance_table:: format:: { DataFile , Fragment , Index } ;
11
11
use pyo3:: exceptions:: PyValueError ;
12
- use pyo3:: types:: { PyDict , PyNone } ;
13
- use pyo3:: { intern, prelude:: * } ;
12
+ use pyo3:: types:: { PyDict , PyList , PyNone , PySet } ;
13
+ use pyo3:: { intern, prelude:: * , PyTypeCheck } ;
14
14
use pyo3:: { Bound , FromPyObject , PyAny , PyResult , Python } ;
15
15
use uuid:: Uuid ;
16
16
@@ -49,19 +49,30 @@ impl<'py> IntoPyObject<'py> for PyLance<&DataReplacementGroup> {
49
49
50
50
impl FromPyObject < ' _ > for PyLance < Index > {
51
51
fn extract_bound ( ob : & Bound < ' _ , PyAny > ) -> PyResult < Self > {
52
- let uuid = ob. get_item ( "uuid" ) ?. extract ( ) ? ;
52
+ let uuid = ob. get_item ( "uuid" ) ?. to_string ( ) ;
53
53
let name = ob. get_item ( "name" ) ?. extract ( ) ?;
54
54
let fields = ob. get_item ( "fields" ) ?. extract ( ) ?;
55
55
let dataset_version = ob. get_item ( "version" ) ?. extract ( ) ?;
56
56
57
57
let fragment_ids = ob. get_item ( "fragment_ids" ) ?;
58
- let fragment_ids = fragment_ids
59
- . iter ( ) ?
60
- . map ( |id| id?. extract :: < u32 > ( ) )
61
- . collect :: < PyResult < Vec < u32 > > > ( ) ?;
58
+ let fragment_ids = if PySet :: type_check ( & fragment_ids) {
59
+ let fragment_ids_ref: & Bound < ' _ , PySet > = fragment_ids. downcast ( ) ?;
60
+ fragment_ids_ref
61
+ . into_iter ( )
62
+ . map ( |id| id. extract ( ) )
63
+ . collect :: < PyResult < Vec < u32 > > > ( ) ?
64
+ } else if PyList :: type_check ( & fragment_ids) {
65
+ let fragment_ids_ref: & Bound < ' _ , PyList > = fragment_ids. downcast ( ) ?;
66
+ fragment_ids_ref
67
+ . into_iter ( )
68
+ . map ( |id| id. extract ( ) )
69
+ . collect :: < PyResult < Vec < u32 > > > ( ) ?
70
+ } else {
71
+ return Err ( PyValueError :: new_err ( "Invalid fragment_ids" ) ) ;
72
+ } ;
62
73
let fragment_bitmap = Some ( fragment_ids. into_iter ( ) . collect ( ) ) ;
63
74
Ok ( Self ( Index {
64
- uuid : Uuid :: parse_str ( uuid) . map_err ( |e| PyValueError :: new_err ( e. to_string ( ) ) ) ?,
75
+ uuid : Uuid :: parse_str ( & uuid) . map_err ( |e| PyValueError :: new_err ( e. to_string ( ) ) ) ?,
65
76
name,
66
77
fields,
67
78
dataset_version,
@@ -73,30 +84,38 @@ impl FromPyObject<'_> for PyLance<Index> {
73
84
}
74
85
}
75
86
76
- impl ToPyObject for PyLance < & Index > {
77
- fn to_object ( & self , py : Python < ' _ > ) -> PyObject {
78
- let uuid = self . 0 . uuid . to_string ( ) . to_object ( py) ;
79
- let name = self . 0 . name . to_object ( py) ;
80
- let fields = export_vec ( py, & self . 0 . fields ) . to_object ( py) ;
81
- let dataset_version = self . 0 . dataset_version . to_object ( py) ;
87
+ impl < ' py > IntoPyObject < ' py > for PyLance < & Index > {
88
+ type Target = PyDict ;
89
+ type Output = Bound < ' py , Self :: Target > ;
90
+ type Error = PyErr ;
91
+
92
+ fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
93
+ let uuid = self . 0 . uuid . to_string ( ) . into_pyobject ( py) ?;
94
+ let name = self . 0 . name . clone ( ) . into_pyobject ( py) ?;
95
+ let fields = export_vec ( py, & self . 0 . fields ) ?;
96
+ let dataset_version = self . 0 . dataset_version . into_pyobject ( py) ?;
82
97
let fragment_ids = match & self . 0 . fragment_bitmap {
83
- Some ( bitmap) => bitmap. into_iter ( ) . collect :: < Vec < _ > > ( ) . to_object ( py) ,
84
- None => PyNone :: get_bound ( py) . to_object ( py ) ,
98
+ Some ( bitmap) => bitmap. into_iter ( ) . collect :: < Vec < _ > > ( ) . into_pyobject ( py) ? ,
99
+ None => PyNone :: get ( py) . to_owned ( ) . into_any ( ) ,
85
100
} ;
86
101
87
- let kwargs = PyDict :: new_bound ( py) ;
102
+ let kwargs = PyDict :: new ( py) ;
88
103
kwargs. set_item ( "uuid" , uuid) . unwrap ( ) ;
89
104
kwargs. set_item ( "name" , name) . unwrap ( ) ;
90
105
kwargs. set_item ( "fields" , fields) . unwrap ( ) ;
91
106
kwargs. set_item ( "version" , dataset_version) . unwrap ( ) ;
92
107
kwargs. set_item ( "fragment_ids" , fragment_ids) . unwrap ( ) ;
93
- kwargs . into ( )
108
+ Ok ( kwargs )
94
109
}
95
110
}
96
111
97
- impl ToPyObject for PyLance < Index > {
98
- fn to_object ( & self , py : Python < ' _ > ) -> PyObject {
99
- PyLance ( & self . 0 ) . to_object ( py)
112
+ impl < ' py > IntoPyObject < ' py > for PyLance < Index > {
113
+ type Target = PyDict ;
114
+ type Output = Bound < ' py , Self :: Target > ;
115
+ type Error = PyErr ;
116
+
117
+ fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
118
+ PyLance ( & self . 0 ) . into_pyobject ( py)
100
119
}
101
120
}
102
121
@@ -245,14 +264,12 @@ impl<'py> IntoPyObject<'py> for PyLance<&Operation> {
245
264
removed_indices,
246
265
new_indices,
247
266
} => {
248
- let removed_indices = export_vec ( py, removed_indices. as_slice ( ) ) ;
249
- let new_indices = export_vec ( py, new_indices. as_slice ( ) ) ;
267
+ let removed_indices = export_vec ( py, removed_indices. as_slice ( ) ) ? ;
268
+ let new_indices = export_vec ( py, new_indices. as_slice ( ) ) ? ;
250
269
let cls = namespace
251
270
. getattr ( "CreateIndex" )
252
271
. expect ( "Failed to get CreateIndex class" ) ;
253
272
cls. call1 ( ( removed_indices, new_indices) )
254
- . unwrap ( )
255
- . to_object ( py)
256
273
}
257
274
Operation :: DataReplacement { replacements } => {
258
275
let replacements = export_vec ( py, replacements. as_slice ( ) ) ?;
0 commit comments