-
Notifications
You must be signed in to change notification settings - Fork 221
fix(manifest): protect DataFile metadata from external mutation #1575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4e7f842
3b006be
c2928e9
ecbe15b
3ee1c2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // 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 "github.com/apache/iceberg-go/internal" | ||
|
|
||
| // DataFileStatsRef returns statistics maps owned by the data file. The token | ||
| // restricts this zero-copy accessor to trusted in-module callers; the public | ||
| // DataFile getters continue returning defensive copies. Callers must treat the | ||
| // returned maps and the byte slices stored in the bounds maps as read-only. | ||
| func (d *dataFile) DataFileStatsRef(_ internal.DataFileRef) ( | ||
| valueCounts map[int]int64, | ||
| nullCounts map[int]int64, | ||
| nanCounts map[int]int64, | ||
| lowerBounds map[int][]byte, | ||
| upperBounds map[int][]byte, | ||
| ) { | ||
| d.initColumnStatsData() | ||
|
|
||
| return d.valCntMap, d.nullCntMap, d.nanCntMap, d.lowerBoundMap, d.upperBoundMap | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // 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 internal | ||
|
|
||
| // DataFileRef authorizes zero-copy access to immutable DataFile state from | ||
| // trusted packages within this module. Go's internal-package rule prevents | ||
| // external callers from constructing this token. | ||
| type DataFileRef struct{} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ import ( | |
| "fmt" | ||
| "io" | ||
| "iter" | ||
| "maps" | ||
| "math" | ||
| "math/big" | ||
| "reflect" | ||
|
|
@@ -2201,7 +2202,45 @@ func (d *dataFile) FileFormat() FileFormat { return d.Format } | |
| func (d *dataFile) Partition() map[int]any { | ||
| d.initPartitionData() | ||
|
|
||
| return d.fieldIDToPartitionData | ||
| return clonePartitionMap(d.fieldIDToPartitionData) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
More to the point: the escape hatch adds a new internal package, a token type, an interface, and a runtime assertion to avoid five map copies per eval, and there's no benchmark showing that copy was ever hot. This PR needs a benchmark that demonstrates the saving (e.g. |
||
| } | ||
|
|
||
| func clonePartitionMap(src map[int]any) map[int]any { | ||
| if src == nil { | ||
| return nil | ||
| } | ||
|
|
||
| out := maps.Clone(src) | ||
| for id, value := range out { | ||
| if bytes, ok := value.([]byte); ok { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For Avro-decoded files that's fine, since |
||
| out[id] = slices.Clone(bytes) | ||
| } | ||
| } | ||
|
|
||
| return out | ||
| } | ||
|
|
||
| func cloneByteMap(src map[int][]byte) map[int][]byte { | ||
| if src == nil { | ||
| return nil | ||
| } | ||
|
|
||
| out := make(map[int][]byte, len(src)) | ||
| for id, value := range src { | ||
| out[id] = slices.Clone(value) | ||
| } | ||
|
|
||
| return out | ||
| } | ||
|
|
||
| func clonePointer[T any](value *T) *T { | ||
| if value == nil { | ||
| return nil | ||
| } | ||
|
|
||
| cloned := *value | ||
|
|
||
| return &cloned | ||
| } | ||
|
|
||
| func (d *dataFile) Count() int64 { return d.RecordCount } | ||
|
|
@@ -2211,75 +2250,75 @@ func (d *dataFile) SpecID() int32 { return d.specID } | |
| func (d *dataFile) ColumnSizes() map[int]int64 { | ||
| d.initColumnStatsData() | ||
|
|
||
| return d.colSizeMap | ||
| return maps.Clone(d.colSizeMap) | ||
| } | ||
|
|
||
| func (d *dataFile) ValueCounts() map[int]int64 { | ||
| d.initColumnStatsData() | ||
|
|
||
| return d.valCntMap | ||
| return maps.Clone(d.valCntMap) | ||
| } | ||
|
|
||
| func (d *dataFile) NullValueCounts() map[int]int64 { | ||
| d.initColumnStatsData() | ||
|
|
||
| return d.nullCntMap | ||
| return maps.Clone(d.nullCntMap) | ||
| } | ||
|
|
||
| func (d *dataFile) NaNValueCounts() map[int]int64 { | ||
| d.initColumnStatsData() | ||
|
|
||
| return d.nanCntMap | ||
| return maps.Clone(d.nanCntMap) | ||
| } | ||
|
|
||
| func (d *dataFile) DistinctValueCounts() map[int]int64 { | ||
| d.initColumnStatsData() | ||
|
|
||
| return d.distinctCntMap | ||
| return maps.Clone(d.distinctCntMap) | ||
| } | ||
|
|
||
| func (d *dataFile) LowerBoundValues() map[int][]byte { | ||
| d.initColumnStatsData() | ||
|
|
||
| return d.lowerBoundMap | ||
| return cloneByteMap(d.lowerBoundMap) | ||
| } | ||
|
|
||
| func (d *dataFile) UpperBoundValues() map[int][]byte { | ||
| d.initColumnStatsData() | ||
|
|
||
| return d.upperBoundMap | ||
| return cloneByteMap(d.upperBoundMap) | ||
| } | ||
|
|
||
| func (d *dataFile) KeyMetadata() []byte { | ||
| if d.Key == nil { | ||
| return nil | ||
| } | ||
|
|
||
| return *d.Key | ||
| return slices.Clone(*d.Key) | ||
| } | ||
|
|
||
| func (d *dataFile) SplitOffsets() []int64 { | ||
| if d.Splits == nil { | ||
| return nil | ||
| } | ||
|
|
||
| return *d.Splits | ||
| return slices.Clone(*d.Splits) | ||
| } | ||
|
|
||
| func (d *dataFile) EqualityFieldIDs() []int { | ||
| if d.EqualityIDs == nil { | ||
| return nil | ||
| } | ||
|
|
||
| return *d.EqualityIDs | ||
| return slices.Clone(*d.EqualityIDs) | ||
| } | ||
|
|
||
| func (d *dataFile) SortOrderID() *int { return d.SortOrder } | ||
| func (d *dataFile) SortOrderID() *int { return clonePointer(d.SortOrder) } | ||
|
|
||
| func (d *dataFile) FirstRowID() *int64 { return d.FirstRowIDField } | ||
| func (d *dataFile) ReferencedDataFile() *string { return d.ReferencedDataFileField } | ||
| func (d *dataFile) ContentSizeInBytes() *int64 { return d.ContentSizeInBytesField } | ||
| func (d *dataFile) ContentOffset() *int64 { return d.ContentOffsetField } | ||
| func (d *dataFile) FirstRowID() *int64 { return clonePointer(d.FirstRowIDField) } | ||
| func (d *dataFile) ReferencedDataFile() *string { return clonePointer(d.ReferencedDataFileField) } | ||
| func (d *dataFile) ContentSizeInBytes() *int64 { return clonePointer(d.ContentSizeInBytesField) } | ||
| func (d *dataFile) ContentOffset() *int64 { return clonePointer(d.ContentOffsetField) } | ||
|
|
||
| type ManifestEntryBuilder struct { | ||
| m *manifestEntry | ||
|
|
@@ -2414,6 +2453,9 @@ type DataFileBuilder struct { | |
| // before calling [DataFileBuilder.Build] to construct the object. | ||
| // The fieldIDToFixedSize argument is retained for source compatibility and is | ||
| // ignored; manifest schemas determine decimal fixed widths during encoding. | ||
| // Byte-slice partition values are cloned. Other partition values must be the | ||
| // immutable scalar or literal values produced by Iceberg's manifest decoder; | ||
| // mutable caller-defined values are retained by reference. | ||
| func NewDataFileBuilder( | ||
| spec PartitionSpec, | ||
| content ManifestEntryContent, | ||
|
|
@@ -2476,7 +2518,7 @@ func NewDataFileBuilder( | |
| RecordCount: recordCount, | ||
| FileSize: fileSize, | ||
| specID: int32(spec.id), | ||
| fieldIDToPartitionData: fieldIDToPartitionData, | ||
| fieldIDToPartitionData: clonePartitionMap(fieldIDToPartitionData), | ||
| fieldNameToID: fieldNameToID, | ||
| fieldIDToLogicalType: fieldIDToLogicalType, | ||
| }, | ||
|
|
@@ -2534,35 +2576,38 @@ func (b *DataFileBuilder) DistinctValueCounts(counts map[int]int64) *DataFileBui | |
|
|
||
| // LowerBoundValues sets the lower bound values for the data file. | ||
| func (b *DataFileBuilder) LowerBoundValues(bounds map[int][]byte) *DataFileBuilder { | ||
| b.d.LowerBounds = mapToAvroColMap(bounds) | ||
| b.d.LowerBounds = mapToAvroColMap(cloneByteMap(bounds)) | ||
|
|
||
| return b | ||
| } | ||
|
|
||
| // UpperBoundValues sets the upper bound values for the data file. | ||
| func (b *DataFileBuilder) UpperBoundValues(bounds map[int][]byte) *DataFileBuilder { | ||
| b.d.UpperBounds = mapToAvroColMap(bounds) | ||
| b.d.UpperBounds = mapToAvroColMap(cloneByteMap(bounds)) | ||
|
|
||
| return b | ||
| } | ||
|
|
||
| // KeyMetadata sets the key metadata for the data file. | ||
| func (b *DataFileBuilder) KeyMetadata(key []byte) *DataFileBuilder { | ||
| b.d.Key = &key | ||
| cloned := slices.Clone(key) | ||
| b.d.Key = &cloned | ||
|
|
||
| return b | ||
| } | ||
|
|
||
| // SplitOffsets sets the split offsets for the data file. | ||
| func (b *DataFileBuilder) SplitOffsets(offsets []int64) *DataFileBuilder { | ||
| b.d.Splits = &offsets | ||
| cloned := slices.Clone(offsets) | ||
| b.d.Splits = &cloned | ||
|
|
||
| return b | ||
| } | ||
|
|
||
| // EqualityFieldIDs sets the equality field ids for the data file. | ||
| func (b *DataFileBuilder) EqualityFieldIDs(ids []int) *DataFileBuilder { | ||
| b.d.EqualityIDs = &ids | ||
| cloned := slices.Clone(ids) | ||
| b.d.EqualityIDs = &cloned | ||
|
|
||
| return b | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the one I'd want settled before merge.
The PR is about protecting DataFile state from callers, but the
[]bytevalues in thelowerBounds/upperBoundsreturned here are the same backing arrays asd.lowerBoundMap/d.upperBoundMap— a caller doinglowerBounds[id][0] = xwrites straight through into cached state, and sinceinitColumnStatsDatais async.Onceit never rebuilds.Both current callers are read-only so there's no live bug, but "zero-copy access to immutable DataFile state" claims more than the code delivers — the maps are shared and the byte values are mutable. At minimum, document on this method that callers must treat the returned slices as read-only, so the next caller wiring into this path doesn't corrupt bounds by accident.