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 e8061a26a fix(table): keep empty string and binary bounds (#2709)
e8061a26a is described below
commit e8061a26a8fc0ce7585ab92e3cd8c2a1a0bc9a7b
Author: riverandeye <[email protected]>
AuthorDate: Sat Nov 28 06:48:52 2026 +0900
fix(table): keep empty string and binary bounds (#2709)
An empty string or binary value serializes to zero bytes, and
ToDataFile only recorded bounds with len > 0, so these bounds were
dropped. MaxAsBytes also returned no upper bound when the max was "",
treating it like a prefix that could not be incremented. A file whose
string column holds only "" got neither bound and could not be skipped
by scans, while Java writes empty bounds for the same rows.
Record a bound whenever MinAsBytes/MaxAsBytes return a non-nil slice,
keep empty values non-nil in toBytes, and return no upper bound only
when truncation found no greater prefix.
Fixes #2708
---
table/empty_string_partition_scan_test.go | 54 +++++++++++++++
table/evaluators.go | 4 +-
table/internal/parquet_files_test.go | 105 ++++++++++++++++++++++++++++++
table/internal/utils.go | 34 +++++++---
4 files changed, 185 insertions(+), 12 deletions(-)
diff --git a/table/empty_string_partition_scan_test.go
b/table/empty_string_partition_scan_test.go
index 73b164b5a..ed6c9b8ba 100644
--- a/table/empty_string_partition_scan_test.go
+++ b/table/empty_string_partition_scan_test.go
@@ -83,3 +83,57 @@ func TestEmptyStringIdentityPartitionScans(t *testing.T) {
})
}
}
+
+// A file whose string column holds only "" must keep "" as both bounds, so
+// scans can skip it for any other value.
+func TestEmptyStringBoundsPruneScans(t *testing.T) {
+ ctx := context.Background()
+ cat, err := hadoop.NewCatalog(ctx, "local", t.TempDir(), nil)
+ require.NoError(t, err)
+ require.NoError(t, cat.CreateNamespace(ctx, catalog.ToIdentifier("db"),
nil))
+
+ schema := iceberg.NewSchema(0,
+ iceberg.NestedField{ID: 1, Name: "id", Type:
iceberg.PrimitiveTypes.Int64, Required: true},
+ iceberg.NestedField{ID: 2, Name: "s", Type:
iceberg.PrimitiveTypes.String},
+ )
+ tbl, err := cat.CreateTable(ctx, catalog.ToIdentifier("db", "events"),
schema)
+ require.NoError(t, err)
+
+ arrowSchema, err := table.SchemaToArrowSchema(schema, nil, false, false)
+ require.NoError(t, err)
+ builder := array.NewRecordBuilder(memory.DefaultAllocator, arrowSchema)
+ defer builder.Release()
+ builder.Field(0).(*array.Int64Builder).AppendValues([]int64{1, 2}, nil)
+ builder.Field(1).(*array.StringBuilder).AppendValues([]string{"", ""},
nil)
+
+ record := builder.NewRecordBatch()
+ defer record.Release()
+ reader, err := array.NewRecordReader(arrowSchema,
[]arrow.RecordBatch{record})
+ require.NoError(t, err)
+ defer reader.Release()
+ tbl, err = tbl.Append(ctx, reader, nil)
+ require.NoError(t, err)
+
+ tasks, err := tbl.Scan().PlanFiles(ctx)
+ require.NoError(t, err)
+ require.Len(t, tasks, 1)
+ require.Equal(t, []byte{}, tasks[0].File.LowerBoundValues()[2])
+ require.Equal(t, []byte{}, tasks[0].File.UpperBoundValues()[2])
+
+ for _, tc := range []struct {
+ name string
+ expr iceberg.BooleanExpression
+ files int
+ }{
+ {name: "equal to empty", expr:
iceberg.EqualTo(iceberg.Reference("s"), ""), files: 1},
+ {name: "equal to other", expr:
iceberg.EqualTo(iceberg.Reference("s"), "zz"), files: 0},
+ {name: "greater than", expr:
iceberg.GreaterThan(iceberg.Reference("s"), "a"), files: 0},
+ {name: "starts with", expr:
iceberg.StartsWith(iceberg.Reference("s"), "a"), files: 0},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ tasks, err :=
tbl.Scan(table.WithRowFilter(tc.expr)).PlanFiles(ctx)
+ require.NoError(t, err)
+ require.Len(t, tasks, tc.files)
+ })
+ }
+}
diff --git a/table/evaluators.go b/table/evaluators.go
index b6bf4707b..495ab3ede 100644
--- a/table/evaluators.go
+++ b/table/evaluators.go
@@ -1489,7 +1489,7 @@ func (m *inclusiveMetricsEval) VisitStartsWith(t
iceberg.BoundTerm, lit iceberg.
v = v[:lenPrefix]
}
- if len(v) > 0 && v > prefix {
+ if v > prefix {
return rowsCannotMatch
}
}
@@ -1507,7 +1507,7 @@ func (m *inclusiveMetricsEval) VisitStartsWith(t
iceberg.BoundTerm, lit iceberg.
v = v[:lenPrefix]
}
- if len(v) > 0 && v < prefix {
+ if v < prefix {
return rowsCannotMatch
}
}
diff --git a/table/internal/parquet_files_test.go
b/table/internal/parquet_files_test.go
index 5f545b840..ced21045c 100644
--- a/table/internal/parquet_files_test.go
+++ b/table/internal/parquet_files_test.go
@@ -636,6 +636,111 @@ func TestMetricsPrimitiveTypes(t *testing.T) {
})
}
+// An empty string or binary value serializes to zero bytes. It is still a
+// bound and must be written, as Iceberg Java does.
+func TestMetricsEmptyStringAndBinaryBounds(t *testing.T) {
+ const maxRune = "\U0010FFFF"
+
+ truncate16 := internal.MetricsMode{Typ: internal.MetricModeTruncate,
Len: 16}
+
+ tests := []struct {
+ name string
+ mode internal.MetricsMode
+ // rows is a JSON array of {"s": ..., "b": ...} objects; b is
base64.
+ rows string
+ // Field 1 is s (string), field 2 is b (binary). A missing key
means no bound.
+ lower, upper map[int][]byte
+ }{
+ {
+ name: "all empty",
+ mode: truncate16,
+ rows: `[{"s": "", "b": ""}, {"s": "", "b": ""}]`,
+ lower: map[int][]byte{1: {}, 2: {}},
+ upper: map[int][]byte{1: {}, 2: {}},
+ },
+ {
+ name: "empty minimum",
+ mode: truncate16,
+ rows: `[{"s": "", "b": ""}, {"s": "z", "b": "eg=="}]`,
+ lower: map[int][]byte{1: {}, 2: {}},
+ upper: map[int][]byte{1: []byte("z"), 2: []byte("z")},
+ },
+ {
+ name: "all empty, full mode",
+ mode: internal.MetricsMode{Typ:
internal.MetricModeFull},
+ rows: `[{"s": "", "b": ""}]`,
+ lower: map[int][]byte{1: {}, 2: {}},
+ upper: map[int][]byte{1: {}, 2: {}},
+ },
+ {
+ // A truncated maximum with no greater prefix still has
no upper bound.
+ name: "no upper bound after truncation",
+ mode: internal.MetricsMode{Typ:
internal.MetricModeTruncate, Len: 1},
+ rows: `[{"s": "", "b": ""}, {"s": "` + maxRune +
maxRune + `", "b": "//8="}]`,
+ lower: map[int][]byte{1: {}, 2: {}},
+ upper: nil,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ df := emptyBoundsDataFile(t, tt.mode, tt.rows)
+
+ assert.Equal(t, tt.lower, df.LowerBoundValues())
+ if tt.upper == nil {
+ assert.Empty(t, df.UpperBoundValues())
+ } else {
+ assert.Equal(t, tt.upper, df.UpperBoundValues())
+ }
+ })
+ }
+}
+
+func emptyBoundsDataFile(t *testing.T, mode internal.MetricsMode, rows string)
iceberg.DataFile {
+ t.Helper()
+
+ sc := iceberg.NewSchema(0,
+ iceberg.NestedField{ID: 1, Name: "s", Type:
iceberg.PrimitiveTypes.String},
+ iceberg.NestedField{ID: 2, Name: "b", Type:
iceberg.PrimitiveTypes.Binary},
+ )
+ arrowSchema, err := table.SchemaToArrowSchema(sc, nil, true, false)
+ require.NoError(t, err)
+
+ rec, _, err := array.RecordFromJSON(memory.DefaultAllocator,
arrowSchema, strings.NewReader(rows))
+ require.NoError(t, err)
+ defer rec.Release()
+
+ var buf bytes.Buffer
+ wr, err := pqarrow.NewFileWriter(arrowSchema, &buf,
+ parquet.NewWriterProperties(parquet.WithStats(true)),
pqarrow.DefaultWriterProps())
+ require.NoError(t, err)
+ require.NoError(t, wr.Write(rec))
+ require.NoError(t, wr.Close())
+
+ rdr, err := file.NewParquetReader(bytes.NewReader(buf.Bytes()))
+ require.NoError(t, err)
+ defer rdr.Close()
+
+ format := internal.GetFileFormat(iceberg.ParquetFile)
+ mapping, err := format.PathToIDMapping(sc)
+ require.NoError(t, err)
+
+ collector := map[int]internal.StatisticsCollector{
+ 1: {FieldID: 1, Mode: mode, ColName: "s", IcebergTyp:
iceberg.PrimitiveTypes.String},
+ 2: {FieldID: 2, Mode: mode, ColName: "b", IcebergTyp:
iceberg.PrimitiveTypes.Binary},
+ }
+ stats :=
format.DataFileStatsFromMeta(internal.Metadata(rdr.MetaData()), collector,
mapping, nil, nil)
+
+ return stats.ToDataFile(internal.DataFileOpts{
+ Schema: sc,
+ Spec: *iceberg.UnpartitionedSpec,
+ Path: "empty-bounds.parquet",
+ Format: iceberg.ParquetFile,
+ Content: iceberg.EntryContentData,
+ FileSize: int64(buf.Len()),
+ })
+}
+
func TestDataFileStatsFromMetaWithMalformedUUIDStats(t *testing.T) {
format := internal.GetFileFormat(iceberg.ParquetFile)
diff --git a/table/internal/utils.go b/table/internal/utils.go
index d7d9c0bc5..da214f321 100644
--- a/table/internal/utils.go
+++ b/table/internal/utils.go
@@ -361,12 +361,14 @@ func (d *DataFileStatistics) ToDataFile(opts
DataFileOpts) iceberg.DataFile {
upperBounds := make(map[int][]byte)
for fieldID, agg := range d.ColAggs {
+ // An empty string or binary bound serializes to zero bytes, so
only
+ // nil means there is no bound.
min := must(agg.MinAsBytes())
max := must(agg.MaxAsBytes())
- if len(min) > 0 {
+ if min != nil {
lowerBounds[fieldID] = min
}
- if len(max) > 0 {
+ if max != nil {
upperBounds[fieldID] = max
}
}
@@ -502,7 +504,15 @@ func (s *statsAggregator[T]) toBytes(val iceberg.Literal)
([]byte, error) {
return nil, err
}
- return v.MarshalBinary()
+ b, err := v.MarshalBinary()
+ if err != nil {
+ return nil, err
+ }
+ if b == nil {
+ b = []byte{}
+ }
+
+ return b, nil
}
func (s *statsAggregator[T]) updateMin(val T) {
@@ -554,24 +564,28 @@ func (s *statsAggregator[T]) MaxAsBytes() ([]byte, error)
{
}
curMax := any(s.curMax.Value()).(string)
- result := TruncateUpperBoundText(curMax, s.truncLen)
- if result != "" {
- return s.toBytes(iceberg.StringLiteral(result))
+ result, truncated := truncateUpperBoundText(curMax, s.truncLen)
+ if truncated && result == "" {
+ return nil, nil
}
- return nil, nil
+ return s.toBytes(iceberg.StringLiteral(result))
case iceberg.BinaryType:
if !s.curMax.Type().Equals(s.primitiveType) {
return nil, errors.New("expected current max to be a
binary")
}
curMax := any(s.curMax.Value()).([]byte)
+ if len(curMax) <= s.truncLen {
+ return s.toBytes(iceberg.BinaryLiteral(curMax))
+ }
+
result := TruncateUpperBoundBinary(curMax, s.truncLen)
- if len(result) > 0 {
- return s.toBytes(iceberg.BinaryLiteral(result))
+ if result == nil {
+ return nil, nil
}
- return nil, nil
+ return s.toBytes(iceberg.BinaryLiteral(result))
default:
return nil, fmt.Errorf("%s cannot be truncated for upper
bound", s.primitiveType)
}