-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathselect_stmt.go
595 lines (561 loc) · 19 KB
/
select_stmt.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
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
package chconn
import (
"bytes"
"context"
"fmt"
"strconv"
"time"
"github.com/vahid-sohrabloo/chconn/v2/column"
"github.com/vahid-sohrabloo/chconn/v2/internal/helper"
"github.com/vahid-sohrabloo/chconn/v2/types"
)
// Select executes a query and return select stmt.
// NOTE: only use for select query
func (ch *conn) Select(ctx context.Context, query string, columns ...column.ColumnBasic) (SelectStmt, error) {
return ch.SelectWithOption(ctx, query, nil, columns...)
}
// Select executes a query with the the query options and return select stmt.
// NOTE: only use for select query
func (ch *conn) SelectWithOption(
ctx context.Context,
query string,
queryOptions *QueryOptions,
columns ...column.ColumnBasic,
) (SelectStmt, error) {
err := ch.lock()
if err != nil {
return nil, err
}
var hasError bool
defer func() {
if hasError {
ch.Close()
}
}()
if ctx != context.Background() {
select {
case <-ctx.Done():
return nil, newContextAlreadyDoneError(ctx)
default:
}
ch.contextWatcher.Watch(ctx)
}
if queryOptions == nil {
queryOptions = emptyQueryOptions
}
err = ch.sendQueryWithOption(query, queryOptions.QueryID, queryOptions.Settings, queryOptions.Parameters)
if err != nil {
hasError = true
return nil, preferContextOverNetTimeoutError(ctx, err)
}
s := &selectStmt{
conn: ch,
query: query,
queryOptions: queryOptions,
clientInfo: nil,
ctx: ctx,
columnsForRead: columns,
}
res, err := s.conn.receiveAndProcessData(nil)
if err != nil {
s.lastErr = err
s.Close()
return nil, err
}
if block, ok := res.(*block); ok {
if block.NumRows == 0 {
err = s.readEmptyBlock(block)
if err != nil {
return nil, err
}
return s, nil
}
}
return nil, &unexpectedPacket{expected: "serverData with zero len", actual: res}
}
// SelectStmt is a interface for select statement
type SelectStmt interface {
// Next read the next block of data for reading.
// It returns true on success, or false if there is no next result row or an error happened while preparing it.
// Err should be consulted to distinguish between the two cases.
Next() bool
// Err returns the error, if any, that was encountered during iteration.
// Err may be called after an explicit or implicit Close.
Err() error
// RowsInBlock return number of rows in this current block
RowsInBlock() int
// Columns return the columns of this select statement.
Columns() []column.ColumnBasic
// Close close the statement and release the connection
// If Next is called and returns false and there are no further blocks,
// the Rows are closed automatically and it will suffice to check the result of Err.
// Close is idempotent and does not affect the result of Err.
Close()
}
type selectStmt struct {
block *block
conn *conn
query string
queryOptions *QueryOptions
clientInfo *ClientInfo
lastErr error
closed bool
columnsForRead []column.ColumnBasic
ctx context.Context
finishSelect bool
validateData bool
}
var _ SelectStmt = &selectStmt{}
func (s *selectStmt) readEmptyBlock(b *block) error {
err := b.readColumns(s.conn)
if err != nil {
s.lastErr = err
s.Close()
return err
}
if len(s.columnsForRead) == 0 {
s.columnsForRead, err = s.getColumnsByChType(b)
if err != nil {
s.lastErr = err
s.Close()
return err
}
} else if len(s.columnsForRead[0].Name()) != 0 {
s.columnsForRead, err = b.reorderColumns(s.columnsForRead)
if err != nil {
return err
}
}
return nil
}
func (s *selectStmt) Next() bool {
// protect after close
if s.closed {
return false
}
s.conn.reader.SetCompress(false)
res, err := s.conn.receiveAndProcessData(nil)
if err != nil {
s.lastErr = err
s.Close()
return false
}
if block, ok := res.(*block); ok {
if block.NumRows == 0 {
err = s.readEmptyBlock(block)
if err != nil {
return false
}
return s.Next()
}
s.block = block
needValidateData := !s.validateData
s.validateData = false
if needValidateData {
if errValidate := s.validate(); errValidate != nil {
s.lastErr = errValidate
s.Close()
return false
}
}
err = block.readColumnsData(s.conn, needValidateData, s.columnsForRead...)
if err != nil {
s.lastErr = preferContextOverNetTimeoutError(s.ctx, err)
s.Close()
return false
}
return true
}
if profile, ok := res.(*Profile); ok {
if s.queryOptions.OnProfile != nil {
s.queryOptions.OnProfile(profile)
}
return s.Next()
}
if progress, ok := res.(*Progress); ok {
if s.queryOptions.OnProgress != nil {
s.queryOptions.OnProgress(progress)
}
return s.Next()
}
if profileEvent, ok := res.(*ProfileEvent); ok {
if s.queryOptions.OnProfileEvent != nil {
s.queryOptions.OnProfileEvent(profileEvent)
}
return s.Next()
}
if res == nil {
s.finishSelect = true
s.columnsForRead = nil
s.Close()
return false
}
s.lastErr = &unexpectedPacket{expected: "serverData", actual: res}
s.Close()
return false
}
func (s *selectStmt) validate() error {
if int(s.block.NumColumns) != len(s.columnsForRead) {
return &ColumnNumberReadError{
Read: len(s.columnsForRead),
Available: s.block.NumColumns,
}
}
return nil
}
// RowsInBlock return number of rows in this current block
func (s *selectStmt) RowsInBlock() int {
return int(s.block.NumRows)
}
// Err returns the error, if any, that was encountered during iteration.
// Err may be called after an explicit or implicit Close.
func (s *selectStmt) Err() error {
return preferContextOverNetTimeoutError(s.ctx, s.lastErr)
}
// Close close the statement and release the connection
// If Next is called and returns false and there are no further blocks,
// the Rows are closed automatically and it will suffice to check the result of Err.
// Close is idempotent and does not affect the result of Err.
func (s *selectStmt) Close() {
s.conn.reader.SetCompress(false)
if !s.closed {
s.closed = true
s.conn.contextWatcher.Unwatch()
s.conn.unlock()
if s.Err() != nil || !s.finishSelect {
s.conn.Close()
}
}
}
func (s *selectStmt) Columns() []column.ColumnBasic {
return s.columnsForRead
}
func (s *selectStmt) getColumnsByChType(b *block) ([]column.ColumnBasic, error) {
columns := make([]column.ColumnBasic, len(b.Columns))
for i, col := range b.Columns {
columnByType, err := s.columnByType(col.ChType, 0, false, false)
if err != nil {
return nil, err
}
columnByType.SetName(col.Name)
columnByType.SetType(col.ChType)
err = columnByType.Validate()
if err != nil {
return nil, err
}
columns[i] = columnByType
}
return columns, nil
}
//nolint:funlen,gocyclo
func (s *selectStmt) columnByType(chType []byte, arrayLevel int, nullable, lc bool) (column.ColumnBasic, error) {
switch {
case string(chType) == "Bool":
return column.New[bool]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "Int8" || helper.IsEnum8(chType):
return column.New[int8]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "Int16" || helper.IsEnum16(chType):
return column.New[int16]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "Int32":
return column.New[int32]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "Int64":
return column.New[int64]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "Int128":
return column.New[types.Int128]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "Int256":
return column.New[types.Int256]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "UInt8":
return column.New[uint8]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "UInt16":
return column.New[uint16]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "UInt32":
return column.New[uint32]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "UInt64":
return column.New[uint64]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "UInt128":
return column.New[types.Uint128]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "UInt256":
return column.New[types.Uint256]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "Float32":
return column.New[float32]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "Float64":
return column.New[float64]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "String":
return column.NewString().Elem(arrayLevel, nullable, lc), nil
case helper.IsFixedString(chType):
strLen, err := strconv.Atoi(string(chType[helper.FixedStringStrLen : len(chType)-1]))
if err != nil {
return nil, fmt.Errorf("invalid fixed string length: %s: %w", string(chType), err)
}
return getFixedType(strLen, arrayLevel, nullable, lc)
case string(chType) == "Date":
if !s.queryOptions.UseGoTime {
return column.New[types.Date]().Elem(arrayLevel, nullable, lc), nil
}
return column.NewDate[types.Date]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "Date32":
if !s.queryOptions.UseGoTime {
return column.New[types.Date32]().Elem(arrayLevel, nullable, lc), nil
}
return column.NewDate[types.Date32]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "DateTime" || helper.IsDateTimeWithParam(chType):
if !s.queryOptions.UseGoTime {
return column.New[types.DateTime]().Elem(arrayLevel, nullable, lc), nil
}
var params [][]byte
if bytes.HasPrefix(chType, []byte("DateTime(")) {
params = bytes.Split(chType[len("DateTime("):len(chType)-1], []byte(", "))
}
col := column.NewDate[types.DateTime]()
if len(params) > 0 && len(params[0]) >= 3 {
if loc, err := time.LoadLocation(string(params[0][1 : len(params[0])-1])); err == nil {
col.SetLocation(loc)
} else if loc, err := time.LoadLocation(s.conn.serverInfo.Timezone); err == nil {
col.SetLocation(loc)
}
}
return col.Elem(arrayLevel, nullable, lc), nil
case helper.IsDateTime64(chType):
if !s.queryOptions.UseGoTime {
return column.New[types.DateTime64]().Elem(arrayLevel, nullable, lc), nil
}
params := bytes.Split(chType[helper.DateTime64StrLen:len(chType)-1], []byte(", "))
if len(params) == 0 {
panic("DateTime64 invalid params")
}
precision, err := strconv.Atoi(string(params[0]))
if err != nil {
panic("DateTime64 invalid precision: " + err.Error())
}
col := column.NewDate[types.DateTime64]()
col.SetPrecision(precision)
if len(params) > 1 && len(params[1]) >= 3 {
if loc, err := time.LoadLocation(string(params[1][1 : len(params[1])-1])); err == nil {
col.SetLocation(loc)
} else if loc, err := time.LoadLocation(s.conn.serverInfo.Timezone); err == nil {
col.SetLocation(loc)
}
}
return col.Elem(arrayLevel, nullable, lc), nil
case helper.IsDecimal(chType):
params := bytes.Split(chType[helper.DecimalStrLen:len(chType)-1], []byte(", "))
precision, _ := strconv.Atoi(string(params[0]))
if precision <= 9 {
return column.New[types.Decimal32]().Elem(arrayLevel, nullable, lc), nil
}
if precision <= 18 {
return column.New[types.Decimal64]().Elem(arrayLevel, nullable, lc), nil
}
if precision <= 38 {
return column.New[types.Decimal128]().Elem(arrayLevel, nullable, lc), nil
}
if precision <= 76 {
return column.New[types.Decimal256]().Elem(arrayLevel, nullable, lc), nil
}
panic("Decimal invalid precision: " + string(chType))
case string(chType) == "UUID":
return column.New[types.UUID]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "IPv4":
return column.New[types.IPv4]().Elem(arrayLevel, nullable, lc), nil
case string(chType) == "IPv6":
return column.New[types.IPv6]().Elem(arrayLevel, nullable, lc), nil
case helper.IsNullable(chType):
return s.columnByType(chType[helper.LenNullableStr:len(chType)-1], arrayLevel, true, lc)
case bytes.HasPrefix(chType, []byte("SimpleAggregateFunction(")):
return s.columnByType(helper.FilterSimpleAggregate(chType), arrayLevel, nullable, lc)
case helper.IsArray(chType):
if arrayLevel == 3 {
return nil, fmt.Errorf("max array level is 3")
}
if nullable {
return nil, fmt.Errorf("array is not allowed in nullable")
}
if lc {
return nil, fmt.Errorf("LowCardinality is not allowed in nullable")
}
return s.columnByType(chType[helper.LenArrayStr:len(chType)-1], arrayLevel+1, nullable, lc)
case helper.IsLowCardinality(chType):
return s.columnByType(chType[helper.LenLowCardinalityStr:len(chType)-1], arrayLevel, nullable, true)
case helper.IsTuple(chType):
columnsTuple, err := helper.TypesInParentheses(chType[helper.LenTupleStr : len(chType)-1])
if err != nil {
return nil, fmt.Errorf("tuple invalid types: %w", err)
}
columns := make([]column.ColumnBasic, len(columnsTuple))
for i, c := range columnsTuple {
col, err := s.columnByType(c.ChType, 0, false, false)
if err != nil {
return nil, err
}
col.SetName(c.Name)
columns[i] = col
}
return column.NewTuple(columns...).Elem(arrayLevel), nil
case helper.IsMap(chType):
columnsMap, err := helper.TypesInParentheses(chType[helper.LenMapStr : len(chType)-1])
if err != nil {
return nil, fmt.Errorf("map invalid types: %w", err)
}
if len(columnsMap) != 2 {
return nil, fmt.Errorf("map must have 2 columns")
}
columns := make([]column.ColumnBasic, len(columnsMap))
for i, col := range columnsMap {
col, err := s.columnByType(col.ChType, arrayLevel, nullable, lc)
if err != nil {
return nil, err
}
columns[i] = col
}
return column.NewMapBase(columns[0], columns[1]), nil
case helper.IsNested(chType):
return s.columnByType(helper.NestedToArrayType(chType), arrayLevel, nullable, lc)
}
return nil, fmt.Errorf("unknown type: %s", chType)
}
//nolint:funlen,gocyclo
func getFixedType(fixedLen, arrayLevel int, nullable, lc bool) (column.ColumnBasic, error) {
switch fixedLen {
case 1:
return column.New[[1]byte]().Elem(arrayLevel, nullable, lc), nil
case 2:
return column.New[[2]byte]().Elem(arrayLevel, nullable, lc), nil
case 3:
return column.New[[3]byte]().Elem(arrayLevel, nullable, lc), nil
case 4:
return column.New[[4]byte]().Elem(arrayLevel, nullable, lc), nil
case 5:
return column.New[[5]byte]().Elem(arrayLevel, nullable, lc), nil
case 6:
return column.New[[6]byte]().Elem(arrayLevel, nullable, lc), nil
case 7:
return column.New[[7]byte]().Elem(arrayLevel, nullable, lc), nil
case 8:
return column.New[[8]byte]().Elem(arrayLevel, nullable, lc), nil
case 9:
return column.New[[9]byte]().Elem(arrayLevel, nullable, lc), nil
case 10:
return column.New[[10]byte]().Elem(arrayLevel, nullable, lc), nil
case 11:
return column.New[[11]byte]().Elem(arrayLevel, nullable, lc), nil
case 12:
return column.New[[12]byte]().Elem(arrayLevel, nullable, lc), nil
case 13:
return column.New[[13]byte]().Elem(arrayLevel, nullable, lc), nil
case 14:
return column.New[[14]byte]().Elem(arrayLevel, nullable, lc), nil
case 15:
return column.New[[15]byte]().Elem(arrayLevel, nullable, lc), nil
case 16:
return column.New[[16]byte]().Elem(arrayLevel, nullable, lc), nil
case 17:
return column.New[[17]byte]().Elem(arrayLevel, nullable, lc), nil
case 18:
return column.New[[18]byte]().Elem(arrayLevel, nullable, lc), nil
case 19:
return column.New[[19]byte]().Elem(arrayLevel, nullable, lc), nil
case 20:
return column.New[[20]byte]().Elem(arrayLevel, nullable, lc), nil
case 21:
return column.New[[21]byte]().Elem(arrayLevel, nullable, lc), nil
case 22:
return column.New[[22]byte]().Elem(arrayLevel, nullable, lc), nil
case 23:
return column.New[[23]byte]().Elem(arrayLevel, nullable, lc), nil
case 24:
return column.New[[24]byte]().Elem(arrayLevel, nullable, lc), nil
case 25:
return column.New[[25]byte]().Elem(arrayLevel, nullable, lc), nil
case 26:
return column.New[[26]byte]().Elem(arrayLevel, nullable, lc), nil
case 27:
return column.New[[27]byte]().Elem(arrayLevel, nullable, lc), nil
case 28:
return column.New[[28]byte]().Elem(arrayLevel, nullable, lc), nil
case 29:
return column.New[[29]byte]().Elem(arrayLevel, nullable, lc), nil
case 30:
return column.New[[30]byte]().Elem(arrayLevel, nullable, lc), nil
case 31:
return column.New[[31]byte]().Elem(arrayLevel, nullable, lc), nil
case 32:
return column.New[[32]byte]().Elem(arrayLevel, nullable, lc), nil
case 33:
return column.New[[33]byte]().Elem(arrayLevel, nullable, lc), nil
case 34:
return column.New[[34]byte]().Elem(arrayLevel, nullable, lc), nil
case 35:
return column.New[[35]byte]().Elem(arrayLevel, nullable, lc), nil
case 36:
return column.New[[36]byte]().Elem(arrayLevel, nullable, lc), nil
case 37:
return column.New[[37]byte]().Elem(arrayLevel, nullable, lc), nil
case 38:
return column.New[[38]byte]().Elem(arrayLevel, nullable, lc), nil
case 39:
return column.New[[39]byte]().Elem(arrayLevel, nullable, lc), nil
case 40:
return column.New[[40]byte]().Elem(arrayLevel, nullable, lc), nil
case 41:
return column.New[[41]byte]().Elem(arrayLevel, nullable, lc), nil
case 42:
return column.New[[42]byte]().Elem(arrayLevel, nullable, lc), nil
case 43:
return column.New[[43]byte]().Elem(arrayLevel, nullable, lc), nil
case 44:
return column.New[[44]byte]().Elem(arrayLevel, nullable, lc), nil
case 45:
return column.New[[45]byte]().Elem(arrayLevel, nullable, lc), nil
case 46:
return column.New[[46]byte]().Elem(arrayLevel, nullable, lc), nil
case 47:
return column.New[[47]byte]().Elem(arrayLevel, nullable, lc), nil
case 48:
return column.New[[48]byte]().Elem(arrayLevel, nullable, lc), nil
case 49:
return column.New[[49]byte]().Elem(arrayLevel, nullable, lc), nil
case 50:
return column.New[[50]byte]().Elem(arrayLevel, nullable, lc), nil
case 51:
return column.New[[51]byte]().Elem(arrayLevel, nullable, lc), nil
case 52:
return column.New[[52]byte]().Elem(arrayLevel, nullable, lc), nil
case 53:
return column.New[[53]byte]().Elem(arrayLevel, nullable, lc), nil
case 54:
return column.New[[54]byte]().Elem(arrayLevel, nullable, lc), nil
case 55:
return column.New[[55]byte]().Elem(arrayLevel, nullable, lc), nil
case 56:
return column.New[[56]byte]().Elem(arrayLevel, nullable, lc), nil
case 57:
return column.New[[57]byte]().Elem(arrayLevel, nullable, lc), nil
case 58:
return column.New[[58]byte]().Elem(arrayLevel, nullable, lc), nil
case 59:
return column.New[[59]byte]().Elem(arrayLevel, nullable, lc), nil
case 60:
return column.New[[60]byte]().Elem(arrayLevel, nullable, lc), nil
case 61:
return column.New[[61]byte]().Elem(arrayLevel, nullable, lc), nil
case 62:
return column.New[[62]byte]().Elem(arrayLevel, nullable, lc), nil
case 63:
return column.New[[63]byte]().Elem(arrayLevel, nullable, lc), nil
case 64:
return column.New[[64]byte]().Elem(arrayLevel, nullable, lc), nil
case 65:
return column.New[[65]byte]().Elem(arrayLevel, nullable, lc), nil
case 66:
return column.New[[66]byte]().Elem(arrayLevel, nullable, lc), nil
case 67:
return column.New[[67]byte]().Elem(arrayLevel, nullable, lc), nil
case 68:
return column.New[[68]byte]().Elem(arrayLevel, nullable, lc), nil
case 69:
return column.New[[69]byte]().Elem(arrayLevel, nullable, lc), nil
case 70:
return column.New[[70]byte]().Elem(arrayLevel, nullable, lc), nil
}
return nil, fmt.Errorf("fixed length %d is not supported", fixedLen)
}