use crate::bindings;
use crate::connection::Connection;
use crate::error::Result;
use crate::format::OutputFormat;
use crate::query_result::QueryResult;
use arrow::array::RecordBatchReader;
use arrow::ffi::{FFI_ArrowArray, FFI_ArrowSchema};
use arrow::ffi_stream::FFI_ArrowArrayStream;
use std::sync::atomic::{AtomicU64, Ordering};
static NEXT_REGISTRATION: AtomicU64 = AtomicU64::new(0);
#[must_use = "keep the registration alive until its queries finish"]
pub struct ArrowRegistration<'a> {
conn: &'a mut Connection,
name: String,
source: Option<Box<FFI_ArrowArrayStream>>,
registered: bool,
}
impl<'a> ArrowRegistration<'a> {
pub(crate) fn new(
conn: &'a mut Connection,
reader: Box<dyn RecordBatchReader + Send>,
) -> Result<Self> {
let mut source = Box::new(FFI_ArrowArrayStream::new(reader));
let name = format!(
"__chdb_rust_{}_{}",
std::process::id(),
NEXT_REGISTRATION.fetch_add(1, Ordering::Relaxed)
);
let raw = unsafe { ArrowStream::from_raw(source.as_mut()) };
conn.register_arrow_stream(&name, &raw)?;
Ok(Self {
conn,
name,
source: Some(source),
registered: true,
})
}
pub fn sql_expr(&self) -> String {
arrow_stream_table_sql(&self.name)
}
pub fn query(&self, sql: &str, format: OutputFormat) -> Result<QueryResult> {
self.conn.query(sql, format)
}
pub fn query_arrow(&self, sql: &str) -> Result<crate::arrow_query_stream::ArrowReader> {
self.conn.query_arrow(sql)
}
pub fn query_stream_arrow(
&mut self,
sql: &str,
) -> Result<crate::arrow_query_stream::ArrowQueryStream<'_>> {
self.conn.query_stream_arrow(sql)
}
pub fn query_stream(
&mut self,
sql: &str,
format: OutputFormat,
) -> Result<crate::query_stream::QueryStream<'_>> {
self.conn.query_stream(sql, format)
}
pub fn close(mut self) -> Result<()> {
self.unregister()
}
fn unregister(&mut self) -> Result<()> {
if self.registered {
self.conn.unregister_arrow_table(&self.name)?;
self.registered = false;
}
Ok(())
}
}
impl Drop for ArrowRegistration<'_> {
fn drop(&mut self) {
if self.unregister().is_err() {
if let Some(source) = self.source.take() {
std::mem::forget(source);
}
}
}
}
pub type RawArrowArrayStream = *mut FFI_ArrowArrayStream;
pub type RawArrowSchema = *mut FFI_ArrowSchema;
pub type RawArrowArray = *mut FFI_ArrowArray;
#[derive(Debug, Clone, Copy)]
pub struct ArrowStream {
inner: RawArrowArrayStream,
}
unsafe impl Send for ArrowStream {}
impl ArrowStream {
pub unsafe fn from_raw(stream: RawArrowArrayStream) -> Self {
Self { inner: stream }
}
pub fn as_raw(&self) -> RawArrowArrayStream {
self.inner
}
pub(crate) fn as_c(&self) -> bindings::chdb_arrow_stream {
self.inner.cast()
}
}
#[derive(Debug, Clone, Copy)]
pub struct ArrowSchema {
inner: RawArrowSchema,
}
unsafe impl Send for ArrowSchema {}
impl ArrowSchema {
pub unsafe fn from_raw(schema: RawArrowSchema) -> Self {
Self { inner: schema }
}
pub fn as_raw(&self) -> RawArrowSchema {
self.inner
}
pub(crate) fn as_c(&self) -> bindings::chdb_arrow_schema {
self.inner.cast()
}
}
#[derive(Debug, Clone, Copy)]
pub struct ArrowArray {
inner: RawArrowArray,
}
unsafe impl Send for ArrowArray {}
impl ArrowArray {
pub unsafe fn from_raw(array: RawArrowArray) -> Self {
Self { inner: array }
}
pub fn as_raw(&self) -> RawArrowArray {
self.inner
}
pub(crate) fn as_c(&self) -> bindings::chdb_arrow_array {
self.inner.cast()
}
}
pub fn arrow_stream_table_sql(table_name: &str) -> String {
let escaped = table_name.replace('\'', "''");
format!("ArrowStream('{escaped}')")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn arrow_stream_table_sql_escapes_quotes() {
assert_eq!(arrow_stream_table_sql("a'b"), "ArrowStream('a''b')");
}
#[test]
fn arrow_stream_from_raw_accepts_null() {
let stream = unsafe { ArrowStream::from_raw(std::ptr::null_mut()) };
assert!(stream.as_raw().is_null());
}
#[test]
fn arrow_schema_from_raw_accepts_null() {
let schema = unsafe { ArrowSchema::from_raw(std::ptr::null_mut()) };
assert!(schema.as_raw().is_null());
}
#[test]
fn arrow_array_from_raw_accepts_null() {
let array = unsafe { ArrowArray::from_raw(std::ptr::null_mut()) };
assert!(array.as_raw().is_null());
}
}