scovich commented on code in PR #8105: URL: https://github.com/apache/arrow-rs/pull/8105#discussion_r2290952570
########## parquet-variant-compute/src/cast_to_variant.rs: ########## @@ -327,7 +227,7 @@ pub fn cast_to_variant(input: &dyn Array) -> Result<VariantArray, ArrowError> { ); } DataType::Decimal256(_, scale) => { - generic_conversion!( + generic_conversion_array!( Decimal256Type, as_primitive, |v: i256| { Review Comment: aside: L237-241 below could simplify to just: ```rust v.to_i128().map_or( Variant::Null, decimal_to_variant_decimal!(v, scale, i128, VariantDecimal16, ) ``` ########## parquet-variant-compute/src/cast_to_variant.rs: ########## @@ -248,44 +148,44 @@ pub fn cast_to_variant(input: &dyn Array) -> Result<VariantArray, ArrowError> { // todo: handle other types like Boolean, Date, Timestamp, etc. match input_type { DataType::Boolean => { - non_generic_conversion!(as_boolean, |v| v, input, builder); + non_generic_conversion_array!(input.as_boolean(), |v| v, builder); } DataType::Binary => { - generic_conversion!(BinaryType, as_bytes, |v| v, input, builder); + generic_conversion_array!(BinaryType, as_bytes, |v| v, input, builder); } DataType::LargeBinary => { - generic_conversion!(LargeBinaryType, as_bytes, |v| v, input, builder); + generic_conversion_array!(LargeBinaryType, as_bytes, |v| v, input, builder); } DataType::BinaryView => { - generic_conversion!(BinaryViewType, as_byte_view, |v| v, input, builder); + generic_conversion_array!(BinaryViewType, as_byte_view, |v| v, input, builder); } DataType::Int8 => { - primitive_conversion!(Int8Type, input, builder); + primitive_conversion_array!(Int8Type, input, builder); } DataType::Int16 => { - primitive_conversion!(Int16Type, input, builder); + primitive_conversion_array!(Int16Type, input, builder); } DataType::Int32 => { - primitive_conversion!(Int32Type, input, builder); + primitive_conversion_array!(Int32Type, input, builder); } DataType::Int64 => { - primitive_conversion!(Int64Type, input, builder); + primitive_conversion_array!(Int64Type, input, builder); } DataType::UInt8 => { - primitive_conversion!(UInt8Type, input, builder); + primitive_conversion_array!(UInt8Type, input, builder); } DataType::UInt16 => { - primitive_conversion!(UInt16Type, input, builder); + primitive_conversion_array!(UInt16Type, input, builder); } DataType::UInt32 => { - primitive_conversion!(UInt32Type, input, builder); + primitive_conversion_array!(UInt32Type, input, builder); } DataType::UInt64 => { - primitive_conversion!(UInt64Type, input, builder); + primitive_conversion_array!(UInt64Type, input, builder); } DataType::Float16 => { - generic_conversion!( + generic_conversion_array!( Float16Type, as_primitive, |v: f16| -> f32 { v.into() }, Review Comment: aside: ```suggestion f32::from, ``` ########## parquet-variant-compute/src/type_conversion.rs: ########## @@ -0,0 +1,113 @@ +// 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. + +//! Module for transforming a typed arrow `Array` to `VariantArray`. + +/// Convert the input array to a `VariantArray` row by row, using `method` +/// not requiring a generic type to downcast the generic array to a specific +/// array type and `cast_fn` to transform each element to a type compatible with Variant +#[macro_export] +macro_rules! non_generic_conversion_array { + ($array:expr, $cast_fn:expr, $builder:expr) => {{ + let array = $array; + for i in 0..array.len() { + if array.is_null(i) { + $builder.append_null(); + continue; + } + let cast_value = $cast_fn(array.value(i)); + $builder.append_variant(Variant::from(cast_value)); + } + }}; +} + +/// Convert the value at a specific index in the given array into a `Variant`. +#[macro_export] +macro_rules! non_generic_conversion_single_value { + ($method:ident, $cast_fn:expr, $input:expr, $index:expr) => {{ + let array = $input.$method(); + if array.is_null($index) { + return Variant::Null; + } + let cast_value = $cast_fn(array.value($index)); + Variant::from(cast_value) + }}; +} + +/// Convert the input array to a `VariantArray` row by row, using `method` +/// requiring a generic type to downcast the generic array to a specific +/// array type and `cast_fn` to transform each element to a type compatible with Variant +#[macro_export] +macro_rules! generic_conversion_array { + ($t:ty, $method:ident, $cast_fn:expr, $input:expr, $builder:expr) => {{ + non_generic_conversion_array!($input.$method::<$t>(), $cast_fn, $builder) + }}; +} + +/// Convert the value at a specific index in the given array into a `Variant`, +/// using `method` requiring a generic type to downcast the generic array +/// to a specific array type and `cast_fn` to transform the element. +#[macro_export] +macro_rules! generic_conversion_single_value { + ($t:ty, $method:ident, $cast_fn:expr, $input:expr, $index:expr) => {{ + let array = $input.$method::<$t>(); + if array.is_null($index) { + return Variant::Null; + } + let cast_value = $cast_fn(array.value($index)); + Variant::from(cast_value) + }}; +} + +/// Convert the input array of a specific primitive type to a `VariantArray` +/// row by row +#[macro_export] +macro_rules! primitive_conversion_array { + ($t:ty, $input:expr, $builder:expr) => {{ + generic_conversion_array!($t, as_primitive, |v| v, $input, $builder) + }}; +} + +/// Convert the value at a specific index in the given array into a `Variant`. +#[macro_export] +macro_rules! primitive_conversion_single_value { + ($t:ty, $input:expr, $index:expr) => {{ + let array = $input.as_primitive::<$t>(); + if array.is_null($index) { + return Variant::Null; Review Comment: `return` from a macro seems dangerous/wrong? It would return from whatever function invoked the macro which is probably _not_ what the caller expected? Is it even what the macro writer intended? To return `Variant::Null` from the _function_ on NULL, but the macro invocation produces a normal `Variant` otherwise? ########## parquet-variant-compute/src/cast_to_variant.rs: ########## @@ -404,7 +304,7 @@ pub fn cast_to_variant(input: &dyn Array) -> Result<VariantArray, ArrowError> { ); } TimeUnit::Nanosecond => { - generic_conversion!( + generic_conversion_array!( Time64NanosecondType, as_primitive, |v| NaiveTime::from_num_seconds_from_midnight_opt( Review Comment: aside: @alamb -- I'm not sure CI is running `fmt` against this file? At least, I've never seen it willing to omit trailing commas for non-macro invocations (L412), and it always formats multi-line lambdas with curly braces even tho I'd personally prefer it didn't: ```rust |v| { NaiveTime::foo( a, b, ) } ``` -- 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