This is an automated email from the ASF dual-hosted git repository.

zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 611b9724 fix(arrow/extensions): reject unsigned Variant typed_value 
(#1480)
611b9724 is described below

commit 611b9724e692fe5b6801f7522f071c061712363a
Author: Digvijay <[email protected]>
AuthorDate: Mon Nov 16 11:04:42 2026 -0600

    fix(arrow/extensions): reject unsigned Variant typed_value (#1480)
    
    ### Rationale for this change
    
    An unsigned Arrow integer used as a Variant `typed_value` was written to
    Parquet as an unsigned integer logical type. That is not a valid
    shredded Variant type. The Parquet shredding spec allows only signed
    integers (`int8`, `int16`, `int32`, `int64`), and apache/arrow#50810
    removed the unsigned mappings from the Arrow canonical extension.
    
    Case 127 of the shredded-variant interoperability suite expects
    `INTEGER(32,false)` to be rejected. It was skipped with a comment that
    said the column is upcast to `int64`. It is not. The column was kept as
    Arrow `uint32`.
    
    Fixes #1202
    
    ### What changes are included in this PR?
    
    - `NewVariantType` rejects unsigned `typed_value` fields, including
    nested shredded fields
    - A struct that only has `typed_value` uses the same nested check as a
    struct that also has `value`
    - Reading a Variant group whose `typed_value` is an unsigned integer
    fails while building the extension type
    - Case 127 is no longer skipped
    
    ### Are these changes tested?
    
    - `go test ./arrow/extensions -count=1 -run 'TestVariant|TestManyTypes'`
    - `go test ./parquet/pqarrow -count=1 -run
    'TestConvertSchema|TestShreddedVariant|TestToParquet|TestVariant'`
    
    The shredded-variant file suite still needs `PARQUET_TEST_DATA`. The
    schema conversion test covers the same rejection case 127 hits, without
    that data.
    
    ### Are there any user-facing changes?
    
    Yes. Constructing a Variant extension type with an unsigned
    `typed_value`, or reading a Parquet Variant shredded as an unsigned
    integer, now returns `arrow.ErrInvalid`.
    
    Since v18.4.0 the Parquet writer has written Arrow `uint8`, `uint16`,
    `uint32`, and `uint64` `typed_value` columns as unsigned `INTEGER`
    logical types. Those files now fail to read. That is what the shredding
    spec and case 127 require.
---
 arrow/extensions/variant.go      | 64 ++++++++++++++--------------
 arrow/extensions/variant_test.go | 91 ++++++++++++++++++++++++++++++----------
 parquet/pqarrow/schema_test.go   | 16 +++++++
 parquet/pqarrow/variant_test.go  | 11 ++++-
 4 files changed, 129 insertions(+), 53 deletions(-)

diff --git a/arrow/extensions/variant.go b/arrow/extensions/variant.go
index 270f1db3..73eb791e 100644
--- a/arrow/extensions/variant.go
+++ b/arrow/extensions/variant.go
@@ -148,8 +148,9 @@ func NewShreddedVariantType(dt arrow.DataType) *VariantType 
{
 //  3. Must satisfy exactly one of the following:
 //     a. MUST have non-nullable field named "value" that is 
binary/largebinary/binary_view
 //     b. MUST have an nullable field named "value" that is 
binary/largebinary/binary_view
-//     and another nullable field named "typed_value" that is either a 
primitive type or
-//     a list/large_list/list_view or struct which also satisfies the 
following requirements:
+//     and another nullable field named "typed_value" that is either a 
primitive type
+//     (other than an unsigned integer) or a list/large_list/list_view or 
struct which
+//     also satisfies the following requirements.
 //     i. The elements must be NON-NULLABLE
 //     ii. There must either be a single NON-NULLABLE field named "value" 
which is
 //     binary/largebinary/binary_view or have an nullable "value" field and an 
nullable
@@ -227,6 +228,9 @@ func NewVariantType(storage arrow.DataType) (*VariantType, 
error) {
        if dt.ID() == arrow.NULL {
                return nil, fmt.Errorf("%w: typed_value field must not be null 
type", arrow.ErrInvalid)
        }
+       if isUnsignedInteger(dt) {
+               return nil, fmt.Errorf("%w: unsigned typed_value %s is not a 
valid shredded variant type", arrow.ErrInvalid, dt)
+       }
 
        if nt, ok := dt.(arrow.NestedType); ok {
                if !validNestedType(nt) {
@@ -347,6 +351,19 @@ func isNullType(dt arrow.DataType) bool {
        return st == nil || st.ID() == arrow.NULL
 }
 
+func isUnsignedInteger(dt arrow.DataType) bool {
+       st := storageType(dt)
+       if st == nil {
+               return false
+       }
+       switch st.ID() {
+       case arrow.UINT8, arrow.UINT16, arrow.UINT32, arrow.UINT64:
+               return true
+       default:
+               return false
+       }
+}
+
 func validStruct(s *arrow.StructType) bool {
        switch s.NumFields() {
        case 1:
@@ -354,7 +371,7 @@ func validStruct(s *arrow.StructType) bool {
                if f.Name == "value" {
                        return isBinary(f.Type)
                }
-               return f.Name == "typed_value" && !isNullType(f.Type)
+               return f.Name == "typed_value" && validTypedValue(f.Type)
        case 2:
                valField, ok := s.FieldByName("value")
                if !ok || !valField.Nullable || !isBinary(valField.Type) {
@@ -366,16 +383,19 @@ func validStruct(s *arrow.StructType) bool {
                        return false
                }
 
-               if nt, ok := typedField.Type.(arrow.NestedType); ok && 
nt.Name() != "extension" {
-                       return validNestedType(nt)
-               }
-
-               return !isNullType(typedField.Type)
+               return validTypedValue(typedField.Type)
        default:
                return false
        }
 }
 
+func validTypedValue(dt arrow.DataType) bool {
+       if nt, ok := dt.(arrow.NestedType); ok && nt.Name() != "extension" {
+               return validNestedType(nt)
+       }
+       return !isNullType(dt) && !isUnsignedInteger(dt)
+}
+
 func validNestedType(dt arrow.NestedType) bool {
        switch t := dt.(type) {
        case arrow.ListLikeType:
@@ -729,16 +749,10 @@ func createPrimitiveVariantReader(arr arrow.Array) 
(typedValReader, error) {
                return asVariantReader[bool]{typedVal: a}, nil
        case *array.Int8:
                return asVariantReader[int8]{typedVal: a}, nil
-       case *array.Uint8:
-               return asVariantReader[uint8]{typedVal: a}, nil
        case *array.Int16:
                return asVariantReader[int16]{typedVal: a}, nil
-       case *array.Uint16:
-               return asVariantReader[uint16]{typedVal: a}, nil
        case *array.Int32:
                return asVariantReader[int32]{typedVal: a}, nil
-       case *array.Uint32:
-               return asVariantReader[uint32]{typedVal: a}, nil
        case *array.Int64:
                return asVariantReader[int64]{typedVal: a}, nil
        case *array.Float32:
@@ -1278,11 +1292,11 @@ func variantTypeFromArrow(dt arrow.DataType) 
variant.Type {
                return variant.Bool
        case arrow.INT8:
                return variant.Int8
-       case arrow.INT16, arrow.UINT8:
+       case arrow.INT16:
                return variant.Int16
-       case arrow.INT32, arrow.UINT16:
+       case arrow.INT32:
                return variant.Int32
-       case arrow.INT64, arrow.UINT32:
+       case arrow.INT64:
                return variant.Int64
        case arrow.FLOAT32:
                return variant.Float
@@ -1499,7 +1513,7 @@ type typedBuilder[T arrow.ValueType] interface {
        Append(T)
 }
 
-func appendToTarget[T int8 | uint8 | int16 | uint16 | int32 | uint32 | 
int64](bldr typedBuilder[T], v int64) bool {
+func appendToTarget[T int8 | int16 | int32 | int64](bldr typedBuilder[T], v 
int64) bool {
        if int64(T(v)) == v {
                bldr.Append(T(v))
                return true
@@ -1508,7 +1522,7 @@ func appendToTarget[T int8 | uint8 | int16 | uint16 | 
int32 | uint32 | int64](bl
        return false
 }
 
-func appendNumericToTarget[T int8 | uint8 | int16 | uint16 | int32 | uint32 | 
int64](bldr typedBuilder[T], v variant.Value) bool {
+func appendNumericToTarget[T int8 | int16 | int32 | int64](bldr 
typedBuilder[T], v variant.Value) bool {
        switch val := v.Value().(type) {
        case int8:
                return appendToTarget(bldr, int64(val))
@@ -1542,26 +1556,14 @@ func (b *shreddedPrimitiveBuilder) tryTyped(v 
variant.Value) (residual []byte) {
                if appendNumericToTarget(bldr, v) {
                        return nil
                }
-       case *array.Uint8Builder:
-               if appendNumericToTarget(bldr, v) {
-                       return nil
-               }
        case *array.Int16Builder:
                if appendNumericToTarget(bldr, v) {
                        return nil
                }
-       case *array.Uint16Builder:
-               if appendNumericToTarget(bldr, v) {
-                       return nil
-               }
        case *array.Int32Builder:
                if appendNumericToTarget(bldr, v) {
                        return nil
                }
-       case *array.Uint32Builder:
-               if appendNumericToTarget(bldr, v) {
-                       return nil
-               }
        case *array.Int64Builder:
                if appendNumericToTarget(bldr, v) {
                        return nil
diff --git a/arrow/extensions/variant_test.go b/arrow/extensions/variant_test.go
index 4ac0f867..7709bd5d 100644
--- a/arrow/extensions/variant_test.go
+++ b/arrow/extensions/variant_test.go
@@ -111,6 +111,55 @@ func TestVariantExtensionType(t *testing.T) {
                        arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
                        arrow.Field{Name: "typed_value", Type: 
extensions.NewOpaqueType(nil, "null", "test"), Nullable: true}),
                        "typed_value field has invalid storage type"},
+               {arrow.StructOf(
+                       arrow.Field{Name: "metadata", Type: 
arrow.BinaryTypes.Binary, Nullable: false},
+                       arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
+                       arrow.Field{Name: "typed_value", Type: 
arrow.PrimitiveTypes.Uint8, Nullable: true}),
+                       "unsigned typed_value uint8 is not a valid shredded 
variant type"},
+               {arrow.StructOf(
+                       arrow.Field{Name: "metadata", Type: 
arrow.BinaryTypes.Binary, Nullable: false},
+                       arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
+                       arrow.Field{Name: "typed_value", Type: 
arrow.PrimitiveTypes.Uint16, Nullable: true}),
+                       "unsigned typed_value uint16 is not a valid shredded 
variant type"},
+               {arrow.StructOf(
+                       arrow.Field{Name: "metadata", Type: 
arrow.BinaryTypes.Binary, Nullable: false},
+                       arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
+                       arrow.Field{Name: "typed_value", Type: 
arrow.PrimitiveTypes.Uint32, Nullable: true}),
+                       "unsigned typed_value uint32 is not a valid shredded 
variant type"},
+               {arrow.StructOf(
+                       arrow.Field{Name: "metadata", Type: 
arrow.BinaryTypes.Binary, Nullable: false},
+                       arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
+                       arrow.Field{Name: "typed_value", Type: 
arrow.PrimitiveTypes.Uint64, Nullable: true}),
+                       "unsigned typed_value uint64 is not a valid shredded 
variant type"},
+               {arrow.StructOf(
+                       arrow.Field{Name: "metadata", Type: 
arrow.BinaryTypes.Binary, Nullable: false},
+                       arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
+                       arrow.Field{Name: "typed_value", Type: arrow.StructOf(
+                               arrow.Field{Name: "n", Type: arrow.StructOf(
+                                       arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
+                                       arrow.Field{Name: "typed_value", Type: 
arrow.PrimitiveTypes.Uint32, Nullable: true},
+                               )},
+                       ), Nullable: true}),
+                       "typed_value field must be a valid nested type"},
+               {arrow.StructOf(
+                       arrow.Field{Name: "metadata", Type: 
arrow.BinaryTypes.Binary, Nullable: false},
+                       arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
+                       arrow.Field{Name: "typed_value", Type: 
arrow.ListOfField(arrow.Field{Name: "element", Type: arrow.StructOf(
+                               arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
+                               arrow.Field{Name: "typed_value", Type: 
arrow.PrimitiveTypes.Uint32, Nullable: true},
+                       ), Nullable: false}), Nullable: true}),
+                       "typed_value field must be a valid nested type"},
+               {arrow.StructOf(
+                       arrow.Field{Name: "metadata", Type: 
arrow.BinaryTypes.Binary, Nullable: false},
+                       arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
+                       arrow.Field{Name: "typed_value", Type: 
arrow.ListOfField(arrow.Field{Name: "element", Type: arrow.StructOf(
+                               arrow.Field{Name: "typed_value", Type: 
arrow.StructOf(
+                                       arrow.Field{Name: "n", Type: 
arrow.StructOf(
+                                               arrow.Field{Name: 
"typed_value", Type: arrow.PrimitiveTypes.Uint32, Nullable: true},
+                                       )},
+                               ), Nullable: true},
+                       ), Nullable: false}), Nullable: true}),
+                       "typed_value field must be a valid nested type"},
        }
 
        for _, tt := range tests {
@@ -1197,25 +1246,25 @@ func TestManyTypesShredded(t *testing.T) {
                                arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
                                arrow.Field{Name: "typed_value", Type: 
arrow.PrimitiveTypes.Int8, Nullable: true},
                        )},
-                       arrow.Field{Name: "uint8", Type: arrow.StructOf(
+                       arrow.Field{Name: "int16_from_uint8", Type: 
arrow.StructOf(
                                arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
-                               arrow.Field{Name: "typed_value", Type: 
arrow.PrimitiveTypes.Uint8, Nullable: true},
+                               arrow.Field{Name: "typed_value", Type: 
arrow.PrimitiveTypes.Int16, Nullable: true},
                        )},
                        arrow.Field{Name: "int16", Type: arrow.StructOf(
                                arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
                                arrow.Field{Name: "typed_value", Type: 
arrow.PrimitiveTypes.Int16, Nullable: true},
                        )},
-                       arrow.Field{Name: "uint16", Type: arrow.StructOf(
+                       arrow.Field{Name: "int32_from_uint16", Type: 
arrow.StructOf(
                                arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
-                               arrow.Field{Name: "typed_value", Type: 
arrow.PrimitiveTypes.Uint16, Nullable: true},
+                               arrow.Field{Name: "typed_value", Type: 
arrow.PrimitiveTypes.Int32, Nullable: true},
                        )},
                        arrow.Field{Name: "int32", Type: arrow.StructOf(
                                arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
                                arrow.Field{Name: "typed_value", Type: 
arrow.PrimitiveTypes.Int32, Nullable: true},
                        )},
-                       arrow.Field{Name: "uint32", Type: arrow.StructOf(
+                       arrow.Field{Name: "int64_from_uint32", Type: 
arrow.StructOf(
                                arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
-                               arrow.Field{Name: "typed_value", Type: 
arrow.PrimitiveTypes.Uint32, Nullable: true},
+                               arrow.Field{Name: "typed_value", Type: 
arrow.PrimitiveTypes.Int64, Nullable: true},
                        )},
                        arrow.Field{Name: "bytes", Type: arrow.StructOf(
                                arrow.Field{Name: "value", Type: 
arrow.BinaryTypes.Binary, Nullable: true},
@@ -1254,18 +1303,18 @@ func TestManyTypesShredded(t *testing.T) {
 
        values := []any{
                map[string]any{
-                       "strval":    "click",
-                       "bool":      true,
-                       "int8":      int8(42),
-                       "uint8":     uint8(255),
-                       "int16":     int16(12345),
-                       "uint16":    uint16(54321),
-                       "int32":     int32(1234567890),
-                       "uint32":    uint32(1234567890),
-                       "bytes":     []byte{0xDE, 0xAD, 0xBE, 0xEF},
-                       "timemicro": arrow.Time64(43200000000),
-                       "uuid":      
uuid.MustParse("123e4567-e89b-12d3-a456-426614174000"),
-                       "event_day": arrow.Date32FromTime(time.Date(2024, 10, 
24, 0, 0, 0, 0, time.UTC)),
+                       "strval":            "click",
+                       "bool":              true,
+                       "int8":              int8(42),
+                       "int16_from_uint8":  uint8(255),
+                       "int16":             int16(12345),
+                       "int32_from_uint16": uint16(54321),
+                       "int32":             int32(1234567890),
+                       "int64_from_uint32": uint32(1234567890),
+                       "bytes":             []byte{0xDE, 0xAD, 0xBE, 0xEF},
+                       "timemicro":         arrow.Time64(43200000000),
+                       "uuid":              
uuid.MustParse("123e4567-e89b-12d3-a456-426614174000"),
+                       "event_day":         
arrow.Date32FromTime(time.Date(2024, 10, 24, 0, 0, 0, 0, time.UTC)),
                        "location": map[string]any{
                                "latitude":  37.7749,
                                "longitude": -122.4194,
@@ -1290,11 +1339,11 @@ func TestManyTypesShredded(t *testing.T) {
                "strval": "click",
                "bool": true,
                "int8": 42,
-               "uint8": 255,
+               "int16_from_uint8": 255,
                "int16": 12345,
-               "uint16": 54321,
+               "int32_from_uint16": 54321,
                "int32": 1234567890,
-               "uint32": 1234567890,
+               "int64_from_uint32": 1234567890,
                "bytes": "3q2+7w==",
                "timemicro": 
"`+time.UnixMicro(43200000000).In(time.Local).Format("15:04:05.999999Z0700")+`",
                "uuid": "123e4567-e89b-12d3-a456-426614174000",
diff --git a/parquet/pqarrow/schema_test.go b/parquet/pqarrow/schema_test.go
index 1144d73c..7e308d70 100644
--- a/parquet/pqarrow/schema_test.go
+++ b/parquet/pqarrow/schema_test.go
@@ -1231,6 +1231,22 @@ func TestConvertSchemaParquetVariant(t *testing.T) {
        assert.True(t, pqschema.Equals(sc), pqschema.String(), sc.String())
 }
 
+func TestConvertSchemaRejectsUnsignedVariantTypedValue(t *testing.T) {
+       metadata := schema.NewByteArrayNode("metadata", 
parquet.Repetitions.Required, -1)
+       value := schema.NewByteArrayNode("value", parquet.Repetitions.Optional, 
-1)
+       typed := 
schema.MustPrimitive(schema.NewPrimitiveNodeLogical("typed_value", 
parquet.Repetitions.Optional,
+               schema.NewIntLogicalType(32, false), parquet.Types.Int32, -1, 
-1))
+       variant, err := schema.NewGroupNodeLogical("variant_col", 
parquet.Repetitions.Optional,
+               schema.FieldList{metadata, value, typed}, 
schema.VariantLogicalType{}, -1)
+       require.NoError(t, err)
+
+       pqschema := 
schema.NewSchema(schema.MustGroup(schema.NewGroupNode("schema", 
parquet.Repetitions.Required,
+               schema.FieldList{variant}, -1)))
+       _, err = pqarrow.FromParquet(pqschema, nil, nil)
+       require.Error(t, err)
+       require.ErrorContains(t, err, "unsigned typed_value uint32 is not a 
valid shredded variant type")
+}
+
 func TestToParquetLegacyRegistryVariantType(t *testing.T) {
        ext := arrow.GetExtensionType(extensions.LegacyVariantExtensionName)
        require.NotNil(t, ext)
diff --git a/parquet/pqarrow/variant_test.go b/parquet/pqarrow/variant_test.go
index 078a9dc4..b519d62c 100644
--- a/parquet/pqarrow/variant_test.go
+++ b/parquet/pqarrow/variant_test.go
@@ -301,7 +301,16 @@ func (s *ShreddedVariantTestSuite) TestErrorCases() {
                        s.Run(fmt.Sprint(c.Number), func() {
                                switch c.Number {
                                case 127:
-                                       s.T().Skip("Skipping case 127: test 
says uint32 should error, we just upcast to int64")
+                                       // INTEGER(32,false) is not a shredded 
variant type. Reading
+                                       // fails while building the extension 
type, before any value
+                                       // is reconstructed.
+                                       file, err := 
os.Open(filepath.Join(s.dirPrefix, c.ParquetFile))
+                                       s.Require().NoError(err)
+                                       defer file.Close()
+                                       _, err = 
pqarrow.ReadTable(context.Background(), file, nil, 
pqarrow.ArrowReadProperties{}, memory.DefaultAllocator)
+                                       s.Require().Error(err)
+                                       s.ErrorContains(err, "unsigned 
typed_value")
+                                       return
                                case 137:
                                        s.T().Skip("Skipping case 137: test 
says flba(4) should error, we just treat it as a binary variant")
                                }

Reply via email to