laskoviymishka commented on code in PR #2487: URL: https://github.com/apache/iceberg-go/pull/2487#discussion_r4596833319
########## table/identity_partition_projection_internal_test.go: ########## @@ -0,0 +1,413 @@ +// 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 + +import ( + "context" + "fmt" + "iter" + "os" + "path/filepath" + "sort" + "testing" + + "github.com/apache/arrow-go/v18/arrow" + "github.com/apache/arrow-go/v18/arrow/array" + "github.com/apache/arrow-go/v18/arrow/decimal128" + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/apache/arrow-go/v18/parquet/pqarrow" + "github.com/apache/iceberg-go" + iceio "github.com/apache/iceberg-go/io" + "github.com/stretchr/testify/require" +) + +// A writer may leave an identity partition column out of its data files, as +// Delta and Hive-style layouts do. The spec's column projection reads such a +// field from the file's partition value (rule 1), required or not. +func TestScanFillsIdentityPartitionColumnMissingFromDataFile(t *testing.T) { + ctx := context.Background() + 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: "region", Type: iceberg.PrimitiveTypes.String, Required: true}, + ) + spec := iceberg.NewPartitionSpec(iceberg.PartitionField{ + SourceIDs: []int{2}, FieldID: 1000, Name: "region", Transform: iceberg.IdentityTransform{}, + }) + meta, err := NewMetadata(schema, &spec, UnsortedSortOrder, location, nil) + require.NoError(t, err) + tbl := New(Identifier{"db", "identity"}, meta, location+"/metadata/v1.metadata.json", + func(context.Context) (iceio.IO, error) { return iceio.LocalFS{}, nil }, &countingCatalog{metadata: meta}) + + // Each file stores only id; its region lives in the manifest. + idOnly := arrow.NewSchema([]arrow.Field{{ + Name: "id", Type: arrow.PrimitiveTypes.Int64, + Metadata: arrow.NewMetadata([]string{ArrowParquetFieldIDKey}, []string{"1"}), + }}, nil) + files := make([]iceberg.DataFile, 0, 2) + for _, part := range []struct { + region string + ids string + }{{"east", `[{"id":1},{"id":2}]`}, {"west", `[{"id":3}]`}} { + data, err := array.TableFromJSON(memory.DefaultAllocator, idOnly, []string{part.ids}) + require.NoError(t, err) + path := location + "/data/" + part.region + ".parquet" + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + f, err := os.Create(path) + require.NoError(t, err) + require.NoError(t, pqarrow.WriteTable(data, f, 1024, nil, pqarrow.DefaultWriterProps())) + info, err := os.Stat(path) + require.NoError(t, err) + + b, err := iceberg.NewDataFileBuilder(spec, iceberg.EntryContentData, path, iceberg.ParquetFile, + map[int]any{1000: part.region}, nil, nil, data.NumRows(), info.Size()) + require.NoError(t, err) + files = append(files, b.Build()) + data.Release() + } + txn := tbl.NewTransaction() + require.NoError(t, txn.AddDataFiles(ctx, files, nil)) + tbl, err = txn.Commit(ctx) + require.NoError(t, err) + + result, err := tbl.Scan().ToArrowTable(ctx) + require.NoError(t, err) + defer result.Release() + + got := map[int64]string{} + reader := array.NewTableReader(result, -1) + defer reader.Release() + for reader.Next() { + rec := reader.RecordBatch() + ids := rec.Column(0).(*array.Int64) + regions := rec.Column(1).(*array.String) + for i := range int(rec.NumRows()) { + got[ids.Value(i)] = regions.Value(i) + } + } + require.Equal(t, map[int64]string{1: "east", 2: "east", 3: "west"}, got) + + // A filter on the partition column prunes by it and still fills it. + filtered, err := tbl.Scan(WithRowFilter(iceberg.EqualTo(iceberg.Reference("region"), "west"))).ToArrowTable(ctx) + require.NoError(t, err) + defer filtered.Release() + require.EqualValues(t, 1, filtered.NumRows()) +} + +// Partition values come back from the manifest as literals; a decimal one +// must still fill the column rather than fail the scan. +func TestScanFillsDecimalIdentityPartitionColumnMissingFromDataFile(t *testing.T) { + ctx := context.Background() + 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: "amount", Type: iceberg.MustDecimalTypeOf(9, 2)}, + ) + spec := iceberg.NewPartitionSpec(iceberg.PartitionField{ + SourceIDs: []int{2}, FieldID: 1000, Name: "amount", Transform: iceberg.IdentityTransform{}, + }) + meta, err := NewMetadata(schema, &spec, UnsortedSortOrder, location, nil) + require.NoError(t, err) + tbl := New(Identifier{"db", "identity_decimal"}, meta, location+"/metadata/v1.metadata.json", + func(context.Context) (iceio.IO, error) { return iceio.LocalFS{}, nil }, &countingCatalog{metadata: meta}) + + idOnly := arrow.NewSchema([]arrow.Field{{ + Name: "id", Type: arrow.PrimitiveTypes.Int64, + Metadata: arrow.NewMetadata([]string{ArrowParquetFieldIDKey}, []string{"1"}), + }}, nil) + data, err := array.TableFromJSON(memory.DefaultAllocator, idOnly, []string{`[{"id":1},{"id":2}]`}) + require.NoError(t, err) + defer data.Release() + path := location + "/data/amount.parquet" + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + f, err := os.Create(path) + require.NoError(t, err) + require.NoError(t, pqarrow.WriteTable(data, f, 1024, nil, pqarrow.DefaultWriterProps())) + info, err := os.Stat(path) + require.NoError(t, err) + + value := iceberg.Decimal{Val: decimal128.FromI64(1234), Scale: 2} + b, err := iceberg.NewDataFileBuilder(spec, iceberg.EntryContentData, path, iceberg.ParquetFile, + map[int]any{1000: value}, nil, nil, data.NumRows(), info.Size()) + require.NoError(t, err) + txn := tbl.NewTransaction() + require.NoError(t, txn.AddDataFiles(ctx, []iceberg.DataFile{b.Build()}, nil)) + tbl, err = txn.Commit(ctx) + require.NoError(t, err) + + result, err := tbl.Scan().ToArrowTable(ctx) + require.NoError(t, err) + defer result.Release() + require.EqualValues(t, 2, result.NumRows()) + for _, chunk := range result.Column(1).Data().Chunks() { + amounts := chunk.(*array.Decimal128) + for i := range amounts.Len() { + require.False(t, amounts.IsNull(i)) + require.Equal(t, "12.34", amounts.ValueStr(i)) + } + } +} + +// identityPartitionTestTable returns an empty table at a fresh location. +func identityPartitionTestTable(t *testing.T, schema *iceberg.Schema, spec iceberg.PartitionSpec, props iceberg.Properties) *Table { + t.Helper() + + location := filepath.ToSlash(t.TempDir()) + meta, err := NewMetadata(schema, &spec, UnsortedSortOrder, location, props) + require.NoError(t, err) + + return New(Identifier{"db", "identity"}, meta, location+"/metadata/v1.metadata.json", + func(context.Context) (iceio.IO, error) { return iceio.LocalFS{}, nil }, &countingCatalog{metadata: meta}) +} + +// addIDOnlyFile commits a data file that stores only the id column (field ID +// 1), with partition as its partition data under the table's current spec. +func addIDOnlyFile(t *testing.T, tbl *Table, name string, partition map[int]any, ids string) *Table { + t.Helper() + + idOnly := arrow.NewSchema([]arrow.Field{{ + Name: "id", Type: arrow.PrimitiveTypes.Int64, + Metadata: arrow.NewMetadata([]string{ArrowParquetFieldIDKey}, []string{"1"}), + }}, nil) + data, err := array.TableFromJSON(memory.DefaultAllocator, idOnly, []string{ids}) + require.NoError(t, err) + defer data.Release() + + path := tbl.Location() + "/data/" + name + ".parquet" + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + f, err := os.Create(path) + require.NoError(t, err) + require.NoError(t, pqarrow.WriteTable(data, f, 1024, nil, pqarrow.DefaultWriterProps())) + info, err := os.Stat(path) + require.NoError(t, err) + + b, err := iceberg.NewDataFileBuilder(tbl.Spec(), iceberg.EntryContentData, path, iceberg.ParquetFile, + partition, nil, nil, data.NumRows(), info.Size()) + require.NoError(t, err) + if tbl.Metadata().FormatVersion() >= 3 { + b.FirstRowID(tbl.Metadata().NextRowID()) + } + txn := tbl.NewTransaction() + require.NoError(t, txn.AddDataFiles(t.Context(), []iceberg.DataFile{b.Build()}, nil)) + tbl, err = txn.Commit(t.Context()) + require.NoError(t, err) + + return tbl +} + +// scanIDAndColumn returns each row of a two-column (id, value) scan as +// "id:value", sorted, with "id:<null>" for a null value. +func scanIDAndColumn(t *testing.T, tbl *Table, opts ...ScanOption) []string { + t.Helper() + + result, err := tbl.Scan(opts...).ToArrowTable(t.Context()) + require.NoError(t, err) + defer result.Release() + + rows := []string{} + reader := array.NewTableReader(result, -1) + defer reader.Release() + for reader.Next() { + rec := reader.RecordBatch() + ids := rec.Column(0).(*array.Int64) + for i := range int(rec.NumRows()) { + value := "<null>" + if !rec.Column(1).IsNull(i) { + value = rec.Column(1).ValueStr(i) + } + rows = append(rows, fmt.Sprintf("%d:%s", ids.Value(i), value)) + } + } + sort.Strings(rows) + + return rows +} + +// regionTestTable has id 1 in both partitions, in files that store only id. +func regionTestTable(t *testing.T, props iceberg.Properties) *Table { + t.Helper() + + 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: true}, + ) + spec := iceberg.NewPartitionSpec(iceberg.PartitionField{ + SourceIDs: []int{2}, FieldID: 1000, Name: "region", Transform: iceberg.IdentityTransform{}, + }) + tbl := identityPartitionTestTable(t, schema, spec, props) + tbl = addIDOnlyFile(t, tbl, "east", map[int]any{1000: "east"}, `[{"id":1},{"id":2}]`) + + return addIDOnlyFile(t, tbl, "west", map[int]any{1000: "west"}, `[{"id":1},{"id":3}]`) +} + +// A row filter must see the partition value of a column the file does not +// store, not a null. A scan takes each file's residual from planning, which +// substitutes the partition value already; this pins that it stays so. +func TestFilterOnIdentityPartitionColumnMissingFromDataFile(t *testing.T) { Review Comment: Could we add a table-driven test that calls `foldIdentityPartitionConstants` directly, with OR, NOT, IN/NOT IN and IsNull/NotNull on the missing column? With the folder returning its input unchanged, every subtest here still passes through the planning residual; only the merge-on-read delete and the initial-default `not equal` case fail. ########## table/arrow_utils.go: ########## @@ -1276,7 +1280,17 @@ func (a *arrowProjectionVisitor) Struct(st iceberg.StructType, structArr arrow.A dt := a.typeToArrowType(field.Type) alloc := compute.GetAllocator(a.ctx) + constant, hasConstant := a.partitionConstants[field.ID] switch { + case hasConstant && constant != nil: + // Widen a value written before the field was promoted. A value + // the conversion does not cover is used as the manifest gave it. + if converted, err := identityPartitionValue(constant, field.Type); err == nil { Review Comment: Could this return the conversion error instead of dropping it? On failure the raw value reaches `defaultToArray`, which fails with a `write-default` message naming the wrong cause; the folder reports it with the column name ([arrow_scanner.go#L3034-L3037](https://github.com/apache/iceberg-go/blob/c878aeb541e4cec655516910ae4fb2f03cb067a2/table/arrow_scanner.go#L3034-L3037)). `Struct` can't return an error, so could the constants be converted once per file, in `identityPartitionConstants` or `projectToRequestedSchema`? ########## table/arrow_utils.go: ########## @@ -1276,7 +1280,17 @@ func (a *arrowProjectionVisitor) Struct(st iceberg.StructType, structArr arrow.A dt := a.typeToArrowType(field.Type) alloc := compute.GetAllocator(a.ctx) + constant, hasConstant := a.partitionConstants[field.ID] switch { + case hasConstant && constant != nil: + // Widen a value written before the field was promoted. A value + // the conversion does not cover is used as the manifest gave it. + if converted, err := identityPartitionValue(constant, field.Type); err == nil { + constant = converted + } + arr = defaultToArray(constant, field.Type, dt, structArr.Len(), alloc) + case hasConstant: Review Comment: Should a null partition value on a required field fall through to the missing-required check, as it does on [`main`](https://github.com/apache/iceberg-go/blob/8d13e317c79f36f38f52a16b2f81f1d966f1fc27/table/arrow_utils.go#L1279-L1289)? With `case hasConstant:` first, the column comes out all null in a field marked non-nullable. Only metadata outside the spec reaches it, e.g. a column made required with `allowIncompatibleChanges`. ########## table/identity_partition_projection_internal_test.go: ########## @@ -0,0 +1,413 @@ +// 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 + +import ( + "context" + "fmt" + "iter" + "os" + "path/filepath" + "sort" + "testing" + + "github.com/apache/arrow-go/v18/arrow" + "github.com/apache/arrow-go/v18/arrow/array" + "github.com/apache/arrow-go/v18/arrow/decimal128" + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/apache/arrow-go/v18/parquet/pqarrow" + "github.com/apache/iceberg-go" + iceio "github.com/apache/iceberg-go/io" + "github.com/stretchr/testify/require" +) + +// A writer may leave an identity partition column out of its data files, as +// Delta and Hive-style layouts do. The spec's column projection reads such a +// field from the file's partition value (rule 1), required or not. +func TestScanFillsIdentityPartitionColumnMissingFromDataFile(t *testing.T) { + ctx := context.Background() + 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: "region", Type: iceberg.PrimitiveTypes.String, Required: true}, + ) + spec := iceberg.NewPartitionSpec(iceberg.PartitionField{ + SourceIDs: []int{2}, FieldID: 1000, Name: "region", Transform: iceberg.IdentityTransform{}, + }) + meta, err := NewMetadata(schema, &spec, UnsortedSortOrder, location, nil) + require.NoError(t, err) + tbl := New(Identifier{"db", "identity"}, meta, location+"/metadata/v1.metadata.json", + func(context.Context) (iceio.IO, error) { return iceio.LocalFS{}, nil }, &countingCatalog{metadata: meta}) + + // Each file stores only id; its region lives in the manifest. + idOnly := arrow.NewSchema([]arrow.Field{{ + Name: "id", Type: arrow.PrimitiveTypes.Int64, + Metadata: arrow.NewMetadata([]string{ArrowParquetFieldIDKey}, []string{"1"}), + }}, nil) + files := make([]iceberg.DataFile, 0, 2) + for _, part := range []struct { + region string + ids string + }{{"east", `[{"id":1},{"id":2}]`}, {"west", `[{"id":3}]`}} { + data, err := array.TableFromJSON(memory.DefaultAllocator, idOnly, []string{part.ids}) + require.NoError(t, err) + path := location + "/data/" + part.region + ".parquet" + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + f, err := os.Create(path) + require.NoError(t, err) + require.NoError(t, pqarrow.WriteTable(data, f, 1024, nil, pqarrow.DefaultWriterProps())) + info, err := os.Stat(path) + require.NoError(t, err) + + b, err := iceberg.NewDataFileBuilder(spec, iceberg.EntryContentData, path, iceberg.ParquetFile, + map[int]any{1000: part.region}, nil, nil, data.NumRows(), info.Size()) + require.NoError(t, err) + files = append(files, b.Build()) + data.Release() + } + txn := tbl.NewTransaction() + require.NoError(t, txn.AddDataFiles(ctx, files, nil)) + tbl, err = txn.Commit(ctx) + require.NoError(t, err) + + result, err := tbl.Scan().ToArrowTable(ctx) + require.NoError(t, err) + defer result.Release() + + got := map[int64]string{} + reader := array.NewTableReader(result, -1) + defer reader.Release() + for reader.Next() { + rec := reader.RecordBatch() + ids := rec.Column(0).(*array.Int64) + regions := rec.Column(1).(*array.String) + for i := range int(rec.NumRows()) { + got[ids.Value(i)] = regions.Value(i) + } + } + require.Equal(t, map[int64]string{1: "east", 2: "east", 3: "west"}, got) + + // A filter on the partition column prunes by it and still fills it. + filtered, err := tbl.Scan(WithRowFilter(iceberg.EqualTo(iceberg.Reference("region"), "west"))).ToArrowTable(ctx) + require.NoError(t, err) + defer filtered.Release() + require.EqualValues(t, 1, filtered.NumRows()) +} + +// Partition values come back from the manifest as literals; a decimal one +// must still fill the column rather than fail the scan. +func TestScanFillsDecimalIdentityPartitionColumnMissingFromDataFile(t *testing.T) { + ctx := context.Background() + 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: "amount", Type: iceberg.MustDecimalTypeOf(9, 2)}, + ) + spec := iceberg.NewPartitionSpec(iceberg.PartitionField{ + SourceIDs: []int{2}, FieldID: 1000, Name: "amount", Transform: iceberg.IdentityTransform{}, + }) + meta, err := NewMetadata(schema, &spec, UnsortedSortOrder, location, nil) + require.NoError(t, err) + tbl := New(Identifier{"db", "identity_decimal"}, meta, location+"/metadata/v1.metadata.json", + func(context.Context) (iceio.IO, error) { return iceio.LocalFS{}, nil }, &countingCatalog{metadata: meta}) + + idOnly := arrow.NewSchema([]arrow.Field{{ + Name: "id", Type: arrow.PrimitiveTypes.Int64, + Metadata: arrow.NewMetadata([]string{ArrowParquetFieldIDKey}, []string{"1"}), + }}, nil) + data, err := array.TableFromJSON(memory.DefaultAllocator, idOnly, []string{`[{"id":1},{"id":2}]`}) + require.NoError(t, err) + defer data.Release() + path := location + "/data/amount.parquet" + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + f, err := os.Create(path) + require.NoError(t, err) + require.NoError(t, pqarrow.WriteTable(data, f, 1024, nil, pqarrow.DefaultWriterProps())) + info, err := os.Stat(path) + require.NoError(t, err) + + value := iceberg.Decimal{Val: decimal128.FromI64(1234), Scale: 2} + b, err := iceberg.NewDataFileBuilder(spec, iceberg.EntryContentData, path, iceberg.ParquetFile, + map[int]any{1000: value}, nil, nil, data.NumRows(), info.Size()) + require.NoError(t, err) + txn := tbl.NewTransaction() + require.NoError(t, txn.AddDataFiles(ctx, []iceberg.DataFile{b.Build()}, nil)) + tbl, err = txn.Commit(ctx) + require.NoError(t, err) + + result, err := tbl.Scan().ToArrowTable(ctx) + require.NoError(t, err) + defer result.Release() + require.EqualValues(t, 2, result.NumRows()) + for _, chunk := range result.Column(1).Data().Chunks() { + amounts := chunk.(*array.Decimal128) + for i := range amounts.Len() { + require.False(t, amounts.IsNull(i)) + require.Equal(t, "12.34", amounts.ValueStr(i)) + } + } +} + +// identityPartitionTestTable returns an empty table at a fresh location. +func identityPartitionTestTable(t *testing.T, schema *iceberg.Schema, spec iceberg.PartitionSpec, props iceberg.Properties) *Table { + t.Helper() + + location := filepath.ToSlash(t.TempDir()) + meta, err := NewMetadata(schema, &spec, UnsortedSortOrder, location, props) + require.NoError(t, err) + + return New(Identifier{"db", "identity"}, meta, location+"/metadata/v1.metadata.json", + func(context.Context) (iceio.IO, error) { return iceio.LocalFS{}, nil }, &countingCatalog{metadata: meta}) +} + +// addIDOnlyFile commits a data file that stores only the id column (field ID +// 1), with partition as its partition data under the table's current spec. +func addIDOnlyFile(t *testing.T, tbl *Table, name string, partition map[int]any, ids string) *Table { + t.Helper() + + idOnly := arrow.NewSchema([]arrow.Field{{ + Name: "id", Type: arrow.PrimitiveTypes.Int64, + Metadata: arrow.NewMetadata([]string{ArrowParquetFieldIDKey}, []string{"1"}), + }}, nil) + data, err := array.TableFromJSON(memory.DefaultAllocator, idOnly, []string{ids}) + require.NoError(t, err) + defer data.Release() + + path := tbl.Location() + "/data/" + name + ".parquet" + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + f, err := os.Create(path) + require.NoError(t, err) + require.NoError(t, pqarrow.WriteTable(data, f, 1024, nil, pqarrow.DefaultWriterProps())) + info, err := os.Stat(path) + require.NoError(t, err) + + b, err := iceberg.NewDataFileBuilder(tbl.Spec(), iceberg.EntryContentData, path, iceberg.ParquetFile, + partition, nil, nil, data.NumRows(), info.Size()) + require.NoError(t, err) + if tbl.Metadata().FormatVersion() >= 3 { + b.FirstRowID(tbl.Metadata().NextRowID()) + } + txn := tbl.NewTransaction() + require.NoError(t, txn.AddDataFiles(t.Context(), []iceberg.DataFile{b.Build()}, nil)) + tbl, err = txn.Commit(t.Context()) + require.NoError(t, err) + + return tbl +} + +// scanIDAndColumn returns each row of a two-column (id, value) scan as +// "id:value", sorted, with "id:<null>" for a null value. +func scanIDAndColumn(t *testing.T, tbl *Table, opts ...ScanOption) []string { + t.Helper() + + result, err := tbl.Scan(opts...).ToArrowTable(t.Context()) + require.NoError(t, err) + defer result.Release() + + rows := []string{} + reader := array.NewTableReader(result, -1) + defer reader.Release() + for reader.Next() { + rec := reader.RecordBatch() + ids := rec.Column(0).(*array.Int64) + for i := range int(rec.NumRows()) { + value := "<null>" + if !rec.Column(1).IsNull(i) { + value = rec.Column(1).ValueStr(i) + } + rows = append(rows, fmt.Sprintf("%d:%s", ids.Value(i), value)) + } + } + sort.Strings(rows) + + return rows +} + +// regionTestTable has id 1 in both partitions, in files that store only id. +func regionTestTable(t *testing.T, props iceberg.Properties) *Table { + t.Helper() + + 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: true}, + ) + spec := iceberg.NewPartitionSpec(iceberg.PartitionField{ + SourceIDs: []int{2}, FieldID: 1000, Name: "region", Transform: iceberg.IdentityTransform{}, + }) + tbl := identityPartitionTestTable(t, schema, spec, props) + tbl = addIDOnlyFile(t, tbl, "east", map[int]any{1000: "east"}, `[{"id":1},{"id":2}]`) + + return addIDOnlyFile(t, tbl, "west", map[int]any{1000: "west"}, `[{"id":1},{"id":3}]`) +} + +// A row filter must see the partition value of a column the file does not +// store, not a null. A scan takes each file's residual from planning, which +// substitutes the partition value already; this pins that it stays so. +func TestFilterOnIdentityPartitionColumnMissingFromDataFile(t *testing.T) { + tbl := regionTestTable(t, nil) + region, id := iceberg.Reference("region"), iceberg.Reference("id") + westOne := iceberg.NewAnd(iceberg.EqualTo(region, "west"), iceberg.EqualTo(id, int64(1))) + + tests := []struct { + name string + filter iceberg.BooleanExpression + want []string + }{ + {"and", westOne, []string{"1:west"}}, + {"not and", iceberg.NewNot(westOne), []string{"1:east", "2:east", "3:west"}}, + {"not equal", iceberg.NotEqualTo(region, "east"), []string{"1:west", "3:west"}}, + { + "or", iceberg.NewOr(iceberg.EqualTo(region, "east"), iceberg.EqualTo(id, int64(3))), + []string{"1:east", "2:east", "3:west"}, + }, + {"in", iceberg.IsIn(region, "west", "north"), []string{"1:west", "3:west"}}, + {"not in", iceberg.NotIn(region, "west", "north"), []string{"1:east", "2:east"}}, + {"starts with", iceberg.StartsWith(region, "ea"), []string{"1:east", "2:east"}}, + {"is null", iceberg.IsNull(region), []string{}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.Equal(t, tt.want, scanIDAndColumn(t, tbl, WithRowFilter(tt.filter))) + }) + } +} + +// Delete evaluates its filter per file: copy-on-write keeps the rows that do +// not match, and merge-on-read writes position deletes for the rows that do. +func TestDeleteOnIdentityPartitionColumnMissingFromDataFile(t *testing.T) { + for _, mode := range []string{WriteModeCopyOnWrite, WriteModeMergeOnRead} { + t.Run(mode, func(t *testing.T) { + tbl := regionTestTable(t, iceberg.Properties{ + PropertyFormatVersion: "2", Review Comment: Could this also run a format-version 3 copy-on-write delete with row lineage? That's the only case where `as.rowGroupFilter` is set ([transaction_delete.go#L787-L819](https://github.com/apache/iceberg-go/blob/c878aeb541e4cec655516910ae4fb2f03cb067a2/table/transaction_delete.go#L787-L819)), so the separate fold in `processRecordsWithPlans` ([arrow_scanner.go#L2684-L2690](https://github.com/apache/iceberg-go/blob/c878aeb541e4cec655516910ae4fb2f03cb067a2/table/arrow_scanner.go#L2684-L2690)) runs in no test today. -- 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]
