dgvj-work commented on code in PR #2170: URL: https://github.com/apache/iceberg-go/pull/2170#discussion_r4420176441
########## table/changelog_reader_external_test.go: ########## @@ -0,0 +1,344 @@ +// 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" + "fmt" + "iter" + "os" + "testing" + + "github.com/apache/arrow-go/v18/arrow" + "github.com/apache/arrow-go/v18/arrow/array" + "github.com/apache/arrow-go/v18/arrow/compute" + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/apache/iceberg-go" + "github.com/apache/iceberg-go/table" + "github.com/stretchr/testify/require" +) + +func TestIncrementalChangelogReadEmptyTable(t *testing.T) { + schema, records, err := newV2RowLineageTestTable(t).NewIncrementalChangelogScan().ToArrowRecords(t.Context()) + require.NoError(t, err) + require.Equal(t, []string{ + "id", "data", + table.ChangelogChangeTypeColumn, + table.ChangelogChangeOrdinalColumn, + table.ChangelogCommitSnapshotIDColumn, + }, schemaFieldNames(schema)) + + require.Empty(t, collectChangelogRows(t, records)) +} + +func TestIncrementalChangelogReadFileChanges(t *testing.T) { + ctx := t.Context() + tbl := changelogOverwriteTable(t, ctx) + + tasks, err := tbl.NewIncrementalChangelogScan().PlanFiles(ctx) + require.NoError(t, err) + require.Len(t, tasks, 3) + + schema, records, err := tbl.NewIncrementalChangelogScan().Read(ctx, tasks) + require.NoError(t, err) + require.Equal(t, []string{ + "id", "data", + table.ChangelogChangeTypeColumn, + table.ChangelogChangeOrdinalColumn, + table.ChangelogCommitSnapshotIDColumn, + }, schemaFieldNames(schema)) + + got := collectChangelogRows(t, records) + require.Equal(t, []changelogRow{ + {id: 1, data: "a", op: string(table.ChangelogOpInsert), ordinal: int64(tasks[0].ChangeOrdinal()), snapshot: tasks[0].CommitSnapshotID()}, + {id: 1, data: "a", op: string(table.ChangelogOpDelete), ordinal: int64(tasks[1].ChangeOrdinal()), snapshot: tasks[1].CommitSnapshotID()}, + {id: 2, data: "b", op: string(table.ChangelogOpInsert), ordinal: int64(tasks[2].ChangeOrdinal()), snapshot: tasks[2].CommitSnapshotID()}, + }, got) + require.NotEqual(t, tasks[0].CommitSnapshotID(), tasks[1].CommitSnapshotID()) + require.Equal(t, tasks[1].CommitSnapshotID(), tasks[2].CommitSnapshotID()) + require.Equal(t, 0, tasks[0].ChangeOrdinal()) + require.Equal(t, 1, tasks[1].ChangeOrdinal()) + require.Equal(t, 1, tasks[2].ChangeOrdinal()) +} + +func TestIncrementalChangelogReadHonorsProjectionFilterAndLimit(t *testing.T) { + ctx := t.Context() + tbl := changelogOverwriteTable(t, ctx) + + schema, records, err := tbl.NewIncrementalChangelogScan( + table.WithSelectedFields("id"), + table.WithRowFilter(iceberg.EqualTo(iceberg.Reference("id"), int64(2))), + table.WithLimit(1), + ).ToArrowRecords(ctx) + require.NoError(t, err) + require.Equal(t, []string{ + "id", + table.ChangelogChangeTypeColumn, + table.ChangelogChangeOrdinalColumn, + table.ChangelogCommitSnapshotIDColumn, + }, schemaFieldNames(schema)) + + got := collectChangelogRows(t, records) + require.Equal(t, []changelogRow{ + {id: 2, op: string(table.ChangelogOpInsert), ordinal: 1}, + }, clearChangelogSnapshot(got)) +} + +func TestIncrementalChangelogReadLimitCutsABatch(t *testing.T) { + ctx := t.Context() + tbl := newV3RowLineageTestTable(t) + arrowSchema := arrow.NewSchema([]arrow.Field{ + {Name: "id", Type: arrow.PrimitiveTypes.Int64, Nullable: false}, + {Name: "data", Type: arrow.BinaryTypes.String, Nullable: true}, + }, nil) + data, err := array.TableFromJSON(memory.DefaultAllocator, arrowSchema, []string{ + `[{"id": 1, "data": "a"}, {"id": 2, "data": "b"}, {"id": 3, "data": "c"}]`, + }) + require.NoError(t, err) + t.Cleanup(data.Release) + tbl, err = tbl.Append(ctx, array.NewTableReader(data, -1), nil) + require.NoError(t, err) + + _, records, err := tbl.NewIncrementalChangelogScan( + table.WithSelectedFields("id"), + table.WithLimit(2), + ).ToArrowRecords(ctx) + require.NoError(t, err) + got := collectChangelogRows(t, records) + require.Equal(t, []changelogRow{ + {id: 1, op: string(table.ChangelogOpInsert), ordinal: 0}, + {id: 2, op: string(table.ChangelogOpInsert), ordinal: 0}, + }, clearChangelogSnapshot(got)) +} + +func TestIncrementalChangelogReadReleasesCheckedAllocator(t *testing.T) { + tbl := newV3RowLineageTestTable(t) + arrowSchema := arrow.NewSchema([]arrow.Field{ + {Name: "id", Type: arrow.PrimitiveTypes.Int64, Nullable: false}, + {Name: "data", Type: arrow.BinaryTypes.String, Nullable: true}, + }, nil) + data, err := array.TableFromJSON(memory.DefaultAllocator, arrowSchema, []string{ + `[{"id": 1, "data": "a"}, {"id": 2, "data": "b"}, {"id": 3, "data": "c"}]`, + }) + require.NoError(t, err) + t.Cleanup(data.Release) + tbl, err = tbl.Append(t.Context(), array.NewTableReader(data, -1), nil) + require.NoError(t, err) + + read := func(t *testing.T, stopEarly bool) { + t.Helper() + + mem := memory.NewCheckedAllocator(memory.DefaultAllocator) + ctx := compute.WithAllocator(t.Context(), mem) + defer mem.AssertSize(t, 0) + + _, records, err := tbl.NewIncrementalChangelogScan( + table.WithSelectedFields("id"), + ).ToArrowRecords(ctx) + require.NoError(t, err) + for rec, err := range records { + require.NoError(t, err) + rec.Release() + if stopEarly { + break + } + } + } + + t.Run("full drain", func(t *testing.T) { read(t, false) }) + t.Run("early break", func(t *testing.T) { read(t, true) }) +} + +func TestIncrementalChangelogReadAppliesFileDeletes(t *testing.T) { + ctx := t.Context() + tbl := newV2RowLineageTestTable(t) + arrowSchema, err := table.SchemaToArrowSchema(tbl.Metadata().CurrentSchema(), nil, true, false) + require.NoError(t, err) + + dataPath := tbl.Location() + "/data/data-001.parquet" + writeParquetFile(t, dataPath, arrowSchema, `[ + {"id": 1, "data": "a"}, + {"id": 2, "data": "b"}, + {"id": 3, "data": "c"} + ]`) + posPath := tbl.Location() + "/data/pos-del.parquet" + writeParquetFile(t, posPath, table.PositionalDeleteArrowSchema, fmt.Sprintf(`[{"file_path": %q, "pos": 1}]`, dataPath)) + + tx := tbl.NewTransaction() + require.NoError(t, tx.NewRowDelta(nil). + AddRows(changelogDataFile(t, dataPath, 3)). + AddDeletes(changelogPosDeleteFile(t, posPath)). + Commit(ctx)) + tbl, err = tx.Commit(ctx) + require.NoError(t, err) + + replacementSchema := arrow.NewSchema([]arrow.Field{ + {Name: "id", Type: arrow.PrimitiveTypes.Int64, Nullable: false}, + {Name: "data", Type: arrow.BinaryTypes.String, Nullable: true}, + }, nil) + replacement, err := array.TableFromJSON(memory.DefaultAllocator, replacementSchema, []string{ + `[{"id": 4, "data": "d"}]`, + }) + require.NoError(t, err) + t.Cleanup(replacement.Release) + tbl, err = tbl.Overwrite(ctx, array.NewTableReader(replacement, -1), nil, table.WithOverwriteConcurrency(1)) + require.NoError(t, err) + + _, records, err := tbl.NewIncrementalChangelogScan().ToArrowRecords(ctx) + require.NoError(t, err) + got := collectChangelogRows(t, records) + require.Equal(t, []int64{1, 3}, changelogIDs(got, table.ChangelogOpInsert, 0)) + require.Equal(t, []int64{1, 3}, changelogIDs(got, table.ChangelogOpDelete, 1)) Review Comment: Added a read where the position delete is committed before `FromSnapshotExclusive` and the file is removed inside the range. Row 2 stays out of the DELETE. The deletion-vector case uses two vectors in one Puffin, replaces one of them before the range, and checks that both that row and the sibling's row stay out of the file delete. -- 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]
