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/iceberg-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 911ada48b perf(table): skip single-value data files for != and NOT IN 
(#2728)
911ada48b is described below

commit 911ada48be09f7856f100692b042899109b2fdfe
Author: riverandeye <[email protected]>
AuthorDate: Sat Nov 28 03:11:13 2026 +0900

    perf(table): skip single-value data files for != and NOT IN (#2728)
    
    * perf(table): skip single-value data files for != and NOT IN
    
    Signed-off-by: riverandeye <[email protected]>
    
    * refactor(table): rename uniqueValue to findUniqueValue
    
    Signed-off-by: riverandeye <[email protected]>
    
    ---------
    
    Signed-off-by: riverandeye <[email protected]>
---
 table/evaluators.go                      | 60 ++++++++++++++++++++++++++--
 table/evaluators_test.go                 | 67 ++++++++++++++++++++++++++++++++
 table/scan_row_filter_regression_test.go | 52 +++++++++++++++++++++++++
 3 files changed, 176 insertions(+), 3 deletions(-)

diff --git a/table/evaluators.go b/table/evaluators.go
index c1d631740..b6bf4707b 100644
--- a/table/evaluators.go
+++ b/table/evaluators.go
@@ -1310,7 +1310,11 @@ func (m *inclusiveMetricsEval) VisitEqual(t 
iceberg.BoundTerm, lit iceberg.Liter
        return rowsMightMatch
 }
 
-func (m *inclusiveMetricsEval) VisitNotEqual(iceberg.BoundTerm, 
iceberg.Literal) bool {
+func (m *inclusiveMetricsEval) VisitNotEqual(t iceberg.BoundTerm, lit 
iceberg.Literal) bool {
+       if value := m.findUniqueValue(t); value != nil && 
getCmpLiteral(value)(value, lit) == 0 {
+               return rowsCannotMatch
+       }
+
        return rowsMightMatch
 }
 
@@ -1399,13 +1403,63 @@ func (m *inclusiveMetricsEval) visitIn(
        return rowsMightMatch
 }
 
-func (m *inclusiveMetricsEval) VisitNotIn(iceberg.BoundTerm, 
iceberg.Set[iceberg.Literal]) bool {
+func (m *inclusiveMetricsEval) VisitNotIn(t iceberg.BoundTerm, s 
iceberg.Set[iceberg.Literal]) bool {
        // because the bounds are not necessarily a min or max value, this 
cannot be
        // answered using them. notIn(col, {X, ...}) with (XX, Y) doesn't 
guarantee that
-       // X is a value in col
+       // X is a value in col, unless every row holds the same value.
+       if value := m.findUniqueValue(t); value != nil && s.Contains(value) {
+               return rowsCannotMatch
+       }
+
        return rowsMightMatch
 }
 
+// findUniqueValue returns the value every row of the file holds for t, or nil 
if
+// the stats don't prove that the column has a single value.
+func (m *inclusiveMetricsEval) findUniqueValue(t iceberg.BoundTerm) 
iceberg.Literal {
+       if _, ok := t.(iceberg.BoundExtract); ok {
+               return nil
+       }
+
+       field := t.Ref().Field()
+       if m.mayContainNull(field.ID) {
+               return nil
+       }
+
+       lowerBound, upperBound := m.lowerBounds[field.ID], 
m.upperBounds[field.ID]
+       if lowerBound == nil || upperBound == nil || !slices.Equal(lowerBound, 
upperBound) {
+               return nil
+       }
+
+       switch field.Type.(type) {
+       case iceberg.GeometryType, iceberg.GeographyType:
+               return nil
+       case iceberg.Float32Type, iceberg.Float64Type:
+               if count, ok := m.nanCounts[field.ID]; !ok || count != 0 {
+                       return nil
+               }
+       }
+
+       value, err := iceberg.LiteralFromBytes(field.Type, lowerBound)
+       if err != nil {
+               return nil
+       }
+
+       // Keep zero floats for the row filter, which can tell -0 from +0.
+       switch v := value.(type) {
+       case iceberg.Float32Literal:
+               if v == 0 || m.isNan(v) {
+                       return nil
+               }
+       case iceberg.Float64Literal:
+               if v == 0 || m.isNan(v) {
+                       return nil
+               }
+       }
+
+       return value
+}
+
 func (m *inclusiveMetricsEval) VisitStartsWith(t iceberg.BoundTerm, lit 
iceberg.Literal) bool {
        fieldID := t.Ref().Field().ID
 
diff --git a/table/evaluators_test.go b/table/evaluators_test.go
index e1fc165d7..108c8b551 100644
--- a/table/evaluators_test.go
+++ b/table/evaluators_test.go
@@ -2177,6 +2177,73 @@ func (suite *InclusiveMetricsTestSuite) 
TestNotInMetrics() {
        }
 }
 
+func (suite *InclusiveMetricsTestSuite) TestNotEqualAndNotInSingleValue() {
+       bound := func(lit iceberg.Literal) []byte {
+               b, err := lit.MarshalBinary()
+               suite.Require().NoError(err)
+
+               return b
+       }
+       singleValue := func(id int, value []byte, nulls int64, nans 
map[int]int64) iceberg.DataFile {
+               return &mockDataFile{
+                       path:        "single_value.parquet",
+                       format:      iceberg.ParquetFile,
+                       count:       10,
+                       valueCounts: map[int]int64{id: 10},
+                       nullCounts:  map[int]int64{id: nulls},
+                       nanCounts:   nans,
+                       lowerBounds: map[int][]byte{id: value},
+                       upperBounds: map[int][]byte{id: value},
+               }
+       }
+
+       abc := bound(iceberg.StringLiteral("abc"))
+       rangeOfValues := &mockDataFile{
+               path:        "range_of_values.parquet",
+               format:      iceberg.ParquetFile,
+               count:       10,
+               valueCounts: map[int]int64{3: 10},
+               nullCounts:  map[int]int64{3: 0},
+               lowerBounds: map[int][]byte{3: 
bound(iceberg.StringLiteral("aaa"))},
+               upperBounds: map[int][]byte{3: abc},
+       }
+       one := bound(iceberg.Float32Literal(1))
+       zero := bound(iceberg.Float32Literal(0))
+       nan := bound(iceberg.Float32Literal(float32(math.NaN())))
+
+       tests := []struct {
+               expr     iceberg.BooleanExpression
+               file     iceberg.DataFile
+               expected bool
+               msg      string
+       }{
+               {iceberg.NotEqualTo(iceberg.Reference("required"), "aaa"), 
rangeOfValues, true, "should read: range of values"},
+               {iceberg.NotEqualTo(iceberg.Reference("required"), "abc"), 
singleValue(3, abc, 0, nil), false, "should skip: single value equals literal"},
+               {iceberg.NotEqualTo(iceberg.Reference("required"), "def"), 
singleValue(3, abc, 0, nil), true, "should read: single value differs from 
literal"},
+               {iceberg.NotEqualTo(iceberg.Reference("some_empty"), "abc"), 
singleValue(14, abc, 2, nil), true, "should read: nulls match !="},
+               {iceberg.NotEqualTo(iceberg.Reference("no_nans"), float32(1)), 
singleValue(9, one, 0, map[int]int64{9: 0}), false, "should skip: single float 
equals literal"},
+               {iceberg.NotEqualTo(iceberg.Reference("no_nans"), float32(1)), 
singleValue(9, one, 0, map[int]int64{9: 3}), true, "should read: NaNs match 
!="},
+               {iceberg.NotEqualTo(iceberg.Reference("no_nans"), float32(1)), 
singleValue(9, one, 0, nil), true, "should read: unknown NaN count"},
+               {iceberg.NotEqualTo(iceberg.Reference("no_nans"), float32(0)), 
singleValue(9, zero, 0, map[int]int64{9: 0}), true, "should read: zero may be 
signed"},
+               {iceberg.NotEqualTo(iceberg.Reference("no_nans"), float32(1)), 
singleValue(9, nan, 0, map[int]int64{9: 0}), true, "should read: NaN bounds"},
+               {iceberg.NotIn(iceberg.Reference("required"), "aaa", "abc"), 
rangeOfValues, true, "should read: range of values"},
+               {iceberg.NotIn(iceberg.Reference("required"), "abc", "def"), 
singleValue(3, abc, 0, nil), false, "should skip: single value in exclusion 
list"},
+               {iceberg.NotIn(iceberg.Reference("required"), "def", "ghi"), 
singleValue(3, abc, 0, nil), true, "should read: single value not in exclusion 
list"},
+               {iceberg.NotIn(iceberg.Reference("some_empty"), "abc", "def"), 
singleValue(14, abc, 2, nil), true, "should read: nulls match NOT IN"},
+               {iceberg.NotIn(iceberg.Reference("no_nans"), float32(1), 
float32(5)), singleValue(9, one, 0, map[int]int64{9: 3}), true, "should read: 
NaNs match NOT IN"},
+       }
+
+       for _, tt := range tests {
+               suite.Run(tt.msg, func() {
+                       eval, err := 
newInclusiveMetricsEvaluator(suite.schemaDataFile, tt.expr, true, true)
+                       suite.Require().NoError(err)
+                       shouldRead, err := eval(tt.file)
+                       suite.Require().NoError(err)
+                       suite.Equal(tt.expected, shouldRead, tt.msg)
+               })
+       }
+}
+
 func (suite *InclusiveMetricsTestSuite) TestLessAndLessEqualNans() {
        type Op func(iceberg.UnboundTerm, int32) iceberg.UnboundPredicate
        for _, operator := range []Op{iceberg.LessThan[int32], 
iceberg.LessThanEqual[int32]} {
diff --git a/table/scan_row_filter_regression_test.go 
b/table/scan_row_filter_regression_test.go
index 4499f063f..757873e7e 100644
--- a/table/scan_row_filter_regression_test.go
+++ b/table/scan_row_filter_regression_test.go
@@ -138,6 +138,58 @@ func TestScanRowFilterStartsWith(t *testing.T) {
        })
 }
 
+func TestScanRowFilterNotEqualSingleValueFile(t *testing.T) {
+       schema := iceberg.NewSchema(0,
+               iceberg.NestedField{ID: 1, Name: "id", Type: 
iceberg.PrimitiveTypes.Int64, Required: true},
+               iceberg.NestedField{ID: 2, Name: "region", Type: 
iceberg.PrimitiveTypes.String, Required: false},
+       )
+       arrowSchema := arrow.NewSchema([]arrow.Field{
+               {Name: "id", Type: arrow.PrimitiveTypes.Int64, Nullable: false},
+               {Name: "region", Type: arrow.BinaryTypes.String, Nullable: 
true},
+       }, nil)
+
+       tbl := newTestScanTable(t, "not_equal_single_value", schema)
+       for _, rows := range []struct {
+               ids     []int64
+               regions []string
+       }{
+               {ids: []int64{1, 2}, regions: []string{"us", "us"}},
+               {ids: []int64{3, 4}, regions: []string{"us", "eu"}},
+       } {
+               bldr := array.NewRecordBuilder(memory.DefaultAllocator, 
arrowSchema)
+               bldr.Field(0).(*array.Int64Builder).AppendValues(rows.ids, nil)
+               bldr.Field(1).(*array.StringBuilder).AppendValues(rows.regions, 
nil)
+               rec := bldr.NewRecordBatch()
+               bldr.Release()
+
+               rdr, err := array.NewRecordReader(arrowSchema, 
[]arrow.RecordBatch{rec})
+               require.NoError(t, err)
+               tbl, err = tbl.Append(t.Context(), rdr, nil)
+               rdr.Release()
+               rec.Release()
+               require.NoError(t, err)
+       }
+
+       region := iceberg.Reference("region")
+       for _, tc := range []struct {
+               name   string
+               filter iceberg.BooleanExpression
+               files  int
+               ids    []int64
+       }{
+               {name: "not equal", filter: iceberg.NotEqualTo(region, "us"), 
files: 1, ids: []int64{4}},
+               {name: "not in", filter: iceberg.NotIn(region, "us", "kr"), 
files: 1, ids: []int64{4}},
+               {name: "not equal other", filter: iceberg.NotEqualTo(region, 
"eu"), files: 2, ids: []int64{1, 2, 3}},
+       } {
+               t.Run(tc.name, func(t *testing.T) {
+                       tasks, err := 
tbl.Scan(table.WithRowFilter(tc.filter)).PlanFiles(t.Context())
+                       require.NoError(t, err)
+                       require.Len(t, tasks, tc.files)
+                       require.ElementsMatch(t, tc.ids, 
scanIDsWithCheckedAlloc(t, tbl, tc.filter))
+               })
+       }
+}
+
 func TestScanRowFilterEmbeddedDictionary(t *testing.T) {
        schema := iceberg.NewSchema(0,
                iceberg.NestedField{ID: 1, Name: "id", Type: 
iceberg.PrimitiveTypes.Int64, Required: true},

Reply via email to