-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathtransaction.rs
379 lines (337 loc) · 14 KB
/
transaction.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors
use arrow::pyarrow::PyArrowType;
use arrow_schema::Schema as ArrowSchema;
use lance::dataset::transaction::{
DataReplacementGroup, Operation, RewriteGroup, RewrittenIndex, Transaction,
};
use lance::datatypes::Schema;
use lance_table::format::{DataFile, Fragment, Index};
use pyo3::exceptions::PyValueError;
use pyo3::types::{PyDict, PyNone};
use pyo3::{intern, prelude::*};
use pyo3::{Bound, FromPyObject, PyAny, PyObject, PyResult, Python, ToPyObject};
use uuid::Uuid;
use crate::schema::LanceSchema;
use crate::utils::{class_name, export_vec, extract_vec, PyLance};
impl FromPyObject<'_> for PyLance<DataReplacementGroup> {
fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self> {
let fragment_id = ob.getattr("fragment_id")?.extract::<u64>()?;
let new_file = &ob.getattr("new_file")?.extract::<PyLance<DataFile>>()?;
Ok(Self(DataReplacementGroup(fragment_id, new_file.0.clone())))
}
}
impl ToPyObject for PyLance<&DataReplacementGroup> {
fn to_object(&self, py: Python<'_>) -> PyObject {
let namespace = py
.import_bound(intern!(py, "lance"))
.and_then(|module| module.getattr(intern!(py, "LanceOperation")))
.expect("Failed to import LanceOperation namespace");
let fragment_id = self.0 .0;
let new_file = PyLance(&self.0 .1).to_object(py);
let cls = namespace
.getattr("DataReplacementGroup")
.expect("Failed to get DataReplacementGroup class");
cls.call1((fragment_id, new_file)).unwrap().to_object(py)
}
}
impl FromPyObject<'_> for PyLance<Index> {
fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self> {
let uuid = ob.get_item("uuid")?.extract()?;
let name = ob.get_item("name")?.extract()?;
let fields = ob.get_item("fields")?.extract()?;
let dataset_version = ob.get_item("dataset_version")?.extract()?;
let fragment_ids = ob.get_item("fragment_ids")?;
let fragment_ids = fragment_ids.iter()?.map(|id| id?.extract::<u32>())
.collect::<PyResult<Vec<u32>>>()?;
let fragment_bitmap = Some(fragment_ids.into_iter().collect());
Ok(Self(Index {
uuid: Uuid::parse_str(uuid)
.map_err(|e| PyValueError::new_err(e.to_string()))?,
name,
fields,
dataset_version,
fragment_bitmap,
// TODO: we should use lance::dataset::Dataset::commit_existing_index once
// we have a way to determine index details from an existing index.
index_details: None,
}))
}
}
impl ToPyObject for PyLance<&Index> {
fn to_object(&self, py: Python<'_>) -> PyObject {
let uuid = self.0.uuid.to_string().to_object(py);
let name = self.0.name.to_object(py);
let fields = export_vec(py, &self.0.fields).to_object(py);
let dataset_version = self.0.dataset_version.to_object(py);
let fragment_ids = match &self.0.fragment_bitmap {
Some(bitmap) => bitmap.into_iter().collect::<Vec<_>>().to_object(py),
None => PyNone::get_bound(py).to_object(py),
};
let kwargs = PyDict::new_bound(py);
kwargs.set_item("uuid", uuid).unwrap();
kwargs.set_item("name", name).unwrap();
kwargs.set_item("fields", fields).unwrap();
kwargs.set_item("dataset_version", dataset_version).unwrap();
kwargs.set_item("fragment_ids", fragment_ids).unwrap();
kwargs.into()
}
}
impl ToPyObject for PyLance<Index> {
fn to_object(&self, py: Python<'_>) -> PyObject {
PyLance(&self.0).to_object(py)
}
}
impl FromPyObject<'_> for PyLance<Operation> {
fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self> {
match class_name(ob)? {
"Overwrite" => {
let schema = extract_schema(&ob.getattr("new_schema")?)?;
let fragments = extract_vec(&ob.getattr("fragments")?)?;
let op = Operation::Overwrite {
schema,
fragments,
config_upsert_values: None,
};
Ok(Self(op))
}
"Append" => {
let fragments = extract_vec(&ob.getattr("fragments")?)?;
let op = Operation::Append { fragments };
Ok(Self(op))
}
"Delete" => {
let updated_fragments = extract_vec(&ob.getattr("updated_fragments")?)?;
let deleted_fragment_ids = ob.getattr("deleted_fragment_ids")?.extract()?;
let predicate = ob.getattr("predicate")?.extract()?;
let op = Operation::Delete {
updated_fragments,
deleted_fragment_ids,
predicate,
};
Ok(Self(op))
}
"Update" => {
let removed_fragment_ids = ob.getattr("removed_fragment_ids")?.extract()?;
let updated_fragments = extract_vec(&ob.getattr("updated_fragments")?)?;
let new_fragments = extract_vec(&ob.getattr("new_fragments")?)?;
let op = Operation::Update {
removed_fragment_ids,
updated_fragments,
new_fragments,
};
Ok(Self(op))
}
"Merge" => {
let schema = extract_schema(&ob.getattr("schema")?)?;
let fragments = ob
.getattr("fragments")?
.extract::<Vec<PyLance<Fragment>>>()?;
let fragments = fragments.into_iter().map(|f| f.0).collect();
let op = Operation::Merge { schema, fragments };
Ok(Self(op))
}
"Restore" => {
let version = ob.getattr("version")?.extract()?;
let op = Operation::Restore { version };
Ok(Self(op))
}
"Rewrite" => {
let groups = extract_vec(&ob.getattr("groups")?)?;
let rewritten_indices = extract_vec(&ob.getattr("rewritten_indices")?)?;
let op = Operation::Rewrite {
groups,
rewritten_indices,
};
Ok(Self(op))
}
"CreateIndex" => {
let removed_indices = extract_vec(&ob.getattr("removed_indices")?)?;
let new_indices = extract_vec(&ob.getattr("new_indices")?)?;
let op = Operation::CreateIndex {
removed_indices,
new_indices,
};
Ok(Self(op))
}
"DataReplacement" => {
let replacements = extract_vec(&ob.getattr("replacements")?)?;
let op = Operation::DataReplacement { replacements };
Ok(Self(op))
}
unsupported => Err(PyValueError::new_err(format!(
"Unsupported operation: {unsupported}",
))),
}
}
}
impl ToPyObject for PyLance<&Operation> {
fn to_object(&self, py: Python<'_>) -> PyObject {
let namespace = py
.import_bound(intern!(py, "lance"))
.and_then(|module| module.getattr(intern!(py, "LanceOperation")))
.expect("Failed to import LanceOperation namespace");
match self.0 {
Operation::Append { ref fragments } => {
let fragments = export_vec(py, fragments.as_slice());
let cls = namespace
.getattr("Append")
.expect("Failed to get Append class");
cls.call1((fragments,)).unwrap().to_object(py)
}
Operation::Overwrite {
ref fragments,
ref schema,
..
} => {
let fragments_py = export_vec(py, fragments.as_slice());
let schema_py = LanceSchema(schema.clone());
let cls = namespace
.getattr("Overwrite")
.expect("Failed to get Overwrite class");
cls.call1((schema_py, fragments_py))
.expect("Failed to create Overwrite instance")
.to_object(py)
}
Operation::Update {
removed_fragment_ids,
updated_fragments,
new_fragments,
} => {
let removed_fragment_ids = removed_fragment_ids.to_object(py);
let updated_fragments = export_vec(py, updated_fragments.as_slice());
let new_fragments = export_vec(py, new_fragments.as_slice());
let cls = namespace
.getattr("Update")
.expect("Failed to get Update class");
cls.call1((removed_fragment_ids, updated_fragments, new_fragments))
.unwrap()
.to_object(py)
}
Operation::CreateIndex {
removed_indices,
new_indices,
} => {
let removed_indices = export_vec(py, removed_indices.as_slice());
let new_indices = export_vec(py, new_indices.as_slice());
let cls = namespace
.getattr("CreateIndex")
.expect("Failed to get CreateIndex class");
cls.call1((removed_indices, new_indices))
.unwrap()
.to_object(py)
}
Operation::DataReplacement { replacements } => {
let replacements = export_vec(py, replacements.as_slice());
let cls = namespace
.getattr("DataReplacement")
.expect("Failed to get DataReplacement class");
cls.call1((replacements,)).unwrap().to_object(py)
}
_ => todo!(),
}
}
}
impl FromPyObject<'_> for PyLance<Transaction> {
fn extract_bound(ob: &pyo3::Bound<'_, PyAny>) -> PyResult<Self> {
let read_version = ob.getattr("read_version")?.extract()?;
let uuid = ob.getattr("uuid")?.extract()?;
let operation = ob.getattr("operation")?.extract::<PyLance<Operation>>()?.0;
let blobs_op = ob
.getattr("blobs_op")?
.extract::<Option<PyLance<Operation>>>()?
.map(|op| op.0);
Ok(Self(Transaction {
read_version,
uuid,
operation,
blobs_op,
tag: None,
}))
}
}
impl ToPyObject for PyLance<&Transaction> {
fn to_object(&self, py: Python<'_>) -> PyObject {
let namespace = py
.import_bound(intern!(py, "lance"))
.expect("Failed to import lance module");
let read_version = self.0.read_version;
let uuid = &self.0.uuid;
let operation = PyLance(&self.0.operation).to_object(py);
let blobs_op = self.0.blobs_op.as_ref().map(|op| PyLance(op).to_object(py));
let cls = namespace
.getattr("Transaction")
.expect("Failed to get Transaction class");
cls.call1((read_version, operation, uuid, blobs_op))
.unwrap()
.to_object(py)
}
}
impl ToPyObject for PyLance<Transaction> {
fn to_object(&self, py: Python<'_>) -> PyObject {
PyLance(&self.0).to_object(py)
}
}
impl FromPyObject<'_> for PyLance<RewriteGroup> {
fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self> {
Ok(Self(RewriteGroup {
old_fragments: extract_vec(&ob.getattr("old_fragments")?)?,
new_fragments: extract_vec(&ob.getattr("new_fragments")?)?,
}))
}
}
impl ToPyObject for PyLance<&RewriteGroup> {
fn to_object(&self, py: Python<'_>) -> PyObject {
let cls = py
.import_bound(intern!(py, "lance"))
.and_then(|module| module.getattr(intern!(py, "LanceTransaction")))
.and_then(|cls| cls.getattr(intern!(py, "RewriteGroup")))
.expect("Failed to get RewriteGroup class");
let old_fragments = export_vec(py, self.0.old_fragments.as_slice());
let new_fragments = export_vec(py, self.0.new_fragments.as_slice());
cls.call1((old_fragments, new_fragments))
.unwrap()
.to_object(py)
}
}
impl FromPyObject<'_> for PyLance<RewrittenIndex> {
fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self> {
let old_id: &str = ob.getattr("old_id")?.extract()?;
let new_id: &str = ob.getattr("new_id")?.extract()?;
let old_id = Uuid::parse_str(old_id)
.map_err(|e| PyValueError::new_err(format!("Failed to parse UUID: {}", e)))?;
let new_id = Uuid::parse_str(new_id)
.map_err(|e| PyValueError::new_err(format!("Failed to parse UUID: {}", e)))?;
Ok(Self(RewrittenIndex { old_id, new_id }))
}
}
impl ToPyObject for PyLance<&RewrittenIndex> {
fn to_object(&self, py: Python<'_>) -> PyObject {
let cls = py
.import_bound(intern!(py, "lance"))
.and_then(|module| module.getattr(intern!(py, "LanceTransaction")))
.and_then(|cls| cls.getattr(intern!(py, "RewrittenIndex")))
.expect("Failed to get RewrittenIndex class");
let old_id = self.0.old_id.to_string();
let new_id = self.0.new_id.to_string();
cls.call1((old_id, new_id)).unwrap().to_object(py)
}
}
fn extract_schema(schema: &Bound<'_, PyAny>) -> PyResult<Schema> {
match schema.downcast::<LanceSchema>() {
Ok(schema) => Ok(schema.borrow().0.clone()),
Err(_) => {
let arrow_schema = schema.extract::<PyArrowType<ArrowSchema>>()?.0;
convert_schema(&arrow_schema)
}
}
}
fn convert_schema(arrow_schema: &ArrowSchema) -> PyResult<Schema> {
// Note: the field ids here are wrong.
Schema::try_from(arrow_schema).map_err(|e| {
PyValueError::new_err(format!(
"Failed to convert Arrow schema to Lance schema: {}",
e
))
})
}