You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "superserious-dev (via GitHub)" <gi...@apache.org> on 2025/08/20 16:06:22 UTC

[PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

superserious-dev opened a new pull request, #8179:
URL: https://github.com/apache/arrow-rs/pull/8179

   # Which issue does this PR close?
   
   - Closes #8178
   
   # Are these changes tested?
   Yes
   
   # Are there any user-facing changes?
   Can use `variant_get` for shredded numeric types
   


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "scovich (via GitHub)" <gi...@apache.org>.
scovich commented on code in PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#discussion_r2292587330


##########
parquet-variant-compute/src/variant_get/output/variant.rs:
##########
@@ -44,40 +85,103 @@ impl OutputBuilder for VariantOutputBuilder<'_> {
         _value_field: &BinaryViewArray,
         typed_value: &ArrayRef,
     ) -> arrow::error::Result<ArrayRef> {
-        // in this case dispatch on the typed_value and
-        // TODO macro'ize this using downcast! to handle all other primitive types
         // TODO(perf): avoid builders entirely (and write the raw variant directly as we know the metadata is the same)
-        let mut array_builder = VariantArrayBuilder::new(variant_array.len());
         match typed_value.data_type() {
+            DataType::Int8 => {
+                cast_partially_shredded_primitive!(
+                    typed_value,
+                    variant_array,
+                    Int8Type,
+                    DataType::Int8
+                )
+            }

Review Comment:
   I actually suspect `fmt` just plain isn't running, for some reason? I've seen other places in this file that aren't formatted as I would have expected.



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "superserious-dev (via GitHub)" <gi...@apache.org>.
superserious-dev commented on code in PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#discussion_r2292436024


##########
parquet-variant-compute/src/variant_get/output/variant.rs:
##########
@@ -44,40 +85,103 @@ impl OutputBuilder for VariantOutputBuilder<'_> {
         _value_field: &BinaryViewArray,
         typed_value: &ArrayRef,
     ) -> arrow::error::Result<ArrayRef> {
-        // in this case dispatch on the typed_value and
-        // TODO macro'ize this using downcast! to handle all other primitive types
         // TODO(perf): avoid builders entirely (and write the raw variant directly as we know the metadata is the same)
-        let mut array_builder = VariantArrayBuilder::new(variant_array.len());
         match typed_value.data_type() {
+            DataType::Int8 => {
+                cast_partially_shredded_primitive!(
+                    typed_value,
+                    variant_array,
+                    Int8Type,
+                    DataType::Int8
+                )
+            }

Review Comment:
   I wonder if it's having trouble inferring things across the macro boundary, so it doesn't know that it can simplify the match arms. I've noticed that macros sometimes behave strangely when it comes to formatting(and LSP diagnostics).



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "superserious-dev (via GitHub)" <gi...@apache.org>.
superserious-dev commented on code in PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#discussion_r2305831426


##########
parquet-variant-compute/src/variant_get/mod.rs:
##########
@@ -202,29 +203,95 @@ mod test {
         );
     }
 
-    /// Shredding: extract a value as a VariantArray
-    #[test]
-    fn get_variant_shredded_int32_as_variant() {
-        let array = shredded_int32_variant_array();
-        let options = GetOptions::new();
-        let result = variant_get(&array, options).unwrap();
-
-        // expect the result is a VariantArray
-        let result: &VariantArray = result.as_any().downcast_ref().unwrap();
-        assert_eq!(result.len(), 4);
-
-        // Expect the values are the same as the original values
-        assert_eq!(result.value(0), Variant::Int32(34));
-        assert!(!result.is_valid(1));
-        assert_eq!(result.value(2), Variant::from("n/a"));
-        assert_eq!(result.value(3), Variant::Int32(100));
+    /// Partial Shredding: extract a value as a VariantArray
+    macro_rules! numeric_partially_shredded_test {
+        ($test:ident, $data_fn:ident, $primitive_type:ty) => {
+            #[test]
+            fn $test() {

Review Comment:
   Updated. I agree that it looks more readable this way.



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "scovich (via GitHub)" <gi...@apache.org>.
scovich commented on code in PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#discussion_r2289375837


##########
parquet-variant-compute/src/variant_get/output/variant.rs:
##########
@@ -44,40 +85,103 @@ impl OutputBuilder for VariantOutputBuilder<'_> {
         _value_field: &BinaryViewArray,
         typed_value: &ArrayRef,
     ) -> arrow::error::Result<ArrayRef> {
-        // in this case dispatch on the typed_value and
-        // TODO macro'ize this using downcast! to handle all other primitive types
         // TODO(perf): avoid builders entirely (and write the raw variant directly as we know the metadata is the same)
-        let mut array_builder = VariantArrayBuilder::new(variant_array.len());
         match typed_value.data_type() {
+            DataType::Int8 => {
+                cast_partially_shredded_primitive!(
+                    typed_value,
+                    variant_array,
+                    Int8Type,
+                    DataType::Int8
+                )
+            }

Review Comment:
   aside: I'm a bit surprised that `fmt` didn't simplify these match arms:
   ```suggestion
               DataType::Int8 => cast_partially_shredded_primitive!(
                   typed_value,
                   variant_array,
                   Int8Type,
                   DataType::Int8
               ),
   ```



##########
parquet-variant-compute/src/variant_get/output/variant.rs:
##########
@@ -18,11 +18,52 @@
 use crate::variant_get::output::OutputBuilder;
 use crate::{VariantArray, VariantArrayBuilder};
 use arrow::array::{Array, ArrayRef, AsArray, BinaryViewArray};
-use arrow::datatypes::Int32Type;
+use arrow::datatypes::{
+    Float16Type, Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type,
+    UInt32Type, UInt64Type, UInt8Type,
+};
 use arrow_schema::{ArrowError, DataType};
 use parquet_variant::{Variant, VariantPath};
 use std::sync::Arc;
 
+macro_rules! cast_perfectly_shredded_primitive {
+    ($typed_value:expr, $variant_array:expr, $arrow_type:ty, $data_type:expr) => {{
+        let mut array_builder = VariantArrayBuilder::new($variant_array.len());
+        let primitive_array = $typed_value.as_primitive::<$arrow_type>();
+        for i in 0..$variant_array.len() {
+            if primitive_array.is_null(i) {
+                array_builder.append_null();
+            } else {
+                let value = primitive_array.value(i);
+                array_builder.append_variant(Variant::from(value));
+            }
+        }

Review Comment:
   if https://github.com/apache/arrow-rs/pull/8105/files#diff-ed469f33b865066aa56a96aa5279ce67d4bf85ea34c7a8c12ffb16afef107b6fR23-R31 merges, this simplifies to:
   ```suggestion
           primitive_conversion_array!($arrow_type, $typed_value, array_builder);
   ```
   TBD whether it's worth defining the macro just to turn 3 lines into one?



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "scovich (via GitHub)" <gi...@apache.org>.
scovich commented on code in PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#discussion_r2298304549


##########
parquet-variant-compute/src/variant_get/output/variant.rs:
##########
@@ -18,11 +18,52 @@
 use crate::variant_get::output::OutputBuilder;
 use crate::{VariantArray, VariantArrayBuilder};
 use arrow::array::{Array, ArrayRef, AsArray, BinaryViewArray};
-use arrow::datatypes::Int32Type;
+use arrow::datatypes::{
+    Float16Type, Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type,
+    UInt32Type, UInt64Type, UInt8Type,
+};
 use arrow_schema::{ArrowError, DataType};
 use parquet_variant::{Variant, VariantPath};
 use std::sync::Arc;
 
+macro_rules! cast_perfectly_shredded_primitive {
+    ($typed_value:expr, $variant_array:expr, $arrow_type:ty, $data_type:expr) => {{
+        let mut array_builder = VariantArrayBuilder::new($variant_array.len());
+        let primitive_array = $typed_value.as_primitive::<$arrow_type>();
+        for i in 0..$variant_array.len() {
+            if primitive_array.is_null(i) {
+                array_builder.append_null();
+            } else {
+                let value = primitive_array.value(i);
+                array_builder.append_variant(Variant::from(value));
+            }
+        }

Review Comment:
   * https://github.com/apache/arrow-rs/pull/8105 has merged



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "superserious-dev (via GitHub)" <gi...@apache.org>.
superserious-dev commented on code in PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#discussion_r2304957672


##########
parquet-variant-compute/src/variant_get/output/variant.rs:
##########
@@ -18,11 +18,52 @@
 use crate::variant_get::output::OutputBuilder;
 use crate::{VariantArray, VariantArrayBuilder};
 use arrow::array::{Array, ArrayRef, AsArray, BinaryViewArray};
-use arrow::datatypes::Int32Type;
+use arrow::datatypes::{
+    Float16Type, Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type,
+    UInt32Type, UInt64Type, UInt8Type,
+};
 use arrow_schema::{ArrowError, DataType};
 use parquet_variant::{Variant, VariantPath};
 use std::sync::Arc;
 
+macro_rules! cast_perfectly_shredded_primitive {
+    ($typed_value:expr, $variant_array:expr, $arrow_type:ty, $data_type:expr) => {{
+        let mut array_builder = VariantArrayBuilder::new($variant_array.len());
+        let primitive_array = $typed_value.as_primitive::<$arrow_type>();
+        for i in 0..$variant_array.len() {
+            if primitive_array.is_null(i) {
+                array_builder.append_null();
+            } else {
+                let value = primitive_array.value(i);
+                array_builder.append_variant(Variant::from(value));
+            }
+        }

Review Comment:
   I switched over to using the merged macro.



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#issuecomment-3254803033

   Sorry for the delay here. I was out for the last week but I am back now and catching up on PRs. I plan to merge this once CI passes


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "superserious-dev (via GitHub)" <gi...@apache.org>.
superserious-dev commented on code in PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#discussion_r2305831897


##########
parquet-variant-compute/src/variant_get/output/variant.rs:
##########
@@ -18,11 +18,36 @@
 use crate::variant_get::output::OutputBuilder;
 use crate::{type_conversion::primitive_conversion_array, VariantArray, VariantArrayBuilder};
 use arrow::array::{Array, ArrayRef, AsArray, BinaryViewArray};
-use arrow::datatypes::{Int16Type, Int32Type};
+use arrow::datatypes::{
+    Float16Type, Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type,
+    UInt32Type, UInt64Type, UInt8Type,
+};
 use arrow_schema::{ArrowError, DataType};
 use parquet_variant::{Variant, VariantPath};
 use std::sync::Arc;
 
+macro_rules! cast_partially_shredded_primitive {
+    ($typed_value:expr, $variant_array:expr, $arrow_type:ty, $data_type:expr) => {{

Review Comment:
   Good catch; removed.



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "scovich (via GitHub)" <gi...@apache.org>.
scovich commented on PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#issuecomment-3234074606

   > > Can use `variant_get` for shredded numeric types
   >        
   >        What happens if `variant_get` encounters a `typed_value: LONG` at the requested path, but the caller requested `Some(Int32)` type that doesn't match? Is there code somewhere else to handle the casting? (I don't see any unit tests that cover the case, either)
   
   Any thoughts on this, @superserious-dev ?


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "superserious-dev (via GitHub)" <gi...@apache.org>.
superserious-dev commented on PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#issuecomment-3234315990

   > > > Can use `variant_get` for shredded numeric types
   > > 
   > > 
   > > What happens if `variant_get` encounters a `typed_value: LONG` at the requested path, but the caller requested `Some(Int32)` type that doesn't match? Is there code somewhere else to handle the casting? (I don't see any unit tests that cover the case, either)
   > 
   > Any thoughts on this, @superserious-dev ?
   
   Currently this PR only covers the case where the user has not requested a specific output type. I noticed there was a separate issue/discussion https://github.com/apache/arrow-rs/issues/8086 for casting, so I didn't implement it here. However I'd be fine with amending this PR to include the casting behavior for numeric types, if that is preferred.
   
   For reference, the code for determining typed output(`as_type` is not None) vs. variant output(`as_type` is None) is here:
   https://github.com/apache/arrow-rs/blob/21a9a2ad5f047d08883fd7589b2053cdd9b56626/parquet-variant-compute/src/variant_get/output/mod.rs#L63-L87 and it's called in `variant_get` here: https://github.com/apache/arrow-rs/blob/1dacecba8e11cac307eea5d1a0f10c22d7f4a8b7/parquet-variant-compute/src/variant_get/mod.rs#L45
   There's a set of pre-existing tests that cover the specific case of int32 -> int32. This could be macro-ified for coverage of all the different combinations of numeric types, eg. int32->int64. https://github.com/apache/arrow-rs/blob/1dacecba8e11cac307eea5d1a0f10c22d7f4a8b7/parquet-variant-compute/src/variant_get/mod.rs#L223-L259
   
   


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#issuecomment-3207645927

   FYI @carpecodeum and @scovich  do you have some time to review this 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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "scovich (via GitHub)" <gi...@apache.org>.
scovich commented on PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#issuecomment-3234582016

   > > > > Can use `variant_get` for shredded numeric types
   > > > 
   > > > 
   > > > What happens if `variant_get` encounters a `typed_value: LONG` at the requested path, but the caller requested `Some(Int32)` type that doesn't match? Is there code somewhere else to handle the casting? (I don't see any unit tests that cover the case, either)
   > > 
   > > 
   > > Any thoughts on this, @superserious-dev ?
   > 
   > Currently this PR only covers the case where the user has not requested a specific output type.
   
   Ah, that makes it easy then!
   
   Thanks for the clear explanation.


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "superserious-dev (via GitHub)" <gi...@apache.org>.
superserious-dev commented on code in PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#discussion_r2288648340


##########
parquet-variant-compute/src/variant_get/output/variant.rs:
##########
@@ -87,31 +191,103 @@ impl OutputBuilder for VariantOutputBuilder<'_> {
         _metadata: &BinaryViewArray,
         typed_value: &ArrayRef,
     ) -> arrow::error::Result<ArrayRef> {
-        // in this case dispatch on the typed_value and
-        // TODO macro'ize this using downcast! to handle all other primitive types
         // TODO(perf): avoid builders entirely (and write the raw variant directly as we know the metadata is the same)
-        let mut array_builder = VariantArrayBuilder::new(variant_array.len());

Review Comment:
   I moved this variable into the macro itself, although passing this into the macro would also work.



##########
parquet-variant/src/variant.rs:
##########
@@ -1242,6 +1242,12 @@ impl From<VariantDecimal16> for Variant<'_, '_> {
     }
 }
 
+impl From<half::f16> for Variant<'_, '_> {

Review Comment:
   Needed to support `DataType::Float16`.
   
   Unrelated to this issue: Seems like it would make sense to also add `Variant::as_f16` for symmetry.



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "scovich (via GitHub)" <gi...@apache.org>.
scovich commented on code in PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#discussion_r2294363770


##########
parquet-variant-compute/src/variant_get/output/variant.rs:
##########
@@ -44,40 +85,103 @@ impl OutputBuilder for VariantOutputBuilder<'_> {
         _value_field: &BinaryViewArray,
         typed_value: &ArrayRef,
     ) -> arrow::error::Result<ArrayRef> {
-        // in this case dispatch on the typed_value and
-        // TODO macro'ize this using downcast! to handle all other primitive types
         // TODO(perf): avoid builders entirely (and write the raw variant directly as we know the metadata is the same)
-        let mut array_builder = VariantArrayBuilder::new(variant_array.len());
         match typed_value.data_type() {
+            DataType::Int8 => {
+                cast_partially_shredded_primitive!(
+                    typed_value,
+                    variant_array,
+                    Int8Type,
+                    DataType::Int8
+                )
+            }

Review Comment:
   ok... weird, but I guess fmt does what it does



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "superserious-dev (via GitHub)" <gi...@apache.org>.
superserious-dev commented on PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#issuecomment-3229419418

   Incorporated @scovich's suggestion and rebased on the latest main.
   
   One question: This PR introduces a few macros(eg. `numeric_perfectly_shredded_test`,` numeric_partially_shredded_test `, `numeric_perfectly_shredded_variant_array_fn`,  `numeric_partially_shredded_variant_array_fn`) to reduce some boilerplate with the numeric tests; @klion26 Introduced an alternate approach for using functions that are generic over PrimitiveArray https://github.com/apache/arrow-rs/blob/1dacecba8e11cac307eea5d1a0f10c22d7f4a8b7/parquet-variant-compute/src/variant_get/mod.rs#L368-L387. I can see pros and cons to each:
   - the macro approach reduces some boilerplate while making it harder to read the tests 
   - the generic approach makes the tests easier to understand at the cost of some more boilerplate
   
   Any suggestions for whether to keep the macros in this PR or switch over to the generic functions? There are also quite a few conversion left, so trying to consider what would work well for those.
   - StringTypes: DataType::Utf8, Utf8View, etc
   - BinaryTypes: Binary, BinaryView, etc
   - Boolean
   - Time Types
   - Date Types
   - Timestamp Types
   


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "superserious-dev (via GitHub)" <gi...@apache.org>.
superserious-dev commented on PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#issuecomment-3231031658

   re casting, I left a comment here: https://github.com/apache/arrow-rs/issues/8087#issuecomment-3207094102. Since casting is an extra step after typed access, I suggested implementing the conversion logic in https://github.com/apache/arrow-rs/issues/8086. 


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "superserious-dev (via GitHub)" <gi...@apache.org>.
superserious-dev commented on code in PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#discussion_r2294220317


##########
parquet-variant-compute/src/variant_get/output/variant.rs:
##########
@@ -44,40 +85,103 @@ impl OutputBuilder for VariantOutputBuilder<'_> {
         _value_field: &BinaryViewArray,
         typed_value: &ArrayRef,
     ) -> arrow::error::Result<ArrayRef> {
-        // in this case dispatch on the typed_value and
-        // TODO macro'ize this using downcast! to handle all other primitive types
         // TODO(perf): avoid builders entirely (and write the raw variant directly as we know the metadata is the same)
-        let mut array_builder = VariantArrayBuilder::new(variant_array.len());
         match typed_value.data_type() {
+            DataType::Int8 => {
+                cast_partially_shredded_primitive!(
+                    typed_value,
+                    variant_array,
+                    Int8Type,
+                    DataType::Int8
+                )
+            }

Review Comment:
   I did an experiment with messing up the formatting of the file and running `cargo fmt` manually. It modifies the file back to it's current state. I don't have any `rustfmt.toml` customizations, so I think this is the formatted result.



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#issuecomment-3254859134

   Thanks again @superserious-dev and @scovich 


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "superserious-dev (via GitHub)" <gi...@apache.org>.
superserious-dev commented on code in PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#discussion_r2292424051


##########
parquet-variant-compute/src/variant_get/mod.rs:
##########
@@ -377,53 +581,114 @@ mod test {
     ///   typed_value: Int32Array,
     /// }
     /// ```
-    fn shredded_int32_variant_array() -> ArrayRef {
-        // At the time of writing, the `VariantArrayBuilder` does not support shredding.
-        // so we must construct the array manually.  see https://github.com/apache/arrow-rs/issues/7895
-        let (metadata, string_value) = {
-            let mut builder = parquet_variant::VariantBuilder::new();
-            builder.append_value("n/a");
-            builder.finish()
+    macro_rules! numeric_partially_shredded_variant_array_fn {
+        ($func:ident, $array_type:ident, $primitive_type:ty) => {
+            fn $func() -> ArrayRef {
+                // At the time of writing, the `VariantArrayBuilder` does not support shredding.
+                // so we must construct the array manually.  see https://github.com/apache/arrow-rs/issues/7895
+                let (metadata, string_value) = {
+                    let mut builder = parquet_variant::VariantBuilder::new();
+                    builder.append_value("n/a");
+                    builder.finish()
+                };
+
+                let nulls = NullBuffer::from(vec![
+                    true,  // row 0 non null
+                    false, // row 1 is null
+                    true,  // row 2 non null
+                    true,  // row 3 non null
+                ]);
+
+                // metadata is the same for all rows
+                let metadata = BinaryViewArray::from_iter_values(std::iter::repeat_n(&metadata, 4));
+
+                // See https://docs.google.com/document/d/1pw0AWoMQY3SjD7R4LgbPvMjG_xSCtXp3rZHkVp9jpZ4/edit?disco=AAABml8WQrY
+                // about why row1 is an empty but non null, value.
+                let values = BinaryViewArray::from(vec![
+                    None,                // row 0 is shredded, so no value
+                    Some(b"" as &[u8]),  // row 1 is null, so empty value (why?)
+                    Some(&string_value), // copy the string value "N/A"
+                    None,                // row 3 is shredded, so no value
+                ]);
+
+                let typed_value = $array_type::from(vec![
+                    Some(<$primitive_type>::try_from(34u8).unwrap()), // row 0 is shredded, so it has a value
+                    None,                                             // row 1 is null, so no value
+                    None, // row 2 is a string, so no typed value

Review Comment:
   Not sure why `cargo fmt` aligns the comment like this.



##########
parquet-variant-compute/src/variant_get/output/variant.rs:
##########
@@ -18,11 +18,52 @@
 use crate::variant_get::output::OutputBuilder;
 use crate::{VariantArray, VariantArrayBuilder};
 use arrow::array::{Array, ArrayRef, AsArray, BinaryViewArray};
-use arrow::datatypes::Int32Type;
+use arrow::datatypes::{
+    Float16Type, Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type,
+    UInt32Type, UInt64Type, UInt8Type,
+};
 use arrow_schema::{ArrowError, DataType};
 use parquet_variant::{Variant, VariantPath};
 use std::sync::Arc;
 
+macro_rules! cast_perfectly_shredded_primitive {
+    ($typed_value:expr, $variant_array:expr, $arrow_type:ty, $data_type:expr) => {{
+        let mut array_builder = VariantArrayBuilder::new($variant_array.len());
+        let primitive_array = $typed_value.as_primitive::<$arrow_type>();
+        for i in 0..$variant_array.len() {
+            if primitive_array.is_null(i) {
+                array_builder.append_null();
+            } else {
+                let value = primitive_array.value(i);
+                array_builder.append_variant(Variant::from(value));
+            }
+        }

Review Comment:
   Yeah, re-using that macro after it merges would work. 



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Variant] Support typed access for numeric types in variant_get [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #8179:
URL: https://github.com/apache/arrow-rs/pull/8179#discussion_r2300543901


##########
parquet-variant-compute/src/variant_get/output/variant.rs:
##########
@@ -44,40 +85,92 @@ impl OutputBuilder for VariantOutputBuilder<'_> {
         _value_field: &BinaryViewArray,
         typed_value: &ArrayRef,
     ) -> arrow::error::Result<ArrayRef> {
-        // in this case dispatch on the typed_value and
-        // TODO macro'ize this using downcast! to handle all other primitive types
         // TODO(perf): avoid builders entirely (and write the raw variant directly as we know the metadata is the same)

Review Comment:
   Now that https://github.com/apache/arrow-rs/pull/8208 is done, we could use the ReadOnlyMetadataBuilder (as a follow on PR)



##########
parquet-variant/src/variant.rs:
##########
@@ -1242,6 +1242,12 @@ impl From<VariantDecimal16> for Variant<'_, '_> {
     }
 }
 
+impl From<half::f16> for Variant<'_, '_> {

Review Comment:
   - Filed https://github.com/apache/arrow-rs/issues/8228



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org