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: Add Routine DataGovernanceType #3006

Merged
merged 3 commits into from Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -53,7 +53,7 @@ If you are using Maven without the BOM, add this to your dependencies:
If you are using Gradle 5.x or later, add this to your dependencies:

```Groovy
implementation platform('com.google.cloud:libraries-bom:26.26.0')
implementation platform('com.google.cloud:libraries-bom:26.27.0')

implementation 'com.google.cloud:google-cloud-bigquery'
```
Expand Down
5 changes: 5 additions & 0 deletions google-cloud-bigquery/clirr-ignored-differences.xml
Expand Up @@ -19,6 +19,11 @@
<className>com/google/cloud/bigquery/ExternalTableDefinition*</className>
<method>*ReferenceFileSchemaUri(*)</method>
</difference>
<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/bigquery/RoutineInfo*</className>
<method>*DataGovernanceType(*)</method>
</difference>
<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/bigquery/RoutineInfo*</className>
Expand Down
Expand Up @@ -135,6 +135,12 @@ public Builder setRemoteFunctionOptions(RemoteFunctionOptions remoteFunctionOpti
return this;
}

@Override
public Builder setDataGovernanceType(String dataGovernanceType) {
infoBuilder.setDataGovernanceType(dataGovernanceType);
return this;
}

@Override
public Routine build() {
return new Routine(bigquery, infoBuilder);
Expand Down
Expand Up @@ -72,6 +72,8 @@ public Routine apply(RoutineInfo routineInfo) {
private final String body;
private final RemoteFunctionOptions remoteFunctionOptions;

private final String dataGovernanceType;

public abstract static class Builder {

abstract Builder setRoutineId(RoutineId id);
Expand Down Expand Up @@ -157,6 +159,13 @@ public abstract static class Builder {
*/
public abstract Builder setRemoteFunctionOptions(RemoteFunctionOptions remoteFunctionOptions);

/**
* Sets the data governance type for the Builder (e.g. DATA_MASKING).
*
* <p>See https://cloud.google.com/bigquery/docs/reference/rest/v2/routines
*/
public abstract Builder setDataGovernanceType(String dataGovernanceType);

/** Creates a {@code RoutineInfo} object. */
public abstract RoutineInfo build();
}
Expand All @@ -177,6 +186,8 @@ static class BuilderImpl extends Builder {
private String body;
private RemoteFunctionOptions remoteFunctionOptions;

private String dataGovernanceType;

BuilderImpl() {}

BuilderImpl(RoutineInfo routineInfo) {
Expand All @@ -194,6 +205,7 @@ static class BuilderImpl extends Builder {
this.importedLibrariesList = routineInfo.importedLibrariesList;
this.body = routineInfo.body;
this.remoteFunctionOptions = routineInfo.remoteFunctionOptions;
this.dataGovernanceType = routineInfo.dataGovernanceType;
}

BuilderImpl(Routine routinePb) {
Expand Down Expand Up @@ -225,6 +237,7 @@ static class BuilderImpl extends Builder {
this.remoteFunctionOptions =
RemoteFunctionOptions.fromPb(routinePb.getRemoteFunctionOptions());
}
this.dataGovernanceType = routinePb.getDataGovernanceType();
}

@Override
Expand Down Expand Up @@ -311,6 +324,12 @@ public Builder setRemoteFunctionOptions(RemoteFunctionOptions remoteFunctionOpti
return this;
}

@Override
public Builder setDataGovernanceType(String dataGovernanceType) {
this.dataGovernanceType = dataGovernanceType;
return this;
}

@Override
public RoutineInfo build() {
return new RoutineInfo(this);
Expand All @@ -332,6 +351,7 @@ public RoutineInfo build() {
this.importedLibrariesList = builder.importedLibrariesList;
this.body = builder.body;
this.remoteFunctionOptions = builder.remoteFunctionOptions;
this.dataGovernanceType = builder.dataGovernanceType;
}

/** Returns the RoutineId identified for the routine resource. * */
Expand Down Expand Up @@ -411,6 +431,11 @@ public RemoteFunctionOptions getRemoteFunctionOptions() {
return remoteFunctionOptions;
};

/** Returns the data governance type of the routine, e.g. DATA_MASKING. */
public String getDataGovernanceType() {
return dataGovernanceType;
}

/** Returns a builder pre-populated using the current values of this routine. */
public Builder toBuilder() {
return new BuilderImpl(this);
Expand All @@ -433,6 +458,7 @@ public String toString() {
.add("importedLibrariesList", importedLibrariesList)
.add("body", body)
.add("remoteFunctionOptions", remoteFunctionOptions)
.add("dataGovernanceType", dataGovernanceType)
.toString();
}

Expand All @@ -452,7 +478,8 @@ public int hashCode() {
returnTableType,
importedLibrariesList,
body,
remoteFunctionOptions);
remoteFunctionOptions,
dataGovernanceType);
}

@Override
Expand Down Expand Up @@ -490,7 +517,8 @@ Routine toPb() {
.setDescription(getDescription())
.setDeterminismLevel(getDeterminismLevel())
.setLastModifiedTime(getLastModifiedTime())
.setLanguage(getLanguage());
.setLanguage(getLanguage())
.setDataGovernanceType(getDataGovernanceType());
if (getRoutineId() != null) {
routinePb.setRoutineReference(getRoutineId().toPb());
}
Expand All @@ -506,6 +534,9 @@ Routine toPb() {
if (getRemoteFunctionOptions() != null) {
routinePb.setRemoteFunctionOptions(getRemoteFunctionOptions().toPb());
}
if (getImportedLibraries() != null) {
PhongChuong marked this conversation as resolved.
Show resolved Hide resolved
routinePb.setImportedLibraries(getImportedLibraries());
}
return routinePb;
}

Expand Down
Expand Up @@ -33,6 +33,8 @@ public class RoutineInfoTest {
private static final Long LAST_MODIFIED_TIME = 20L;
private static final String LANGUAGE = "SQL";

private static final String DATA_GOVERNANCE_TYPE = "DATA_MASKING";

private static final RoutineArgument ARG_1 =
RoutineArgument.newBuilder()
.setDataType(StandardSQLDataType.newBuilder("STRING").build())
Expand Down Expand Up @@ -63,6 +65,7 @@ public class RoutineInfoTest {
.setReturnType(RETURN_TYPE)
.setImportedLibraries(IMPORTED_LIBRARIES)
.setBody(BODY)
.setDataGovernanceType(DATA_GOVERNANCE_TYPE)
.build();

@Test
Expand Down Expand Up @@ -90,6 +93,7 @@ public void testBuilder() {
assertEquals(RETURN_TYPE, ROUTINE_INFO.getReturnType());
assertEquals(IMPORTED_LIBRARIES, ROUTINE_INFO.getImportedLibraries());
assertEquals(BODY, ROUTINE_INFO.getBody());
assertEquals(DATA_GOVERNANCE_TYPE, ROUTINE_INFO.getDataGovernanceType());
}

@Test
Expand All @@ -107,8 +111,10 @@ public void testOf() {
assertNull(routineInfo.getReturnType());
assertNull(routineInfo.getImportedLibraries());
assertNull(routineInfo.getBody());
assertNull(routineInfo.getDataGovernanceType());
}

@Test
public void testToAndFromPb() {
compareRoutineInfo(ROUTINE_INFO, RoutineInfo.fromPb(ROUTINE_INFO.toPb()));
}
Expand All @@ -132,6 +138,7 @@ public void compareRoutineInfo(RoutineInfo expected, RoutineInfo value) {
assertEquals(expected.getReturnType(), value.getReturnType());
assertEquals(expected.getImportedLibraries(), value.getImportedLibraries());
assertEquals(expected.getBody(), value.getBody());
assertEquals(expected.getDataGovernanceType(), value.getDataGovernanceType());
assertEquals(expected.hashCode(), value.hashCode());
assertEquals(expected.toString(), value.toString());
}
Expand Down
Expand Up @@ -91,6 +91,8 @@ public class RoutineTest {
.setMaxBatchingRows(10L)
.build();

private static final String DATA_GOVERNANCE_TYPE = "DATA_MASKING";

private static final RoutineInfo ROUTINE_INFO =
RoutineInfo.newBuilder(ROUTINE_ID)
.setEtag(ETAG)
Expand All @@ -104,6 +106,7 @@ public class RoutineTest {
.setImportedLibraries(IMPORTED_LIBRARIES)
.setBody(BODY)
.setRemoteFunctionOptions(REMOTE_FUNCTION_OPTIONS)
.setDataGovernanceType(DATA_GOVERNANCE_TYPE)
.build();

private static final RoutineInfo ROUTINE_INFO_TVF =
Expand Down Expand Up @@ -146,6 +149,7 @@ public void testBuilder() {
.setImportedLibraries(IMPORTED_LIBRARIES)
.setBody(BODY)
.setRemoteFunctionOptions(REMOTE_FUNCTION_OPTIONS)
.setDataGovernanceType(DATA_GOVERNANCE_TYPE)
.build();
assertEquals(ETAG, builtRoutine.getEtag());
assertEquals(DETERMINISM_LEVEL, builtRoutine.getDeterminismLevel());
Expand Down Expand Up @@ -247,5 +251,6 @@ public void compareRoutineInfo(RoutineInfo expected, RoutineInfo value) {
assertEquals(expected.getBody(), value.getBody());
assertEquals(expected.hashCode(), value.hashCode());
assertEquals(expected.getRemoteFunctionOptions(), value.getRemoteFunctionOptions());
assertEquals(expected.getDataGovernanceType(), value.getDataGovernanceType());
}
}
Expand Up @@ -2684,6 +2684,33 @@ public void testRoutineAPICreationTVF() {
assertEquals(routine.getReturnTableType(), returnTableType);
}

@Test
public void testRoutineDataGovernanceType() {
String routineName = RemoteBigQueryHelper.generateRoutineName();
RoutineId routineId = RoutineId.of(ROUTINE_DATASET, routineName);
RoutineInfo routineInfo =
RoutineInfo.newBuilder(routineId)
.setLanguage("SQL")
.setRoutineType("SCALAR_FUNCTION")
.setBody("x")
.setArguments(
ImmutableList.of(
RoutineArgument.newBuilder()
.setName("x")
.setDataType(StandardSQLDataType.newBuilder("INT64").build())
.build()))
.setReturnType(StandardSQLDataType.newBuilder("INT64").build())
.setDataGovernanceType("DATA_MASKING")
.build();

Routine routine = bigquery.create(routineInfo);
assertNotNull(routine);
assertEquals(routine.getLanguage(), "SQL");
assertEquals(routine.getRoutineType(), "SCALAR_FUNCTION");
assertEquals(routine.getReturnType(), StandardSQLDataType.newBuilder("INT64").build());
assertEquals(routine.getDataGovernanceType(), "DATA_MASKING");
}

@Test
public void testAuthorizeRoutine() {
String routineName = RemoteBigQueryHelper.generateRoutineName();
Expand Down