diff --git a/bigquery/routine_integration_test.go b/bigquery/routine_integration_test.go index ffd8228417c3..363ec31edbc4 100644 --- a/bigquery/routine_integration_test.go +++ b/bigquery/routine_integration_test.go @@ -53,6 +53,44 @@ func TestIntegration_RoutineScalarUDF(t *testing.T) { } } +func TestIntegration_RoutineRangeType(t *testing.T) { + if client == nil { + t.Skip("Integration tests skipped") + } + ctx := context.Background() + + routineID := routineIDs.New() + routine := dataset.Routine(routineID) + err := routine.Create(ctx, &RoutineMetadata{ + Type: "SCALAR_FUNCTION", + Language: "SQL", + Body: "RANGE_CONTAINS(r1,r2)", + Arguments: []*RoutineArgument{ + { + Name: "r1", + DataType: &StandardSQLDataType{ + TypeKind: "RANGE", + RangeElementType: &StandardSQLDataType{ + TypeKind: "TIMESTAMP", + }, + }, + }, + { + Name: "r2", + DataType: &StandardSQLDataType{ + TypeKind: "RANGE", + RangeElementType: &StandardSQLDataType{ + TypeKind: "TIMESTAMP", + }, + }, + }, + }, + }) + if err != nil { + t.Fatalf("Create: %v", err) + } +} + func TestIntegration_RoutineDataGovernance(t *testing.T) { if client == nil { t.Skip("Integration tests skipped") diff --git a/bigquery/standardsql.go b/bigquery/standardsql.go index 7f8ca6e11678..da24f4262e8a 100644 --- a/bigquery/standardsql.go +++ b/bigquery/standardsql.go @@ -26,6 +26,8 @@ type StandardSQLDataType struct { // ArrayElementType indicates the type of an array's elements, when the // TypeKind is ARRAY. ArrayElementType *StandardSQLDataType + // The type of the range's elements, if TypeKind is RANGE. + RangeElementType *StandardSQLDataType // StructType indicates the struct definition (fields), when the // TypeKind is STRUCT. StructType *StandardSQLStructType @@ -60,6 +62,13 @@ func (ssdt *StandardSQLDataType) toBQ() (*bq.StandardSqlDataType, error) { } bqdt.StructType = dt } + if ssdt.RangeElementType != nil { + dt, err := ssdt.RangeElementType.toBQ() + if err != nil { + return nil, err + } + bqdt.RangeElementType = dt + } return bqdt, nil } @@ -77,6 +86,14 @@ func (ssdt StandardSQLDataType) toBQParamType() *bq.QueryParameterType { } return &bq.QueryParameterType{Type: "STRUCT", StructTypes: fts} } + if ssdt.RangeElementType != nil { + return &bq.QueryParameterType{ + Type: string(RangeFieldType), + RangeElementType: &bq.QueryParameterType{ + Type: ssdt.RangeElementType.TypeKind, + }, + } + } return &bq.QueryParameterType{Type: ssdt.TypeKind} } @@ -102,6 +119,13 @@ func bqToStandardSQLDataType(bqdt *bq.StandardSqlDataType) (*StandardSQLDataType } ssdt.StructType = st } + if bqdt.RangeElementType != nil { + st, err := bqToStandardSQLDataType(bqdt.RangeElementType) + if err != nil { + return nil, err + } + ssdt.RangeElementType = st + } return ssdt, nil } diff --git a/bigquery/standardsql_test.go b/bigquery/standardsql_test.go index b7bfc91e54f7..abc051566916 100644 --- a/bigquery/standardsql_test.go +++ b/bigquery/standardsql_test.go @@ -49,6 +49,20 @@ func TestBQToStandardSQLDataType(t *testing.T) { }, }, }, + { + &bq.StandardSqlDataType{ + TypeKind: "RANGE", + RangeElementType: &bq.StandardSqlDataType{ + TypeKind: "DATETIME", + }, + }, + &StandardSQLDataType{ + TypeKind: "RANGE", + RangeElementType: &StandardSQLDataType{ + TypeKind: "DATETIME", + }, + }, + }, } { got, err := bqToStandardSQLDataType(test.in) if err != nil {