Skip to content

Commit 9207743

Browse files
authored
feat: Partial Projection of Table Metadata (#2756)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/java-bigquery/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #<issue_number_goes_here> ☕️ If you write sample code, please follow the [samples format]( https://togithub.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md).
1 parent afb571c commit 9207743

File tree

5 files changed

+160
-4
lines changed

5 files changed

+160
-4
lines changed

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ public String getSelector() {
121121
}
122122
}
123123

124+
/**
125+
* Metadata of a BigQuery Table.
126+
*
127+
* @see <a href=
128+
* "https://cloud.google.com/bigquery/docs/reference/rest/v2/tables/get#tablemetadataview">Table
129+
* Resource</a>
130+
*/
131+
enum TableMetadataView {
132+
BASIC,
133+
FULL,
134+
STORAGE_STATS,
135+
TABLE_METADATA_VIEW_UNSPECIFIED;
136+
}
137+
124138
/**
125139
* Fields of a BigQuery Model resource.
126140
*
@@ -384,6 +398,11 @@ public static TableOption fields(TableField... fields) {
384398
public static TableOption autodetectSchema(boolean autodetect) {
385399
return new TableOption(BigQueryRpc.Option.AUTODETECT_SCHEMA, autodetect);
386400
}
401+
402+
/** Returns an option to specify the metadata of the table. */
403+
public static TableOption tableMetadataView(TableMetadataView tableMetadataView) {
404+
return new TableOption(BigQueryRpc.Option.TABLE_METADATA_VIEW, tableMetadataView);
405+
}
387406
}
388407

389408
/* Class for specifying IAM options. */

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public static class Builder extends TableInfo.Builder {
4949
private final BigQuery bigquery;
5050
private final TableInfo.BuilderImpl infoBuilder;
5151

52-
Builder(BigQuery bigquery, TableId tableId, TableDefinition defintion) {
52+
Builder(BigQuery bigquery, TableId tableId, TableDefinition definition) {
5353
this.bigquery = bigquery;
5454
this.infoBuilder = new TableInfo.BuilderImpl();
55-
this.infoBuilder.setTableId(tableId).setDefinition(defintion);
55+
this.infoBuilder.setTableId(tableId).setDefinition(definition);
5656
}
5757

5858
Builder(Table table) {

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
@InternalExtensionOnly
4141
public interface BigQueryRpc extends ServiceRpc {
4242

43-
// These options are part of the Google Cloud BigQuery query parameters
43+
// These options are part of the Google Cloud BigQuery query parameters.
4444
enum Option {
4545
FIELDS("fields"),
4646
DELETE_CONTENTS("deleteContents"),
@@ -56,7 +56,8 @@ enum Option {
5656
START_INDEX("startIndex"),
5757
STATE_FILTER("stateFilter"),
5858
TIMEOUT("timeoutMs"),
59-
REQUESTED_POLICY_VERSION("requestedPolicyVersion");
59+
REQUESTED_POLICY_VERSION("requestedPolicyVersion"),
60+
TABLE_METADATA_VIEW("view");
6061

6162
private final String value;
6263

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ public Table getTable(
294294
.get(projectId, datasetId, tableId)
295295
.setPrettyPrint(false)
296296
.setFields(Option.FIELDS.getString(options))
297+
.setView(getTableMetadataOption(options))
297298
.execute();
298299
} catch (IOException ex) {
299300
BigQueryException serviceException = translate(ex);
@@ -304,6 +305,13 @@ public Table getTable(
304305
}
305306
}
306307

308+
private String getTableMetadataOption(Map<Option, ?> options) {
309+
if (options.containsKey(Option.TABLE_METADATA_VIEW)) {
310+
return options.get(Option.TABLE_METADATA_VIEW).toString();
311+
}
312+
return "STORAGE_STATS";
313+
}
314+
307315
@Override
308316
public Tuple<String, Iterable<Table>> listTables(
309317
String projectId, String datasetId, Map<Option, ?> options) {

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.google.cloud.bigquery.BigQuery.JobListOption;
5050
import com.google.cloud.bigquery.BigQuery.JobOption;
5151
import com.google.cloud.bigquery.BigQuery.TableField;
52+
import com.google.cloud.bigquery.BigQuery.TableMetadataView;
5253
import com.google.cloud.bigquery.BigQuery.TableOption;
5354
import com.google.cloud.bigquery.BigQueryDryRunResult;
5455
import com.google.cloud.bigquery.BigQueryError;
@@ -1549,6 +1550,133 @@ public void testCreateAndGetTable() {
15491550
assertTrue(remoteTable.delete());
15501551
}
15511552

1553+
@Test
1554+
public void testCreateAndGetTableWithBasicTableMetadataView() {
1555+
String tableName = "test_create_and_get_table_with_basic_metadata_view";
1556+
TableId tableId = TableId.of(DATASET, tableName);
1557+
TimePartitioning partitioning = TimePartitioning.of(Type.DAY);
1558+
Clustering clustering =
1559+
Clustering.newBuilder().setFields(ImmutableList.of(STRING_FIELD_SCHEMA.getName())).build();
1560+
StandardTableDefinition tableDefinition =
1561+
StandardTableDefinition.newBuilder()
1562+
.setSchema(TABLE_SCHEMA)
1563+
.setTimePartitioning(partitioning)
1564+
.setClustering(clustering)
1565+
.build();
1566+
Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition));
1567+
assertNotNull(createdTable);
1568+
assertEquals(DATASET, createdTable.getTableId().getDataset());
1569+
assertEquals(tableName, createdTable.getTableId().getTable());
1570+
TableOption tableOption = BigQuery.TableOption.tableMetadataView(TableMetadataView.BASIC);
1571+
Table remoteTable = bigquery.getTable(DATASET, tableName, tableOption);
1572+
assertNotNull(remoteTable);
1573+
assertTrue(remoteTable.getDefinition() instanceof StandardTableDefinition);
1574+
assertEquals(createdTable.getTableId(), remoteTable.getTableId());
1575+
assertEquals(TableDefinition.Type.TABLE, remoteTable.getDefinition().getType());
1576+
assertEquals(TABLE_SCHEMA, remoteTable.getDefinition().getSchema());
1577+
// Next four values are considered transient fields that should not be calculated
1578+
assertNull(remoteTable.getLastModifiedTime());
1579+
assertNull(remoteTable.<StandardTableDefinition>getDefinition().getNumBytes());
1580+
assertNull(remoteTable.<StandardTableDefinition>getDefinition().getNumLongTermBytes());
1581+
assertNull(remoteTable.<StandardTableDefinition>getDefinition().getNumRows());
1582+
assertTrue(remoteTable.delete());
1583+
}
1584+
1585+
@Test
1586+
public void testCreateAndGetTableWithFullTableMetadataView() {
1587+
String tableName = "test_create_and_get_table_with_full_metadata_view";
1588+
TableId tableId = TableId.of(DATASET, tableName);
1589+
TimePartitioning partitioning = TimePartitioning.of(Type.DAY);
1590+
Clustering clustering =
1591+
Clustering.newBuilder().setFields(ImmutableList.of(STRING_FIELD_SCHEMA.getName())).build();
1592+
StandardTableDefinition tableDefinition =
1593+
StandardTableDefinition.newBuilder()
1594+
.setSchema(TABLE_SCHEMA)
1595+
.setTimePartitioning(partitioning)
1596+
.setClustering(clustering)
1597+
.build();
1598+
Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition));
1599+
assertNotNull(createdTable);
1600+
assertEquals(DATASET, createdTable.getTableId().getDataset());
1601+
assertEquals(tableName, createdTable.getTableId().getTable());
1602+
TableOption tableOption = BigQuery.TableOption.tableMetadataView(TableMetadataView.FULL);
1603+
Table remoteTable = bigquery.getTable(DATASET, tableName, tableOption);
1604+
assertNotNull(remoteTable);
1605+
assertTrue(remoteTable.getDefinition() instanceof StandardTableDefinition);
1606+
assertEquals(createdTable.getTableId(), remoteTable.getTableId());
1607+
assertEquals(TableDefinition.Type.TABLE, remoteTable.getDefinition().getType());
1608+
assertEquals(TABLE_SCHEMA, remoteTable.getDefinition().getSchema());
1609+
assertNotNull(remoteTable.getLastModifiedTime());
1610+
assertNotNull(remoteTable.<StandardTableDefinition>getDefinition().getNumBytes());
1611+
assertNotNull(remoteTable.<StandardTableDefinition>getDefinition().getNumLongTermBytes());
1612+
assertNotNull(remoteTable.<StandardTableDefinition>getDefinition().getNumRows());
1613+
assertTrue(remoteTable.delete());
1614+
}
1615+
1616+
@Test
1617+
public void testCreateAndGetTableWithStorageStatsTableMetadataView() {
1618+
String tableName = "test_create_and_get_table_with_storage_stats_metadata_view";
1619+
TableId tableId = TableId.of(DATASET, tableName);
1620+
TimePartitioning partitioning = TimePartitioning.of(Type.DAY);
1621+
Clustering clustering =
1622+
Clustering.newBuilder().setFields(ImmutableList.of(STRING_FIELD_SCHEMA.getName())).build();
1623+
StandardTableDefinition tableDefinition =
1624+
StandardTableDefinition.newBuilder()
1625+
.setSchema(TABLE_SCHEMA)
1626+
.setTimePartitioning(partitioning)
1627+
.setClustering(clustering)
1628+
.build();
1629+
Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition));
1630+
assertNotNull(createdTable);
1631+
assertEquals(DATASET, createdTable.getTableId().getDataset());
1632+
assertEquals(tableName, createdTable.getTableId().getTable());
1633+
TableOption tableOption =
1634+
BigQuery.TableOption.tableMetadataView(TableMetadataView.STORAGE_STATS);
1635+
Table remoteTable = bigquery.getTable(DATASET, tableName, tableOption);
1636+
assertNotNull(remoteTable);
1637+
assertTrue(remoteTable.getDefinition() instanceof StandardTableDefinition);
1638+
assertEquals(createdTable.getTableId(), remoteTable.getTableId());
1639+
assertEquals(TableDefinition.Type.TABLE, remoteTable.getDefinition().getType());
1640+
assertEquals(TABLE_SCHEMA, remoteTable.getDefinition().getSchema());
1641+
assertNotNull(remoteTable.getLastModifiedTime());
1642+
assertNotNull(remoteTable.<StandardTableDefinition>getDefinition().getNumBytes());
1643+
assertNotNull(remoteTable.<StandardTableDefinition>getDefinition().getNumLongTermBytes());
1644+
assertNotNull(remoteTable.<StandardTableDefinition>getDefinition().getNumRows());
1645+
assertTrue(remoteTable.delete());
1646+
}
1647+
1648+
@Test
1649+
public void testCreateAndGetTableWithUnspecifiedTableMetadataView() {
1650+
String tableName = "test_create_and_get_table_with_unspecified_metadata_view";
1651+
TableId tableId = TableId.of(DATASET, tableName);
1652+
TimePartitioning partitioning = TimePartitioning.of(Type.DAY);
1653+
Clustering clustering =
1654+
Clustering.newBuilder().setFields(ImmutableList.of(STRING_FIELD_SCHEMA.getName())).build();
1655+
StandardTableDefinition tableDefinition =
1656+
StandardTableDefinition.newBuilder()
1657+
.setSchema(TABLE_SCHEMA)
1658+
.setTimePartitioning(partitioning)
1659+
.setClustering(clustering)
1660+
.build();
1661+
Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition));
1662+
assertNotNull(createdTable);
1663+
assertEquals(DATASET, createdTable.getTableId().getDataset());
1664+
assertEquals(tableName, createdTable.getTableId().getTable());
1665+
TableOption tableOption =
1666+
BigQuery.TableOption.tableMetadataView(TableMetadataView.TABLE_METADATA_VIEW_UNSPECIFIED);
1667+
Table remoteTable = bigquery.getTable(DATASET, tableName, tableOption);
1668+
assertNotNull(remoteTable);
1669+
assertTrue(remoteTable.getDefinition() instanceof StandardTableDefinition);
1670+
assertEquals(createdTable.getTableId(), remoteTable.getTableId());
1671+
assertEquals(TableDefinition.Type.TABLE, remoteTable.getDefinition().getType());
1672+
assertEquals(TABLE_SCHEMA, remoteTable.getDefinition().getSchema());
1673+
assertNotNull(remoteTable.getLastModifiedTime());
1674+
assertNotNull(remoteTable.<StandardTableDefinition>getDefinition().getNumBytes());
1675+
assertNotNull(remoteTable.<StandardTableDefinition>getDefinition().getNumLongTermBytes());
1676+
assertNotNull(remoteTable.<StandardTableDefinition>getDefinition().getNumRows());
1677+
assertTrue(remoteTable.delete());
1678+
}
1679+
15521680
@Test
15531681
public void testCreateAndGetTableWithSelectedField() {
15541682
String tableName = "test_create_and_get_selected_fields_table";

0 commit comments

Comments
 (0)