From ff30d469a8dcfd10ed1eed5f63b2b00169c882d6 Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Tue, 25 Jun 2019 15:17:21 -0700 Subject: [PATCH 01/16] bigquery: add routines support --- .../google-cloud-bigquery/pom.xml | 2 +- .../com/google/cloud/bigquery/Routine.java | 4 + .../com/google/cloud/bigquery/RoutineId.java | 108 +++++++++++++ .../google/cloud/bigquery/RoutineInfo.java | 0 .../cloud/bigquery/StandardSQLDataType.java | 148 ++++++++++++++++++ .../cloud/bigquery/StandardSQLField.java | 144 +++++++++++++++++ .../cloud/bigquery/StandardSQLStructType.java | 124 +++++++++++++++ .../google/cloud/bigquery/RoutineIdTest.java | 63 ++++++++ .../cloud/bigquery/StandardSQLFieldTest.java | 72 +++++++++ .../bigquery/StandardSQLStructTypeTest.java | 67 ++++++++ 10 files changed, 731 insertions(+), 1 deletion(-) create mode 100644 google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java create mode 100644 google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineId.java create mode 100644 google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java create mode 100644 google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java create mode 100644 google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java create mode 100644 google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLStructType.java create mode 100644 google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineIdTest.java create mode 100644 google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLFieldTest.java create mode 100644 google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLStructTypeTest.java diff --git a/google-cloud-clients/google-cloud-bigquery/pom.xml b/google-cloud-clients/google-cloud-bigquery/pom.xml index 8ba1725869b1..97b0d8960b74 100644 --- a/google-cloud-clients/google-cloud-bigquery/pom.xml +++ b/google-cloud-clients/google-cloud-bigquery/pom.xml @@ -12,7 +12,7 @@ com.google.cloud google-cloud-clients - 0.97.1-alpha-SNAPSHOT + 0.97.0-alpha google-cloud-bigquery diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java new file mode 100644 index 000000000000..7dee8cabcf06 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java @@ -0,0 +1,4 @@ +package com.google.cloud.bigquery; + +public class Routine { +} diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineId.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineId.java new file mode 100644 index 000000000000..5c885fde4307 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineId.java @@ -0,0 +1,108 @@ +/* + * Copyright 2019 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 com.google.common.base.Preconditions.checkNotNull; + + import com.google.api.services.bigquery.model.RoutineReference; + import com.google.common.base.Function; + import com.google.common.base.Preconditions; + import com.google.common.base.Strings; + import java.io.Serializable; + import java.util.Objects; + +public final class RoutineId implements Serializable { + + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public RoutineId apply(RoutineReference pb) { + return RoutineId.fromPb(pb); + } + }; + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public RoutineReference apply(RoutineId routineId) { + return routineId.toPb(); + } + }; + + private final String project; + private final String dataset; + private final String routine; + + /** Return corresponding project ID for this routine. * */ + public String getProject() { + return project; + } + + /** Return corresponding dataset ID for this routine. * */ + public String getDataset() { + return dataset; + } + + /** Return corresponding routine ID for this routine. * */ + public String getRoutine() { + return routine; + } + + private RoutineId(String project, String dataset, String routine) { + this.project = project; + this.dataset = dataset; + this.routine = routine; + } + + /** Creates a routine identity given project, dataset, and routine identifiers. * */ + public static RoutineId of(String project, String dataset, String routine) { + return new RoutineId(checkNotNull(project), checkNotNull(dataset), checkNotNull(routine)); + } + + /** Creates a routine identity given dataset and routine identifiers. * */ + public static RoutineId of(String dataset, String routine) { + return new RoutineId(null, checkNotNull(dataset), checkNotNull(routine)); + } + + @Override + public boolean equals(Object obj) { + return obj == this || obj instanceof RoutineId && Objects.equals(toPb(), ((RoutineId) obj).toPb()); + } + + @Override + public int hashCode() { + return Objects.hash(project, dataset, routine); + } + + @Override + public String toString() { + return toPb().toString(); + } + + RoutineId setProjectId(String projectId) { + Preconditions.checkArgument( + !Strings.isNullOrEmpty(projectId), "Provided projectId is null or empty"); + return RoutineId.of(projectId, getDataset(), getRoutine()); + } + + RoutineReference toPb() { + return new RoutineReference().setProjectId(project).setDatasetId(dataset).setRoutineId(routine); + } + + static RoutineId fromPb(RoutineReference routineRef) { + return new RoutineId(routineRef.getProjectId(), routineRef.getDatasetId(), routineRef.getRoutineId()); + } +} diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java new file mode 100644 index 000000000000..6a9f638665f9 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java @@ -0,0 +1,148 @@ +/* + * Copyright 2019 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.services.bigquery.model.StandardSqlDataType; +import com.google.common.base.MoreObjects; + +import java.io.Serializable; +import java.util.Objects; + +public class StandardSQLDataType implements Serializable { + + private final StandardSQLDataType arrayElementType; + private final StandardSQLStructType structType; + private final String typeKind; + + public abstract static class Builder { + /** Sets the type of an array's elements, when the TypeKind is + * ARRAY. + */ + public abstract Builder setArrayElementType(StandardSQLDataType arrayElementType); + + /** Sets the struct type definition (list of fields) when the + * TypeKind is STRUCT. + */ + public abstract Builder setStructType(StandardSQLStructType structType); + + /** Sets the top-level type of this data type. Can be + * any standard SQL data type. For more information, see + * https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types + */ + public abstract Builder setTypeKind(String typeKind); + + /** Creates a {@code StandardSQLDataType} object. */ + public abstract StandardSQLDataType build(); + + } + + static class BuilderImpl extends Builder { + + private String typeKind; + private StandardSQLDataType arrayElementType; + private StandardSQLStructType structType; + + BuilderImpl() {} + + BuilderImpl(StandardSQLDataType dataType) { + this.arrayElementType = dataType.arrayElementType; + this.structType = dataType.structType; + this.typeKind = dataType.typeKind; + } + + BuilderImpl(StandardSqlDataType dataTypePb) { + this.arrayElementType = StandardSQLDataType.fromPb(dataTypePb.getArrayElementType()); + this.structType = StandardSQLStructType.fromPb(dataTypePb.getStructType()); + this.typeKind = dataTypePb.getTypeKind(); + } + + @Override + public Builder setArrayElementType(StandardSQLDataType arrayElementType) { + this.arrayElementType = arrayElementType; + return this; + } + + @Override + public Builder setStructType(StandardSQLStructType structType) { + this.structType = structType; + return this; + } + + @Override + public Builder setTypeKind(String typeKind) { + this.typeKind = typeKind; + return this; + } + + @Override + public StandardSQLDataType build() { + return new StandardSQLDataType(this); + } + } + + StandardSQLDataType(BuilderImpl builder) { + this.arrayElementType = builder.arrayElementType; + this.structType = builder.structType; + this.typeKind = builder.typeKind; + } + + public Builder toBuilder() { + return new BuilderImpl(this); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("arrayElementType", arrayElementType) + .add("structType", structType) + .add("typeKind", typeKind) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash( + typeKind, + arrayElementType, + structType + ); + } + + @Override + public boolean equals(Object obj) { + return obj == this + || obj != null + && obj.getClass().equals(StandardSQLDataType.class) + && Objects.equals(toPb(), ((StandardSQLDataType) obj).toPb()); + } + + public static Builder newBuilder(String typeKind) { + return new BuilderImpl().setTypeKind(typeKind); + } + + StandardSqlDataType toPb() { + StandardSqlDataType dataTypePb = new StandardSqlDataType(); + dataTypePb.setArrayElementType(arrayElementType.toPb()); + dataTypePb.setStructType(structType.toPb()); + dataTypePb.setTypeKind(typeKind); + return dataTypePb; + } + + static StandardSQLDataType fromPb(StandardSqlDataType dataTypePb) { + return new BuilderImpl(dataTypePb).build(); + } + +} diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java new file mode 100644 index 000000000000..1c1117ef29d3 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java @@ -0,0 +1,144 @@ +/* + * Copyright 2019 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.services.bigquery.model.StandardSqlField; +import com.google.common.base.Function; +import com.google.common.base.MoreObjects; + +import java.io.Serializable; +import java.util.Objects; + +public class StandardSQLField implements Serializable { + + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public StandardSQLField apply(StandardSqlField pb) { + return StandardSQLField.fromPb(pb); + } + }; + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public StandardSqlField apply(StandardSQLField field) { + return field.toPb(); + } + }; + + private final String name; + private final StandardSQLDataType dataType; + + public abstract static class Builder { + + public abstract Builder setName(String name); + + public abstract Builder setDataType(StandardSQLDataType dataType); + + public abstract StandardSQLField build(); + } + + static class BuilderImpl extends Builder { + + private String name; + private StandardSQLDataType dataType; + + BuilderImpl() {} + + BuilderImpl(StandardSQLField field) { + this.name = field.name; + this.dataType = field.dataType; + } + + BuilderImpl(StandardSqlField fieldPb) { + this.name = fieldPb.getName(); + this.dataType = StandardSQLDataType.fromPb(fieldPb.getType()); + } + + @Override + public Builder setName(String name) { + this.name = name; + return this; + } + + @Override + public Builder setDataType(StandardSQLDataType dataType) { + this.dataType = dataType; + return this; + } + + @Override + public StandardSQLField build() { + return new StandardSQLField(this); + } + } + + StandardSQLField(BuilderImpl builder) { + this.name = builder.name; + this.dataType = builder.dataType; + } + + public String getName() { return name; } + + public StandardSQLDataType getDataType() { return dataType; } + + public Builder toBuilder() { return new BuilderImpl(this); } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("name", name) + .add("dataType", dataType) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash( + name, + dataType + ); + } + + @Override + public boolean equals(Object obj) { + return obj == this + || obj != null + && obj.getClass().equals(StandardSQLField.class) + && Objects.equals(toPb(), ((StandardSQLField) obj).toPb()); + } + + public static Builder newBuilder(String name, StandardSQLDataType dataType) { + return new BuilderImpl().setDataType(dataType).setName(name); + } + + public static Builder newBuilder(StandardSQLDataType dataType) { + return new BuilderImpl().setDataType(dataType); + } + + public StandardSqlField toPb() { + StandardSqlField fieldPb = new StandardSqlField(); + fieldPb.setName(name); + fieldPb.setType(dataType.toPb()); + return fieldPb; + } + + static StandardSQLField fromPb(StandardSqlField fieldPb) { + return new BuilderImpl(fieldPb).build(); + } + + +} diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLStructType.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLStructType.java new file mode 100644 index 000000000000..603fc1d004a6 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLStructType.java @@ -0,0 +1,124 @@ +/* + * Copyright 2019 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.services.bigquery.model.StandardSqlStructType; +import com.google.common.collect.Lists; + +import java.io.Serializable; +import java.util.Collections; +import java.util.List; +import java.util.Objects; + +import static com.google.common.base.Preconditions.checkNotNull; + +public final class StandardSQLStructType implements Serializable { + + private final List fieldList; + + private StandardSQLStructType(BuilderImpl builder) { + this.fieldList = builder.fieldList; + } + + static StandardSQLStructType fromPb(com.google.api.services.bigquery.model.StandardSqlStructType structTypePb) { + Builder builder = new StandardSQLStructType.BuilderImpl(); + if (structTypePb.getFields() == null) { + builder.setFields(Collections.emptyList()); + } else { + builder.setFields(Lists.transform(structTypePb.getFields(), StandardSQLField.FROM_PB_FUNCTION)); + } + return builder.build(); + } + + public List getFields() { + return fieldList; + } + + @Override + public boolean equals(Object obj) { + return obj == this || obj instanceof StandardSQLStructType && Objects.equals(toPb(), ((StandardSQLStructType) obj).toPb()); + } + + @Override + public int hashCode() { + return Objects.hash(fieldList); + } + + /** + * Returns a builder for the StandardSQLStructType object. + */ + public Builder toBuilder() { + return new BuilderImpl(this); + } + + @Override + public String toString() { + return toPb().toString(); + } + + public static Builder newBuilder(List fieldList) { + return new BuilderImpl().setFields(fieldList); + } + + StandardSqlStructType toPb() { + StandardSqlStructType structTypePb = new StandardSqlStructType(); + if (fieldList != null) { + structTypePb.setFields(Lists.transform(fieldList, StandardSQLField.TO_PB_FUNCTION)); + } + return structTypePb; + } + + public abstract static class Builder { + + public abstract Builder setFields(List fields); + + public abstract StandardSQLStructType build(); + + } + + static class BuilderImpl extends Builder { + + private List fieldList; + + BuilderImpl() { + } + + BuilderImpl(StandardSQLStructType structType) { + this.fieldList = structType.fieldList; + } + + BuilderImpl(StandardSqlStructType structTypePb) { + if (structTypePb.getFields() == null) { + this.fieldList = Collections.emptyList(); + } + this.fieldList = Lists.transform(structTypePb.getFields(), StandardSQLField.FROM_PB_FUNCTION); + } + + @Override + public Builder setFields(List fieldList) { + this.fieldList = checkNotNull(fieldList); + return this; + } + + @Override + public StandardSQLStructType build() { + return new StandardSQLStructType(this); + } + + } + +} diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineIdTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineIdTest.java new file mode 100644 index 000000000000..78be7aef1916 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineIdTest.java @@ -0,0 +1,63 @@ +/* + * Copyright 2019 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 org.junit.Test; + +public class RoutineIdTest { + + public static final RoutineId ROUTINE = RoutineId.of("dataset", "routine"); + public static final RoutineId ROUTINE_COMPLETE = RoutineId.of("project", "dataset", "routine"); + + @Test + public void testOf() { + assertEquals(null, ROUTINE.getProject()); + assertEquals("dataset", ROUTINE.getDataset()); + assertEquals("routine", ROUTINE.getRoutine()); + + assertEquals("project", ROUTINE_COMPLETE.getProject()); + assertEquals("dataset", ROUTINE_COMPLETE.getDataset()); + assertEquals("routine",ROUTINE_COMPLETE.getRoutine()); + } + + @Test + public void testEquals() { + compareRoutineIds(ROUTINE, RoutineId.of("dataset", "routine")); + compareRoutineIds(ROUTINE_COMPLETE, RoutineId.of("project", "dataset", "routine")); + } + + @Test + public void testToPbAndFromPb() { + compareRoutineIds(ROUTINE, RoutineId.fromPb(ROUTINE.toPb())); + compareRoutineIds(ROUTINE_COMPLETE, RoutineId.fromPb(ROUTINE_COMPLETE.toPb())); + } + + @Test + public void testSetProjectId() { + RoutineId differentProjectTable = RoutineId.of("differentProject", "dataset", "routine"); + assertEquals(differentProjectTable, ROUTINE.setProjectId("differentProject")); + } + + private void compareRoutineIds(RoutineId expected, RoutineId value) { + assertEquals(expected, value); + assertEquals(expected.getProject(), value.getProject()); + assertEquals(expected.getDataset(), value.getDataset()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLFieldTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLFieldTest.java new file mode 100644 index 000000000000..ca2a2e6e9f48 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLFieldTest.java @@ -0,0 +1,72 @@ +/* + * Copyright 2019 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.assertNull; + +import com.google.api.services.bigquery.model.TrainingOptions; +import com.google.api.services.bigquery.model.TrainingRun; +import java.util.Arrays; +import java.util.List; +import org.junit.Test; + +public class StandardSQLFieldTest { + + + private static final String NAME = "field_name"; + private static final StandardSQLDataType STRING_DATA_TYPE = StandardSQLDataType + .newBuilder("STRING") + .build(); + private static final StandardSQLDataType ARRAY_OF_STRING_DATA_TYPE = StandardSQLDataType + .newBuilder("ARRAY") + .setArrayElementType(STRING_DATA_TYPE) + .build(); + private static final StandardSQLField STANDARD_SQL_FIELD_1 = StandardSQLField + .newBuilder(NAME, STRING_DATA_TYPE) + .build(); + private static final StandardSQLField STANDARD_SQL_FIELD_2 = StandardSQLField + .newBuilder(NAME, ARRAY_OF_STRING_DATA_TYPE) + .build(); + + + + @Test + public void testToBuilder() { + compareStandardSQLField(STANDARD_SQL_FIELD_1, STANDARD_SQL_FIELD_1.toBuilder().build()); + compareStandardSQLField(STANDARD_SQL_FIELD_2, STANDARD_SQL_FIELD_2.toBuilder().build()); + + } + + @Test + public void testBuilder() { + assertEquals(NAME, STANDARD_SQL_FIELD_1.getName()); + assertEquals(STRING_DATA_TYPE, STANDARD_SQL_FIELD_1.getDataType()); + assertEquals(ARRAY_OF_STRING_DATA_TYPE, STANDARD_SQL_FIELD_2.getDataType()); + } + + @Test + public void testToAndFromPb() { + compareStandardSQLField(STANDARD_SQL_FIELD_1, StandardSQLField.fromPb(STANDARD_SQL_FIELD_1.toPb())); + } + + private void compareStandardSQLField(StandardSQLField expected, StandardSQLField value) { + assertEquals(expected, value); + assertEquals(expected.getName(), value.getName()); + assertEquals(expected.getDataType(), value.getDataType()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLStructTypeTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLStructTypeTest.java new file mode 100644 index 000000000000..f421d55d9d41 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLStructTypeTest.java @@ -0,0 +1,67 @@ +/* + * Copyright 2019 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.assertNull; + + +import java.util.Arrays; +import java.util.List; + +import com.google.common.collect.ImmutableList; +import org.junit.Test; + +public class StandardSQLStructTypeTest { + + + private static final StandardSQLField FIELD_1 = StandardSQLField + .newBuilder("FIELD_1", + StandardSQLDataType.newBuilder("STRING").build()) + .build(); + private static final StandardSQLField FIELD_2 = StandardSQLField + .newBuilder("FIELD_2", + StandardSQLDataType.newBuilder("FLOAT64").build()) + .build(); + + private static final List FIELD_LIST = ImmutableList.of(FIELD_1, FIELD_2); + private static final StandardSQLStructType STRUCT_TYPE = StandardSQLStructType.newBuilder(FIELD_LIST).build(); + + + + + @Test + public void testToBuilder() { + compareStandardSQLStructType(STRUCT_TYPE, STRUCT_TYPE.toBuilder().build()); + } + + @Test + public void testBuilder() { + assertEquals(FIELD_1, STRUCT_TYPE.getFields().get(0)); + assertEquals(FIELD_2, STRUCT_TYPE.getFields().get(1)); + } + + @Test + public void testToAndFromPb() { + compareStandardSQLStructType(STRUCT_TYPE, StandardSQLStructType.fromPb(STRUCT_TYPE.toPb())); + } + + private void compareStandardSQLStructType(StandardSQLStructType expected, StandardSQLStructType value) { + assertEquals(expected, value); + assertEquals(expected.getFields(), value.getFields()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} From 7a4c364ed010d4fc31f614c194bd2ceaae1b6ed8 Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Wed, 26 Jun 2019 10:24:46 -0700 Subject: [PATCH 02/16] working testing for basic standardsql types --- .../cloud/bigquery/StandardSQLDataType.java | 22 +++++- .../cloud/bigquery/StandardSQLField.java | 4 +- .../bigquery/StandardSQLDataTypeTest.java | 74 +++++++++++++++++++ .../cloud/bigquery/StandardSQLFieldTest.java | 4 +- 4 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLDataTypeTest.java diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java index 6a9f638665f9..3f7e24a6a43c 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java @@ -64,9 +64,13 @@ static class BuilderImpl extends Builder { } BuilderImpl(StandardSqlDataType dataTypePb) { - this.arrayElementType = StandardSQLDataType.fromPb(dataTypePb.getArrayElementType()); - this.structType = StandardSQLStructType.fromPb(dataTypePb.getStructType()); this.typeKind = dataTypePb.getTypeKind(); + if (this.arrayElementType != null) { + this.arrayElementType = StandardSQLDataType.fromPb(dataTypePb.getArrayElementType()); + } + if (this.structType != null) { + this.structType = StandardSQLStructType.fromPb(dataTypePb.getStructType()); + } } @Override @@ -99,6 +103,12 @@ public StandardSQLDataType build() { this.typeKind = builder.typeKind; } + public String getTypeKind() { return typeKind; } + + public StandardSQLDataType getArrayElementType() { return arrayElementType; } + + public StandardSQLStructType getStructType() { return structType; } + public Builder toBuilder() { return new BuilderImpl(this); } @@ -135,9 +145,13 @@ public static Builder newBuilder(String typeKind) { StandardSqlDataType toPb() { StandardSqlDataType dataTypePb = new StandardSqlDataType(); - dataTypePb.setArrayElementType(arrayElementType.toPb()); - dataTypePb.setStructType(structType.toPb()); dataTypePb.setTypeKind(typeKind); + if (arrayElementType != null) { + dataTypePb.setArrayElementType(arrayElementType.toPb()); + } + if (structType != null) { + dataTypePb.setStructType(structType.toPb()); + } return dataTypePb; } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java index 1c1117ef29d3..b325ac41d520 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java @@ -132,7 +132,9 @@ public static Builder newBuilder(StandardSQLDataType dataType) { public StandardSqlField toPb() { StandardSqlField fieldPb = new StandardSqlField(); fieldPb.setName(name); - fieldPb.setType(dataType.toPb()); + if (dataType != null) { + fieldPb.setType(dataType.toPb()); + } return fieldPb; } diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLDataTypeTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLDataTypeTest.java new file mode 100644 index 000000000000..3501d992ceed --- /dev/null +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLDataTypeTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2019 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.common.collect.ImmutableList; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class StandardSQLDataTypeTest { + private static final String STRING_TYPEKIND="STRING"; + private static final String ARRAY_TYPEKIND="ARRAY"; + private static final String STRUCT_TYPEKIND="STRUCT"; + + + + private static final StandardSQLDataType STRING_DATA_TYPE = StandardSQLDataType.newBuilder(STRING_TYPEKIND).build(); + private static final StandardSQLDataType ARRAY_OF_STRING_DATA_TYPE = StandardSQLDataType.newBuilder(ARRAY_TYPEKIND).setArrayElementType(STRING_DATA_TYPE).build(); + + private static final List FIELD_LIST = ImmutableList.of( + StandardSQLField.newBuilder(STRING_DATA_TYPE).build(), + StandardSQLField.newBuilder(ARRAY_OF_STRING_DATA_TYPE).build() + ); + private static final StandardSQLStructType STRUCT_TYPE = StandardSQLStructType.newBuilder(FIELD_LIST).build(); + + private static final StandardSQLDataType STRUCT_DATA_TYPE = StandardSQLDataType.newBuilder(STRUCT_TYPEKIND).setStructType(STRUCT_TYPE).build(); + + + + @Test + public void testToBuilder() { + compareStandardSQLDataType(STRING_DATA_TYPE,STRING_DATA_TYPE.toBuilder().build()); + compareStandardSQLDataType(ARRAY_OF_STRING_DATA_TYPE,ARRAY_OF_STRING_DATA_TYPE.toBuilder().build()); + compareStandardSQLDataType(STRUCT_DATA_TYPE,STRUCT_DATA_TYPE.toBuilder().build()); + + + } + + @Test + public void testBuilder() { + assertEquals(STRING_TYPEKIND, STRING_DATA_TYPE.getTypeKind()); + assertEquals(ARRAY_TYPEKIND, ARRAY_OF_STRING_DATA_TYPE.getTypeKind()); + assertEquals(STRING_DATA_TYPE, ARRAY_OF_STRING_DATA_TYPE.getArrayElementType()); + assertEquals(STRUCT_TYPE, STRUCT_DATA_TYPE.getStructType()); + } + + @Test + public void testToAndFromPb() { + + } + + private void compareStandardSQLDataType(StandardSQLDataType expected, StandardSQLDataType value) { + assertEquals(expected, value); + assertEquals(expected.getTypeKind(), value.getTypeKind()); + assertEquals(expected.getArrayElementType(), value.getArrayElementType()); + assertEquals(expected.getStructType(), value.getStructType()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLFieldTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLFieldTest.java index ca2a2e6e9f48..d8215e58710b 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLFieldTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLFieldTest.java @@ -36,7 +36,7 @@ public class StandardSQLFieldTest { .setArrayElementType(STRING_DATA_TYPE) .build(); private static final StandardSQLField STANDARD_SQL_FIELD_1 = StandardSQLField - .newBuilder(NAME, STRING_DATA_TYPE) + .newBuilder(STRING_DATA_TYPE) .build(); private static final StandardSQLField STANDARD_SQL_FIELD_2 = StandardSQLField .newBuilder(NAME, ARRAY_OF_STRING_DATA_TYPE) @@ -53,7 +53,7 @@ public void testToBuilder() { @Test public void testBuilder() { - assertEquals(NAME, STANDARD_SQL_FIELD_1.getName()); + assertEquals(null, STANDARD_SQL_FIELD_1.getName()); assertEquals(STRING_DATA_TYPE, STANDARD_SQL_FIELD_1.getDataType()); assertEquals(ARRAY_OF_STRING_DATA_TYPE, STANDARD_SQL_FIELD_2.getDataType()); } From e5f492d382167262360769e73a09fd719f357ee8 Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Wed, 26 Jun 2019 14:25:41 -0700 Subject: [PATCH 03/16] RoutineArgument and test --- .../cloud/bigquery/RoutineArgument.java | 91 ++++++++++++++ .../google/cloud/bigquery/RoutineInfo.java | 115 ++++++++++++++++++ .../cloud/bigquery/RoutineArgumentTest.java | 61 ++++++++++ 3 files changed, 267 insertions(+) create mode 100644 google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineArgument.java create mode 100644 google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineArgumentTest.java diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineArgument.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineArgument.java new file mode 100644 index 000000000000..08480e243d9c --- /dev/null +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineArgument.java @@ -0,0 +1,91 @@ +/* + * Copyright 2019 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.services.bigquery.model.Argument; +import com.google.auto.value.AutoValue; +import com.google.common.base.Function; + +import javax.annotation.Nullable; +import java.io.Serializable; +import java.util.Objects; + +@AutoValue +public abstract class RoutineArgument { + + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public RoutineArgument apply(Argument pb) { + return RoutineArgument.fromPb(pb); + } + }; + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public Argument apply(RoutineArgument argument) { + return argument.toPb(); + } + }; + + @AutoValue.Builder + public abstract static class Builder { + public abstract Builder setName(String name); + public abstract Builder setKind(String kind); + public abstract Builder setMode(String mode); + public abstract Builder setDataType(StandardSQLDataType dataType); + public abstract RoutineArgument build(); + } + + @Nullable + public abstract String getName(); + + @Nullable + public abstract String getKind(); + + @Nullable + public abstract String getMode(); + + @Nullable + public abstract StandardSQLDataType getDataType(); + + public abstract Builder toBuilder(); + + static Builder newBuilder() { return new AutoValue_RoutineArgument.Builder(); } + + Argument toPb() { + Argument argumentPb = new Argument() + .setName(getName()) + .setArgumentKind(getKind()) + .setMode(getMode()); + if (getDataType() != null) { + argumentPb.setDataType(getDataType().toPb()); + } + return argumentPb; + } + + static RoutineArgument fromPb(Argument argumentPb) { + Builder builder = newBuilder(); + builder.setName(argumentPb.getName()); + builder.setKind(argumentPb.getArgumentKind()); + builder.setMode(argumentPb.getMode()); + if (argumentPb.getDataType() != null) { + builder.setDataType(StandardSQLDataType.fromPb(argumentPb.getDataType())); + } + return builder.build(); + } + +} diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java index e69de29bb2d1..001dd32c1125 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java @@ -0,0 +1,115 @@ +/* + * Copyright 2019 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.services.bigquery.model.Routine; +import com.google.auto.value.AutoValue; +import com.google.common.base.Function; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; + +import javax.annotation.Nullable; +import java.util.List; + +@AutoValue +public abstract class RoutineInfo { + + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public RoutineInfo apply(Routine pb) { + return RoutineInfo.fromPb(pb); + } + }; + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public Routine apply(RoutineInfo routineInfo) { + return routineInfo.toPb(); + } + }; + + + @AutoValue.Builder + public abstract static class Builder { + abstract Builder setEtag(String etag); + public abstract Builder setRoutineType(String routineType); + abstract Builder setCreationTime(Long creationMillis); + abstract Builder setLastModifiedTime(Long lastModifiedMillis); + public abstract Builder setArguments(List arguments); + public abstract Builder setReturnType(StandardSQLDataType returnType); + public abstract Builder setImportedLibraries(List libraries); + public abstract Builder setBody(String body); + public abstract RoutineInfo build(); + } + + @Nullable + public abstract String getEtag(); + + @Nullable + public abstract String getRoutineType(); + + @Nullable + public abstract Long getCreationTime(); + + @Nullable + public abstract Long getLastModifiedTime(); + + public abstract List getArguments(); + + public abstract StandardSQLDataType getReturnType(); + + public abstract List getImportedLibraries(); + + @Nullable + public abstract String getBody(); + + public abstract Builder toBuilder(); + + static Builder newBuilder() { return new AutoValue_RoutineInfo.Builder(); } + + Routine toPb() { + Routine routinePb = new Routine() + .setEtag(getEtag()) + .setRoutineType(getRoutineType()) + .setCreationTime(getCreationTime()) + .setLastModifiedTime(getLastModifiedTime()); + if (getArguments() != null) { + routinePb.setArguments(Lists.transform(getArguments(), RoutineArgument.TO_PB_FUNCTION)); + } + return routinePb; + } + + static RoutineInfo fromPb(Routine routinePb) { + Builder builder = newBuilder(); + builder.setEtag(routinePb.getEtag()); + builder.setRoutineType(routinePb.getRoutineType()); + builder.setCreationTime(routinePb.getCreationTime()); + builder.setLastModifiedTime(routinePb.getLastModifiedTime()); + if (routinePb.getArguments() != null) { + builder.setArguments(Lists.transform(routinePb.getArguments(), RoutineArgument.FROM_PB_FUNCTION)); + } + if (routinePb.getReturnType() != null) { + builder.setReturnType(StandardSQLDataType.fromPb(routinePb.getReturnType())); + } + if (routinePb.getImportedLibraries() != null) { + builder.setImportedLibraries(ImmutableList.copyOf(routinePb.getImportedLibraries())); + } + builder.setBody(routinePb.getDefinitionBody()); + return builder.build(); + } + +} \ No newline at end of file diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineArgumentTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineArgumentTest.java new file mode 100644 index 000000000000..65a31cf49e90 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineArgumentTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 2019 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 org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class RoutineArgumentTest { + + private static final String NAME = "foo"; + private static final String KIND = "SCALAR_FUNCTION"; + private static final String MODE = "IN"; + private static final StandardSQLDataType DATA_TYPE = StandardSQLDataType.newBuilder("STRING").build(); + private static final RoutineArgument ARGUMENT = RoutineArgument.newBuilder() + .setName(NAME) + .setKind(KIND) + .setMode(MODE) + .setDataType(DATA_TYPE) + .build(); + + + @Test + public void testToBuilder() { + compareRoutineArguments(ARGUMENT, ARGUMENT.toBuilder().build()); + } + + @Test + public void testBuilder() { + assertEquals(NAME, ARGUMENT.getName()); + assertEquals(KIND, ARGUMENT.getKind()); + assertEquals(MODE, ARGUMENT.getMode()); + assertEquals(DATA_TYPE, ARGUMENT.getDataType()); + } + @Test + public void testToPbAndFromPb() { + compareRoutineArguments(ARGUMENT, RoutineArgument.fromPb(ARGUMENT.toPb())); + } + + public void compareRoutineArguments(RoutineArgument expected, RoutineArgument value) { + assertEquals(expected, value); + assertEquals(expected.getName(), value.getName()); + assertEquals(expected.getKind(), value.getKind()); + assertEquals(expected.getMode(), value.getMode()); + assertEquals(expected.getDataType(), value.getDataType()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} From 3dde407b2a59813e6993a6a7b99a3318dd58371a Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Wed, 26 Jun 2019 16:32:15 -0700 Subject: [PATCH 04/16] plumbing plumbing plumbing --- .../com/google/cloud/bigquery/BigQuery.java | 95 ++++++++- .../com/google/cloud/bigquery/Routine.java | 153 +++++++++++++- .../google/cloud/bigquery/RoutineInfo.java | 186 ++++++++++++++---- .../cloud/bigquery/spi/v2/BigQueryRpc.java | 13 ++ .../bigquery/spi/v2/HttpBigQueryRpc.java | 66 +++++++ 5 files changed, 476 insertions(+), 37 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java index a566fbeb8421..ce65b0a69ea4 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java @@ -151,6 +151,33 @@ public String getSelector() { } } + /** + * Fields of a BigQuery Routine resource. + * + * @see Routine + * Resource + */ + enum RoutineField implements FieldSelector { + CREATION_TIME("creationTime"), + ETAG("etag"), + LAST_MODIFIED_TIME("lastModifiedTime"), + ROUTINE_REFERENCE("routineReference"), + TYPE("modelType"); + + static final List REQUIRED_FIELDS = ImmutableList.of(ROUTINE_REFERENCE); + + private final String selector; + + RoutineField(String selector) { + this.selector = selector; + } + + @Override + public String getSelector() { + return selector; + } + } + /** * Fields of a BigQuery Job resource. * @@ -309,7 +336,7 @@ public static TableOption fields(TableField... fields) { } } - /** Class for specifying table get, create and update options. */ + /** Class for specifying model get, create and update options. */ class ModelOption extends Option { private static final long serialVersionUID = -1723870134095226772L; @@ -329,6 +356,26 @@ public static ModelOption fields(ModelField... fields) { } } + /** Class for specifying table get, create and update options. */ + class RoutineOption extends Option { + + private static final long serialVersionUID = -1723870122095226772L; + + private RoutineOption(BigQueryRpc.Option option, Object value) { + super(option, value); + } + + /** + * Returns an option to specify the routines's fields to be returned by the RPC call. If this + * option is not provided all model's fields are returned. {@code RoutineOption.fields} can be + * used to specify only the fields of interest. + */ + public static RoutineOption fields(RoutineField... fields) { + return new RoutineOption( + BigQueryRpc.Option.FIELDS, Helper.selector(RoutineField.REQUIRED_FIELDS, fields)); + } + } + /** Class for specifying table data list options. */ class TableDataListOption extends Option { @@ -583,6 +630,14 @@ public int hashCode() { */ Table create(TableInfo tableInfo, TableOption... options); + /** + * Creates a new routine. + * TODO: docs + * + * @throws BigQueryException upon failure + */ + Table create(RoutineInfo routineInfo, RoutineOption... options); + /** * Creates a new job. * @@ -804,6 +859,30 @@ public int hashCode() { */ boolean delete(ModelId modelId); + /** + * Deletes the requested routine. + * + *

Example of deleting a routine. + * + *

{@code
+   * String projectId = "my_project_id";
+   * String datasetId = "my_dataset_id";
+   * String routineId = "my_routine_id";
+   * RoutineId routineId = RoutineId.of(projectId, datasetId, routineId);
+   * boolean deleted = bigquery.delete(routineId);
+   * if (deleted) {
+   *   // the routine was deleted
+   * } else {
+   *   // the routine was not found
+   * }
+   * }
+ * + * @return {@code true} if routine was deleted, {@code false} if it was not found + * @throws BigQueryException upon failure + */ + boolean delete(RoutineId routineId); + + /** * Updates dataset information. * @@ -899,6 +978,14 @@ public int hashCode() { */ Model update(ModelInfo modelInfo, ModelOption... options); + /** + * Updates routine information. + * TODO: docs + * + * @throws BigQueryException upon failure + */ + Routine update(RoutineInfo routineInfo, RoutineOption... options); + /** * Returns the requested table or {@code null} if not found. * @@ -955,6 +1042,12 @@ public int hashCode() { */ Model getModel(ModelId tableId, ModelOption... options); + // TODO: docs + Routine getRoutine(String datasetId, String routineId, RoutineOption... options); + + // TODO: docs + Routine getRoutine(RoutineId routineId, RoutineOption... options); + /** * Lists the tables in the dataset. This method returns partial information on each table: ({@link * Table#getTableId()}, {@link Table#getFriendlyName()}, {@link Table#getGeneratedId()} and type, diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java index 7dee8cabcf06..1e34d7330b99 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java @@ -1,4 +1,155 @@ +/* + * Copyright 2019 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; -public class Routine { + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.util.List; +import java.util.Objects; + +import static com.google.common.base.Preconditions.checkNotNull; +import com.google.cloud.bigquery.BigQuery.RoutineOption; + + +public class Routine extends RoutineInfo { + + private final BigQueryOptions options; + private transient BigQuery bigquery; + + public static class Builder extends RoutineInfo.Builder { + + private final BigQuery bigquery; + private final RoutineInfo.BuilderImpl infoBuilder; + + Builder(BigQuery bigquery, RoutineId routineId) { + this.bigquery = bigquery; + this.infoBuilder = new RoutineInfo.BuilderImpl(); + this.infoBuilder.setRoutineId(routineId); + } + + Builder(Routine routine) { + this.bigquery = routine.bigquery; + this.infoBuilder = new RoutineInfo.BuilderImpl(); + } + + @Override + Builder setRoutineId(RoutineId id) { + infoBuilder.setRoutineId(id); + return this; + } + + @Override + Builder setEtag(String etag) { + infoBuilder.setEtag(etag); + return this; + } + + @Override + public Builder setRoutineType(String routineType) { + infoBuilder.setRoutineType(routineType); + return this; + } + + @Override + Builder setCreationTime(Long creationMillis) { + infoBuilder.setCreationTime(creationMillis); + return this; + } + + @Override + Builder setLastModifiedTime(Long lastModifiedMillis) { + infoBuilder.setLastModifiedTime(lastModifiedMillis); + return this; + } + + @Override + public Builder setArguments(List arguments) { + infoBuilder.setArguments(arguments); + return this; + } + + @Override + public Builder setReturnType(StandardSQLDataType returnType) { + infoBuilder.setReturnType(returnType); + return this; + } + + @Override + public Builder setImportedLibraries(List libraries) { + infoBuilder.setImportedLibraries(libraries); + return this; + } + + @Override + public Builder setBody(String body) { + infoBuilder.setBody(body); + return this; + } + + @Override + public Routine build() { + return new Routine(bigquery, infoBuilder); + } + } + + Routine(BigQuery bigquery, RoutineInfo.BuilderImpl infoBuilder) { + super(infoBuilder); + this.bigquery = checkNotNull(bigquery); + this.options = bigquery.getOptions(); + } + + + public boolean exists() { return bigquery.getRoutine(getRoutineId(), RoutineOption.fields()) != null; } + + public Routine reload(RoutineOption... options) { return bigquery.getRoutine(getRoutineId(), options); } + + public Routine update(RoutineOption... options) { return bigquery.update(this, options); } + + public boolean delete() { return bigquery.delete(getRoutineId()); } + + + public BigQuery getBigQuery() { return bigquery; } + + @Override + public Builder toBuilder() { + return new Builder(this); + } + + @Override + public final boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || !obj.getClass().equals(Routine.class)) { + return false; + } + Routine other = (Routine) obj; + return Objects.equals(toPb(), other.toPb()) && Objects.equals(options, other.options); + } + + public final int hashCode() { return Objects.hash(super.hashCode(), options); } + + public void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { + in.defaultReadObject(); + this.bigquery = options.getService(); + } + + static Routine fromPb(BigQuery bigquery, com.google.api.services.bigquery.model.Routine routinePb) { + return new Routine(bigquery, new RoutineInfo.BuilderImpl(routinePb)); + } + } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java index 001dd32c1125..4c6a5c511300 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java @@ -22,10 +22,14 @@ import com.google.common.collect.Lists; import javax.annotation.Nullable; +import java.io.Serializable; +import java.util.Collections; import java.util.List; +import static com.google.common.base.Preconditions.checkNotNull; + @AutoValue -public abstract class RoutineInfo { +public class RoutineInfo implements Serializable { static final Function FROM_PB_FUNCTION = new Function() { @@ -43,43 +47,165 @@ public Routine apply(RoutineInfo routineInfo) { }; - @AutoValue.Builder + private final RoutineId routineId; + private final String etag; + private final String routineType; + private final Long creationTime; + private final Long lastModifiedTime; + private final List argumentList; + private final StandardSQLDataType returnType; + private final List importedLibrariesList; + private final String body; + public abstract static class Builder { + abstract Builder setRoutineId(RoutineId id); abstract Builder setEtag(String etag); public abstract Builder setRoutineType(String routineType); abstract Builder setCreationTime(Long creationMillis); abstract Builder setLastModifiedTime(Long lastModifiedMillis); - public abstract Builder setArguments(List arguments); + public abstract Builder setArguments(List argumentList); public abstract Builder setReturnType(StandardSQLDataType returnType); - public abstract Builder setImportedLibraries(List libraries); + public abstract Builder setImportedLibraries(List importedLibrariesList); public abstract Builder setBody(String body); public abstract RoutineInfo build(); } - @Nullable - public abstract String getEtag(); + static class BuilderImpl extends Builder { + private RoutineId routineId; + private String etag; + private String routineType; + private Long creationTime; + private Long lastModifiedTime; + private List argumentList; + private StandardSQLDataType returnType; + private List importedLibrariesList; + private String body; + + BuilderImpl() {} + + BuilderImpl(RoutineInfo routineInfo) { + this.routineId = routineInfo.routineId; + this.etag = routineInfo.etag; + this.routineType = routineInfo.routineType; + this.creationTime = routineInfo.creationTime; + this.lastModifiedTime = routineInfo.lastModifiedTime; + this.argumentList = routineInfo.argumentList; + this.returnType = routineInfo.returnType; + this.importedLibrariesList = routineInfo.importedLibrariesList; + this.body = routineInfo.body; + } + + BuilderImpl(Routine routinePb) { + this.routineId = RoutineId.fromPb(routinePb.getRoutineReference()); + this.etag = routinePb.getEtag(); + this.routineType = routinePb.getRoutineType(); + this.creationTime = routinePb.getCreationTime(); + this.lastModifiedTime = routinePb.getLastModifiedTime(); + if (routinePb.getArguments() != null) { + this.argumentList = Lists.transform(routinePb.getArguments(), RoutineArgument.FROM_PB_FUNCTION); + } + if (routinePb.getReturnType() != null) { + this.returnType = StandardSQLDataType.fromPb(routinePb.getReturnType()); + } + if (routinePb.getImportedLibraries() == null) { + this.importedLibrariesList = Collections.emptyList(); + } else { + this.importedLibrariesList = routinePb.getImportedLibraries(); + } + this.body = routinePb.getDefinitionBody(); + } + + @Override + Builder setRoutineId(RoutineId id) { + this.routineId = id; + return this; + } + + @Override + Builder setEtag(String etag) { + this.etag = etag; + return this; + } + + @Override + public Builder setRoutineType(String routineType) { + this.routineType = routineType; + return this; + } + + @Override + Builder setCreationTime(Long creationMillis) { + this.creationTime = creationMillis; + return this; + } + + @Override + Builder setLastModifiedTime(Long lastModifiedMillis) { + this.lastModifiedTime = lastModifiedMillis; + return this; + } + + @Override + public Builder setArguments(List argumentList) { + this.argumentList = argumentList; + return this; + } + + @Override + public Builder setReturnType(StandardSQLDataType returnType) { + this.returnType = returnType; + return this; + } + + @Override + public Builder setImportedLibraries(List importedLibrariesList) { + this.importedLibrariesList = importedLibrariesList; + return this; + } + + @Override + public Builder setBody(String body) { + this.body = body; + return this; + } + + @Override + public RoutineInfo build() { + return new RoutineInfo(this); + } + } + + RoutineInfo(BuilderImpl builder) { + this.routineId = checkNotNull(builder.routineId); + this.etag = builder.etag; + this.routineType = builder.routineType; + this.creationTime = builder.creationTime; + this.lastModifiedTime = builder.lastModifiedTime; + this.argumentList = builder.argumentList; + this.returnType = builder.returnType; + this.importedLibrariesList = builder.importedLibrariesList; + this.body = builder.body; + } + + public RoutineId getRoutineId() { return routineId; } - @Nullable - public abstract String getRoutineType(); + public String getEtag() { return etag; } - @Nullable - public abstract Long getCreationTime(); + public String getRoutineType() { return routineType; } - @Nullable - public abstract Long getLastModifiedTime(); + public Long getCreationTime() { return creationTime; } - public abstract List getArguments(); + public Long getLastModifiedTime() { return lastModifiedTime; } - public abstract StandardSQLDataType getReturnType(); + public List getArguments() { return argumentList; } - public abstract List getImportedLibraries(); + public StandardSQLDataType getReturnType() { return returnType; } - @Nullable - public abstract String getBody(); + public List getImportedLibraries() { return importedLibrariesList; } - public abstract Builder toBuilder(); + public String getBody() { return body; } - static Builder newBuilder() { return new AutoValue_RoutineInfo.Builder(); } + public Builder toBuilder() { return new BuilderImpl(this); } Routine toPb() { Routine routinePb = new Routine() @@ -87,29 +213,19 @@ Routine toPb() { .setRoutineType(getRoutineType()) .setCreationTime(getCreationTime()) .setLastModifiedTime(getLastModifiedTime()); + if (getRoutineId() != null) { + routinePb.setRoutineReference(getRoutineId().toPb()); + } if (getArguments() != null) { routinePb.setArguments(Lists.transform(getArguments(), RoutineArgument.TO_PB_FUNCTION)); } return routinePb; } + + static RoutineInfo fromPb(Routine routinePb) { - Builder builder = newBuilder(); - builder.setEtag(routinePb.getEtag()); - builder.setRoutineType(routinePb.getRoutineType()); - builder.setCreationTime(routinePb.getCreationTime()); - builder.setLastModifiedTime(routinePb.getLastModifiedTime()); - if (routinePb.getArguments() != null) { - builder.setArguments(Lists.transform(routinePb.getArguments(), RoutineArgument.FROM_PB_FUNCTION)); - } - if (routinePb.getReturnType() != null) { - builder.setReturnType(StandardSQLDataType.fromPb(routinePb.getReturnType())); - } - if (routinePb.getImportedLibraries() != null) { - builder.setImportedLibraries(ImmutableList.copyOf(routinePb.getImportedLibraries())); - } - builder.setBody(routinePb.getDefinitionBody()); - return builder.build(); + return new BuilderImpl(routinePb).build(); } } \ No newline at end of file diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java index 8255bcfd5a56..4637c8ee3106 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java @@ -21,6 +21,7 @@ import com.google.api.services.bigquery.model.GetQueryResultsResponse; import com.google.api.services.bigquery.model.Job; import com.google.api.services.bigquery.model.Model; +import com.google.api.services.bigquery.model.Routine; import com.google.api.services.bigquery.model.Table; import com.google.api.services.bigquery.model.TableDataInsertAllRequest; import com.google.api.services.bigquery.model.TableDataInsertAllResponse; @@ -28,6 +29,7 @@ import com.google.cloud.ServiceRpc; import com.google.cloud.Tuple; import com.google.cloud.bigquery.BigQueryException; + import java.util.Map; @InternalExtensionOnly @@ -187,6 +189,17 @@ Tuple> listModels( */ boolean deleteModel(String projectId, String datasetId, String modelId); + // TODO: document these + Routine update(Routine routine, Map options); + + Routine getRoutine(String projectId, String datasetId, String routineId, Map options); + + Tuple> listRoutines( + String projectId, String datasetId, Map options); + + boolean deleteRoutine(String projectId, String datasetId, String routineId); + + /** * Sends an insert all request. * diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java index 4a83eb32fdbc..4dbe13d9e733 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java @@ -42,8 +42,11 @@ import com.google.api.services.bigquery.model.JobList; import com.google.api.services.bigquery.model.JobStatus; import com.google.api.services.bigquery.model.ListModelsResponse; +import com.google.api.services.bigquery.model.ListRoutinesResponse; import com.google.api.services.bigquery.model.Model; import com.google.api.services.bigquery.model.ModelReference; +import com.google.api.services.bigquery.model.Routine; +import com.google.api.services.bigquery.model.RoutineReference; import com.google.api.services.bigquery.model.Table; import com.google.api.services.bigquery.model.TableDataInsertAllRequest; import com.google.api.services.bigquery.model.TableDataInsertAllResponse; @@ -368,6 +371,69 @@ public boolean deleteModel(String projectId, String datasetId, String modelId) { } } + @Override + public Routine update(Routine routine, Map options) { + try { + // unset the type, as it is output only + RoutineReference reference = routine.getRoutineReference(); + return bigquery + .routines() + .update(reference.getProjectId(), reference.getDatasetId(), reference.getRoutineId(), routine) + .setFields(Option.FIELDS.getString(options)) + .execute(); + } catch (IOException ex) { + throw translate(ex); + } + } + + @Override + public Routine getRoutine(String projectId, String datasetId, String routineId, Map options) { + try { + return bigquery + .routines() + .get(projectId, datasetId, routineId) + .setFields(Option.FIELDS.getString(options)) + .execute(); + } catch (IOException ex) { + BigQueryException serviceException = translate(ex); + if (serviceException.getCode() == HTTP_NOT_FOUND) { + return null; + } + throw serviceException; + } + } + + @Override + public Tuple> listRoutines(String projectId, String datasetId, Map options) { + try { + ListRoutinesResponse routineList = + bigquery + .routines() + .list(projectId, datasetId) + .setMaxResults(Option.MAX_RESULTS.getLong(options)) + .setPageToken(Option.PAGE_TOKEN.getString(options)) + .execute(); + Iterable routines = routineList.getRoutines(); + return Tuple.of(routineList.getNextPageToken(), routines); + } catch (IOException ex) { + throw translate(ex); + } + } + + @Override + public boolean deleteRoutine(String projectId, String datasetId, String routineId) { + try { + bigquery.routines().delete(projectId, datasetId, routineId).execute(); + return true; + } catch (IOException ex) { + BigQueryException serviceException = translate(ex); + if (serviceException.getCode() == HTTP_NOT_FOUND) { + return false; + } + throw serviceException; + } + } + @Override public TableDataInsertAllResponse insertAll( String projectId, String datasetId, String tableId, TableDataInsertAllRequest request) { From 964934f54bc15acfbaddc9538a7fec78bfda475a Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Thu, 27 Jun 2019 10:29:13 -0700 Subject: [PATCH 05/16] most of the RPC layer, sans testing. --- .../com/google/cloud/bigquery/BigQuery.java | 29 ++- .../google/cloud/bigquery/BigQueryImpl.java | 192 ++++++++++++++++++ .../google/cloud/bigquery/RoutineInfo.java | 10 +- .../cloud/bigquery/spi/v2/BigQueryRpc.java | 3 + .../bigquery/spi/v2/HttpBigQueryRpc.java | 15 ++ 5 files changed, 246 insertions(+), 3 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java index ce65b0a69ea4..179f040f18bc 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java @@ -294,6 +294,27 @@ public static ModelListOption pageToken(String pageToken) { } } + /** Class for specifying table list options. */ + class RoutineListOption extends Option { + + private static final long serialVersionUID = 8660294969063312498L; + + private RoutineListOption(BigQueryRpc.Option option, Object value) { + super(option, value); + } + + /** Returns an option to specify the maximum number of models returned per page. */ + public static RoutineListOption pageSize(long pageSize) { + checkArgument(pageSize >= 0); + return new RoutineListOption(BigQueryRpc.Option.MAX_RESULTS, pageSize); + } + + /** Returns an option to specify the page token from which to start listing models. */ + public static RoutineListOption pageToken(String pageToken) { + return new RoutineListOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken); + } + } + /** Class for specifying table list options. */ class TableListOption extends Option { @@ -636,7 +657,7 @@ public int hashCode() { * * @throws BigQueryException upon failure */ - Table create(RoutineInfo routineInfo, RoutineOption... options); + Routine create(RoutineInfo routineInfo, RoutineOption... options); /** * Creates a new job. @@ -1048,6 +1069,12 @@ public int hashCode() { // TODO: docs Routine getRoutine(RoutineId routineId, RoutineOption... options); + /** Lists the models in the dataset. */ + Page listRoutines(String datasetId, RoutineListOption... options); + + /** Lists the models in the dataset. */ + Page listRoutines(DatasetId datasetId, RoutineListOption... options); + /** * Lists the tables in the dataset. This method returns partial information on each table: ({@link * Table#getTableId()}, {@link Table#getFriendlyName()}, {@link Table#getGeneratedId()} and type, diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java index 06b3f150b580..5219cdef91be 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java @@ -125,6 +125,30 @@ public Page getNextPage() { } } + private static class RoutinePageFetcher implements NextPageFetcher { + + private static final long serialVersionUID = 8611242311504201187L; + private final Map requestOptions; + private final BigQueryOptions serviceOptions; + private final DatasetId datasetId; + + RoutinePageFetcher( + DatasetId datasetId, + BigQueryOptions serviceOptions, + String cursor, + Map optionMap) { + this.requestOptions = + PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap); + this.serviceOptions = serviceOptions; + this.datasetId = datasetId; + } + + @Override + public Page getNextPage() { + return listRoutines(datasetId, serviceOptions, requestOptions); + } + } + private static class JobPageFetcher implements NextPageFetcher { private static final long serialVersionUID = 8536533282558245472L; @@ -226,6 +250,34 @@ public com.google.api.services.bigquery.model.Table call() { } } + @Override + public Routine create(RoutineInfo routineInfo, RoutineOption... options) { + final com.google.api.services.bigquery.model.Routine routinePb = + routineInfo + .setProjectId( + Strings.isNullOrEmpty(routineInfo.getRoutineId().getProject()) + ? getOptions().getProjectId() + : routineInfo.getRoutineId().getProject()) + .toPb(); + final Map optionsMap = optionMap(options); + try { + return Routine.fromPb( + this, + runWithRetries( + new Callable() { + @Override + public com.google.api.services.bigquery.model.Routine call() { + return bigQueryRpc.create(routinePb, optionsMap); + } + }, + getOptions().getRetrySettings(), + EXCEPTION_HANDLER, + getOptions().getClock())); + } catch (RetryHelper.RetryHelperException e) { + throw BigQueryException.translateAndThrow(e); + } + } + @Override public Job create(JobInfo jobInfo, JobOption... options) { Supplier idProvider = @@ -456,6 +508,32 @@ public Boolean call() { } } + @Override + public boolean delete(RoutineId routineId) { + final RoutineId completeRoutineId = + routineId.setProjectId( + Strings.isNullOrEmpty(routineId.getProject()) + ? getOptions().getProjectId() + : routineId.getProject()); + try { + return runWithRetries( + new Callable() { + @Override + public Boolean call() { + return bigQueryRpc.deleteRoutine( + completeRoutineId.getProject(), + completeRoutineId.getDataset(), + completeRoutineId.getRoutine()); + } + }, + getOptions().getRetrySettings(), + EXCEPTION_HANDLER, + getOptions().getClock()); + } catch (RetryHelper.RetryHelperException e) { + throw BigQueryException.translateAndThrow(e); + } + } + @Override public Dataset update(DatasetInfo datasetInfo, DatasetOption... options) { final com.google.api.services.bigquery.model.Dataset datasetPb = @@ -535,6 +613,34 @@ public com.google.api.services.bigquery.model.Model call() { } } + @Override + public Routine update(RoutineInfo routineInfo, RoutineOption... options) { + final com.google.api.services.bigquery.model.Routine routinePb = + routineInfo + .setProjectId( + Strings.isNullOrEmpty(routineInfo.getRoutineId().getProject()) + ? getOptions().getProjectId() + : routineInfo.getRoutineId().getProject()) + .toPb(); + final Map optionsMap = optionMap(options); + try { + return Routine.fromPb( + this, + runWithRetries( + new Callable() { + @Override + public com.google.api.services.bigquery.model.Routine call() { + return bigQueryRpc.update(routinePb, optionsMap); + } + }, + getOptions().getRetrySettings(), + EXCEPTION_HANDLER, + getOptions().getClock())); + } catch (RetryHelper.RetryHelperException e) { + throw BigQueryException.translateAndThrow(e); + } + } + @Override public Table getTable(final String datasetId, final String tableId, TableOption... options) { return getTable(TableId.of(datasetId, tableId), options); @@ -612,6 +718,44 @@ public com.google.api.services.bigquery.model.Model call() { } } + @Override + public Routine getRoutine(String datasetId, String routineId, RoutineOption... options) { + return getRoutine(RoutineId.of(datasetId, routineId), options); + } + + @Override + public Routine getRoutine(RoutineId routineId, RoutineOption... options) { + final RoutineId completeRoutineId = + routineId.setProjectId( + Strings.isNullOrEmpty(routineId.getProject()) + ? getOptions().getProjectId() + : routineId.getProject()); + final Map optionsMap = optionMap(options); + try { + com.google.api.services.bigquery.model.Routine answer = + runWithRetries( + new Callable() { + @Override + public com.google.api.services.bigquery.model.Routine call() { + return bigQueryRpc.getRoutine( + completeRoutineId.getProject(), + completeRoutineId.getDataset(), + completeRoutineId.getRoutine(), + optionsMap); + } + }, + getOptions().getRetrySettings(), + EXCEPTION_HANDLER, + getOptions().getClock()); + if (getOptions().getThrowNotFound() && answer == null) { + throw new BigQueryException(HTTP_NOT_FOUND, "Model not found"); + } + return answer == null ? null : Routine.fromPb(this, answer); + } catch (RetryHelper.RetryHelperException e) { + throw BigQueryException.translateAndThrow(e); + } + } + @Override public Page listTables(String datasetId, TableListOption... options) { return listTables( @@ -636,6 +780,17 @@ public Page listModels(DatasetId datasetId, ModelListOption... options) { return listModels(completeDatasetId, getOptions(), optionMap(options)); } + @Override + public Page listRoutines(String datasetId, RoutineListOption... options) { + return listRoutines(DatasetId.of(getOptions().getProjectId(), datasetId), getOptions(), optionMap(options)); + } + + @Override + public Page listRoutines(DatasetId datasetId, RoutineListOption... options) { + DatasetId completeDatasetId = datasetId.setProjectId(getOptions().getProjectId()); + return listRoutines(completeDatasetId, getOptions(), optionMap(options)); + } + @Override public List listPartitions(TableId tableId) { List partitions = new ArrayList(); @@ -730,6 +885,43 @@ public Model apply(com.google.api.services.bigquery.model.Model model) { } } + private static Page listRoutines( + final DatasetId datasetId, + final BigQueryOptions serviceOptions, + final Map optionsMap) { + try { + Tuple> result = + runWithRetries( + new Callable< + Tuple>>() { + @Override + public Tuple> + call() { + return serviceOptions + .getBigQueryRpcV2() + .listRoutines(datasetId.getProject(), datasetId.getDataset(), optionsMap); + } + }, + serviceOptions.getRetrySettings(), + EXCEPTION_HANDLER, + serviceOptions.getClock()); + String cursor = result.x(); + Iterable routines = + Iterables.transform( + result.y(), + new Function() { + @Override + public Routine apply(com.google.api.services.bigquery.model.Routine routinePb) { + return Routine.fromPb(serviceOptions.getService(), routinePb); + } + }); + return new PageImpl<>( + new RoutinePageFetcher(datasetId, serviceOptions, cursor, optionsMap), cursor, routines); + } catch (RetryHelper.RetryHelperException e) { + throw BigQueryException.translateAndThrow(e); + } + } + @Override public InsertAllResponse insertAll(InsertAllRequest request) { final TableId tableId = diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java index 4c6a5c511300..ce46200314ad 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java @@ -18,10 +18,9 @@ import com.google.api.services.bigquery.model.Routine; import com.google.auto.value.AutoValue; import com.google.common.base.Function; -import com.google.common.collect.ImmutableList; +import com.google.common.base.Strings; import com.google.common.collect.Lists; -import javax.annotation.Nullable; import java.io.Serializable; import java.util.Collections; import java.util.List; @@ -207,6 +206,13 @@ public RoutineInfo build() { public Builder toBuilder() { return new BuilderImpl(this); } + RoutineInfo setProjectId(String projectId) { + if (Strings.isNullOrEmpty(getRoutineId().getProject())) { + return toBuilder().setRoutineId(getRoutineId().setProjectId(projectId)).build(); + } + return this; + } + Routine toPb() { Routine routinePb = new Routine() .setEtag(getEtag()) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java index 4637c8ee3106..0d30ea263589 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java @@ -190,6 +190,9 @@ Tuple> listModels( boolean deleteModel(String projectId, String datasetId, String modelId); // TODO: document these + + Routine create(Routine routine, Map options); + Routine update(Routine routine, Map options); Routine getRoutine(String projectId, String datasetId, String routineId, Map options); diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java index 4dbe13d9e733..acdb919bc8a6 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java @@ -175,6 +175,21 @@ public Table create(Table table, Map options) { } } + @Override + public Routine create(Routine routine, Map options) { + try { + // unset the type, as it is output only + RoutineReference reference = routine.getRoutineReference(); + return bigquery + .routines() + .insert(reference.getProjectId(), reference.getDatasetId(), routine) + .setFields(Option.FIELDS.getString(options)) + .execute(); + } catch (IOException ex) { + throw translate(ex); + } + } + @Override public Job create(Job job, Map options) { try { From 3fb5094c01c2aa579041abd82e2e633ada901065 Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Thu, 27 Jun 2019 11:16:11 -0700 Subject: [PATCH 06/16] switch standard sql types over to autovalues. --- .../google/cloud/bigquery/RoutineInfo.java | 2 - .../cloud/bigquery/StandardSQLDataType.java | 122 ++++-------------- .../cloud/bigquery/StandardSQLField.java | 101 ++++----------- .../cloud/bigquery/StandardSQLStructType.java | 100 +++----------- .../bigquery/StandardSQLDataTypeTest.java | 3 +- 5 files changed, 71 insertions(+), 257 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java index ce46200314ad..09574bb6c7ef 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java @@ -16,7 +16,6 @@ package com.google.cloud.bigquery; import com.google.api.services.bigquery.model.Routine; -import com.google.auto.value.AutoValue; import com.google.common.base.Function; import com.google.common.base.Strings; import com.google.common.collect.Lists; @@ -27,7 +26,6 @@ import static com.google.common.base.Preconditions.checkNotNull; -@AutoValue public class RoutineInfo implements Serializable { static final Function FROM_PB_FUNCTION = diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java index 3f7e24a6a43c..3c72a711ceec 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java @@ -16,17 +16,18 @@ package com.google.cloud.bigquery; import com.google.api.services.bigquery.model.StandardSqlDataType; +import com.google.auto.value.AutoValue; import com.google.common.base.MoreObjects; +import javax.annotation.Nullable; import java.io.Serializable; import java.util.Objects; -public class StandardSQLDataType implements Serializable { +@AutoValue +public abstract class StandardSQLDataType implements Serializable { - private final StandardSQLDataType arrayElementType; - private final StandardSQLStructType structType; - private final String typeKind; + @AutoValue.Builder public abstract static class Builder { /** Sets the type of an array's elements, when the TypeKind is * ARRAY. @@ -49,114 +50,45 @@ public abstract static class Builder { } - static class BuilderImpl extends Builder { - private String typeKind; - private StandardSQLDataType arrayElementType; - private StandardSQLStructType structType; + public abstract String getTypeKind(); - BuilderImpl() {} + @Nullable + public abstract StandardSQLDataType getArrayElementType(); - BuilderImpl(StandardSQLDataType dataType) { - this.arrayElementType = dataType.arrayElementType; - this.structType = dataType.structType; - this.typeKind = dataType.typeKind; - } - - BuilderImpl(StandardSqlDataType dataTypePb) { - this.typeKind = dataTypePb.getTypeKind(); - if (this.arrayElementType != null) { - this.arrayElementType = StandardSQLDataType.fromPb(dataTypePb.getArrayElementType()); - } - if (this.structType != null) { - this.structType = StandardSQLStructType.fromPb(dataTypePb.getStructType()); - } - } - - @Override - public Builder setArrayElementType(StandardSQLDataType arrayElementType) { - this.arrayElementType = arrayElementType; - return this; - } - - @Override - public Builder setStructType(StandardSQLStructType structType) { - this.structType = structType; - return this; - } + @Nullable + public abstract StandardSQLStructType getStructType(); - @Override - public Builder setTypeKind(String typeKind) { - this.typeKind = typeKind; - return this; - } - - @Override - public StandardSQLDataType build() { - return new StandardSQLDataType(this); - } - } - - StandardSQLDataType(BuilderImpl builder) { - this.arrayElementType = builder.arrayElementType; - this.structType = builder.structType; - this.typeKind = builder.typeKind; - } + public abstract Builder toBuilder(); - public String getTypeKind() { return typeKind; } - public StandardSQLDataType getArrayElementType() { return arrayElementType; } - - public StandardSQLStructType getStructType() { return structType; } - - public Builder toBuilder() { - return new BuilderImpl(this); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("arrayElementType", arrayElementType) - .add("structType", structType) - .add("typeKind", typeKind) - .toString(); - } - - @Override - public int hashCode() { - return Objects.hash( - typeKind, - arrayElementType, - structType - ); - } - - @Override - public boolean equals(Object obj) { - return obj == this - || obj != null - && obj.getClass().equals(StandardSQLDataType.class) - && Objects.equals(toPb(), ((StandardSQLDataType) obj).toPb()); - } + public static Builder newBuilder() { return new AutoValue_StandardSQLDataType.Builder(); } public static Builder newBuilder(String typeKind) { - return new BuilderImpl().setTypeKind(typeKind); + return newBuilder().setTypeKind(typeKind); } StandardSqlDataType toPb() { StandardSqlDataType dataTypePb = new StandardSqlDataType(); - dataTypePb.setTypeKind(typeKind); - if (arrayElementType != null) { - dataTypePb.setArrayElementType(arrayElementType.toPb()); + dataTypePb.setTypeKind(getTypeKind()); + if (getArrayElementType() != null) { + dataTypePb.setArrayElementType(getArrayElementType().toPb()); } - if (structType != null) { - dataTypePb.setStructType(structType.toPb()); + if (getStructType() != null) { + dataTypePb.setStructType(getStructType().toPb()); } return dataTypePb; } static StandardSQLDataType fromPb(StandardSqlDataType dataTypePb) { - return new BuilderImpl(dataTypePb).build(); + Builder builder = newBuilder(); + builder.setTypeKind(dataTypePb.getTypeKind()); + if (dataTypePb.getArrayElementType() != null) { + builder.setArrayElementType(StandardSQLDataType.fromPb(dataTypePb.getArrayElementType())); + } + if (dataTypePb.getStructType() != null) { + builder.setStructType(StandardSQLStructType.fromPb(dataTypePb.getStructType())); + } + return builder.build(); } - } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java index b325ac41d520..fe29a45a6f67 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java @@ -16,13 +16,14 @@ package com.google.cloud.bigquery; import com.google.api.services.bigquery.model.StandardSqlField; +import com.google.auto.value.AutoValue; import com.google.common.base.Function; -import com.google.common.base.MoreObjects; +import javax.annotation.Nullable; import java.io.Serializable; -import java.util.Objects; -public class StandardSQLField implements Serializable { +@AutoValue +public abstract class StandardSQLField implements Serializable { static final Function FROM_PB_FUNCTION = new Function() { @@ -39,9 +40,7 @@ public StandardSqlField apply(StandardSQLField field) { } }; - private final String name; - private final StandardSQLDataType dataType; - + @AutoValue.Builder public abstract static class Builder { public abstract Builder setName(String name); @@ -51,95 +50,39 @@ public abstract static class Builder { public abstract StandardSQLField build(); } - static class BuilderImpl extends Builder { - - private String name; - private StandardSQLDataType dataType; + @Nullable + public abstract String getName(); - BuilderImpl() {} - - BuilderImpl(StandardSQLField field) { - this.name = field.name; - this.dataType = field.dataType; - } + public abstract StandardSQLDataType getDataType(); - BuilderImpl(StandardSqlField fieldPb) { - this.name = fieldPb.getName(); - this.dataType = StandardSQLDataType.fromPb(fieldPb.getType()); - } - - @Override - public Builder setName(String name) { - this.name = name; - return this; - } + public abstract Builder toBuilder(); - @Override - public Builder setDataType(StandardSQLDataType dataType) { - this.dataType = dataType; - return this; - } - - @Override - public StandardSQLField build() { - return new StandardSQLField(this); - } - } - - StandardSQLField(BuilderImpl builder) { - this.name = builder.name; - this.dataType = builder.dataType; - } + public static Builder newBuilder() { return new AutoValue_StandardSQLField.Builder(); } - public String getName() { return name; } - - public StandardSQLDataType getDataType() { return dataType; } - - public Builder toBuilder() { return new BuilderImpl(this); } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("name", name) - .add("dataType", dataType) - .toString(); - } - - @Override - public int hashCode() { - return Objects.hash( - name, - dataType - ); - } - - @Override - public boolean equals(Object obj) { - return obj == this - || obj != null - && obj.getClass().equals(StandardSQLField.class) - && Objects.equals(toPb(), ((StandardSQLField) obj).toPb()); + public static Builder newBuilder(StandardSQLDataType dataType) { + return newBuilder().setDataType(dataType); } public static Builder newBuilder(String name, StandardSQLDataType dataType) { - return new BuilderImpl().setDataType(dataType).setName(name); - } - - public static Builder newBuilder(StandardSQLDataType dataType) { - return new BuilderImpl().setDataType(dataType); + return newBuilder().setName(name).setDataType(dataType); } public StandardSqlField toPb() { StandardSqlField fieldPb = new StandardSqlField(); - fieldPb.setName(name); - if (dataType != null) { - fieldPb.setType(dataType.toPb()); + fieldPb.setName(getName()); + if (getDataType() != null) { + fieldPb.setType(getDataType().toPb()); } return fieldPb; } static StandardSQLField fromPb(StandardSqlField fieldPb) { - return new BuilderImpl(fieldPb).build(); + Builder builder = newBuilder(); + builder.setName(fieldPb.getName()); + if (fieldPb.getType() != null) { + builder.setDataType(StandardSQLDataType.fromPb(fieldPb.getType())); + } + return builder.build(); } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLStructType.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLStructType.java index 603fc1d004a6..652944ca5f84 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLStructType.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLStructType.java @@ -17,71 +17,18 @@ package com.google.cloud.bigquery; import com.google.api.services.bigquery.model.StandardSqlStructType; +import com.google.auto.value.AutoValue; import com.google.common.collect.Lists; import java.io.Serializable; -import java.util.Collections; import java.util.List; -import java.util.Objects; -import static com.google.common.base.Preconditions.checkNotNull; -public final class StandardSQLStructType implements Serializable { +@AutoValue +public abstract class StandardSQLStructType implements Serializable { - private final List fieldList; - - private StandardSQLStructType(BuilderImpl builder) { - this.fieldList = builder.fieldList; - } - - static StandardSQLStructType fromPb(com.google.api.services.bigquery.model.StandardSqlStructType structTypePb) { - Builder builder = new StandardSQLStructType.BuilderImpl(); - if (structTypePb.getFields() == null) { - builder.setFields(Collections.emptyList()); - } else { - builder.setFields(Lists.transform(structTypePb.getFields(), StandardSQLField.FROM_PB_FUNCTION)); - } - return builder.build(); - } - - public List getFields() { - return fieldList; - } - - @Override - public boolean equals(Object obj) { - return obj == this || obj instanceof StandardSQLStructType && Objects.equals(toPb(), ((StandardSQLStructType) obj).toPb()); - } - - @Override - public int hashCode() { - return Objects.hash(fieldList); - } - - /** - * Returns a builder for the StandardSQLStructType object. - */ - public Builder toBuilder() { - return new BuilderImpl(this); - } - - @Override - public String toString() { - return toPb().toString(); - } - - public static Builder newBuilder(List fieldList) { - return new BuilderImpl().setFields(fieldList); - } - - StandardSqlStructType toPb() { - StandardSqlStructType structTypePb = new StandardSqlStructType(); - if (fieldList != null) { - structTypePb.setFields(Lists.transform(fieldList, StandardSQLField.TO_PB_FUNCTION)); - } - return structTypePb; - } + @AutoValue.Builder public abstract static class Builder { public abstract Builder setFields(List fields); @@ -90,35 +37,30 @@ public abstract static class Builder { } - static class BuilderImpl extends Builder { + public abstract List getFields(); - private List fieldList; + public abstract Builder toBuilder(); - BuilderImpl() { - } + public static Builder newBuilder() { return new AutoValue_StandardSQLStructType.Builder(); } - BuilderImpl(StandardSQLStructType structType) { - this.fieldList = structType.fieldList; - } - - BuilderImpl(StandardSqlStructType structTypePb) { - if (structTypePb.getFields() == null) { - this.fieldList = Collections.emptyList(); - } - this.fieldList = Lists.transform(structTypePb.getFields(), StandardSQLField.FROM_PB_FUNCTION); - } + public static Builder newBuilder(List fieldList) { + return newBuilder().setFields(fieldList); + } - @Override - public Builder setFields(List fieldList) { - this.fieldList = checkNotNull(fieldList); - return this; + static StandardSQLStructType fromPb(com.google.api.services.bigquery.model.StandardSqlStructType structTypePb) { + Builder builder = newBuilder(); + if (structTypePb.getFields() != null) { + builder.setFields(Lists.transform(structTypePb.getFields(), StandardSQLField.FROM_PB_FUNCTION)); } + return builder.build(); + } - @Override - public StandardSQLStructType build() { - return new StandardSQLStructType(this); + StandardSqlStructType toPb() { + StandardSqlStructType structTypePb = new StandardSqlStructType(); + if (getFields() != null) { + structTypePb.setFields(Lists.transform(getFields(), StandardSQLField.TO_PB_FUNCTION)); } - + return structTypePb; } } diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLDataTypeTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLDataTypeTest.java index 3501d992ceed..9bc1e147d579 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLDataTypeTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLDataTypeTest.java @@ -17,7 +17,6 @@ import com.google.common.collect.ImmutableList; import org.junit.Test; - import java.util.List; import static org.junit.Assert.assertEquals; @@ -61,7 +60,7 @@ public void testBuilder() { @Test public void testToAndFromPb() { - + compareStandardSQLDataType(ARRAY_OF_STRING_DATA_TYPE, StandardSQLDataType.fromPb(ARRAY_OF_STRING_DATA_TYPE.toPb())); } private void compareStandardSQLDataType(StandardSQLDataType expected, StandardSQLDataType value) { From d4080ff3bc462b93b0cddf94d1d06238a067d3d0 Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Thu, 27 Jun 2019 13:28:26 -0700 Subject: [PATCH 07/16] more testing, impls --- .../com/google/cloud/bigquery/Routine.java | 6 + .../google/cloud/bigquery/RoutineInfo.java | 70 ++++++++++- .../bigquery/spi/v2/HttpBigQueryRpc.java | 2 - .../testing/RemoteBigQueryHelper.java | 5 + .../cloud/bigquery/RoutineInfoTest.java | 113 ++++++++++++++++++ .../cloud/bigquery/it/ITBigQueryTest.java | 54 +++++++++ 6 files changed, 246 insertions(+), 4 deletions(-) create mode 100644 google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineInfoTest.java diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java index 1e34d7330b99..5463a4725a5d 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java @@ -76,6 +76,12 @@ Builder setLastModifiedTime(Long lastModifiedMillis) { return this; } + @Override + public RoutineInfo.Builder setLanguage(String language) { + infoBuilder.setLanguage(language); + return this; + } + @Override public Builder setArguments(List arguments) { infoBuilder.setArguments(arguments); diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java index 09574bb6c7ef..47137939b448 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java @@ -17,12 +17,14 @@ import com.google.api.services.bigquery.model.Routine; import com.google.common.base.Function; +import com.google.common.base.MoreObjects; import com.google.common.base.Strings; import com.google.common.collect.Lists; import java.io.Serializable; import java.util.Collections; import java.util.List; +import java.util.Objects; import static com.google.common.base.Preconditions.checkNotNull; @@ -49,6 +51,7 @@ public Routine apply(RoutineInfo routineInfo) { private final String routineType; private final Long creationTime; private final Long lastModifiedTime; + private final String language; private final List argumentList; private final StandardSQLDataType returnType; private final List importedLibrariesList; @@ -60,6 +63,7 @@ public abstract static class Builder { public abstract Builder setRoutineType(String routineType); abstract Builder setCreationTime(Long creationMillis); abstract Builder setLastModifiedTime(Long lastModifiedMillis); + public abstract Builder setLanguage(String language); public abstract Builder setArguments(List argumentList); public abstract Builder setReturnType(StandardSQLDataType returnType); public abstract Builder setImportedLibraries(List importedLibrariesList); @@ -73,6 +77,7 @@ static class BuilderImpl extends Builder { private String routineType; private Long creationTime; private Long lastModifiedTime; + private String language; private List argumentList; private StandardSQLDataType returnType; private List importedLibrariesList; @@ -86,6 +91,7 @@ static class BuilderImpl extends Builder { this.routineType = routineInfo.routineType; this.creationTime = routineInfo.creationTime; this.lastModifiedTime = routineInfo.lastModifiedTime; + this.language = routineInfo.language; this.argumentList = routineInfo.argumentList; this.returnType = routineInfo.returnType; this.importedLibrariesList = routineInfo.importedLibrariesList; @@ -98,6 +104,7 @@ static class BuilderImpl extends Builder { this.routineType = routinePb.getRoutineType(); this.creationTime = routinePb.getCreationTime(); this.lastModifiedTime = routinePb.getLastModifiedTime(); + this.language = routinePb.getLanguage(); if (routinePb.getArguments() != null) { this.argumentList = Lists.transform(routinePb.getArguments(), RoutineArgument.FROM_PB_FUNCTION); } @@ -142,6 +149,12 @@ Builder setLastModifiedTime(Long lastModifiedMillis) { return this; } + @Override + public Builder setLanguage(String language) { + this.language = language; + return this; + } + @Override public Builder setArguments(List argumentList) { this.argumentList = argumentList; @@ -178,6 +191,7 @@ public RoutineInfo build() { this.routineType = builder.routineType; this.creationTime = builder.creationTime; this.lastModifiedTime = builder.lastModifiedTime; + this.language = builder.language; this.argumentList = builder.argumentList; this.returnType = builder.returnType; this.importedLibrariesList = builder.importedLibrariesList; @@ -186,7 +200,7 @@ public RoutineInfo build() { public RoutineId getRoutineId() { return routineId; } - public String getEtag() { return etag; } + public String getEtag() { return etag; } public String getRoutineType() { return routineType; } @@ -194,6 +208,8 @@ public RoutineInfo build() { public Long getLastModifiedTime() { return lastModifiedTime; } + public String getLanguage() { return language; } + public List getArguments() { return argumentList; } public StandardSQLDataType getReturnType() { return returnType; } @@ -204,6 +220,55 @@ public RoutineInfo build() { public Builder toBuilder() { return new BuilderImpl(this); } + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("routineId", routineId) + .add("etag", etag) + .add("routineType", routineType) + .add("creationTime", creationTime) + .add("lastModifiedTime", lastModifiedTime) + .add("language", language) + .add("arguments", argumentList) + .add("returnType", returnType) + .add("importedLibrariesList", importedLibrariesList) + .add("body", body) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash( + routineId, + etag, + routineType, + creationTime, + lastModifiedTime, + language, + argumentList, + returnType, + importedLibrariesList, + body); + } + + @Override + public boolean equals(Object obj) { + return obj == this + || obj != null + && obj.getClass().equals(RoutineInfo.class) + && Objects.equals(toPb(), ((RoutineInfo) obj).toPb()); + } + + /** Returns a builder for a {@code RoutineInfo} object given routine identity. */ + public static Builder newBuilder(RoutineId routineId) { + return new BuilderImpl().setRoutineId(routineId); + } + + /** Returns a {@code RoutineInfo} object given routine identity. */ + public static RoutineInfo of(RoutineId routineId) { + return newBuilder(routineId).build(); + } + RoutineInfo setProjectId(String projectId) { if (Strings.isNullOrEmpty(getRoutineId().getProject())) { return toBuilder().setRoutineId(getRoutineId().setProjectId(projectId)).build(); @@ -216,7 +281,8 @@ Routine toPb() { .setEtag(getEtag()) .setRoutineType(getRoutineType()) .setCreationTime(getCreationTime()) - .setLastModifiedTime(getLastModifiedTime()); + .setLastModifiedTime(getLastModifiedTime()) + .setLanguage(getLanguage()); if (getRoutineId() != null) { routinePb.setRoutineReference(getRoutineId().toPb()); } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java index acdb919bc8a6..e63577b7f946 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java @@ -178,7 +178,6 @@ public Table create(Table table, Map options) { @Override public Routine create(Routine routine, Map options) { try { - // unset the type, as it is output only RoutineReference reference = routine.getRoutineReference(); return bigquery .routines() @@ -389,7 +388,6 @@ public boolean deleteModel(String projectId, String datasetId, String modelId) { @Override public Routine update(Routine routine, Map options) { try { - // unset the type, as it is output only RoutineReference reference = routine.getRoutineReference(); return bigquery .routines() diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelper.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelper.java index 672b7e7c74a1..784ca984fa09 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelper.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelper.java @@ -43,6 +43,7 @@ public class RemoteBigQueryHelper { private static final Logger log = Logger.getLogger(RemoteBigQueryHelper.class.getName()); private static final String DATASET_NAME_PREFIX = "gcloud_test_dataset_temp_"; private static final String MODEL_NAME_PREFIX = "model_"; + private static final String ROUTINE_NAME_PREFIX = "routine_"; private final BigQueryOptions options; private static final int connectTimeout = 60000; @@ -76,6 +77,10 @@ public static String generateModelName() { return MODEL_NAME_PREFIX + UUID.randomUUID().toString().replace('-', '_'); } + public static String generateRoutineName() { + return ROUTINE_NAME_PREFIX + UUID.randomUUID().toString().replace('-', '_'); + } + /** * Creates a {@code RemoteBigQueryHelper} object for the given project id and JSON key input * stream. diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineInfoTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineInfoTest.java new file mode 100644 index 000000000000..0d1146cdd58a --- /dev/null +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineInfoTest.java @@ -0,0 +1,113 @@ +package com.google.cloud.bigquery; + +import com.google.common.collect.ImmutableList; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class RoutineInfoTest { + + private static final RoutineId ROUTINE_ID = RoutineId.of("dataset", "routine"); + private static final String ETAG = "etag"; + private static final String ROUTINE_TYPE = "SCALAR_FUNCTION"; + private static final Long CREATION_TIME = 10L; + private static final Long LAST_MODIFIED_TIME = 20L; + private static final String LANGUAGE="SQL"; + + private static final RoutineArgument ARG_1 = RoutineArgument.newBuilder() + .setDataType(StandardSQLDataType.newBuilder("STRING").build()) + .setName("arg1") + .build(); + + private static final List ARGUMENT_LIST = ImmutableList.of(ARG_1); + + private static final StandardSQLDataType RETURN_TYPE = StandardSQLDataType.newBuilder("FLOAT64").build(); + + private static final List IMPORTED_LIBRARIES = ImmutableList.of( + "gs://foo", + "gs://bar", + "gs://baz"); + + private static final String BODY = "body"; + + private static final RoutineInfo ROUTINE_INFO = RoutineInfo.of(ROUTINE_ID).toBuilder() + .setEtag(ETAG) + .setRoutineType(ROUTINE_TYPE) + .setCreationTime(CREATION_TIME) + .setLastModifiedTime(LAST_MODIFIED_TIME) + .setLanguage(LANGUAGE) + .setArguments(ARGUMENT_LIST) + .setReturnType(RETURN_TYPE) + .setImportedLibraries(IMPORTED_LIBRARIES) + .setBody(BODY) + .build(); + + + @Test + public void testToBuilder() { + compareRoutineInfo(ROUTINE_INFO, ROUTINE_INFO.toBuilder().build()); + } + + @Test + public void testBuilderIncomplete() { + RoutineInfo routineInfo = RoutineInfo.of(ROUTINE_ID); + assertEquals(routineInfo, routineInfo.toBuilder().build()); + } + + @Test + public void testBuilder() { + assertEquals(ROUTINE_ID, ROUTINE_INFO.getRoutineId()); + assertEquals(ETAG, ROUTINE_INFO.getEtag()); + assertEquals(ROUTINE_TYPE, ROUTINE_INFO.getRoutineType()); + assertEquals(CREATION_TIME, ROUTINE_INFO.getCreationTime()); + assertEquals(LAST_MODIFIED_TIME, ROUTINE_INFO.getLastModifiedTime()); + assertEquals(LANGUAGE, ROUTINE_INFO.getLanguage()); + assertEquals(ARGUMENT_LIST, ROUTINE_INFO.getArguments()); + assertEquals(RETURN_TYPE, ROUTINE_INFO.getReturnType()); + assertEquals(IMPORTED_LIBRARIES, ROUTINE_INFO.getImportedLibraries()); + assertEquals(BODY, ROUTINE_INFO.getBody()); + } + + @Test + public void testOf() { + RoutineInfo routineInfo = RoutineInfo.of(ROUTINE_ID); + assertEquals(ROUTINE_ID, ROUTINE_INFO.getRoutineId()); + assertNull(routineInfo.getEtag()); + assertNull(routineInfo.getRoutineType()); + assertNull(routineInfo.getCreationTime()); + assertNull(routineInfo.getLastModifiedTime()); + assertNull(routineInfo.getLanguage()); + assertNull(routineInfo.getArguments()); + assertNull(routineInfo.getReturnType()); + assertNull(routineInfo.getImportedLibraries()); + assertNull(routineInfo.getBody()); + } + + public void testToAndFromPb() { + compareRoutineInfo(ROUTINE_INFO, RoutineInfo.fromPb(ROUTINE_INFO.toPb())); + } + + @Test + public void testSetProjectId() { + assertEquals("project", ROUTINE_INFO.setProjectId("project").getRoutineId().getProject()); + } + + + public void compareRoutineInfo(RoutineInfo expected, RoutineInfo value) { + assertEquals(expected, value); + assertEquals(expected.getRoutineId(), value.getRoutineId()); + assertEquals(expected.getEtag(), value.getEtag()); + assertEquals(expected.getRoutineType(), value.getRoutineType()); + assertEquals(expected.getCreationTime(), value.getCreationTime()); + assertEquals(expected.getLastModifiedTime(), value.getLastModifiedTime()); + assertEquals(expected.getLanguage(), value.getLanguage()); + assertEquals(expected.getArguments(), value.getArguments()); + assertEquals(expected.getReturnType(), value.getReturnType()); + assertEquals(expected.getImportedLibraries(), value.getImportedLibraries()); + assertEquals(expected.getBody(), value.getBody()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index f2feffbaad73..2c8fbc361b5b 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -65,6 +65,9 @@ import com.google.cloud.bigquery.ModelInfo; import com.google.cloud.bigquery.QueryJobConfiguration; import com.google.cloud.bigquery.QueryParameterValue; +import com.google.cloud.bigquery.Routine; +import com.google.cloud.bigquery.RoutineId; +import com.google.cloud.bigquery.RoutineInfo; import com.google.cloud.bigquery.Schema; import com.google.cloud.bigquery.StandardTableDefinition; import com.google.cloud.bigquery.Table; @@ -122,6 +125,7 @@ public class ITBigQueryTest { private static final String DESCRIPTION = "Test dataset"; private static final String OTHER_DATASET = RemoteBigQueryHelper.generateDatasetName(); private static final String MODEL_DATASET = RemoteBigQueryHelper.generateDatasetName(); + private static final String ROUTINE_DATASET = RemoteBigQueryHelper.generateDatasetName(); private static final Map LABELS = ImmutableMap.of( "example-label1", "example-value1", @@ -286,6 +290,9 @@ public static void beforeClass() throws InterruptedException, TimeoutException { DatasetInfo info2 = DatasetInfo.newBuilder(MODEL_DATASET).setDescription("java model lifecycle").build(); bigquery.create(info2); + DatasetInfo info3 = + DatasetInfo.newBuilder(ROUTINE_DATASET).setDescription("java routine lifecycle").build(); + bigquery.create(info3); LoadJobConfiguration configuration = LoadJobConfiguration.newBuilder( TABLE_ID, "gs://" + BUCKET + "/" + JSON_LOAD_FILE, FormatOptions.json()) @@ -302,6 +309,8 @@ public static void afterClass() throws ExecutionException, InterruptedException if (bigquery != null) { RemoteBigQueryHelper.forceDelete(bigquery, DATASET); RemoteBigQueryHelper.forceDelete(bigquery, MODEL_DATASET); + RemoteBigQueryHelper.forceDelete(bigquery, ROUTINE_DATASET); + } if (storage != null) { boolean wasDeleted = RemoteStorageHelper.forceDelete(storage, BUCKET, 10, TimeUnit.SECONDS); @@ -1109,6 +1118,51 @@ public void testModelLifecycle() throws InterruptedException { assertTrue(bigquery.delete(modelId)); } + @Test + public void testRoutineLifecycle() throws InterruptedException { + + String routineName = RemoteBigQueryHelper.generateRoutineName(); + // Create a routine using SQL. + String sql = + "CREATE FUNCTION `" + + ROUTINE_DATASET + + "." + + routineName + + "`" + + "(x INT64) AS (x * 3)"; + QueryJobConfiguration config = QueryJobConfiguration.newBuilder(sql).build(); + Job job = bigquery.create(JobInfo.of(JobId.of(), config)); + job.waitFor(); + assertNull(job.getStatus().getError()); + + // Routine is created. Fetch. + RoutineId routineId = RoutineId.of(ROUTINE_DATASET, routineName); + Routine routine = bigquery.getRoutine(routineId); + assertNotNull(routine); + assertEquals(routine.getRoutineType(), "SCALAR_FUNCTION"); + + // Mutate metadata. + RoutineInfo info = routine.toBuilder().setBody("x * 4").build(); + Routine afterUpdate = bigquery.update(info); + assertEquals(afterUpdate.getBody(), "x * 4"); + + // Ensure routine is present in listRoutines. + Page routines = bigquery.listRoutines(ROUTINE_DATASET); + Boolean found = false; + for (Routine r : routines.getValues()) { + if (r.getRoutineId().getRoutine().equals(routineName)) { + found = true; + break; + } + } + assertTrue(found); + + // Delete the routine. + assertTrue(bigquery.delete(routineId)); + } + + + @Test public void testQuery() throws InterruptedException { String query = "SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID.getTable(); From 3dc662cdfde34e922a31c1395e3a6724efa946ed Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Thu, 27 Jun 2019 15:44:47 -0700 Subject: [PATCH 08/16] first working integration test --- .../com/google/cloud/bigquery/Routine.java | 11 +- .../google/cloud/bigquery/RoutineInfo.java | 1 + .../google/cloud/bigquery/RoutineTest.java | 211 ++++++++++++++++++ .../cloud/bigquery/it/ITBigQueryTest.java | 9 +- 4 files changed, 228 insertions(+), 4 deletions(-) create mode 100644 google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineTest.java diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java index 5463a4725a5d..887a505be950 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java @@ -25,6 +25,13 @@ import com.google.cloud.bigquery.BigQuery.RoutineOption; +/** + * A Google BigQuery Routine. + * + *

Objects of this class are immutable. Operations that modify the routine like {@link #update} + * return a new object. To get a {@code routine} object with the most recent information use {@link + * #reload}. + */ public class Routine extends RoutineInfo { private final BigQueryOptions options; @@ -43,7 +50,7 @@ public static class Builder extends RoutineInfo.Builder { Builder(Routine routine) { this.bigquery = routine.bigquery; - this.infoBuilder = new RoutineInfo.BuilderImpl(); + this.infoBuilder = new RoutineInfo.BuilderImpl(routine); } @Override @@ -77,7 +84,7 @@ Builder setLastModifiedTime(Long lastModifiedMillis) { } @Override - public RoutineInfo.Builder setLanguage(String language) { + public Builder setLanguage(String language) { infoBuilder.setLanguage(language); return this; } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java index 47137939b448..a070ba351c68 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java @@ -280,6 +280,7 @@ Routine toPb() { Routine routinePb = new Routine() .setEtag(getEtag()) .setRoutineType(getRoutineType()) + .setDefinitionBody(getBody()) .setCreationTime(getCreationTime()) .setLastModifiedTime(getLastModifiedTime()) .setLanguage(getLanguage()); diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineTest.java new file mode 100644 index 000000000000..000ccdd5ce1e --- /dev/null +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineTest.java @@ -0,0 +1,211 @@ +package com.google.cloud.bigquery; + +import com.google.common.collect.ImmutableList; +import org.junit.Test; + +import java.util.List; + +import static org.easymock.EasyMock.*; +import static org.easymock.EasyMock.replay; +import static org.junit.Assert.*; + +public class RoutineTest { + + private BigQuery serviceMockReturnsOptions = createStrictMock(BigQuery.class); + private BigQueryOptions mockOptions = createMock(BigQueryOptions.class); + + private static final RoutineId ROUTINE_ID = RoutineId.of("dataset", "routine"); + private static final String ETAG = "etag"; + private static final String ROUTINE_TYPE = "SCALAR_FUNCTION"; + private static final Long CREATION_TIME = 10L; + private static final Long LAST_MODIFIED_TIME = 20L; + private static final String LANGUAGE="SQL"; + + private static final RoutineArgument ARG_1 = RoutineArgument.newBuilder() + .setDataType(StandardSQLDataType.newBuilder("STRING").build()) + .setName("arg1") + .build(); + + private static final List ARGUMENT_LIST = ImmutableList.of(ARG_1); + + private static final StandardSQLDataType RETURN_TYPE = StandardSQLDataType.newBuilder("FLOAT64").build(); + + private static final List IMPORTED_LIBRARIES = ImmutableList.of( + "gs://foo", + "gs://bar", + "gs://baz"); + + private static final String BODY = "body"; + + private static final RoutineInfo ROUTINE_INFO = RoutineInfo.newBuilder(ROUTINE_ID) + .setEtag(ETAG) + .setRoutineType(ROUTINE_TYPE) + .setCreationTime(CREATION_TIME) + .setLastModifiedTime(LAST_MODIFIED_TIME) + .setLanguage(LANGUAGE) + .setArguments(ARGUMENT_LIST) + .setReturnType(RETURN_TYPE) + .setImportedLibraries(IMPORTED_LIBRARIES) + .setBody(BODY) + .build(); + + + private BigQuery bigquery; + private Routine expectedRoutine; + private Routine routine; + + private void initializeExpectedRoutine(int optionsCalls) { + expect(serviceMockReturnsOptions.getOptions()).andReturn(mockOptions).times(optionsCalls); + replay(serviceMockReturnsOptions); + bigquery = createStrictMock(BigQuery.class); + expectedRoutine = new Routine(serviceMockReturnsOptions, new RoutineInfo.BuilderImpl(ROUTINE_INFO)); + } + + private void initializeRoutine() { + routine = new Routine(bigquery, new RoutineInfo.BuilderImpl(ROUTINE_INFO)); + } + + private void tearDown() throws Exception { + verify(bigquery, serviceMockReturnsOptions); + } + + @Test + public void testBuilder() { + initializeExpectedRoutine(2); + replay(bigquery); + Routine builtRoutine = + new Routine.Builder(serviceMockReturnsOptions, ROUTINE_ID) + .setEtag(ETAG) + .setRoutineType(ROUTINE_TYPE) + .setCreationTime(CREATION_TIME) + .setLastModifiedTime(LAST_MODIFIED_TIME) + .setLanguage(LANGUAGE) + .setArguments(ARGUMENT_LIST) + .setReturnType(RETURN_TYPE) + .setImportedLibraries(IMPORTED_LIBRARIES) + .setBody(BODY) + .build(); + assertEquals(ETAG, builtRoutine.getEtag()); + assertSame(serviceMockReturnsOptions, builtRoutine.getBigQuery()); + } + + @Test + public void testToBuilder() { + initializeExpectedRoutine(2); + replay(bigquery); + compareRoutineInfo(expectedRoutine, expectedRoutine.toBuilder().build()); + } + + @Test + public void testExists_True() throws Exception { + initializeExpectedRoutine(1); + BigQuery.RoutineOption[] expectedOptions = {BigQuery.RoutineOption.fields()}; + expect(bigquery.getOptions()).andReturn(mockOptions); + expect(bigquery.getRoutine(ROUTINE_INFO.getRoutineId(), expectedOptions)).andReturn(null); + replay(bigquery); + initializeRoutine(); + assertFalse(routine.exists()); + } + + @Test + public void testExists_False() throws Exception { + initializeExpectedRoutine(1); + BigQuery.RoutineOption[] expectedOptions = {BigQuery.RoutineOption.fields()}; + expect(bigquery.getOptions()).andReturn(mockOptions); + expect(bigquery.getRoutine(ROUTINE_INFO.getRoutineId(), expectedOptions)).andReturn(expectedRoutine); + replay(bigquery); + initializeRoutine(); + assertTrue(routine.exists()); + } + + @Test + public void testReload() throws Exception { + initializeExpectedRoutine(4); + RoutineInfo updatedInfo = ROUTINE_INFO.toBuilder().setBody("body2").build(); + Routine expectedRoutine = + new Routine(serviceMockReturnsOptions, new RoutineInfo.BuilderImpl(updatedInfo)); + expect(bigquery.getOptions()).andReturn(mockOptions); + expect(bigquery.getRoutine(ROUTINE_INFO.getRoutineId())).andReturn(expectedRoutine); + replay(bigquery); + initializeRoutine(); + Routine updatedRoutine = routine.reload(); + compareRoutine(expectedRoutine, updatedRoutine); + } + + @Test + public void testReload_Null() throws Exception { + initializeExpectedRoutine(1); + expect(bigquery.getOptions()).andReturn(mockOptions); + expect(bigquery.getRoutine(ROUTINE_INFO.getRoutineId())).andReturn(null); + replay(bigquery); + initializeRoutine(); + assertNull(routine.reload()); + } + + @Test + public void testUpdate() { + initializeExpectedRoutine(4); + Routine expectedUpdatedRoutine = expectedRoutine.toBuilder().setBody("body2").build(); + expect(bigquery.getOptions()).andReturn(mockOptions); + expect(bigquery.update(eq(expectedRoutine))).andReturn(expectedUpdatedRoutine); + replay(bigquery); + initializeRoutine(); + Routine actualUpdatedRoutine = routine.update(); + compareRoutine(expectedUpdatedRoutine, actualUpdatedRoutine); + } + + @Test + public void testUpdateWithOptions() { + initializeExpectedRoutine(4); + Routine expectedUpdatedRoutine = expectedRoutine.toBuilder().setBody("body2").build(); + expect(bigquery.getOptions()).andReturn(mockOptions); + expect(bigquery.update(eq(expectedRoutine), eq(BigQuery.RoutineOption.fields()))) + .andReturn(expectedUpdatedRoutine); + replay(bigquery); + initializeRoutine(); + Routine actualUpdatedRoutine = routine.update(BigQuery.RoutineOption.fields()); + compareRoutine(expectedUpdatedRoutine, actualUpdatedRoutine); + } + + @Test + public void testDeleteTrue() { + initializeExpectedRoutine(1); + expect(bigquery.getOptions()).andReturn(mockOptions); + expect(bigquery.delete(ROUTINE_INFO.getRoutineId())).andReturn(true); + replay(bigquery); + initializeRoutine(); + assertTrue(routine.delete()); + } + + @Test + public void testDeleteFalse() { + initializeExpectedRoutine(1); + expect(bigquery.getOptions()).andReturn(mockOptions); + expect(bigquery.delete(ROUTINE_INFO.getRoutineId())).andReturn(false); + replay(bigquery); + initializeRoutine(); + assertFalse(routine.delete()); + } + + private void compareRoutine(Routine expected, Routine value) { + assertEquals(expected, value); + compareRoutineInfo(expected, value); + assertEquals(expected.getBigQuery().getOptions(), value.getBigQuery().getOptions()); + } + + public void compareRoutineInfo(RoutineInfo expected, RoutineInfo value) { + assertEquals(expected, value); + assertEquals(expected.getRoutineId(), value.getRoutineId()); + assertEquals(expected.getEtag(), value.getEtag()); + assertEquals(expected.getRoutineType(), value.getRoutineType()); + assertEquals(expected.getCreationTime(), value.getCreationTime()); + assertEquals(expected.getLastModifiedTime(), value.getLastModifiedTime()); + assertEquals(expected.getLanguage(), value.getLanguage()); + assertEquals(expected.getArguments(), value.getArguments()); + assertEquals(expected.getReturnType(), value.getReturnType()); + assertEquals(expected.getImportedLibraries(), value.getImportedLibraries()); + assertEquals(expected.getBody(), value.getBody()); + assertEquals(expected.hashCode(), value.hashCode()); + } + +} diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index 2c8fbc361b5b..7d710b5c5b8e 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -1142,8 +1142,13 @@ public void testRoutineLifecycle() throws InterruptedException { assertEquals(routine.getRoutineType(), "SCALAR_FUNCTION"); // Mutate metadata. - RoutineInfo info = routine.toBuilder().setBody("x * 4").build(); - Routine afterUpdate = bigquery.update(info); + RoutineInfo newInfo = routine.toBuilder() + .setBody("x * 4") + .setReturnType(routine.getReturnType()) + .setArguments(routine.getArguments()) + .setRoutineType(routine.getRoutineType()) + .build(); + Routine afterUpdate = bigquery.update(newInfo); assertEquals(afterUpdate.getBody(), "x * 4"); // Ensure routine is present in listRoutines. From 3e2cb681a4c033d52c7edc6ea7b326769ec81c87 Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Fri, 28 Jun 2019 11:21:15 -0700 Subject: [PATCH 09/16] add ddlTargetRoutine (and missing enums). more testing. --- .../google/cloud/bigquery/JobStatistics.java | 24 ++++++++++ .../cloud/bigquery/RoutineArgument.java | 2 +- .../cloud/bigquery/JobStatisticsTest.java | 5 ++ .../cloud/bigquery/it/ITBigQueryTest.java | 48 ++++++++++++------- 4 files changed, 62 insertions(+), 17 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobStatistics.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobStatistics.java index ac6e4a85750a..29bfa75ac6c0 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobStatistics.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobStatistics.java @@ -317,6 +317,7 @@ public static class QueryStatistics extends JobStatistics { private final Boolean cacheHit; private final String ddlOperationPerformed; private final TableId ddlTargetTable; + private final RoutineId ddlTargetRoutine; private final Long estimatedBytesProcessed; private final Long numDmlAffectedRows; private final List referencedTables; @@ -355,8 +356,15 @@ public StatementType apply(String constant) { public static final StatementType CREATE_TABLE_AS_SELECT = type.createAndRegister("CREATE_TABLE_AS_SELECT"); public static final StatementType CREATE_VIEW = type.createAndRegister("CREATE_VIEW"); + public static final StatementType CREATE_MODEL = type.createAndRegister("CREATE_MODEL"); + public static final StatementType CREATE_FUNCTION = type.createAndRegister("CREATE_FUNCTION"); + public static final StatementType CREATE_PROCEDURE = type.createAndRegister("CREATE_PROCEDURE"); + public static final StatementType ALTER_TABLE = type.createAndRegister("ALTER_TABLE"); + public static final StatementType ALTER_VIEW = type.createAndRegister("ALTER_VIEW"); public static final StatementType DROP_TABLE = type.createAndRegister("DROP_TABLE"); public static final StatementType DROP_VIEW = type.createAndRegister("DROP_VIEW"); + public static final StatementType DROP_FUNCTION = type.createAndRegister("DROP_FUNCTION"); + public static final StatementType DROP_PROCEDURE = type.createAndRegister("DROP_PROCEDURE"); public static final StatementType MERGE = type.createAndRegister("MERGE"); private StatementType(String constant) { @@ -388,6 +396,7 @@ static final class Builder extends JobStatistics.Builder referencedTables; @@ -411,6 +420,9 @@ private Builder(com.google.api.services.bigquery.model.JobStatistics statisticsP if (statisticsPb.getQuery().getDdlTargetTable() != null) { this.ddlTargetTable = TableId.fromPb(statisticsPb.getQuery().getDdlTargetTable()); } + if (statisticsPb.getQuery().getDdlTargetRoutine() != null) { + this.ddlTargetRoutine = RoutineId.fromPb(statisticsPb.getQuery().getDdlTargetRoutine()); + } this.estimatedBytesProcessed = statisticsPb.getQuery().getEstimatedBytesProcessed(); this.numDmlAffectedRows = statisticsPb.getQuery().getNumDmlAffectedRows(); this.totalBytesBilled = statisticsPb.getQuery().getTotalBytesBilled(); @@ -462,6 +474,11 @@ Builder setDDLTargetTable(TableId ddlTargetTable) { return self(); } + Builder setDDLTargetRoutine(RoutineId ddlTargetRoutine) { + this.ddlTargetRoutine = ddlTargetRoutine; + return self(); + } + Builder setEstimatedBytesProcessed(Long estimatedBytesProcessed) { this.estimatedBytesProcessed = estimatedBytesProcessed; return self(); @@ -534,6 +551,7 @@ private QueryStatistics(Builder builder) { this.cacheHit = builder.cacheHit; this.ddlOperationPerformed = builder.ddlOperationPerformed; this.ddlTargetTable = builder.ddlTargetTable; + this.ddlTargetRoutine = builder.ddlTargetRoutine; this.estimatedBytesProcessed = builder.estimatedBytesProcessed; this.numDmlAffectedRows = builder.numDmlAffectedRows; this.referencedTables = builder.referencedTables; @@ -571,6 +589,9 @@ public TableId getDdlTargetTable() { return ddlTargetTable; } + /** [BETA] For DDL queries, returns the RoutineId of the targeted routine. */ + public RoutineId getDdlTargetRoutine() { return ddlTargetRoutine; } + /** The original estimate of bytes processed for the job. */ public Long getEstimatedBytesProcessed() { return estimatedBytesProcessed; @@ -696,6 +717,9 @@ com.google.api.services.bigquery.model.JobStatistics toPb() { if (ddlTargetTable != null) { queryStatisticsPb.setDdlTargetTable(ddlTargetTable.toPb()); } + if (ddlTargetRoutine != null) { + queryStatisticsPb.setDdlTargetRoutine(ddlTargetRoutine.toPb()); + } if (referencedTables != null) { queryStatisticsPb.setReferencedTables( diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineArgument.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineArgument.java index 08480e243d9c..2d9d13ec34c4 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineArgument.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineArgument.java @@ -64,7 +64,7 @@ public abstract static class Builder { public abstract Builder toBuilder(); - static Builder newBuilder() { return new AutoValue_RoutineArgument.Builder(); } + public static Builder newBuilder() { return new AutoValue_RoutineArgument.Builder(); } Argument toPb() { Argument argumentPb = new Argument() diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java index 891deb0049b9..004e85f40945 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java @@ -34,6 +34,7 @@ public class JobStatisticsTest { private static final Boolean CACHE_HIT = true; private static final String DDL_OPERATION_PERFORMED = "SKIP"; private static final TableId DDL_TARGET_TABLE = TableId.of("foo", "bar", "baz"); + private static final RoutineId DDL_TARGET_ROUTINE = RoutineId.of("alpha","beta", "gamma"); private static final Long ESTIMATE_BYTES_PROCESSED = 101L; private static final Long NUM_DML_AFFECTED_ROWS = 88L; private static final QueryStatistics.StatementType STATEMENT_TYPE = @@ -136,6 +137,7 @@ public class JobStatisticsTest { .setCacheHit(CACHE_HIT) .setDDLOperationPerformed(DDL_OPERATION_PERFORMED) .setDDLTargetTable(DDL_TARGET_TABLE) + .setDDLTargetRoutine(DDL_TARGET_ROUTINE) .setEstimatedBytesProcessed(ESTIMATE_BYTES_PROCESSED) .setNumDmlAffectedRows(NUM_DML_AFFECTED_ROWS) .setReferenceTables(REFERENCED_TABLES) @@ -180,6 +182,7 @@ public void testBuilder() { assertEquals(CACHE_HIT, QUERY_STATISTICS.getCacheHit()); assertEquals(DDL_OPERATION_PERFORMED, QUERY_STATISTICS.getDdlOperationPerformed()); assertEquals(DDL_TARGET_TABLE, QUERY_STATISTICS.getDdlTargetTable()); + assertEquals(DDL_TARGET_ROUTINE, QUERY_STATISTICS.getDdlTargetRoutine()); assertEquals(ESTIMATE_BYTES_PROCESSED, QUERY_STATISTICS.getEstimatedBytesProcessed()); assertEquals(NUM_DML_AFFECTED_ROWS, QUERY_STATISTICS.getNumDmlAffectedRows()); assertEquals(REFERENCED_TABLES, QUERY_STATISTICS.getReferencedTables()); @@ -207,6 +210,7 @@ public void testBuilder() { assertEquals(CACHE_HIT, QUERY_STATISTICS_INCOMPLETE.getCacheHit()); assertEquals(null, QUERY_STATISTICS_INCOMPLETE.getDdlOperationPerformed()); assertEquals(null, QUERY_STATISTICS_INCOMPLETE.getDdlTargetTable()); + assertEquals(null, QUERY_STATISTICS_INCOMPLETE.getDdlTargetRoutine()); assertEquals(null, QUERY_STATISTICS_INCOMPLETE.getEstimatedBytesProcessed()); assertEquals(null, QUERY_STATISTICS_INCOMPLETE.getNumDmlAffectedRows()); assertEquals(null, QUERY_STATISTICS_INCOMPLETE.getTotalBytesBilled()); @@ -284,6 +288,7 @@ private void compareQueryStatistics(QueryStatistics expected, QueryStatistics va assertEquals(expected.getCacheHit(), value.getCacheHit()); assertEquals(expected.getDdlOperationPerformed(), value.getDdlOperationPerformed()); assertEquals(expected.getDdlTargetTable(), value.getDdlTargetTable()); + assertEquals(expected.getDdlTargetRoutine(), value.getDdlTargetRoutine()); assertEquals(expected.getEstimatedBytesProcessed(), value.getEstimatedBytesProcessed()); assertEquals(expected.getTotalBytesBilled(), value.getTotalBytesBilled()); assertEquals(expected.getTotalBytesProcessed(), value.getTotalBytesProcessed()); diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index 7d710b5c5b8e..e161b9032d82 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -16,16 +16,6 @@ package com.google.cloud.bigquery.it; -import static com.google.cloud.bigquery.JobStatus.State.DONE; -import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - import com.google.api.gax.paging.Page; import com.google.cloud.Date; import com.google.cloud.RetryOption; @@ -66,9 +56,11 @@ import com.google.cloud.bigquery.QueryJobConfiguration; import com.google.cloud.bigquery.QueryParameterValue; import com.google.cloud.bigquery.Routine; +import com.google.cloud.bigquery.RoutineArgument; import com.google.cloud.bigquery.RoutineId; import com.google.cloud.bigquery.RoutineInfo; import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardSQLDataType; import com.google.cloud.bigquery.StandardTableDefinition; import com.google.cloud.bigquery.Table; import com.google.cloud.bigquery.TableDataWriteChannel; @@ -91,6 +83,13 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Sets; import com.google.common.io.BaseEncoding; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.Timeout; +import org.threeten.bp.Duration; + import java.io.IOException; import java.math.BigDecimal; import java.nio.ByteBuffer; @@ -108,12 +107,10 @@ import java.util.concurrent.TimeoutException; import java.util.logging.Level; import java.util.logging.Logger; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.Timeout; -import org.threeten.bp.Duration; + +import static com.google.cloud.bigquery.JobStatus.State.DONE; +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.*; public class ITBigQueryTest { @@ -1166,7 +1163,26 @@ public void testRoutineLifecycle() throws InterruptedException { assertTrue(bigquery.delete(routineId)); } + @Test + public void testRoutineAPICreation() { + String routineName = RemoteBigQueryHelper.generateRoutineName(); + RoutineId routineId = RoutineId.of(ROUTINE_DATASET, routineName); + RoutineInfo routineInfo = RoutineInfo.newBuilder(routineId) + .setRoutineType("SCALAR_FUNCTION") + .setBody("x * 3") + .setLanguage("SQL") + .setArguments( + ImmutableList.of( + RoutineArgument.newBuilder() + .setName("x") + .setDataType(StandardSQLDataType.newBuilder("INT64").build()) + .build())) + .build(); + Routine routine = bigquery.create(routineInfo); + assertNotNull(routine); + assertEquals(routine.getRoutineType(), "SCALAR_FUNCTION"); + } @Test public void testQuery() throws InterruptedException { From c690d10dc57b90af1e7bd6a9cae7164411ad516e Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Fri, 28 Jun 2019 11:24:18 -0700 Subject: [PATCH 10/16] backout pom --- google-cloud-clients/google-cloud-bigquery/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-clients/google-cloud-bigquery/pom.xml b/google-cloud-clients/google-cloud-bigquery/pom.xml index 97b0d8960b74..8ba1725869b1 100644 --- a/google-cloud-clients/google-cloud-bigquery/pom.xml +++ b/google-cloud-clients/google-cloud-bigquery/pom.xml @@ -12,7 +12,7 @@ com.google.cloud google-cloud-clients - 0.97.0-alpha + 0.97.1-alpha-SNAPSHOT google-cloud-bigquery From b32129382ac09ac9eced6f86d8e2b43b4dacd407 Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Fri, 28 Jun 2019 11:35:45 -0700 Subject: [PATCH 11/16] cleanup RoutineField enum --- .../src/main/java/com/google/cloud/bigquery/BigQuery.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java index 179f040f18bc..60214d10376c 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java @@ -158,11 +158,16 @@ public String getSelector() { * Resource */ enum RoutineField implements FieldSelector { + ARGUMENTS("arguments"), CREATION_TIME("creationTime"), + DEFINITION_BODY("definitionBody"), ETAG("etag"), + IMPORTED_LIBRARIES("importedLibraries"), + LANGUAGE("language"), LAST_MODIFIED_TIME("lastModifiedTime"), + RETURN_TYPE("returnType"), ROUTINE_REFERENCE("routineReference"), - TYPE("modelType"); + ROUTINE_TYPE("routineType"); static final List REQUIRED_FIELDS = ImmutableList.of(ROUTINE_REFERENCE); From cc92f2d94cae0a9841a3386917ffe50869f8db82 Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Fri, 28 Jun 2019 12:36:19 -0700 Subject: [PATCH 12/16] add missing license headers, run code formatter --- .../com/google/cloud/bigquery/BigQuery.java | 12 +- .../google/cloud/bigquery/BigQueryImpl.java | 201 +++---- .../google/cloud/bigquery/JobStatistics.java | 7 +- .../com/google/cloud/bigquery/Routine.java | 238 +++++---- .../cloud/bigquery/RoutineArgument.java | 110 ++-- .../com/google/cloud/bigquery/RoutineId.java | 172 +++--- .../google/cloud/bigquery/RoutineInfo.java | 489 ++++++++++-------- .../cloud/bigquery/StandardSQLDataType.java | 103 ++-- .../cloud/bigquery/StandardSQLField.java | 99 ++-- .../cloud/bigquery/StandardSQLStructType.java | 55 +- .../cloud/bigquery/spi/v2/BigQueryRpc.java | 4 +- .../bigquery/spi/v2/HttpBigQueryRpc.java | 43 +- .../cloud/bigquery/JobStatisticsTest.java | 4 +- .../cloud/bigquery/RoutineArgumentTest.java | 80 +-- .../google/cloud/bigquery/RoutineIdTest.java | 77 ++- .../cloud/bigquery/RoutineInfoTest.java | 224 ++++---- .../google/cloud/bigquery/RoutineTest.java | 399 +++++++------- .../bigquery/StandardSQLDataTypeTest.java | 102 ++-- .../cloud/bigquery/StandardSQLFieldTest.java | 83 ++- .../bigquery/StandardSQLStructTypeTest.java | 76 ++- .../cloud/bigquery/it/ITBigQueryTest.java | 48 +- 21 files changed, 1331 insertions(+), 1295 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java index 60214d10376c..f6774b4dc0d9 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java @@ -169,7 +169,8 @@ enum RoutineField implements FieldSelector { ROUTINE_REFERENCE("routineReference"), ROUTINE_TYPE("routineType"); - static final List REQUIRED_FIELDS = ImmutableList.of(ROUTINE_REFERENCE); + static final List REQUIRED_FIELDS = + ImmutableList.of(ROUTINE_REFERENCE); private final String selector; @@ -398,7 +399,7 @@ private RoutineOption(BigQueryRpc.Option option, Object value) { */ public static RoutineOption fields(RoutineField... fields) { return new RoutineOption( - BigQueryRpc.Option.FIELDS, Helper.selector(RoutineField.REQUIRED_FIELDS, fields)); + BigQueryRpc.Option.FIELDS, Helper.selector(RoutineField.REQUIRED_FIELDS, fields)); } } @@ -657,8 +658,7 @@ public int hashCode() { Table create(TableInfo tableInfo, TableOption... options); /** - * Creates a new routine. - * TODO: docs + * Creates a new routine. TODO: docs * * @throws BigQueryException upon failure */ @@ -908,7 +908,6 @@ public int hashCode() { */ boolean delete(RoutineId routineId); - /** * Updates dataset information. * @@ -1005,8 +1004,7 @@ public int hashCode() { Model update(ModelInfo modelInfo, ModelOption... options); /** - * Updates routine information. - * TODO: docs + * Updates routine information. TODO: docs * * @throws BigQueryException upon failure */ diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java index 5219cdef91be..85fcadfdec14 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java @@ -133,12 +133,12 @@ private static class RoutinePageFetcher implements NextPageFetcher { private final DatasetId datasetId; RoutinePageFetcher( - DatasetId datasetId, - BigQueryOptions serviceOptions, - String cursor, - Map optionMap) { + DatasetId datasetId, + BigQueryOptions serviceOptions, + String cursor, + Map optionMap) { this.requestOptions = - PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap); + PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap); this.serviceOptions = serviceOptions; this.datasetId = datasetId; } @@ -253,26 +253,26 @@ public com.google.api.services.bigquery.model.Table call() { @Override public Routine create(RoutineInfo routineInfo, RoutineOption... options) { final com.google.api.services.bigquery.model.Routine routinePb = - routineInfo - .setProjectId( - Strings.isNullOrEmpty(routineInfo.getRoutineId().getProject()) - ? getOptions().getProjectId() - : routineInfo.getRoutineId().getProject()) - .toPb(); + routineInfo + .setProjectId( + Strings.isNullOrEmpty(routineInfo.getRoutineId().getProject()) + ? getOptions().getProjectId() + : routineInfo.getRoutineId().getProject()) + .toPb(); final Map optionsMap = optionMap(options); try { return Routine.fromPb( - this, - runWithRetries( - new Callable() { - @Override - public com.google.api.services.bigquery.model.Routine call() { - return bigQueryRpc.create(routinePb, optionsMap); - } - }, - getOptions().getRetrySettings(), - EXCEPTION_HANDLER, - getOptions().getClock())); + this, + runWithRetries( + new Callable() { + @Override + public com.google.api.services.bigquery.model.Routine call() { + return bigQueryRpc.create(routinePb, optionsMap); + } + }, + getOptions().getRetrySettings(), + EXCEPTION_HANDLER, + getOptions().getClock())); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } @@ -511,24 +511,24 @@ public Boolean call() { @Override public boolean delete(RoutineId routineId) { final RoutineId completeRoutineId = - routineId.setProjectId( - Strings.isNullOrEmpty(routineId.getProject()) - ? getOptions().getProjectId() - : routineId.getProject()); + routineId.setProjectId( + Strings.isNullOrEmpty(routineId.getProject()) + ? getOptions().getProjectId() + : routineId.getProject()); try { return runWithRetries( - new Callable() { - @Override - public Boolean call() { - return bigQueryRpc.deleteRoutine( - completeRoutineId.getProject(), - completeRoutineId.getDataset(), - completeRoutineId.getRoutine()); - } - }, - getOptions().getRetrySettings(), - EXCEPTION_HANDLER, - getOptions().getClock()); + new Callable() { + @Override + public Boolean call() { + return bigQueryRpc.deleteRoutine( + completeRoutineId.getProject(), + completeRoutineId.getDataset(), + completeRoutineId.getRoutine()); + } + }, + getOptions().getRetrySettings(), + EXCEPTION_HANDLER, + getOptions().getClock()); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } @@ -616,26 +616,26 @@ public com.google.api.services.bigquery.model.Model call() { @Override public Routine update(RoutineInfo routineInfo, RoutineOption... options) { final com.google.api.services.bigquery.model.Routine routinePb = - routineInfo - .setProjectId( - Strings.isNullOrEmpty(routineInfo.getRoutineId().getProject()) - ? getOptions().getProjectId() - : routineInfo.getRoutineId().getProject()) - .toPb(); + routineInfo + .setProjectId( + Strings.isNullOrEmpty(routineInfo.getRoutineId().getProject()) + ? getOptions().getProjectId() + : routineInfo.getRoutineId().getProject()) + .toPb(); final Map optionsMap = optionMap(options); try { return Routine.fromPb( - this, - runWithRetries( - new Callable() { - @Override - public com.google.api.services.bigquery.model.Routine call() { - return bigQueryRpc.update(routinePb, optionsMap); - } - }, - getOptions().getRetrySettings(), - EXCEPTION_HANDLER, - getOptions().getClock())); + this, + runWithRetries( + new Callable() { + @Override + public com.google.api.services.bigquery.model.Routine call() { + return bigQueryRpc.update(routinePb, optionsMap); + } + }, + getOptions().getRetrySettings(), + EXCEPTION_HANDLER, + getOptions().getClock())); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } @@ -726,27 +726,27 @@ public Routine getRoutine(String datasetId, String routineId, RoutineOption... o @Override public Routine getRoutine(RoutineId routineId, RoutineOption... options) { final RoutineId completeRoutineId = - routineId.setProjectId( - Strings.isNullOrEmpty(routineId.getProject()) - ? getOptions().getProjectId() - : routineId.getProject()); + routineId.setProjectId( + Strings.isNullOrEmpty(routineId.getProject()) + ? getOptions().getProjectId() + : routineId.getProject()); final Map optionsMap = optionMap(options); try { com.google.api.services.bigquery.model.Routine answer = - runWithRetries( - new Callable() { - @Override - public com.google.api.services.bigquery.model.Routine call() { - return bigQueryRpc.getRoutine( - completeRoutineId.getProject(), - completeRoutineId.getDataset(), - completeRoutineId.getRoutine(), - optionsMap); - } - }, - getOptions().getRetrySettings(), - EXCEPTION_HANDLER, - getOptions().getClock()); + runWithRetries( + new Callable() { + @Override + public com.google.api.services.bigquery.model.Routine call() { + return bigQueryRpc.getRoutine( + completeRoutineId.getProject(), + completeRoutineId.getDataset(), + completeRoutineId.getRoutine(), + optionsMap); + } + }, + getOptions().getRetrySettings(), + EXCEPTION_HANDLER, + getOptions().getClock()); if (getOptions().getThrowNotFound() && answer == null) { throw new BigQueryException(HTTP_NOT_FOUND, "Model not found"); } @@ -782,7 +782,8 @@ public Page listModels(DatasetId datasetId, ModelListOption... options) { @Override public Page listRoutines(String datasetId, RoutineListOption... options) { - return listRoutines(DatasetId.of(getOptions().getProjectId(), datasetId), getOptions(), optionMap(options)); + return listRoutines( + DatasetId.of(getOptions().getProjectId(), datasetId), getOptions(), optionMap(options)); } @Override @@ -886,37 +887,37 @@ public Model apply(com.google.api.services.bigquery.model.Model model) { } private static Page listRoutines( - final DatasetId datasetId, - final BigQueryOptions serviceOptions, - final Map optionsMap) { + final DatasetId datasetId, + final BigQueryOptions serviceOptions, + final Map optionsMap) { try { Tuple> result = - runWithRetries( - new Callable< - Tuple>>() { - @Override - public Tuple> - call() { - return serviceOptions - .getBigQueryRpcV2() - .listRoutines(datasetId.getProject(), datasetId.getDataset(), optionsMap); - } - }, - serviceOptions.getRetrySettings(), - EXCEPTION_HANDLER, - serviceOptions.getClock()); + runWithRetries( + new Callable< + Tuple>>() { + @Override + public Tuple> + call() { + return serviceOptions + .getBigQueryRpcV2() + .listRoutines(datasetId.getProject(), datasetId.getDataset(), optionsMap); + } + }, + serviceOptions.getRetrySettings(), + EXCEPTION_HANDLER, + serviceOptions.getClock()); String cursor = result.x(); Iterable routines = - Iterables.transform( - result.y(), - new Function() { - @Override - public Routine apply(com.google.api.services.bigquery.model.Routine routinePb) { - return Routine.fromPb(serviceOptions.getService(), routinePb); - } - }); + Iterables.transform( + result.y(), + new Function() { + @Override + public Routine apply(com.google.api.services.bigquery.model.Routine routinePb) { + return Routine.fromPb(serviceOptions.getService(), routinePb); + } + }); return new PageImpl<>( - new RoutinePageFetcher(datasetId, serviceOptions, cursor, optionsMap), cursor, routines); + new RoutinePageFetcher(datasetId, serviceOptions, cursor, optionsMap), cursor, routines); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobStatistics.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobStatistics.java index 29bfa75ac6c0..388284c04aa3 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobStatistics.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobStatistics.java @@ -358,7 +358,8 @@ public StatementType apply(String constant) { public static final StatementType CREATE_VIEW = type.createAndRegister("CREATE_VIEW"); public static final StatementType CREATE_MODEL = type.createAndRegister("CREATE_MODEL"); public static final StatementType CREATE_FUNCTION = type.createAndRegister("CREATE_FUNCTION"); - public static final StatementType CREATE_PROCEDURE = type.createAndRegister("CREATE_PROCEDURE"); + public static final StatementType CREATE_PROCEDURE = + type.createAndRegister("CREATE_PROCEDURE"); public static final StatementType ALTER_TABLE = type.createAndRegister("ALTER_TABLE"); public static final StatementType ALTER_VIEW = type.createAndRegister("ALTER_VIEW"); public static final StatementType DROP_TABLE = type.createAndRegister("DROP_TABLE"); @@ -590,7 +591,9 @@ public TableId getDdlTargetTable() { } /** [BETA] For DDL queries, returns the RoutineId of the targeted routine. */ - public RoutineId getDdlTargetRoutine() { return ddlTargetRoutine; } + public RoutineId getDdlTargetRoutine() { + return ddlTargetRoutine; + } /** The original estimate of bytes processed for the job. */ public Long getEstimatedBytesProcessed() { diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java index 887a505be950..260777db27e2 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java @@ -15,16 +15,14 @@ */ package com.google.cloud.bigquery; +import static com.google.common.base.Preconditions.checkNotNull; +import com.google.cloud.bigquery.BigQuery.RoutineOption; import java.io.IOException; import java.io.ObjectInputStream; import java.util.List; import java.util.Objects; -import static com.google.common.base.Preconditions.checkNotNull; -import com.google.cloud.bigquery.BigQuery.RoutineOption; - - /** * A Google BigQuery Routine. * @@ -34,135 +32,145 @@ */ public class Routine extends RoutineInfo { - private final BigQueryOptions options; - private transient BigQuery bigquery; - - public static class Builder extends RoutineInfo.Builder { - - private final BigQuery bigquery; - private final RoutineInfo.BuilderImpl infoBuilder; - - Builder(BigQuery bigquery, RoutineId routineId) { - this.bigquery = bigquery; - this.infoBuilder = new RoutineInfo.BuilderImpl(); - this.infoBuilder.setRoutineId(routineId); - } - - Builder(Routine routine) { - this.bigquery = routine.bigquery; - this.infoBuilder = new RoutineInfo.BuilderImpl(routine); - } - - @Override - Builder setRoutineId(RoutineId id) { - infoBuilder.setRoutineId(id); - return this; - } - - @Override - Builder setEtag(String etag) { - infoBuilder.setEtag(etag); - return this; - } - - @Override - public Builder setRoutineType(String routineType) { - infoBuilder.setRoutineType(routineType); - return this; - } - - @Override - Builder setCreationTime(Long creationMillis) { - infoBuilder.setCreationTime(creationMillis); - return this; - } - - @Override - Builder setLastModifiedTime(Long lastModifiedMillis) { - infoBuilder.setLastModifiedTime(lastModifiedMillis); - return this; - } - - @Override - public Builder setLanguage(String language) { - infoBuilder.setLanguage(language); - return this; - } - - @Override - public Builder setArguments(List arguments) { - infoBuilder.setArguments(arguments); - return this; - } - - @Override - public Builder setReturnType(StandardSQLDataType returnType) { - infoBuilder.setReturnType(returnType); - return this; - } - - @Override - public Builder setImportedLibraries(List libraries) { - infoBuilder.setImportedLibraries(libraries); - return this; - } - - @Override - public Builder setBody(String body) { - infoBuilder.setBody(body); - return this; - } - - @Override - public Routine build() { - return new Routine(bigquery, infoBuilder); - } - } + private final BigQueryOptions options; + private transient BigQuery bigquery; - Routine(BigQuery bigquery, RoutineInfo.BuilderImpl infoBuilder) { - super(infoBuilder); - this.bigquery = checkNotNull(bigquery); - this.options = bigquery.getOptions(); - } + public static class Builder extends RoutineInfo.Builder { + private final BigQuery bigquery; + private final RoutineInfo.BuilderImpl infoBuilder; - public boolean exists() { return bigquery.getRoutine(getRoutineId(), RoutineOption.fields()) != null; } + Builder(BigQuery bigquery, RoutineId routineId) { + this.bigquery = bigquery; + this.infoBuilder = new RoutineInfo.BuilderImpl(); + this.infoBuilder.setRoutineId(routineId); + } - public Routine reload(RoutineOption... options) { return bigquery.getRoutine(getRoutineId(), options); } + Builder(Routine routine) { + this.bigquery = routine.bigquery; + this.infoBuilder = new RoutineInfo.BuilderImpl(routine); + } - public Routine update(RoutineOption... options) { return bigquery.update(this, options); } + @Override + Builder setRoutineId(RoutineId id) { + infoBuilder.setRoutineId(id); + return this; + } - public boolean delete() { return bigquery.delete(getRoutineId()); } + @Override + Builder setEtag(String etag) { + infoBuilder.setEtag(etag); + return this; + } + @Override + public Builder setRoutineType(String routineType) { + infoBuilder.setRoutineType(routineType); + return this; + } - public BigQuery getBigQuery() { return bigquery; } + @Override + Builder setCreationTime(Long creationMillis) { + infoBuilder.setCreationTime(creationMillis); + return this; + } @Override - public Builder toBuilder() { - return new Builder(this); + Builder setLastModifiedTime(Long lastModifiedMillis) { + infoBuilder.setLastModifiedTime(lastModifiedMillis); + return this; } @Override - public final boolean equals(Object obj) { - if (obj == this) { - return true; - } - if (obj == null || !obj.getClass().equals(Routine.class)) { - return false; - } - Routine other = (Routine) obj; - return Objects.equals(toPb(), other.toPb()) && Objects.equals(options, other.options); + public Builder setLanguage(String language) { + infoBuilder.setLanguage(language); + return this; } - public final int hashCode() { return Objects.hash(super.hashCode(), options); } + @Override + public Builder setArguments(List arguments) { + infoBuilder.setArguments(arguments); + return this; + } + + @Override + public Builder setReturnType(StandardSQLDataType returnType) { + infoBuilder.setReturnType(returnType); + return this; + } - public void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { - in.defaultReadObject(); - this.bigquery = options.getService(); + @Override + public Builder setImportedLibraries(List libraries) { + infoBuilder.setImportedLibraries(libraries); + return this; } - static Routine fromPb(BigQuery bigquery, com.google.api.services.bigquery.model.Routine routinePb) { - return new Routine(bigquery, new RoutineInfo.BuilderImpl(routinePb)); + @Override + public Builder setBody(String body) { + infoBuilder.setBody(body); + return this; } + @Override + public Routine build() { + return new Routine(bigquery, infoBuilder); + } + } + + Routine(BigQuery bigquery, RoutineInfo.BuilderImpl infoBuilder) { + super(infoBuilder); + this.bigquery = checkNotNull(bigquery); + this.options = bigquery.getOptions(); + } + + public boolean exists() { + return bigquery.getRoutine(getRoutineId(), RoutineOption.fields()) != null; + } + + public Routine reload(RoutineOption... options) { + return bigquery.getRoutine(getRoutineId(), options); + } + + public Routine update(RoutineOption... options) { + return bigquery.update(this, options); + } + + public boolean delete() { + return bigquery.delete(getRoutineId()); + } + + public BigQuery getBigQuery() { + return bigquery; + } + + @Override + public Builder toBuilder() { + return new Builder(this); + } + + @Override + public final boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || !obj.getClass().equals(Routine.class)) { + return false; + } + Routine other = (Routine) obj; + return Objects.equals(toPb(), other.toPb()) && Objects.equals(options, other.options); + } + + public final int hashCode() { + return Objects.hash(super.hashCode(), options); + } + + public void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { + in.defaultReadObject(); + this.bigquery = options.getService(); + } + + static Routine fromPb( + BigQuery bigquery, com.google.api.services.bigquery.model.Routine routinePb) { + return new Routine(bigquery, new RoutineInfo.BuilderImpl(routinePb)); + } } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineArgument.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineArgument.java index 2d9d13ec34c4..6c6ba2cd97d3 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineArgument.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineArgument.java @@ -18,74 +18,74 @@ import com.google.api.services.bigquery.model.Argument; import com.google.auto.value.AutoValue; import com.google.common.base.Function; - import javax.annotation.Nullable; -import java.io.Serializable; -import java.util.Objects; @AutoValue public abstract class RoutineArgument { - static final Function FROM_PB_FUNCTION = - new Function() { - @Override - public RoutineArgument apply(Argument pb) { - return RoutineArgument.fromPb(pb); - } - }; - static final Function TO_PB_FUNCTION = - new Function() { - @Override - public Argument apply(RoutineArgument argument) { - return argument.toPb(); - } - }; - - @AutoValue.Builder - public abstract static class Builder { - public abstract Builder setName(String name); - public abstract Builder setKind(String kind); - public abstract Builder setMode(String mode); - public abstract Builder setDataType(StandardSQLDataType dataType); - public abstract RoutineArgument build(); - } + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public RoutineArgument apply(Argument pb) { + return RoutineArgument.fromPb(pb); + } + }; + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public Argument apply(RoutineArgument argument) { + return argument.toPb(); + } + }; - @Nullable - public abstract String getName(); + @AutoValue.Builder + public abstract static class Builder { + public abstract Builder setName(String name); - @Nullable - public abstract String getKind(); + public abstract Builder setKind(String kind); - @Nullable - public abstract String getMode(); + public abstract Builder setMode(String mode); - @Nullable - public abstract StandardSQLDataType getDataType(); + public abstract Builder setDataType(StandardSQLDataType dataType); - public abstract Builder toBuilder(); + public abstract RoutineArgument build(); + } - public static Builder newBuilder() { return new AutoValue_RoutineArgument.Builder(); } + @Nullable + public abstract String getName(); - Argument toPb() { - Argument argumentPb = new Argument() - .setName(getName()) - .setArgumentKind(getKind()) - .setMode(getMode()); - if (getDataType() != null) { - argumentPb.setDataType(getDataType().toPb()); - } - return argumentPb; - } + @Nullable + public abstract String getKind(); - static RoutineArgument fromPb(Argument argumentPb) { - Builder builder = newBuilder(); - builder.setName(argumentPb.getName()); - builder.setKind(argumentPb.getArgumentKind()); - builder.setMode(argumentPb.getMode()); - if (argumentPb.getDataType() != null) { - builder.setDataType(StandardSQLDataType.fromPb(argumentPb.getDataType())); - } - return builder.build(); + @Nullable + public abstract String getMode(); + + @Nullable + public abstract StandardSQLDataType getDataType(); + + public abstract Builder toBuilder(); + + public static Builder newBuilder() { + return new AutoValue_RoutineArgument.Builder(); + } + + Argument toPb() { + Argument argumentPb = + new Argument().setName(getName()).setArgumentKind(getKind()).setMode(getMode()); + if (getDataType() != null) { + argumentPb.setDataType(getDataType().toPb()); } + return argumentPb; + } + static RoutineArgument fromPb(Argument argumentPb) { + Builder builder = newBuilder(); + builder.setName(argumentPb.getName()); + builder.setKind(argumentPb.getArgumentKind()); + builder.setMode(argumentPb.getMode()); + if (argumentPb.getDataType() != null) { + builder.setDataType(StandardSQLDataType.fromPb(argumentPb.getDataType())); + } + return builder.build(); + } } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineId.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineId.java index 5c885fde4307..5230282e3199 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineId.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineId.java @@ -16,93 +16,95 @@ package com.google.cloud.bigquery; - import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkNotNull; - import com.google.api.services.bigquery.model.RoutineReference; - import com.google.common.base.Function; - import com.google.common.base.Preconditions; - import com.google.common.base.Strings; - import java.io.Serializable; - import java.util.Objects; +import com.google.api.services.bigquery.model.RoutineReference; +import com.google.common.base.Function; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; +import java.io.Serializable; +import java.util.Objects; public final class RoutineId implements Serializable { - static final Function FROM_PB_FUNCTION = - new Function() { - @Override - public RoutineId apply(RoutineReference pb) { - return RoutineId.fromPb(pb); - } - }; - static final Function TO_PB_FUNCTION = - new Function() { - @Override - public RoutineReference apply(RoutineId routineId) { - return routineId.toPb(); - } - }; - - private final String project; - private final String dataset; - private final String routine; - - /** Return corresponding project ID for this routine. * */ - public String getProject() { - return project; - } - - /** Return corresponding dataset ID for this routine. * */ - public String getDataset() { - return dataset; - } - - /** Return corresponding routine ID for this routine. * */ - public String getRoutine() { - return routine; - } - - private RoutineId(String project, String dataset, String routine) { - this.project = project; - this.dataset = dataset; - this.routine = routine; - } - - /** Creates a routine identity given project, dataset, and routine identifiers. * */ - public static RoutineId of(String project, String dataset, String routine) { - return new RoutineId(checkNotNull(project), checkNotNull(dataset), checkNotNull(routine)); - } - - /** Creates a routine identity given dataset and routine identifiers. * */ - public static RoutineId of(String dataset, String routine) { - return new RoutineId(null, checkNotNull(dataset), checkNotNull(routine)); - } - - @Override - public boolean equals(Object obj) { - return obj == this || obj instanceof RoutineId && Objects.equals(toPb(), ((RoutineId) obj).toPb()); - } - - @Override - public int hashCode() { - return Objects.hash(project, dataset, routine); - } - - @Override - public String toString() { - return toPb().toString(); - } - - RoutineId setProjectId(String projectId) { - Preconditions.checkArgument( - !Strings.isNullOrEmpty(projectId), "Provided projectId is null or empty"); - return RoutineId.of(projectId, getDataset(), getRoutine()); - } - - RoutineReference toPb() { - return new RoutineReference().setProjectId(project).setDatasetId(dataset).setRoutineId(routine); - } - - static RoutineId fromPb(RoutineReference routineRef) { - return new RoutineId(routineRef.getProjectId(), routineRef.getDatasetId(), routineRef.getRoutineId()); - } + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public RoutineId apply(RoutineReference pb) { + return RoutineId.fromPb(pb); + } + }; + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public RoutineReference apply(RoutineId routineId) { + return routineId.toPb(); + } + }; + + private final String project; + private final String dataset; + private final String routine; + + /** Return corresponding project ID for this routine. * */ + public String getProject() { + return project; + } + + /** Return corresponding dataset ID for this routine. * */ + public String getDataset() { + return dataset; + } + + /** Return corresponding routine ID for this routine. * */ + public String getRoutine() { + return routine; + } + + private RoutineId(String project, String dataset, String routine) { + this.project = project; + this.dataset = dataset; + this.routine = routine; + } + + /** Creates a routine identity given project, dataset, and routine identifiers. * */ + public static RoutineId of(String project, String dataset, String routine) { + return new RoutineId(checkNotNull(project), checkNotNull(dataset), checkNotNull(routine)); + } + + /** Creates a routine identity given dataset and routine identifiers. * */ + public static RoutineId of(String dataset, String routine) { + return new RoutineId(null, checkNotNull(dataset), checkNotNull(routine)); + } + + @Override + public boolean equals(Object obj) { + return obj == this + || obj instanceof RoutineId && Objects.equals(toPb(), ((RoutineId) obj).toPb()); + } + + @Override + public int hashCode() { + return Objects.hash(project, dataset, routine); + } + + @Override + public String toString() { + return toPb().toString(); + } + + RoutineId setProjectId(String projectId) { + Preconditions.checkArgument( + !Strings.isNullOrEmpty(projectId), "Provided projectId is null or empty"); + return RoutineId.of(projectId, getDataset(), getRoutine()); + } + + RoutineReference toPb() { + return new RoutineReference().setProjectId(project).setDatasetId(dataset).setRoutineId(routine); + } + + static RoutineId fromPb(RoutineReference routineRef) { + return new RoutineId( + routineRef.getProjectId(), routineRef.getDatasetId(), routineRef.getRoutineId()); + } } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java index a070ba351c68..7074343a1048 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java @@ -15,288 +15,317 @@ */ package com.google.cloud.bigquery; +import static com.google.common.base.Preconditions.checkNotNull; + import com.google.api.services.bigquery.model.Routine; import com.google.common.base.Function; import com.google.common.base.MoreObjects; import com.google.common.base.Strings; import com.google.common.collect.Lists; - import java.io.Serializable; import java.util.Collections; import java.util.List; import java.util.Objects; -import static com.google.common.base.Preconditions.checkNotNull; - public class RoutineInfo implements Serializable { - static final Function FROM_PB_FUNCTION = - new Function() { - @Override - public RoutineInfo apply(Routine pb) { - return RoutineInfo.fromPb(pb); - } - }; - static final Function TO_PB_FUNCTION = - new Function() { - @Override - public Routine apply(RoutineInfo routineInfo) { - return routineInfo.toPb(); - } - }; - - - private final RoutineId routineId; - private final String etag; - private final String routineType; - private final Long creationTime; - private final Long lastModifiedTime; - private final String language; - private final List argumentList; - private final StandardSQLDataType returnType; - private final List importedLibrariesList; - private final String body; - - public abstract static class Builder { - abstract Builder setRoutineId(RoutineId id); - abstract Builder setEtag(String etag); - public abstract Builder setRoutineType(String routineType); - abstract Builder setCreationTime(Long creationMillis); - abstract Builder setLastModifiedTime(Long lastModifiedMillis); - public abstract Builder setLanguage(String language); - public abstract Builder setArguments(List argumentList); - public abstract Builder setReturnType(StandardSQLDataType returnType); - public abstract Builder setImportedLibraries(List importedLibrariesList); - public abstract Builder setBody(String body); - public abstract RoutineInfo build(); - } - - static class BuilderImpl extends Builder { - private RoutineId routineId; - private String etag; - private String routineType; - private Long creationTime; - private Long lastModifiedTime; - private String language; - private List argumentList; - private StandardSQLDataType returnType; - private List importedLibrariesList; - private String body; - - BuilderImpl() {} - - BuilderImpl(RoutineInfo routineInfo) { - this.routineId = routineInfo.routineId; - this.etag = routineInfo.etag; - this.routineType = routineInfo.routineType; - this.creationTime = routineInfo.creationTime; - this.lastModifiedTime = routineInfo.lastModifiedTime; - this.language = routineInfo.language; - this.argumentList = routineInfo.argumentList; - this.returnType = routineInfo.returnType; - this.importedLibrariesList = routineInfo.importedLibrariesList; - this.body = routineInfo.body; - } - - BuilderImpl(Routine routinePb) { - this.routineId = RoutineId.fromPb(routinePb.getRoutineReference()); - this.etag = routinePb.getEtag(); - this.routineType = routinePb.getRoutineType(); - this.creationTime = routinePb.getCreationTime(); - this.lastModifiedTime = routinePb.getLastModifiedTime(); - this.language = routinePb.getLanguage(); - if (routinePb.getArguments() != null) { - this.argumentList = Lists.transform(routinePb.getArguments(), RoutineArgument.FROM_PB_FUNCTION); - } - if (routinePb.getReturnType() != null) { - this.returnType = StandardSQLDataType.fromPb(routinePb.getReturnType()); - } - if (routinePb.getImportedLibraries() == null) { - this.importedLibrariesList = Collections.emptyList(); - } else { - this.importedLibrariesList = routinePb.getImportedLibraries(); - } - this.body = routinePb.getDefinitionBody(); - } - + static final Function FROM_PB_FUNCTION = + new Function() { @Override - Builder setRoutineId(RoutineId id) { - this.routineId = id; - return this; + public RoutineInfo apply(Routine pb) { + return RoutineInfo.fromPb(pb); } - + }; + static final Function TO_PB_FUNCTION = + new Function() { @Override - Builder setEtag(String etag) { - this.etag = etag; - return this; + public Routine apply(RoutineInfo routineInfo) { + return routineInfo.toPb(); } + }; - @Override - public Builder setRoutineType(String routineType) { - this.routineType = routineType; - return this; - } + private final RoutineId routineId; + private final String etag; + private final String routineType; + private final Long creationTime; + private final Long lastModifiedTime; + private final String language; + private final List argumentList; + private final StandardSQLDataType returnType; + private final List importedLibrariesList; + private final String body; - @Override - Builder setCreationTime(Long creationMillis) { - this.creationTime = creationMillis; - return this; - } + public abstract static class Builder { + abstract Builder setRoutineId(RoutineId id); - @Override - Builder setLastModifiedTime(Long lastModifiedMillis) { - this.lastModifiedTime = lastModifiedMillis; - return this; - } + abstract Builder setEtag(String etag); - @Override - public Builder setLanguage(String language) { - this.language = language; - return this; - } + public abstract Builder setRoutineType(String routineType); - @Override - public Builder setArguments(List argumentList) { - this.argumentList = argumentList; - return this; - } + abstract Builder setCreationTime(Long creationMillis); - @Override - public Builder setReturnType(StandardSQLDataType returnType) { - this.returnType = returnType; - return this; - } + abstract Builder setLastModifiedTime(Long lastModifiedMillis); - @Override - public Builder setImportedLibraries(List importedLibrariesList) { - this.importedLibrariesList = importedLibrariesList; - return this; - } + public abstract Builder setLanguage(String language); - @Override - public Builder setBody(String body) { - this.body = body; - return this; - } + public abstract Builder setArguments(List argumentList); - @Override - public RoutineInfo build() { - return new RoutineInfo(this); - } - } - - RoutineInfo(BuilderImpl builder) { - this.routineId = checkNotNull(builder.routineId); - this.etag = builder.etag; - this.routineType = builder.routineType; - this.creationTime = builder.creationTime; - this.lastModifiedTime = builder.lastModifiedTime; - this.language = builder.language; - this.argumentList = builder.argumentList; - this.returnType = builder.returnType; - this.importedLibrariesList = builder.importedLibrariesList; - this.body = builder.body; - } - - public RoutineId getRoutineId() { return routineId; } - - public String getEtag() { return etag; } + public abstract Builder setReturnType(StandardSQLDataType returnType); - public String getRoutineType() { return routineType; } + public abstract Builder setImportedLibraries(List importedLibrariesList); - public Long getCreationTime() { return creationTime; } + public abstract Builder setBody(String body); - public Long getLastModifiedTime() { return lastModifiedTime; } + public abstract RoutineInfo build(); + } - public String getLanguage() { return language; } + static class BuilderImpl extends Builder { + private RoutineId routineId; + private String etag; + private String routineType; + private Long creationTime; + private Long lastModifiedTime; + private String language; + private List argumentList; + private StandardSQLDataType returnType; + private List importedLibrariesList; + private String body; - public List getArguments() { return argumentList; } + BuilderImpl() {} - public StandardSQLDataType getReturnType() { return returnType; } - - public List getImportedLibraries() { return importedLibrariesList; } + BuilderImpl(RoutineInfo routineInfo) { + this.routineId = routineInfo.routineId; + this.etag = routineInfo.etag; + this.routineType = routineInfo.routineType; + this.creationTime = routineInfo.creationTime; + this.lastModifiedTime = routineInfo.lastModifiedTime; + this.language = routineInfo.language; + this.argumentList = routineInfo.argumentList; + this.returnType = routineInfo.returnType; + this.importedLibrariesList = routineInfo.importedLibrariesList; + this.body = routineInfo.body; + } - public String getBody() { return body; } + BuilderImpl(Routine routinePb) { + this.routineId = RoutineId.fromPb(routinePb.getRoutineReference()); + this.etag = routinePb.getEtag(); + this.routineType = routinePb.getRoutineType(); + this.creationTime = routinePb.getCreationTime(); + this.lastModifiedTime = routinePb.getLastModifiedTime(); + this.language = routinePb.getLanguage(); + if (routinePb.getArguments() != null) { + this.argumentList = + Lists.transform(routinePb.getArguments(), RoutineArgument.FROM_PB_FUNCTION); + } + if (routinePb.getReturnType() != null) { + this.returnType = StandardSQLDataType.fromPb(routinePb.getReturnType()); + } + if (routinePb.getImportedLibraries() == null) { + this.importedLibrariesList = Collections.emptyList(); + } else { + this.importedLibrariesList = routinePb.getImportedLibraries(); + } + this.body = routinePb.getDefinitionBody(); + } - public Builder toBuilder() { return new BuilderImpl(this); } + @Override + Builder setRoutineId(RoutineId id) { + this.routineId = id; + return this; + } @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("routineId", routineId) - .add("etag", etag) - .add("routineType", routineType) - .add("creationTime", creationTime) - .add("lastModifiedTime", lastModifiedTime) - .add("language", language) - .add("arguments", argumentList) - .add("returnType", returnType) - .add("importedLibrariesList", importedLibrariesList) - .add("body", body) - .toString(); + Builder setEtag(String etag) { + this.etag = etag; + return this; } @Override - public int hashCode() { - return Objects.hash( - routineId, - etag, - routineType, - creationTime, - lastModifiedTime, - language, - argumentList, - returnType, - importedLibrariesList, - body); + public Builder setRoutineType(String routineType) { + this.routineType = routineType; + return this; } @Override - public boolean equals(Object obj) { - return obj == this - || obj != null - && obj.getClass().equals(RoutineInfo.class) - && Objects.equals(toPb(), ((RoutineInfo) obj).toPb()); + Builder setCreationTime(Long creationMillis) { + this.creationTime = creationMillis; + return this; } - /** Returns a builder for a {@code RoutineInfo} object given routine identity. */ - public static Builder newBuilder(RoutineId routineId) { - return new BuilderImpl().setRoutineId(routineId); + @Override + Builder setLastModifiedTime(Long lastModifiedMillis) { + this.lastModifiedTime = lastModifiedMillis; + return this; } - /** Returns a {@code RoutineInfo} object given routine identity. */ - public static RoutineInfo of(RoutineId routineId) { - return newBuilder(routineId).build(); + @Override + public Builder setLanguage(String language) { + this.language = language; + return this; } - RoutineInfo setProjectId(String projectId) { - if (Strings.isNullOrEmpty(getRoutineId().getProject())) { - return toBuilder().setRoutineId(getRoutineId().setProjectId(projectId)).build(); - } - return this; + @Override + public Builder setArguments(List argumentList) { + this.argumentList = argumentList; + return this; } - Routine toPb() { - Routine routinePb = new Routine() - .setEtag(getEtag()) - .setRoutineType(getRoutineType()) - .setDefinitionBody(getBody()) - .setCreationTime(getCreationTime()) - .setLastModifiedTime(getLastModifiedTime()) - .setLanguage(getLanguage()); - if (getRoutineId() != null) { - routinePb.setRoutineReference(getRoutineId().toPb()); - } - if (getArguments() != null) { - routinePb.setArguments(Lists.transform(getArguments(), RoutineArgument.TO_PB_FUNCTION)); - } - return routinePb; + @Override + public Builder setReturnType(StandardSQLDataType returnType) { + this.returnType = returnType; + return this; } + @Override + public Builder setImportedLibraries(List importedLibrariesList) { + this.importedLibrariesList = importedLibrariesList; + return this; + } + @Override + public Builder setBody(String body) { + this.body = body; + return this; + } - static RoutineInfo fromPb(Routine routinePb) { - return new BuilderImpl(routinePb).build(); + @Override + public RoutineInfo build() { + return new RoutineInfo(this); + } + } + + RoutineInfo(BuilderImpl builder) { + this.routineId = checkNotNull(builder.routineId); + this.etag = builder.etag; + this.routineType = builder.routineType; + this.creationTime = builder.creationTime; + this.lastModifiedTime = builder.lastModifiedTime; + this.language = builder.language; + this.argumentList = builder.argumentList; + this.returnType = builder.returnType; + this.importedLibrariesList = builder.importedLibrariesList; + this.body = builder.body; + } + + public RoutineId getRoutineId() { + return routineId; + } + + public String getEtag() { + return etag; + } + + public String getRoutineType() { + return routineType; + } + + public Long getCreationTime() { + return creationTime; + } + + public Long getLastModifiedTime() { + return lastModifiedTime; + } + + public String getLanguage() { + return language; + } + + public List getArguments() { + return argumentList; + } + + public StandardSQLDataType getReturnType() { + return returnType; + } + + public List getImportedLibraries() { + return importedLibrariesList; + } + + public String getBody() { + return body; + } + + public Builder toBuilder() { + return new BuilderImpl(this); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("routineId", routineId) + .add("etag", etag) + .add("routineType", routineType) + .add("creationTime", creationTime) + .add("lastModifiedTime", lastModifiedTime) + .add("language", language) + .add("arguments", argumentList) + .add("returnType", returnType) + .add("importedLibrariesList", importedLibrariesList) + .add("body", body) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash( + routineId, + etag, + routineType, + creationTime, + lastModifiedTime, + language, + argumentList, + returnType, + importedLibrariesList, + body); + } + + @Override + public boolean equals(Object obj) { + return obj == this + || obj != null + && obj.getClass().equals(RoutineInfo.class) + && Objects.equals(toPb(), ((RoutineInfo) obj).toPb()); + } + + /** Returns a builder for a {@code RoutineInfo} object given routine identity. */ + public static Builder newBuilder(RoutineId routineId) { + return new BuilderImpl().setRoutineId(routineId); + } + + /** Returns a {@code RoutineInfo} object given routine identity. */ + public static RoutineInfo of(RoutineId routineId) { + return newBuilder(routineId).build(); + } + + RoutineInfo setProjectId(String projectId) { + if (Strings.isNullOrEmpty(getRoutineId().getProject())) { + return toBuilder().setRoutineId(getRoutineId().setProjectId(projectId)).build(); + } + return this; + } + + Routine toPb() { + Routine routinePb = + new Routine() + .setEtag(getEtag()) + .setRoutineType(getRoutineType()) + .setDefinitionBody(getBody()) + .setCreationTime(getCreationTime()) + .setLastModifiedTime(getLastModifiedTime()) + .setLanguage(getLanguage()); + if (getRoutineId() != null) { + routinePb.setRoutineReference(getRoutineId().toPb()); + } + if (getArguments() != null) { + routinePb.setArguments(Lists.transform(getArguments(), RoutineArgument.TO_PB_FUNCTION)); } + return routinePb; + } -} \ No newline at end of file + static RoutineInfo fromPb(Routine routinePb) { + return new BuilderImpl(routinePb).build(); + } +} diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java index 3c72a711ceec..c563f288f35a 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java @@ -17,78 +17,69 @@ import com.google.api.services.bigquery.model.StandardSqlDataType; import com.google.auto.value.AutoValue; -import com.google.common.base.MoreObjects; - -import javax.annotation.Nullable; import java.io.Serializable; -import java.util.Objects; +import javax.annotation.Nullable; @AutoValue public abstract class StandardSQLDataType implements Serializable { + @AutoValue.Builder + public abstract static class Builder { + /** Sets the type of an array's elements, when the TypeKind is ARRAY. */ + public abstract Builder setArrayElementType(StandardSQLDataType arrayElementType); - @AutoValue.Builder - public abstract static class Builder { - /** Sets the type of an array's elements, when the TypeKind is - * ARRAY. - */ - public abstract Builder setArrayElementType(StandardSQLDataType arrayElementType); - - /** Sets the struct type definition (list of fields) when the - * TypeKind is STRUCT. - */ - public abstract Builder setStructType(StandardSQLStructType structType); - - /** Sets the top-level type of this data type. Can be - * any standard SQL data type. For more information, see - * https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types - */ - public abstract Builder setTypeKind(String typeKind); + /** Sets the struct type definition (list of fields) when the TypeKind is STRUCT. */ + public abstract Builder setStructType(StandardSQLStructType structType); - /** Creates a {@code StandardSQLDataType} object. */ - public abstract StandardSQLDataType build(); - - } + /** + * Sets the top-level type of this data type. Can be any standard SQL data type. For more + * information, see https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types + */ + public abstract Builder setTypeKind(String typeKind); + /** Creates a {@code StandardSQLDataType} object. */ + public abstract StandardSQLDataType build(); + } - public abstract String getTypeKind(); + public abstract String getTypeKind(); - @Nullable - public abstract StandardSQLDataType getArrayElementType(); + @Nullable + public abstract StandardSQLDataType getArrayElementType(); - @Nullable - public abstract StandardSQLStructType getStructType(); + @Nullable + public abstract StandardSQLStructType getStructType(); - public abstract Builder toBuilder(); + public abstract Builder toBuilder(); + public static Builder newBuilder() { + return new AutoValue_StandardSQLDataType.Builder(); + } - public static Builder newBuilder() { return new AutoValue_StandardSQLDataType.Builder(); } + public static Builder newBuilder(String typeKind) { + return newBuilder().setTypeKind(typeKind); + } - public static Builder newBuilder(String typeKind) { - return newBuilder().setTypeKind(typeKind); + StandardSqlDataType toPb() { + StandardSqlDataType dataTypePb = new StandardSqlDataType(); + dataTypePb.setTypeKind(getTypeKind()); + if (getArrayElementType() != null) { + dataTypePb.setArrayElementType(getArrayElementType().toPb()); } - - StandardSqlDataType toPb() { - StandardSqlDataType dataTypePb = new StandardSqlDataType(); - dataTypePb.setTypeKind(getTypeKind()); - if (getArrayElementType() != null) { - dataTypePb.setArrayElementType(getArrayElementType().toPb()); - } - if (getStructType() != null) { - dataTypePb.setStructType(getStructType().toPb()); - } - return dataTypePb; + if (getStructType() != null) { + dataTypePb.setStructType(getStructType().toPb()); } - - static StandardSQLDataType fromPb(StandardSqlDataType dataTypePb) { - Builder builder = newBuilder(); - builder.setTypeKind(dataTypePb.getTypeKind()); - if (dataTypePb.getArrayElementType() != null) { - builder.setArrayElementType(StandardSQLDataType.fromPb(dataTypePb.getArrayElementType())); - } - if (dataTypePb.getStructType() != null) { - builder.setStructType(StandardSQLStructType.fromPb(dataTypePb.getStructType())); - } - return builder.build(); + return dataTypePb; + } + + static StandardSQLDataType fromPb(StandardSqlDataType dataTypePb) { + Builder builder = newBuilder(); + builder.setTypeKind(dataTypePb.getTypeKind()); + if (dataTypePb.getArrayElementType() != null) { + builder.setArrayElementType(StandardSQLDataType.fromPb(dataTypePb.getArrayElementType())); + } + if (dataTypePb.getStructType() != null) { + builder.setStructType(StandardSQLStructType.fromPb(dataTypePb.getStructType())); } + return builder.build(); + } } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java index fe29a45a6f67..0a6e2eaa4a19 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java @@ -18,72 +18,71 @@ import com.google.api.services.bigquery.model.StandardSqlField; import com.google.auto.value.AutoValue; import com.google.common.base.Function; - -import javax.annotation.Nullable; import java.io.Serializable; +import javax.annotation.Nullable; @AutoValue public abstract class StandardSQLField implements Serializable { - static final Function FROM_PB_FUNCTION = - new Function() { - @Override - public StandardSQLField apply(StandardSqlField pb) { - return StandardSQLField.fromPb(pb); - } - }; - static final Function TO_PB_FUNCTION = - new Function() { - @Override - public StandardSqlField apply(StandardSQLField field) { - return field.toPb(); - } - }; + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public StandardSQLField apply(StandardSqlField pb) { + return StandardSQLField.fromPb(pb); + } + }; + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public StandardSqlField apply(StandardSQLField field) { + return field.toPb(); + } + }; - @AutoValue.Builder - public abstract static class Builder { + @AutoValue.Builder + public abstract static class Builder { - public abstract Builder setName(String name); + public abstract Builder setName(String name); - public abstract Builder setDataType(StandardSQLDataType dataType); + public abstract Builder setDataType(StandardSQLDataType dataType); - public abstract StandardSQLField build(); - } + public abstract StandardSQLField build(); + } - @Nullable - public abstract String getName(); + @Nullable + public abstract String getName(); - public abstract StandardSQLDataType getDataType(); + public abstract StandardSQLDataType getDataType(); - public abstract Builder toBuilder(); + public abstract Builder toBuilder(); - public static Builder newBuilder() { return new AutoValue_StandardSQLField.Builder(); } + public static Builder newBuilder() { + return new AutoValue_StandardSQLField.Builder(); + } - public static Builder newBuilder(StandardSQLDataType dataType) { - return newBuilder().setDataType(dataType); - } + public static Builder newBuilder(StandardSQLDataType dataType) { + return newBuilder().setDataType(dataType); + } - public static Builder newBuilder(String name, StandardSQLDataType dataType) { - return newBuilder().setName(name).setDataType(dataType); - } + public static Builder newBuilder(String name, StandardSQLDataType dataType) { + return newBuilder().setName(name).setDataType(dataType); + } - public StandardSqlField toPb() { - StandardSqlField fieldPb = new StandardSqlField(); - fieldPb.setName(getName()); - if (getDataType() != null) { - fieldPb.setType(getDataType().toPb()); - } - return fieldPb; + public StandardSqlField toPb() { + StandardSqlField fieldPb = new StandardSqlField(); + fieldPb.setName(getName()); + if (getDataType() != null) { + fieldPb.setType(getDataType().toPb()); } - - static StandardSQLField fromPb(StandardSqlField fieldPb) { - Builder builder = newBuilder(); - builder.setName(fieldPb.getName()); - if (fieldPb.getType() != null) { - builder.setDataType(StandardSQLDataType.fromPb(fieldPb.getType())); - } - return builder.build(); + return fieldPb; + } + + static StandardSQLField fromPb(StandardSqlField fieldPb) { + Builder builder = newBuilder(); + builder.setName(fieldPb.getName()); + if (fieldPb.getType() != null) { + builder.setDataType(StandardSQLDataType.fromPb(fieldPb.getType())); } - - + return builder.build(); + } } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLStructType.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLStructType.java index 652944ca5f84..9f6685a57f45 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLStructType.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLStructType.java @@ -19,48 +19,47 @@ import com.google.api.services.bigquery.model.StandardSqlStructType; import com.google.auto.value.AutoValue; import com.google.common.collect.Lists; - import java.io.Serializable; import java.util.List; - @AutoValue public abstract class StandardSQLStructType implements Serializable { + @AutoValue.Builder + public abstract static class Builder { - @AutoValue.Builder - public abstract static class Builder { + public abstract Builder setFields(List fields); - public abstract Builder setFields(List fields); + public abstract StandardSQLStructType build(); + } - public abstract StandardSQLStructType build(); - - } + public abstract List getFields(); - public abstract List getFields(); + public abstract Builder toBuilder(); - public abstract Builder toBuilder(); + public static Builder newBuilder() { + return new AutoValue_StandardSQLStructType.Builder(); + } - public static Builder newBuilder() { return new AutoValue_StandardSQLStructType.Builder(); } + public static Builder newBuilder(List fieldList) { + return newBuilder().setFields(fieldList); + } - public static Builder newBuilder(List fieldList) { - return newBuilder().setFields(fieldList); + static StandardSQLStructType fromPb( + com.google.api.services.bigquery.model.StandardSqlStructType structTypePb) { + Builder builder = newBuilder(); + if (structTypePb.getFields() != null) { + builder.setFields( + Lists.transform(structTypePb.getFields(), StandardSQLField.FROM_PB_FUNCTION)); } + return builder.build(); + } - static StandardSQLStructType fromPb(com.google.api.services.bigquery.model.StandardSqlStructType structTypePb) { - Builder builder = newBuilder(); - if (structTypePb.getFields() != null) { - builder.setFields(Lists.transform(structTypePb.getFields(), StandardSQLField.FROM_PB_FUNCTION)); - } - return builder.build(); + StandardSqlStructType toPb() { + StandardSqlStructType structTypePb = new StandardSqlStructType(); + if (getFields() != null) { + structTypePb.setFields(Lists.transform(getFields(), StandardSQLField.TO_PB_FUNCTION)); } - - StandardSqlStructType toPb() { - StandardSqlStructType structTypePb = new StandardSqlStructType(); - if (getFields() != null) { - structTypePb.setFields(Lists.transform(getFields(), StandardSQLField.TO_PB_FUNCTION)); - } - return structTypePb; - } - + return structTypePb; + } } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java index 0d30ea263589..d003db49c105 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java @@ -29,7 +29,6 @@ import com.google.cloud.ServiceRpc; import com.google.cloud.Tuple; import com.google.cloud.bigquery.BigQueryException; - import java.util.Map; @InternalExtensionOnly @@ -198,11 +197,10 @@ Tuple> listModels( Routine getRoutine(String projectId, String datasetId, String routineId, Map options); Tuple> listRoutines( - String projectId, String datasetId, Map options); + String projectId, String datasetId, Map options); boolean deleteRoutine(String projectId, String datasetId, String routineId); - /** * Sends an insert all request. * diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java index e63577b7f946..0584ee1d2041 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java @@ -180,10 +180,10 @@ public Routine create(Routine routine, Map options) { try { RoutineReference reference = routine.getRoutineReference(); return bigquery - .routines() - .insert(reference.getProjectId(), reference.getDatasetId(), routine) - .setFields(Option.FIELDS.getString(options)) - .execute(); + .routines() + .insert(reference.getProjectId(), reference.getDatasetId(), routine) + .setFields(Option.FIELDS.getString(options)) + .execute(); } catch (IOException ex) { throw translate(ex); } @@ -390,23 +390,25 @@ public Routine update(Routine routine, Map options) { try { RoutineReference reference = routine.getRoutineReference(); return bigquery - .routines() - .update(reference.getProjectId(), reference.getDatasetId(), reference.getRoutineId(), routine) - .setFields(Option.FIELDS.getString(options)) - .execute(); + .routines() + .update( + reference.getProjectId(), reference.getDatasetId(), reference.getRoutineId(), routine) + .setFields(Option.FIELDS.getString(options)) + .execute(); } catch (IOException ex) { throw translate(ex); } } @Override - public Routine getRoutine(String projectId, String datasetId, String routineId, Map options) { + public Routine getRoutine( + String projectId, String datasetId, String routineId, Map options) { try { return bigquery - .routines() - .get(projectId, datasetId, routineId) - .setFields(Option.FIELDS.getString(options)) - .execute(); + .routines() + .get(projectId, datasetId, routineId) + .setFields(Option.FIELDS.getString(options)) + .execute(); } catch (IOException ex) { BigQueryException serviceException = translate(ex); if (serviceException.getCode() == HTTP_NOT_FOUND) { @@ -417,15 +419,16 @@ public Routine getRoutine(String projectId, String datasetId, String routineId, } @Override - public Tuple> listRoutines(String projectId, String datasetId, Map options) { + public Tuple> listRoutines( + String projectId, String datasetId, Map options) { try { ListRoutinesResponse routineList = - bigquery - .routines() - .list(projectId, datasetId) - .setMaxResults(Option.MAX_RESULTS.getLong(options)) - .setPageToken(Option.PAGE_TOKEN.getString(options)) - .execute(); + bigquery + .routines() + .list(projectId, datasetId) + .setMaxResults(Option.MAX_RESULTS.getLong(options)) + .setPageToken(Option.PAGE_TOKEN.getString(options)) + .execute(); Iterable routines = routineList.getRoutines(); return Tuple.of(routineList.getNextPageToken(), routines); } catch (IOException ex) { diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java index 004e85f40945..2113eb8f5410 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java @@ -34,7 +34,7 @@ public class JobStatisticsTest { private static final Boolean CACHE_HIT = true; private static final String DDL_OPERATION_PERFORMED = "SKIP"; private static final TableId DDL_TARGET_TABLE = TableId.of("foo", "bar", "baz"); - private static final RoutineId DDL_TARGET_ROUTINE = RoutineId.of("alpha","beta", "gamma"); + private static final RoutineId DDL_TARGET_ROUTINE = RoutineId.of("alpha", "beta", "gamma"); private static final Long ESTIMATE_BYTES_PROCESSED = 101L; private static final Long NUM_DML_AFFECTED_ROWS = 88L; private static final QueryStatistics.StatementType STATEMENT_TYPE = @@ -137,7 +137,7 @@ public class JobStatisticsTest { .setCacheHit(CACHE_HIT) .setDDLOperationPerformed(DDL_OPERATION_PERFORMED) .setDDLTargetTable(DDL_TARGET_TABLE) - .setDDLTargetRoutine(DDL_TARGET_ROUTINE) + .setDDLTargetRoutine(DDL_TARGET_ROUTINE) .setEstimatedBytesProcessed(ESTIMATE_BYTES_PROCESSED) .setNumDmlAffectedRows(NUM_DML_AFFECTED_ROWS) .setReferenceTables(REFERENCED_TABLES) diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineArgumentTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineArgumentTest.java index 65a31cf49e90..909d5981d7f9 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineArgumentTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineArgumentTest.java @@ -15,47 +15,49 @@ */ package com.google.cloud.bigquery; -import org.junit.Test; - import static org.junit.Assert.assertEquals; +import org.junit.Test; + public class RoutineArgumentTest { - private static final String NAME = "foo"; - private static final String KIND = "SCALAR_FUNCTION"; - private static final String MODE = "IN"; - private static final StandardSQLDataType DATA_TYPE = StandardSQLDataType.newBuilder("STRING").build(); - private static final RoutineArgument ARGUMENT = RoutineArgument.newBuilder() - .setName(NAME) - .setKind(KIND) - .setMode(MODE) - .setDataType(DATA_TYPE) - .build(); - - - @Test - public void testToBuilder() { - compareRoutineArguments(ARGUMENT, ARGUMENT.toBuilder().build()); - } - - @Test - public void testBuilder() { - assertEquals(NAME, ARGUMENT.getName()); - assertEquals(KIND, ARGUMENT.getKind()); - assertEquals(MODE, ARGUMENT.getMode()); - assertEquals(DATA_TYPE, ARGUMENT.getDataType()); - } - @Test - public void testToPbAndFromPb() { - compareRoutineArguments(ARGUMENT, RoutineArgument.fromPb(ARGUMENT.toPb())); - } - - public void compareRoutineArguments(RoutineArgument expected, RoutineArgument value) { - assertEquals(expected, value); - assertEquals(expected.getName(), value.getName()); - assertEquals(expected.getKind(), value.getKind()); - assertEquals(expected.getMode(), value.getMode()); - assertEquals(expected.getDataType(), value.getDataType()); - assertEquals(expected.hashCode(), value.hashCode()); - } + private static final String NAME = "foo"; + private static final String KIND = "SCALAR_FUNCTION"; + private static final String MODE = "IN"; + private static final StandardSQLDataType DATA_TYPE = + StandardSQLDataType.newBuilder("STRING").build(); + private static final RoutineArgument ARGUMENT = + RoutineArgument.newBuilder() + .setName(NAME) + .setKind(KIND) + .setMode(MODE) + .setDataType(DATA_TYPE) + .build(); + + @Test + public void testToBuilder() { + compareRoutineArguments(ARGUMENT, ARGUMENT.toBuilder().build()); + } + + @Test + public void testBuilder() { + assertEquals(NAME, ARGUMENT.getName()); + assertEquals(KIND, ARGUMENT.getKind()); + assertEquals(MODE, ARGUMENT.getMode()); + assertEquals(DATA_TYPE, ARGUMENT.getDataType()); + } + + @Test + public void testToPbAndFromPb() { + compareRoutineArguments(ARGUMENT, RoutineArgument.fromPb(ARGUMENT.toPb())); + } + + public void compareRoutineArguments(RoutineArgument expected, RoutineArgument value) { + assertEquals(expected, value); + assertEquals(expected.getName(), value.getName()); + assertEquals(expected.getKind(), value.getKind()); + assertEquals(expected.getMode(), value.getMode()); + assertEquals(expected.getDataType(), value.getDataType()); + assertEquals(expected.hashCode(), value.hashCode()); + } } diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineIdTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineIdTest.java index 78be7aef1916..94a19fbfd573 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineIdTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineIdTest.java @@ -13,7 +13,6 @@ * 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; @@ -22,42 +21,42 @@ public class RoutineIdTest { - public static final RoutineId ROUTINE = RoutineId.of("dataset", "routine"); - public static final RoutineId ROUTINE_COMPLETE = RoutineId.of("project", "dataset", "routine"); - - @Test - public void testOf() { - assertEquals(null, ROUTINE.getProject()); - assertEquals("dataset", ROUTINE.getDataset()); - assertEquals("routine", ROUTINE.getRoutine()); - - assertEquals("project", ROUTINE_COMPLETE.getProject()); - assertEquals("dataset", ROUTINE_COMPLETE.getDataset()); - assertEquals("routine",ROUTINE_COMPLETE.getRoutine()); - } - - @Test - public void testEquals() { - compareRoutineIds(ROUTINE, RoutineId.of("dataset", "routine")); - compareRoutineIds(ROUTINE_COMPLETE, RoutineId.of("project", "dataset", "routine")); - } - - @Test - public void testToPbAndFromPb() { - compareRoutineIds(ROUTINE, RoutineId.fromPb(ROUTINE.toPb())); - compareRoutineIds(ROUTINE_COMPLETE, RoutineId.fromPb(ROUTINE_COMPLETE.toPb())); - } - - @Test - public void testSetProjectId() { - RoutineId differentProjectTable = RoutineId.of("differentProject", "dataset", "routine"); - assertEquals(differentProjectTable, ROUTINE.setProjectId("differentProject")); - } - - private void compareRoutineIds(RoutineId expected, RoutineId value) { - assertEquals(expected, value); - assertEquals(expected.getProject(), value.getProject()); - assertEquals(expected.getDataset(), value.getDataset()); - assertEquals(expected.hashCode(), value.hashCode()); - } + public static final RoutineId ROUTINE = RoutineId.of("dataset", "routine"); + public static final RoutineId ROUTINE_COMPLETE = RoutineId.of("project", "dataset", "routine"); + + @Test + public void testOf() { + assertEquals(null, ROUTINE.getProject()); + assertEquals("dataset", ROUTINE.getDataset()); + assertEquals("routine", ROUTINE.getRoutine()); + + assertEquals("project", ROUTINE_COMPLETE.getProject()); + assertEquals("dataset", ROUTINE_COMPLETE.getDataset()); + assertEquals("routine", ROUTINE_COMPLETE.getRoutine()); + } + + @Test + public void testEquals() { + compareRoutineIds(ROUTINE, RoutineId.of("dataset", "routine")); + compareRoutineIds(ROUTINE_COMPLETE, RoutineId.of("project", "dataset", "routine")); + } + + @Test + public void testToPbAndFromPb() { + compareRoutineIds(ROUTINE, RoutineId.fromPb(ROUTINE.toPb())); + compareRoutineIds(ROUTINE_COMPLETE, RoutineId.fromPb(ROUTINE_COMPLETE.toPb())); + } + + @Test + public void testSetProjectId() { + RoutineId differentProjectTable = RoutineId.of("differentProject", "dataset", "routine"); + assertEquals(differentProjectTable, ROUTINE.setProjectId("differentProject")); + } + + private void compareRoutineIds(RoutineId expected, RoutineId value) { + assertEquals(expected, value); + assertEquals(expected.getProject(), value.getProject()); + assertEquals(expected.getDataset(), value.getDataset()); + assertEquals(expected.hashCode(), value.hashCode()); + } } diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineInfoTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineInfoTest.java index 0d1146cdd58a..866daf82d0fa 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineInfoTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineInfoTest.java @@ -1,113 +1,127 @@ +/* + * Copyright 2019 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.common.collect.ImmutableList; -import org.junit.Test; - -import java.util.List; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import com.google.common.collect.ImmutableList; +import java.util.List; +import org.junit.Test; + public class RoutineInfoTest { - private static final RoutineId ROUTINE_ID = RoutineId.of("dataset", "routine"); - private static final String ETAG = "etag"; - private static final String ROUTINE_TYPE = "SCALAR_FUNCTION"; - private static final Long CREATION_TIME = 10L; - private static final Long LAST_MODIFIED_TIME = 20L; - private static final String LANGUAGE="SQL"; - - private static final RoutineArgument ARG_1 = RoutineArgument.newBuilder() - .setDataType(StandardSQLDataType.newBuilder("STRING").build()) - .setName("arg1") - .build(); - - private static final List ARGUMENT_LIST = ImmutableList.of(ARG_1); - - private static final StandardSQLDataType RETURN_TYPE = StandardSQLDataType.newBuilder("FLOAT64").build(); - - private static final List IMPORTED_LIBRARIES = ImmutableList.of( - "gs://foo", - "gs://bar", - "gs://baz"); - - private static final String BODY = "body"; - - private static final RoutineInfo ROUTINE_INFO = RoutineInfo.of(ROUTINE_ID).toBuilder() - .setEtag(ETAG) - .setRoutineType(ROUTINE_TYPE) - .setCreationTime(CREATION_TIME) - .setLastModifiedTime(LAST_MODIFIED_TIME) - .setLanguage(LANGUAGE) - .setArguments(ARGUMENT_LIST) - .setReturnType(RETURN_TYPE) - .setImportedLibraries(IMPORTED_LIBRARIES) - .setBody(BODY) - .build(); - - - @Test - public void testToBuilder() { - compareRoutineInfo(ROUTINE_INFO, ROUTINE_INFO.toBuilder().build()); - } - - @Test - public void testBuilderIncomplete() { - RoutineInfo routineInfo = RoutineInfo.of(ROUTINE_ID); - assertEquals(routineInfo, routineInfo.toBuilder().build()); - } - - @Test - public void testBuilder() { - assertEquals(ROUTINE_ID, ROUTINE_INFO.getRoutineId()); - assertEquals(ETAG, ROUTINE_INFO.getEtag()); - assertEquals(ROUTINE_TYPE, ROUTINE_INFO.getRoutineType()); - assertEquals(CREATION_TIME, ROUTINE_INFO.getCreationTime()); - assertEquals(LAST_MODIFIED_TIME, ROUTINE_INFO.getLastModifiedTime()); - assertEquals(LANGUAGE, ROUTINE_INFO.getLanguage()); - assertEquals(ARGUMENT_LIST, ROUTINE_INFO.getArguments()); - assertEquals(RETURN_TYPE, ROUTINE_INFO.getReturnType()); - assertEquals(IMPORTED_LIBRARIES, ROUTINE_INFO.getImportedLibraries()); - assertEquals(BODY, ROUTINE_INFO.getBody()); - } - - @Test - public void testOf() { - RoutineInfo routineInfo = RoutineInfo.of(ROUTINE_ID); - assertEquals(ROUTINE_ID, ROUTINE_INFO.getRoutineId()); - assertNull(routineInfo.getEtag()); - assertNull(routineInfo.getRoutineType()); - assertNull(routineInfo.getCreationTime()); - assertNull(routineInfo.getLastModifiedTime()); - assertNull(routineInfo.getLanguage()); - assertNull(routineInfo.getArguments()); - assertNull(routineInfo.getReturnType()); - assertNull(routineInfo.getImportedLibraries()); - assertNull(routineInfo.getBody()); - } - - public void testToAndFromPb() { - compareRoutineInfo(ROUTINE_INFO, RoutineInfo.fromPb(ROUTINE_INFO.toPb())); - } - - @Test - public void testSetProjectId() { - assertEquals("project", ROUTINE_INFO.setProjectId("project").getRoutineId().getProject()); - } - - - public void compareRoutineInfo(RoutineInfo expected, RoutineInfo value) { - assertEquals(expected, value); - assertEquals(expected.getRoutineId(), value.getRoutineId()); - assertEquals(expected.getEtag(), value.getEtag()); - assertEquals(expected.getRoutineType(), value.getRoutineType()); - assertEquals(expected.getCreationTime(), value.getCreationTime()); - assertEquals(expected.getLastModifiedTime(), value.getLastModifiedTime()); - assertEquals(expected.getLanguage(), value.getLanguage()); - assertEquals(expected.getArguments(), value.getArguments()); - assertEquals(expected.getReturnType(), value.getReturnType()); - assertEquals(expected.getImportedLibraries(), value.getImportedLibraries()); - assertEquals(expected.getBody(), value.getBody()); - assertEquals(expected.hashCode(), value.hashCode()); - } + private static final RoutineId ROUTINE_ID = RoutineId.of("dataset", "routine"); + private static final String ETAG = "etag"; + private static final String ROUTINE_TYPE = "SCALAR_FUNCTION"; + private static final Long CREATION_TIME = 10L; + private static final Long LAST_MODIFIED_TIME = 20L; + private static final String LANGUAGE = "SQL"; + + private static final RoutineArgument ARG_1 = + RoutineArgument.newBuilder() + .setDataType(StandardSQLDataType.newBuilder("STRING").build()) + .setName("arg1") + .build(); + + private static final List ARGUMENT_LIST = ImmutableList.of(ARG_1); + + private static final StandardSQLDataType RETURN_TYPE = + StandardSQLDataType.newBuilder("FLOAT64").build(); + + private static final List IMPORTED_LIBRARIES = + ImmutableList.of("gs://foo", "gs://bar", "gs://baz"); + + private static final String BODY = "body"; + + private static final RoutineInfo ROUTINE_INFO = + RoutineInfo.of(ROUTINE_ID) + .toBuilder() + .setEtag(ETAG) + .setRoutineType(ROUTINE_TYPE) + .setCreationTime(CREATION_TIME) + .setLastModifiedTime(LAST_MODIFIED_TIME) + .setLanguage(LANGUAGE) + .setArguments(ARGUMENT_LIST) + .setReturnType(RETURN_TYPE) + .setImportedLibraries(IMPORTED_LIBRARIES) + .setBody(BODY) + .build(); + + @Test + public void testToBuilder() { + compareRoutineInfo(ROUTINE_INFO, ROUTINE_INFO.toBuilder().build()); + } + + @Test + public void testBuilderIncomplete() { + RoutineInfo routineInfo = RoutineInfo.of(ROUTINE_ID); + assertEquals(routineInfo, routineInfo.toBuilder().build()); + } + + @Test + public void testBuilder() { + assertEquals(ROUTINE_ID, ROUTINE_INFO.getRoutineId()); + assertEquals(ETAG, ROUTINE_INFO.getEtag()); + assertEquals(ROUTINE_TYPE, ROUTINE_INFO.getRoutineType()); + assertEquals(CREATION_TIME, ROUTINE_INFO.getCreationTime()); + assertEquals(LAST_MODIFIED_TIME, ROUTINE_INFO.getLastModifiedTime()); + assertEquals(LANGUAGE, ROUTINE_INFO.getLanguage()); + assertEquals(ARGUMENT_LIST, ROUTINE_INFO.getArguments()); + assertEquals(RETURN_TYPE, ROUTINE_INFO.getReturnType()); + assertEquals(IMPORTED_LIBRARIES, ROUTINE_INFO.getImportedLibraries()); + assertEquals(BODY, ROUTINE_INFO.getBody()); + } + + @Test + public void testOf() { + RoutineInfo routineInfo = RoutineInfo.of(ROUTINE_ID); + assertEquals(ROUTINE_ID, ROUTINE_INFO.getRoutineId()); + assertNull(routineInfo.getEtag()); + assertNull(routineInfo.getRoutineType()); + assertNull(routineInfo.getCreationTime()); + assertNull(routineInfo.getLastModifiedTime()); + assertNull(routineInfo.getLanguage()); + assertNull(routineInfo.getArguments()); + assertNull(routineInfo.getReturnType()); + assertNull(routineInfo.getImportedLibraries()); + assertNull(routineInfo.getBody()); + } + + public void testToAndFromPb() { + compareRoutineInfo(ROUTINE_INFO, RoutineInfo.fromPb(ROUTINE_INFO.toPb())); + } + + @Test + public void testSetProjectId() { + assertEquals("project", ROUTINE_INFO.setProjectId("project").getRoutineId().getProject()); + } + + public void compareRoutineInfo(RoutineInfo expected, RoutineInfo value) { + assertEquals(expected, value); + assertEquals(expected.getRoutineId(), value.getRoutineId()); + assertEquals(expected.getEtag(), value.getEtag()); + assertEquals(expected.getRoutineType(), value.getRoutineType()); + assertEquals(expected.getCreationTime(), value.getCreationTime()); + assertEquals(expected.getLastModifiedTime(), value.getLastModifiedTime()); + assertEquals(expected.getLanguage(), value.getLanguage()); + assertEquals(expected.getArguments(), value.getArguments()); + assertEquals(expected.getReturnType(), value.getReturnType()); + assertEquals(expected.getImportedLibraries(), value.getImportedLibraries()); + assertEquals(expected.getBody(), value.getBody()); + assertEquals(expected.hashCode(), value.hashCode()); + } } diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineTest.java index 000ccdd5ce1e..36301aae58fd 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineTest.java @@ -1,43 +1,95 @@ +/* + * Copyright 2019 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.common.collect.ImmutableList; -import org.junit.Test; - -import java.util.List; - import static org.easymock.EasyMock.*; import static org.easymock.EasyMock.replay; import static org.junit.Assert.*; -public class RoutineTest { - - private BigQuery serviceMockReturnsOptions = createStrictMock(BigQuery.class); - private BigQueryOptions mockOptions = createMock(BigQueryOptions.class); - - private static final RoutineId ROUTINE_ID = RoutineId.of("dataset", "routine"); - private static final String ETAG = "etag"; - private static final String ROUTINE_TYPE = "SCALAR_FUNCTION"; - private static final Long CREATION_TIME = 10L; - private static final Long LAST_MODIFIED_TIME = 20L; - private static final String LANGUAGE="SQL"; - - private static final RoutineArgument ARG_1 = RoutineArgument.newBuilder() - .setDataType(StandardSQLDataType.newBuilder("STRING").build()) - .setName("arg1") - .build(); - - private static final List ARGUMENT_LIST = ImmutableList.of(ARG_1); - - private static final StandardSQLDataType RETURN_TYPE = StandardSQLDataType.newBuilder("FLOAT64").build(); - - private static final List IMPORTED_LIBRARIES = ImmutableList.of( - "gs://foo", - "gs://bar", - "gs://baz"); +import com.google.common.collect.ImmutableList; +import java.util.List; +import org.junit.Test; - private static final String BODY = "body"; +public class RoutineTest { - private static final RoutineInfo ROUTINE_INFO = RoutineInfo.newBuilder(ROUTINE_ID) + private BigQuery serviceMockReturnsOptions = createStrictMock(BigQuery.class); + private BigQueryOptions mockOptions = createMock(BigQueryOptions.class); + + private static final RoutineId ROUTINE_ID = RoutineId.of("dataset", "routine"); + private static final String ETAG = "etag"; + private static final String ROUTINE_TYPE = "SCALAR_FUNCTION"; + private static final Long CREATION_TIME = 10L; + private static final Long LAST_MODIFIED_TIME = 20L; + private static final String LANGUAGE = "SQL"; + + private static final RoutineArgument ARG_1 = + RoutineArgument.newBuilder() + .setDataType(StandardSQLDataType.newBuilder("STRING").build()) + .setName("arg1") + .build(); + + private static final List ARGUMENT_LIST = ImmutableList.of(ARG_1); + + private static final StandardSQLDataType RETURN_TYPE = + StandardSQLDataType.newBuilder("FLOAT64").build(); + + private static final List IMPORTED_LIBRARIES = + ImmutableList.of("gs://foo", "gs://bar", "gs://baz"); + + private static final String BODY = "body"; + + private static final RoutineInfo ROUTINE_INFO = + RoutineInfo.newBuilder(ROUTINE_ID) + .setEtag(ETAG) + .setRoutineType(ROUTINE_TYPE) + .setCreationTime(CREATION_TIME) + .setLastModifiedTime(LAST_MODIFIED_TIME) + .setLanguage(LANGUAGE) + .setArguments(ARGUMENT_LIST) + .setReturnType(RETURN_TYPE) + .setImportedLibraries(IMPORTED_LIBRARIES) + .setBody(BODY) + .build(); + + private BigQuery bigquery; + private Routine expectedRoutine; + private Routine routine; + + private void initializeExpectedRoutine(int optionsCalls) { + expect(serviceMockReturnsOptions.getOptions()).andReturn(mockOptions).times(optionsCalls); + replay(serviceMockReturnsOptions); + bigquery = createStrictMock(BigQuery.class); + expectedRoutine = + new Routine(serviceMockReturnsOptions, new RoutineInfo.BuilderImpl(ROUTINE_INFO)); + } + + private void initializeRoutine() { + routine = new Routine(bigquery, new RoutineInfo.BuilderImpl(ROUTINE_INFO)); + } + + private void tearDown() throws Exception { + verify(bigquery, serviceMockReturnsOptions); + } + + @Test + public void testBuilder() { + initializeExpectedRoutine(2); + replay(bigquery); + Routine builtRoutine = + new Routine.Builder(serviceMockReturnsOptions, ROUTINE_ID) .setEtag(ETAG) .setRoutineType(ROUTINE_TYPE) .setCreationTime(CREATION_TIME) @@ -48,164 +100,127 @@ public class RoutineTest { .setImportedLibraries(IMPORTED_LIBRARIES) .setBody(BODY) .build(); - - - private BigQuery bigquery; - private Routine expectedRoutine; - private Routine routine; - - private void initializeExpectedRoutine(int optionsCalls) { - expect(serviceMockReturnsOptions.getOptions()).andReturn(mockOptions).times(optionsCalls); - replay(serviceMockReturnsOptions); - bigquery = createStrictMock(BigQuery.class); - expectedRoutine = new Routine(serviceMockReturnsOptions, new RoutineInfo.BuilderImpl(ROUTINE_INFO)); - } - - private void initializeRoutine() { - routine = new Routine(bigquery, new RoutineInfo.BuilderImpl(ROUTINE_INFO)); - } - - private void tearDown() throws Exception { - verify(bigquery, serviceMockReturnsOptions); - } - - @Test - public void testBuilder() { - initializeExpectedRoutine(2); - replay(bigquery); - Routine builtRoutine = - new Routine.Builder(serviceMockReturnsOptions, ROUTINE_ID) - .setEtag(ETAG) - .setRoutineType(ROUTINE_TYPE) - .setCreationTime(CREATION_TIME) - .setLastModifiedTime(LAST_MODIFIED_TIME) - .setLanguage(LANGUAGE) - .setArguments(ARGUMENT_LIST) - .setReturnType(RETURN_TYPE) - .setImportedLibraries(IMPORTED_LIBRARIES) - .setBody(BODY) - .build(); - assertEquals(ETAG, builtRoutine.getEtag()); - assertSame(serviceMockReturnsOptions, builtRoutine.getBigQuery()); - } - - @Test - public void testToBuilder() { - initializeExpectedRoutine(2); - replay(bigquery); - compareRoutineInfo(expectedRoutine, expectedRoutine.toBuilder().build()); - } - - @Test - public void testExists_True() throws Exception { - initializeExpectedRoutine(1); - BigQuery.RoutineOption[] expectedOptions = {BigQuery.RoutineOption.fields()}; - expect(bigquery.getOptions()).andReturn(mockOptions); - expect(bigquery.getRoutine(ROUTINE_INFO.getRoutineId(), expectedOptions)).andReturn(null); - replay(bigquery); - initializeRoutine(); - assertFalse(routine.exists()); - } - - @Test - public void testExists_False() throws Exception { - initializeExpectedRoutine(1); - BigQuery.RoutineOption[] expectedOptions = {BigQuery.RoutineOption.fields()}; - expect(bigquery.getOptions()).andReturn(mockOptions); - expect(bigquery.getRoutine(ROUTINE_INFO.getRoutineId(), expectedOptions)).andReturn(expectedRoutine); - replay(bigquery); - initializeRoutine(); - assertTrue(routine.exists()); - } - - @Test - public void testReload() throws Exception { - initializeExpectedRoutine(4); - RoutineInfo updatedInfo = ROUTINE_INFO.toBuilder().setBody("body2").build(); - Routine expectedRoutine = - new Routine(serviceMockReturnsOptions, new RoutineInfo.BuilderImpl(updatedInfo)); - expect(bigquery.getOptions()).andReturn(mockOptions); - expect(bigquery.getRoutine(ROUTINE_INFO.getRoutineId())).andReturn(expectedRoutine); - replay(bigquery); - initializeRoutine(); - Routine updatedRoutine = routine.reload(); - compareRoutine(expectedRoutine, updatedRoutine); - } - - @Test - public void testReload_Null() throws Exception { - initializeExpectedRoutine(1); - expect(bigquery.getOptions()).andReturn(mockOptions); - expect(bigquery.getRoutine(ROUTINE_INFO.getRoutineId())).andReturn(null); - replay(bigquery); - initializeRoutine(); - assertNull(routine.reload()); - } - - @Test - public void testUpdate() { - initializeExpectedRoutine(4); - Routine expectedUpdatedRoutine = expectedRoutine.toBuilder().setBody("body2").build(); - expect(bigquery.getOptions()).andReturn(mockOptions); - expect(bigquery.update(eq(expectedRoutine))).andReturn(expectedUpdatedRoutine); - replay(bigquery); - initializeRoutine(); - Routine actualUpdatedRoutine = routine.update(); - compareRoutine(expectedUpdatedRoutine, actualUpdatedRoutine); - } - - @Test - public void testUpdateWithOptions() { - initializeExpectedRoutine(4); - Routine expectedUpdatedRoutine = expectedRoutine.toBuilder().setBody("body2").build(); - expect(bigquery.getOptions()).andReturn(mockOptions); - expect(bigquery.update(eq(expectedRoutine), eq(BigQuery.RoutineOption.fields()))) - .andReturn(expectedUpdatedRoutine); - replay(bigquery); - initializeRoutine(); - Routine actualUpdatedRoutine = routine.update(BigQuery.RoutineOption.fields()); - compareRoutine(expectedUpdatedRoutine, actualUpdatedRoutine); - } - - @Test - public void testDeleteTrue() { - initializeExpectedRoutine(1); - expect(bigquery.getOptions()).andReturn(mockOptions); - expect(bigquery.delete(ROUTINE_INFO.getRoutineId())).andReturn(true); - replay(bigquery); - initializeRoutine(); - assertTrue(routine.delete()); - } - - @Test - public void testDeleteFalse() { - initializeExpectedRoutine(1); - expect(bigquery.getOptions()).andReturn(mockOptions); - expect(bigquery.delete(ROUTINE_INFO.getRoutineId())).andReturn(false); - replay(bigquery); - initializeRoutine(); - assertFalse(routine.delete()); - } - - private void compareRoutine(Routine expected, Routine value) { - assertEquals(expected, value); - compareRoutineInfo(expected, value); - assertEquals(expected.getBigQuery().getOptions(), value.getBigQuery().getOptions()); - } - - public void compareRoutineInfo(RoutineInfo expected, RoutineInfo value) { - assertEquals(expected, value); - assertEquals(expected.getRoutineId(), value.getRoutineId()); - assertEquals(expected.getEtag(), value.getEtag()); - assertEquals(expected.getRoutineType(), value.getRoutineType()); - assertEquals(expected.getCreationTime(), value.getCreationTime()); - assertEquals(expected.getLastModifiedTime(), value.getLastModifiedTime()); - assertEquals(expected.getLanguage(), value.getLanguage()); - assertEquals(expected.getArguments(), value.getArguments()); - assertEquals(expected.getReturnType(), value.getReturnType()); - assertEquals(expected.getImportedLibraries(), value.getImportedLibraries()); - assertEquals(expected.getBody(), value.getBody()); - assertEquals(expected.hashCode(), value.hashCode()); - } - + assertEquals(ETAG, builtRoutine.getEtag()); + assertSame(serviceMockReturnsOptions, builtRoutine.getBigQuery()); + } + + @Test + public void testToBuilder() { + initializeExpectedRoutine(2); + replay(bigquery); + compareRoutineInfo(expectedRoutine, expectedRoutine.toBuilder().build()); + } + + @Test + public void testExists_True() throws Exception { + initializeExpectedRoutine(1); + BigQuery.RoutineOption[] expectedOptions = {BigQuery.RoutineOption.fields()}; + expect(bigquery.getOptions()).andReturn(mockOptions); + expect(bigquery.getRoutine(ROUTINE_INFO.getRoutineId(), expectedOptions)).andReturn(null); + replay(bigquery); + initializeRoutine(); + assertFalse(routine.exists()); + } + + @Test + public void testExists_False() throws Exception { + initializeExpectedRoutine(1); + BigQuery.RoutineOption[] expectedOptions = {BigQuery.RoutineOption.fields()}; + expect(bigquery.getOptions()).andReturn(mockOptions); + expect(bigquery.getRoutine(ROUTINE_INFO.getRoutineId(), expectedOptions)) + .andReturn(expectedRoutine); + replay(bigquery); + initializeRoutine(); + assertTrue(routine.exists()); + } + + @Test + public void testReload() throws Exception { + initializeExpectedRoutine(4); + RoutineInfo updatedInfo = ROUTINE_INFO.toBuilder().setBody("body2").build(); + Routine expectedRoutine = + new Routine(serviceMockReturnsOptions, new RoutineInfo.BuilderImpl(updatedInfo)); + expect(bigquery.getOptions()).andReturn(mockOptions); + expect(bigquery.getRoutine(ROUTINE_INFO.getRoutineId())).andReturn(expectedRoutine); + replay(bigquery); + initializeRoutine(); + Routine updatedRoutine = routine.reload(); + compareRoutine(expectedRoutine, updatedRoutine); + } + + @Test + public void testReload_Null() throws Exception { + initializeExpectedRoutine(1); + expect(bigquery.getOptions()).andReturn(mockOptions); + expect(bigquery.getRoutine(ROUTINE_INFO.getRoutineId())).andReturn(null); + replay(bigquery); + initializeRoutine(); + assertNull(routine.reload()); + } + + @Test + public void testUpdate() { + initializeExpectedRoutine(4); + Routine expectedUpdatedRoutine = expectedRoutine.toBuilder().setBody("body2").build(); + expect(bigquery.getOptions()).andReturn(mockOptions); + expect(bigquery.update(eq(expectedRoutine))).andReturn(expectedUpdatedRoutine); + replay(bigquery); + initializeRoutine(); + Routine actualUpdatedRoutine = routine.update(); + compareRoutine(expectedUpdatedRoutine, actualUpdatedRoutine); + } + + @Test + public void testUpdateWithOptions() { + initializeExpectedRoutine(4); + Routine expectedUpdatedRoutine = expectedRoutine.toBuilder().setBody("body2").build(); + expect(bigquery.getOptions()).andReturn(mockOptions); + expect(bigquery.update(eq(expectedRoutine), eq(BigQuery.RoutineOption.fields()))) + .andReturn(expectedUpdatedRoutine); + replay(bigquery); + initializeRoutine(); + Routine actualUpdatedRoutine = routine.update(BigQuery.RoutineOption.fields()); + compareRoutine(expectedUpdatedRoutine, actualUpdatedRoutine); + } + + @Test + public void testDeleteTrue() { + initializeExpectedRoutine(1); + expect(bigquery.getOptions()).andReturn(mockOptions); + expect(bigquery.delete(ROUTINE_INFO.getRoutineId())).andReturn(true); + replay(bigquery); + initializeRoutine(); + assertTrue(routine.delete()); + } + + @Test + public void testDeleteFalse() { + initializeExpectedRoutine(1); + expect(bigquery.getOptions()).andReturn(mockOptions); + expect(bigquery.delete(ROUTINE_INFO.getRoutineId())).andReturn(false); + replay(bigquery); + initializeRoutine(); + assertFalse(routine.delete()); + } + + private void compareRoutine(Routine expected, Routine value) { + assertEquals(expected, value); + compareRoutineInfo(expected, value); + assertEquals(expected.getBigQuery().getOptions(), value.getBigQuery().getOptions()); + } + + public void compareRoutineInfo(RoutineInfo expected, RoutineInfo value) { + assertEquals(expected, value); + assertEquals(expected.getRoutineId(), value.getRoutineId()); + assertEquals(expected.getEtag(), value.getEtag()); + assertEquals(expected.getRoutineType(), value.getRoutineType()); + assertEquals(expected.getCreationTime(), value.getCreationTime()); + assertEquals(expected.getLastModifiedTime(), value.getLastModifiedTime()); + assertEquals(expected.getLanguage(), value.getLanguage()); + assertEquals(expected.getArguments(), value.getArguments()); + assertEquals(expected.getReturnType(), value.getReturnType()); + assertEquals(expected.getImportedLibraries(), value.getImportedLibraries()); + assertEquals(expected.getBody(), value.getBody()); + assertEquals(expected.hashCode(), value.hashCode()); + } } diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLDataTypeTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLDataTypeTest.java index 9bc1e147d579..635a75612b94 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLDataTypeTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLDataTypeTest.java @@ -15,59 +15,59 @@ */ package com.google.cloud.bigquery; +import static org.junit.Assert.assertEquals; + import com.google.common.collect.ImmutableList; -import org.junit.Test; import java.util.List; - -import static org.junit.Assert.assertEquals; +import org.junit.Test; public class StandardSQLDataTypeTest { - private static final String STRING_TYPEKIND="STRING"; - private static final String ARRAY_TYPEKIND="ARRAY"; - private static final String STRUCT_TYPEKIND="STRUCT"; - - - - private static final StandardSQLDataType STRING_DATA_TYPE = StandardSQLDataType.newBuilder(STRING_TYPEKIND).build(); - private static final StandardSQLDataType ARRAY_OF_STRING_DATA_TYPE = StandardSQLDataType.newBuilder(ARRAY_TYPEKIND).setArrayElementType(STRING_DATA_TYPE).build(); - - private static final List FIELD_LIST = ImmutableList.of( - StandardSQLField.newBuilder(STRING_DATA_TYPE).build(), - StandardSQLField.newBuilder(ARRAY_OF_STRING_DATA_TYPE).build() - ); - private static final StandardSQLStructType STRUCT_TYPE = StandardSQLStructType.newBuilder(FIELD_LIST).build(); - - private static final StandardSQLDataType STRUCT_DATA_TYPE = StandardSQLDataType.newBuilder(STRUCT_TYPEKIND).setStructType(STRUCT_TYPE).build(); - - - - @Test - public void testToBuilder() { - compareStandardSQLDataType(STRING_DATA_TYPE,STRING_DATA_TYPE.toBuilder().build()); - compareStandardSQLDataType(ARRAY_OF_STRING_DATA_TYPE,ARRAY_OF_STRING_DATA_TYPE.toBuilder().build()); - compareStandardSQLDataType(STRUCT_DATA_TYPE,STRUCT_DATA_TYPE.toBuilder().build()); - - - } - - @Test - public void testBuilder() { - assertEquals(STRING_TYPEKIND, STRING_DATA_TYPE.getTypeKind()); - assertEquals(ARRAY_TYPEKIND, ARRAY_OF_STRING_DATA_TYPE.getTypeKind()); - assertEquals(STRING_DATA_TYPE, ARRAY_OF_STRING_DATA_TYPE.getArrayElementType()); - assertEquals(STRUCT_TYPE, STRUCT_DATA_TYPE.getStructType()); - } - - @Test - public void testToAndFromPb() { - compareStandardSQLDataType(ARRAY_OF_STRING_DATA_TYPE, StandardSQLDataType.fromPb(ARRAY_OF_STRING_DATA_TYPE.toPb())); - } - - private void compareStandardSQLDataType(StandardSQLDataType expected, StandardSQLDataType value) { - assertEquals(expected, value); - assertEquals(expected.getTypeKind(), value.getTypeKind()); - assertEquals(expected.getArrayElementType(), value.getArrayElementType()); - assertEquals(expected.getStructType(), value.getStructType()); - assertEquals(expected.hashCode(), value.hashCode()); - } + private static final String STRING_TYPEKIND = "STRING"; + private static final String ARRAY_TYPEKIND = "ARRAY"; + private static final String STRUCT_TYPEKIND = "STRUCT"; + + private static final StandardSQLDataType STRING_DATA_TYPE = + StandardSQLDataType.newBuilder(STRING_TYPEKIND).build(); + private static final StandardSQLDataType ARRAY_OF_STRING_DATA_TYPE = + StandardSQLDataType.newBuilder(ARRAY_TYPEKIND).setArrayElementType(STRING_DATA_TYPE).build(); + + private static final List FIELD_LIST = + ImmutableList.of( + StandardSQLField.newBuilder(STRING_DATA_TYPE).build(), + StandardSQLField.newBuilder(ARRAY_OF_STRING_DATA_TYPE).build()); + private static final StandardSQLStructType STRUCT_TYPE = + StandardSQLStructType.newBuilder(FIELD_LIST).build(); + + private static final StandardSQLDataType STRUCT_DATA_TYPE = + StandardSQLDataType.newBuilder(STRUCT_TYPEKIND).setStructType(STRUCT_TYPE).build(); + + @Test + public void testToBuilder() { + compareStandardSQLDataType(STRING_DATA_TYPE, STRING_DATA_TYPE.toBuilder().build()); + compareStandardSQLDataType( + ARRAY_OF_STRING_DATA_TYPE, ARRAY_OF_STRING_DATA_TYPE.toBuilder().build()); + compareStandardSQLDataType(STRUCT_DATA_TYPE, STRUCT_DATA_TYPE.toBuilder().build()); + } + + @Test + public void testBuilder() { + assertEquals(STRING_TYPEKIND, STRING_DATA_TYPE.getTypeKind()); + assertEquals(ARRAY_TYPEKIND, ARRAY_OF_STRING_DATA_TYPE.getTypeKind()); + assertEquals(STRING_DATA_TYPE, ARRAY_OF_STRING_DATA_TYPE.getArrayElementType()); + assertEquals(STRUCT_TYPE, STRUCT_DATA_TYPE.getStructType()); + } + + @Test + public void testToAndFromPb() { + compareStandardSQLDataType( + ARRAY_OF_STRING_DATA_TYPE, StandardSQLDataType.fromPb(ARRAY_OF_STRING_DATA_TYPE.toPb())); + } + + private void compareStandardSQLDataType(StandardSQLDataType expected, StandardSQLDataType value) { + assertEquals(expected, value); + assertEquals(expected.getTypeKind(), value.getTypeKind()); + assertEquals(expected.getArrayElementType(), value.getArrayElementType()); + assertEquals(expected.getStructType(), value.getStructType()); + assertEquals(expected.hashCode(), value.hashCode()); + } } diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLFieldTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLFieldTest.java index d8215e58710b..904ed80280f6 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLFieldTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLFieldTest.java @@ -16,57 +16,44 @@ package com.google.cloud.bigquery; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import com.google.api.services.bigquery.model.TrainingOptions; -import com.google.api.services.bigquery.model.TrainingRun; -import java.util.Arrays; -import java.util.List; import org.junit.Test; public class StandardSQLFieldTest { - - private static final String NAME = "field_name"; - private static final StandardSQLDataType STRING_DATA_TYPE = StandardSQLDataType - .newBuilder("STRING") - .build(); - private static final StandardSQLDataType ARRAY_OF_STRING_DATA_TYPE = StandardSQLDataType - .newBuilder("ARRAY") - .setArrayElementType(STRING_DATA_TYPE) - .build(); - private static final StandardSQLField STANDARD_SQL_FIELD_1 = StandardSQLField - .newBuilder(STRING_DATA_TYPE) - .build(); - private static final StandardSQLField STANDARD_SQL_FIELD_2 = StandardSQLField - .newBuilder(NAME, ARRAY_OF_STRING_DATA_TYPE) - .build(); - - - - @Test - public void testToBuilder() { - compareStandardSQLField(STANDARD_SQL_FIELD_1, STANDARD_SQL_FIELD_1.toBuilder().build()); - compareStandardSQLField(STANDARD_SQL_FIELD_2, STANDARD_SQL_FIELD_2.toBuilder().build()); - - } - - @Test - public void testBuilder() { - assertEquals(null, STANDARD_SQL_FIELD_1.getName()); - assertEquals(STRING_DATA_TYPE, STANDARD_SQL_FIELD_1.getDataType()); - assertEquals(ARRAY_OF_STRING_DATA_TYPE, STANDARD_SQL_FIELD_2.getDataType()); - } - - @Test - public void testToAndFromPb() { - compareStandardSQLField(STANDARD_SQL_FIELD_1, StandardSQLField.fromPb(STANDARD_SQL_FIELD_1.toPb())); - } - - private void compareStandardSQLField(StandardSQLField expected, StandardSQLField value) { - assertEquals(expected, value); - assertEquals(expected.getName(), value.getName()); - assertEquals(expected.getDataType(), value.getDataType()); - assertEquals(expected.hashCode(), value.hashCode()); - } + private static final String NAME = "field_name"; + private static final StandardSQLDataType STRING_DATA_TYPE = + StandardSQLDataType.newBuilder("STRING").build(); + private static final StandardSQLDataType ARRAY_OF_STRING_DATA_TYPE = + StandardSQLDataType.newBuilder("ARRAY").setArrayElementType(STRING_DATA_TYPE).build(); + private static final StandardSQLField STANDARD_SQL_FIELD_1 = + StandardSQLField.newBuilder(STRING_DATA_TYPE).build(); + private static final StandardSQLField STANDARD_SQL_FIELD_2 = + StandardSQLField.newBuilder(NAME, ARRAY_OF_STRING_DATA_TYPE).build(); + + @Test + public void testToBuilder() { + compareStandardSQLField(STANDARD_SQL_FIELD_1, STANDARD_SQL_FIELD_1.toBuilder().build()); + compareStandardSQLField(STANDARD_SQL_FIELD_2, STANDARD_SQL_FIELD_2.toBuilder().build()); + } + + @Test + public void testBuilder() { + assertEquals(null, STANDARD_SQL_FIELD_1.getName()); + assertEquals(STRING_DATA_TYPE, STANDARD_SQL_FIELD_1.getDataType()); + assertEquals(ARRAY_OF_STRING_DATA_TYPE, STANDARD_SQL_FIELD_2.getDataType()); + } + + @Test + public void testToAndFromPb() { + compareStandardSQLField( + STANDARD_SQL_FIELD_1, StandardSQLField.fromPb(STANDARD_SQL_FIELD_1.toPb())); + } + + private void compareStandardSQLField(StandardSQLField expected, StandardSQLField value) { + assertEquals(expected, value); + assertEquals(expected.getName(), value.getName()); + assertEquals(expected.getDataType(), value.getDataType()); + assertEquals(expected.hashCode(), value.hashCode()); + } } diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLStructTypeTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLStructTypeTest.java index f421d55d9d41..d4fa86950f02 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLStructTypeTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLStructTypeTest.java @@ -16,52 +16,44 @@ package com.google.cloud.bigquery; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - - -import java.util.Arrays; -import java.util.List; import com.google.common.collect.ImmutableList; +import java.util.List; import org.junit.Test; public class StandardSQLStructTypeTest { - - private static final StandardSQLField FIELD_1 = StandardSQLField - .newBuilder("FIELD_1", - StandardSQLDataType.newBuilder("STRING").build()) - .build(); - private static final StandardSQLField FIELD_2 = StandardSQLField - .newBuilder("FIELD_2", - StandardSQLDataType.newBuilder("FLOAT64").build()) - .build(); - - private static final List FIELD_LIST = ImmutableList.of(FIELD_1, FIELD_2); - private static final StandardSQLStructType STRUCT_TYPE = StandardSQLStructType.newBuilder(FIELD_LIST).build(); - - - - - @Test - public void testToBuilder() { - compareStandardSQLStructType(STRUCT_TYPE, STRUCT_TYPE.toBuilder().build()); - } - - @Test - public void testBuilder() { - assertEquals(FIELD_1, STRUCT_TYPE.getFields().get(0)); - assertEquals(FIELD_2, STRUCT_TYPE.getFields().get(1)); - } - - @Test - public void testToAndFromPb() { - compareStandardSQLStructType(STRUCT_TYPE, StandardSQLStructType.fromPb(STRUCT_TYPE.toPb())); - } - - private void compareStandardSQLStructType(StandardSQLStructType expected, StandardSQLStructType value) { - assertEquals(expected, value); - assertEquals(expected.getFields(), value.getFields()); - assertEquals(expected.hashCode(), value.hashCode()); - } + private static final StandardSQLField FIELD_1 = + StandardSQLField.newBuilder("FIELD_1", StandardSQLDataType.newBuilder("STRING").build()) + .build(); + private static final StandardSQLField FIELD_2 = + StandardSQLField.newBuilder("FIELD_2", StandardSQLDataType.newBuilder("FLOAT64").build()) + .build(); + + private static final List FIELD_LIST = ImmutableList.of(FIELD_1, FIELD_2); + private static final StandardSQLStructType STRUCT_TYPE = + StandardSQLStructType.newBuilder(FIELD_LIST).build(); + + @Test + public void testToBuilder() { + compareStandardSQLStructType(STRUCT_TYPE, STRUCT_TYPE.toBuilder().build()); + } + + @Test + public void testBuilder() { + assertEquals(FIELD_1, STRUCT_TYPE.getFields().get(0)); + assertEquals(FIELD_2, STRUCT_TYPE.getFields().get(1)); + } + + @Test + public void testToAndFromPb() { + compareStandardSQLStructType(STRUCT_TYPE, StandardSQLStructType.fromPb(STRUCT_TYPE.toPb())); + } + + private void compareStandardSQLStructType( + StandardSQLStructType expected, StandardSQLStructType value) { + assertEquals(expected, value); + assertEquals(expected.getFields(), value.getFields()); + assertEquals(expected.hashCode(), value.hashCode()); + } } diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index e161b9032d82..718a92ccfefa 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -16,6 +16,10 @@ package com.google.cloud.bigquery.it; +import static com.google.cloud.bigquery.JobStatus.State.DONE; +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.*; + import com.google.api.gax.paging.Page; import com.google.cloud.Date; import com.google.cloud.RetryOption; @@ -83,13 +87,6 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Sets; import com.google.common.io.BaseEncoding; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.Timeout; -import org.threeten.bp.Duration; - import java.io.IOException; import java.math.BigDecimal; import java.nio.ByteBuffer; @@ -107,10 +104,12 @@ import java.util.concurrent.TimeoutException; import java.util.logging.Level; import java.util.logging.Logger; - -import static com.google.cloud.bigquery.JobStatus.State.DONE; -import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.*; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.Timeout; +import org.threeten.bp.Duration; public class ITBigQueryTest { @@ -288,7 +287,7 @@ public static void beforeClass() throws InterruptedException, TimeoutException { DatasetInfo.newBuilder(MODEL_DATASET).setDescription("java model lifecycle").build(); bigquery.create(info2); DatasetInfo info3 = - DatasetInfo.newBuilder(ROUTINE_DATASET).setDescription("java routine lifecycle").build(); + DatasetInfo.newBuilder(ROUTINE_DATASET).setDescription("java routine lifecycle").build(); bigquery.create(info3); LoadJobConfiguration configuration = LoadJobConfiguration.newBuilder( @@ -307,7 +306,6 @@ public static void afterClass() throws ExecutionException, InterruptedException RemoteBigQueryHelper.forceDelete(bigquery, DATASET); RemoteBigQueryHelper.forceDelete(bigquery, MODEL_DATASET); RemoteBigQueryHelper.forceDelete(bigquery, ROUTINE_DATASET); - } if (storage != null) { boolean wasDeleted = RemoteStorageHelper.forceDelete(storage, BUCKET, 10, TimeUnit.SECONDS); @@ -1121,12 +1119,7 @@ public void testRoutineLifecycle() throws InterruptedException { String routineName = RemoteBigQueryHelper.generateRoutineName(); // Create a routine using SQL. String sql = - "CREATE FUNCTION `" - + ROUTINE_DATASET - + "." - + routineName - + "`" - + "(x INT64) AS (x * 3)"; + "CREATE FUNCTION `" + ROUTINE_DATASET + "." + routineName + "`" + "(x INT64) AS (x * 3)"; QueryJobConfiguration config = QueryJobConfiguration.newBuilder(sql).build(); Job job = bigquery.create(JobInfo.of(JobId.of(), config)); job.waitFor(); @@ -1139,7 +1132,9 @@ public void testRoutineLifecycle() throws InterruptedException { assertEquals(routine.getRoutineType(), "SCALAR_FUNCTION"); // Mutate metadata. - RoutineInfo newInfo = routine.toBuilder() + RoutineInfo newInfo = + routine + .toBuilder() .setBody("x * 4") .setReturnType(routine.getReturnType()) .setArguments(routine.getArguments()) @@ -1167,16 +1162,17 @@ public void testRoutineLifecycle() throws InterruptedException { public void testRoutineAPICreation() { String routineName = RemoteBigQueryHelper.generateRoutineName(); RoutineId routineId = RoutineId.of(ROUTINE_DATASET, routineName); - RoutineInfo routineInfo = RoutineInfo.newBuilder(routineId) + RoutineInfo routineInfo = + RoutineInfo.newBuilder(routineId) .setRoutineType("SCALAR_FUNCTION") .setBody("x * 3") .setLanguage("SQL") .setArguments( - ImmutableList.of( - RoutineArgument.newBuilder() - .setName("x") - .setDataType(StandardSQLDataType.newBuilder("INT64").build()) - .build())) + ImmutableList.of( + RoutineArgument.newBuilder() + .setName("x") + .setDataType(StandardSQLDataType.newBuilder("INT64").build()) + .build())) .build(); Routine routine = bigquery.create(routineInfo); From 7dbdb20b4f44f2cec8cc020ef959849ad813918a Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Mon, 8 Jul 2019 13:33:10 -0700 Subject: [PATCH 13/16] commenting pass. --- .../com/google/cloud/bigquery/BigQuery.java | 20 ++++-- .../com/google/cloud/bigquery/Routine.java | 15 ++++ .../cloud/bigquery/RoutineArgument.java | 26 +++++++ .../com/google/cloud/bigquery/RoutineId.java | 1 + .../google/cloud/bigquery/RoutineInfo.java | 68 +++++++++++++++++++ .../cloud/bigquery/StandardSQLDataType.java | 15 ++++ .../cloud/bigquery/StandardSQLField.java | 15 +++- .../cloud/bigquery/StandardSQLStructType.java | 7 ++ 8 files changed, 160 insertions(+), 7 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java index f6774b4dc0d9..7164849b9bde 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java @@ -658,7 +658,7 @@ public int hashCode() { Table create(TableInfo tableInfo, TableOption... options); /** - * Creates a new routine. TODO: docs + * Creates a new routine. * * @throws BigQueryException upon failure */ @@ -1004,7 +1004,7 @@ public int hashCode() { Model update(ModelInfo modelInfo, ModelOption... options); /** - * Updates routine information. TODO: docs + * Updates routine information. * * @throws BigQueryException upon failure */ @@ -1066,16 +1066,24 @@ public int hashCode() { */ Model getModel(ModelId tableId, ModelOption... options); - // TODO: docs + /** + * Returns the requested routine or {@code null} if not found. + * + * @throws BigQueryException upon failure + */ Routine getRoutine(String datasetId, String routineId, RoutineOption... options); - // TODO: docs + /** + * Returns the requested routine or {@code null} if not found. + * + * @throws BigQueryException upon failure + */ Routine getRoutine(RoutineId routineId, RoutineOption... options); - /** Lists the models in the dataset. */ + /** Lists the routines in the specified dataset. */ Page listRoutines(String datasetId, RoutineListOption... options); - /** Lists the models in the dataset. */ + /** Lists the routines in the specified dataset. */ Page listRoutines(DatasetId datasetId, RoutineListOption... options); /** diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java index 260777db27e2..61c8848fb188 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Routine.java @@ -123,22 +123,37 @@ public Routine build() { this.options = bigquery.getOptions(); } + /** Checks if this routine exists. */ public boolean exists() { return bigquery.getRoutine(getRoutineId(), RoutineOption.fields()) != null; } + /** + * Fetches this routine's latest information. Returns {@code null} if the routine does not exist. + */ public Routine reload(RoutineOption... options) { return bigquery.getRoutine(getRoutineId(), options); } + /** + * Update's the routine's information with this Routine's information. This method does not allow + * changing the RoutineId identifier of the routine. A new {@code Routine} is returned. + */ public Routine update(RoutineOption... options) { return bigquery.update(this, options); } + /** + * Deletes this routine. + * + * @return {@code true} if routine was deleted, {@code false} if it was not found + * @throws BigQueryException upon failure + */ public boolean delete() { return bigquery.delete(getRoutineId()); } + /** Returns the routine's {@code BigQuery} object used to issue requests. */ public BigQuery getBigQuery() { return bigquery; } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineArgument.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineArgument.java index 6c6ba2cd97d3..d36d1f229517 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineArgument.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineArgument.java @@ -20,6 +20,7 @@ import com.google.common.base.Function; import javax.annotation.Nullable; +/** An argument for a BigQuery Routine. */ @AutoValue public abstract class RoutineArgument { @@ -40,31 +41,56 @@ public Argument apply(RoutineArgument argument) { @AutoValue.Builder public abstract static class Builder { + /** Sets the argument name. */ public abstract Builder setName(String name); + /** + * Sets the kind of argument. + * + *

A FIXED_TYPE argument is a fully specified type. It can be a struct or an array, but not a + * table. + * + *

An ANY_TYPE argument is any type. It can be a struct or an array, but not a table. + */ public abstract Builder setKind(String kind); + /** + * Optionally specifies the input/output mode of the argument. + * + *

An IN mode argument is input-only. An OUT mode argument is output-only. An INOUT mode + * argument is both an input and output. + */ public abstract Builder setMode(String mode); + /** + * Sets the data type specification for the argument. It is required except for ANY_TYPE + * argument kinds. + */ public abstract Builder setDataType(StandardSQLDataType dataType); + /** Creates a {@code RoutineArgument} object. */ public abstract RoutineArgument build(); } + /** Returns the name of the argument. */ @Nullable public abstract String getName(); + /** Returns the kind of the argument. */ @Nullable public abstract String getKind(); + /** Returns the mode of the argument. */ @Nullable public abstract String getMode(); @Nullable public abstract StandardSQLDataType getDataType(); + /** Returns a builder pre-populated using the current values of this {@code RoutineArgument}. */ public abstract Builder toBuilder(); + /** Returns a builder for a {@Code RoutineArgument} object. */ public static Builder newBuilder() { return new AutoValue_RoutineArgument.Builder(); } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineId.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineId.java index 5230282e3199..d5d3c5bfe982 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineId.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineId.java @@ -25,6 +25,7 @@ import java.io.Serializable; import java.util.Objects; +/** RoutineId represents the identifier for a given Routine. */ public final class RoutineId implements Serializable { static final Function FROM_PB_FUNCTION = diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java index 7074343a1048..ef8ecf71c429 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java @@ -27,6 +27,15 @@ import java.util.List; import java.util.Objects; +/** + * Google BigQuery routine information. + * + *

For more information about the REST representation of routines, see: + * https://cloud.google.com/bigquery/docs/reference/rest/v2/routines + * + *

For more information about interacting with scalar functions via DDL query jobs, see: + * https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_function_statement + */ public class RoutineInfo implements Serializable { static final Function FROM_PB_FUNCTION = @@ -56,26 +65,67 @@ public Routine apply(RoutineInfo routineInfo) { private final String body; public abstract static class Builder { + abstract Builder setRoutineId(RoutineId id); abstract Builder setEtag(String etag); + /** Sets the routine type for the Builder (e.g. SCALAR_FUNCTION). * */ public abstract Builder setRoutineType(String routineType); abstract Builder setCreationTime(Long creationMillis); abstract Builder setLastModifiedTime(Long lastModifiedMillis); + /** Sets the language for the routine (e.g. SQL or JAVASCRIPT) */ public abstract Builder setLanguage(String language); + /** Specifies the list of arguments for the routine. */ public abstract Builder setArguments(List argumentList); + /** + * Sets the return type of the routine. + * + *

Optional if language = "SQL"; required otherwise. + * + *

If absent, the return type is inferred from definitionBody at query time in each query + * that references this routine. If present, then the evaluated result will be cast to the + * specified returned type at query time. + */ public abstract Builder setReturnType(StandardSQLDataType returnType); + /** + * Optional. If language = "JAVASCRIPT", this field stores the path of the imported JAVASCRIPT + * libraries as a list of gs:// URLs. + */ public abstract Builder setImportedLibraries(List importedLibrariesList); + /** + * Required. The body of the routine. + * + *

For functions, this is the expression in the AS clause. + * + *

If language=SQL, it is the substring inside (but excluding) the parentheses. For example, + * for the function created with the following statement: + * + *

CREATE FUNCTION JoinLines(x string, y string) as (concat(x, "\n", y)) + * + *

The definitionBody is concat(x, "\n", y) (\n is not replaced with linebreak). + * + *

If language=JAVASCRIPT, it is the evaluated string in the AS clause. For example, for the + * function created with the following statement: + * + *

CREATE FUNCTION f() RETURNS STRING LANGUAGE js AS 'return "\n";\n' + * + *

The definitionBody is + * + *

return "\n";\n + * + *

Note that both \n are replaced with linebreaks. + */ public abstract Builder setBody(String body); + /** Creates a {@code RoutineInfo} object. */ public abstract RoutineInfo build(); } @@ -207,46 +257,64 @@ public RoutineInfo build() { this.body = builder.body; } + /** Returns the RoutineId identified for the routine resource. * */ public RoutineId getRoutineId() { return routineId; } + /** Returns the hash of the routine resource. */ public String getEtag() { return etag; } + /** Returns the type of the routine, e.g. SCALAR_FUNCTION. */ public String getRoutineType() { return routineType; } + /** Returns the creation time of the routine, represented as milliseconds since the epoch. */ public Long getCreationTime() { return creationTime; } + /** + * Returns the last modification time of the routine, represented as milliseconds since the epoch. + */ public Long getLastModifiedTime() { return lastModifiedTime; } + /** + * Returns the language of the routine. Currently supported languages include SQL and JAVASCRIPT. + */ public String getLanguage() { return language; } + /** Returns the list of arguments for the routine. */ public List getArguments() { return argumentList; } + /** If specified, returns the data type returned from the routine. */ public StandardSQLDataType getReturnType() { return returnType; } + /** + * Returns the list of imported libraries for the routine. Only relevant for routines implemented + * using the JAVASCRIPT language. + */ public List getImportedLibraries() { return importedLibrariesList; } + /** Returns the definition body of the routine. */ public String getBody() { return body; } + /** Returns a builder pre-populated using the current values of this routine. */ public Builder toBuilder() { return new BuilderImpl(this); } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java index c563f288f35a..99ed10af1461 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLDataType.java @@ -20,6 +20,7 @@ import java.io.Serializable; import javax.annotation.Nullable; +/** Represents Standard SQL data type information. */ @AutoValue public abstract class StandardSQLDataType implements Serializable { @@ -41,11 +42,19 @@ public abstract static class Builder { public abstract StandardSQLDataType build(); } + /** + * Returns the type kind of the data type. + * + *

Can be any standard SQL data type. For more information, see + * https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types + */ public abstract String getTypeKind(); + /** Returns the type of an ARRAY's elements. */ @Nullable public abstract StandardSQLDataType getArrayElementType(); + /** Returns the struct definition's list of fields for a STRUCT type. */ @Nullable public abstract StandardSQLStructType getStructType(); @@ -55,10 +64,16 @@ public static Builder newBuilder() { return new AutoValue_StandardSQLDataType.Builder(); } + /** Returns a new builder initialized with the type kind. */ public static Builder newBuilder(String typeKind) { return newBuilder().setTypeKind(typeKind); } + /** Returns a new builder initialized with a StandardSQLTypeName as the type kind. */ + public static Builder newBuilder(StandardSQLTypeName typeName) { + return newBuilder().setTypeKind(typeName.toString()); + } + StandardSqlDataType toPb() { StandardSqlDataType dataTypePb = new StandardSqlDataType(); dataTypePb.setTypeKind(getTypeKind()); diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java index 0a6e2eaa4a19..013742548720 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLField.java @@ -21,6 +21,7 @@ import java.io.Serializable; import javax.annotation.Nullable; +/** A Google BigQuery SQL Field. */ @AutoValue public abstract class StandardSQLField implements Serializable { @@ -42,33 +43,45 @@ public StandardSqlField apply(StandardSQLField field) { @AutoValue.Builder public abstract static class Builder { + /** Sets the name of the field. */ public abstract Builder setName(String name); + /** Sets the data type of the field. */ public abstract Builder setDataType(StandardSQLDataType dataType); + /** Creates a {@code StandardSQLField} object. */ public abstract StandardSQLField build(); } + /** Returns the field name. */ @Nullable public abstract String getName(); + /** Returns the field's data type. */ public abstract StandardSQLDataType getDataType(); + /** Returns a builder pre-populated using the current values of this field. */ public abstract Builder toBuilder(); + /** Returns a builder for a {@code StandardSQLField} object. */ public static Builder newBuilder() { return new AutoValue_StandardSQLField.Builder(); } + /** Returns a builder for a {@code StandardSQLField} object with the specified data type. */ public static Builder newBuilder(StandardSQLDataType dataType) { return newBuilder().setDataType(dataType); } + /** + * Returns a builder for a {@code StandardSQLField} object with the specified field name and data + * type. + */ public static Builder newBuilder(String name, StandardSQLDataType dataType) { return newBuilder().setName(name).setDataType(dataType); } - public StandardSqlField toPb() { + StandardSqlField toPb() { StandardSqlField fieldPb = new StandardSqlField(); fieldPb.setName(getName()); if (getDataType() != null) { diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLStructType.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLStructType.java index 9f6685a57f45..43844916ec12 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLStructType.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLStructType.java @@ -22,25 +22,32 @@ import java.io.Serializable; import java.util.List; +/** A set of fields contained within a SQL STRUCT in Google BigQuery. */ @AutoValue public abstract class StandardSQLStructType implements Serializable { @AutoValue.Builder public abstract static class Builder { + /** Sets the fields of the struct type. */ public abstract Builder setFields(List fields); + /** Creates a {@code StandardSQLStructType} object. */ public abstract StandardSQLStructType build(); } + /** Returns the list of fields within a struct type. */ public abstract List getFields(); + /** Returns a builder pre-populated using the current values of this field. */ public abstract Builder toBuilder(); + /** Returns a builder for a {@code StandardSQLStructType} object. */ public static Builder newBuilder() { return new AutoValue_StandardSQLStructType.Builder(); } + /** Returns a builder for a {@code StandardSQLStructType} object with the specified fields. */ public static Builder newBuilder(List fieldList) { return newBuilder().setFields(fieldList); } From 1387b02be8348954f049eb279c91ebc483074ea3 Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Tue, 9 Jul 2019 08:47:32 -0700 Subject: [PATCH 14/16] more documentation and cleanup. --- .../com/google/cloud/bigquery/BigQuery.java | 6 ++--- .../cloud/bigquery/spi/v2/BigQueryRpc.java | 26 ++++++++++++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java index 7164849b9bde..3bc37726dde1 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java @@ -300,7 +300,7 @@ public static ModelListOption pageToken(String pageToken) { } } - /** Class for specifying table list options. */ + /** Class for specifying routine list options. */ class RoutineListOption extends Option { private static final long serialVersionUID = 8660294969063312498L; @@ -309,13 +309,13 @@ private RoutineListOption(BigQueryRpc.Option option, Object value) { super(option, value); } - /** Returns an option to specify the maximum number of models returned per page. */ + /** Returns an option to specify the maximum number of routines returned per page. */ public static RoutineListOption pageSize(long pageSize) { checkArgument(pageSize >= 0); return new RoutineListOption(BigQueryRpc.Option.MAX_RESULTS, pageSize); } - /** Returns an option to specify the page token from which to start listing models. */ + /** Returns an option to specify the page token from which to start listing routines. */ public static RoutineListOption pageToken(String pageToken) { return new RoutineListOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken); } diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java index d003db49c105..29cc5a711f6b 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java @@ -159,7 +159,7 @@ Tuple> listTables( boolean deleteTable(String projectId, String datasetId, String tableId); /** - * Updates table information. + * Updates model information. * * @throws BigQueryException upon failure */ @@ -188,17 +188,35 @@ Tuple> listModels( */ boolean deleteModel(String projectId, String datasetId, String modelId); - // TODO: document these - + /** + * Creates the requested routine. + * + * @throws BigQueryException upon failure + */ Routine create(Routine routine, Map options); + /** + * Updates the requested routine. + * + * @throws BigQueryException upon failure + */ Routine update(Routine routine, Map options); + /** + * Returns the requested routine or {@code null} if not found. + * + * @throws BigQueryException upon failure + */ Routine getRoutine(String projectId, String datasetId, String routineId, Map options); Tuple> listRoutines( String projectId, String datasetId, Map options); - + /** + * Deletes the requested routine. + * + * @return {@code true} if routine was deleted, {@code false} if it was not found + * @throws BigQueryException upon failure + */ boolean deleteRoutine(String projectId, String datasetId, String routineId); /** From 0b3635f6f8f36cf6e29a411247209c7cc11d9619 Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Tue, 9 Jul 2019 09:01:01 -0700 Subject: [PATCH 15/16] more doc cleanup --- .../com/google/cloud/bigquery/RoutineInfo.java | 16 +++++++++++----- .../cloud/bigquery/spi/v2/BigQueryRpc.java | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java index ef8ecf71c429..1474b1994c9e 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/RoutineInfo.java @@ -28,13 +28,15 @@ import java.util.Objects; /** - * Google BigQuery routine information. + * Google BigQuery routine information. A Routine is an API abstraction that encapsulates several + * related concepts inside the BigQuery service, including scalar user defined functions (UDFS) and + * stored procedures. * *

For more information about the REST representation of routines, see: * https://cloud.google.com/bigquery/docs/reference/rest/v2/routines * - *

For more information about interacting with scalar functions via DDL query jobs, see: - * https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_function_statement + *

For more information about working with scalar functions, see: + * https://cloud.google.com/bigquery/docs/reference/standard-sql/user-defined-functions */ public class RoutineInfo implements Serializable { @@ -70,7 +72,11 @@ public abstract static class Builder { abstract Builder setEtag(String etag); - /** Sets the routine type for the Builder (e.g. SCALAR_FUNCTION). * */ + /** + * Sets the routine type for the Builder (e.g. SCALAR_FUNCTION). + * + *

See https://cloud.google.com/bigquery/docs/reference/rest/v2/routines + */ public abstract Builder setRoutineType(String routineType); abstract Builder setCreationTime(Long creationMillis); @@ -80,7 +86,7 @@ public abstract static class Builder { /** Sets the language for the routine (e.g. SQL or JAVASCRIPT) */ public abstract Builder setLanguage(String language); - /** Specifies the list of arguments for the routine. */ + /** Specifies the list of input/output arguments for the routine. */ public abstract Builder setArguments(List argumentList); /** diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java index 29cc5a711f6b..a0dde557d259 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java @@ -203,7 +203,7 @@ Tuple> listModels( Routine update(Routine routine, Map options); /** - * Returns the requested routine or {@code null} if not found. + * Returns the requested routine or {@code null} if not found. * * @throws BigQueryException upon failure */ From d9a549dfbd49d87da72a370ee3295347c8d6bf1b Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Thu, 11 Jul 2019 10:20:55 -0700 Subject: [PATCH 16/16] address reviewer comments --- .../src/main/java/com/google/cloud/bigquery/BigQuery.java | 2 +- .../java/com/google/cloud/bigquery/it/ITBigQueryTest.java | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java index 3bc37726dde1..9eb6b92d2325 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java @@ -901,7 +901,7 @@ public int hashCode() { * } else { * // the routine was not found * } - * } + * * * @return {@code true} if routine was deleted, {@code false} if it was not found * @throws BigQueryException upon failure diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index c7d6ec33bb8e..bb744d63ec59 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -18,7 +18,13 @@ import static com.google.cloud.bigquery.JobStatus.State.DONE; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.*; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import com.google.api.gax.paging.Page; import com.google.cloud.Date;