-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrows.go
More file actions
526 lines (487 loc) · 13 KB
/
Copy pathrows.go
File metadata and controls
526 lines (487 loc) · 13 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package flink
import (
"bytes"
"context"
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"io"
"iter"
"reflect"
"strings"
"time"
)
type typeAlias string
const (
tinyIntType typeAlias = "TINYINT"
smallIntType typeAlias = "SMALLINT"
integerType typeAlias = "INTEGER"
bigIntType typeAlias = "BIGINT"
intervalType typeAlias = "INTERVAL"
floatType typeAlias = "FLOAT"
doubleType typeAlias = "DOUBLE"
booleanType typeAlias = "BOOLEAN"
charType typeAlias = "CHAR"
varCharType typeAlias = "VARCHAR"
decimal typeAlias = "DECIMAL"
dateType typeAlias = "DATE"
timeType typeAlias = "TIME"
timestamp typeAlias = "TIMESTAMP"
timestampWTZType typeAlias = "TIMESTAMP_WITH_TIME_ZONE"
timestampLTZType typeAlias = "TIMESTAMP_LTZ"
intervalYearMonthType typeAlias = "INTERVAL_YEAR_MONTH"
intervalDayTimeType typeAlias = "INTERVAL_DAY_TIME"
arrayType typeAlias = "ARRAY"
mapType typeAlias = "MAP"
rowType typeAlias = "ROW"
multisetType typeAlias = "MULTISET"
)
const (
resultsMinBackoff = 100 * time.Millisecond
resultsMaxBackoff = 1 * time.Second
)
func normalizeFlinkType(s string) typeAlias {
switch strings.ToUpper(s) {
case "TINYINT":
return tinyIntType
case "SMALLINT":
return smallIntType
case "INTEGER", "INT":
return integerType
case "BIGINT":
return bigIntType
case "INTERVAL":
return intervalType
case "FLOAT":
return floatType
case "DOUBLE":
return doubleType
case "BOOLEAN":
return booleanType
case "CHAR":
return charType
case "VARCHAR", "STRING":
return varCharType
case "DECIMAL", "DEC", "NUMERIC":
return decimal
case "DATE":
return dateType
case "TIME", "TIME_WITHOUT_TIME_ZONE":
return timeType
case "TIMESTAMP", "TIMESTAMP_WITHOUT_TIME_ZONE":
return timestamp
case "TIMESTAMP_LTZ", "TIMESTAMP_WITH_LOCAL_TIME_ZONE":
return timestampLTZType
case "TIMESTAMP_WITH_TIME_ZONE":
return timestampWTZType
case "INTERVAL_YEAR_MONTH":
return intervalYearMonthType
case "INTERVAL_DAY_TIME":
return intervalDayTimeType
case "ARRAY":
return arrayType
case "MAP":
return mapType
case "ROW":
return rowType
case "MULTISET":
return multisetType
default:
return ""
}
}
var (
scanTypeInt64 = reflect.TypeOf(int64(0))
scanTypeFloat64 = reflect.TypeOf(float64(0))
scanTypeBool = reflect.TypeOf(true)
scanTypeString = reflect.TypeOf("")
scanTypeTime = reflect.TypeOf(time.Time{})
scanTypeBytes = reflect.TypeOf([]byte{})
scanTypeNullFloat = reflect.TypeOf(sql.NullFloat64{})
scanTypeNullInt = reflect.TypeOf(sql.NullInt64{})
scanTypeNullTime = reflect.TypeOf(sql.NullTime{})
scanTypeNullString = reflect.TypeOf(sql.NullString{})
scanTypeNullBool = reflect.TypeOf(sql.NullBool{})
)
var errRowsClosed = errors.New("flink: rows are closed")
// Rows exposes Flink SQL query results through database/sql by implementing
// the driver.Rows interface on top of paged gateway responses.
type Rows struct {
conn *flinkConn
operationHandle string
ctx context.Context
iterator iter.Seq[RowData]
columns []ColumnInfo
iterErr error
closed bool
}
// Columns reports the column names in the order returned by the gateway.
func (r *Rows) Columns() []string {
names := make([]string, len(r.columns))
for i, c := range r.columns {
names[i] = c.Name
}
return names
}
// ColumnTypeDatabaseTypeName returns the Flink logical type name for a column.
func (r *Rows) ColumnTypeDatabaseTypeName(index int) string {
return strings.ToUpper((r.columns)[index].LogicalType.Type)
}
// RowsColumnTypeNullable reports whether the column permits NULL values.
func (r *Rows) RowsColumnTypeNullable(index int) (nullable, ok bool) {
return (r.columns)[index].LogicalType.Nullable, true
}
// Close releases the iterator and asks the gateway to close the underlying operation.
func (r *Rows) Close() error {
if !r.closed {
r.closed = true
r.iterator = nil
r.conn.client.CloseOperation(r.ctx, r.conn.sessionHandle, r.operationHandle)
}
return nil
}
// ColumnTypeScanType returns the Go type expected by Scan for the column.
func (r *Rows) ColumnTypeScanType(index int) reflect.Type {
nullable := (r.columns)[index].LogicalType.Nullable
t := normalizeFlinkType((r.columns)[index].LogicalType.Type)
switch t {
case tinyIntType, smallIntType, integerType, bigIntType, intervalType:
if nullable {
return scanTypeNullInt
}
return scanTypeInt64
case floatType, doubleType:
if nullable {
return scanTypeNullFloat
}
return scanTypeFloat64
case booleanType:
if nullable {
return scanTypeNullBool
}
return scanTypeBool
case charType, varCharType:
if nullable {
return scanTypeNullString
}
return scanTypeString
case decimal:
if nullable {
return scanTypeNullString
}
return scanTypeString
case dateType, timeType, timestamp, timestampLTZType:
if nullable {
return scanTypeNullTime
}
return scanTypeTime
case intervalYearMonthType, intervalDayTimeType:
if nullable {
return scanTypeNullInt
}
return scanTypeInt64
default:
return scanTypeBytes
}
}
// ColumnTypeLength reports the declared length for variable-size columns.
func (r *Rows) ColumnTypeLength(index int) (length int64, ok bool) {
typeLen := (r.columns)[index].LogicalType.Length
if typeLen == nil {
return 0, false
}
return *typeLen, true
}
func (r *Rows) columnTypePrecision(index int) (length int, ok bool) {
perc := (r.columns)[index].LogicalType.Precision
if perc == nil {
return 0, false
}
return *perc, true
}
// ColumnTypePrecisionScale returns precision and scale for a column when available.
func (r *Rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) {
perc := (r.columns)[index].LogicalType.Precision
sc := (r.columns)[index].LogicalType.Scale
if perc == nil || sc == nil {
return 0, 0, false
}
return int64(*perc), int64(*sc), true
}
func (r *Rows) decodeField(t typeAlias, nullable bool, raw []byte, colIdx int) (driver.Value, error) {
isNull := bytes.Equal(raw, []byte("null"))
switch t {
case tinyIntType, smallIntType, integerType, bigIntType, intervalType:
if isNull {
if nullable {
return sql.NullInt64{Valid: false}, nil
}
return nil, nil
}
var v int64
if err := json.Unmarshal(raw, &v); err != nil {
return nil, fmt.Errorf("column %d: int decode failed: %w", colIdx, err)
}
if nullable {
return sql.NullInt64{Int64: v, Valid: true}, nil
}
return v, nil
case floatType, doubleType:
if isNull {
if nullable {
return sql.NullFloat64{Valid: false}, nil
}
return nil, nil
}
var v float64
if err := json.Unmarshal(raw, &v); err != nil {
return nil, fmt.Errorf("column %d: float decode failed: %w", colIdx, err)
}
if nullable {
return sql.NullFloat64{Float64: v, Valid: true}, nil
}
return v, nil
case booleanType:
if isNull {
if nullable {
return sql.NullBool{Valid: false}, nil
}
return nil, nil
}
var v bool
if err := json.Unmarshal(raw, &v); err != nil {
return nil, fmt.Errorf("column %d: bool decode failed: %w", colIdx, err)
}
if nullable {
return sql.NullBool{Bool: v, Valid: true}, nil
}
return v, nil
case charType, varCharType:
if isNull {
if nullable {
return sql.NullString{Valid: false}, nil
}
return nil, nil
}
var v string
if err := json.Unmarshal(raw, &v); err != nil {
return nil, fmt.Errorf("column %d: string decode failed: %w", colIdx, err)
}
if nullable {
return sql.NullString{String: v, Valid: true}, nil
}
return v, nil
case decimal:
if isNull {
if nullable {
return sql.NullString{Valid: false}, nil
}
return nil, nil
}
dec := json.NewDecoder(bytes.NewReader(raw))
dec.UseNumber()
var num json.Number
if err := dec.Decode(&num); err != nil {
return nil, fmt.Errorf("column %d: decimal decode failed: %w", colIdx, err)
}
if nullable {
return sql.NullString{String: num.String(), Valid: true}, nil
}
return num.String(), nil
case dateType:
if isNull {
if nullable {
return sql.NullTime{Valid: false}, nil
}
return nil, nil
}
var s string
if err := json.Unmarshal(raw, &s); err != nil {
return nil, fmt.Errorf("column %d: date decode failed: %w", colIdx, err)
}
tval, err := time.Parse("2006-01-02", s)
if err != nil {
return nil, fmt.Errorf("column %d: date parse failed: %w", colIdx, err)
}
if nullable {
return sql.NullTime{Time: tval, Valid: true}, nil
}
return tval, nil
case timeType:
if isNull {
if nullable {
return sql.NullTime{Valid: false}, nil
}
return nil, nil
}
var s string
if err := json.Unmarshal(raw, &s); err != nil {
return nil, fmt.Errorf("column %d: time decode failed: %w", colIdx, err)
}
prec, ok := r.columnTypePrecision(colIdx)
if !ok {
prec = 0
}
layout := "15:04:05"
if prec > 0 {
layout = layout + "." + strings.Repeat("9", int(prec))
}
tval, err := time.Parse(layout, s)
if err != nil {
return nil, fmt.Errorf("column %d: time parse failed for %q with precision %d: %w", colIdx, s, prec, err)
}
if nullable {
return sql.NullTime{Time: tval, Valid: true}, nil
}
return tval, nil
case timestampWTZType:
// Keep raw bytes for WTZ
return raw, nil
case timestamp, timestampLTZType:
if isNull {
if nullable {
return sql.NullTime{Valid: false}, nil
}
return nil, nil
}
var s string
if err := json.Unmarshal(raw, &s); err != nil {
return nil, fmt.Errorf("column %d: timestamp decode failed: %w", colIdx, err)
}
prec, ok := r.columnTypePrecision(colIdx)
if !ok {
prec = 6
}
base := "2006-01-02T15:04:05"
if prec > 0 {
base = base + "." + strings.Repeat("9", int(prec))
}
layout := base
if strings.HasSuffix(s, "Z") {
layout = base + "Z"
}
tval, err := time.Parse(layout, s)
if err != nil {
return nil, fmt.Errorf("column %d: timestamp parse failed for %q with precision %d using layout %q: %w", colIdx, s, prec, layout, err)
}
if nullable {
return sql.NullTime{Time: tval, Valid: true}, nil
}
return tval, nil
case intervalYearMonthType, intervalDayTimeType:
if isNull {
if nullable {
return sql.NullInt64{Valid: false}, nil
}
return nil, nil
}
var n int64
if err := json.Unmarshal(raw, &n); err != nil {
return nil, fmt.Errorf("column %d: interval decode failed: %w", colIdx, err)
}
if nullable {
return sql.NullInt64{Int64: n, Valid: true}, nil
}
return n, nil
case arrayType, mapType, rowType, multisetType:
return raw, nil
default:
return raw, nil
}
}
// Next advances to the next row, materialising values into dest.
func (r *Rows) Next(dest []driver.Value) error {
if r.closed {
return errRowsClosed
}
var row RowData
var ok bool
r.iterator(func(rdata RowData) bool {
row = rdata
ok = true
return false
})
// No more data or fetch error
if !ok {
if r.iterErr != nil {
return r.iterErr
}
return io.EOF
}
for i, raw := range row.Fields {
t := normalizeFlinkType((r.columns)[i].LogicalType.Type)
nullable := (r.columns)[i].LogicalType.Nullable
val, err := r.decodeField(t, nullable, raw, i)
if err != nil {
return err
}
dest[i] = val
}
return nil
}
func newRows(ctx context.Context, conn *flinkConn, operationHandle string, initialResults []RowData, columns []ColumnInfo, nextToken string) (*Rows, error) {
rows := &Rows{
conn: conn,
ctx: ctx,
operationHandle: operationHandle,
columns: columns,
}
rows.iterator = newResultsIterator(rows, ctx, conn, operationHandle, initialResults, nextToken)
return rows, nil
}
func newResultsIterator(rows *Rows, ctx context.Context, conn *flinkConn, operationHandle string, initialResults []RowData, nextToken string) iter.Seq[RowData] {
results := initialResults
token := nextToken
pos := 0
client := conn.client
sessionHandle := conn.sessionHandle
return func(yield func(RowData) bool) {
backoff := resultsMinBackoff
for {
if pos < len(results) {
row := results[pos]
pos++
if !yield(row) {
return
}
continue
}
// todo: fetch next results asynchronously
response, err := client.FetchResults(ctx, sessionHandle, operationHandle, token, "")
if err != nil {
if rows.iterErr == nil {
rows.iterErr = fmt.Errorf("flink: fetch results failed: %w", err)
}
conn.cancelOperation(ctx, operationHandle)
return
}
if response.ResultType == ResultTypeEOS {
return
}
results = response.Results.Data
token = response.NextToken()
pos = 0
if len(results) == 0 {
select {
case <-ctx.Done():
if rows.iterErr == nil {
rows.iterErr = ctx.Err()
}
conn.cancelOperation(ctx, operationHandle)
return
case <-time.After(backoff):
if backoff < resultsMaxBackoff {
backoff *= 2
if backoff > resultsMaxBackoff {
backoff = resultsMaxBackoff
}
}
}
} else {
backoff = resultsMinBackoff
}
}
}
}