diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableInfo.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableInfo.java index 9ca74eb3c1f7..c899ac53da94 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableInfo.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableInfo.java @@ -59,29 +59,6 @@ public Table apply(TableInfo tableInfo) { private static final long serialVersionUID = -7679032506430816205L; - /** - * The table type. - */ - public enum Type { - /** - * A normal BigQuery table. - */ - TABLE, - /** - * A virtual table defined by a SQL query. - * - * @see Views - */ - VIEW, - /** - * A BigQuery table backed by external data. - * - * @see Federated Data - * Sources - */ - EXTERNAL - } - /** * Google BigQuery Table's Streaming Buffer information. This class contains information on a * table's streaming buffer as the estimated size in number of rows/bytes. @@ -159,18 +136,14 @@ static StreamingBuffer fromPb(Streamingbuffer streamingBufferPb) { private final String id; private final String selfLink; private final TableId tableId; + private final TableType type; private final String friendlyName; private final String description; - private final Schema schema; private final Long numBytes; private final Long numRows; private final Long creationTime; private final Long expirationTime; private final Long lastModifiedTime; - private final String viewQuery; - private final List userDefinedFunctions; - private final Type type; - private final ExternalDataConfiguration externalConfiguration; private final String location; private final StreamingBuffer streamingBuffer; @@ -180,18 +153,14 @@ public static final class Builder { private String id; private String selfLink; private TableId tableId; + private TableType type; private String friendlyName; private String description; - private Schema schema; private Long numBytes; private Long numRows; private Long creationTime; private Long expirationTime; private Long lastModifiedTime; - private String viewQuery; - private List userDefinedFunctions; - private Type type; - private ExternalDataConfiguration externalConfiguration; private String location; private StreamingBuffer streamingBuffer; @@ -202,18 +171,14 @@ private Builder(TableInfo tableInfo) { this.id = tableInfo.id; this.selfLink = tableInfo.selfLink; this.tableId = tableInfo.tableId; + this.type = tableInfo.type; this.friendlyName = tableInfo.friendlyName; this.description = tableInfo.description; - this.schema = tableInfo.schema; this.numBytes = tableInfo.numBytes; this.numRows = tableInfo.numRows; this.creationTime = tableInfo.creationTime; this.expirationTime = tableInfo.expirationTime; this.lastModifiedTime = tableInfo.lastModifiedTime; - this.viewQuery = tableInfo.viewQuery; - this.userDefinedFunctions = tableInfo.userDefinedFunctions; - this.type = tableInfo.type; - this.externalConfiguration = tableInfo.externalConfiguration; this.location = tableInfo.location; this.streamingBuffer = tableInfo.streamingBuffer; } @@ -245,18 +210,6 @@ public Builder expirationTime(Long expirationTime) { return this; } - /** - * Sets the data format, location, and other properties of a table stored outside of BigQuery. - * This property is experimental and might be subject to change or removed. - * - * @see Federated Data - * Sources - */ - public Builder externalConfiguration(ExternalDataConfiguration externalConfiguration) { - this.externalConfiguration = externalConfiguration; - return this; - } - /** * Sets a user-friendly name for the table. */ @@ -290,15 +243,6 @@ Builder numRows(Long numRows) { return this; } - /** - * Sets the table's schema. Providing a schema is not necessary when {@link #viewQuery(String)} - * or {@link #externalConfiguration(ExternalDataConfiguration)} are provided. - */ - public Builder schema(Schema schema) { - this.schema = schema; - return this; - } - Builder selfLink(String selfLink) { this.selfLink = selfLink; return this; @@ -317,29 +261,11 @@ public Builder tableId(TableId tableId) { return this; } - Builder type(Type type) { - this.type = type; - return this; - } - /** - * Sets the query used to create a table of {@link Type#VIEW} type. + * Sets the table type. */ - public Builder viewQuery(String viewQuery) { - this.viewQuery = viewQuery; - return this; - } - - /** - * Sets user-defined functions to be used by the view's query. User defined functions are only - * used when {@link #type(TableInfo.Type)} is set to {@link Type#VIEW} and a query is provided via - * {@link #viewQuery(String)}. - * - * @see User-Defined - * Functions - */ - public Builder userDefinedFunctions(List userDefinedFunctions) { - this.userDefinedFunctions = userDefinedFunctions; + public Builder type(TableType type) { + this.type = type; return this; } @@ -358,16 +284,12 @@ private TableInfo(Builder builder) { this.selfLink = builder.selfLink; this.friendlyName = builder.friendlyName; this.description = builder.description; - this.schema = builder.schema; + this.type = builder.type; this.numBytes = builder.numBytes; this.numRows = builder.numRows; this.creationTime = builder.creationTime; this.expirationTime = builder.expirationTime; this.lastModifiedTime = builder.lastModifiedTime; - this.viewQuery = builder.viewQuery; - this.userDefinedFunctions = builder.userDefinedFunctions; - this.type = builder.type; - this.externalConfiguration = builder.externalConfiguration; this.location = builder.location; this.streamingBuffer = builder.streamingBuffer; } @@ -405,21 +327,14 @@ public TableId tableId() { * Returns a user-friendly name for the table. */ public String friendlyName() { - return friendlyName; + return Data.isNull(friendlyName) ? null : friendlyName; } /** * Returns a user-friendly description for the table. */ public String description() { - return description; - } - - /** - * Returns the table's schema. - */ - public Schema schema() { - return schema; + return Data.isNull(description) ? null : description; } /** @@ -448,7 +363,7 @@ public Long creationTime() { * table will persist indefinitely. Expired tables will be deleted and their storage reclaimed. */ public Long expirationTime() { - return expirationTime; + return Data.isNull(expirationTime) ? null : expirationTime; } /** @@ -458,43 +373,22 @@ public Long lastModifiedTime() { return lastModifiedTime; } - /** - * If this table is a view ({@link #type()} returns {@link Type#VIEW}) this method returns the - * query used to create the view. Returns {@code null} otherwise. - */ - public String viewQuery() { - return viewQuery; - } - - /** - * If this table is a view ({@link #type()} returns {@link Type#VIEW} this method returns user - * defined functions that can be used by {@link #viewQuery}. - * - * @see User-Defined Functions - * - */ - public List userDefinedFunctions() { - return userDefinedFunctions; - } - /** * Returns the type of the table. */ - public Type type() { - return type; + public Schema schema() { + return type.schema(); } /** - * If this table is external ({@link #type()} returns {@link Type#EXTERNAL}) this method returns - * the data format, location, and other properties of a table stored outside of BigQuery. - * If the table is not {@link Type#EXTERNAL} this method returns {@code null}. This property - * is experimental and might be subject to change or removed. - * - * @see Federated Data Sources - * + * Returns the table's type. If this table is simple table the method returns an instance of + * {@link TableType}. If this table is an external table this method returns an instance of + * {@link com.google.gcloud.bigquery.TableType.ExternalTable}. If this table is a view table this + * method returns an instance of {@link com.google.gcloud.bigquery.TableType.View}. */ - public ExternalDataConfiguration externalConfiguration() { - return externalConfiguration; + @SuppressWarnings("unchecked") + public T type() { + return (T) type; } /** @@ -527,10 +421,6 @@ public Builder toBuilder() { public String toString() { return MoreObjects.toStringHelper(this) .add("tableId", tableId) - .add("schema", schema) - .add("externalDataConfiguration", externalConfiguration) - .add("query", viewQuery) - .add("userDefinedFunctions", userDefinedFunctions) .add("type", type) .add("location", location) .add("streamingBuffer", streamingBuffer) @@ -560,31 +450,30 @@ public boolean equals(Object obj) { Table toPb() { Table tablePb = new Table(); tablePb.setTableReference(tableId.toPb()); - if (externalConfiguration != null) { - tablePb.setExternalDataConfiguration(externalConfiguration.toPb()); - } if (lastModifiedTime != null) { tablePb.setLastModifiedTime(BigInteger.valueOf(lastModifiedTime)); } if (numRows != null) { tablePb.setNumRows(BigInteger.valueOf(numRows)); } - if (schema != null) { - tablePb.setSchema(schema.toPb()); + if (schema() != null) { + tablePb.setSchema(schema().toPb()); } - if (streamingBuffer != null) { - tablePb.setStreamingBuffer(streamingBuffer.toPb()); - } - if (viewQuery != null) { - ViewDefinition viewDefinition = new ViewDefinition().setQuery(viewQuery); - if (userDefinedFunctions != null) { + if (type instanceof TableType.View) { + TableType.View viewType = (TableType.View) type; + ViewDefinition viewDefinition = new ViewDefinition().setQuery(viewType.query()); + if (viewType.userDefinedFunctions() != null) { viewDefinition.setUserDefinedFunctionResources( - Lists.transform(userDefinedFunctions, UserDefinedFunction.TO_PB_FUNCTION)); + Lists.transform(viewType.userDefinedFunctions(), UserDefinedFunction.TO_PB_FUNCTION)); } - tablePb.setView(new ViewDefinition().setQuery(viewQuery)); + tablePb.setView(viewDefinition); } - if (type != null) { - tablePb.setType(type.name()); + if (type instanceof TableType.ExternalTable) { + tablePb.setExternalDataConfiguration( + ((TableType.ExternalTable) type).configuration().toPb()); + } + if (streamingBuffer != null) { + tablePb.setStreamingBuffer(streamingBuffer.toPb()); } tablePb.setCreationTime(creationTime); tablePb.setDescription(description); @@ -599,99 +488,38 @@ Table toPb() { } /** - * Returns a builder for a BigQuery table backed by external data. External tables are - * experimental and might be subject to change or removed. - * - * @param tableId table id - * @param configuration configuration for external data source - */ - public static Builder builder(TableId tableId, ExternalDataConfiguration configuration) { - return new Builder().tableId(tableId).externalConfiguration(configuration); - } - - /** - * Returns a builder for a BigQuery view. - * - * @param tableId table id - * @param query query used to create the view - */ - public static Builder builder(TableId tableId, String query) { - return new Builder().tableId(tableId).viewQuery(query); - } - - /** - * Returns a builder for a BigQuery view. + * Returns a builder for a BigQuery table given its type. * * @param tableId table id - * @param query query used to create the view + * @param type the type of the table */ - public static Builder builder(TableId tableId, String query, List functions) { - return new Builder().tableId(tableId).viewQuery(query).userDefinedFunctions(functions); + public static Builder builder(TableId tableId, TableType type) { + return new Builder().tableId(tableId).type(type); } /** - * Returns a builder for a BigQuery table. + * Creates BigQuery table given its type * * @param tableId table id - * @param schema table's schema definition + * @param type the type of the table */ - public static Builder builder(TableId tableId, Schema schema) { - return new Builder().tableId(tableId).schema(schema); - } - - /** - * Creates BigQuery table backed by external data. - * - * @param tableId table id - * @param configuration configuration for external data source - */ - public static TableInfo of(TableId tableId, ExternalDataConfiguration configuration) { - return builder(tableId, configuration).build(); - } - - /** - * Creates a BigQuery view. - * - * @param tableId table id - * @param query query used to create the view - */ - public static TableInfo of(TableId tableId, String query) { - return builder(tableId, query).build(); - } - - /** - * Creates a BigQuery view. - * - * @param tableId table id - * @param query query used to create the view - * @param functions user defined funtions that can be used in {@code query} - */ - public static TableInfo of(TableId tableId, String query, List functions) { - return builder(tableId, query, functions).build(); - } - - /** - * Creates a BigQuery table. - * - * @param tableId table id - * @param schema table's schema definition - */ - public static TableInfo of(TableId tableId, Schema schema) { - return builder(tableId, schema).build(); + public static TableInfo of(TableId tableId, TableType type) { + return builder(tableId, type).build(); } static TableInfo fromPb(Table tablePb) { Builder builder = new Builder().tableId(TableId.fromPb(tablePb.getTableReference())); + Schema schema = null; + if (tablePb.getSchema() != null) { + schema = Schema.fromPb(tablePb.getSchema()); + builder.type(TableType.table(schema)); + } if (tablePb.getDescription() != null) { builder.description(tablePb.getDescription()); } if (tablePb.getExpirationTime() != null) { builder.expirationTime(tablePb.getExpirationTime()); } - if (tablePb.getExternalDataConfiguration() != null) { - builder.externalConfiguration( - ExternalDataConfiguration.fromPb(tablePb.getExternalDataConfiguration())); - } if (tablePb.getFriendlyName() != null) { builder.friendlyName(tablePb.getFriendlyName()); } @@ -704,22 +532,22 @@ static TableInfo fromPb(Table tablePb) { if (tablePb.getNumRows() != null) { builder.numRows(tablePb.getNumRows().longValue()); } - if (tablePb.getSchema() != null) { - builder.schema(Schema.fromPb(tablePb.getSchema())); - } if (tablePb.getStreamingBuffer() != null) { builder.streamingBuffer(StreamingBuffer.fromPb(tablePb.getStreamingBuffer())); } - if (tablePb.getType() != null) { - builder.type(Type.valueOf(tablePb.getType())); - } if (tablePb.getView() != null) { - builder.viewQuery(tablePb.getView().getQuery()); + String query = tablePb.getView().getQuery(); + List functions = null; if (tablePb.getView().getUserDefinedFunctionResources() != null) { - builder.userDefinedFunctions( - Lists.transform(tablePb.getView().getUserDefinedFunctionResources(), - UserDefinedFunction.FROM_PB_FUNCTION)); + functions = Lists.transform(tablePb.getView().getUserDefinedFunctionResources(), + UserDefinedFunction.FROM_PB_FUNCTION); } + builder.type(new TableType.View(schema, query, functions)); + } + if (tablePb.getExternalDataConfiguration() != null) { + ExternalDataConfiguration configuration = + ExternalDataConfiguration.fromPb(tablePb.getExternalDataConfiguration()); + builder.type(new TableType.ExternalTable(schema, configuration)); } if (tablePb.getCreationTime() != null) { builder.creationTime(tablePb.getCreationTime()); diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableType.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableType.java new file mode 100644 index 000000000000..9a74d55e8bbb --- /dev/null +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableType.java @@ -0,0 +1,260 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * 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.gcloud.bigquery; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.base.MoreObjects; +import com.google.common.collect.ImmutableList; + +import java.io.Serializable; +import java.util.List; +import java.util.Objects; + +/** + * Google BigQuery Table type. A BigQuery table can be of three types. {@link Type#TABLE} is a + * normal BigQuery table. {@link Type#VIEW} is a view table, created from a query. + * {@link Type#EXTERNAL} is an external table, backed by Google Cloud Storage data. + */ +public class TableType implements Serializable { + + /** + * The table type. + */ + public enum Type { + /** + * A normal BigQuery table. + */ + TABLE, + /** + * A virtual table defined by a SQL query. + * + * @see Views + */ + VIEW, + /** + * A BigQuery table backed by external data. + * + * @see Federated Data + * Sources + */ + EXTERNAL + } + + private final Type type; + private final Schema schema; + + TableType(Type type, Schema schema) { + this.type = type; + this.schema = schema; + } + + /** + * Returns the type of the table. + */ + public Type type() { + return type; + } + + /** + * Returns the table's schema. + */ + public Schema schema() { + return schema; + } + + /** + * Google BigQuery View type. + */ + public static class View extends TableType { + + private static final long serialVersionUID = 8275064752050652381L; + + private final String query; + private final List userDefinedFunctions; + + View(String query) { + this(null, query, null); + } + + View(String query, List userDefinedFunctions) { + this(null, query, userDefinedFunctions); + } + + View(Schema schema, String query, List userDefinedFunctions) { + super(Type.VIEW, schema); + this.query = query; + this.userDefinedFunctions = userDefinedFunctions; + } + + /** + * Returns the query used to create the view. + */ + public String query() { + return query; + }; + + /** + * Returns user defined functions that can be used by {@link #query()}. Returns {@code null} if + * not set. + * + * @see User-Defined Functions + * + */ + public List userDefinedFunctions() { + return userDefinedFunctions; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("type", type()) + .add("query", query) + .add("userDefinedFunctions", userDefinedFunctions) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash(schema(), query, userDefinedFunctions); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof View)) { + return false; + } + View other = (View) obj; + return Objects.equals(type(), other.type()) + && Objects.equals(query, other.query) + && Objects.equals(userDefinedFunctions, other.userDefinedFunctions); + } + } + + /** + * Google BigQuery External Table type. + */ + public static class ExternalTable extends TableType { + + private static final long serialVersionUID = -6912214693318638552L; + + private final ExternalDataConfiguration configuration; + + ExternalTable(ExternalDataConfiguration configuration) { + this(null, configuration); + } + + ExternalTable(Schema schema, ExternalDataConfiguration configuration) { + super(Type.EXTERNAL, schema); + this.configuration = configuration; + } + + /** + * Returns the data format, location and other properties of a table stored outside of BigQuery. + * This property is experimental and might be subject to change or removed. + * + * @see Federated Data + * Sources + */ + public ExternalDataConfiguration configuration() { + return configuration; + }; + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("type", type()) + .add("configuration", configuration) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash(schema(), configuration); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof ExternalTable)) { + return false; + } + ExternalTable other = (ExternalTable) obj; + return Objects.equals(type(), other.type()) + && Objects.equals(configuration, other.configuration); + } + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("type", type) + .add("schema", schema) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash(type, schema); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof TableType)) { + return false; + } + TableType other = (TableType) obj; + return Objects.equals(type, other.type) + && Objects.equals(schema, other.schema); + } + + /** + * Returns a simple BigQuery Table type. + */ + public static TableType table(Schema schema) { + return new TableType(Type.TABLE, checkNotNull(schema)); + } + + /** + * Returns a BigQuery View type given the query needed to create the view. + */ + public static View view(String query) { + return new View(checkNotNull(query)); + } + + /** + * Returns a BigQuery View type given the query needed to create the view and a list of user + * defined functions that can be used by the query. + */ + public static View view(String query, List userDefinedFunctions) { + return new View(checkNotNull(query), checkNotNull(userDefinedFunctions)); + } + + /** + * Returns a BigQuery View type given the query needed to create the view and a list of user + * defined functions that can be used by the query. + */ + public static View view(String query, UserDefinedFunction... userDefinedFunctions) { + return new View(checkNotNull(query), ImmutableList.copyOf(userDefinedFunctions)); + } + + /** + * Returns a BigQuery External Table type given the external data configuration. + */ + public static ExternalTable externalTable(ExternalDataConfiguration configuration) { + return new ExternalTable(checkNotNull(configuration)); + } +} diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java index b3bdbd3eda57..86e18ac74c97 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java @@ -104,15 +104,15 @@ public class SerializationTest { new UserDefinedFunction.InlineFunction("inline"); private static final UserDefinedFunction URI_FUNCTION = new UserDefinedFunction.UriFunction("URI"); - private static final TableInfo TABLE_INFO = TableInfo.builder(TABLE_ID, TABLE_SCHEMA) - .creationTime(CREATION_TIME) - .description(DESCRIPTION) - .etag(ETAG) - .id(ID) - .location(LOCATION) - .type(TableInfo.Type.TABLE) - .streamingBuffer(STREAMING_BUFFER) - .build(); + private static final TableInfo TABLE_INFO = + TableInfo.builder(TABLE_ID, TableType.table(TABLE_SCHEMA)) + .creationTime(CREATION_TIME) + .description(DESCRIPTION) + .etag(ETAG) + .id(ID) + .location(LOCATION) + .streamingBuffer(STREAMING_BUFFER) + .build(); @Test public void testServiceOptions() throws Exception { diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableInfoTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableInfoTest.java index 8fa84f766d17..ecde5f2c907c 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableInfoTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableInfoTest.java @@ -17,6 +17,7 @@ package com.google.gcloud.bigquery; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import com.google.common.collect.ImmutableList; import com.google.gcloud.bigquery.TableInfo.StreamingBuffer; @@ -68,40 +69,40 @@ public class TableInfoTest { private static final Long LAST_MODIFIED_TIME = 20L; private static final String LOCATION = "US"; private static final StreamingBuffer STREAMING_BUFFER = new StreamingBuffer(1L, 2L, 3L); - private static final TableInfo TABLE_INFO = TableInfo.builder(TABLE_ID, TABLE_SCHEMA) - .creationTime(CREATION_TIME) - .description(DESCRIPTION) - .etag(ETAG) - .expirationTime(EXPIRATION_TIME) - .friendlyName(FRIENDLY_NAME) - .id(ID) - .lastModifiedTime(LAST_MODIFIED_TIME) - .location(LOCATION) - .numBytes(NUM_BYTES) - .numRows(NUM_ROWS) - .selfLink(SELF_LINK) - .streamingBuffer(STREAMING_BUFFER) - .type(TableInfo.Type.TABLE) - .build(); - private static final TableInfo EXTERNAL_TABLE_INFO = TableInfo.builder(TABLE_ID, CONFIGURATION) - .creationTime(CREATION_TIME) - .description(DESCRIPTION) - .etag(ETAG) - .expirationTime(EXPIRATION_TIME) - .friendlyName(FRIENDLY_NAME) - .id(ID) - .lastModifiedTime(LAST_MODIFIED_TIME) - .location(LOCATION) - .numBytes(NUM_BYTES) - .numRows(NUM_ROWS) - .selfLink(SELF_LINK) - .streamingBuffer(STREAMING_BUFFER) - .type(TableInfo.Type.TABLE) - .build(); + private static final TableInfo TABLE_INFO = + TableInfo.builder(TABLE_ID, TableType.table(TABLE_SCHEMA)) + .creationTime(CREATION_TIME) + .description(DESCRIPTION) + .etag(ETAG) + .expirationTime(EXPIRATION_TIME) + .friendlyName(FRIENDLY_NAME) + .id(ID) + .lastModifiedTime(LAST_MODIFIED_TIME) + .location(LOCATION) + .numBytes(NUM_BYTES) + .numRows(NUM_ROWS) + .selfLink(SELF_LINK) + .streamingBuffer(STREAMING_BUFFER) + .build(); + private static final TableInfo EXTERNAL_TABLE_INFO = + TableInfo.builder(TABLE_ID, TableType.externalTable(CONFIGURATION)) + .creationTime(CREATION_TIME) + .description(DESCRIPTION) + .etag(ETAG) + .expirationTime(EXPIRATION_TIME) + .friendlyName(FRIENDLY_NAME) + .id(ID) + .lastModifiedTime(LAST_MODIFIED_TIME) + .location(LOCATION) + .numBytes(NUM_BYTES) + .numRows(NUM_ROWS) + .selfLink(SELF_LINK) + .streamingBuffer(STREAMING_BUFFER) + .build(); private static List USER_DEFINED_FUNCTIONS = ImmutableList.of( UserDefinedFunction.inline("Function"), UserDefinedFunction.fromUri("URI")); private static final TableInfo VIEW_INFO = - TableInfo.builder(TABLE_ID, VIEW_QUERY, USER_DEFINED_FUNCTIONS) + TableInfo.builder(TABLE_ID, TableType.view(VIEW_QUERY, USER_DEFINED_FUNCTIONS)) .creationTime(CREATION_TIME) .description(DESCRIPTION) .etag(ETAG) @@ -114,7 +115,6 @@ public class TableInfoTest { .numRows(NUM_ROWS) .selfLink(SELF_LINK) .streamingBuffer(STREAMING_BUFFER) - .type(TableInfo.Type.VIEW) .build(); @Test @@ -123,13 +123,10 @@ public void testToBuilder() { compareTableInfo(VIEW_INFO, VIEW_INFO.toBuilder().build()); compareTableInfo(EXTERNAL_TABLE_INFO, EXTERNAL_TABLE_INFO.toBuilder().build()); TableInfo tableInfo = TABLE_INFO.toBuilder() - .type(TableInfo.Type.VIEW) .description("newDescription") .build(); - assertEquals(TableInfo.Type.VIEW, tableInfo.type()); assertEquals("newDescription", tableInfo.description()); tableInfo = tableInfo.toBuilder() - .type(TableInfo.Type.TABLE) .description("description") .build(); compareTableInfo(TABLE_INFO, tableInfo); @@ -137,15 +134,14 @@ public void testToBuilder() { @Test public void testToBuilderIncomplete() { - TableInfo tableInfo = TableInfo.of(TABLE_ID, VIEW_QUERY); + TableInfo tableInfo = TableInfo.of(TABLE_ID, TableType.view(VIEW_QUERY)); assertEquals(tableInfo, tableInfo.toBuilder().build()); } @Test public void testBuilder() { assertEquals(TABLE_ID, TABLE_INFO.tableId()); + assertTrue(VIEW_INFO.type() instanceof TableType); assertEquals(TABLE_SCHEMA, TABLE_INFO.schema()); - assertEquals(null, TABLE_INFO.viewQuery()); - assertEquals(null, TABLE_INFO.externalConfiguration()); assertEquals(CREATION_TIME, TABLE_INFO.creationTime()); assertEquals(DESCRIPTION, TABLE_INFO.description()); assertEquals(ETAG, TABLE_INFO.etag()); @@ -158,11 +154,11 @@ public void testBuilder() { assertEquals(NUM_ROWS, TABLE_INFO.numRows()); assertEquals(SELF_LINK, TABLE_INFO.selfLink()); assertEquals(STREAMING_BUFFER, TABLE_INFO.streamingBuffer()); - assertEquals(TableInfo.Type.TABLE, TABLE_INFO.type()); + assertEquals(TableType.Type.TABLE, TABLE_INFO.type().type()); assertEquals(TABLE_ID, VIEW_INFO.tableId()); assertEquals(null, VIEW_INFO.schema()); - assertEquals(VIEW_QUERY, VIEW_INFO.viewQuery()); - assertEquals(null, VIEW_INFO.externalConfiguration()); + assertTrue(VIEW_INFO.type() instanceof TableType.View); + assertEquals(VIEW_QUERY, ((TableType.View) VIEW_INFO.type()).query()); assertEquals(CREATION_TIME, VIEW_INFO.creationTime()); assertEquals(DESCRIPTION, VIEW_INFO.description()); assertEquals(ETAG, VIEW_INFO.etag()); @@ -175,11 +171,12 @@ public void testBuilder() { assertEquals(NUM_ROWS, VIEW_INFO.numRows()); assertEquals(SELF_LINK, VIEW_INFO.selfLink()); assertEquals(STREAMING_BUFFER, VIEW_INFO.streamingBuffer()); - assertEquals(TableInfo.Type.VIEW, VIEW_INFO.type()); + assertEquals(TableType.Type.VIEW, VIEW_INFO.type().type()); assertEquals(TABLE_ID, EXTERNAL_TABLE_INFO.tableId()); assertEquals(null, EXTERNAL_TABLE_INFO.schema()); - assertEquals(null, EXTERNAL_TABLE_INFO.viewQuery()); - assertEquals(CONFIGURATION, EXTERNAL_TABLE_INFO.externalConfiguration()); + assertTrue(EXTERNAL_TABLE_INFO.type() instanceof TableType.ExternalTable); + assertEquals(CONFIGURATION, + ((TableType.ExternalTable) EXTERNAL_TABLE_INFO.type()).configuration()); assertEquals(CREATION_TIME, EXTERNAL_TABLE_INFO.creationTime()); assertEquals(DESCRIPTION, EXTERNAL_TABLE_INFO.description()); assertEquals(ETAG, EXTERNAL_TABLE_INFO.etag()); @@ -192,7 +189,7 @@ public void testBuilder() { assertEquals(NUM_ROWS, EXTERNAL_TABLE_INFO.numRows()); assertEquals(SELF_LINK, EXTERNAL_TABLE_INFO.selfLink()); assertEquals(STREAMING_BUFFER, EXTERNAL_TABLE_INFO.streamingBuffer()); - assertEquals(TableInfo.Type.TABLE, EXTERNAL_TABLE_INFO.type()); + assertEquals(TableType.Type.EXTERNAL, EXTERNAL_TABLE_INFO.type().type()); } @Test @@ -206,8 +203,7 @@ private void compareTableInfo(TableInfo expected, TableInfo value) { assertEquals(expected, value); assertEquals(expected.tableId(), value.tableId()); assertEquals(expected.schema(), value.schema()); - assertEquals(expected.viewQuery(), value.viewQuery()); - assertEquals(expected.externalConfiguration(), value.externalConfiguration()); + assertEquals(expected.type(), value.type()); assertEquals(expected.creationTime(), value.creationTime()); assertEquals(expected.description(), value.description()); assertEquals(expected.etag(), value.etag()); diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTypeTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTypeTest.java new file mode 100644 index 000000000000..6b67e8968e03 --- /dev/null +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTypeTest.java @@ -0,0 +1,126 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * 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.gcloud.bigquery; + +import com.google.common.collect.ImmutableList; + +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class TableTypeTest { + + private static final Field FIELD_SCHEMA1 = + Field.builder("StringField", Field.Type.string()) + .mode(Field.Mode.NULLABLE) + .description("FieldDescription1") + .build(); + private static final Field FIELD_SCHEMA2 = + Field.builder("IntegerField", Field.Type.integer()) + .mode(Field.Mode.REPEATED) + .description("FieldDescription2") + .build(); + private static final Field FIELD_SCHEMA3 = + Field.builder("RecordField", Field.Type.record(FIELD_SCHEMA1, FIELD_SCHEMA2)) + .mode(Field.Mode.REQUIRED) + .description("FieldDescription3") + .build(); + private static final Schema TABLE_SCHEMA = Schema.of(FIELD_SCHEMA1, FIELD_SCHEMA2, FIELD_SCHEMA3); + private static final String VIEW_QUERY = "VIEW QUERY"; + private static final ExternalDataConfiguration CONFIGURATION = ExternalDataConfiguration + .builder(ImmutableList.of("URI"), TABLE_SCHEMA, FormatOptions.datastoreBackup()) + .compression("ZIP") + .ignoreUnknownValues(true) + .maxBadRecords(42) + .build(); + private static List FUNCTIONS = ImmutableList.of( + UserDefinedFunction.inline("Function"), UserDefinedFunction.fromUri("URI")); + private static final TableType TABLE = TableType.table(TABLE_SCHEMA); + private static final TableType.View VIEW = TableType.view(VIEW_QUERY); + private static final TableType.View VIEW_WITH_FUNCTIONS = TableType.view(VIEW_QUERY, FUNCTIONS); + private static final TableType.ExternalTable EXTERNAL = TableType.externalTable(CONFIGURATION); + + @Test + public void testConstructor() { + TableType table = new TableType(TableType.Type.TABLE, TABLE_SCHEMA); + assertEquals(TableType.Type.TABLE, table.type()); + assertEquals(TABLE_SCHEMA, table.schema()); + TableType.View view = new TableType.View(VIEW_QUERY); + assertEquals(TableType.Type.VIEW, view.type()); + assertEquals(null, view.schema()); + assertEquals(VIEW_QUERY, view.query()); + assertEquals(null, view.userDefinedFunctions()); + view = new TableType.View(VIEW_QUERY, FUNCTIONS); + assertEquals(TableType.Type.VIEW, view.type()); + assertEquals(null, view.schema()); + assertEquals(VIEW_QUERY, view.query()); + assertEquals(FUNCTIONS, view.userDefinedFunctions()); + view = new TableType.View(TABLE_SCHEMA, VIEW_QUERY, FUNCTIONS); + assertEquals(TableType.Type.VIEW, view.type()); + assertEquals(TABLE_SCHEMA, view.schema()); + assertEquals(VIEW_QUERY, view.query()); + assertEquals(FUNCTIONS, view.userDefinedFunctions()); + TableType.ExternalTable extern = new TableType.ExternalTable(CONFIGURATION); + assertEquals(TableType.Type.EXTERNAL, extern.type()); + assertEquals(null, extern.schema()); + assertEquals(CONFIGURATION, extern.configuration()); + extern = new TableType.ExternalTable(TABLE_SCHEMA, CONFIGURATION); + assertEquals(TableType.Type.EXTERNAL, extern.type()); + assertEquals(TABLE_SCHEMA, extern.schema()); + assertEquals(CONFIGURATION, extern.configuration()); + } + + @Test + public void testFactoryMethods() { + TableType table = TableType.table(TABLE_SCHEMA); + assertEquals(TableType.Type.TABLE, table.type()); + assertEquals(TABLE_SCHEMA, table.schema()); + TableType.View view = TableType.view(VIEW_QUERY); + assertEquals(TableType.Type.VIEW, view.type()); + assertEquals(null, view.schema()); + assertEquals(VIEW_QUERY, view.query()); + assertEquals(null, view.userDefinedFunctions()); + view = TableType.view(VIEW_QUERY, FUNCTIONS); + assertEquals(TableType.Type.VIEW, view.type()); + assertEquals(null, view.schema()); + assertEquals(VIEW_QUERY, view.query()); + assertEquals(FUNCTIONS, view.userDefinedFunctions()); + TableType.ExternalTable extern = TableType.externalTable(CONFIGURATION); + assertEquals(TableType.Type.EXTERNAL, extern.type()); + assertEquals(null, extern.schema()); + assertEquals(CONFIGURATION, extern.configuration()); + } + + @Test + public void testEquals() { + assertEquals(new TableType(TableType.Type.TABLE, TABLE_SCHEMA), TableType.table(TABLE_SCHEMA)); + assertEquals(new TableType.View(VIEW_QUERY), TableType.view(VIEW_QUERY)); + assertEquals(new TableType.View(VIEW_QUERY, FUNCTIONS), TableType.view(VIEW_QUERY, FUNCTIONS)); + assertEquals(new TableType.ExternalTable(CONFIGURATION), + TableType.externalTable(CONFIGURATION)); + assertEquals(new TableType(TableType.Type.TABLE, TABLE_SCHEMA).hashCode(), + TableType.table(TABLE_SCHEMA).hashCode()); + assertEquals(new TableType.View(VIEW_QUERY).hashCode(), + TableType.view(VIEW_QUERY).hashCode()); + assertEquals(new TableType.View(VIEW_QUERY, FUNCTIONS).hashCode(), + TableType.view(VIEW_QUERY, FUNCTIONS).hashCode()); + assertEquals(new TableType.ExternalTable(CONFIGURATION).hashCode(), + TableType.externalTable(CONFIGURATION).hashCode()); + } +}