alamb commented on code in PR #8044: URL: https://github.com/apache/arrow-rs/pull/8044#discussion_r2256758139
########## parquet-variant-compute/src/cast_to_variant.rs: ########## @@ -0,0 +1,348 @@ +// 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 crate::{VariantArray, VariantArrayBuilder}; +use arrow::array::{Array, AsArray}; +use arrow::datatypes::{ + Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type, UInt32Type, + UInt64Type, UInt8Type, +}; +use arrow_schema::{ArrowError, DataType}; +use parquet_variant::Variant; + +/// Convert the input array of a specific primitive type to a `VariantArray` +/// row by row +macro_rules! primtive_conversion { + ($t:ty, $input:expr, $builder:expr) => {{ + let array = $input.as_primitive::<$t>(); + for i in 0..array.len() { + if array.is_null(i) { + $builder.append_null(); + continue; + } + $builder.append_variant(Variant::from(array.value(i))); + } + }}; +} + +/// Casts a typed arrow [`Array`] to a [`VariantArray`]. This is useful when you +/// need to convert a specific data type +/// +/// # Arguments +/// * `input` - A reference to the input [`ArrayRef`] to cast +/// +/// # Notes +/// If the input array element is null, the corresponding element in the +/// output `VariantArray` will also be null (not `Variant::Null`). +/// +/// # Example +/// ``` +/// # use arrow::array::{Array, ArrayRef, Int64Array}; +/// # use parquet_variant::Variant; +/// # use parquet_variant_compute::cast_to_variant::cast_to_variant; +/// // input is an Int64Array, which will be cast to a VariantArray +/// let input = Int64Array::from(vec![Some(1), None, Some(3)]); +/// let result = cast_to_variant(&input).unwrap(); +/// assert_eq!(result.len(), 3); +/// assert_eq!(result.value(0), Variant::Int64(1)); +/// assert!(result.is_null(1)); // note null, not Variant::Null +/// assert_eq!(result.value(2), Variant::Int64(3)); +/// ``` +pub fn cast_to_variant(input: &dyn Array) -> Result<VariantArray, ArrowError> { + let mut builder = VariantArrayBuilder::new(input.len()); + + let input_type = input.data_type(); + // todo: use `downcast_primitive` to avoid the boilerplate and match more types + match input_type { + DataType::Int8 => { + primtive_conversion!(Int8Type, input, builder); + } + DataType::Int16 => { + primtive_conversion!(Int16Type, input, builder); + } + DataType::Int32 => { + primtive_conversion!(Int32Type, input, builder); + } + DataType::Int64 => { + primtive_conversion!(Int64Type, input, builder); + } + DataType::UInt8 => { + primtive_conversion!(UInt8Type, input, builder); + } + DataType::UInt16 => { + primtive_conversion!(UInt16Type, input, builder); + } + DataType::UInt32 => { + primtive_conversion!(UInt32Type, input, builder); + } + DataType::UInt64 => { + primtive_conversion!(UInt64Type, input, builder); + } + DataType::Float32 => { + primtive_conversion!(Float32Type, input, builder); + } + DataType::Float64 => { + primtive_conversion!(Float64Type, input, builder); + } + dt => { + return Err(ArrowError::CastError(format!( + "Unsupported data type for casting to Variant: {dt:?}", + ))); + } + }; + Ok(builder.build()) +} + +// TODO add cast_with_options that allow specifying Review Comment: I thought more about this, and since there is (currently) no way for a conversion from Variant --> Array to fail, that means the cast options are less obviously relevant (and thus I can't file a ticket as I don't know what it should do) I updated the comment to reflect this ########## parquet-variant-compute/src/cast_to_variant.rs: ########## @@ -0,0 +1,348 @@ +// 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 crate::{VariantArray, VariantArrayBuilder}; +use arrow::array::{Array, AsArray}; +use arrow::datatypes::{ + Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type, UInt32Type, + UInt64Type, UInt8Type, +}; +use arrow_schema::{ArrowError, DataType}; +use parquet_variant::Variant; + +/// Convert the input array of a specific primitive type to a `VariantArray` +/// row by row +macro_rules! primtive_conversion { + ($t:ty, $input:expr, $builder:expr) => {{ + let array = $input.as_primitive::<$t>(); + for i in 0..array.len() { + if array.is_null(i) { + $builder.append_null(); + continue; + } + $builder.append_variant(Variant::from(array.value(i))); + } + }}; +} + +/// Casts a typed arrow [`Array`] to a [`VariantArray`]. This is useful when you +/// need to convert a specific data type +/// +/// # Arguments +/// * `input` - A reference to the input [`ArrayRef`] to cast +/// +/// # Notes +/// If the input array element is null, the corresponding element in the +/// output `VariantArray` will also be null (not `Variant::Null`). +/// +/// # Example +/// ``` +/// # use arrow::array::{Array, ArrayRef, Int64Array}; +/// # use parquet_variant::Variant; +/// # use parquet_variant_compute::cast_to_variant::cast_to_variant; +/// // input is an Int64Array, which will be cast to a VariantArray +/// let input = Int64Array::from(vec![Some(1), None, Some(3)]); +/// let result = cast_to_variant(&input).unwrap(); +/// assert_eq!(result.len(), 3); +/// assert_eq!(result.value(0), Variant::Int64(1)); +/// assert!(result.is_null(1)); // note null, not Variant::Null +/// assert_eq!(result.value(2), Variant::Int64(3)); +/// ``` +pub fn cast_to_variant(input: &dyn Array) -> Result<VariantArray, ArrowError> { + let mut builder = VariantArrayBuilder::new(input.len()); + + let input_type = input.data_type(); + // todo: use `downcast_primitive` to avoid the boilerplate and match more types + match input_type { + DataType::Int8 => { + primtive_conversion!(Int8Type, input, builder); + } + DataType::Int16 => { + primtive_conversion!(Int16Type, input, builder); + } + DataType::Int32 => { + primtive_conversion!(Int32Type, input, builder); + } + DataType::Int64 => { + primtive_conversion!(Int64Type, input, builder); + } + DataType::UInt8 => { + primtive_conversion!(UInt8Type, input, builder); + } + DataType::UInt16 => { + primtive_conversion!(UInt16Type, input, builder); + } + DataType::UInt32 => { + primtive_conversion!(UInt32Type, input, builder); + } + DataType::UInt64 => { + primtive_conversion!(UInt64Type, input, builder); + } + DataType::Float32 => { + primtive_conversion!(Float32Type, input, builder); + } + DataType::Float64 => { + primtive_conversion!(Float64Type, input, builder); + } Review Comment: Indeed it does. However, when I make this change, I get the following errorrs (because we need to implement conversion to/from the other types (like Decimal128 --> i128) <img width="898" height="171" alt="Screenshot 2025-08-06 at 6 42 20 AM" src="https://github.com/user-attachments/assets/5075653c-a2cd-4a69-a1f7-65b14a44ca7e" /> I think we may have to handle those cases specially. I will file a follow on ticket to do so. ``` error[E0277]: the trait bound `parquet_variant::Variant<'_, '_>: std::convert::From<half::binary16::f16>` is not satisfied --> parquet-variant-compute/src/cast_to_variant.rs:72:52 | 72 | Some(value) => builder.append_variant(Variant::from(value)), | ^^^^^^^ the trait `std::convert::From<half::binary16::f16>` is not implemented for `parquet_variant::Variant<'_, '_>` | = help: the following other types implement trait `std::convert::From<T>`: `parquet_variant::Variant<'_, '_>` implements `std::convert::From<&[u8]>` `parquet_variant::Variant<'_, '_>` implements `std::convert::From<&str>` `parquet_variant::Variant<'_, '_>` implements `std::convert::From<()>` `parquet_variant::Variant<'_, '_>` implements `std::convert::From<bool>` `parquet_variant::Variant<'_, '_>` implements `std::convert::From<chrono::datetime::DateTime<chrono::offset::utc::Utc>>` `parquet_variant::Variant<'_, '_>` implements `std::convert::From<chrono::naive::date::NaiveDate>` `parquet_variant::Variant<'_, '_>` implements `std::convert::From<chrono::naive::datetime::NaiveDateTime>` `parquet_variant::Variant<'_, '_>` implements `std::convert::From<f32>` and 12 others ``` -- 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