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 MetadataCacheMode to ExternalTableDefinition #3351

Merged
merged 3 commits into from
Jun 19, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ 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.40.0')
implementation platform('com.google.cloud:libraries-bom:26.41.0')
implementation 'com.google.cloud:google-cloud-bigquery'
```
If you are using Gradle without BOM, add this to your dependencies:

```Groovy
implementation 'com.google.cloud:google-cloud-bigquery:2.40.2'
implementation 'com.google.cloud:google-cloud-bigquery:2.40.3'
```

If you are using SBT, add this to your dependencies:

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-bigquery" % "2.40.2"
libraryDependencies += "com.google.cloud" % "google-cloud-bigquery" % "2.40.3"
```
<!-- {x-version-update-end} -->

Expand Down Expand Up @@ -351,7 +351,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-bigquery/java11.html
[stability-image]: https://img.shields.io/badge/stability-stable-green
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-bigquery.svg
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-bigquery/2.40.2
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-bigquery/2.40.3
[authentication]: https://github.com/googleapis/google-cloud-java#authentication
[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ public Builder setObjectMetadata(String objectMetadata) {

abstract Builder setObjectMetadataInner(String objectMetadata);

/**
* [Optional] Metadata Cache Mode for the table. Set this to enable caching of metadata from
* external data source.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#metadatacachemode">
* MetadataCacheMode</a>
*/
public Builder setMetadataCacheMode(String metadataCacheMode) {
PhongChuong marked this conversation as resolved.
Show resolved Hide resolved
return setMetadataCacheModeInner(metadataCacheMode);
}

abstract Builder setMetadataCacheModeInner(String metadataCacheMode);

/** Creates an {@code ExternalTableDefinition} object. */
@Override
public abstract ExternalTableDefinition build();
Expand Down Expand Up @@ -276,6 +290,21 @@ public String getObjectMetadata() {
@Nullable
abstract String getObjectMetadataInner();

/**
* Returns the metadata cache mode.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#metadatacachemode">
* MetadataCacheMode</a>
*/
@Nullable
public String getMetadataCacheMode() {
return getMetadataCacheModeInner();
}

@Nullable
abstract String getMetadataCacheModeInner();

/**
* Returns the source format, and possibly some parsing options, of the external data. Supported
* formats are {@code CSV} and {@code NEWLINE_DELIMITED_JSON}.
Expand Down Expand Up @@ -387,6 +416,10 @@ com.google.api.services.bigquery.model.ExternalDataConfiguration toExternalDataC
externalConfigurationPb.setObjectMetadata(getObjectMetadata());
}

if (getMetadataCacheMode() != null) {
externalConfigurationPb.setMetadataCacheMode(getMetadataCacheMode());
}

return externalConfigurationPb;
}

Expand Down Expand Up @@ -580,6 +613,9 @@ static ExternalTableDefinition fromPb(Table tablePb) {
if (externalDataConfiguration.getObjectMetadata() != null) {
builder.setObjectMetadata(externalDataConfiguration.getObjectMetadata());
}
if (externalDataConfiguration.getMetadataCacheMode() != null) {
builder.setMetadataCacheMode(externalDataConfiguration.getMetadataCacheMode());
}
}
return builder.build();
}
Expand Down Expand Up @@ -647,6 +683,10 @@ static ExternalTableDefinition fromExternalDataConfiguration(
builder.setObjectMetadata(externalDataConfiguration.getObjectMetadata());
}

if (externalDataConfiguration.getMetadataCacheMode() != null) {
builder.setMetadataCacheMode(externalDataConfiguration.getMetadataCacheMode());
}

return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public class ExternalTableDefinitionTest {
.setSourceUriPrefix(SOURCE_URIS.get(0))
.build();
private static final String OBJECT_METADATA = "SIMPLE";

private static final String METADATA_CACHE_MODE = "AUTOMATIC";
private static final ExternalTableDefinition EXTERNAL_TABLE_DEFINITION =
ExternalTableDefinition.newBuilder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS)
.setFileSetSpecType("FILE_SET_SPEC_TYPE_FILE_SYSTEM_MATCH")
Expand All @@ -70,6 +72,7 @@ public class ExternalTableDefinitionTest {
.setAutodetect(AUTODETECT)
.setHivePartitioningOptions(HIVE_PARTITIONING_OPTIONS)
.setObjectMetadata(OBJECT_METADATA)
.setMetadataCacheMode(METADATA_CACHE_MODE)
.build();

private static final ExternalTableDefinition EXTERNAL_TABLE_DEFINITION_AVRO =
Expand Down Expand Up @@ -170,5 +173,6 @@ private void compareExternalTableDefinition(
assertEquals(expected.getAutodetect(), value.getAutodetect());
assertEquals(expected.getHivePartitioningOptions(), value.getHivePartitioningOptions());
assertEquals(expected.getObjectMetadata(), value.getObjectMetadata());
assertEquals(expected.getMetadataCacheMode(), value.getMetadataCacheMode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6832,6 +6832,32 @@ public void testExternalTableMetadataCachingNotEnable() throws InterruptedExcept
assertTrue(remoteTable.delete());
}

@Test
public void testExternalMetadataCacheModeFailForNonBiglake() {
// Validate that MetadataCacheMode is passed to the backend.
// TODO: Enhance this test after BigLake testing infrastructure is inplace.
String tableName = "test_metadata_cache_mode_fail_for_non_biglake";
TableId tableId = TableId.of(DATASET, tableName);
ExternalTableDefinition externalTableDefinition =
ExternalTableDefinition.newBuilder(
"gs://" + BUCKET + "/" + JSON_LOAD_FILE, TABLE_SCHEMA, FormatOptions.json())
.setMetadataCacheMode("AUTOMATIC")
.build();
TableInfo tableInfo = TableInfo.of(tableId, externalTableDefinition);

try {
bigquery.create(tableInfo);
fail("BigQueryException was expected");
} catch (BigQueryException e) {
BigQueryError error = e.getError();
assertNotNull(error);
assertEquals("invalid", error.getReason());
assertThat(
e.getMessage().contains("metadataCacheMode provided for non BigLake external table"))
.isTrue();
}
}

@Test
public void testObjectTable() throws InterruptedException {
String tableName = "test_object_table";
Expand Down
Loading