badalprasadsingh commented on code in PR #2437: URL: https://github.com/apache/iceberg-go/pull/2437#discussion_r4484127727
########## table/statistics_write.go: ########## @@ -0,0 +1,63 @@ +// 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 ( + "fmt" + + "github.com/apache/iceberg-go" +) + +// SetStatistics registers a previously written Puffin statistics file with Review Comment: nit: `website/src/feature-status.md` still lists Statistics files (read / round-trip) please add registering and removing to that row. ########## table/metadata.go: ########## @@ -2009,10 +2009,13 @@ func (b *MetadataBuilder) SetStatistics(stats StatisticsFile) error { // RemoveStatistics removes the statistics file associated with the given snapshot ID. // It is not an error if no such file exists. func (b *MetadataBuilder) RemoveStatistics(snapshotID int64) error { + previousLen := len(b.statisticsList) b.statisticsList = slices.DeleteFunc(b.statisticsList, func(s StatisticsFile) bool { return s.SnapshotID == snapshotID }) - b.updates = append(b.updates, NewRemoveStatisticsUpdate(snapshotID)) + if len(b.statisticsList) != previousLen { + b.updates = append(b.updates, NewRemoveStatisticsUpdate(snapshotID)) + } Review Comment: `RemovePartitionStatistics` below still records an update when nothing was removed, so an absent file writes new metadata. we should make both no-ops. ########## table/statistics_write.go: ########## @@ -0,0 +1,63 @@ +// 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 ( + "fmt" + + "github.com/apache/iceberg-go" +) + +// SetStatistics registers a previously written Puffin statistics file with +// the file's snapshot. A file for the same snapshot replaces the previous +// reference. This only stages a metadata update; Commit publishes it, and +// the caller is responsible for writing a valid Puffin file beforehand. +func (t *Transaction) SetStatistics(stats StatisticsFile) error { + if _, err := t.txnMeta(); err != nil { + return err + } + if stats.StatisticsPath == "" || stats.FileSizeInBytes <= 0 || Review Comment: following iceberg-java is fine, but an unset SnapshotID still commits, and expiry never removes it. better: reject SnapshotID <= 0 with its own error message. ########## table/statistics_write_test.go: ########## @@ -0,0 +1,219 @@ +// 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 ( + "encoding/json" + "slices" + "testing" + + "github.com/apache/iceberg-go" + "github.com/apache/iceberg-go/puffin" + "github.com/stretchr/testify/require" +) + +func testStatisticsRegistrationFile() StatisticsFile { + return StatisticsFile{ + SnapshotID: 103, + StatisticsPath: "s3://warehouse/db/stats/metadata/sketch.puffin", + FileSizeInBytes: 1024, + FileFooterSizeInBytes: 128, + BlobMetadata: []BlobMetadata{{ + Type: puffin.BlobTypeDataSketchesTheta, + SnapshotID: 103, + SequenceNumber: 3, + Fields: []int32{1}, + Properties: map[string]string{"creator": "go"}, + }}, + } +} + +func TestTransactionStatisticsRegistration(t *testing.T) { Review Comment: no test commits a registration through a catalog please add one that commits, reloads and checks the snapshot is unchanged ########## table/statistics_write.go: ########## @@ -0,0 +1,63 @@ +// 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 ( + "fmt" + + "github.com/apache/iceberg-go" +) + +// SetStatistics registers a previously written Puffin statistics file with +// the file's snapshot. A file for the same snapshot replaces the previous +// reference. This only stages a metadata update; Commit publishes it, and +// the caller is responsible for writing a valid Puffin file beforehand. +func (t *Transaction) SetStatistics(stats StatisticsFile) error { + if _, err := t.txnMeta(); err != nil { + return err + } + if stats.StatisticsPath == "" || stats.FileSizeInBytes <= 0 || + stats.FileFooterSizeInBytes <= 0 || stats.FileFooterSizeInBytes > stats.FileSizeInBytes { + return fmt.Errorf("%w: statistics path must be nonempty, and file and footer sizes must be positive with footer size at most file size", + iceberg.ErrInvalidArgument) + } + if stats.BlobMetadata == nil { + return fmt.Errorf("%w: statistics blob-metadata must be a list", iceberg.ErrInvalidArgument) + } + for idx, blob := range stats.BlobMetadata { + if blob.Type == "" || blob.Fields == nil { + return fmt.Errorf("%w: statistics blob %d requires a type and fields list", + iceberg.ErrInvalidArgument, idx) + } + } + // A transaction may be committed after the caller has reused its buffers. + // Keep the staged update independent of the caller's maps and slices. + registered := cloneStatisticsFiles([]StatisticsFile{stats})[0] + + return t.apply([]Update{NewSetStatisticsUpdate(registered)}, nil) +} + +// RemoveStatistics removes the statistics file reference for snapshotID. An +// absent reference is not an error. The physical Puffin file is not deleted. +func (t *Transaction) RemoveStatistics(snapshotID int64) error { + if _, err := t.txnMeta(); err != nil { + return err + } Review Comment: `apply` already checks this under the lock drop it here and in `SetStatistics`, it reads t.meta unlocked, so concurrent calls race. ########## table/statistics_write.go: ########## @@ -0,0 +1,63 @@ +// 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 ( + "fmt" + + "github.com/apache/iceberg-go" +) + +// SetStatistics registers a previously written Puffin statistics file with +// the file's snapshot. A file for the same snapshot replaces the previous +// reference. This only stages a metadata update; Commit publishes it, and +// the caller is responsible for writing a valid Puffin file beforehand. Review Comment: `FileFooterSizeInBytes` is required, but `puffin.Writer` exposes no file or footer size, so callers count bytes by hand. please add accessors, or file an issue? ########## table/statistics_write_test.go: ########## @@ -0,0 +1,279 @@ +// 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 ( + "encoding/json" + "slices" + "testing" + + "github.com/apache/iceberg-go" + "github.com/apache/iceberg-go/puffin" + "github.com/stretchr/testify/require" +) + +func testStatisticsRegistrationFile() StatisticsFile { + return StatisticsFile{ + SnapshotID: 103, + StatisticsPath: "s3://warehouse/db/stats/metadata/sketch.puffin", + FileSizeInBytes: 1024, + FileFooterSizeInBytes: 128, + BlobMetadata: []BlobMetadata{{ + Type: puffin.BlobTypeDataSketchesTheta, + SnapshotID: 103, + SequenceNumber: 3, + Fields: []int32{1}, + Properties: map[string]string{"creator": "go"}, + }}, + } +} + +func TestTransactionStatisticsRegistration(t *testing.T) { + tbl := inspectTestTable(Identifier{"db", "stats"}, map[string]SnapshotRef{ + MainBranch: {SnapshotID: 103, Type: BranchRef}, + }) + tx := tbl.NewTransaction() + stats := testStatisticsRegistrationFile() + + require.NoError(t, tx.SetStatistics(stats)) + meta, err := tx.meta.Build() + require.NoError(t, err) + require.Equal(t, []StatisticsFile{stats}, slices.Collect(meta.Statistics())) + + // Removing another snapshot must not stage a metadata update. + stagedUpdateCount := len(tx.meta.updates) + require.NoError(t, tx.RemoveStatistics(999)) + require.Len(t, tx.meta.updates, stagedUpdateCount) + meta, err = tx.meta.Build() + require.NoError(t, err) + require.Equal(t, []StatisticsFile{stats}, slices.Collect(meta.Statistics())) + + replacement := stats + replacement.StatisticsPath = "s3://warehouse/db/stats/metadata/replaced.puffin" + replacement.FileSizeInBytes = 2048 + require.NoError(t, tx.SetStatistics(replacement)) + meta, err = tx.meta.Build() + require.NoError(t, err) + require.Equal(t, []StatisticsFile{replacement}, slices.Collect(meta.Statistics())) + + require.NoError(t, tx.RemoveStatistics(stats.SnapshotID)) + meta, err = tx.meta.Build() + require.NoError(t, err) + require.Empty(t, slices.Collect(meta.Statistics())) + + // Java's removeStatistics is idempotent if no reference exists. + updateCount := len(tx.meta.updates) + require.NoError(t, tx.RemoveStatistics(stats.SnapshotID)) + require.NoError(t, tx.RemoveStatistics(999)) + require.Len(t, tx.meta.updates, updateCount) +} + +func TestTransactionStatisticsMetadataRoundTrip(t *testing.T) { + base, err := getTestTableMetadata("TableMetadataV2Valid.json") + require.NoError(t, err) + require.NotEmpty(t, base.Snapshots()) + tbl := New(Identifier{"db", "stats"}, base, "", nil, nil) + tx := tbl.NewTransaction() + stats := testStatisticsRegistrationFile() + stats.SnapshotID = base.Snapshots()[0].SnapshotID + stats.BlobMetadata[0].SnapshotID = stats.SnapshotID + stats.BlobMetadata[0].SequenceNumber = base.Snapshots()[0].SequenceNumber + require.NoError(t, tx.SetStatistics(stats)) + + meta, err := tx.meta.Build() + require.NoError(t, err) + encoded, err := json.Marshal(meta) + require.NoError(t, err) + parsed, err := ParseMetadataBytes(encoded) + require.NoError(t, err) + require.Equal(t, []StatisticsFile{stats}, slices.Collect(parsed.Statistics())) +} + +func TestTransactionSetStatisticsClonesCallerData(t *testing.T) { + tbl := inspectTestTable(Identifier{"db", "stats"}, map[string]SnapshotRef{ + MainBranch: {SnapshotID: 103, Type: BranchRef}, + }) + tx := tbl.NewTransaction() + stats := testStatisticsRegistrationFile() + keyMetadata := "first" + stats.KeyMetadata = &keyMetadata + require.NoError(t, tx.SetStatistics(stats)) + + keyMetadata = "changed" + stats.BlobMetadata[0].Fields[0] = 99 + stats.BlobMetadata[0].Properties["creator"] = "changed" + meta, err := tx.meta.Build() + require.NoError(t, err) + registered := slices.Collect(meta.Statistics()) + require.Len(t, registered, 1) + require.Equal(t, "first", *registered[0].KeyMetadata) + require.Equal(t, []int32{1}, registered[0].BlobMetadata[0].Fields) + require.Equal(t, "go", registered[0].BlobMetadata[0].Properties["creator"]) +} + +func TestTransactionSetStatisticsRejectsInvalidFiles(t *testing.T) { + tbl := inspectTestTable(Identifier{"db", "stats"}, map[string]SnapshotRef{ + MainBranch: {SnapshotID: 103, Type: BranchRef}, + }) + cases := []struct { + name string + change func(*StatisticsFile) + }{ + {"missing path", func(s *StatisticsFile) { s.StatisticsPath = "" }}, + {"nonpositive file size", func(s *StatisticsFile) { s.FileSizeInBytes = 0 }}, + {"nonpositive footer size", func(s *StatisticsFile) { s.FileFooterSizeInBytes = 0 }}, + {"footer exceeds file", func(s *StatisticsFile) { s.FileFooterSizeInBytes = 1025 }}, + {"empty blob type", func(s *StatisticsFile) { s.BlobMetadata[0].Type = "" }}, Review Comment: fixtures have one blob, and no case uses a negative size. check: a bad second blob and negative sizes fail. -- 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]
