-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlib.rs
More file actions
495 lines (436 loc) · 14.5 KB
/
lib.rs
File metadata and controls
495 lines (436 loc) · 14.5 KB
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#![allow(clippy::missing_safety_doc)]
use std::ffi::{c_char, c_void, CStr};
use std::ptr;
use std::collections::HashMap;
use std::sync::Arc;
use arrow::array::{Array, RecordBatch, StructArray};
use arrow::datatypes::{DataType, Schema};
use arrow::ffi::{FFI_ArrowArray, FFI_ArrowSchema};
use lance::Dataset;
use lance::dataset::builder::DatasetBuilder;
mod runtime;
mod scanner;
mod error;
use scanner::LanceStream;
use error::{clear_last_error, set_last_error, ErrorCode};
// FFI ownership contract (Arrow C Data Interface):
// - All `*_open/create/get` functions return opaque handles owned by the caller,
// which must be released exactly once via the matching `*_close/free` call.
// - `lance_schema_to_arrow` transfers ownership of the populated ArrowSchema to
// the caller. The caller must call `release` exactly once on success.
// - `lance_batch_to_arrow` transfers ownership of the populated ArrowArray and
// ArrowSchema to the caller. The caller must call `release` exactly once on
// each on success.
// - On error (non-zero return), output ArrowSchema/ArrowArray are left
// untouched and must not be released unless the caller initialized them to a
// valid value before calling into this library.
// Dataset operations - just holds the dataset
struct DatasetHandle {
dataset: Arc<Dataset>,
}
#[no_mangle]
pub unsafe extern "C" fn lance_open_dataset(path: *const c_char) -> *mut c_void {
if path.is_null() {
set_last_error(ErrorCode::InvalidArgument, "path is null");
return ptr::null_mut();
}
let path_str = unsafe {
match CStr::from_ptr(path).to_str() {
Ok(s) => s,
Err(err) => {
set_last_error(ErrorCode::Utf8, format!("utf8 decode: {err}"));
return ptr::null_mut();
}
}
};
let dataset = match runtime::block_on(Dataset::open(path_str)) {
Ok(Ok(ds)) => Arc::new(ds),
Ok(Err(err)) => {
set_last_error(
ErrorCode::DatasetOpen,
format!("dataset open '{path_str}': {err}"),
);
return ptr::null_mut();
}
Err(err) => {
set_last_error(ErrorCode::Runtime, format!("runtime: {err}"));
return ptr::null_mut();
}
};
let handle = Box::new(DatasetHandle { dataset });
clear_last_error();
Box::into_raw(handle) as *mut c_void
}
#[no_mangle]
pub unsafe extern "C" fn lance_open_dataset_with_storage_options(
path: *const c_char,
option_keys: *const *const c_char,
option_values: *const *const c_char,
options_len: usize,
) -> *mut c_void {
if path.is_null() {
set_last_error(ErrorCode::InvalidArgument, "path is null");
return ptr::null_mut();
}
if options_len > 0 && (option_keys.is_null() || option_values.is_null()) {
set_last_error(
ErrorCode::InvalidArgument,
"option_keys/option_values is null with non-zero length",
);
return ptr::null_mut();
}
let path_str = match CStr::from_ptr(path).to_str() {
Ok(s) => s,
Err(err) => {
set_last_error(ErrorCode::Utf8, format!("utf8 decode: {err}"));
return ptr::null_mut();
}
};
let mut storage_options = HashMap::<String, String>::new();
for i in 0..options_len {
let key_ptr = *option_keys.add(i);
let value_ptr = *option_values.add(i);
if key_ptr.is_null() || value_ptr.is_null() {
set_last_error(ErrorCode::InvalidArgument, "option key/value is null");
return ptr::null_mut();
}
let key = match CStr::from_ptr(key_ptr).to_str() {
Ok(s) => s,
Err(err) => {
set_last_error(ErrorCode::Utf8, format!("utf8 decode key: {err}"));
return ptr::null_mut();
}
};
let value = match CStr::from_ptr(value_ptr).to_str() {
Ok(s) => s,
Err(err) => {
set_last_error(ErrorCode::Utf8, format!("utf8 decode value: {err}"));
return ptr::null_mut();
}
};
storage_options.insert(key.to_string(), value.to_string());
}
let dataset = match runtime::block_on(async {
DatasetBuilder::from_uri(path_str)
.with_storage_options(storage_options)
.load()
.await
}) {
Ok(Ok(ds)) => Arc::new(ds),
Ok(Err(err)) => {
set_last_error(
ErrorCode::DatasetOpen,
format!("dataset open '{path_str}': {err}"),
);
return ptr::null_mut();
}
Err(err) => {
set_last_error(ErrorCode::Runtime, format!("runtime: {err}"));
return ptr::null_mut();
}
};
let handle = Box::new(DatasetHandle { dataset });
clear_last_error();
Box::into_raw(handle) as *mut c_void
}
#[no_mangle]
pub unsafe extern "C" fn lance_close_dataset(dataset: *mut c_void) {
if !dataset.is_null() {
unsafe {
let _ = Box::from_raw(dataset as *mut DatasetHandle);
}
}
}
#[no_mangle]
pub unsafe extern "C" fn lance_dataset_count_rows(dataset: *mut c_void) -> i64 {
if dataset.is_null() {
set_last_error(ErrorCode::InvalidArgument, "dataset is null");
return -1;
}
let handle = unsafe { &*(dataset as *const DatasetHandle) };
match runtime::block_on(handle.dataset.count_rows(None)) {
Ok(Ok(rows)) => {
clear_last_error();
match i64::try_from(rows) {
Ok(v) => v,
Err(_) => {
set_last_error(ErrorCode::DatasetCountRows, "row count overflow");
-1
}
}
}
Ok(Err(err)) => {
set_last_error(
ErrorCode::DatasetCountRows,
format!("dataset count_rows: {err}"),
);
-1
}
Err(err) => {
set_last_error(ErrorCode::Runtime, format!("runtime: {err}"));
-1
}
}
}
// Schema operations
#[no_mangle]
pub unsafe extern "C" fn lance_get_schema(dataset: *mut c_void) -> *mut c_void {
if dataset.is_null() {
set_last_error(ErrorCode::InvalidArgument, "dataset is null");
return ptr::null_mut();
}
let handle = unsafe { &*(dataset as *const DatasetHandle) };
let schema = handle.dataset.schema();
let arrow_schema: Schema = schema.into();
clear_last_error();
Box::into_raw(Box::new(Arc::new(arrow_schema))) as *mut c_void
}
#[no_mangle]
pub unsafe extern "C" fn lance_free_schema(schema: *mut c_void) {
if !schema.is_null() {
unsafe {
let _ = Box::from_raw(schema as *mut Arc<Schema>);
}
}
}
#[no_mangle]
pub unsafe extern "C" fn lance_schema_to_arrow(
schema: *mut c_void,
out_schema: *mut FFI_ArrowSchema,
) -> i32 {
if schema.is_null() || out_schema.is_null() {
set_last_error(ErrorCode::InvalidArgument, "schema or out_schema is null");
return -1;
}
let schema = unsafe { &*(schema as *const Arc<Schema>) };
let data_type = DataType::Struct(schema.fields().clone());
let ffi_schema = match FFI_ArrowSchema::try_from(&data_type) {
Ok(schema) => schema,
Err(err) => {
set_last_error(ErrorCode::SchemaExport, format!("schema export: {err}"));
return -1;
}
};
std::ptr::write_unaligned(out_schema, ffi_schema);
clear_last_error();
0
}
// Stream operations
#[no_mangle]
pub unsafe extern "C" fn lance_create_stream(dataset: *mut c_void) -> *mut c_void {
if dataset.is_null() {
set_last_error(ErrorCode::InvalidArgument, "dataset is null");
return ptr::null_mut();
}
let handle = unsafe { &*(dataset as *const DatasetHandle) };
let scanner = handle.dataset.scan();
match LanceStream::from_scanner(scanner) {
Ok(stream) => {
clear_last_error();
Box::into_raw(Box::new(stream)) as *mut c_void
}
Err(err) => {
set_last_error(ErrorCode::StreamCreate, format!("stream create: {err}"));
ptr::null_mut()
}
}
}
#[no_mangle]
pub unsafe extern "C" fn lance_dataset_list_fragments(
dataset: *mut c_void,
out_len: *mut usize,
) -> *mut u64 {
if dataset.is_null() || out_len.is_null() {
set_last_error(ErrorCode::InvalidArgument, "dataset or out_len is null");
return ptr::null_mut();
}
let handle = unsafe { &*(dataset as *const DatasetHandle) };
let ids: Vec<u64> = handle.dataset.fragments().iter().map(|f| f.id).collect();
let mut boxed = ids.into_boxed_slice();
let len = boxed.len();
let data = boxed.as_mut_ptr();
std::mem::forget(boxed);
unsafe {
ptr::write_unaligned(out_len, len);
}
clear_last_error();
data
}
#[no_mangle]
pub unsafe extern "C" fn lance_free_fragment_list(ptr: *mut u64, len: usize) {
if ptr.is_null() {
return;
}
unsafe {
let slice = std::ptr::slice_from_raw_parts_mut(ptr, len);
let _ = Box::<[u64]>::from_raw(slice);
}
}
#[no_mangle]
pub unsafe extern "C" fn lance_create_fragment_stream(
dataset: *mut c_void,
fragment_id: u64,
columns: *const *const c_char,
columns_len: usize,
filter_sql: *const c_char,
) -> *mut c_void {
if dataset.is_null() {
set_last_error(ErrorCode::InvalidArgument, "dataset is null");
return ptr::null_mut();
}
let handle = unsafe { &*(dataset as *const DatasetHandle) };
let fragment_id_usize = match usize::try_from(fragment_id) {
Ok(v) => v,
Err(err) => {
set_last_error(ErrorCode::InvalidArgument, format!("invalid fragment id: {err}"));
return ptr::null_mut();
}
};
let fragment = match handle.dataset.get_fragment(fragment_id_usize) {
Some(f) => f,
None => {
set_last_error(
ErrorCode::FragmentScan,
format!("fragment not found: {fragment_id}"),
);
return ptr::null_mut();
}
};
let mut scan = fragment.scan();
if !columns.is_null() && columns_len > 0 {
let mut projection = Vec::with_capacity(columns_len);
for idx in 0..columns_len {
let col_ptr = unsafe { *columns.add(idx) };
if col_ptr.is_null() {
set_last_error(ErrorCode::InvalidArgument, "column name is null");
return ptr::null_mut();
}
let col_name = match unsafe { CStr::from_ptr(col_ptr) }.to_str() {
Ok(v) => v,
Err(err) => {
set_last_error(ErrorCode::Utf8, format!("utf8 decode: {err}"));
return ptr::null_mut();
}
};
projection.push(col_name.to_string());
}
if let Err(err) = scan.project(&projection) {
set_last_error(ErrorCode::FragmentScan, format!("fragment scan project: {err}"));
return ptr::null_mut();
}
}
if !filter_sql.is_null() {
let filter = match unsafe { CStr::from_ptr(filter_sql) }.to_str() {
Ok(v) => v,
Err(err) => {
set_last_error(ErrorCode::Utf8, format!("utf8 decode: {err}"));
return ptr::null_mut();
}
};
if !filter.is_empty() {
if let Err(err) = scan.filter(filter) {
set_last_error(ErrorCode::FragmentScan, format!("fragment scan filter: {err}"));
return ptr::null_mut();
}
}
}
scan.scan_in_order(false);
match LanceStream::from_scanner(scan) {
Ok(stream) => {
clear_last_error();
Box::into_raw(Box::new(stream)) as *mut c_void
}
Err(err) => {
set_last_error(ErrorCode::StreamCreate, format!("stream create: {err}"));
ptr::null_mut()
}
}
}
#[no_mangle]
pub unsafe extern "C" fn lance_stream_next(
stream: *mut c_void,
out_batch: *mut *mut c_void,
) -> i32 {
if out_batch.is_null() {
set_last_error(ErrorCode::InvalidArgument, "out_batch is null");
return -1;
}
unsafe {
ptr::write_unaligned(out_batch, ptr::null_mut());
}
if stream.is_null() {
set_last_error(ErrorCode::InvalidArgument, "stream is null");
return -1;
}
let stream = unsafe { &mut *(stream as *mut LanceStream) };
match stream.next() {
Ok(Some(batch)) => {
clear_last_error();
let batch_ptr = Box::into_raw(Box::new(batch)) as *mut c_void;
unsafe {
ptr::write_unaligned(out_batch, batch_ptr);
}
0
}
Ok(None) => {
clear_last_error();
1
}
Err(err) => {
set_last_error(ErrorCode::StreamNext, format!("stream next: {err}"));
-1
}
}
}
#[no_mangle]
pub unsafe extern "C" fn lance_close_stream(stream: *mut c_void) {
if !stream.is_null() {
unsafe {
let _ = Box::from_raw(stream as *mut LanceStream);
}
}
}
#[no_mangle]
pub unsafe extern "C" fn lance_free_batch(batch: *mut c_void) {
if !batch.is_null() {
unsafe {
let _ = Box::from_raw(batch as *mut RecordBatch);
}
}
}
#[no_mangle]
pub unsafe extern "C" fn lance_batch_num_rows(batch: *mut c_void) -> i64 {
if batch.is_null() {
return 0;
}
let batch = unsafe { &*(batch as *const RecordBatch) };
batch.num_rows() as i64
}
// Export RecordBatch to Arrow C Data Interface
#[no_mangle]
pub unsafe extern "C" fn lance_batch_to_arrow(
batch: *mut c_void,
out_array: *mut FFI_ArrowArray,
out_schema: *mut FFI_ArrowSchema,
) -> i32 {
if batch.is_null() || out_array.is_null() || out_schema.is_null() {
set_last_error(ErrorCode::InvalidArgument, "batch or out pointer is null");
return -1;
}
let batch = unsafe { &*(batch as *const RecordBatch) };
// Convert RecordBatch to StructArray for FFI export
let struct_array: Arc<dyn Array> = Arc::new(StructArray::from(batch.clone()));
let data = struct_array.to_data();
let array = FFI_ArrowArray::new(&data);
let schema = match FFI_ArrowSchema::try_from(data.data_type()) {
Ok(schema) => schema,
Err(err) => {
set_last_error(ErrorCode::BatchExport, format!("batch export: {err}"));
return -1;
}
};
std::ptr::write_unaligned(out_array, array);
std::ptr::write_unaligned(out_schema, schema);
clear_last_error();
0
}