-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconverter.go
More file actions
667 lines (620 loc) · 16.4 KB
/
Copy pathconverter.go
File metadata and controls
667 lines (620 loc) · 16.4 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
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
package goclickzetta
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"math"
"math/big"
"strings"
"time"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/decimal128"
)
type timezoneType int
const (
// TimestampLTZType denotes a LTZ timezoneType for array binds
TimestampLTZType timezoneType = iota
// DateType denotes a date type for array binds
DateType
)
// goTypeToClickzetta translates Go data type to Clickzetta data type.
func goTypeToClickzetta(v driver.Value, tsmode clickzettaType) clickzettaType {
switch v.(type) {
case int64, sql.NullInt64:
return BIGINT
case float64, sql.NullFloat64:
return DOUBLE
case bool, sql.NullBool:
return BOOLEAN
case float32:
return FLOAT
case int8:
return TINYINT
case int16, sql.NullInt16:
return SMALLINT
case int32, sql.NullInt32, int:
return INT
case string, sql.NullString:
return STRING
case time.Time, sql.NullTime:
return tsmode
}
return NOT_SUPPORTED
}
var decimalShift = new(big.Int).Exp(big.NewInt(2), big.NewInt(64), nil)
func intToBigFloat(val int64, scale int64) *big.Float {
f := new(big.Float).SetInt64(val)
s := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(scale), nil))
return new(big.Float).Quo(f, s)
}
func decimalToBigInt(num decimal128.Num) *big.Int {
high := new(big.Int).SetInt64(num.HighBits())
low := new(big.Int).SetUint64(num.LowBits())
return new(big.Int).Add(new(big.Int).Mul(high, decimalShift), low)
}
func decimalToBigFloat(num decimal128.Num, divisor *big.Float) *big.Float {
f := new(big.Float).SetInt(decimalToBigInt(num))
return new(big.Float).Quo(f, divisor)
}
// normalizeKeyValueJSON takes a JSON string potentially containing arrays of
// objects with shape {"key":..., "value":...} and recursively converts those
// arrays into JSON objects, so nested map-like data becomes object style.
func normalizeKeyValueJSON(raw string) string {
var v interface{}
if err := json.Unmarshal([]byte(raw), &v); err != nil {
return raw
}
nv := transformKeyValueShape(v)
b, err := json.Marshal(nv)
if err != nil {
return raw
}
return string(b)
}
func transformKeyValueShape(v interface{}) interface{} {
switch x := v.(type) {
case []interface{}:
// Check if this is an array of {key,value} objects
allKV := true
obj := make(map[string]interface{})
for _, el := range x {
m, ok := el.(map[string]interface{})
if !ok {
allKV = false
break
}
// strict mode: only when key and value are present
if len(m) != 2 {
allKV = false
break
}
keyVal, hasKey := m["key"]
val, hasVal := m["value"]
if !hasKey || !hasVal {
allKV = false
break
}
keyStr, ok := keyVal.(string)
if !ok {
allKV = false
break
}
obj[keyStr] = transformKeyValueShape(val)
}
if allKV {
return obj
}
for i, el := range x {
x[i] = transformKeyValueShape(el)
}
return x
case map[string]interface{}:
for k, vv := range x {
x[k] = transformKeyValueShape(vv)
}
return x
default:
return v
}
}
// arrowArrayToSlice 将 Arrow Array 转换为 Go slice
func arrowArrayToSlice(arr arrow.Array) []interface{} {
if arr == nil {
return nil
}
result := make([]interface{}, arr.Len())
for i := 0; i < arr.Len(); i++ {
if arr.IsNull(i) {
result[i] = nil
continue
}
result[i] = arrowValueToInterface(arr, i)
}
return result
}
// arrowValueToInterface 将 Arrow Array 的单个元素转换为 Go interface{}
func arrowValueToInterface(arr arrow.Array, index int) interface{} {
if arr.IsNull(index) {
return nil
}
switch data := arr.(type) {
case *array.Boolean:
return data.Value(index)
case *array.Int8:
return data.Value(index)
case *array.Int16:
return data.Value(index)
case *array.Int32:
return data.Value(index)
case *array.Int64:
return data.Value(index)
case *array.Uint8:
return data.Value(index)
case *array.Uint16:
return data.Value(index)
case *array.Uint32:
return data.Value(index)
case *array.Uint64:
return data.Value(index)
case *array.Float32:
return data.Value(index)
case *array.Float64:
return data.Value(index)
case *array.String:
return data.Value(index)
case *array.Binary:
return data.Value(index)
case *array.Timestamp:
return data.Value(index).ToTime(arrow.Microsecond)
case *array.Date32:
return time.Unix(int64(data.Value(index))*86400, 0).UTC()
case *array.List:
case *array.LargeList:
start, end := data.ValueOffsets(index)
listValues := data.ListValues()
result := make([]interface{}, end-start)
for i := int(start); i < int(end); i++ {
result[i-int(start)] = arrowValueToInterface(listValues, i)
}
return result
case *array.FixedSizeList:
listSize := data.DataType().(*arrow.FixedSizeListType).Len()
start := index * int(listSize)
end := start + int(listSize)
listValues := data.ListValues()
result := make([]interface{}, listSize)
for i := start; i < end; i++ {
result[i-start] = arrowValueToInterface(listValues, i)
}
return result
case *array.Map:
return arrowMapToGoMap(data, index)
case *array.Struct:
return arrowStructToGoMap(data, index)
default:
// 对于不支持的类型,返回字符串表示
return arr.(fmt.Stringer).String()
}
return nil
}
// arrowMapToGoMap 将 Arrow Map 转换为 Go map
func arrowMapToGoMap(data *array.Map, index int) map[string]interface{} {
if data.IsNull(index) {
return nil
}
start, end := data.ValueOffsets(index)
result := make(map[string]interface{}, end-start)
keys := data.Keys()
items := data.Items()
for j := int(start); j < int(end); j++ {
var keyStr string
if strKeys, ok := keys.(*array.String); ok {
keyStr = strKeys.Value(j)
} else {
// 非字符串 key,尝试转换
keyStr = fmt.Sprintf("%v", arrowValueToInterface(keys, j))
}
result[keyStr] = arrowValueToInterface(items, j)
}
return result
}
// arrowStructToGoMap 将 Arrow Struct 转换为 Go map
func arrowStructToGoMap(data *array.Struct, index int) map[string]interface{} {
if data.IsNull(index) {
return nil
}
structType := data.DataType().(*arrow.StructType)
result := make(map[string]interface{}, data.NumField())
for i := 0; i < data.NumField(); i++ {
fieldName := structType.Field(i).Name
fieldArray := data.Field(i)
result[fieldName] = arrowValueToInterface(fieldArray, index)
}
return result
}
// arrowMapToTypedGoMap converts Arrow Map to a typed Go map based on value type
func arrowMapToTypedGoMap(data *array.Map, index int) interface{} {
if data.IsNull(index) {
return nil
}
start, end := data.ValueOffsets(index)
keys := data.Keys()
items := data.Items()
switch itemData := items.(type) {
case *array.String:
result := make(map[string]string, end-start)
for j := int(start); j < int(end); j++ {
keyStr := getMapKeyString(keys, j)
if !itemData.IsNull(j) {
result[keyStr] = itemData.Value(j)
} else {
result[keyStr] = ""
}
}
return result
case *array.Int64:
result := make(map[string]int64, end-start)
for j := int(start); j < int(end); j++ {
keyStr := getMapKeyString(keys, j)
if !itemData.IsNull(j) {
result[keyStr] = itemData.Value(j)
}
}
return result
case *array.Int32:
result := make(map[string]int32, end-start)
for j := int(start); j < int(end); j++ {
keyStr := getMapKeyString(keys, j)
if !itemData.IsNull(j) {
result[keyStr] = itemData.Value(j)
}
}
return result
case *array.Float64:
result := make(map[string]float64, end-start)
for j := int(start); j < int(end); j++ {
keyStr := getMapKeyString(keys, j)
if !itemData.IsNull(j) {
result[keyStr] = itemData.Value(j)
}
}
return result
case *array.Boolean:
result := make(map[string]bool, end-start)
for j := int(start); j < int(end); j++ {
keyStr := getMapKeyString(keys, j)
if !itemData.IsNull(j) {
result[keyStr] = itemData.Value(j)
}
}
return result
default:
return arrowMapToGoMap(data, index)
}
}
// getMapKeyString extracts string key from Arrow array
func getMapKeyString(keys arrow.Array, index int) string {
if strKeys, ok := keys.(*array.String); ok {
return strKeys.Value(index)
}
return fmt.Sprintf("%v", arrowValueToInterface(keys, index))
}
// convertListToTypedSlice converts Arrow list values to a typed Go slice
func convertListToTypedSlice(listValues arrow.Array, start, end int) interface{} {
if listValues == nil {
return []interface{}{}
}
if start >= end {
switch listValues.(type) {
case *array.String:
return []string{}
case *array.Int64:
return []int64{}
case *array.Int32:
return []int32{}
case *array.Float64:
return []float64{}
case *array.Boolean:
return []bool{}
default:
return []interface{}{}
}
}
switch data := listValues.(type) {
case *array.String:
result := make([]string, end-start)
for j := start; j < end; j++ {
if data.IsNull(j) {
result[j-start] = ""
} else {
result[j-start] = data.Value(j)
}
}
return result
case *array.Int64:
result := make([]int64, end-start)
for j := start; j < end; j++ {
if !data.IsNull(j) {
result[j-start] = data.Value(j)
}
}
return result
case *array.Int32:
result := make([]int32, end-start)
for j := start; j < end; j++ {
if !data.IsNull(j) {
result[j-start] = data.Value(j)
}
}
return result
case *array.Float64:
result := make([]float64, end-start)
for j := start; j < end; j++ {
if !data.IsNull(j) {
result[j-start] = data.Value(j)
}
}
return result
case *array.Boolean:
result := make([]bool, end-start)
for j := start; j < end; j++ {
if !data.IsNull(j) {
result[j-start] = data.Value(j)
}
}
return result
default:
result := make([]interface{}, end-start)
for j := start; j < end; j++ {
result[j-start] = arrowValueToInterface(listValues, j)
}
return result
}
}
func processIntValues(srcValue arrow.Array, destcol []interface{}, scale int64, higherPrecision bool) {
var divisor float64
if scale != 0 && !higherPrecision {
divisor = math.Pow10(int(scale))
}
switch data := srcValue.(type) {
case *array.Int64:
for i, val := range data.Int64Values() {
if !srcValue.IsNull(i) {
destcol[i] = formatIntValue(int64(val), scale, higherPrecision, divisor)
}
}
case *array.Int32:
for i, val := range data.Int32Values() {
if !srcValue.IsNull(i) {
destcol[i] = formatIntValue(int64(val), scale, higherPrecision, divisor)
}
}
case *array.Int16:
for i, val := range data.Int16Values() {
if !srcValue.IsNull(i) {
destcol[i] = formatIntValue(int64(val), scale, higherPrecision, divisor)
}
}
case *array.Int8:
for i, val := range data.Int8Values() {
if !srcValue.IsNull(i) {
destcol[i] = formatIntValue(int64(val), scale, higherPrecision, divisor)
}
}
}
}
func formatIntValue(val int64, scale int64, higherPrecision bool, divisor float64) interface{} {
if scale == 0 {
if higherPrecision {
return val
}
return fmt.Sprintf("%d", val)
}
if higherPrecision {
return intToBigFloat(val, scale)
}
return fmt.Sprintf("%.*f", scale, float64(val)/divisor)
}
func arrowToValue(
destcol []interface{},
srcColumnMeta execResponseColumnType,
srcValue arrow.Array,
loc *time.Location,
higherPrecision bool) error {
var err error
if len(destcol) != srcValue.Len() {
err = fmt.Errorf("array interface length mismatch")
}
logger.Debugf("clickzetta data type: %v, arrow data type: %v", srcColumnMeta.Type, srcValue.DataType())
switch getclickzettaType(strings.ToUpper(srcColumnMeta.Type)) {
case DECIMAL, TINYINT, SMALLINT, INT, BIGINT:
switch data := srcValue.(type) {
case *array.Decimal128:
var divisor *big.Float
if srcColumnMeta.Scale != 0 {
divisor = new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(srcColumnMeta.Scale), nil))
}
for i, num := range data.Values() {
if !srcValue.IsNull(i) {
if srcColumnMeta.Scale == 0 {
if higherPrecision {
destcol[i] = num.BigInt()
} else {
destcol[i] = num.ToString(0)
}
} else {
f := decimalToBigFloat(num, divisor)
if higherPrecision {
destcol[i] = f
} else {
destcol[i] = fmt.Sprintf("%.*f", srcColumnMeta.Scale, f)
}
}
}
}
case *array.Int64, *array.Int32, *array.Int16, *array.Int8:
processIntValues(data, destcol, srcColumnMeta.Scale, higherPrecision)
}
return err
case ARRAY:
switch data := srcValue.(type) {
case *array.List:
for i := range destcol {
if !srcValue.IsNull(i) {
start, end := data.ValueOffsets(i)
listValues := data.ListValues()
destcol[i] = convertListToTypedSlice(listValues, int(start), int(end))
}
}
case *array.LargeList:
for i := range destcol {
if !srcValue.IsNull(i) {
start, end := data.ValueOffsets(i)
listValues := data.ListValues()
destcol[i] = convertListToTypedSlice(listValues, int(start), int(end))
}
}
case *array.FixedSizeList:
for i := range destcol {
if !srcValue.IsNull(i) {
listSize := data.DataType().(*arrow.FixedSizeListType).Len()
start := i * int(listSize)
end := start + int(listSize)
listValues := data.ListValues()
destcol[i] = convertListToTypedSlice(listValues, start, end)
}
}
default:
return fmt.Errorf("unsupported ARRAY arrow type: %T", srcValue)
}
return err
case MAP:
// convert Arrow Map to typed Go map
data := srcValue.(*array.Map)
for i := range destcol {
if !srcValue.IsNull(i) {
destcol[i] = arrowMapToTypedGoMap(data, i)
}
}
return err
case STRUCT:
if data, ok := srcValue.(*array.Struct); ok {
for i := range destcol {
if !srcValue.IsNull(i) {
destcol[i] = arrowStructToGoMap(data, i)
}
}
return err
}
return fmt.Errorf("unsupported STRUCT arrow type: %T", srcValue)
case BOOLEAN:
boolData := srcValue.(*array.Boolean)
for i := range destcol {
if !srcValue.IsNull(i) {
destcol[i] = boolData.Value(i)
}
}
return err
case DOUBLE:
for i, flt64 := range srcValue.(*array.Float64).Float64Values() {
if !srcValue.IsNull(i) {
destcol[i] = flt64
}
}
return err
case FLOAT:
for i, flt32 := range srcValue.(*array.Float32).Float32Values() {
if !srcValue.IsNull(i) {
destcol[i] = flt32
}
}
return err
case STRING, VARCHAR, CHAR, JSON:
str := srcValue.(*array.String)
for i := range destcol {
if !srcValue.IsNull(i) {
destcol[i] = str.Value(i)
}
}
return err
case VECTOR, VECTOR_TYPE:
// Vector data is transmitted as List/FixedSizeList in Arrow format,
// similar to ARRAY but specifically for vector types.
switch data := srcValue.(type) {
case *array.List:
for i := range destcol {
if !srcValue.IsNull(i) {
start, end := data.ValueOffsets(i)
listValues := data.ListValues()
destcol[i] = convertListToTypedSlice(listValues, int(start), int(end))
}
}
case *array.LargeList:
for i := range destcol {
if !srcValue.IsNull(i) {
start, end := data.ValueOffsets(i)
listValues := data.ListValues()
destcol[i] = convertListToTypedSlice(listValues, int(start), int(end))
}
}
case *array.FixedSizeList:
for i := range destcol {
if !srcValue.IsNull(i) {
listSize := data.DataType().(*arrow.FixedSizeListType).Len()
start := i * int(listSize)
end := start + int(listSize)
listValues := data.ListValues()
destcol[i] = convertListToTypedSlice(listValues, start, end)
}
}
case *array.String:
// Fallback: some responses may still encode vector as string
for i := range destcol {
if !srcValue.IsNull(i) {
destcol[i] = data.Value(i)
}
}
default:
return fmt.Errorf("unsupported VECTOR arrow type: %T", srcValue)
}
return err
case DATE:
for i, date32 := range srcValue.(*array.Date32).Date32Values() {
if !srcValue.IsNull(i) {
t0 := time.Unix(int64(date32)*86400, 0).UTC()
destcol[i] = t0
}
}
return err
case TIMESTAMP_LTZ:
for i, t := range srcValue.(*array.Timestamp).TimestampValues() {
if !srcValue.IsNull(i) {
destcol[i] = t.ToTime(arrow.Microsecond).UTC()
}
}
return err
case TIMESTAMP_NTZ:
for i, t := range srcValue.(*array.Timestamp).TimestampValues() {
if !srcValue.IsNull(i) {
destcol[i] = t.ToTime(arrow.Microsecond)
}
}
return err
}
return fmt.Errorf("unsupported data type")
}
type TypedNullTime struct {
Time sql.NullTime
TzType timezoneType
}
func convertTzTypeToClickzettaType(tzType timezoneType) clickzettaType {
switch tzType {
case TimestampLTZType:
return TIMESTAMP_LTZ
case DateType:
return DATE
}
return NOT_SUPPORTED
}