forked from apache/iceberg-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
236 lines (202 loc) · 6.72 KB
/
table.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
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
// 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 (
"context"
"fmt"
"io"
"math/rand"
"reflect"
"time"
"github.com/go-kit/log"
"github.com/oklog/ulid"
"github.com/parquet-go/parquet-go"
"github.com/polarsignals/iceberg-go"
"github.com/thanos-io/objstore"
"golang.org/x/exp/slices"
)
const (
manifestFileExt = ".avro"
metadataFileExt = ".json"
metadataDirName = "metadata"
dataDirName = "data"
)
type Identifier = []string
type Table interface {
Identifier() Identifier
Metadata() Metadata
MetadataLocation() string
Bucket() objstore.Bucket
Schema() *iceberg.Schema
Spec() iceberg.PartitionSpec
SortOrder() SortOrder
Properties() iceberg.Properties
Location() string
CurrentSnapshot() *Snapshot
SnapshotByID(id int64) *Snapshot
SnapshotByName(name string) *Snapshot
Schemas() map[int]*iceberg.Schema
Equals(other Table) bool
SnapshotWriter(options ...WriterOption) (SnapshotWriter, error)
}
type ReadOnlyTable struct {
*baseTable
}
func (r *ReadOnlyTable) SnapshotWriter(options ...WriterOption) (SnapshotWriter, error) {
return nil, fmt.Errorf("table is read-only")
}
type baseTable struct {
identifier Identifier
metadata Metadata
metadataLocation string
bucket objstore.Bucket
}
func (t *baseTable) Equals(other Table) bool {
return slices.Equal(t.identifier, other.Identifier()) &&
t.metadataLocation == other.MetadataLocation() &&
reflect.DeepEqual(t.metadata, other.Metadata())
}
func (t *baseTable) Identifier() Identifier { return t.identifier }
func (t *baseTable) Metadata() Metadata { return t.metadata }
func (t *baseTable) MetadataLocation() string { return t.metadataLocation }
func (t *baseTable) Bucket() objstore.Bucket { return t.bucket }
func (t *baseTable) Schema() *iceberg.Schema { return t.metadata.CurrentSchema() }
func (t *baseTable) Spec() iceberg.PartitionSpec { return t.metadata.PartitionSpec() }
func (t *baseTable) SortOrder() SortOrder { return t.metadata.SortOrder() }
func (t *baseTable) Properties() iceberg.Properties { return t.metadata.Properties() }
func (t *baseTable) Location() string { return t.metadata.Location() }
func (t *baseTable) CurrentSnapshot() *Snapshot { return t.metadata.CurrentSnapshot() }
func (t *baseTable) SnapshotByID(id int64) *Snapshot { return t.metadata.SnapshotByID(id) }
func (t *baseTable) SnapshotByName(name string) *Snapshot { return t.metadata.SnapshotByName(name) }
func (t *baseTable) Schemas() map[int]*iceberg.Schema {
m := make(map[int]*iceberg.Schema)
for _, s := range t.metadata.Schemas() {
m[s.ID] = s
}
return m
}
func New(ident Identifier, meta Metadata, location string, bucket objstore.Bucket) Table {
return &ReadOnlyTable{
baseTable: &baseTable{
identifier: ident,
metadata: meta,
metadataLocation: location,
bucket: bucket,
},
}
}
func NewFromLocation(ident Identifier, metalocation string, bucket objstore.Bucket) (Table, error) {
var meta Metadata
r, err := bucket.Get(context.Background(), metalocation)
if err != nil {
return nil, err
}
defer r.Close()
if meta, err = ParseMetadata(r); err != nil {
return nil, err
}
return New(ident, meta, metalocation, bucket), nil
}
// SnapshotWriter is an interface for writing a new snapshot to a table.
type SnapshotWriter interface {
// Append accepts a ReaderAt object that should read the Parquet file that is to be added to the snapshot.
Append(ctx context.Context, r io.Reader) error
// DeleteDataFile deletes a data file from the table. The function passed to this method should return true if the file should be deleted.
DeleteDataFile(ctx context.Context, del func(file iceberg.DataFile) bool) error
// Close writes the new snapshot to the table and closes the writer. It is an error to call Append after Close.
Close(ctx context.Context) error
}
type WriterOption func(*writerOptions)
type writerOptions struct {
fastAppendMode bool
manifestSizeBytes int
mergeSchema bool
expireSnapshotsOlderThan time.Duration
metadataDeleteAfterCommit bool
metadataPreviousVersionsMax int
logger log.Logger
}
func WithLogger(logger log.Logger) WriterOption {
return func(o *writerOptions) {
o.logger = logger
}
}
func WithMetadataDeleteAfterCommit() WriterOption {
return func(o *writerOptions) {
o.metadataDeleteAfterCommit = true
}
}
func WithMetadataPreviousVersionsMax(n int) WriterOption {
return func(o *writerOptions) {
o.metadataPreviousVersionsMax = n
}
}
func WithExpireSnapshotsOlderThan(d time.Duration) WriterOption {
return func(o *writerOptions) {
o.expireSnapshotsOlderThan = d
}
}
func WithMergeSchema() WriterOption {
return func(o *writerOptions) {
o.mergeSchema = true
}
}
func WithFastAppend() WriterOption {
return func(o *writerOptions) {
o.fastAppendMode = true
}
}
func WithManifestSizeBytes(size int) WriterOption {
return func(o *writerOptions) {
o.manifestSizeBytes = size
}
}
func generateULID() ulid.ULID {
t := time.Now()
entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0)
return ulid.MustNew(ulid.Timestamp(t), entropy)
}
func parquetSchemaToIcebergSchema(id int, schema *parquet.Schema) *iceberg.Schema {
fields := make([]iceberg.NestedField, 0, len(schema.Fields()))
for i, f := range schema.Fields() {
fields = append(fields, iceberg.NestedField{
Type: parquetTypeToIcebergType(f.Type()),
ID: i,
Name: f.Name(),
Required: f.Required(),
})
}
return iceberg.NewSchema(id, fields...)
}
func parquetTypeToIcebergType(t parquet.Type) iceberg.Type {
switch tp := t.Kind(); tp {
case parquet.Boolean:
return iceberg.BooleanType{}
case parquet.Int32:
return iceberg.Int32Type{}
case parquet.Int64:
return iceberg.Int64Type{}
case parquet.Float:
return iceberg.Float32Type{}
case parquet.Double:
return iceberg.Float64Type{}
case parquet.ByteArray:
return iceberg.BinaryType{}
default:
panic(fmt.Sprintf("unsupported parquet type: %v", tp))
}
}