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 3d1d6769b fix(table): compare promoted partition values after widening 
(#2620)
3d1d6769b is described below

commit 3d1d6769bff89d2f6277596e3124f3790001d357
Author: Andrei Tserakhau <[email protected]>
AuthorDate: Wed Nov 18 14:35:48 2026 +0100

    fix(table): compare promoted partition values after widening (#2620)
    
    After an identity partition source is promoted (int to long, float to
    double), manifests written before the promotion still decode its
    partition values as int32/float32, while newer manifests decode them as
    int64/float64. Partition comparisons treated those equal values as
    different. A v2 merge-on-read Delete on an old data file therefore
    committed a position delete that scan planning then rejected with
    "partition does not match" (#2605), and equality deletes in a promoted
    float partition skipped the old data files, so deleted rows came back
    (#2610).
    
    partitionsMatch now widens int32 to int64 and float32 to float64 before
    comparing. This covers the position delete check and
    matchEqualityDeletesToData. The equality delete index already widened
    integers in its single-field key; it now widens float32 to float64 too.
    Both widenings are exact, NaNs still compare equal and -0.0 and +0.0
    still differ. No other type pairs are widened, and
    comparablePartitionKey, which the writers use for partition keys, is
    unchanged.
    
    The longer-term fix is to decode old manifests with the current
    partition type, as Java does; that stays a follow-up.
---
 table/equality_delete_index.go            |   9 +-
 table/partition_promotion_deletes_test.go | 135 ++++++++++++++++++++++++++++++
 table/scanner.go                          |  20 ++++-
 table/scanner_internal_test.go            |  27 +++++-
 4 files changed, 183 insertions(+), 8 deletions(-)

diff --git a/table/equality_delete_index.go b/table/equality_delete_index.go
index ba7db20ce..f671fc1e2 100644
--- a/table/equality_delete_index.go
+++ b/table/equality_delete_index.go
@@ -343,9 +343,12 @@ func comparableEqualityDeletePartitionValue(value any) 
(any, error) {
                return equalityDeleteIntegerPartitionValue(value), nil
        case iceberg.TimestampNano:
                return equalityDeleteIntegerPartitionValue(value), nil
-       case float32, float64:
-               // Keep equality-delete indexing aligned with writer partition 
keys:
-               // canonicalize NaNs within each width and preserve signed zero.
+       case float32:
+               // Widen like integers so values decoded from manifests written 
before
+               // a float to double promotion share a key with newer ones.
+               return tblutils.ComparablePartitionKey(float64(value)), nil
+       case float64:
+               // Canonicalize NaNs and preserve signed zero.
                return tblutils.ComparablePartitionKey(value), nil
        case string:
                return equalityDeleteStringPartitionValue(value), nil
diff --git a/table/partition_promotion_deletes_test.go 
b/table/partition_promotion_deletes_test.go
new file mode 100644
index 000000000..16d4fa54a
--- /dev/null
+++ b/table/partition_promotion_deletes_test.go
@@ -0,0 +1,135 @@
+// 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.
+
+package table_test
+
+import (
+       "context"
+       "path/filepath"
+       "testing"
+
+       "github.com/apache/iceberg-go"
+       iceio "github.com/apache/iceberg-go/io"
+       "github.com/apache/iceberg-go/table"
+       "github.com/stretchr/testify/assert"
+       "github.com/stretchr/testify/require"
+)
+
+// newPromotablePartitionTable builds a table identity-partitioned on a
+// promotable column "p", so manifests written before and after promoting p
+// decode its partition values with different Go widths.
+func newPromotablePartitionTable(t *testing.T, formatVersion string, partType 
iceberg.Type) *table.Table {
+       t.Helper()
+
+       location := filepath.ToSlash(t.TempDir())
+       schema := iceberg.NewSchema(0,
+               iceberg.NestedField{ID: 1, Name: "id", Type: 
iceberg.PrimitiveTypes.Int64, Required: true},
+               iceberg.NestedField{ID: 2, Name: "p", Type: partType, Required: 
false},
+       )
+       spec := iceberg.NewPartitionSpec(
+               iceberg.PartitionField{SourceIDs: []int{2}, FieldID: 1000, 
Name: "p", Transform: iceberg.IdentityTransform{}},
+       )
+       meta, err := table.NewMetadata(schema, &spec, table.UnsortedSortOrder, 
location,
+               iceberg.Properties{
+                       table.PropertyFormatVersion: formatVersion,
+                       table.WriteDeleteModeKey:    table.WriteModeMergeOnRead,
+               })
+       require.NoError(t, err)
+
+       metaLoc := location + "/metadata/v1.metadata.json"
+       fsF := func(context.Context) (iceio.IO, error) { return 
iceio.LocalFS{}, nil }
+       cat := &concurrentTestCatalog{metadata: meta, location: metaLoc, fsF: 
fsF}
+
+       return table.New(table.Identifier{"db", "partition_promotion"}, meta, 
metaLoc, fsF, cat)
+}
+
+func promotePartitionSource(t *testing.T, tbl *table.Table, target 
iceberg.Type) *table.Table {
+       t.Helper()
+
+       tx := tbl.NewTransaction()
+       require.NoError(t, tx.UpdateSchema(true, false).
+               UpdateColumn([]string{"p"}, table.ColumnUpdate{
+                       FieldType: iceberg.Optional[iceberg.Type]{Valid: true, 
Val: target},
+               }).Commit())
+       tbl, err := tx.Commit(t.Context())
+       require.NoError(t, err)
+
+       return tbl
+}
+
+// TestMergeOnReadDeleteAfterPartitionSourcePromotion covers #2605: a v2 MoR
+// delete on a file written before the promotion records the partition with
+// the new width, and the scan must still pair it with the old data file.
+func TestMergeOnReadDeleteAfterPartitionSourcePromotion(t *testing.T) {
+       tests := []struct {
+               name     string
+               from, to iceberg.Type
+               rows     string
+       }{
+               {
+                       name: "int to long",
+                       from: iceberg.PrimitiveTypes.Int32, to: 
iceberg.PrimitiveTypes.Int64,
+                       rows: `[{"id": 1, "p": 7}, {"id": 2, "p": 7}, {"id": 3, 
"p": 8}]`,
+               },
+               {
+                       name: "float to double",
+                       from: iceberg.PrimitiveTypes.Float32, to: 
iceberg.PrimitiveTypes.Float64,
+                       rows: `[{"id": 1, "p": 1.5}, {"id": 2, "p": 1.5}, 
{"id": 3, "p": 4.5}]`,
+               },
+       }
+
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       tbl := newPromotablePartitionTable(t, "2", tt.from)
+                       tbl = appendRows(t, tbl, tt.rows)
+                       tbl = promotePartitionSource(t, tbl, tt.to)
+
+                       tbl, err := tbl.Delete(t.Context(), 
iceberg.EqualTo(iceberg.Reference("id"), int64(1)), nil)
+                       require.NoError(t, err)
+
+                       assert.Equal(t, []int64{2, 3}, idsInTable(t, tbl))
+               })
+       }
+}
+
+// TestEqualityDeletesAfterFloatPartitionSourcePromotion covers #2610: an
+// equality delete written in a promoted double partition must apply to data
+// files whose manifests still decode the partition as float. A delete whose
+// partition differs after widening must not.
+func TestEqualityDeletesAfterFloatPartitionSourcePromotion(t *testing.T) {
+       for _, formatVersion := range []string{"2", "3"} {
+               t.Run("v"+formatVersion, func(t *testing.T) {
+                       tbl := newPromotablePartitionTable(t, formatVersion, 
iceberg.PrimitiveTypes.Float32)
+                       tbl = appendRows(t, tbl, `[{"id": 1, "p": 1.5}, {"id": 
2, "p": 1.5}, {"id": 3, "p": 4.5}]`)
+                       tbl = promotePartitionSource(t, tbl, 
iceberg.PrimitiveTypes.Float64)
+
+                       arrowSc, err := table.SchemaToArrowSchema(tbl.Schema(), 
nil, true, false)
+                       require.NoError(t, err)
+                       // id 3 lives in partition 4.5, so the delete in 
partition 4.6
+                       // must not remove it.
+                       records, release := makeEqDeleteRecords(t, arrowSc, 
`[{"id": 1, "p": 1.5}, {"id": 3, "p": 4.6}]`)
+                       defer release()
+
+                       tx := tbl.NewTransaction()
+                       require.NoError(t, 
tx.CommitEqualityDeletes(t.Context(), []int{1}, records, nil))
+                       tbl, err = tx.Commit(t.Context())
+                       require.NoError(t, err)
+
+                       assert.Equal(t, []int64{2, 3}, idsInTable(t, tbl))
+               })
+       }
+}
diff --git a/table/scanner.go b/table/scanner.go
index 94cea194e..ca9538a91 100644
--- a/table/scanner.go
+++ b/table/scanner.go
@@ -1002,7 +1002,25 @@ func matchEqualityDeletesToData(dataEntry 
iceberg.ManifestEntry, eqDeleteEntries
 }
 
 func partitionsMatch(a, b map[int]any) bool {
-       return maps.EqualFunc(a, b, partitionValuesEqual)
+       return maps.EqualFunc(a, b, promotedPartitionValuesEqual)
+}
+
+// promotedPartitionValuesEqual compares partition values across an identity
+// source promotion (int to long, float to double): manifests written before
+// the promotion decode the old width. Both widenings are exact.
+func promotedPartitionValuesEqual(left, right any) bool {
+       return partitionValuesEqual(widenPromotedPartitionValue(left), 
widenPromotedPartitionValue(right))
+}
+
+func widenPromotedPartitionValue(value any) any {
+       switch value := value.(type) {
+       case int32:
+               return int64(value)
+       case float32:
+               return float64(value)
+       }
+
+       return value
 }
 
 // buildDVIndex indexes deletion vectors by the data file path they reference.
diff --git a/table/scanner_internal_test.go b/table/scanner_internal_test.go
index 811ff761b..33008d2c8 100644
--- a/table/scanner_internal_test.go
+++ b/table/scanner_internal_test.go
@@ -396,7 +396,8 @@ func TestEqualityDeletePartitionKeyNormalizesValues(t 
*testing.T) {
        }{
                {name: "integer widths", left: int32(7), right: int64(7), 
equal: true},
                {name: "date and integer", left: iceberg.Date(7), right: 
int32(7), equal: true},
-               {name: "float widths", left: float32(1.5), right: float64(1.5)},
+               {name: "float widths", left: float32(1.5), right: float64(1.5), 
equal: true},
+               {name: "float widths differ", left: float32(4.5), right: 
float64(4.6)},
                {
                        name:  "float32 NaN payloads",
                        left:  math.Float32frombits(0x7fc00001),
@@ -410,9 +411,15 @@ func TestEqualityDeletePartitionKeyNormalizesValues(t 
*testing.T) {
                        equal: true,
                },
                {
-                       name:  "float widths keep NaNs distinct",
+                       name:  "float NaN across widths",
                        left:  math.Float32frombits(0x7fc00001),
                        right: math.Float64frombits(0x7ff8000000000001),
+                       equal: true,
+               },
+               {
+                       name:  "signed zero across widths",
+                       left:  float32(math.Copysign(0, -1)),
+                       right: float64(0),
                },
                {
                        name:  "float32 signed zero",
@@ -481,9 +488,15 @@ func TestEqualityDeleteIndexUsesFloatSemantics(t 
*testing.T) {
                        match:           true,
                },
                {
-                       name:            "float widths stay distinct",
+                       name:            "float widened to double matches",
                        dataPartition:   float32(1.5),
                        deletePartition: float64(1.5),
+                       match:           true,
+               },
+               {
+                       name:            "float widened to double differs",
+                       dataPartition:   float32(4.5),
+                       deletePartition: float64(4.6),
                },
                {
                        name:            "float32 signed zero stays distinct",
@@ -738,7 +751,13 @@ func TestPartitionsMatchUsesFloatSemantics(t *testing.T) {
                {name: "float64 NaN payloads", left: 
math.Float64frombits(0x7ff8000000000001), right: 
math.Float64frombits(0x7ff8000000000002), match: true},
                {name: "float32 signed zero", left: float32(math.Copysign(0, 
-1)), right: float32(0), match: false},
                {name: "float64 signed zero", left: math.Copysign(0, -1), 
right: float64(0), match: false},
-               {name: "float widths differ", left: float32(1), right: 
float64(1), match: false},
+               {name: "float widened to double", left: float32(1), right: 
float64(1), match: true},
+               {name: "float widened to double differs", left: float32(4.5), 
right: 4.6, match: false},
+               {name: "float NaN across widths", left: float32(math.NaN()), 
right: math.NaN(), match: true},
+               {name: "signed zero across widths", left: 
float32(math.Copysign(0, -1)), right: float64(0), match: false},
+               {name: "int widened to long", left: int32(7), right: int64(7), 
match: true},
+               {name: "int widened to long differs", left: int32(7), right: 
int64(8), match: false},
+               {name: "int and double not widened", left: int32(1), right: 
float64(1), match: false},
                {name: "pass-through integer", left: int64(42), right: 
int64(42), match: true},
                {name: "pass-through strings differ", left: "a", right: "b", 
match: false},
                {name: "nil values", left: nil, right: nil, match: true},

Reply via email to