Skip to content

Commit a2330e4

Browse files
shollymancodyoss
andauthored
feat(bigquery): add support for parameterized types (#4103)
* feat(bigquery): add support for parameterized types Exposes type parameterization constraints for STRING, BYTES, NUMERIC, BIGNUMERIC types. * Update bigquery/schema.go Co-authored-by: Cody Oss <6331106+codyoss@users.noreply.github.com> Co-authored-by: Cody Oss <6331106+codyoss@users.noreply.github.com>
1 parent a2d137d commit a2330e4

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

Diff for: bigquery/integration_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,36 @@ func TestIntegration_TableCreate(t *testing.T) {
249249
}
250250
}
251251

252+
func TestIntegration_TableCreateWithConstraints(t *testing.T) {
253+
if client == nil {
254+
t.Skip("Integration tests skipped")
255+
}
256+
table := dataset.Table("constraints")
257+
schema := Schema{
258+
{Name: "str_col", Type: StringFieldType, MaxLength: 10},
259+
{Name: "bytes_col", Type: BytesFieldType, MaxLength: 150},
260+
{Name: "num_col", Type: NumericFieldType, Precision: 20},
261+
{Name: "bignumeric_col", Type: BigNumericFieldType, Precision: 30, Scale: 5},
262+
}
263+
err := table.Create(context.Background(), &TableMetadata{
264+
Schema: schema,
265+
ExpirationTime: testTableExpiration.Add(5 * time.Minute),
266+
})
267+
if err != nil {
268+
t.Fatalf("table create error: %v", err)
269+
}
270+
271+
meta, err := table.Metadata(context.Background())
272+
if err != nil {
273+
t.Fatalf("couldn't get metadata: %v", err)
274+
}
275+
276+
if diff := testutil.Diff(meta.Schema, schema); diff != "" {
277+
t.Fatalf("got=-, want=+:\n%s", diff)
278+
}
279+
280+
}
281+
252282
func TestIntegration_TableCreateView(t *testing.T) {
253283
if client == nil {
254284
t.Skip("Integration tests skipped")

Diff for: bigquery/schema.go

+41
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,41 @@ type FieldSchema struct {
7070

7171
// Describes the nested schema if Type is set to Record.
7272
Schema Schema
73+
74+
// Maximum length of the field for STRING or BYTES type.
75+
//
76+
// It is invalid to set value for types other than STRING or BYTES.
77+
//
78+
// For STRING type, this represents the maximum UTF-8 length of strings
79+
// allowed in the field. For BYTES type, this represents the maximum
80+
// number of bytes in the field.
81+
MaxLength int64
82+
83+
// Precision can be used to constrain the maximum number of
84+
// total digits allowed for NUMERIC or BIGNUMERIC types.
85+
//
86+
// It is invalid to set values for Precision for types other than
87+
// NUMERIC or BIGNUMERIC.
88+
//
89+
// For NUMERIC type, acceptable values for Precision must
90+
// be: 1 ≤ (Precision - Scale) ≤ 29. Values for Scale
91+
// must be: 0 ≤ Scale ≤ 9.
92+
//
93+
// For BIGNUMERIC type, acceptable values for Precision must
94+
// be: 1 ≤ (Precision - Scale) ≤ 38. Values for Scale
95+
// must be: 0 ≤ Scale ≤ 38.
96+
Precision int64
97+
98+
// Scale can be used to constrain the maximum number of digits
99+
// in the fractional part of a NUMERIC or BIGNUMERIC type.
100+
//
101+
// If the Scale value is set, the Precision value must be set as well.
102+
//
103+
// It is invalid to set values for Scale for types other than
104+
// NUMERIC or BIGNUMERIC.
105+
//
106+
// See the Precision field for additional guidance about valid values.
107+
Scale int64
73108
}
74109

75110
func (fs *FieldSchema) toBQ() *bq.TableFieldSchema {
@@ -78,6 +113,9 @@ func (fs *FieldSchema) toBQ() *bq.TableFieldSchema {
78113
Name: fs.Name,
79114
Type: string(fs.Type),
80115
PolicyTags: fs.PolicyTags.toBQ(),
116+
MaxLength: fs.MaxLength,
117+
Precision: fs.Precision,
118+
Scale: fs.Scale,
81119
}
82120

83121
if fs.Repeated {
@@ -133,6 +171,9 @@ func bqToFieldSchema(tfs *bq.TableFieldSchema) *FieldSchema {
133171
Required: tfs.Mode == "REQUIRED",
134172
Type: FieldType(tfs.Type),
135173
PolicyTags: bqToPolicyTagList(tfs.PolicyTags),
174+
MaxLength: tfs.MaxLength,
175+
Precision: tfs.Precision,
176+
Scale: tfs.Scale,
136177
}
137178

138179
for _, f := range tfs.Fields {

Diff for: bigquery/schema_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,40 @@ func TestSchemaConversion(t *testing.T) {
284284
fieldSchema("geo", "g", "GEOGRAPHY", false, false, nil),
285285
},
286286
},
287+
{
288+
// constrained
289+
bqSchema: &bq.TableSchema{
290+
Fields: []*bq.TableFieldSchema{
291+
{
292+
Name: "foo",
293+
Type: "STRING",
294+
MaxLength: 0,
295+
Precision: 0,
296+
Scale: 0,
297+
},
298+
{
299+
Name: "bar",
300+
Type: "STRING",
301+
MaxLength: 1,
302+
Precision: 2,
303+
Scale: 3,
304+
},
305+
}},
306+
schema: Schema{
307+
{Name: "foo",
308+
Type: StringFieldType,
309+
MaxLength: 0,
310+
Precision: 0,
311+
Scale: 0,
312+
},
313+
{Name: "bar",
314+
Type: StringFieldType,
315+
MaxLength: 1,
316+
Precision: 2,
317+
Scale: 3,
318+
},
319+
},
320+
},
287321
{
288322
// policy tags
289323
bqSchema: &bq.TableSchema{

0 commit comments

Comments
 (0)