-
Notifications
You must be signed in to change notification settings - Fork 500
/
to_pg.go
199 lines (194 loc) · 5.05 KB
/
to_pg.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
package pgarrow
import (
"strconv"
"github.com/apache/arrow/go/v16/arrow"
cqtypes "github.com/cloudquery/plugin-sdk/v4/types"
)
func ArrowToPg10(t arrow.DataType) string {
switch dt := t.(type) {
case *arrow.BooleanType:
return "boolean"
case *arrow.Int8Type:
return "smallint"
case *arrow.Int16Type:
return "smallint"
case *arrow.Int32Type:
return "int"
case *arrow.Int64Type:
return "bigint"
case *arrow.Uint8Type:
return "smallint"
case *arrow.Uint16Type:
return "int"
case *arrow.Uint32Type:
return "bigint"
case *arrow.Uint64Type:
return "numeric(20,0)"
case *arrow.Float32Type:
return "real"
case *arrow.Float64Type:
return "double precision"
case arrow.DecimalType:
return "numeric(" + strconv.Itoa(int(dt.GetPrecision())) + "," + strconv.Itoa(int(dt.GetScale())) + ")"
case *arrow.StringType:
return "text"
case *arrow.BinaryType:
return "bytea"
case *arrow.LargeBinaryType:
return "bytea"
case *arrow.TimestampType:
return "timestamp without time zone"
case *arrow.Time32Type, *arrow.Time64Type:
return "time without time zone"
case *arrow.Date32Type, *arrow.Date64Type:
return "date"
case *cqtypes.UUIDType:
return "uuid"
case *cqtypes.JSONType:
return "jsonb"
case *cqtypes.MACType:
return "macaddr"
case *cqtypes.InetType:
return "inet"
case *arrow.ListType:
return ArrowToPg10(dt.Elem()) + "[]"
case *arrow.FixedSizeListType:
return ArrowToPg10(dt.Elem()) + "[]"
case *arrow.LargeListType:
return ArrowToPg10(dt.Elem()) + "[]"
case *arrow.MapType:
return "text"
default:
return "text"
}
}
// ArrowToCockroach converts arrow data type to cockroach data type. CockroachDB lacks support for
// some data types like macaddr and has different aliases for ints.
// See: https://www.cockroachlabs.com/docs/stable/int.html
func ArrowToCockroach(t arrow.DataType) string {
switch dt := t.(type) {
case *arrow.BooleanType:
return "boolean"
case *arrow.Int8Type:
return "int2"
case *arrow.Int16Type:
return "int2"
case *arrow.Int32Type:
return "int8"
case *arrow.Int64Type:
return "int8"
case *arrow.Uint8Type:
return "int2"
case *arrow.Uint16Type:
return "int8"
case *arrow.Uint32Type:
return "int8"
case *arrow.Uint64Type:
return "numeric(20,0)"
case *arrow.Float32Type:
return "real"
case *arrow.Float64Type:
return "double precision"
case arrow.DecimalType:
return "numeric(" + strconv.Itoa(int(dt.GetPrecision())) + "," + strconv.Itoa(int(dt.GetScale())) + ")"
case *arrow.StringType:
return "text"
case *arrow.BinaryType:
return "bytea"
case *arrow.LargeBinaryType:
return "bytea"
case *arrow.TimestampType:
return "timestamp without time zone"
case *arrow.Time32Type, *arrow.Time64Type:
return "time without time zone"
case *arrow.Date32Type, *arrow.Date64Type:
return "date"
case *cqtypes.UUIDType:
return "uuid"
case *cqtypes.JSONType:
return "jsonb"
case *cqtypes.MACType:
return "text"
case *cqtypes.InetType:
return "inet"
case *arrow.ListType:
return ArrowToCockroach(dt.Elem()) + "[]"
case *arrow.FixedSizeListType:
return ArrowToCockroach(dt.Elem()) + "[]"
case *arrow.LargeListType:
return ArrowToCockroach(dt.Elem()) + "[]"
case *arrow.MapType:
return "text"
default:
return "text"
}
}
func ArrowToCrateDB(t arrow.DataType) string {
switch dt := t.(type) {
case *arrow.BooleanType:
return "boolean"
case *arrow.Int8Type:
return "smallint"
case *arrow.Int16Type:
return "smallint"
case *arrow.Int32Type:
return "int"
case *arrow.Int64Type:
return "bigint"
case *arrow.Uint8Type:
return "smallint"
case *arrow.Uint16Type:
return "int"
case *arrow.Uint32Type:
return "bigint"
case *arrow.Uint64Type:
// CrateDB does not support storing numeric types.
return "text"
case *arrow.Float32Type:
return "real"
case *arrow.Float64Type:
return "double precision"
case arrow.DecimalType:
// CrateDB does not support storing numeric types
return "text"
case *arrow.StringType:
return "text"
case *arrow.BinaryType:
// CrateDB does not support bytea
return "text"
case *arrow.LargeBinaryType:
// CrateDB does not support bytea
return "text"
case *arrow.TimestampType:
return "timestamp without time zone"
case *arrow.Time32Type, *arrow.Time64Type:
// CrateDB does not support "time without time zone", and does not support
// storage of "time with time zone".
return "text"
case *arrow.Date32Type, *arrow.Date64Type:
// CrateDB does not support storing "date" type
return "timestamp without time zone"
case *cqtypes.UUIDType:
// CrateDB does not support UUID type
return "text"
case *cqtypes.JSONType:
// CrateDB calls JSON type "object"
return "object"
case *cqtypes.MACType:
// CrateDB does not support macaddr type
return "text"
case *cqtypes.InetType:
// CrateDB does not support inet type, but we can use IP
return "ip"
case *arrow.ListType:
return ArrowToCrateDB(dt.Elem()) + "[]"
case *arrow.FixedSizeListType:
return ArrowToCrateDB(dt.Elem()) + "[]"
case *arrow.LargeListType:
return ArrowToCrateDB(dt.Elem()) + "[]"
case *arrow.MapType:
return "text"
default:
return "text"
}
}