eneskeles commented on code in PR #2111:
URL: https://github.com/apache/iceberg-go/pull/2111#discussion_r4209997306


##########
table/copy_on_write_null_rows_test.go:
##########
@@ -0,0 +1,347 @@
+// 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"
+       "math"
+       "path/filepath"
+       "slices"
+       "testing"
+
+       "github.com/apache/arrow-go/v18/arrow"
+       "github.com/apache/arrow-go/v18/arrow/array"
+       "github.com/apache/arrow-go/v18/arrow/memory"
+       "github.com/apache/iceberg-go"
+       iceio "github.com/apache/iceberg-go/io"
+       "github.com/apache/iceberg-go/table"
+       "github.com/stretchr/testify/require"
+)
+
+// A copy-on-write delete must remove only the rows the filter is true for.
+// Rows where the filter is NULL (because a referenced column is NULL) don't
+// match it and must survive the rewrite.
+func TestCopyOnWriteDeleteKeepsRowsWhereFilterIsNull(t *testing.T) {
+       age := iceberg.Reference("age")
+       name := iceberg.Reference("name")
+
+       // rows: id=1 {25, "a"}, id=2 {30, NULL}, id=3 {NULL, "c"}, id=4 {40, 
"d"}
+       cases := []struct {
+               name   string
+               filter iceberg.BooleanExpression
+               want   []int64
+       }{
+               {"equal", iceberg.EqualTo(age, int64(30)), []int64{1, 3, 4}},
+               {"greater than", iceberg.GreaterThan(age, int64(35)), 
[]int64{1, 2, 3}},
+               {"in", iceberg.IsIn(age, int64(30), int64(40)), []int64{1, 3}},
+               // binding reduces the set to one value and the predicate to 
Equal
+               {"in with duplicate values", iceberg.IsIn(age, int64(30), 
int64(30)), []int64{1, 3, 4}},
+               {"not in", iceberg.NotIn(age, int64(30), int64(40)), []int64{2, 
3, 4}},
+               {"not in with duplicate values", iceberg.NotIn(age, int64(30), 
int64(30)), []int64{2, 3}},
+               {"not", iceberg.NewNot(iceberg.EqualTo(age, int64(30))), 
[]int64{2, 3}},
+               {"is null", iceberg.IsNull(age), []int64{1, 2, 4}},
+               {"and", iceberg.NewAnd(iceberg.EqualTo(age, int64(30)), 
iceberg.EqualTo(name, "c")), []int64{1, 2, 3, 4}},
+               {"or", iceberg.NewOr(iceberg.EqualTo(age, int64(25)), 
iceberg.EqualTo(name, "c")), []int64{2, 4}},
+               {"not and", iceberg.NewNot(iceberg.NewAnd(iceberg.EqualTo(age, 
int64(30)), iceberg.EqualTo(name, "c"))), []int64{2, 3}},
+               // the literal is outside the file's bounds, so every non-NULL 
row
+               // matches and only the NULL row keeps the file from being 
deleted whole
+               {"not equal outside bounds", iceberg.NotEqualTo(age, 
int64(100)), []int64{3}},
+               {"not outside bounds", iceberg.NewNot(iceberg.EqualTo(age, 
int64(100))), []int64{3}},
+               {"not in outside bounds", iceberg.NotIn(age, int64(100), 
int64(200)), []int64{3}},
+       }
+
+       for _, version := range []string{"2", "3"} {
+               for _, c := range cases {
+                       t.Run("v"+version+"/"+c.name, func(t *testing.T) {
+                               ctx := context.Background()
+                               tbl := newCopyOnWriteNullRowsTable(t, version)
+
+                               txn := tbl.NewTransaction()
+                               require.NoError(t, txn.Delete(ctx, c.filter, 
nil))
+                               tbl, err := txn.Commit(ctx)
+                               require.NoError(t, err)
+
+                               ids := scanIDs(t, tbl)
+                               slices.Sort(ids)
+                               require.Equal(t, c.want, ids)
+                       })
+               }
+       }
+}
+
+// A filtered overwrite rewrites matched files through the same path.
+func TestCopyOnWriteOverwriteKeepsRowsWhereFilterIsNull(t *testing.T) {
+       ctx := context.Background()
+       tbl := newCopyOnWriteNullRowsTable(t, "2")
+
+       arrowSchema, err := table.SchemaToArrowSchema(tbl.Schema(), nil, false, 
false)
+       require.NoError(t, err)
+       data, err := array.TableFromJSON(memory.DefaultAllocator, arrowSchema, 
[]string{
+               `[{"id":5,"age":30,"name":"e"}]`,
+       })
+       require.NoError(t, err)
+       defer data.Release()
+
+       txn := tbl.NewTransaction()
+       require.NoError(t, txn.OverwriteTable(ctx, data, 1024, nil,
+               
table.WithOverwriteFilter(iceberg.EqualTo(iceberg.Reference("age"), 
int64(30)))))
+       tbl, err = txn.Commit(ctx)
+       require.NoError(t, err)
+
+       ids := scanIDs(t, tbl)
+       slices.Sort(ids)
+       require.Equal(t, []int64{1, 3, 4, 5}, ids)
+}
+
+// For floating-point columns, NaN compares false rather than NULL, so a delete
+// must remove exactly the rows a scan with the same filter returns.
+func TestCopyOnWriteDeleteMatchesScanWithNaNAndNull(t *testing.T) {
+       x := iceberg.Reference("x")
+
+       cases := []struct {
+               name   string
+               filter iceberg.BooleanExpression
+       }{
+               {"less than", iceberg.LessThan(x, 5.0)},
+               {"greater than or equal", iceberg.GreaterThanEqual(x, 5.0)},
+               {"not less than", iceberg.NewNot(iceberg.LessThan(x, 5.0))},
+               {"not equal", iceberg.NotEqualTo(x, 1.0)},
+               {"is nan", iceberg.IsNaN(x)},
+               {"not nan", iceberg.NotNaN(x)},
+               {"not is nan", iceberg.NewNot(iceberg.IsNaN(x))},
+       }
+
+       for _, version := range []string{"2", "3"} {
+               for _, c := range cases {
+                       t.Run("v"+version+"/"+c.name, func(t *testing.T) {
+                               tbl := newCopyOnWriteNaNTable(t, version,
+                                       []float64{1, math.NaN(), 10, 0}, 
[]bool{true, true, true, false})
+                               requireDeleteMatchesScan(t, tbl, c.filter)
+                       })
+               }
+       }
+}
+
+// Row-group pruning on the rows to keep must not skip a NaN row. Parquet
+// min/max leave NaN out, so with every other value on one side of 5 a pruning
+// filter of x >= 5 for NOT(x < 5) would drop the whole row group.
+func TestCopyOnWriteDeleteKeepsNaNRowsInPrunedRowGroups(t *testing.T) {
+       x := iceberg.Reference("x")
+
+       cases := []struct {
+               name   string
+               values []float64
+               filter iceberg.BooleanExpression
+       }{
+               {"less than", []float64{1, math.NaN(), 2}, iceberg.LessThan(x, 
5.0)},
+               {"less than or equal", []float64{1, math.NaN(), 2}, 
iceberg.LessThanEqual(x, 5.0)},
+               {"greater than", []float64{10, math.NaN(), 20}, 
iceberg.GreaterThan(x, 5.0)},
+               {"greater than or equal", []float64{10, math.NaN(), 20}, 
iceberg.GreaterThanEqual(x, 5.0)},
+               {"or", []float64{1, math.NaN(), 20}, 
iceberg.NewOr(iceberg.LessThan(x, 5.0), iceberg.GreaterThan(x, 10.0))},
+               {"and", []float64{1, math.NaN(), 2}, 
iceberg.NewAnd(iceberg.GreaterThan(x, 0.0), iceberg.LessThan(x, 5.0))},
+       }
+
+       for _, version := range []string{"2", "3"} {
+               for _, c := range cases {
+                       t.Run("v"+version+"/"+c.name, func(t *testing.T) {
+                               tbl := newCopyOnWriteNaNTable(t, version, 
c.values, nil)
+                               requireDeleteMatchesScan(t, tbl, c.filter)
+                       })
+               }
+       }
+}
+
+// Rows of a file written before the filtered column was added read as NULL
+// and must survive, also for IsNaN, which is false rather than NULL for them.
+func TestCopyOnWriteDeleteKeepsRowsOfFilesWithoutTheColumn(t *testing.T) {
+       x := iceberg.Reference("x")
+
+       cases := []struct {
+               name   string
+               filter iceberg.BooleanExpression
+               want   []int64
+       }{
+               {"is nan", iceberg.IsNaN(x), []int64{1, 2, 3, 4, 5}},
+               {"is nan and", iceberg.NewAnd(iceberg.IsNaN(x), 
iceberg.GreaterThan(iceberg.Reference("id"), int64(0))), []int64{1, 2, 3, 4, 
5}},
+               {"less than", iceberg.LessThan(x, 5.0), []int64{1, 2, 3, 4, 6}},
+               {"is null", iceberg.IsNull(x), []int64{5, 6}},

Review Comment:
   Makes sense. Added.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to