Skip to content
Open
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
38 changes: 38 additions & 0 deletions parquet-variant-compute/src/variant_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ mod test {
use arrow::compute::CastOptions;
use arrow::datatypes::DataType::{Int16, Int32, Int64};
use arrow::datatypes::i256;
use arrow::util::display::FormatOptions;
use arrow_schema::DataType::{Boolean, Float32, Float64, Int8};
use arrow_schema::{DataType, Field, FieldRef, Fields, IntervalUnit, TimeUnit};
use chrono::DateTime;
Expand Down Expand Up @@ -1039,6 +1040,43 @@ mod test {
arrow::array::NullArray::new(3)
);

perfectly_shredded_variant_array_fn!(perfectly_shredded_null_variant_array_with_int, || {
Int32Array::from(vec![Some(32), Some(64), Some(48)])
});

// We append null values if type miss match happens in safe mode
perfectly_shredded_to_arrow_primitive_test!(
get_variant_perfectly_shredded_null_with_type_missmatch_in_safe_mode,
DataType::Null,
perfectly_shredded_null_variant_array_with_int,
arrow::array::NullArray::new(3)
);

// We'll return an error if type miss match happens in strict mode
#[test]
fn get_variant_perfectly_shredded_null_as_null_with_type_missmatch_in_strict_mode() {
let array = perfectly_shredded_null_variant_array_with_int();
let field = Field::new("typed_value", DataType::Null, true);
let options = GetOptions::new()
.with_as_type(Some(FieldRef::from(field)))
.with_cast_options(CastOptions {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use arrow::compute::CastOptions in variant_get and crate::CastOptions in cast_to_variant_with_options, not sure if we need to unify these in the future.

safe: false,
format_options: FormatOptions::default(),
});

let result = variant_get(&array, options);

assert!(result.is_err());
let error_msg = format!("{}", result.unwrap_err());
assert!(
error_msg
.contains("Failed to extract Null from variant Int32(32) at path VariantPath([])"),
"Expected=[Failed to extract Null from variant Int32(32) at path VariantPath([])],\
Got error message=[{}]",
error_msg
);
}

perfectly_shredded_variant_array_fn!(perfectly_shredded_decimal4_variant_array, || {
Decimal32Array::from(vec![Some(12345), Some(23400), Some(-12342)])
.with_precision_and_scale(5, 2)
Expand Down
49 changes: 33 additions & 16 deletions parquet-variant-compute/src/variant_to_arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub(crate) fn make_primitive_variant_to_arrow_row_builder<'a>(

let builder =
match data_type {
DataType::Null => Null(VariantToNullArrowRowBuilder::new(cast_options, capacity)),
DataType::Null => Null(VariantToNullArrowRowBuilder::new(cast_options)),
DataType::Boolean => {
Boolean(VariantToBooleanArrowRowBuilder::new(cast_options, capacity))
}
Expand Down Expand Up @@ -696,26 +696,43 @@ impl VariantToBinaryVariantArrowRowBuilder {
}
}

struct FakeNullBuilder(NullArray);
pub(crate) struct VariantToNullArrowRowBuilder<'a> {
cast_option: &'a CastOptions<'a>,
item_count: usize,
}

impl FakeNullBuilder {
fn new(capacity: usize) -> Self {
Self(NullArray::new(capacity))
impl<'a> VariantToNullArrowRowBuilder<'a> {
fn new(cast_option: &'a CastOptions<'a>) -> Self {
Self {
cast_option,
item_count: 0,
}
}
fn append_value<T>(&mut self, _: T) {}
fn append_null(&mut self) {}

fn finish(self) -> NullArray {
self.0
fn append_value(&mut self, value: &Variant<'_, '_>) -> Result<bool> {
if value.as_null().is_some() {
self.item_count += 1;
Ok(true)
} else if self.cast_option.safe {
self.append_null()?;
Ok(false)
} else {
Err(ArrowError::CastError(format!(
"Failed to extract Null from variant {:?} at path VariantPath([])",
value
)))
}
}
}

define_variant_to_primitive_builder!(
struct VariantToNullArrowRowBuilder<'a>
|capacity| -> FakeNullBuilder { FakeNullBuilder::new(capacity) },
|_value| Some(Variant::Null),
type_name: "Null"
);
fn append_null(&mut self) -> Result<()> {
self.item_count += 1;
Ok(())
}

fn finish(self) -> Result<ArrayRef> {
Ok(Arc::new(NullArray::new(self.item_count)))
}
}

#[cfg(test)]
mod tests {
Expand Down
Loading