Skip to content

Commit c1fb390

Browse files
gsabhnanitswast
authored andcommitted
Add model table type for the new BigQuery ML models (#3529)
Fixes listTables on datasets containing models.
1 parent 5a5c66f commit c1fb390

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.bigquery;
18+
19+
import com.google.api.core.BetaApi;
20+
import com.google.api.services.bigquery.model.Table;
21+
import com.google.auto.value.AutoValue;
22+
import javax.annotation.Nullable;
23+
24+
/**
25+
* A Google BigQuery Model table definition. This definition is used to represent a BigQuery
26+
* ML model.
27+
*
28+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/standard-sql/bigqueryml-syntax-create#models_in_bqml_name">BigQuery ML Model</a>
29+
*/
30+
@AutoValue
31+
@BetaApi
32+
public abstract class ModelTableDefinition extends TableDefinition {
33+
34+
private static final long serialVersionUID = 2113445776046717900L;
35+
36+
@AutoValue.Builder
37+
public abstract static class Builder
38+
extends TableDefinition.Builder<ModelTableDefinition, Builder> {
39+
40+
public abstract Builder setNumBytes(Long numBytes);
41+
42+
public abstract Builder setLocation(String location);
43+
44+
public abstract Builder setType(Type type);
45+
46+
/** Creates a {@code ModelTableDefinition} object. */
47+
public abstract ModelTableDefinition build();
48+
}
49+
50+
/** Returns the size of this table in bytes, excluding any data in the streaming buffer. */
51+
@Nullable
52+
public abstract Long getNumBytes();
53+
54+
/**
55+
* Returns the geographic location where the table should reside. This value is inherited from the
56+
* dataset.
57+
*
58+
* @see <a
59+
* href="https://cloud.google.com/bigquery/docs/managing_jobs_datasets_projects#dataset-location">
60+
* Dataset Location</a>
61+
*/
62+
@Nullable
63+
public abstract String getLocation();
64+
65+
/**
66+
* Returns a builder for a BigQuery ML model table definition.
67+
*/
68+
public static Builder newBuilder() {
69+
return new AutoValue_ModelTableDefinition.Builder().setType(Type.MODEL);
70+
}
71+
72+
/** Returns a builder for the {@code <ModelTableDefinition} object. */
73+
public abstract Builder toBuilder();
74+
75+
@Override
76+
Table toPb() {
77+
Table tablePb = super.toPb();
78+
tablePb.setNumBytes(getNumBytes());
79+
tablePb.setLocation(getLocation());
80+
return tablePb;
81+
}
82+
83+
@SuppressWarnings("unchecked")
84+
static ModelTableDefinition fromPb(Table tablePb) {
85+
Builder builder = newBuilder().table(tablePb);
86+
return builder.setNumBytes(tablePb.getNumBytes()).setLocation(tablePb.getLocation()).build();
87+
}
88+
}

google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableDefinition.java

+10
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public Type apply(String constant) {
7171
*/
7272
public static final Type EXTERNAL = type.createAndRegister("EXTERNAL");
7373

74+
/**
75+
* A BigQuery table representing BigQuery ML Model.
76+
*
77+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/standard-sql/bigqueryml-syntax-create#models_in_bqml_name">
78+
* BigQuery ML Model</a>
79+
*/
80+
public static final Type MODEL = type.createAndRegister("MODEL");
81+
7482
private Type(String constant) {
7583
super(constant);
7684
}
@@ -157,6 +165,8 @@ static <T extends TableDefinition> T fromPb(Table tablePb) {
157165
return (T) ViewDefinition.fromPb(tablePb);
158166
case "EXTERNAL":
159167
return (T) ExternalTableDefinition.fromPb(tablePb);
168+
case "MODEL":
169+
return (T) ModelTableDefinition.fromPb(tablePb);
160170
default:
161171
// never reached
162172
throw new IllegalArgumentException("Format " + tablePb.getType() + " is not supported");

google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
/**
3333
* Google BigQuery table information. Use {@link StandardTableDefinition} to create simple BigQuery
3434
* table. Use {@link ViewDefinition} to create a BigQuery view. Use {@link ExternalTableDefinition}
35-
* to create a BigQuery a table backed by external data.
35+
* to create a BigQuery a table backed by external data. Use {@link ModelDefinition} to create a
36+
* BigQuery ML model.
3637
*
3738
* @see <a href="https://cloud.google.com/bigquery/docs/tables">Managing Tables</a>
3839
*/

google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,14 @@ public class BigQueryImplTest {
108108
private static final Schema TABLE_SCHEMA = Schema.of(FIELD_SCHEMA1, FIELD_SCHEMA2);
109109
private static final StandardTableDefinition TABLE_DEFINITION =
110110
StandardTableDefinition.of(TABLE_SCHEMA);
111+
private static final ModelTableDefinition MODEL_TABLE_DEFINITION =
112+
ModelTableDefinition.newBuilder().build();
111113
private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID, TABLE_DEFINITION);
112114
private static final TableInfo OTHER_TABLE_INFO = TableInfo.of(OTHER_TABLE_ID, TABLE_DEFINITION);
113115
private static final TableInfo TABLE_INFO_WITH_PROJECT =
114116
TableInfo.of(TABLE_ID_WITH_PROJECT, TABLE_DEFINITION);
117+
private static final TableInfo MODEL_TABLE_INFO_WITH_PROJECT =
118+
TableInfo.of(TABLE_ID_WITH_PROJECT, MODEL_TABLE_DEFINITION);
115119
private static final LoadJobConfiguration LOAD_JOB_CONFIGURATION =
116120
LoadJobConfiguration.of(TABLE_ID, "URI");
117121
private static final LoadJobConfiguration LOAD_JOB_CONFIGURATION_WITH_PROJECT =
@@ -613,7 +617,8 @@ public void testListTables() {
613617
ImmutableList<Table> tableList =
614618
ImmutableList.of(
615619
new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)),
616-
new Table(bigquery, new TableInfo.BuilderImpl(OTHER_TABLE_INFO)));
620+
new Table(bigquery, new TableInfo.BuilderImpl(OTHER_TABLE_INFO)),
621+
new Table(bigquery, new TableInfo.BuilderImpl(MODEL_TABLE_INFO_WITH_PROJECT)));
617622
Tuple<String, Iterable<com.google.api.services.bigquery.model.Table>> result =
618623
Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION));
619624
EasyMock.expect(bigqueryRpcMock.listTables(PROJECT, DATASET, EMPTY_RPC_OPTIONS))

0 commit comments

Comments
 (0)