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
1 change: 1 addition & 0 deletions include/lbug_arrow.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace lbug_arrow {

ArrowSchema query_result_get_arrow_schema(const lbug::main::QueryResult& result);
ArrowSchema prepared_statement_get_arrow_schema(const lbug::main::PreparedStatement& stmt);
bool query_result_has_next_arrow_chunk(lbug::main::QueryResult& result);
ArrowArray query_result_get_next_arrow_chunk(lbug::main::QueryResult& result, uint64_t chunkSize);
ArrowArray query_result_get_csr_indptr(const lbug::main::QueryResult& result);
Expand Down
13 changes: 13 additions & 0 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ impl PreparedStatement {
pub fn is_read_only(&self) -> bool {
ffi::prepared_statement_is_read_only(&self.statement)
}

#[cfg(feature = "arrow")]
/// Returns the query's result schema as Arrow without executing it.
///
/// The schema comes from the bound and planned statement, so this is
/// cheap even for expensive queries.
///
/// *Requires the `arrow` feature*
pub fn get_arrow_schema(&self) -> Result<arrow::ffi::FFI_ArrowSchema, Error> {
Ok(
crate::ffi::arrow::ffi_arrow::prepared_statement_get_arrow_schema(&self.statement)?.0,
)
}
}

/// Connections are used to interact with a Database instance.
Expand Down
8 changes: 8 additions & 0 deletions src/ffi/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub(crate) mod ffi_arrow {

#[namespace = "lbug::main"]
type QueryResult<'db> = crate::ffi::ffi::QueryResult<'db>;

#[namespace = "lbug::main"]
type PreparedStatement = crate::ffi::ffi::PreparedStatement;
}

unsafe extern "C++" {
Expand Down Expand Up @@ -61,6 +64,11 @@ pub(crate) mod ffi_arrow {

#[namespace = "lbug_arrow"]
fn query_result_get_arrow_schema<'db>(result: &QueryResult<'db>) -> Result<ArrowSchema>;

#[namespace = "lbug_arrow"]
fn prepared_statement_get_arrow_schema(
statement: &PreparedStatement,
) -> Result<ArrowSchema>;
}

#[namespace = "lbug_rs"]
Expand Down
8 changes: 8 additions & 0 deletions src/lbug_arrow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <stdexcept>
#include <vector>

#include "common/arrow/arrow_converter.h"

namespace lbug {
namespace main {

Expand Down Expand Up @@ -69,6 +71,12 @@ ArrowSchema query_result_get_arrow_schema(const lbug::main::QueryResult& result)
return *result.getArrowSchema();
}

ArrowSchema prepared_statement_get_arrow_schema(const lbug::main::PreparedStatement& stmt) {
// Schema only: names/types come from the bound and planned statement, no execution.
return *lbug::common::ArrowConverter::toArrowSchema(stmt.getColumnTypes(),
stmt.getColumnNames(), false /* fallbackExtensionTypes */);
}

bool query_result_has_next_arrow_chunk(lbug::main::QueryResult& result) {
return result.hasNextArrowChunk();
}
Expand Down
Loading