laskoviymishka commented on code in PR #2776: URL: https://github.com/apache/iceberg-go/pull/2776#discussion_r4755265703
########## table/stats_writer.go: ########## @@ -0,0 +1,140 @@ +// 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" + "slices" + "strconv" + + "github.com/apache/iceberg-go/puffin" + "github.com/google/uuid" +) + +// ndvBlobProperty is the blob-metadata property key under which the estimated +// number of distinct values is recorded, matching Iceberg-Java. +const ndvBlobProperty = "ndv" + +// WriteStatistics computes NDV Theta sketches for a snapshot, writes them to a +// Puffin statistics file, registers the file via SetStatistics, and commits. +// The updated table is returned. +// +// When snapshotID is 0 the current snapshot is used. When columns is empty all +// top-level primitive columns are analyzed. If the snapshot has no primitive +// columns the table is returned unchanged. +func (t Table) WriteStatistics(ctx context.Context, snapshotID int64, columns ...string) (*Table, error) { + snap := t.CurrentSnapshot() + if snapshotID != 0 { + snap = t.SnapshotByID(snapshotID) + } + if snap == nil { Review Comment: Should a table with no snapshot be a no-op here, like `ComputeNDVs`? It returns an empty result for a nil snapshot ([stats_ndv.go#L73-L75](https://github.com/apache/iceberg-go/blob/3b5eb60c3a5325fbf5db255d3e9d49717314d89d/table/stats_ndv.go#L73-L75)), as Java's ComputeTableStats does, while this returns `ErrInvalidOperation`. The same branch also catches an unknown explicit `snapshotID`, where `ResolveSnapshot` would have said `snapshot not found: <id>` ([scanner.go#L663-L670](https://github.com/apache/iceberg-go/blob/3b5eb60c3a5325fbf5db255d3e9d49717314d89d/table/scanner.go#L663-L670)). Since this is new public API, it's cheaper to settle both now. ########## table/stats_writer.go: ########## @@ -0,0 +1,140 @@ +// 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" + "slices" + "strconv" + + "github.com/apache/iceberg-go/puffin" + "github.com/google/uuid" +) + +// ndvBlobProperty is the blob-metadata property key under which the estimated +// number of distinct values is recorded, matching Iceberg-Java. +const ndvBlobProperty = "ndv" + +// WriteStatistics computes NDV Theta sketches for a snapshot, writes them to a +// Puffin statistics file, registers the file via SetStatistics, and commits. +// The updated table is returned. +// +// When snapshotID is 0 the current snapshot is used. When columns is empty all +// top-level primitive columns are analyzed. If the snapshot has no primitive +// columns the table is returned unchanged. +func (t Table) WriteStatistics(ctx context.Context, snapshotID int64, columns ...string) (*Table, error) { + snap := t.CurrentSnapshot() + if snapshotID != 0 { + snap = t.SnapshotByID(snapshotID) + } + if snap == nil { + return nil, fmt.Errorf("%w: no snapshot to compute statistics for", ErrInvalidOperation) + } + + ndv, err := t.ComputeNDVs(ctx, snap.SnapshotID, columns...) + if err != nil { + return nil, err + } + if len(ndv.Columns) == 0 { + return &t, nil + } + + statsFile, err := t.writeStatisticsFile(ctx, ndv.Snapshot, ndv.Columns) + if err != nil { + return nil, err + } + + tx := t.NewTransaction() + if err := tx.SetStatistics(*statsFile); err != nil { + return nil, err + } + + return tx.Commit(ctx) Review Comment: Could we best-effort remove the `.stats` file when `SetStatistics` or `Commit` fails? The file is written under a fresh UUID before staging, so a failed commit leaves it unreferenced and each retry adds another. `partition_statistics_write.go` already does `_ = wfs.Remove(context.WithoutCancel(ctx), path)` on a staging failure ([L232-L236](https://github.com/apache/iceberg-go/blob/3b5eb60c3a5325fbf5db255d3e9d49717314d89d/table/partition_statistics_write.go#L232-L236)), and `IO` already has `Remove`. ########## table/stats_writer.go: ########## @@ -0,0 +1,140 @@ +// 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" + "slices" + "strconv" + + "github.com/apache/iceberg-go/puffin" + "github.com/google/uuid" +) + +// ndvBlobProperty is the blob-metadata property key under which the estimated +// number of distinct values is recorded, matching Iceberg-Java. +const ndvBlobProperty = "ndv" + +// WriteStatistics computes NDV Theta sketches for a snapshot, writes them to a +// Puffin statistics file, registers the file via SetStatistics, and commits. +// The updated table is returned. +// +// When snapshotID is 0 the current snapshot is used. When columns is empty all +// top-level primitive columns are analyzed. If the snapshot has no primitive +// columns the table is returned unchanged. +func (t Table) WriteStatistics(ctx context.Context, snapshotID int64, columns ...string) (*Table, error) { + snap := t.CurrentSnapshot() + if snapshotID != 0 { + snap = t.SnapshotByID(snapshotID) + } + if snap == nil { + return nil, fmt.Errorf("%w: no snapshot to compute statistics for", ErrInvalidOperation) + } + + ndv, err := t.ComputeNDVs(ctx, snap.SnapshotID, columns...) + if err != nil { + return nil, err + } + if len(ndv.Columns) == 0 { + return &t, nil + } + + statsFile, err := t.writeStatisticsFile(ctx, ndv.Snapshot, ndv.Columns) + if err != nil { + return nil, err + } + + tx := t.NewTransaction() + if err := tx.SetStatistics(*statsFile); err != nil { + return nil, err + } + + return tx.Commit(ctx) +} + +// writeStatisticsFile assembles the sketches into a Puffin file, persists it via +// the table's FileIO, and returns the StatisticsFile describing it. +func (t Table) writeStatisticsFile(ctx context.Context, snap *Snapshot, results map[int]NDVResult) (*StatisticsFile, error) { + fs, err := t.FS(ctx) + if err != nil { + return nil, err Review Comment: The errors from `t.FS`, `requireWriteFileIO`, `LocationProvider` and `puffin.NewWriter` here, and from `SetStatistics` above, come back bare, while the later ones carry `write statistics:`. Could we wrap these the same way? ########## table/stats_writer_test.go: ########## @@ -0,0 +1,87 @@ +// 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 ( + "bytes" + "io" + "strconv" + "testing" + + "github.com/apache/iceberg-go/puffin" + "github.com/apache/iceberg-go/table" + "github.com/apache/iceberg-go/table/stats" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestWriteStatisticsEndToEnd(t *testing.T) { + tbl, ctx := ndvTestTable(t, "stats_write") + + updated, err := tbl.WriteStatistics(ctx, 0) + require.NoError(t, err) + + snap := updated.CurrentSnapshot() + require.NotNil(t, snap) + + var files []table.StatisticsFile + for sf := range updated.Metadata().Statistics() { + files = append(files, sf) + } + require.Len(t, files, 1) + + sf := files[0] + assert.Equal(t, snap.SnapshotID, sf.SnapshotID) + require.Len(t, sf.BlobMetadata, 2) + assert.Positive(t, sf.FileSizeInBytes) + assert.Positive(t, sf.FileFooterSizeInBytes) + + // Every blob targets the analyzed snapshot and records an ndv property. + for _, bm := range sf.BlobMetadata { + assert.Equal(t, puffin.BlobTypeDataSketchesTheta, bm.Type) + assert.Equal(t, snap.SnapshotID, bm.SnapshotID) + assert.Contains(t, bm.Properties, "ndv") Review Comment: Could this check `sf.BlobMetadata` against the footer blobs from `r.Blobs()` field by field? `writeStatisticsFile` copies `SequenceNumber` and `Fields` by hand ([stats_writer.go#L116-L122](https://github.com/apache/iceberg-go/blob/3b5eb60c3a5325fbf5db255d3e9d49717314d89d/table/stats_writer.go#L116-L122)), and nothing asserts them on the registered side. A second call with an explicit `snapshotID` would cover the non-default path too. ########## table/stats_writer.go: ########## @@ -0,0 +1,140 @@ +// 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" + "slices" + "strconv" + + "github.com/apache/iceberg-go/puffin" + "github.com/google/uuid" +) + +// ndvBlobProperty is the blob-metadata property key under which the estimated +// number of distinct values is recorded, matching Iceberg-Java. +const ndvBlobProperty = "ndv" + +// WriteStatistics computes NDV Theta sketches for a snapshot, writes them to a +// Puffin statistics file, registers the file via SetStatistics, and commits. +// The updated table is returned. +// +// When snapshotID is 0 the current snapshot is used. When columns is empty all +// top-level primitive columns are analyzed. If the snapshot has no primitive +// columns the table is returned unchanged. +func (t Table) WriteStatistics(ctx context.Context, snapshotID int64, columns ...string) (*Table, error) { + snap := t.CurrentSnapshot() + if snapshotID != 0 { + snap = t.SnapshotByID(snapshotID) + } + if snap == nil { + return nil, fmt.Errorf("%w: no snapshot to compute statistics for", ErrInvalidOperation) + } + + ndv, err := t.ComputeNDVs(ctx, snap.SnapshotID, columns...) + if err != nil { + return nil, err + } + if len(ndv.Columns) == 0 { + return &t, nil + } + + statsFile, err := t.writeStatisticsFile(ctx, ndv.Snapshot, ndv.Columns) + if err != nil { + return nil, err + } + + tx := t.NewTransaction() + if err := tx.SetStatistics(*statsFile); err != nil { + return nil, err + } + + return tx.Commit(ctx) +} + +// writeStatisticsFile assembles the sketches into a Puffin file, persists it via +// the table's FileIO, and returns the StatisticsFile describing it. +func (t Table) writeStatisticsFile(ctx context.Context, snap *Snapshot, results map[int]NDVResult) (*StatisticsFile, error) { + fs, err := t.FS(ctx) + if err != nil { + return nil, err + } + wfs, err := requireWriteFileIO(fs) + if err != nil { + return nil, err + } + + lp, err := t.LocationProvider() + if err != nil { + return nil, err + } + path := lp.NewMetadataLocation(fmt.Sprintf("%s-%d.stats", uuid.NewString(), snap.SnapshotID)) Review Comment: Java names this file `<snapshotId>-<uuid>.stats` ([ComputeTableStatsSparkAction.java#L174-L178](https://github.com/apache/iceberg/blob/3bfa3e47a4bc75df4e747d048fdc0f340e4c1f00/spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/actions/ComputeTableStatsSparkAction.java#L174-L178)). The spec doesn't fix a name, but swapping the order is a one-liner, and doing it now is cheaper than after the later parts of #1929 land. ```suggestion path := lp.NewMetadataLocation(fmt.Sprintf("%d-%s.stats", snap.SnapshotID, uuid.NewString())) ``` -- 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]
