-
Notifications
You must be signed in to change notification settings - Fork 499
/
types.go
146 lines (136 loc) · 3.63 KB
/
types.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
package client
import (
"strconv"
"strings"
"github.com/apache/arrow/go/v16/arrow"
"github.com/cloudquery/plugin-sdk/v4/types"
)
const defaultPrecision = 10
const defaultScale = 0
func isUnsigned(sqlType string) bool {
return strings.Contains(sqlType, "unsigned")
}
func parseStringValue(str string, defaultValue int32) int32 {
val, err := strconv.ParseInt(str, 10, 32)
if err != nil {
return defaultValue
}
return int32(val)
}
func getPrecisionAndScale(dataType string) (precision, scale int32) {
str := strings.TrimPrefix(dataType, "decimal")
str = strings.TrimPrefix(str, "numeric")
if str == "" {
return defaultPrecision, defaultScale
}
str = strings.TrimPrefix(str, "(")
str = strings.TrimSuffix(str, ")")
parts := strings.Split(str, ",")
switch len(parts) {
case 1:
precision = parseStringValue(parts[0], defaultPrecision)
scale = defaultScale
case 2:
precision = parseStringValue(parts[0], defaultPrecision)
scale = parseStringValue(parts[1], defaultScale)
default:
precision = defaultPrecision
scale = defaultScale
}
return precision, scale
}
func mySQLTypeToArrowType(sqlType string) arrow.DataType {
if sqlType == "binary(16)" {
return types.ExtensionTypes.UUID
}
if sqlType == "tinyint(1)" {
return arrow.FixedWidthTypes.Boolean
}
if strings.HasPrefix(sqlType, "datetime") {
return arrow.FixedWidthTypes.Timestamp_us
}
if strings.HasPrefix(sqlType, "decimal") || strings.HasPrefix(sqlType, "numeric") {
precision, scale := getPrecisionAndScale(sqlType)
return &arrow.Decimal128Type{Precision: precision, Scale: scale}
}
if strings.HasPrefix(sqlType, "tinyint") {
if isUnsigned(sqlType) {
return arrow.PrimitiveTypes.Uint8
}
return arrow.PrimitiveTypes.Int8
}
if strings.HasPrefix(sqlType, "smallint") {
if isUnsigned(sqlType) {
return arrow.PrimitiveTypes.Uint16
}
return arrow.PrimitiveTypes.Int16
}
if strings.HasPrefix(sqlType, "int") {
if isUnsigned(sqlType) {
return arrow.PrimitiveTypes.Uint32
}
return arrow.PrimitiveTypes.Int32
}
if strings.HasPrefix(sqlType, "bigint") {
if isUnsigned(sqlType) {
return arrow.PrimitiveTypes.Uint64
}
return arrow.PrimitiveTypes.Int64
}
switch sqlType {
case "bool", "boolean":
return arrow.FixedWidthTypes.Boolean
case "float":
return arrow.PrimitiveTypes.Float32
case "double":
return arrow.PrimitiveTypes.Float64
case "timestamp":
return arrow.FixedWidthTypes.Timestamp_us
case "json":
return types.ExtensionTypes.JSON
case "binary", "blob":
return arrow.BinaryTypes.Binary
}
return arrow.BinaryTypes.String
}
func arrowTypeToMySqlStr(t arrow.DataType) string {
switch t.(type) {
case *arrow.BooleanType:
// we can use `bool` which is an alias for `tinyint(1)` but since MySQL information schema returns `tinyint(1)` we use it here as well
// to be aligned with `mySQLTypeToArrowType`
return "tinyint(1)"
case *arrow.Int8Type:
return "tinyint"
case *arrow.Int16Type:
return "smallint"
case *arrow.Int32Type:
return "int"
case *arrow.Int64Type:
return "bigint"
case *arrow.Uint8Type:
return "tinyint unsigned"
case *arrow.Uint16Type:
return "smallint unsigned"
case *arrow.Uint32Type:
return "int unsigned"
case *arrow.Uint64Type:
return "bigint unsigned"
case *arrow.Float32Type:
return "float"
case *arrow.Float64Type:
return "double"
case *arrow.StringType, *arrow.LargeStringType:
return "text"
case *arrow.BinaryType, *arrow.LargeBinaryType:
return "blob"
// nolint:typecheck
case *types.UUIDType:
return "binary(16)"
case *arrow.TimestampType:
return "datetime(6)"
case *arrow.StructType, *arrow.ListType, *types.JSONType:
return "json"
default:
return "text"
}
}