Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bigquery): add DataGovernanceType to routines #8990

Merged
merged 5 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions bigquery/routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ type RoutineMetadata struct {
// For JAVASCRIPT function, it is the evaluated string in the AS clause of
// a CREATE FUNCTION statement.
Body string

// For data governance use cases. If set to "DATA_MASKING", the function
// is validated and made available as a masking function. For more information,
// see: https://cloud.google.com/bigquery/docs/user-defined-functions#custom-mask
DataGovernanceType string
}

// RemoteFunctionOptions contains information for a remote user-defined function.
Expand Down Expand Up @@ -278,6 +283,7 @@ func (rm *RoutineMetadata) toBQ() (*bq.Routine, error) {
r.Language = rm.Language
r.RoutineType = rm.Type
r.DefinitionBody = rm.Body
r.DataGovernanceType = rm.DataGovernanceType
rt, err := rm.ReturnType.toBQ()
if err != nil {
return nil, err
Expand Down Expand Up @@ -405,15 +411,16 @@ func routineArgumentsToBQ(in []*RoutineArgument) ([]*bq.Argument, error) {

// RoutineMetadataToUpdate governs updating a routine.
type RoutineMetadataToUpdate struct {
Arguments []*RoutineArgument
Description optional.String
DeterminismLevel optional.String
Type optional.String
Language optional.String
Body optional.String
ImportedLibraries []string
ReturnType *StandardSQLDataType
ReturnTableType *StandardSQLTableType
Arguments []*RoutineArgument
Description optional.String
DeterminismLevel optional.String
Type optional.String
Language optional.String
Body optional.String
ImportedLibraries []string
ReturnType *StandardSQLDataType
ReturnTableType *StandardSQLTableType
DataGovernanceType optional.String
}

func (rm *RoutineMetadataToUpdate) toBQ() (*bq.Routine, error) {
Expand Down Expand Up @@ -491,20 +498,25 @@ func (rm *RoutineMetadataToUpdate) toBQ() (*bq.Routine, error) {
r.ReturnTableType = tt
forceSend("ReturnTableType")
}
if rm.DataGovernanceType != nil {
r.DataGovernanceType = optional.ToString(rm.DataGovernanceType)
forceSend("DataGovernanceType")
}
return r, nil
}

func bqToRoutineMetadata(r *bq.Routine) (*RoutineMetadata, error) {
meta := &RoutineMetadata{
ETag: r.Etag,
Type: r.RoutineType,
CreationTime: unixMillisToTime(r.CreationTime),
Description: r.Description,
DeterminismLevel: RoutineDeterminism(r.DeterminismLevel),
LastModifiedTime: unixMillisToTime(r.LastModifiedTime),
Language: r.Language,
ImportedLibraries: r.ImportedLibraries,
Body: r.DefinitionBody,
ETag: r.Etag,
Type: r.RoutineType,
CreationTime: unixMillisToTime(r.CreationTime),
Description: r.Description,
DeterminismLevel: RoutineDeterminism(r.DeterminismLevel),
LastModifiedTime: unixMillisToTime(r.LastModifiedTime),
Language: r.Language,
ImportedLibraries: r.ImportedLibraries,
Body: r.DefinitionBody,
DataGovernanceType: r.DataGovernanceType,
}
args, err := bqToArgs(r.Arguments)
if err != nil {
Expand Down
93 changes: 59 additions & 34 deletions bigquery/routine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,16 @@ func TestRoutineTypeConversions(t *testing.T) {
in interface{}
want interface{}
}{
{"empty", "ToRoutineMetadata", &bq.Routine{}, &RoutineMetadata{}},
{"basic", "ToRoutineMetadata",
&bq.Routine{
{
shollyman marked this conversation as resolved.
Show resolved Hide resolved
name: "empty",
conversion: "ToRoutineMetadata",
in: &bq.Routine{},
want: &RoutineMetadata{},
},
{
name: "basic",
conversion: "ToRoutineMetadata",
in: &bq.Routine{
CreationTime: aTimeMillis,
LastModifiedTime: aTimeMillis,
DefinitionBody: "body",
Expand All @@ -89,8 +96,9 @@ func TestRoutineTypeConversions(t *testing.T) {
{Name: "field", Type: &bq.StandardSqlDataType{TypeKind: "FLOAT64"}},
},
},
DataGovernanceType: "DATA_MASKING",
},
&RoutineMetadata{
want: &RoutineMetadata{
CreationTime: aTime,
LastModifiedTime: aTime,
Description: "desc",
Expand All @@ -105,55 +113,73 @@ func TestRoutineTypeConversions(t *testing.T) {
{Name: "field", Type: &StandardSQLDataType{TypeKind: "FLOAT64"}},
},
},
}},
{"body_and_libs", "FromRoutineMetadataToUpdate",
&RoutineMetadataToUpdate{
Body: "body",
ImportedLibraries: []string{"foo", "bar"},
ReturnType: &StandardSQLDataType{TypeKind: "FOO"},
DataGovernanceType: "DATA_MASKING",
},
&bq.Routine{
DefinitionBody: "body",
ImportedLibraries: []string{"foo", "bar"},
ReturnType: &bq.StandardSqlDataType{TypeKind: "FOO"},
ForceSendFields: []string{"DefinitionBody", "ImportedLibraries", "ReturnType"},
}},
{"null_fields", "FromRoutineMetadataToUpdate",
&RoutineMetadataToUpdate{
},
{
name: "body_and_libs",
conversion: "FromRoutineMetadataToUpdate",
in: &RoutineMetadataToUpdate{
Body: "body",
ImportedLibraries: []string{"foo", "bar"},
ReturnType: &StandardSQLDataType{TypeKind: "FOO"},
DataGovernanceType: "DATA_MASKING",
},
want: &bq.Routine{
DefinitionBody: "body",
ImportedLibraries: []string{"foo", "bar"},
ReturnType: &bq.StandardSqlDataType{TypeKind: "FOO"},
DataGovernanceType: "DATA_MASKING",
ForceSendFields: []string{"DefinitionBody", "ImportedLibraries", "ReturnType", "DataGovernanceType"},
},
},
{
name: "null_fields",
conversion: "FromRoutineMetadataToUpdate",
in: &RoutineMetadataToUpdate{
Type: "type",
Arguments: []*RoutineArgument{},
ImportedLibraries: []string{},
},
&bq.Routine{
want: &bq.Routine{
RoutineType: "type",
ForceSendFields: []string{"RoutineType"},
NullFields: []string{"Arguments", "ImportedLibraries"},
}},
{"empty", "ToRoutineArgument",
&bq.Argument{},
&RoutineArgument{}},
{"basic", "ToRoutineArgument",
&bq.Argument{
},
},
{name: "empty",
shollyman marked this conversation as resolved.
Show resolved Hide resolved
conversion: "ToRoutineArgument",
in: &bq.Argument{},
want: &RoutineArgument{}},
{
name: "basic",
conversion: "ToRoutineArgument",
in: &bq.Argument{
Name: "foo",
ArgumentKind: "bar",
Mode: "baz",
},
&RoutineArgument{
want: &RoutineArgument{
Name: "foo",
Kind: "bar",
Mode: "baz",
}},
{"empty", "FromRoutineArgument",
&RoutineArgument{},
&bq.Argument{},
},
},
{"basic", "FromRoutineArgument",
&RoutineArgument{
{
name: "empty",
conversion: "FromRoutineArgument",
in: &RoutineArgument{},
want: &bq.Argument{},
},
{
name: "basic",
conversion: "FromRoutineArgument",
in: &RoutineArgument{
Name: "foo",
Kind: "bar",
Mode: "baz",
},
&bq.Argument{
want: &bq.Argument{
Name: "foo",
ArgumentKind: "bar",
Mode: "baz",
Expand All @@ -162,7 +188,6 @@ func TestRoutineTypeConversions(t *testing.T) {

for _, test := range tests {
t.Run(fmt.Sprintf("%s/%s", test.conversion, test.name), func(t *testing.T) {
t.Parallel()
testRoutineConversion(t, test.conversion, test.in, test.want)
})
}
Expand Down
Loading