forked from apache/iceberg-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanifest_builder.go
188 lines (163 loc) · 4.73 KB
/
manifest_builder.go
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
package iceberg
import (
"bytes"
"encoding/json"
"fmt"
"io"
"text/template"
"time"
"github.com/hamba/avro/v2/ocf"
)
func NewManifestEntryV1(entryStatus ManifestEntryStatus, snapshotID int64, data DataFile) ManifestEntry {
return &manifestEntryV1{
EntryStatus: entryStatus,
Snapshot: snapshotID,
Data: data,
}
}
type DataFileBuilder struct {
*dataFile
}
func NewDataFileV1Builder(
FilePath string,
FileFormat FileFormat,
PartitionSpec map[string]any,
RecordCount int64,
FileSizeBytes int64,
) DataFileBuilder {
return DataFileBuilder{
dataFile: &dataFile{
Path: FilePath,
Format: FileFormat,
PartitionData: PartitionSpec,
RecordCount: RecordCount,
FileSize: FileSizeBytes,
},
}
}
func (builder DataFileBuilder) Build() DataFile {
return builder.dataFile
}
func (builder DataFileBuilder) WithColumnSizes(columnSizes map[int]int64) DataFileBuilder {
builder.ColSizes = avroColMapFromMap[int, int64](columnSizes)
return builder
}
func (builder DataFileBuilder) WithValueCounts(valueCounts map[int]int64) DataFileBuilder {
builder.ValCounts = avroColMapFromMap[int, int64](valueCounts)
return builder
}
func (builder DataFileBuilder) WithNullValueCounts(nullValueCounts map[int]int64) DataFileBuilder {
builder.NullCounts = avroColMapFromMap[int, int64](nullValueCounts)
return builder
}
func (builder DataFileBuilder) WithNanValueCounts(nanValueCounts map[int]int64) DataFileBuilder {
builder.NaNCounts = avroColMapFromMap[int, int64](nanValueCounts)
return builder
}
func (builder DataFileBuilder) WithDistinctCounts(distinctCounts map[int]int64) DataFileBuilder {
builder.DistinctCounts = avroColMapFromMap[int, int64](distinctCounts)
return builder
}
func (builder DataFileBuilder) WithLowerBounds(lowerBounds map[int][]byte) DataFileBuilder {
builder.LowerBounds = avroColMapFromMap[int, []byte](lowerBounds)
return builder
}
func (builder DataFileBuilder) WithUpperBounds(upperBounds map[int][]byte) DataFileBuilder {
builder.UpperBounds = avroColMapFromMap[int, []byte](upperBounds)
return builder
}
func (builder DataFileBuilder) WithKeyMetadata(keyMetadata []byte) DataFileBuilder {
builder.Key = &keyMetadata
return builder
}
func (builder DataFileBuilder) WithSplitOffsets(splitOffsets []int64) DataFileBuilder {
builder.Splits = &splitOffsets
return builder
}
func (builder DataFileBuilder) WithSortOrderID(sortOrderID int) DataFileBuilder {
builder.SortOrder = &sortOrderID
return builder
}
func WriteManifestListV1(w io.Writer, files []ManifestFile) error {
enc, err := ocf.NewEncoder(
AvroManifestListV1Schema,
w,
ocf.WithMetadata(map[string][]byte{
"avro.codec": []byte("deflate"),
}),
ocf.WithCodec(ocf.Deflate),
)
if err != nil {
return err
}
defer enc.Close()
for _, file := range files {
if err := enc.Encode(file); err != nil {
return err
}
}
return nil
}
func WriteManifestV1(w io.Writer, schema *Schema, entries []ManifestEntry) error {
b, err := json.Marshal(schema)
if err != nil {
return fmt.Errorf("failed to marshal schema: %w", err)
}
enc, err := ocf.NewEncoder(
AvroSchemaFromEntriesV1(entries),
w,
ocf.WithMetadata(map[string][]byte{
"format-version": []byte("1"),
"schema": b,
//"partition-spec": []byte("todo"), // TODO
"avro.codec": []byte("deflate"),
}),
ocf.WithCodec(ocf.Deflate),
)
if err != nil {
return err
}
defer enc.Close()
for _, entry := range entries {
if err := enc.Encode(entry); err != nil {
return err
}
}
return nil
}
// AvroSchemaFromEntriesV1 creates an Avro schema from the given manifest entries.
// The entries must all share the same partition spec.
func AvroSchemaFromEntriesV1(entries []ManifestEntry) string {
partitions := entries[0].DataFile().Partition() // Pull the first entries partition spec since they are expected to be the same for all entries.
partitionFieldID := 1000 // According to the spec partition field IDs start at 1000. https://iceberg.apache.org/spec/#partition-evolution
b := &bytes.Buffer{}
if err := template.Must(
template.New("EntryV1Schema").
Funcs(template.FuncMap{
"PartitionFieldID": func(_ any) int {
prev := partitionFieldID
partitionFieldID++
return prev
},
"Type": func(i any) string {
switch t := i.(type) {
case string:
return `["null", "string"]`
case int:
return `["null", "int"]`
case int64:
return `["null", "long"]`
case []byte:
return `["null", "bytes"]`
case time.Time:
return `["null", {"type": "int", "logicalType": "date"}]`
default:
panic(fmt.Sprintf("unsupported type %T", t))
}
},
}).
Parse(AvroEntryV1SchemaTmpl)).Execute(b, partitions); err != nil {
panic(err)
}
return b.String()
}