scovich commented on code in PR #7919: URL: https://github.com/apache/arrow-rs/pull/7919#discussion_r2207925813
########## parquet-variant-compute/src/variant_get.rs: ########## @@ -0,0 +1,202 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +use std::sync::Arc; + +use arrow::{ + array::{Array, ArrayRef}, + compute::CastOptions, + error::Result, +}; +use arrow_schema::{ArrowError, Field}; +use parquet_variant::path::VariantPath; + +use crate::{VariantArray, VariantArrayBuilder}; + +/// Returns an array with the specified path extracted from the variant values. +/// +/// The return array type depends on the `as_type` field of the options parameter +/// 1. `as_type: None`: a VariantArray is returned. The values in this new VariantArray will point +/// to the specified path. +/// 2. `as_type: Some(<specific field>)`: an array of the specified type is returned. +pub fn variant_get(input: &ArrayRef, options: GetOptions) -> Result<ArrayRef> { + let variant_array: &VariantArray = input.as_any().downcast_ref().ok_or_else(|| { + ArrowError::InvalidArgumentError( + "expected a VariantArray as the input for variant_get".to_owned(), + ) + })?; + + if let Some(as_type) = options.as_type { + return Err(ArrowError::NotYetImplemented(format!( + "getting a {} from a VariantArray is not implemented yet", + as_type + ))); + } + + let mut builder = VariantArrayBuilder::new(variant_array.len()); + for i in 0..variant_array.len() { + let new_variant = variant_array.value(i); Review Comment: I just realized something -- when dealing with shredded variants, this `value` method will do a lot of work to unshred and encode the whole thing (see e.g. https://github.com/apache/arrow-rs/pull/7915#discussion_r2205755479). And that work is not memoized anywhere unless the caller is able to do so. For efficiency reasons we should strongly consider some kind of direct pathing support in `VariantArray` itself. Otherwise, it would be far too easy for a caller to accidentally do quadratic work when repeatedly calling value+get_path pairs for different paths. ########## parquet-variant-compute/src/variant_get.rs: ########## @@ -0,0 +1,202 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +use std::sync::Arc; + +use arrow::{ + array::{Array, ArrayRef}, + compute::CastOptions, + error::Result, +}; +use arrow_schema::{ArrowError, Field}; +use parquet_variant::path::VariantPath; + +use crate::{VariantArray, VariantArrayBuilder}; + +/// Returns an array with the specified path extracted from the variant values. +/// +/// The return array type depends on the `as_type` field of the options parameter +/// 1. `as_type: None`: a VariantArray is returned. The values in this new VariantArray will point +/// to the specified path. +/// 2. `as_type: Some(<specific field>)`: an array of the specified type is returned. +pub fn variant_get(input: &ArrayRef, options: GetOptions) -> Result<ArrayRef> { + let variant_array: &VariantArray = input.as_any().downcast_ref().ok_or_else(|| { + ArrowError::InvalidArgumentError( + "expected a VariantArray as the input for variant_get".to_owned(), + ) + })?; + + if let Some(as_type) = options.as_type { + return Err(ArrowError::NotYetImplemented(format!( + "getting a {} from a VariantArray is not implemented yet", + as_type + ))); + } + + let mut builder = VariantArrayBuilder::new(variant_array.len()); + for i in 0..variant_array.len() { + let new_variant = variant_array.value(i); + // TODO: perf? + let new_variant = new_variant.get_path(&options.path); + if let Some(new_variant) = new_variant { + // TODO: we're decoding the value and doing a copy into a variant value again. This + // copy can be much smarter. + builder.append_variant(new_variant); + } else { + builder.append_null(); + } Review Comment: nit - why not just: ```suggestion // TODO: we're decoding the value and doing a copy into a variant value again. This // copy can be much smarter. match new_variant.get_path(&options.path) { Some(new_variant) => builder.append_variant(new_variant), None => builder.append_null(), } ``` ########## parquet-variant-compute/src/variant_get.rs: ########## @@ -0,0 +1,202 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +use std::sync::Arc; + +use arrow::{ + array::{Array, ArrayRef}, + compute::CastOptions, + error::Result, +}; +use arrow_schema::{ArrowError, Field}; +use parquet_variant::path::VariantPath; + +use crate::{VariantArray, VariantArrayBuilder}; + +/// Returns an array with the specified path extracted from the variant values. +/// +/// The return array type depends on the `as_type` field of the options parameter +/// 1. `as_type: None`: a VariantArray is returned. The values in this new VariantArray will point +/// to the specified path. +/// 2. `as_type: Some(<specific field>)`: an array of the specified type is returned. +pub fn variant_get(input: &ArrayRef, options: GetOptions) -> Result<ArrayRef> { + let variant_array: &VariantArray = input.as_any().downcast_ref().ok_or_else(|| { + ArrowError::InvalidArgumentError( + "expected a VariantArray as the input for variant_get".to_owned(), + ) + })?; + + if let Some(as_type) = options.as_type { + return Err(ArrowError::NotYetImplemented(format!( + "getting a {} from a VariantArray is not implemented yet", + as_type + ))); + } + + let mut builder = VariantArrayBuilder::new(variant_array.len()); + for i in 0..variant_array.len() { + let new_variant = variant_array.value(i); + // TODO: perf? + let new_variant = new_variant.get_path(&options.path); + if let Some(new_variant) = new_variant { + // TODO: we're decoding the value and doing a copy into a variant value again. This + // copy can be much smarter. + builder.append_variant(new_variant); + } else { + builder.append_null(); + } + } + + Ok(Arc::new(builder.build())) +} + +/// Controls the action of the variant_get kernel. +#[derive(Debug, Clone)] +pub struct GetOptions<'a> { + /// What path to extract + pub path: VariantPath<'a>, + /// if `as_type` is None, the returned array will itself be a VariantArray. + /// + /// if `as_type` is `Some(type)` the field is returned as the specified type if possible. To specify returning + /// a Variant, pass a Field with variant type in the metadata. + pub as_type: Option<Field>, Review Comment: I thought `None` was the way to request a variant result? But meanwhile -- I tend to agree with @alamb that we should separate out casting from pathing. If `get_path` anyway has to return a `Variant`, somebody (ie arrow array builder) who actually cares about the type will still have to match on the result even if they just panic in the "impossible" case of an unexpected enum variant. Seems better to let the caller do the casting themselves since they probably have a strongly-typed place to put the result. As a bonus, that would also mean we don't need to define `GetOptions` at this low level -- `get_path` only needs to know about `VariantPath`. Arrow variant compute and the array builders could worry about the rest of the options space. ########## parquet-variant/src/variant.rs: ########## @@ -1063,6 +1064,20 @@ impl<'m, 'v> Variant<'m, 'v> { _ => None, } } + + /// Return a new Variant with the path followed. + /// + /// If the path is not found, `None` is returned. + pub fn get_path(&self, path: &VariantPath) -> Option<Variant> { + let mut output = self.clone(); + for element in path.iter() { + output = match element { + VariantPathElement::Field { name } => output.get_object_field(name)?, + VariantPathElement::Index { index } => output.get_list_element(*index)?, + }; + } + Some(output) Review Comment: I _think_ this is really just [Iterator::try_fold](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.try_fold)? ```suggestion path.iter().try_fold(self.clone(), |output, element| match element { VariantPathElement::Field { name } => output.get_object_field(name), VariantPathElement::Index { index } => output.get_list_element(*index), }) ``` -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org