-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigquery.go
229 lines (198 loc) · 5.39 KB
/
bigquery.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
package bqmigrator
import (
"errors"
"fmt"
"net/http"
"strings"
"time"
"google.golang.org/api/googleapi"
"context"
"cloud.google.com/go/bigquery"
)
type bqclient struct {
client *bigquery.Client
}
func newClient(bq *bigquery.Client) *bqclient {
return &bqclient{
client: bq,
}
}
func (bq *bqclient) GetClient() *bigquery.Client {
return bq.client
}
func (bq *bqclient) Query(ctx context.Context, query string) (rowIterator, error) {
q := bq.client.Query(query)
iter, err := q.Read(ctx)
if err != nil {
return nil, fmt.Errorf("querying: %v", err)
}
return &rowIteratorWrapper{RowIterator: iter}, nil
}
func (bq *bqclient) CreateDataset(ctx context.Context, name string) error {
exists, err := bq.datasetExists(ctx, name)
if err != nil {
return fmt.Errorf("checking dataset exists: %v", err)
}
if exists {
return nil
}
dataset := bq.client.Dataset(name)
err = dataset.Create(ctx, &bigquery.DatasetMetadata{Name: name})
if err != nil {
if isAlreadyExistsError(err) {
return nil
}
return fmt.Errorf("creating dataset: %v", err)
}
exists, err = retryUntil(func() (bool, error) {
exists, err = bq.datasetExists(ctx, name)
if err != nil {
return false, fmt.Errorf("checking dataset exists: %v", err)
}
if exists {
return true, nil
}
return false, nil
}, 12, 5*time.Second, true)
if err != nil {
return fmt.Errorf("waiting for dataset to exist: %v", err)
}
return nil
}
func (bq *bqclient) datasetExists(ctx context.Context, dataset string) (exists bool, err error) {
datasetRef := bq.client.Dataset(dataset)
_, err = datasetRef.Metadata(ctx)
if err != nil {
if isNotFoundErr(err) {
return false, nil
} else {
return false, fmt.Errorf("checking dataset metadata: %v", err)
}
}
return true, nil
}
// CreateTable first checks if the table exists, if it does not exist it creates one and waits for it to exists
func (bq *bqclient) CreateTable(ctx context.Context, dataset, table string, schema bigquery.Schema) (exists bool, err error) {
err = bq.CreateDataset(ctx, dataset)
if err != nil {
return false, err
}
exists, err = bq.tableExists(ctx, dataset, table)
if err != nil {
return false, fmt.Errorf("checking table exists: %v", err)
}
if exists {
return true, nil
}
tableRef := bq.client.Dataset(dataset).Table(table)
if err = tableRef.Create(ctx, &bigquery.TableMetadata{Name: table, Schema: schema}); err != nil {
return false, fmt.Errorf("creating table: %v", err)
}
exists, err = retryUntil(func() (bool, error) {
exists, err = bq.tableExists(ctx, dataset, table)
if err != nil {
return false, fmt.Errorf("checking table exists: %v", err)
}
if exists {
return true, nil
}
return false, nil
}, 12, 5*time.Second, true)
if err != nil {
return false, fmt.Errorf("waiting for table to exist: %v", err)
}
return exists, nil
}
func (bq *bqclient) tableExists(ctx context.Context, dataset, table string) (created bool, err error) {
tableRef := bq.client.Dataset(dataset).Table(table)
_, err = tableRef.Metadata(ctx)
if err != nil {
if isNotFoundErr(err) {
return false, nil
} else {
return false, fmt.Errorf("checking table metadata: %v", err)
}
}
return true, nil
}
func (bq *bqclient) CreateView(ctx context.Context, dataset, view, query string) error {
exists, err := bq.tableExists(ctx, dataset, view)
if err != nil {
return fmt.Errorf("checking view exists: %v", err)
}
if exists {
return nil
}
q := bq.client.Query(query)
_, err = q.Read(ctx)
if err != nil {
return fmt.Errorf("creating view: %v", err)
}
return nil
}
func (bq *bqclient) CreateTableFunction(ctx context.Context, dataset, function, query string) error {
routine := bq.client.Dataset(dataset).Routine(function)
_, err := routine.Metadata(ctx)
if err != nil {
if isNotFoundErr(err) {
q := bq.client.Query(query)
_, err = q.Read(ctx)
if err != nil {
return fmt.Errorf("creating table function: %v", err)
}
} else {
return fmt.Errorf("checking routine metadata: %v", err)
}
}
return nil
}
func (bq *bqclient) CopyTable(ctx context.Context, dataset, table, copy string) error {
copier := bq.client.Dataset(dataset).Table(copy).CopierFrom(bq.client.Dataset(dataset).Table(table))
copier.WriteDisposition = bigquery.WriteTruncate
job, err := copier.Run(ctx)
if err != nil {
return fmt.Errorf("copying table: %v", err)
}
status, err := job.Wait(ctx)
if err != nil {
return fmt.Errorf("waiting for copy job to complete: %v", err)
}
if err = status.Err(); err != nil {
return fmt.Errorf("copy job failed: %v", err)
}
return nil
}
func (bq *bqclient) DeleteTable(ctx context.Context, dataset, table string) error {
err := bq.client.Dataset(dataset).Table(table).Delete(ctx)
if err != nil {
return fmt.Errorf("deleting table: %v", err)
}
return nil
}
func isNotFoundErr(err error) bool {
var e *googleapi.Error
ok := errors.As(err, &e)
return ok && e.Code == http.StatusNotFound
}
func isAlreadyExistsError(err error) bool {
var e *googleapi.Error
if errors.As(err, &e) {
if e.Code == 409 || (e.Code == 400 && strings.Contains(e.Message, "already exists in schema")) {
return true
}
}
return false
}
type rowIteratorWrapper struct {
RowIterator *bigquery.RowIterator
}
func (r *rowIteratorWrapper) Next(dst any) error {
return r.RowIterator.Next(dst)
}
func (r *rowIteratorWrapper) TotalRows() int {
return int(r.RowIterator.TotalRows)
}
type rowIterator interface {
Next(dst any) error
TotalRows() int
}