- Notifications
You must be signed in to change notification settings - Fork 130
feat: add support for table snapshot #1320
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
Merged
stephaniewang526 merged 8 commits into googleapis:master from stephaniewang526:table-snapshot Jun 28, 2021
Merged
Changes from 6 commits
Commits
Show all changes
8 commits Select commit Hold shift + click to select a range
6b91c14 feat: add table snapshot support
stephaniewang526 3247ed2 feat: add support for table snapshot
stephaniewang526 9f7539b add comment on operationType
stephaniewang526 16e944d lint update and remove extraneous update table snapshot IT
stephaniewang526 95b29d5 add cleanup for restoredTable
stephaniewang526 39d6c0e update base on comments
stephaniewang526 5b0c139 Merge branch 'master' into table-snapshot
stephaniewang526 6c4b0bf update based on comments
stephaniewang526 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions 112 google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/SnapshotTableDefinition.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /* | ||
| * Copyright 2021 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| | ||
| package com.google.cloud.bigquery; | ||
| | ||
| import com.google.api.client.util.DateTime; | ||
| import com.google.api.core.BetaApi; | ||
| import com.google.api.services.bigquery.model.Table; | ||
| import com.google.auto.value.AutoValue; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
| import javax.annotation.Nullable; | ||
| | ||
| @AutoValue | ||
| @BetaApi | ||
| public abstract class SnapshotTableDefinition extends TableDefinition { | ||
| | ||
| private static final long serialVersionUID = 2113445776046717526L; | ||
| | ||
| @AutoValue.Builder | ||
| public abstract static class Builder | ||
| extends TableDefinition.Builder<SnapshotTableDefinition, Builder> { | ||
| | ||
| /** Reference describing the ID of the table that was snapshot. * */ | ||
| public abstract Builder setBaseTableId(TableId baseTableId); | ||
| | ||
| /** | ||
| * The time at which the base table was snapshot. This value is reported in the JSON response | ||
| * using RFC3339 format. * | ||
| */ | ||
| public abstract Builder setSnapshotTime(String dateTime); | ||
| | ||
| public abstract Builder setTimePartitioning(TimePartitioning timePartitioning); | ||
| | ||
| public abstract Builder setRangePartitioning(RangePartitioning rangePartitioning); | ||
| | ||
| public abstract Builder setClustering(Clustering clustering); | ||
| | ||
| /** Creates a {@code SnapshotTableDefinition} object. */ | ||
| public abstract SnapshotTableDefinition build(); | ||
| } | ||
| | ||
| @Nullable | ||
| public abstract TableId getBaseTableId(); | ||
| | ||
| @Nullable | ||
| public abstract String getSnapshotTime(); | ||
| | ||
| @Nullable | ||
| public abstract TimePartitioning getTimePartitioning(); | ||
| | ||
| @Nullable | ||
| public abstract RangePartitioning getRangePartitioning(); | ||
| | ||
| @Nullable | ||
| public abstract Clustering getClustering(); | ||
| | ||
| /** Returns a builder for a snapshot table definition. */ | ||
| public static SnapshotTableDefinition.Builder newBuilder() { | ||
| return new AutoValue_SnapshotTableDefinition.Builder().setType(Type.SNAPSHOT); | ||
| } | ||
| | ||
| @VisibleForTesting | ||
| public abstract SnapshotTableDefinition.Builder toBuilder(); | ||
| | ||
| @Override | ||
| Table toPb() { | ||
| Table tablePb = super.toPb(); | ||
| com.google.api.services.bigquery.model.SnapshotDefinition snapshotDefinition = | ||
| new com.google.api.services.bigquery.model.SnapshotDefinition(); | ||
| snapshotDefinition.setBaseTableReference(getBaseTableId().toPb()); | ||
| snapshotDefinition.setSnapshotTime(DateTime.parseRfc3339(getSnapshotTime())); | ||
| tablePb.setSnapshotDefinition(snapshotDefinition); | ||
| if (getTimePartitioning() != null) { | ||
| tablePb.setTimePartitioning(getTimePartitioning().toPb()); | ||
| } | ||
| if (getRangePartitioning() != null) { | ||
| tablePb.setRangePartitioning(getRangePartitioning().toPb()); | ||
| } | ||
| if (getClustering() != null) { | ||
| tablePb.setClustering(getClustering().toPb()); | ||
| } | ||
| return tablePb; | ||
| } | ||
| | ||
| static SnapshotTableDefinition fromPb(Table tablePb) { | ||
| Builder builder = newBuilder().table(tablePb); | ||
| com.google.api.services.bigquery.model.SnapshotDefinition snapshotDefinition = | ||
| tablePb.getSnapshotDefinition(); | ||
| if (snapshotDefinition != null) { | ||
| if (snapshotDefinition.getBaseTableReference() != null) { | ||
| builder.setBaseTableId(TableId.fromPb(snapshotDefinition.getBaseTableReference())); | ||
| } | ||
| if (snapshotDefinition.getSnapshotTime() != null) { | ||
| builder.setSnapshotTime(snapshotDefinition.getSnapshotTime().toStringRfc3339()); | ||
| } | ||
| } | ||
| return builder.build(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions 72 ...e-cloud-bigquery/src/test/java/com/google/cloud/bigquery/SnapshotTableDefinitionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Copyright 2021 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| | ||
| package com.google.cloud.bigquery; | ||
| | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
| | ||
| import org.junit.Test; | ||
| | ||
| public class SnapshotTableDefinitionTest { | ||
| | ||
| private static final TableId BASE_TABLE_ID = TableId.of("DATASET_NAME", "BASE_TABLE_NAME"); | ||
| private static final String SNAPSHOT_TIME = "2021-05-19T11:32:26.553Z"; | ||
| private static final SnapshotTableDefinition SNAPSHOTTABLE_DEFINITION = | ||
| SnapshotTableDefinition.newBuilder() | ||
| .setBaseTableId(BASE_TABLE_ID) | ||
| .setSnapshotTime(SNAPSHOT_TIME) | ||
| .build(); | ||
| | ||
| @Test | ||
| public void testToBuilder() { | ||
| compareSnapshotTableDefinition( | ||
| SNAPSHOTTABLE_DEFINITION, SNAPSHOTTABLE_DEFINITION.toBuilder().build()); | ||
| SnapshotTableDefinition snapshotTableDefinition = | ||
| SNAPSHOTTABLE_DEFINITION.toBuilder().setSnapshotTime("2021-05-20T11:32:26.553Z").build(); | ||
| assertEquals("2021-05-20T11:32:26.553Z", snapshotTableDefinition.getSnapshotTime()); | ||
| } | ||
| | ||
| @Test | ||
| public void testBuilder() { | ||
| assertEquals(TableDefinition.Type.SNAPSHOT, SNAPSHOTTABLE_DEFINITION.getType()); | ||
| assertEquals(BASE_TABLE_ID, SNAPSHOTTABLE_DEFINITION.getBaseTableId()); | ||
| assertEquals(SNAPSHOT_TIME, SNAPSHOTTABLE_DEFINITION.getSnapshotTime()); | ||
| SnapshotTableDefinition snapshotTableDefinition = | ||
| SnapshotTableDefinition.newBuilder() | ||
| .setBaseTableId(BASE_TABLE_ID) | ||
| .setSnapshotTime(SNAPSHOT_TIME) | ||
| .build(); | ||
| assertEquals(SNAPSHOTTABLE_DEFINITION, snapshotTableDefinition); | ||
| } | ||
| | ||
| @Test | ||
| public void testToAndFromPb() { | ||
| SnapshotTableDefinition snapshotTableDefinition = SNAPSHOTTABLE_DEFINITION.toBuilder().build(); | ||
| assertTrue( | ||
| TableDefinition.fromPb(snapshotTableDefinition.toPb()) instanceof SnapshotTableDefinition); | ||
| compareSnapshotTableDefinition( | ||
| snapshotTableDefinition, | ||
| TableDefinition.<SnapshotTableDefinition>fromPb(snapshotTableDefinition.toPb())); | ||
| } | ||
| | ||
| private void compareSnapshotTableDefinition( | ||
| SnapshotTableDefinition expected, SnapshotTableDefinition value) { | ||
| assertEquals(expected, value); | ||
| assertEquals(expected.getBaseTableId(), value.getBaseTableId()); | ||
| assertEquals(expected.getSnapshotTime(), value.getSnapshotTime()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -86,6 +86,7 @@ | |
| import com.google.cloud.bigquery.RoutineId; | ||
| import com.google.cloud.bigquery.RoutineInfo; | ||
| import com.google.cloud.bigquery.Schema; | ||
| import com.google.cloud.bigquery.SnapshotTableDefinition; | ||
| import com.google.cloud.bigquery.StandardSQLDataType; | ||
| import com.google.cloud.bigquery.StandardSQLField; | ||
| import com.google.cloud.bigquery.StandardSQLTableType; | ||
| | @@ -2589,6 +2590,70 @@ public void testCopyJob() throws InterruptedException, TimeoutException { | |
| assertTrue(remoteTable.delete()); | ||
| } | ||
| | ||
| @Test | ||
| public void testSnapshotTableCopyJob() throws InterruptedException { | ||
| String sourceTableName = "test_copy_job_base_table"; | ||
| // this creates a snapshot table at specified snapshotTime | ||
| String snapshotTableName = String.format("test_snapshot_table"); | ||
| // Create source table | ||
| TableId sourceTableId = TableId.of(DATASET, sourceTableName); | ||
| StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); | ||
| TableInfo tableInfo = TableInfo.of(sourceTableId, tableDefinition); | ||
| Table createdTable = bigquery.create(tableInfo); | ||
stephaniewang526 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| assertNotNull(createdTable); | ||
| | ||
| // Create snapshot table using source table as the base table | ||
| TableId snapshotTableId = TableId.of(DATASET, snapshotTableName); | ||
| CopyJobConfiguration snapshotConfiguration = | ||
| CopyJobConfiguration.newBuilder(snapshotTableId, sourceTableId) | ||
| .setOperationType("SNAPSHOT") | ||
| .build(); | ||
| Job createdJob = bigquery.create(JobInfo.of(snapshotConfiguration)); | ||
| CopyJobConfiguration createdConfiguration = createdJob.getConfiguration(); | ||
| assertNotNull(createdConfiguration.getSourceTables()); | ||
| assertNotNull(createdConfiguration.getOperationType()); | ||
| assertNotNull(createdConfiguration.getDestinationTable()); | ||
| Job completedJob = createdJob.waitFor(); | ||
| assertNull(completedJob.getStatus().getError()); | ||
| Table snapshotTable = bigquery.getTable(DATASET, snapshotTableName); | ||
| assertNotNull(snapshotTable); | ||
| assertEquals(snapshotTableId.getDataset(), snapshotTable.getTableId().getDataset()); | ||
| assertEquals(snapshotTableName, snapshotTable.getTableId().getTable()); | ||
| assertTrue(snapshotTable.getDefinition() instanceof SnapshotTableDefinition); | ||
| assertEquals(TABLE_SCHEMA, snapshotTable.getDefinition().getSchema()); | ||
| assertNotNull(((SnapshotTableDefinition) snapshotTable.getDefinition()).getSnapshotTime()); | ||
| assertEquals( | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason you didn't just compare tableIds? Something with relative resolution of project? Contributor Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I would get this error if comparing tableIds: So I am comparing just the tableNames. | ||
| sourceTableName, | ||
| ((SnapshotTableDefinition) snapshotTable.getDefinition()).getBaseTableId().getTable()); | ||
| | ||
| // Restore base table to a new table | ||
| String restoredTableName = "test_restore_table"; | ||
| TableId restoredTableId = TableId.of(DATASET, restoredTableName); | ||
| CopyJobConfiguration restoreConfiguration = | ||
| CopyJobConfiguration.newBuilder(restoredTableId, snapshotTableId) | ||
| .setOperationType("RESTORE") | ||
| .build(); | ||
| Job createdRestoreJob = bigquery.create(JobInfo.of(restoreConfiguration)); | ||
| CopyJobConfiguration createdRestoreConfiguration = createdRestoreJob.getConfiguration(); | ||
| assertNotNull(createdRestoreConfiguration.getSourceTables()); | ||
stephaniewang526 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| assertNotNull(createdRestoreConfiguration.getOperationType()); | ||
| assertNotNull(createdRestoreConfiguration.getDestinationTable()); | ||
| Job completedRestoreJob = createdRestoreJob.waitFor(); | ||
| assertNull(completedRestoreJob.getStatus().getError()); | ||
| Table restoredTable = bigquery.getTable(DATASET, restoredTableName); | ||
| assertNotNull(restoredTable); | ||
| assertEquals(restoredTableId.getDataset(), restoredTable.getTableId().getDataset()); | ||
| assertEquals(restoredTableName, restoredTable.getTableId().getTable()); | ||
| assertEquals(TABLE_SCHEMA, restoredTable.getDefinition().getSchema()); | ||
| assertEquals(snapshotTable.getNumBytes(), restoredTable.getNumBytes()); | ||
| assertEquals(snapshotTable.getNumRows(), restoredTable.getNumRows()); | ||
| | ||
| // Clean up | ||
| assertTrue(createdTable.delete()); | ||
stephaniewang526 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| assertTrue(restoredTable.delete()); | ||
| assertTrue(snapshotTable.delete()); | ||
| } | ||
| | ||
| @Test | ||
| public void testCopyJobWithLabels() throws InterruptedException { | ||
| String sourceTableName = "test_copy_job_source_table_label"; | ||
| | ||
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.