laskoviymishka commented on code in PR #2644: URL: https://github.com/apache/iceberg-go/pull/2644#discussion_r4641934479
########## manifest_deflate.go: ########## @@ -0,0 +1,94 @@ +// 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 iceberg + +import ( + "bytes" + "compress/flate" + "fmt" + "io" + "math" + + "github.com/twmb/avro/ocf" +) + +// manifestDeflateCodec reuses the inflater and output buffer across blocks of +// one manifest. The avro decoder copies strings and bytes out of the block unless +// avro.AliasInput is set, and ocf.Reader drops that option, so the output buffer +// can be overwritten on the next block. Review Comment: Could the comment also say one instance serves one reader? The inflater and buffer have no locking ([manifest_deflate.go#L33-L38](https://github.com/apache/iceberg-go/blob/da59f68e6306ca7029161f68e7681ce4f1f02645/manifest_deflate.go#L33-L38)). That's fine today because `newManifestReader` builds a fresh codec per reader ([manifest.go#L716](https://github.com/apache/iceberg-go/blob/da59f68e6306ca7029161f68e7681ce4f1f02645/manifest.go#L716)). ```suggestion // can be overwritten on the next block. Each reader needs its own instance. ``` ########## manifest_deflate_test.go: ########## @@ -0,0 +1,201 @@ +// 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 iceberg + +import ( + "bytes" + "io" + "strings" + "testing" + + "github.com/stretchr/testify/require" + "github.com/twmb/avro/ocf" +) + +type countingManifestDeflateCodec struct { + *manifestDeflateCodec + decompressions int +} + +func (c *countingManifestDeflateCodec) DecompressBounded(src []byte, maxBytes int64) ([]byte, error) { + c.decompressions++ + + return c.manifestDeflateCodec.DecompressBounded(src, maxBytes) +} + +func TestManifestReaderKeepsDeflateEntriesFromEarlierBlocks(t *testing.T) { + schema := NewSchema(1, NestedField{ID: 1, Name: "id", Type: PrimitiveTypes.Int64, Required: true}) + snapshotID := int64(1) + // Each path exceeds the writer's 64 KiB block threshold. + paths := []string{strings.Repeat("first", 14<<10), strings.Repeat("other", 14<<10)} + bounds := [][]byte{{1}, {2}} + entries := make([]ManifestEntry, len(paths)) + for i, path := range paths { + builder, err := NewDataFileBuilder(*UnpartitionedSpec, EntryContentData, path, ParquetFile, + nil, nil, nil, 1, 128) + require.NoError(t, err) + builder.LowerBoundValues(map[int][]byte{1: bounds[i]}) + entries[i] = NewManifestEntry(EntryStatusADDED, &snapshotID, nil, nil, builder.Build()) + } + + var encoded bytes.Buffer + manifest, err := WriteManifest("manifest.avro", &encoded, 2, *UnpartitionedSpec, schema, snapshotID, entries) + require.NoError(t, err) + + // Prove that the test exercises buffer reuse across two compressed blocks. + countingCodec := &countingManifestDeflateCodec{manifestDeflateCodec: newManifestDeflateCodec()} + blockReader, err := ocf.NewReader(bytes.NewReader(encoded.Bytes()), ocf.WithCodec(countingCodec)) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, blockReader.Close()) }) + for range entries { + var decoded any + require.NoError(t, blockReader.Decode(&decoded)) + } + var extra any + require.ErrorIs(t, blockReader.Decode(&extra), io.EOF) + require.Equal(t, len(entries), countingCodec.decompressions) + + for _, test := range []struct { + name string + projection *ManifestEntryProjection + }{ + {name: "full"}, + {name: "projected", projection: &ManifestEntryProjection{IncludePruningStats: true}}, + } { + t.Run(test.name, func(t *testing.T) { + reader, err := newManifestReader(manifest, bytes.NewReader(encoded.Bytes()), test.projection) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, reader.Close()) }) + + first, err := reader.ReadEntry() + require.NoError(t, err) + second, err := reader.ReadEntry() + require.NoError(t, err) + _, err = reader.ReadEntry() + require.ErrorIs(t, err, io.EOF) + + for i, entry := range []ManifestEntry{first, second} { + require.Equal(t, paths[i], entry.DataFile().FilePath()) + require.Equal(t, bounds[i], entry.DataFile().LowerBoundValues()[1]) + } + }) + } +} + +func TestManifestDeflateCodecRejectsOversizedAndTruncatedBlocks(t *testing.T) { + codec := newManifestDeflateCodec() + defer codec.Close() Review Comment: Could this one and the one at line 119 use the `t.Cleanup` form the other tests in this file use ([L139-L141](https://github.com/apache/iceberg-go/blob/da59f68e6306ca7029161f68e7681ce4f1f02645/manifest_deflate_test.go#L139-L141))? `Close` always returns nil today, so it's only for consistency. ```suggestion t.Cleanup(func() { require.NoError(t, codec.Close()) }) ``` -- 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]
