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

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


The following commit(s) were added to refs/heads/main by this push:
     new 8b3c96972 fix(table): report a promoted identity partition once in the 
partitions table and partition statistics (#2637)
8b3c96972 is described below

commit 8b3c96972ac8f891fea1652a9d5ff046a27b68aa
Author: Andrei Tserakhau <[email protected]>
AuthorDate: Fri Nov 20 21:27:55 2026 +0100

    fix(table): report a promoted identity partition once in the partitions 
table and partition statistics (#2637)
    
    After an identity partition source is promoted from int to long (or float
    to double), manifests written before the promotion decode the partition
    value as int32 (float32) and newer ones as int64 (float64). The partition
    aggregate tree keys those apart, so Inspect().Partitions() and partition
    statistics returned two rows for the same partition.
    
    CoercePartition and PartitionAggregateTree.LookupPartition now widen
    int32 to int64 when the partition field is long and float32 to float64
    when it is double. Both widenings are exact, so values that differ still
    land in separate rows. ComparablePartitionKey is unchanged.
---
 table/inspect/inspect_test.go                   | 81 +++++++++++++++++++++++++
 table/internal/partition_aggregate.go           | 27 +++++++--
 table/internal/partition_aggregate_test.go      | 42 +++++++++++++
 table/internal/partition_struct_builder.go      |  1 +
 table/internal/partition_struct_builder_test.go | 10 +++
 table/partition_statistics_write_test.go        | 36 +++++++++++
 6 files changed, 193 insertions(+), 4 deletions(-)

diff --git a/table/inspect/inspect_test.go b/table/inspect/inspect_test.go
index bbc22e842..575cc08db 100644
--- a/table/inspect/inspect_test.go
+++ b/table/inspect/inspect_test.go
@@ -2638,6 +2638,87 @@ func TestInspectPartitionsAggregatesDataAndDeletes(t 
*testing.T) {
        require.EqualValues(t, snapshotID, 
record.Column(10).(*array.Int64).Value(0))
 }
 
+func TestInspectPartitionsPromotedIdentitySource(t *testing.T) {
+       int64Value := func(a arrow.Array) any { return 
a.(*array.Int64).Value(0) }
+       float64Value := func(a arrow.Array) any { return 
a.(*array.Float64).Value(0) }
+       tests := []struct {
+               name           string
+               oldType        iceberg.Type
+               newType        iceberg.Type
+               oldValue       any
+               newValue       any
+               partitionValue func(arrow.Array) any
+               files          int32
+               records        int64
+       }{
+               {
+                       name: "int to long, old manifest only", oldType: 
iceberg.PrimitiveTypes.Int32,
+                       newType: iceberg.PrimitiveTypes.Int64, oldValue: 
int32(5),
+                       partitionValue: int64Value, files: 2, records: 8,
+               },
+               {
+                       name: "int to long, old and new manifests", oldType: 
iceberg.PrimitiveTypes.Int32,
+                       newType: iceberg.PrimitiveTypes.Int64, oldValue: 
int32(5), newValue: int64(5),
+                       partitionValue: int64Value, files: 3, records: 11,
+               },
+               {
+                       name: "float to double, old and new manifests", 
oldType: iceberg.PrimitiveTypes.Float32,
+                       newType: iceberg.PrimitiveTypes.Float64, oldValue: 
float32(1.5), newValue: float64(1.5),
+                       partitionValue: float64Value, files: 3, records: 11,
+               },
+       }
+
+       for i, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       const snapshotID = int64(1)
+                       spec := partitionedSpec()
+                       oldSchema := iceberg.NewSchema(0, 
iceberg.NestedField{ID: 1, Name: "id", Type: tt.oldType, Required: true})
+                       newSchema := iceberg.NewSchema(1, 
iceberg.NestedField{ID: 1, Name: "id", Type: tt.newType, Required: true})
+                       fs, err := iceio.LoadFS(t.Context(), nil, 
"mem://default/table-location")
+                       require.NoError(t, err)
+                       memIO := fs.(iceio.WriteFileIO)
+                       prefix := "mem://default/table-location/promoted-" + 
strconv.Itoa(i) + "/"
+                       sequenceNumber := int64(1)
+                       entry := func(name string, value any, count int64) 
iceberg.ManifestEntry {
+                               df := newTestDataFileWithCount(t, spec, 
prefix+name+".parquet", map[int]any{1000: value}, count)
+
+                               return 
iceberg.NewManifestEntry(iceberg.EntryStatusADDED, new(snapshotID), 
&sequenceNumber, &sequenceNumber, df)
+                       }
+                       // Manifests written before the promotion decode the 
partition value
+                       // as the old type, later ones as the new type.
+                       manifests := 
[]iceberg.ManifestFile{writeInspectManifest(t, memIO, prefix+"old.avro", spec, 
oldSchema,
+                               snapshotID, iceberg.ManifestContentData,
+                               []iceberg.ManifestEntry{entry("old-a", 
tt.oldValue, 3), entry("old-b", tt.oldValue, 5)})}
+                       if tt.newValue != nil {
+                               manifests = append(manifests, 
writeInspectManifest(t, memIO, prefix+"new.avro", spec, newSchema,
+                                       snapshotID, 
iceberg.ManifestContentData, []iceberg.ManifestEntry{entry("new", tt.newValue, 
3)}))
+                       }
+                       tbl := inspectTableWithManifestListAndSchemas(t, 
oldSchema, newSchema, spec, 2, manifests)
+
+                       rr, err := Partitions(t.Context(), tbl)
+                       require.NoError(t, err)
+                       defer rr.Release()
+                       record := collectRecord(t, rr)
+                       defer record.Release()
+                       require.EqualValues(t, 1, record.NumRows())
+                       partition := record.Column(0).(*array.Struct)
+                       require.EqualValues(t, tt.oldValue, 
tt.partitionValue(partition.Field(0)))
+                       require.Equal(t, tt.records, 
record.Column(2).(*array.Int64).Value(0))
+                       require.Equal(t, tt.files, 
record.Column(3).(*array.Int32).Value(0))
+
+                       files, err := Files(t.Context(), tbl)
+                       require.NoError(t, err)
+                       defer files.Release()
+                       var fileRows int64
+                       for files.Next() {
+                               fileRows += files.RecordBatch().NumRows()
+                       }
+                       require.NoError(t, files.Err())
+                       require.EqualValues(t, tt.files, fileRows)
+               })
+       }
+}
+
 func TestInspectPartitionsLeavesSpecIDUnsetForExpiredSnapshot(t *testing.T) {
        const (
                expiredSnapshotID = int64(1)
diff --git a/table/internal/partition_aggregate.go 
b/table/internal/partition_aggregate.go
index c90698f28..cb0384ce3 100644
--- a/table/internal/partition_aggregate.go
+++ b/table/internal/partition_aggregate.go
@@ -106,7 +106,8 @@ func (t *PartitionAggregateTree) LookupPartition(
 ) *PartitionAggregate {
        node := t
        for i := range partitionType.NumFields() {
-               child, ok := node.child(partition[partitionType.Field(i).ID])
+               field := partitionType.Field(i)
+               child, ok := 
node.child(widenPartitionValue(partition[field.ID], field.Type))
                if !ok {
                        return nil
                }
@@ -139,15 +140,33 @@ func CoercePartition(values map[int]any, partitionType 
*iceberg.StructType) map[
 
        coerced := make(map[int]any, partitionType.NumFields())
        for i := range partitionType.NumFields() {
-               id := partitionType.Field(i).ID
-               if value, ok := values[id]; ok {
-                       coerced[id] = value
+               field := partitionType.Field(i)
+               if value, ok := values[field.ID]; ok {
+                       coerced[field.ID] = widenPartitionValue(value, 
field.Type)
                }
        }
 
        return coerced
 }
 
+// widenPartitionValue applies the exact int to long and float to double
+// promotions, so values from manifests written before an identity source was
+// promoted aggregate with values written after it.
+func widenPartitionValue(value any, typ iceberg.Type) any {
+       switch v := value.(type) {
+       case int32:
+               if _, ok := typ.(iceberg.Int64Type); ok {
+                       return int64(v)
+               }
+       case float32:
+               if _, ok := typ.(iceberg.Float64Type); ok {
+                       return float64(v)
+               }
+       }
+
+       return value
+}
+
 func ClonePartition(partition map[int]any) map[int]any {
        if partition == nil {
                return nil
diff --git a/table/internal/partition_aggregate_test.go 
b/table/internal/partition_aggregate_test.go
index 8b1d8f060..1e9f40b54 100644
--- a/table/internal/partition_aggregate_test.go
+++ b/table/internal/partition_aggregate_test.go
@@ -161,6 +161,48 @@ func TestPartitionAggregateTreeLookupPartition(t 
*testing.T) {
        require.Same(t, evolvedAggregate, tree.Lookup(evolvedRecord))
 }
 
+func TestPartitionAggregateTreeWidensPromotedValues(t *testing.T) {
+       tests := []struct {
+               name     string
+               typ      iceberg.Type
+               inserted any
+               wantType any
+               lookup   any
+               found    bool
+       }{
+               {"int found by long", iceberg.PrimitiveTypes.Int64, int32(5), 
int64(0), int64(5), true},
+               {"long found by int", iceberg.PrimitiveTypes.Int64, int64(5), 
int64(0), int32(5), true},
+               {"other long", iceberg.PrimitiveTypes.Int64, int32(5), 
int64(0), int64(6), false},
+               {"float found by double", iceberg.PrimitiveTypes.Float64, 
float32(1.5), float64(0), float64(1.5), true},
+               {"double found by float", iceberg.PrimitiveTypes.Float64, 
float64(1.5), float64(0), float32(1.5), true},
+               {"float NaN found by double NaN", 
iceberg.PrimitiveTypes.Float64, float32(math.NaN()), float64(0), math.NaN(), 
true},
+               // float32(0.1) widens exactly, so it stays distinct from 
float64(0.1).
+               {"float differs after widening", 
iceberg.PrimitiveTypes.Float64, float32(0.1), float64(0), float64(0.1), false},
+               {"int field keeps int", iceberg.PrimitiveTypes.Int32, int32(5), 
int32(0), int32(5), true},
+               {"int field does not widen", iceberg.PrimitiveTypes.Int32, 
int32(5), int32(0), int64(5), false},
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       partitionType := iceberg.NewStructType(
+                               iceberg.NestedField{ID: 1000, Name: "p", Type: 
tt.typ},
+                       )
+                       coerced := CoercePartition(map[int]any{1000: 
tt.inserted}, partitionType)
+                       require.IsType(t, tt.wantType, coerced[1000])
+
+                       aggregate := &PartitionAggregate{SpecID: 1}
+                       tree := NewPartitionAggregateTree()
+                       tree.Insert(NewPartitionRecord(coerced, partitionType), 
aggregate)
+
+                       got := tree.LookupPartition(map[int]any{1000: 
tt.lookup}, partitionType)
+                       if tt.found {
+                               require.Same(t, aggregate, got)
+                       } else {
+                               require.Nil(t, got)
+                       }
+               })
+       }
+}
+
 func TestClonePartitionClonesBinaryValues(t *testing.T) {
        value := []byte{1, 2, 3}
        cloned := ClonePartition(map[int]any{1000: value})
diff --git a/table/internal/partition_struct_builder.go 
b/table/internal/partition_struct_builder.go
index 79e89915e..2a46731aa 100644
--- a/table/internal/partition_struct_builder.go
+++ b/table/internal/partition_struct_builder.go
@@ -166,6 +166,7 @@ func newTypedPartitionAppender[T any](
 }
 
 func PartitionValueScalar(value any, typ iceberg.Type, arrowType 
arrow.DataType) (scalar.Scalar, error) {
+       value = widenPartitionValue(value, typ)
        switch typ.(type) {
        case iceberg.DateType:
                switch value := value.(type) {
diff --git a/table/internal/partition_struct_builder_test.go 
b/table/internal/partition_struct_builder_test.go
index 1b83daf6c..bd33ae135 100644
--- a/table/internal/partition_struct_builder_test.go
+++ b/table/internal/partition_struct_builder_test.go
@@ -271,6 +271,16 @@ func TestPartitionValueScalarDecimal(t *testing.T) {
        require.Equal(t, value.Val, got.(*scalar.Decimal128).Value)
 }
 
+func TestPartitionValueScalarWidensPromotedValues(t *testing.T) {
+       got, err := internal.PartitionValueScalar(int32(5), 
iceberg.PrimitiveTypes.Int64, arrow.PrimitiveTypes.Int64)
+       require.NoError(t, err)
+       require.Equal(t, int64(5), got.(*scalar.Int64).Value)
+
+       got, err = internal.PartitionValueScalar(float32(1.5), 
iceberg.PrimitiveTypes.Float64, arrow.PrimitiveTypes.Float64)
+       require.NoError(t, err)
+       require.Equal(t, float64(1.5), got.(*scalar.Float64).Value)
+}
+
 func TestPartitionValueScalarRejectsMismatchedPartitionValues(t *testing.T) {
        tests := []struct {
                name      string
diff --git a/table/partition_statistics_write_test.go 
b/table/partition_statistics_write_test.go
index fbceb8dc6..2610b9bb9 100644
--- a/table/partition_statistics_write_test.go
+++ b/table/partition_statistics_write_test.go
@@ -320,6 +320,42 @@ func TestCollectPartitionStatisticsDecimalFromManifest(t 
*testing.T) {
        require.Equal(t, amount.Val, 
partition.Field(0).(*array.Decimal128).Value(0))
 }
 
+func TestCollectPartitionStatisticsPromotedIdentitySource(t *testing.T) {
+       spec := iceberg.NewPartitionSpec(iceberg.PartitionField{
+               SourceIDs: []int{1}, FieldID: 1000, Name: "p", Transform: 
iceberg.IdentityTransform{},
+       })
+       partitionType := iceberg.NewStructType(
+               iceberg.NestedField{ID: 1000, Name: "p", Type: 
iceberg.PrimitiveTypes.Int64},
+       )
+       const snapshotID = int64(7)
+       fs := iceio.NewMemFS()
+       // Manifests written before the int to long promotion decode p as int32,
+       // later ones as int64. Both must count toward the same partition.
+       writeManifest := func(name string, sourceType iceberg.Type, value any, 
count int64) string {
+               schema := iceberg.NewSchema(0, iceberg.NestedField{ID: 1, Name: 
"p", Type: sourceType, Required: true})
+               df := newTestDataFileWithCount(t, spec, 
"mem://stats/"+name+".parquet", map[int]any{1000: value}, count)
+               path := "mem://stats/" + name + "-manifest.avro"
+               var encoded bytes.Buffer
+               _, err := iceberg.WriteManifest(path, &encoded, 1, spec, 
schema, snapshotID,
+                       
[]iceberg.ManifestEntry{iceberg.NewManifestEntry(iceberg.EntryStatusADDED, 
new(snapshotID), nil, nil, df)})
+               require.NoError(t, err)
+               require.NoError(t, fs.WriteFile(t.Context(), path, 
encoded.Bytes()))
+
+               return path
+       }
+       snapshot := Snapshot{SnapshotID: snapshotID, ManifestLocations: 
[]string{
+               writeManifest("before", iceberg.PrimitiveTypes.Int32, int32(5), 
4),
+               writeManifest("after", iceberg.PrimitiveTypes.Int64, int64(5), 
6),
+       }}
+
+       rows, err := collectPartitionStatistics(t.Context(), fs, snapshot, 
partitionType, []iceberg.PartitionSpec{spec}, 1, nil)
+       require.NoError(t, err)
+       require.Len(t, rows, 1)
+       require.Equal(t, int64(5), rows[0].Partition[1000])
+       require.Equal(t, int64(10), rows[0].DataRecordCount)
+       require.Equal(t, int32(2), rows[0].DataFileCount)
+}
+
 func TestTransactionPartitionStatisticsRegistration(t *testing.T) {
        tbl := inspectTestTable(Identifier{"db", "stats"}, 
map[string]SnapshotRef{
                MainBranch: {SnapshotID: 103, Type: BranchRef},

Reply via email to