-
Notifications
You must be signed in to change notification settings - Fork 5
/
schema.go
246 lines (231 loc) · 8.03 KB
/
schema.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
package protobq
import (
"fmt"
"strings"
"cloud.google.com/go/bigquery"
"go.einride.tech/aip/fieldbehavior"
"go.einride.tech/protobuf-bigquery/internal/wkt"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
// InferSchema infers a BigQuery schema for the given proto.Message using default options.
func InferSchema(msg proto.Message) bigquery.Schema {
return SchemaOptions{}.InferSchema(msg)
}
// SchemaOptions contains configuration options for BigQuery schema inference.
type SchemaOptions struct {
// UseEnumNumbers converts enum values to INTEGER types.
UseEnumNumbers bool
// UseDateTimeWithoutOffset converts google.type.DateTime values to DATETIME, discarding the optional time offset.
UseDateTimeWithoutOffset bool
// UseOneofFields adds an extra STRING field for oneof fields with the name of the oneof,
// containing the name of the field that is set.
UseOneofFields bool
// UseModeFromFieldBehavior sets the mode of a field to REQUIRED if the field is defined with REQUIRED behavior
// in proto.
UseModeFromFieldBehavior bool
// UseProtoCommentsAsDescription use the proto comments to populate the description of the BigQuery field
UseProtoCommentsAsDescription bool
// UseJSONStructs use JSON type instead of string for google.protobuf.Struct type
UseJSONStructs bool
// UseJSONValues use JSON type instead of string for google.protobuf.Value type
UseJSONValues bool
}
// InferSchema infers a BigQuery schema for the given proto.Message using options in
// MarshalOptions.
func (o SchemaOptions) InferSchema(msg proto.Message) bigquery.Schema {
return o.InferMessageSchema(msg.ProtoReflect().Descriptor())
}
// InferMessageSchema infers the BigQuery schema for the given protoreflect.MessageDescriptor.
func (o SchemaOptions) InferMessageSchema(msg protoreflect.MessageDescriptor) bigquery.Schema {
schema := make(bigquery.Schema, 0, msg.Fields().Len())
for i := 0; i < msg.Fields().Len(); i++ {
fieldSchema := o.inferFieldSchema(msg.Fields().Get(i))
if fieldSchema != nil {
schema = append(schema, fieldSchema)
}
}
if o.UseOneofFields {
for i := 0; i < msg.Oneofs().Len(); i++ {
oneof := msg.Oneofs().Get(i)
// The `optional` keyword creates a synthetic field treated as an oneof
// which creates a new field with _ as prefix missmatching the schema creation
if oneof.IsSynthetic() {
continue
}
schema = append(schema, o.inferOneofFieldSchema(oneof))
}
}
return schema
}
func (o SchemaOptions) inferFieldSchema(field protoreflect.FieldDescriptor) *bigquery.FieldSchema {
if field.IsMap() {
return o.inferMapFieldSchema(field)
}
if (field.Kind() == protoreflect.MessageKind || field.Kind() == protoreflect.GroupKind) &&
field.Message().FullName() == wkt.DateTime {
return o.inferDateTimeFieldSchema(field)
}
fieldSchema := &bigquery.FieldSchema{
Name: string(field.Name()),
Type: o.inferFieldSchemaType(field),
Repeated: field.IsList(),
Description: o.buildDescription(field),
}
if fieldSchema.Type == bigquery.RecordFieldType && fieldSchema.Schema == nil {
fieldSchema.Schema = o.InferMessageSchema(field.Message())
if len(fieldSchema.Schema) == 0 {
return nil
}
}
if o.UseModeFromFieldBehavior {
fieldSchema.Required = fieldbehavior.Has(field, annotations.FieldBehavior_REQUIRED)
}
return fieldSchema
}
func appendNonEmpty(parts []string, value string) []string {
trimmed := strings.TrimSpace(value)
if len(trimmed) > 0 {
parts = append(parts, trimmed)
}
return parts
}
func (o SchemaOptions) buildDescription(field protoreflect.FieldDescriptor) string {
if !o.UseProtoCommentsAsDescription {
return ""
}
sourceLocation := field.ParentFile().SourceLocations().ByDescriptor(field)
var parts []string
for _, comment := range strings.Split(sourceLocation.LeadingComments, "\n") {
parts = appendNonEmpty(parts, comment)
}
parts = appendNonEmpty(parts, sourceLocation.TrailingComments)
// Prune the result in case it is long. The maximum description supported in bigquery is 1024 characters.
// https://cloud.google.com/bigquery/docs/schemas#column_descriptions
result := []rune(strings.Join(parts, "\n"))
if len(result) > 1024 {
return string(result[:1024])
}
return string(result)
}
func (o SchemaOptions) inferOneofFieldSchema(oneof protoreflect.OneofDescriptor) *bigquery.FieldSchema {
fieldSchema := &bigquery.FieldSchema{
Name: string(oneof.Name()),
Type: bigquery.StringFieldType,
}
oneofFieldNames := make([]string, 0, oneof.Fields().Len())
for i := 0; i < oneof.Fields().Len(); i++ {
field := oneof.Fields().Get(i)
oneofFieldNames = append(oneofFieldNames, string(field.Name()))
}
fieldSchema.Description = fmt.Sprintf("One of: %s.", strings.Join(oneofFieldNames, ", "))
return fieldSchema
}
func (o SchemaOptions) inferDateTimeFieldSchema(field protoreflect.FieldDescriptor) *bigquery.FieldSchema {
fieldSchema := &bigquery.FieldSchema{
Name: string(field.Name()),
Repeated: field.IsList(),
Description: o.buildDescription(field),
}
if o.UseDateTimeWithoutOffset {
fieldSchema.Type = bigquery.DateTimeFieldType
} else {
fieldSchema.Type = bigquery.RecordFieldType
fieldSchema.Schema = bigquery.Schema{
{Name: "datetime", Type: bigquery.DateTimeFieldType},
{Name: "utc_offset", Type: bigquery.FloatFieldType},
{
Name: "time_zone",
Type: bigquery.RecordFieldType,
Schema: bigquery.Schema{
{Name: "id", Type: bigquery.StringFieldType},
{Name: "version", Type: bigquery.StringFieldType},
},
},
}
}
return fieldSchema
}
func (o SchemaOptions) inferFieldSchemaType(field protoreflect.FieldDescriptor) bigquery.FieldType {
switch field.Kind() {
case protoreflect.DoubleKind, protoreflect.FloatKind:
return bigquery.FloatFieldType
case protoreflect.Int64Kind,
protoreflect.Uint64Kind,
protoreflect.Int32Kind,
protoreflect.Fixed64Kind,
protoreflect.Fixed32Kind,
protoreflect.Uint32Kind,
protoreflect.Sfixed32Kind,
protoreflect.Sfixed64Kind,
protoreflect.Sint32Kind,
protoreflect.Sint64Kind:
return bigquery.IntegerFieldType
case protoreflect.BoolKind:
return bigquery.BooleanFieldType
case protoreflect.StringKind:
return bigquery.StringFieldType
case protoreflect.BytesKind:
return bigquery.BytesFieldType
case protoreflect.EnumKind:
return o.inferEnumFieldType(field)
case protoreflect.MessageKind, protoreflect.GroupKind:
switch field.Message().FullName() {
case wkt.Timestamp:
return bigquery.TimestampFieldType
case wkt.Duration:
return bigquery.FloatFieldType
case wkt.DoubleValue, wkt.FloatValue:
return bigquery.FloatFieldType
case wkt.Int32Value, wkt.Int64Value, wkt.UInt32Value, wkt.UInt64Value:
return bigquery.IntegerFieldType
case wkt.BoolValue:
return bigquery.BooleanFieldType
case wkt.StringValue:
return bigquery.StringFieldType
case wkt.BytesValue:
return bigquery.BytesFieldType
case wkt.Struct:
if o.UseJSONStructs {
return bigquery.JSONFieldType
}
return bigquery.StringFieldType
case wkt.Date:
return bigquery.DateFieldType
case wkt.DateTime:
if o.UseDateTimeWithoutOffset {
return bigquery.DateTimeFieldType
}
return bigquery.RecordFieldType // to include explicit UTC offset or time zone
case wkt.LatLng:
return bigquery.GeographyFieldType
case wkt.TimeOfDay:
return bigquery.TimeFieldType
case wkt.Value:
if o.UseJSONValues {
return bigquery.JSONFieldType
}
return bigquery.StringFieldType
}
}
return bigquery.RecordFieldType
}
func (o SchemaOptions) inferEnumFieldType(_ protoreflect.FieldDescriptor) bigquery.FieldType {
if o.UseEnumNumbers {
return bigquery.IntegerFieldType
}
return bigquery.StringFieldType
}
func (o SchemaOptions) inferMapFieldSchema(field protoreflect.FieldDescriptor) *bigquery.FieldSchema {
return &bigquery.FieldSchema{
Name: string(field.Name()),
Repeated: true,
Type: bigquery.RecordFieldType,
Description: o.buildDescription(field),
Schema: bigquery.Schema{
o.inferFieldSchema(field.MapKey()),
o.inferFieldSchema(field.MapValue()),
},
}
}