Repository navigation
Expand file tree
/
Copy pathdata_file_codec.go
More file actions
411 lines (376 loc) · 14.9 KB
/
Copy pathdata_file_codec.go
File metadata and controls
411 lines (376 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// 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 (
"fmt"
"strconv"
"strings"
"github.com/apache/iceberg-go/internal"
"github.com/apache/iceberg-go/internal/datafileavro"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/twmb/avro"
)
// defaultSchemaCacheSize is the initial capacity of dataFileSchemaCache.
// 8192 covers a few thousand active tables (each contributing 1–3 entries
// for its partition spec × format version) which is what a long-running
// consumer like a server-side compaction service typically sees.
// SetSchemaCacheSize tunes this at runtime.
const defaultSchemaCacheSize = 8192
func init() {
if datafileavro.Unmarshal != nil {
panic("iceberg: datafileavro.Unmarshal already set")
}
datafileavro.Unmarshal = func(data []byte, spec, schema any, version int) (any, error) {
s, ok := spec.(PartitionSpec)
if !ok {
return nil, fmt.Errorf("iceberg: datafileavro.Unmarshal: expected PartitionSpec, got %T", spec)
}
sc, ok := schema.(*Schema)
if !ok {
return nil, fmt.Errorf("iceberg: datafileavro.Unmarshal: expected *Schema, got %T", schema)
}
return unmarshalAvroDataFileEntry(data, s, sc, version)
}
}
// AvroEntryMarshaler is implemented by DataFile values that can be
// encoded using the manifest-entry Avro encoding. The iceberg
// package's built-in DataFile implementation satisfies it; external
// implementations can also satisfy it to participate in the
// [github.com/apache/iceberg-go/codec] DataFile codec.
//
// The encoded bytes are the same bytes a manifest carries for this
// data file. Implementations must produce output that the iceberg
// package's manifest-entry Avro decoder accepts.
// The built-in DataFile implementation rejects projected or stat-stripped files
// with ErrInvalidArgument because omitted metadata cannot be recovered.
type AvroEntryMarshaler interface {
MarshalAvroEntry(spec PartitionSpec, schema *Schema, version int) ([]byte, error)
}
// MarshalAvroEntry encodes this DataFile as Avro bytes using the
// manifest-entry encoding for the given partition spec, table schema
// and format version (1, 2, or 3). The wire format is the same one a
// manifest carries for this data file. The Avro tags define the wire fields;
// TestCloneDataFileAvroFieldsCopiesOnlyAvroFields checks that the explicit
// clone list stays synchronized when new fields are added.
//
// MarshalAvroEntry is the iceberg-package side of the
// [github.com/apache/iceberg-go/codec] DataFile codec; callers
// performing cross-process transport should prefer that package's
// high-level API.
//
// MarshalAvroEntry is safe to call concurrently with any other
// reader or encoder of the same DataFile: a fresh *dataFile is
// cloned (avro-tagged fields only) and the avro encoder reads, but
// does not mutate, the cloned values. Pointer-typed avro fields like
// ColSizes share their backing storage with the source; the
// thread-safety guarantee relies on the avro encoder being
// non-mutating.
//
// v1 note: the v1 manifest-entry schema has a non-nullable snapshot_id
// field. MarshalAvroEntry writes 0 there, so v1 bytes are not usable
// as a standalone manifest entry — they only round-trip via the
// matching decoder.
//
// distinct_counts (field 111) is deprecated in the spec for every
// version (apache/iceberg#12182). MarshalAvroEntry never encodes it,
// for v1, v2, and v3 alike. The Avro tag on the dataFile struct is
// intentionally retained so legacy manifests that already carry the
// field on the wire still decode through the matching reader.
func (d *dataFile) MarshalAvroEntry(spec PartitionSpec, schema *Schema, version int) ([]byte, error) {
if err := d.validateForWrite(); err != nil {
return nil, err
}
if version < 1 || version > 3 {
return nil, fmt.Errorf("iceberg: MarshalAvroEntry: unsupported format version %d", version)
}
s, maps, err := manifestEntrySchemaFor(spec, schema, version)
if err != nil {
return nil, err
}
clone := cloneDataFileAvroFields(d)
partitionData, err := avroEncodePartitionData(
d.DataFilePartitionRef(internal.DataFileRef{}), maps)
if err != nil {
return nil, err
}
clone.PartitionData = partitionData
return s.Encode(newEncodeEntry(version, clone))
}
// unmarshalAvroDataFileEntry decodes Avro bytes produced by
// [(*dataFile).MarshalAvroEntry] back into a DataFile. The
// (spec, schema, version) triple must match the encoder; passing a
// different spec or version yields a decode error or silently
// mis-typed partition values.
//
// The returned DataFile carries the partition spec id and the field-id
// lookup tables, so Partition() and the stats accessors return id-keyed
// maps as if the file had been read from a manifest.
//
// It is reachable from the [github.com/apache/iceberg-go/codec]
// package through the [datafileavro] bridge.
func unmarshalAvroDataFileEntry(data []byte, spec PartitionSpec, schema *Schema, version int) (DataFile, error) {
if version < 1 || version > 3 {
return nil, fmt.Errorf("iceberg: unmarshalAvroDataFileEntry: unsupported format version %d", version)
}
s, maps, err := manifestEntrySchemaFor(spec, schema, version)
if err != nil {
return nil, err
}
entry, df := newDecodeEntry(version)
if _, err := s.Decode(data, entry); err != nil {
return nil, fmt.Errorf("iceberg: unmarshalAvroDataFileEntry: %w", err)
}
if err := df.normalizeFormat(); err != nil {
return nil, fmt.Errorf("iceberg: unmarshalAvroDataFileEntry: %w", err)
}
df.specID = int32(spec.ID())
df.fieldNameToID = maps.nameToID
df.fieldIDToLogicalType = maps.idToType
df.fieldIDToDecimalScale = maps.idToDecimalScale
return df, nil
}
// newEncodeEntry returns the right manifest-entry shape for the schema
// version: v1's manifest_entry has a non-nullable snapshot_id and uses
// [fallbackManifestEntry], v2/v3 use [manifestEntry] with nullable
// pointers.
func newEncodeEntry(version int, df *dataFile) any {
if version == 1 {
return &fallbackManifestEntry{
manifestEntry: manifestEntry{EntryStatus: EntryStatusADDED, Data: df},
}
}
return &manifestEntry{EntryStatus: EntryStatusADDED, Data: df}
}
// newDecodeEntry mirrors [newEncodeEntry] for the read side: it returns
// the pointer to pass to avro.Schema.Decode along with the pre-allocated
// *dataFile that will be populated.
func newDecodeEntry(version int) (any, *dataFile) {
df := &dataFile{}
if version == 1 {
return &fallbackManifestEntry{manifestEntry: manifestEntry{Data: df}}, df
}
return &manifestEntry{Data: df}, df
}
// cloneDataFileAvroFields shallow-copies the Avro-tagged fields without
// reflection. Internal state, including sync.Once, caches, and spec IDs, is
// intentionally left at zero values. The test checks every Avro-tagged field
// so that new fields cannot be silently omitted from this list.
//
// Pointer-typed fields share backing storage with src. The encoder must not
// mutate these values (see TestMarshalAvroEntryDoesNotMutateAnyAvroField).
func cloneDataFileAvroFields(src *dataFile) *dataFile {
return &dataFile{
Content: src.Content,
Path: src.Path,
Format: src.Format,
PartitionData: src.PartitionData,
RecordCount: src.RecordCount,
FileSize: src.FileSize,
BlockSizeInBytes: src.BlockSizeInBytes,
ColSizes: src.ColSizes,
ValCounts: src.ValCounts,
NullCounts: src.NullCounts,
NaNCounts: src.NaNCounts,
DistinctCounts: src.DistinctCounts,
LowerBounds: src.LowerBounds,
UpperBounds: src.UpperBounds,
Key: src.Key,
Splits: src.Splits,
EqualityIDs: src.EqualityIDs,
SortOrder: src.SortOrder,
FirstRowIDField: src.FirstRowIDField,
ReferencedDataFileField: src.ReferencedDataFileField,
ContentOffsetField: src.ContentOffsetField,
ContentSizeInBytesField: src.ContentSizeInBytesField,
}
}
// avroEncodePartitionData converts an id-keyed partition tuple (carrying
// iceberg-typed values like Date or Decimal) into the name-keyed
// avro-friendly map the manifest-entry schema expects. Idempotent:
// values already in primitive form pass through unchanged.
func avroEncodePartitionData(idKeyed map[int]any, fields dataFileFieldMaps) (map[string]any, error) {
out := make(map[string]any, len(fields.nameToID))
for name, id := range fields.nameToID {
if _, unknown := fields.unknownFieldIDs[id]; unknown {
// UnknownType has no value representation and is encoded as Avro
// null. Normalize historical values whose source type was dropped.
out[name] = nil
continue
}
v, ok := idKeyed[id]
if !ok {
continue
}
if logical, ok := fields.idToType[id]; ok {
converted, err := convertLogicalTypeValue(v, logical, fields.idToFixedSize[id])
if err != nil {
return nil, fmt.Errorf("failed to convert partition field %d: %w", id, err)
}
out[name] = converted
} else {
out[name] = v
}
}
return out, nil
}
type dataFileFieldMaps struct {
nameToID map[string]int
idToType map[int]string
idToFixedSize map[int]int
idToDecimalScale map[int]int
unknownFieldIDs map[int]struct{}
}
// dataFileSchemaCacheKey identifies a cached Avro schema by the ordered
// partition field IDs, names, result types, and format version. These determine
// the partition Avro shape without including docs or table-local schema/spec IDs.
type dataFileSchemaCacheKey struct {
partAvroFingerprint string
version int
}
type dataFileSchemaEntry struct {
schema *avro.Schema
maps dataFileFieldMaps
}
var dataFileSchemaCache = mustNewSchemaCache(defaultSchemaCacheSize)
func mustNewSchemaCache(size int) *lru.Cache[dataFileSchemaCacheKey, *dataFileSchemaEntry] {
c, err := lru.New[dataFileSchemaCacheKey, *dataFileSchemaEntry](size)
if err != nil {
panic(fmt.Sprintf("iceberg: schema cache size %d invalid: %v", size, err))
}
return c
}
// SetSchemaCacheSize resizes the manifest-entry schema cache used by
// the DataFile codec. The default capacity is sized for a few thousand
// active partition specs; long-running consumers with larger working
// sets (e.g. a compaction service touching many tables) should raise
// it. Existing entries are preserved on grow; on shrink, least-recently
// used entries are evicted down to the new size. Safe to call concurrently
// with codec operations: the underlying golang-lru/v2 cache serializes Resize
// against Get/Add through the same mutex.
func SetSchemaCacheSize(size int) error {
if size <= 0 {
return fmt.Errorf("iceberg: SetSchemaCacheSize: size must be positive, got %d", size)
}
dataFileSchemaCache.Resize(size)
return nil
}
// manifestEntrySchemaFor returns the cached avro schema and partition
// field-id lookups for the given partition type and format version.
// The cache key fingerprints the partition Avro shape, so specs that
// differ only in field documentation share a single entry.
func manifestEntrySchemaFor(spec PartitionSpec, schema *Schema, version int) (*avro.Schema, dataFileFieldMaps, error) {
fingerprint, err := partitionSchemaFingerprint(spec, schema)
if err != nil {
return nil, dataFileFieldMaps{}, err
}
key := dataFileSchemaCacheKey{partAvroFingerprint: fingerprint, version: version}
if cached, ok := dataFileSchemaCache.Get(key); ok {
return cached.schema, cached.maps, nil
}
partSchema, err := partitionTypeToAvroSchema(spec.PartitionType(schema))
if err != nil {
return nil, dataFileFieldMaps{}, err
}
fullSchema, err := internal.NewManifestEntrySchema(partSchema, version)
if err != nil {
return nil, dataFileFieldMaps{}, err
}
fieldMaps := getFieldIDMap(fullSchema)
entry := &dataFileSchemaEntry{
schema: fullSchema,
maps: fieldMaps,
}
dataFileSchemaCache.Add(key, entry)
return entry.schema, entry.maps, nil
}
func partitionSchemaFingerprint(spec PartitionSpec, schema *Schema) (string, error) {
var key strings.Builder
for _, field := range spec.fields {
sourceType := Type(UnknownType{})
// ResultType only inspects the source type here; borrow it to avoid
// cloning nested source types on every schema-cache hit.
if sourceField, ok := schema.FindFieldByIDRef(field.FirstSourceID(), internal.SchemaRef{}); ok {
sourceType = sourceField.Type
}
resultType := field.Transform.ResultType(sourceType)
// Reserve room for the ID, name length, type tag, and typical parameters.
key.Grow(len(field.Name) + 32)
// Length prefixes keep names unambiguous. The compact type tag also
// includes fixed lengths and decimal precision/scale without formatting
// a temporary type string on every cache hit.
writePartitionFingerprintInt(&key, field.FieldID)
writePartitionFingerprintInt(&key, len(field.Name))
key.WriteString(field.Name)
if err := writePartitionTypeFingerprint(&key, resultType); err != nil {
return "", err
}
}
return key.String(), nil
}
// Keep the supported types and parameters in sync with partitionTypeToAvroSchema.
// TestPartitionTypeFingerprintMatchesAvroConversion checks both paths together.
func writePartitionTypeFingerprint(key *strings.Builder, typ Type) error {
// These tags are internal to the cache key. Parameterized types append their
// parameters so equal Avro partition shapes still produce equal keys.
switch t := typ.(type) {
case Int32Type:
key.WriteByte('i')
case Int64Type:
key.WriteByte('j')
case Float32Type:
key.WriteByte('k')
case Float64Type:
key.WriteByte('d')
case StringType:
key.WriteByte('S')
case DateType:
key.WriteByte('D')
case TimeType:
key.WriteByte('T')
case TimestampType:
key.WriteByte('t')
case TimestampTzType:
key.WriteByte('z')
case TimestampNsType:
key.WriteByte('n')
case TimestampTzNsType:
key.WriteByte('N')
case UUIDType:
key.WriteByte('u')
case BooleanType:
key.WriteByte('b')
case BinaryType:
key.WriteByte('B')
case FixedType:
key.WriteByte('f')
writePartitionFingerprintInt(key, t.Len())
case DecimalType:
key.WriteByte('q')
writePartitionFingerprintInt(key, t.Precision())
writePartitionFingerprintInt(key, t.Scale())
case UnknownType:
key.WriteByte('U')
default:
return fmt.Errorf("unsupported partition type: %s", typ.String())
}
return nil
}
func writePartitionFingerprintInt(key *strings.Builder, value int) {
var buf [20]byte
key.Write(strconv.AppendInt(buf[:0], int64(value), 10))
key.WriteByte(':')
}