klion26 commented on code in PR #8105:
URL: https://github.com/apache/arrow-rs/pull/8105#discussion_r2289872387


##########
parquet-variant-compute/src/type_conversion.rs:
##########
@@ -0,0 +1,166 @@
+// 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 of a specific primitive type to a `VariantArray`
+/// row by row
+#[macro_export]
+macro_rules! primitive_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)));

Review Comment:
   I'm fine with the ruturn/break/continue as soon as possible way, but if we 
have a strong preference here, I can change it.



##########
parquet-variant-compute/src/type_conversion.rs:
##########
@@ -0,0 +1,166 @@
+// 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 of a specific primitive type to a `VariantArray`
+/// row by row
+#[macro_export]
+macro_rules! primitive_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)));
+        }
+    }};
+}
+
+/// 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;
+        }
+        Variant::from(array.value($index))
+    }};
+}
+
+/// 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) => {{
+        let array = $input.$method::<$t>();
+        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`,
+/// 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 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 {
+    ($method:ident, $cast_fn:expr, $input:expr, $builder:expr) => {{
+        let array = $input.$method();
+        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));
+        }
+    }};
+}
+
+#[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;
+        }
+        Variant::from(cast_value)
+    }};
+}
+
+/// Convert a decimal value to a `VariantDecimal`
+#[macro_export]
+macro_rules! decimal_to_variant_decimal {
+    ($v:ident, $scale:expr, $value_type:ty, $variant_type:ty) => {
+        if *$scale < 0 {
+            // For negative scale, we need to multiply the value by 10^|scale|
+            // For example: 123 with scale -2 becomes 12300
+            let multiplier = (10 as $value_type).pow((-*$scale) as u32);
+            // Check for overflow
+            if $v > 0 && $v > <$value_type>::MAX / multiplier {
+                return Variant::Null;
+            }
+            if $v < 0 && $v < <$value_type>::MIN / multiplier {
+                return Variant::Null;
+            }
+            <$variant_type>::try_new($v * multiplier, 0)
+                .map(|v| v.into())
+                .unwrap_or(Variant::Null)
+        } else {
+            <$variant_type>::try_new($v, *$scale as u8)
+                .map(|v| v.into())
+                .unwrap_or(Variant::Null)
+        }
+    };
+}
+
+/// Convert arrays that don't need generic type parameters
+#[macro_export]
+macro_rules! cast_conversion_nongeneric {

Review Comment:
   Yes, I've removed the redundant macros. My bad for not figuring it out when 
copying it. 



##########
parquet-variant-compute/src/type_conversion.rs:
##########
@@ -0,0 +1,166 @@
+// 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 of a specific primitive type to a `VariantArray`
+/// row by row
+#[macro_export]
+macro_rules! primitive_conversion {

Review Comment:
   fixed



##########
parquet-variant-compute/src/type_conversion.rs:
##########
@@ -0,0 +1,166 @@
+// 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 of a specific primitive type to a `VariantArray`
+/// row by row
+#[macro_export]
+macro_rules! primitive_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)));
+        }
+    }};
+}
+
+/// 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;
+        }
+        Variant::from(array.value($index))
+    }};
+}
+
+/// 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) => {{
+        let array = $input.$method::<$t>();
+        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`,
+/// 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 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 {
+    ($method:ident, $cast_fn:expr, $input:expr, $builder:expr) => {{
+        let array = $input.$method();
+        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));
+        }
+    }};
+}
+
+#[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;
+        }
+        Variant::from(cast_value)
+    }};
+}
+
+/// Convert a decimal value to a `VariantDecimal`
+#[macro_export]
+macro_rules! decimal_to_variant_decimal {
+    ($v:ident, $scale:expr, $value_type:ty, $variant_type:ty) => {
+        if *$scale < 0 {
+            // For negative scale, we need to multiply the value by 10^|scale|
+            // For example: 123 with scale -2 becomes 12300
+            let multiplier = (10 as $value_type).pow((-*$scale) as u32);
+            // Check for overflow
+            if $v > 0 && $v > <$value_type>::MAX / multiplier {
+                return Variant::Null;
+            }
+            if $v < 0 && $v < <$value_type>::MIN / multiplier {
+                return Variant::Null;
+            }
+            <$variant_type>::try_new($v * multiplier, 0)
+                .map(|v| v.into())
+                .unwrap_or(Variant::Null)
+        } else {
+            <$variant_type>::try_new($v, *$scale as u8)
+                .map(|v| v.into())
+                .unwrap_or(Variant::Null)
+        }

Review Comment:
   fixed



##########
parquet-variant-compute/src/type_conversion.rs:
##########
@@ -0,0 +1,166 @@
+// 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 of a specific primitive type to a `VariantArray`
+/// row by row
+#[macro_export]
+macro_rules! primitive_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)));
+        }
+    }};
+}
+
+/// 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;
+        }
+        Variant::from(array.value($index))
+    }};
+}
+
+/// 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) => {{
+        let array = $input.$method::<$t>();
+        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`,
+/// 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 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 {
+    ($method:ident, $cast_fn:expr, $input:expr, $builder:expr) => {{
+        let array = $input.$method();

Review Comment:
   I tried this, and it works; the code looks cleaner than before. But there is 
a tiny problem with this: when using the `primitive_conversion` macro, we need 
to import the three macros(encounter this in 
`parquet-variant-compute/src/variant_get/output.variant.rs`). If we accept this 
way, I will do it in a follow-up pr.



-- 
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

Reply via email to