Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions crates/modelardb_embedded/bindings/c/modelardb_embedded.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ int modelardb_embedded_tables(void* maybe_operations_ptr,
int modelardb_embedded_schema(void* maybe_operations_ptr,
bool is_data_folder,
const char* table_name_ptr,
struct ArrowArray* schema_struct_array_ptr,
struct ArrowSchema* schema_struct_array_schema_ptr);
struct ArrowSchema* schema_ptr);

// Write data to the table with the given name.
int modelardb_embedded_write(void* maybe_operations_ptr,
Expand Down
15 changes: 5 additions & 10 deletions crates/modelardb_embedded/bindings/python/modelardb/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,7 @@ def modelardb_type(self) -> ModelarDBType:
modelardb_type_int_ffi = ffi.new("int*")

return_code = self.__library.modelardb_embedded_modelardb_type(
self.__operations_ptr,
self.__is_data_folder,
modelardb_type_int_ffi
self.__operations_ptr, self.__is_data_folder, modelardb_type_int_ffi
)
self.__check_return_code_and_raise_error(return_code)

Expand Down Expand Up @@ -351,21 +349,18 @@ def schema(self, table_name: str) -> Schema:
"""
table_name_ptr = ffi.new("char[]", bytes(table_name, "UTF-8"))

# The schema is retrieved using an empty record batch since using a pointer to the schema causes an
# ArrowInvalid error.
schema_batch_ffi = FFIArray.from_type(RecordBatch)
schema_ptr = ffi.new("struct ArrowSchema*")
schema_ptr_int = int(ffi.cast("uintptr_t", schema_ptr))

return_code = self.__library.modelardb_embedded_schema(
self.__operations_ptr,
self.__is_data_folder,
table_name_ptr,
schema_batch_ffi.array_ptr,
schema_batch_ffi.schema_ptr,
schema_ptr,
)
self.__check_return_code_and_raise_error(return_code)

schema_batch: RecordBatch = schema_batch_ffi.array()
return schema_batch.schema
return Schema._import_from_c(schema_ptr_int)

def write(self, table_name: str, uncompressed_batch: RecordBatch):
"""Writes the data in `uncompressed_batch` to the table with
Expand Down
32 changes: 10 additions & 22 deletions crates/modelardb_embedded/src/capi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,27 +445,22 @@ unsafe fn tables(
}

/// Writes the [`Schema`] of the table with the name in `table_name_ptr` in the [`DataFolder`] or
/// [`Client`] in `maybe_operations_ptr` to `schema_struct_array_ptr` and
/// `schema_struct_array_schema_ptr`. Assumes `maybe_operations_ptr` points to a [`DataFolder`] or
/// [`Client`]; table_name_ptr` points to a valid C string; schema_struct_array_ptr` is a valid
/// pointer to enough memory for an Apache Arrow C Data Interface Array; and
/// `schema_struct_array_schema_ptr` is a valid pointer to enough memory for an Apache Arrow C Data
/// Interface Schema.
/// [`Client`] in `maybe_operations_ptr` to `schema_ptr`. Assumes `maybe_operations_ptr` points to
/// a [`DataFolder`] or [`Client`]; `table_name_ptr` points to a valid C string; and `schema_ptr`
/// is a valid pointer to enough memory for an Apache Arrow C Data Interface Schema.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn modelardb_embedded_schema(
maybe_operations_ptr: *mut c_void,
is_data_folder: bool,
table_name_ptr: *const c_char,
schema_struct_array_ptr: *mut FFI_ArrowArray,
schema_struct_array_schema_ptr: *mut FFI_ArrowSchema,
schema_ptr: *mut FFI_ArrowSchema,
) -> c_int {
let maybe_unit = unsafe {
schema(
maybe_operations_ptr,
is_data_folder,
table_name_ptr,
schema_struct_array_ptr,
schema_struct_array_schema_ptr,
schema_ptr,
)
};
set_error_and_return_code(maybe_unit)
Expand All @@ -476,24 +471,17 @@ unsafe fn schema(
maybe_operations_ptr: *mut c_void,
is_data_folder: bool,
table_name_ptr: *const c_char,
schema_struct_array_ptr: *mut FFI_ArrowArray,
schema_struct_array_schema_ptr: *mut FFI_ArrowSchema,
schema_ptr: *mut FFI_ArrowSchema,
) -> Result<()> {
let modelardb = unsafe { c_void_to_operations(maybe_operations_ptr, is_data_folder)? };
let table_name = unsafe { c_char_ptr_to_str(table_name_ptr)? };

let schema = TOKIO_RUNTIME.block_on(modelardb.schema(table_name))?;
let schema_batch = RecordBatch::new_empty(Arc::new(schema));

// The schema is returned using an empty record batch since using a pointer to the schema
// causes an ArrowInvalid error.
unsafe {
record_batch_to_pointers(
schema_batch,
schema_struct_array_ptr,
schema_struct_array_schema_ptr,
)
}
let ffi_schema = FFI_ArrowSchema::try_from(&schema)?;
unsafe { schema_ptr.write(ffi_schema) };

Ok(())
}

/// Writes the data in `uncompressed_struct_array_ptr` and `uncompressed_struct_array_schema_ptr`
Expand Down
Loading