Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for BinaryArray in arrow-vtab #324

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
54 changes: 47 additions & 7 deletions src/vtab/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use crate::vtab::vector::Inserter;
use arrow::array::{
as_boolean_array, as_large_list_array, as_list_array, as_primitive_array, as_string_array, as_struct_array, Array,
ArrayData, AsArray, BooleanArray, Decimal128Array, FixedSizeListArray, GenericListArray, OffsetSizeTrait,
PrimitiveArray, StringArray, StructArray,
as_boolean_array, as_generic_binary_array, as_large_list_array, as_list_array, as_primitive_array, as_string_array,
as_struct_array, Array, ArrayData, AsArray, BinaryArray, BooleanArray, Decimal128Array, FixedSizeListArray,
GenericListArray, OffsetSizeTrait, PrimitiveArray, StringArray, StructArray,
};

use arrow::{
Expand Down Expand Up @@ -227,6 +227,9 @@
DataType::Utf8 => {
string_array_to_vector(as_string_array(col.as_ref()), &mut chunk.flat_vector(i));
}
DataType::Binary => {
binary_array_to_vector(as_generic_binary_array(col.as_ref()), &mut chunk.flat_vector(i));
}
DataType::List(_) => {
list_array_to_vector(as_list_array(col.as_ref()), &mut chunk.list_vector(i))?;
}
Expand Down Expand Up @@ -427,6 +430,15 @@
}
}

fn binary_array_to_vector(array: &BinaryArray, out: &mut FlatVector) {
assert!(array.len() <= out.capacity());

for i in 0..array.len() {
let s = array.value(i);
out.insert(i, s);
}
}

fn list_array_to_vector<O: OffsetSizeTrait + AsPrimitive<usize>>(
array: &GenericListArray<O>,
out: &mut ListVector,
Expand All @@ -440,6 +452,9 @@
DataType::Utf8 => {
string_array_to_vector(as_string_array(value_array.as_ref()), &mut child);
}
DataType::Binary => {
binary_array_to_vector(as_generic_binary_array(value_array.as_ref()), &mut child);
}
_ => {
return Err("Nested list is not supported yet.".into());
}
Expand Down Expand Up @@ -472,6 +487,9 @@
DataType::Utf8 => {
string_array_to_vector(as_string_array(value_array.as_ref()), &mut child);
}
DataType::Binary => {
binary_array_to_vector(as_generic_binary_array(value_array.as_ref()), &mut child);
}
_ => {
return Err("Nested list is not supported yet.".into());
}
Expand Down Expand Up @@ -502,6 +520,9 @@
DataType::Utf8 => {
string_array_to_vector(as_string_array(column.as_ref()), &mut out.child(i));
}
DataType::Binary => {
binary_array_to_vector(as_generic_binary_array(column.as_ref()), &mut out.child(i));
}
DataType::List(_) => {
list_array_to_vector(as_list_array(column.as_ref()), &mut out.list_vector_child(i))?;
}
Expand Down Expand Up @@ -569,10 +590,10 @@
use crate::{Connection, Result};
use arrow::{
array::{
Array, ArrayRef, AsArray, Date32Array, Date64Array, Decimal256Array, Float64Array, GenericListArray,
Int32Array, ListArray, OffsetSizeTrait, PrimitiveArray, StringArray, StructArray, Time32SecondArray,
Time64MicrosecondArray, TimestampMicrosecondArray, TimestampMillisecondArray, TimestampNanosecondArray,
TimestampSecondArray,
Array, ArrayRef, AsArray, BinaryArray, Date32Array, Date64Array, Decimal256Array, Float64Array,
GenericListArray, Int32Array, ListArray, OffsetSizeTrait, PrimitiveArray, StringArray, StructArray,
Time32SecondArray, Time64MicrosecondArray, TimestampMicrosecondArray, TimestampMillisecondArray,
TimestampNanosecondArray, TimestampSecondArray,
},
buffer::{OffsetBuffer, ScalarBuffer},
datatypes::{i256, ArrowPrimitiveType, DataType, Field, Fields, Schema},
Expand Down Expand Up @@ -889,4 +910,23 @@
)
);
}

#[test]
fn test_arrow_binary() {
let byte_array = BinaryArray::from_iter_values(vec![b"test"].iter());

Check failure on line 916 in src/vtab/arrow.rs

View workflow job for this annotation

GitHub Actions / Test x86_64-unknown-linux-gnu

useless use of `vec!`
let arc: ArrayRef = Arc::new(byte_array);
let batch = RecordBatch::try_from_iter(vec![("x", arc)]).unwrap();

let db = Connection::open_in_memory().unwrap();
db.register_table_function::<ArrowVTab>("arrow").unwrap();

let mut stmt = db.prepare("SELECT * FROM arrow(?, ?)").unwrap();

let mut arr = stmt.query_arrow(arrow_recordbatch_to_query_params(batch)).unwrap();
let rb = arr.next().expect("no record batch");

let column = rb.column(0).as_any().downcast_ref::<BinaryArray>().unwrap();
assert_eq!(column.len(), 1);
assert_eq!(column.value(0), b"test");
}
}
20 changes: 18 additions & 2 deletions src/vtab/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use crate::ffi::{
duckdb_list_entry, duckdb_list_vector_get_child, duckdb_list_vector_get_size, duckdb_list_vector_reserve,
duckdb_list_vector_set_size, duckdb_struct_type_child_count, duckdb_struct_type_child_name,
duckdb_struct_vector_get_child, duckdb_validity_set_row_invalid, duckdb_vector,
duckdb_vector_assign_string_element, duckdb_vector_ensure_validity_writable, duckdb_vector_get_column_type,
duckdb_vector_get_data, duckdb_vector_get_validity, duckdb_vector_size,
duckdb_vector_assign_string_element, duckdb_vector_assign_string_element_len,
duckdb_vector_ensure_validity_writable, duckdb_vector_get_column_type, duckdb_vector_get_data,
duckdb_vector_get_validity, duckdb_vector_size,
};

/// Vector trait.
Expand Down Expand Up @@ -111,6 +112,21 @@ impl Inserter<&str> for FlatVector {
}
}

impl Inserter<&[u8]> for FlatVector {
fn insert(&self, index: usize, value: &[u8]) {
let value_size = value.len();
unsafe {
// This function also works for binary data. https://duckdb.org/docs/api/c/api#duckdb_vector_assign_string_element_len
duckdb_vector_assign_string_element_len(
self.ptr,
index as u64,
value.as_ptr() as *const ::std::os::raw::c_char,
value_size as u64,
);
}
}
}

/// A list vector.
pub struct ListVector {
/// ListVector does not own the vector pointer.
Expand Down
Loading