klion26 commented on code in PR #12465:
URL: https://github.com/apache/arrow-rs/pull/12465#discussion_r4797996119
##########
parquet-variant-compute/src/variant_to_arrow.rs:
##########
@@ -1307,6 +1310,76 @@ define_variant_to_primitive_builder!(
type_name: B::type_name()
);
+pub(crate) struct VariantToStringArrowBuilder<'a, B: StringLikeArrayBuilder> {
+ builder: B,
+ cast_options: &'a CastOptions<'a>,
+ cast_mode: VariantCastMode,
+ buffer: String,
+}
+
+impl<'a, B: StringLikeArrayBuilder> VariantToStringArrowBuilder<'a, B> {
+ fn new(cast_options: &'a CastOptions<'a>, capacity: usize, cast_mode:
VariantCastMode) -> Self {
+ Self {
+ builder: B::with_capacity(capacity),
+ cast_options,
+ cast_mode,
+ buffer: String::new(),
+ }
+ }
+
+ fn append_null(&mut self) -> Result<()> {
+ self.builder.append_null();
+ Ok(())
+ }
+
+ fn append_value(&mut self, value: &Variant<'_, '_>) -> Result<bool> {
+ let value = match value {
+ // short-circuit the common case of string-like variants to avoid
the overhead of casting
+ Variant::String(value) => value,
+ Variant::ShortString(value) => value.as_str(),
+ Variant::Null => {
+ self.builder.append_null();
+ return Ok(false);
+ }
+ _ => return self.append_cast_value(value),
+ };
+
+ self.builder.append_value(value);
+ Ok(true)
+ }
+
+ fn append_cast_value(&mut self, value: &Variant<'_, '_>) -> Result<bool> {
+ self.buffer.clear();
+ match variant_cast_with_options(value, self.cast_options, |value| {
+ match self.cast_mode {
+ VariantCastMode::Get => match write_variant_to_string(&mut
self.buffer, value) {
+ true => Some(self.buffer.as_str()),
+ false => None,
+ },
+ // this function only handle no-string variant, return None
here because we don't support shred non-string to string.
+ VariantCastMode::Shred => None,
+ }
+ }) {
+ Ok(Some(value)) => {
+ self.builder.append_value(value);
+ Ok(true)
+ }
+ Ok(None) => {
+ self.builder.append_null();
+ Ok(false)
+ }
+ Err(_) => Err(ArrowError::CastError(format!(
+ "Failed to extract primitive of type {} from variant
{value:?}",
+ B::type_name()
+ ))),
+ }
+ }
Review Comment:
From my side, The `string/shortstring/null` handling arm is intentional, so
I left the current state as is.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]