Skip to content

Commit 33666cf

Browse files
authored
feat(bigquery): RANGE type StandardSQLDataType support (#9754)
This PR augments the StandardSQLDataRepresentation(s) to support range-specific augmentations, and adds some testing. Astute observers will note that this does include mapping changes to param handling, which will be tested in a subsequent PR that expands RANGE coverage to that area of the library.
1 parent 7fc9c54 commit 33666cf

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

bigquery/routine_integration_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,44 @@ func TestIntegration_RoutineScalarUDF(t *testing.T) {
5353
}
5454
}
5555

56+
func TestIntegration_RoutineRangeType(t *testing.T) {
57+
if client == nil {
58+
t.Skip("Integration tests skipped")
59+
}
60+
ctx := context.Background()
61+
62+
routineID := routineIDs.New()
63+
routine := dataset.Routine(routineID)
64+
err := routine.Create(ctx, &RoutineMetadata{
65+
Type: "SCALAR_FUNCTION",
66+
Language: "SQL",
67+
Body: "RANGE_CONTAINS(r1,r2)",
68+
Arguments: []*RoutineArgument{
69+
{
70+
Name: "r1",
71+
DataType: &StandardSQLDataType{
72+
TypeKind: "RANGE",
73+
RangeElementType: &StandardSQLDataType{
74+
TypeKind: "TIMESTAMP",
75+
},
76+
},
77+
},
78+
{
79+
Name: "r2",
80+
DataType: &StandardSQLDataType{
81+
TypeKind: "RANGE",
82+
RangeElementType: &StandardSQLDataType{
83+
TypeKind: "TIMESTAMP",
84+
},
85+
},
86+
},
87+
},
88+
})
89+
if err != nil {
90+
t.Fatalf("Create: %v", err)
91+
}
92+
}
93+
5694
func TestIntegration_RoutineDataGovernance(t *testing.T) {
5795
if client == nil {
5896
t.Skip("Integration tests skipped")

bigquery/standardsql.go

+24
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type StandardSQLDataType struct {
2626
// ArrayElementType indicates the type of an array's elements, when the
2727
// TypeKind is ARRAY.
2828
ArrayElementType *StandardSQLDataType
29+
// The type of the range's elements, if TypeKind is RANGE.
30+
RangeElementType *StandardSQLDataType
2931
// StructType indicates the struct definition (fields), when the
3032
// TypeKind is STRUCT.
3133
StructType *StandardSQLStructType
@@ -60,6 +62,13 @@ func (ssdt *StandardSQLDataType) toBQ() (*bq.StandardSqlDataType, error) {
6062
}
6163
bqdt.StructType = dt
6264
}
65+
if ssdt.RangeElementType != nil {
66+
dt, err := ssdt.RangeElementType.toBQ()
67+
if err != nil {
68+
return nil, err
69+
}
70+
bqdt.RangeElementType = dt
71+
}
6372
return bqdt, nil
6473
}
6574

@@ -77,6 +86,14 @@ func (ssdt StandardSQLDataType) toBQParamType() *bq.QueryParameterType {
7786
}
7887
return &bq.QueryParameterType{Type: "STRUCT", StructTypes: fts}
7988
}
89+
if ssdt.RangeElementType != nil {
90+
return &bq.QueryParameterType{
91+
Type: string(RangeFieldType),
92+
RangeElementType: &bq.QueryParameterType{
93+
Type: ssdt.RangeElementType.TypeKind,
94+
},
95+
}
96+
}
8097
return &bq.QueryParameterType{Type: ssdt.TypeKind}
8198
}
8299

@@ -102,6 +119,13 @@ func bqToStandardSQLDataType(bqdt *bq.StandardSqlDataType) (*StandardSQLDataType
102119
}
103120
ssdt.StructType = st
104121
}
122+
if bqdt.RangeElementType != nil {
123+
st, err := bqToStandardSQLDataType(bqdt.RangeElementType)
124+
if err != nil {
125+
return nil, err
126+
}
127+
ssdt.RangeElementType = st
128+
}
105129
return ssdt, nil
106130
}
107131

bigquery/standardsql_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ func TestBQToStandardSQLDataType(t *testing.T) {
4949
},
5050
},
5151
},
52+
{
53+
&bq.StandardSqlDataType{
54+
TypeKind: "RANGE",
55+
RangeElementType: &bq.StandardSqlDataType{
56+
TypeKind: "DATETIME",
57+
},
58+
},
59+
&StandardSQLDataType{
60+
TypeKind: "RANGE",
61+
RangeElementType: &StandardSQLDataType{
62+
TypeKind: "DATETIME",
63+
},
64+
},
65+
},
5266
} {
5367
got, err := bqToStandardSQLDataType(test.in)
5468
if err != nil {

0 commit comments

Comments
 (0)