laskoviymishka commented on code in PR #2120: URL: https://github.com/apache/iceberg-go/pull/2120#discussion_r4193427772
########## table/inspect_partitions_snapshot_history_bench_test.go: ########## @@ -0,0 +1,117 @@ +// 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 ( + "bytes" + "context" + "fmt" + "strconv" + "testing" + + "github.com/apache/iceberg-go" + iceio "github.com/apache/iceberg-go/io" + "github.com/stretchr/testify/require" +) + +func BenchmarkInspectPartitionsSnapshotHistory(b *testing.B) { + for _, count := range []int{0, 1, 100, 1000} { + b.Run(fmt.Sprintf("snapshots=%d", count), func(b *testing.B) { + tbl := inspectPartitionSnapshotHistoryTable(b, count) + inspector := tbl.Inspect() + ctx := context.Background() + if snapshot := tbl.metadata.CurrentSnapshot(); snapshot != nil { + _, err := tbl.manifestSet(ctx, *snapshot) + require.NoError(b, err) + } + b.ReportAllocs() + for b.Loop() { + rr, err := inspector.Partitions(ctx) + if err != nil { + b.Fatal(err) + } + var rows int64 + for rr.Next() { + rows += rr.RecordBatch().NumRows() + } + err = rr.Err() + rr.Release() + if err != nil { + b.Fatal(err) + } + wantRows := int64(min(count, 1)) + if rows != wantRows { + b.Fatalf("got %d rows, want %d", rows, wantRows) + } + } + }) + } +} + +func inspectPartitionSnapshotHistoryTable(t testing.TB, count int) *Table { + t.Helper() + spec := partitionedSpec() + schema := simpleSchema() + meta, err := NewMetadata(schema, &spec, UnsortedSortOrder, "mem://snapshot-history/table", nil) + require.NoError(t, err) + builder, err := MetadataBuilderFromBase(meta, "metadata.json") + require.NoError(t, err) + fs := iceio.NewMemFS() + builder.snapshotList = make([]Snapshot, count) + for index := range count { + id := int64(index + 1) + builder.snapshotList[index] = Snapshot{ + SnapshotID: id, SequenceNumber: id, TimestampMs: id * 1000, + ManifestList: fmt.Sprintf("mem://snapshot-history/table/metadata/snap-%d.avro", id), + SchemaID: new(schema.ID), + Summary: &Summary{Operation: OpAppend, Properties: iceberg.Properties{ + "added-data-files": "1", "added-records": "1", "total-records": strconv.FormatInt(id, 10), + }}, + } + if index > 0 { + builder.snapshotList[index].ParentSnapshotID = new(id - 1) Review Comment: `new(id - 1)` here (and `new(schema.ID)` a few lines up) is the Go 1.26 `new`-of-expression builtin. origin/main is on the 1.26 go.mod and already uses this form, but if the PR branch's CI matrix still runs 1.25.8 these two files won't compile there. Can you confirm the branch is rebased onto the 1.26 go.mod? ########## table/inspect_partitions_snapshot_history_bench_test.go: ########## @@ -0,0 +1,117 @@ +// 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 ( + "bytes" + "context" + "fmt" + "strconv" + "testing" + + "github.com/apache/iceberg-go" + iceio "github.com/apache/iceberg-go/io" + "github.com/stretchr/testify/require" +) + +func BenchmarkInspectPartitionsSnapshotHistory(b *testing.B) { + for _, count := range []int{0, 1, 100, 1000} { + b.Run(fmt.Sprintf("snapshots=%d", count), func(b *testing.B) { + tbl := inspectPartitionSnapshotHistoryTable(b, count) + inspector := tbl.Inspect() + ctx := context.Background() + if snapshot := tbl.metadata.CurrentSnapshot(); snapshot != nil { + _, err := tbl.manifestSet(ctx, *snapshot) + require.NoError(b, err) + } + b.ReportAllocs() + for b.Loop() { + rr, err := inspector.Partitions(ctx) + if err != nil { + b.Fatal(err) + } + var rows int64 + for rr.Next() { + rows += rr.RecordBatch().NumRows() + } + err = rr.Err() + rr.Release() + if err != nil { + b.Fatal(err) + } + wantRows := int64(min(count, 1)) + if rows != wantRows { + b.Fatalf("got %d rows, want %d", rows, wantRows) + } + } + }) + } +} + +func inspectPartitionSnapshotHistoryTable(t testing.TB, count int) *Table { + t.Helper() + spec := partitionedSpec() + schema := simpleSchema() + meta, err := NewMetadata(schema, &spec, UnsortedSortOrder, "mem://snapshot-history/table", nil) + require.NoError(t, err) + builder, err := MetadataBuilderFromBase(meta, "metadata.json") + require.NoError(t, err) + fs := iceio.NewMemFS() + builder.snapshotList = make([]Snapshot, count) + for index := range count { + id := int64(index + 1) + builder.snapshotList[index] = Snapshot{ + SnapshotID: id, SequenceNumber: id, TimestampMs: id * 1000, + ManifestList: fmt.Sprintf("mem://snapshot-history/table/metadata/snap-%d.avro", id), + SchemaID: new(schema.ID), + Summary: &Summary{Operation: OpAppend, Properties: iceberg.Properties{ + "added-data-files": "1", "added-records": "1", "total-records": strconv.FormatInt(id, 10), + }}, + } + if index > 0 { + builder.snapshotList[index].ParentSnapshotID = new(id - 1) + } + } + if count > 0 { + currentID := int64(count) + entryID := max(int64(1), currentID/2) Review Comment: The EXISTING entry is keyed to `entryID` (count/2) while the manifest list is written under `currentID`, so the row resolves its timestamp from a historical snapshot rather than the current one. That's the actual point of the test, but it reads like a bug at first glance, so it's worth a one-line comment. ########## table/inspect_partitions_snapshot_history_test.go: ########## @@ -0,0 +1,73 @@ +// 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 ( + "testing" + + "github.com/apache/arrow-go/v18/arrow/array" + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/stretchr/testify/require" +) + +type countingPartitionSnapshotsMetadata struct { + Metadata + calls int +} + +func (m *countingPartitionSnapshotsMetadata) Snapshots() []Snapshot { + m.calls++ + + return m.Metadata.Snapshots() +} + +func TestInspectPartitionsReadsSnapshotHistoryOnce(t *testing.T) { + t.Parallel() + for _, tt := range []struct { + name string + count, calls int + }{ + {"no snapshot", 0, 0}, + {"one snapshot", 1, 1}, + {"historical snapshot", 4, 1}, + } { + t.Run(tt.name, func(t *testing.T) { + tbl := inspectPartitionSnapshotHistoryTable(t, tt.count) + before := tbl.metadata.Snapshots() + counted := &countingPartitionSnapshotsMetadata{Metadata: tbl.metadata} + tbl.metadata = counted + mem := memory.NewCheckedAllocator(memory.DefaultAllocator) + defer mem.AssertSize(t, 0) + rr, err := tbl.Inspect(WithInspectAllocator(mem)).Partitions(t.Context()) + require.NoError(t, err) + defer rr.Release() + record := collectRecord(t, rr) + defer record.Release() + require.Equal(t, tt.calls, counted.calls) + if tt.count == 0 { + require.Zero(t, record.NumRows()) + } else { + require.EqualValues(t, 1, record.NumRows()) + entryID := max(int64(1), int64(tt.count)/2) + require.EqualValues(t, entryID*1000*1000, record.Column(9).(*array.Timestamp).Value(0)) + require.Equal(t, entryID, record.Column(10).(*array.Int64).Value(0)) + } + require.Equal(t, before, counted.Metadata.Snapshots()) Review Comment: This `before` comparison is near-tautological since the hoist only ever reads the slice and never mutates it. I'd drop it, or add a word on what it's meant to guard against. ########## table/inspect_partitions_snapshot_history_test.go: ########## @@ -0,0 +1,73 @@ +// 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 ( + "testing" + + "github.com/apache/arrow-go/v18/arrow/array" + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/stretchr/testify/require" +) + +type countingPartitionSnapshotsMetadata struct { + Metadata + calls int +} + +func (m *countingPartitionSnapshotsMetadata) Snapshots() []Snapshot { + m.calls++ + + return m.Metadata.Snapshots() +} + +func TestInspectPartitionsReadsSnapshotHistoryOnce(t *testing.T) { + t.Parallel() + for _, tt := range []struct { + name string + count, calls int + }{ + {"no snapshot", 0, 0}, + {"one snapshot", 1, 1}, + {"historical snapshot", 4, 1}, + } { + t.Run(tt.name, func(t *testing.T) { + tbl := inspectPartitionSnapshotHistoryTable(t, tt.count) + before := tbl.metadata.Snapshots() + counted := &countingPartitionSnapshotsMetadata{Metadata: tbl.metadata} + tbl.metadata = counted + mem := memory.NewCheckedAllocator(memory.DefaultAllocator) + defer mem.AssertSize(t, 0) + rr, err := tbl.Inspect(WithInspectAllocator(mem)).Partitions(t.Context()) + require.NoError(t, err) + defer rr.Release() + record := collectRecord(t, rr) + defer record.Release() + require.Equal(t, tt.calls, counted.calls) Review Comment: I'd drop the exact call-count assertion, along with the `calls` field and the 0/1 expectations. It's white-box: it breaks the moment `manifestSet`/`Inspect` or a later refactor reads `Snapshots()` again or switches to `SnapshotByID`, even when behavior is unchanged. The value assertions below are the coverage worth keeping; if you want a guard against re-copying, `testing.AllocsPerRun` with a loose bound says more than a call count. ########## table/inspect_partitions_snapshot_history_bench_test.go: ########## @@ -0,0 +1,117 @@ +// 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 ( + "bytes" + "context" + "fmt" + "strconv" + "testing" + + "github.com/apache/iceberg-go" + iceio "github.com/apache/iceberg-go/io" + "github.com/stretchr/testify/require" +) + +func BenchmarkInspectPartitionsSnapshotHistory(b *testing.B) { + for _, count := range []int{0, 1, 100, 1000} { + b.Run(fmt.Sprintf("snapshots=%d", count), func(b *testing.B) { + tbl := inspectPartitionSnapshotHistoryTable(b, count) + inspector := tbl.Inspect() + ctx := context.Background() + if snapshot := tbl.metadata.CurrentSnapshot(); snapshot != nil { + _, err := tbl.manifestSet(ctx, *snapshot) + require.NoError(b, err) + } + b.ReportAllocs() + for b.Loop() { + rr, err := inspector.Partitions(ctx) + if err != nil { + b.Fatal(err) + } + var rows int64 + for rr.Next() { + rows += rr.RecordBatch().NumRows() + } + err = rr.Err() + rr.Release() + if err != nil { + b.Fatal(err) + } + wantRows := int64(min(count, 1)) + if rows != wantRows { + b.Fatalf("got %d rows, want %d", rows, wantRows) + } + } + }) + } +} + +func inspectPartitionSnapshotHistoryTable(t testing.TB, count int) *Table { + t.Helper() + spec := partitionedSpec() + schema := simpleSchema() + meta, err := NewMetadata(schema, &spec, UnsortedSortOrder, "mem://snapshot-history/table", nil) + require.NoError(t, err) + builder, err := MetadataBuilderFromBase(meta, "metadata.json") + require.NoError(t, err) + fs := iceio.NewMemFS() + builder.snapshotList = make([]Snapshot, count) Review Comment: This builds the fixture by writing `builder.snapshotList`, `currentSnapshotID` and `lastSequenceNumber` directly, which skips `AddSnapshot`/`SetSnapshotRef`, so the result has no `main` ref or snapshot-log and isn't the table you'd get from the normal path. I'd either go through the public builder methods, or if the shortcut is deliberate (cheap 1000-snapshot fixture) leave a one-line comment saying so. As-is it's fragile against any builder invariant added later. ########## table/inspect_partitions_snapshot_history_test.go: ########## @@ -0,0 +1,73 @@ +// 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 ( + "testing" + + "github.com/apache/arrow-go/v18/arrow/array" + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/stretchr/testify/require" +) + +type countingPartitionSnapshotsMetadata struct { + Metadata + calls int +} + +func (m *countingPartitionSnapshotsMetadata) Snapshots() []Snapshot { + m.calls++ + + return m.Metadata.Snapshots() +} + +func TestInspectPartitionsReadsSnapshotHistoryOnce(t *testing.T) { + t.Parallel() + for _, tt := range []struct { + name string + count, calls int + }{ + {"no snapshot", 0, 0}, + {"one snapshot", 1, 1}, + {"historical snapshot", 4, 1}, + } { + t.Run(tt.name, func(t *testing.T) { + tbl := inspectPartitionSnapshotHistoryTable(t, tt.count) + before := tbl.metadata.Snapshots() + counted := &countingPartitionSnapshotsMetadata{Metadata: tbl.metadata} + tbl.metadata = counted + mem := memory.NewCheckedAllocator(memory.DefaultAllocator) + defer mem.AssertSize(t, 0) + rr, err := tbl.Inspect(WithInspectAllocator(mem)).Partitions(t.Context()) + require.NoError(t, err) + defer rr.Release() + record := collectRecord(t, rr) + defer record.Release() + require.Equal(t, tt.calls, counted.calls) + if tt.count == 0 { + require.Zero(t, record.NumRows()) + } else { + require.EqualValues(t, 1, record.NumRows()) + entryID := max(int64(1), int64(tt.count)/2) + require.EqualValues(t, entryID*1000*1000, record.Column(9).(*array.Timestamp).Value(0)) Review Comment: `Column(9)`/`Column(10)` and `entryID*1000*1000` are opaque, and a reorder of the partitions schema would break them silently. I'd look the columns up by name (`FieldIndices("last_updated_at")`) and name the ms-to-µs conversion. -- 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]
