-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathqvalue_convert.go
More file actions
211 lines (195 loc) · 6.8 KB
/
Copy pathqvalue_convert.go
File metadata and controls
211 lines (195 loc) · 6.8 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
package connbigquery
import (
"fmt"
"cloud.google.com/go/bigquery"
"github.com/PeerDB-io/peerdb/flow/generated/protos"
"github.com/PeerDB-io/peerdb/flow/shared/datatypes"
"github.com/PeerDB-io/peerdb/flow/shared/types"
)
func qValueKindToBigQueryType(columnDescription *protos.FieldDescription, nullableEnabled bool) bigquery.FieldSchema {
bqField := bigquery.FieldSchema{
Name: columnDescription.Name,
Required: nullableEnabled && !columnDescription.Nullable,
}
switch types.QValueKind(columnDescription.Type) {
// boolean
case types.QValueKindBoolean:
bqField.Type = bigquery.BooleanFieldType
// integer types
case types.QValueKindInt8, types.QValueKindInt16, types.QValueKindInt32, types.QValueKindInt64,
types.QValueKindUInt8, types.QValueKindUInt16, types.QValueKindUInt32, types.QValueKindUInt64,
types.QValueKindUint16Enum, types.QValueKindUint64Set:
bqField.Type = bigquery.IntegerFieldType
// decimal types
case types.QValueKindFloat32, types.QValueKindFloat64:
bqField.Type = bigquery.FloatFieldType
case types.QValueKindNumeric:
precision, scale := datatypes.GetNumericTypeForWarehouse(columnDescription.TypeModifier, datatypes.BigQueryNumericCompatibility{})
bqField.Type = bigquery.BigNumericFieldType
bqField.Precision = int64(precision)
bqField.Scale = int64(scale)
case types.QValueKindArrayNumeric:
precision, scale := datatypes.GetNumericTypeForWarehouse(columnDescription.TypeModifier, datatypes.BigQueryNumericCompatibility{})
bqField.Type = bigquery.BigNumericFieldType
bqField.Precision = int64(precision)
bqField.Scale = int64(scale)
bqField.Repeated = true
// string related
case types.QValueKindString, types.QValueKindEnum:
bqField.Type = bigquery.StringFieldType
// json related
case types.QValueKindJSON, types.QValueKindJSONB, types.QValueKindHStore:
bqField.Type = bigquery.JSONFieldType
// time related
case types.QValueKindTimestamp, types.QValueKindTimestampTZ:
bqField.Type = bigquery.TimestampFieldType
case types.QValueKindDate:
bqField.Type = bigquery.DateFieldType
case types.QValueKindTime, types.QValueKindTimeTZ:
bqField.Type = bigquery.TimeFieldType
// bytes
case types.QValueKindBytes:
bqField.Type = bigquery.BytesFieldType
case types.QValueKindArrayInt16, types.QValueKindArrayInt32, types.QValueKindArrayInt64:
bqField.Type = bigquery.IntegerFieldType
bqField.Repeated = true
case types.QValueKindArrayFloat32, types.QValueKindArrayFloat64:
bqField.Type = bigquery.FloatFieldType
bqField.Repeated = true
case types.QValueKindArrayBoolean:
bqField.Type = bigquery.BooleanFieldType
bqField.Repeated = true
case types.QValueKindArrayTimestamp, types.QValueKindArrayTimestampTZ:
bqField.Type = bigquery.TimestampFieldType
bqField.Repeated = true
case types.QValueKindArrayDate:
bqField.Type = bigquery.DateFieldType
bqField.Repeated = true
case types.QValueKindArrayString, types.QValueKindArrayEnum, types.QValueKindArrayInterval:
bqField.Type = bigquery.StringFieldType
bqField.Repeated = true
case types.QValueKindGeography, types.QValueKindGeometry, types.QValueKindPoint:
bqField.Type = bigquery.GeographyFieldType
// UUID related - stored as strings
case types.QValueKindUUID:
bqField.Type = bigquery.StringFieldType
case types.QValueKindArrayUUID:
bqField.Type = bigquery.StringFieldType
bqField.Repeated = true
// rest will be strings
default:
bqField.Type = bigquery.StringFieldType
}
return bqField
}
// BigQueryTypeToQValueKind converts a bigquery.FieldType to a QValueKind
func BigQueryTypeToQValueKind(fieldSchema *bigquery.FieldSchema) types.QValueKind {
switch fieldSchema.Type {
case bigquery.StringFieldType:
if fieldSchema.Repeated {
return types.QValueKindArrayString
}
return types.QValueKindString
case bigquery.BytesFieldType:
return types.QValueKindBytes
case bigquery.IntegerFieldType:
if fieldSchema.Repeated {
return types.QValueKindArrayInt64
}
return types.QValueKindInt64
case bigquery.FloatFieldType:
if fieldSchema.Repeated {
return types.QValueKindArrayFloat64
}
return types.QValueKindFloat64
case bigquery.BooleanFieldType:
if fieldSchema.Repeated {
return types.QValueKindArrayBoolean
}
return types.QValueKindBoolean
case bigquery.TimestampFieldType:
if fieldSchema.Repeated {
return types.QValueKindArrayTimestamp
}
return types.QValueKindTimestamp
case bigquery.DateTimeFieldType:
return types.QValueKindTimestamp
case bigquery.DateFieldType:
if fieldSchema.Repeated {
return types.QValueKindArrayDate
}
return types.QValueKindDate
case bigquery.TimeFieldType:
return types.QValueKindTime
case bigquery.NumericFieldType, bigquery.BigNumericFieldType:
if fieldSchema.Repeated {
return types.QValueKindArrayNumeric
}
return types.QValueKindNumeric
case bigquery.GeographyFieldType:
return types.QValueKindGeography
case bigquery.JSONFieldType:
return types.QValueKindJSON
case bigquery.RecordFieldType:
// We treat RECORD as STRING or ARRAY<STRING>
// In the future, we can consider mapping to JSON.
if fieldSchema.Repeated {
return types.QValueKindArrayString
}
return types.QValueKindString
default:
return types.QValueKindInvalid
}
}
func fieldNormalizedTypeName(field *bigquery.FieldSchema) string {
typeName := createTableCompatibleTypeName(field.Type)
switch field.Type {
case bigquery.StringFieldType, bigquery.BytesFieldType:
if field.MaxLength == 0 {
break
}
typeName = fmt.Sprintf("%s(%d)", typeName, field.MaxLength)
case bigquery.NumericFieldType, bigquery.BigNumericFieldType:
if field.Precision == 0 {
break
}
if field.Scale > 0 {
typeName = fmt.Sprintf("%s(%d,%d)", typeName, field.Precision, field.Scale)
} else {
typeName = fmt.Sprintf("%s(%d)", typeName, field.Precision)
}
}
if field.Repeated {
typeName = fmt.Sprintf("ARRAY<%s>", typeName)
}
return typeName
}
func createTableCompatibleTypeName(schemaType bigquery.FieldType) string {
if schemaType == bigquery.FloatFieldType {
return "FLOAT64"
}
if schemaType == bigquery.BooleanFieldType {
return "BOOL"
}
return string(schemaType)
}
func qValueKindToBigQueryTypeString(columnDescription *protos.FieldDescription, nullEnabled bool, forMerge bool) string {
bqTypeSchema := qValueKindToBigQueryType(columnDescription, nullEnabled)
bqType := createTableCompatibleTypeName(bqTypeSchema.Type)
if bqTypeSchema.Type == bigquery.BigNumericFieldType && !forMerge {
bqType = fmt.Sprintf("BIGNUMERIC(%d,%d)", bqTypeSchema.Precision, bqTypeSchema.Scale)
}
if bqTypeSchema.Repeated && !forMerge {
return "ARRAY<" + bqType + ">"
}
return bqType
}
func BigQueryFieldToQField(bqField *bigquery.FieldSchema) types.QField {
return types.QField{
Name: bqField.Name,
Type: BigQueryTypeToQValueKind(bqField),
Precision: int16(bqField.Precision),
Scale: int16(bqField.Scale),
Nullable: !bqField.Required,
}
}