From d82555cc3e5973f746913be2bfb59a33cc8f18af Mon Sep 17 00:00:00 2001 From: obada-ab Date: Fri, 9 Jun 2023 16:45:05 +0200 Subject: [PATCH 01/10] feat: add primary key --- .../com/google/cloud/bigquery/PrimaryKey.java | 51 ++++++++++++++ .../bigquery/StandardTableDefinition.java | 20 ++++++ .../java/com/google/cloud/bigquery/Table.java | 6 ++ .../com/google/cloud/bigquery/TableInfo.java | 26 ++++++++ .../google/cloud/bigquery/PrimaryKeyTest.java | 57 ++++++++++++++++ .../cloud/bigquery/it/ITBigQueryTest.java | 66 +++++++++++++++++++ 6 files changed, 226 insertions(+) create mode 100644 google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/PrimaryKey.java create mode 100644 google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/PrimaryKeyTest.java diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/PrimaryKey.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/PrimaryKey.java new file mode 100644 index 0000000000..859f35f9d3 --- /dev/null +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/PrimaryKey.java @@ -0,0 +1,51 @@ +package com.google.cloud.bigquery; + +import com.google.auto.value.AutoValue; +import com.google.common.annotations.VisibleForTesting; +import java.io.Serializable; +import java.util.List; +import javax.annotation.Nullable; + +@AutoValue +public abstract class PrimaryKey implements Serializable { + public static PrimaryKey.Builder newBuilder() { + return new AutoValue_PrimaryKey.Builder(); + } + + static PrimaryKey fromPb( + com.google.api.services.bigquery.model.TableConstraints.PrimaryKey primaryKey) { + PrimaryKey.Builder builder = newBuilder(); + + if (primaryKey.getColumns() != null) { + builder.setColumns(primaryKey.getColumns()); + } + + return builder.build(); + } + + com.google.api.services.bigquery.model.TableConstraints.PrimaryKey toPb() { + + com.google.api.services.bigquery.model.TableConstraints.PrimaryKey primaryKey = + new com.google.api.services.bigquery.model.TableConstraints.PrimaryKey(); + primaryKey.setColumns(getColumns()); + + return primaryKey; + } + + @Nullable + public abstract List getColumns(); + + /** Returns a builder for primary key. */ + @VisibleForTesting + public abstract PrimaryKey.Builder toBuilder(); + + @AutoValue.Builder + public abstract static class Builder { + + /** The column names that are primary keys. * */ + public abstract PrimaryKey.Builder setColumns(List columns); + + /** Creates a {@code PrimaryKey} object. */ + public abstract PrimaryKey build(); + } +} diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java index 4b09fe3c40..f346a0cae9 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java @@ -18,6 +18,7 @@ import com.google.api.services.bigquery.model.Streamingbuffer; import com.google.api.services.bigquery.model.Table; +import com.google.api.services.bigquery.model.TableConstraints; import com.google.auto.value.AutoValue; import com.google.common.base.MoreObjects; import java.io.Serializable; @@ -161,6 +162,8 @@ public abstract static class Builder */ public abstract Builder setClustering(Clustering clustering); + public abstract Builder setPrimaryKey(PrimaryKey primaryKey); + /** Creates a {@code StandardTableDefinition} object. */ public abstract StandardTableDefinition build(); } @@ -221,6 +224,13 @@ public abstract static class Builder @Nullable public abstract Clustering getClustering(); + /** + * Returns the primary key for this table. Returns {@code null} if no primary keys are set for + * this table. + */ + @Nullable + public abstract PrimaryKey getPrimaryKey(); + /** Returns a builder for a BigQuery standard table definition. */ public static Builder newBuilder() { return new AutoValue_StandardTableDefinition.Builder().setType(Type.TABLE); @@ -259,6 +269,13 @@ Table toPb() { if (getClustering() != null) { tablePb.setClustering(getClustering().toPb()); } + if (getPrimaryKey() != null) { + if (tablePb.getTableConstraints() == null) { + tablePb.setTableConstraints(new TableConstraints()); + } + + tablePb.getTableConstraints().setPrimaryKey(getPrimaryKey().toPb()); + } return tablePb; } @@ -296,6 +313,9 @@ static StandardTableDefinition fromPb(Table tablePb) { if (tablePb.getNumLongTermBytes() != null) { builder.setNumLongTermBytes(tablePb.getNumLongTermBytes()); } + if (tablePb.getTableConstraints() != null) { + builder.setPrimaryKey(PrimaryKey.fromPb(tablePb.getTableConstraints().getPrimaryKey())); + } return builder.setNumBytes(tablePb.getNumBytes()).setLocation(tablePb.getLocation()).build(); } } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java index 3e67e8d750..0a80dc5d1f 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java @@ -168,6 +168,12 @@ public TableInfo.Builder setCloneDefinition(CloneDefinition cloneDefinition) { return this; } + @Override + public Builder setPrimaryKey(PrimaryKey primaryKey) { + infoBuilder.setPrimaryKey(primaryKey); + return this; + } + @Override public Table build() { return new Table(bigquery, infoBuilder); diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java index 19e9de2b79..17f1b7f21d 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java @@ -23,6 +23,7 @@ import com.google.api.client.util.Strings; import com.google.api.core.BetaApi; import com.google.api.services.bigquery.model.Table; +import com.google.api.services.bigquery.model.TableConstraints; import com.google.common.base.Function; import com.google.common.base.MoreObjects; import java.io.Serializable; @@ -75,6 +76,7 @@ public Table apply(TableInfo tableInfo) { private final String defaultCollation; private final CloneDefinition cloneDefinition; + private final PrimaryKey primaryKey; /** A builder for {@code TableInfo} objects. */ public abstract static class Builder { @@ -142,6 +144,8 @@ public Builder setRequirePartitionFilter(Boolean requirePartitionFilter) { public abstract Builder setDefaultCollation(String defaultCollation); public abstract Builder setCloneDefinition(CloneDefinition cloneDefinition); + + public abstract Builder setPrimaryKey(PrimaryKey primaryKey); } static class BuilderImpl extends Builder { @@ -164,6 +168,7 @@ static class BuilderImpl extends Builder { private Boolean requirePartitionFilter; private String defaultCollation; private CloneDefinition cloneDefinition; + private PrimaryKey primaryKey; BuilderImpl() {} @@ -186,6 +191,7 @@ static class BuilderImpl extends Builder { this.requirePartitionFilter = tableInfo.requirePartitionFilter; this.defaultCollation = tableInfo.defaultCollation; this.cloneDefinition = tableInfo.cloneDefinition; + this.primaryKey = tableInfo.primaryKey; } BuilderImpl(Table tablePb) { @@ -214,6 +220,9 @@ static class BuilderImpl extends Builder { if (tablePb.getCloneDefinition() != null) { this.cloneDefinition = CloneDefinition.fromPb(tablePb.getCloneDefinition()); } + if (tablePb.getTableConstraints() != null) { + this.primaryKey = PrimaryKey.fromPb(tablePb.getTableConstraints().getPrimaryKey()); + } } @Override @@ -323,6 +332,11 @@ public Builder setCloneDefinition(CloneDefinition cloneDefinition) { return this; } + public Builder setPrimaryKey(PrimaryKey primaryKey) { + this.primaryKey = primaryKey; + return this; + } + @Override public TableInfo build() { return new TableInfo(this); @@ -348,6 +362,7 @@ public TableInfo build() { this.requirePartitionFilter = builder.requirePartitionFilter; this.defaultCollation = builder.defaultCollation; this.cloneDefinition = builder.cloneDefinition; + this.primaryKey = builder.primaryKey; } /** Returns the hash of the table resource. */ @@ -458,6 +473,10 @@ public CloneDefinition getCloneDefinition() { return cloneDefinition; } + public PrimaryKey getPrimaryKey() { + return primaryKey; + } + /** Returns a builder for the table object. */ public Builder toBuilder() { return new BuilderImpl(this); @@ -484,6 +503,7 @@ public String toString() { .add("requirePartitionFilter", requirePartitionFilter) .add("defaultCollation", defaultCollation) .add("cloneDefinition", cloneDefinition) + .add("primaryKey", primaryKey) .toString(); } @@ -551,6 +571,12 @@ Table toPb() { if (cloneDefinition != null) { tablePb.setCloneDefinition(cloneDefinition.toPb()); } + if (primaryKey != null) { + if (tablePb.getTableConstraints() == null) { + tablePb.setTableConstraints(new TableConstraints()); + } + tablePb.getTableConstraints().setPrimaryKey(primaryKey.toPb()); + } return tablePb; } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/PrimaryKeyTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/PrimaryKeyTest.java new file mode 100644 index 0000000000..5863443975 --- /dev/null +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/PrimaryKeyTest.java @@ -0,0 +1,57 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.bigquery; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.List; +import org.junit.Test; + +public class PrimaryKeyTest { + private static final List COLUMNS = Arrays.asList("column1", "column2"); + private static final PrimaryKey PRIMARY_KEY = + PrimaryKey.newBuilder().setColumns(COLUMNS).build(); + + @Test + public void testToBuilder() { + comparePrimaryKeyDefinition(PRIMARY_KEY, PRIMARY_KEY.toBuilder().build()); + PrimaryKey primaryKey = + PRIMARY_KEY.toBuilder().setColumns(Arrays.asList("col1", "col2", "col3")).build(); + assertEquals(Arrays.asList("col1", "col2", "col3"), primaryKey.getColumns()); + } + + @Test + public void testBuilder() { + assertEquals(COLUMNS, PRIMARY_KEY.getColumns()); + PrimaryKey primaryKey = + PRIMARY_KEY.newBuilder().setColumns(COLUMNS).build(); + assertEquals(PRIMARY_KEY, primaryKey); + } + + @Test + public void testToAndFromPb() { + PrimaryKey primaryKey = PRIMARY_KEY.toBuilder().build(); + assertTrue(PrimaryKey.fromPb(primaryKey.toPb()) instanceof PrimaryKey); + comparePrimaryKeyDefinition(primaryKey, PrimaryKey.fromPb(primaryKey.toPb())); + } + + private void comparePrimaryKeyDefinition(PrimaryKey expected, PrimaryKey value) { + assertEquals(expected.getColumns(), value.getColumns()); + } +} diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index cdfcb80033..16677efd6c 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -97,6 +97,7 @@ import com.google.cloud.bigquery.Parameter; import com.google.cloud.bigquery.ParquetOptions; import com.google.cloud.bigquery.PolicyTags; +import com.google.cloud.bigquery.PrimaryKey; import com.google.cloud.bigquery.QueryJobConfiguration; import com.google.cloud.bigquery.QueryParameterValue; import com.google.cloud.bigquery.RangePartitioning; @@ -157,6 +158,7 @@ import java.time.LocalTime; import java.time.Period; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -5672,4 +5674,68 @@ public void testHivePartitioningOptionsFieldsFieldExistence() throws Interrupted assertTrue(table.delete()); assertTrue(storage.delete(blobInfo.getBlobId())); } + + @Test + public void primaryKeyTest() { + String tableName = "test_primary_key"; + TableId tableId = TableId.of(DATASET, tableName); + PrimaryKey primaryKey = PrimaryKey.newBuilder().setColumns(Arrays.asList("ID")).build(); + + try { + StandardTableDefinition tableDefinition = + StandardTableDefinition.newBuilder() + .setSchema(Schema.of( + Field.newBuilder("ID", LegacySQLTypeName.STRING).setMode(Field.Mode.NULLABLE).build(), + Field.newBuilder("FirstName", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("LastName", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build())) + .setPrimaryKey(primaryKey) + .build(); + Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition)); + assertNotNull(createdTable); + Table remoteTable = bigquery.getTable(DATASET, tableName); + assertEquals( + primaryKey, + remoteTable.getDefinition().getPrimaryKey()); + } finally { + bigquery.delete(tableId); + } + } + + @Test + public void primaryKeyUpdateTest() { + String tableName = "test_primary_key_update"; + TableId tableId = TableId.of(DATASET, tableName); + PrimaryKey primaryKey = PrimaryKey.newBuilder().setColumns(Arrays.asList("FirstName", "LastName")).build(); + + try { + StandardTableDefinition tableDefinition = + StandardTableDefinition.newBuilder() + .setSchema(Schema.of( + Field.newBuilder("ID", LegacySQLTypeName.STRING).setMode(Field.Mode.NULLABLE).build(), + Field.newBuilder("FirstName", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("LastName", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build())) + .build(); + Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition)); + assertNotNull(createdTable); + Table remoteTable = bigquery.getTable(DATASET, tableName); + assertNull(remoteTable.getDefinition().getPrimaryKey()); + + Table updatedTable = remoteTable.toBuilder().setPrimaryKey(primaryKey).build().update(); + assertNotNull(updatedTable); + Table remoteUpdatedTable = bigquery.getTable(DATASET, tableName); + assertEquals( + primaryKey, + remoteUpdatedTable.getDefinition().getPrimaryKey()); + } finally { + bigquery.delete(tableId); + } + } } From 70218ebb41c45e521403055f89d514f2afb2af2a Mon Sep 17 00:00:00 2001 From: obada-ab Date: Tue, 13 Jun 2023 16:40:20 +0200 Subject: [PATCH 02/10] feat: add foreign keys and column references --- .../cloud/bigquery/ColumnReference.java | 79 ++++++++ .../com/google/cloud/bigquery/ForeignKey.java | 111 ++++++++++++ .../com/google/cloud/bigquery/PrimaryKey.java | 16 ++ .../bigquery/StandardTableDefinition.java | 31 +++- .../java/com/google/cloud/bigquery/Table.java | 6 + .../com/google/cloud/bigquery/TableInfo.java | 36 +++- .../google/cloud/bigquery/ForeignKeyTest.java | 95 ++++++++++ .../google/cloud/bigquery/PrimaryKeyTest.java | 6 +- .../cloud/bigquery/it/ITBigQueryTest.java | 171 +++++++++++++++--- 9 files changed, 520 insertions(+), 31 deletions(-) create mode 100644 google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ColumnReference.java create mode 100644 google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ForeignKey.java create mode 100644 google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ForeignKeyTest.java diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ColumnReference.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ColumnReference.java new file mode 100644 index 0000000000..3dc688be5f --- /dev/null +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ColumnReference.java @@ -0,0 +1,79 @@ +/* + * Copyright 2023 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.auto.value.AutoValue; +import com.google.common.annotations.VisibleForTesting; +import javax.annotation.Nullable; + +@AutoValue +public abstract class ColumnReference { + public static ColumnReference.Builder newBuilder() { + return new AutoValue_ColumnReference.Builder(); + } + + static ColumnReference fromPb( + com.google.api.services.bigquery.model.TableConstraints.ForeignKeys.ColumnReferences + columnReference) { + ColumnReference.Builder builder = newBuilder(); + + if (columnReference.getReferencedColumn() != null) { + builder.setReferencedColumn(columnReference.getReferencedColumn()); + } + + if (columnReference.getReferencingColumn() != null) { + builder.setReferencingColumn(columnReference.getReferencingColumn()); + } + + return builder.build(); + } + + com.google.api.services.bigquery.model.TableConstraints.ForeignKeys.ColumnReferences toPb() { + + com.google.api.services.bigquery.model.TableConstraints.ForeignKeys.ColumnReferences + columnReference = + new com.google.api.services.bigquery.model.TableConstraints.ForeignKeys + .ColumnReferences(); + columnReference.setReferencedColumn(getReferencedColumn()); + columnReference.setReferencingColumn(getReferencingColumn()); + + return columnReference; + } + + @Nullable + public abstract String getReferencedColumn(); + + @Nullable + public abstract String getReferencingColumn(); + + /** Returns a builder for column reference. */ + @VisibleForTesting + public abstract ColumnReference.Builder toBuilder(); + + @AutoValue.Builder + public abstract static class Builder { + + /** The target column of this reference. * */ + public abstract ColumnReference.Builder setReferencedColumn(String referencedColumn); + + /** The source column of this reference. * */ + public abstract ColumnReference.Builder setReferencingColumn(String referencingColumn); + + /** Creates a {@code ColumnReference} object. */ + public abstract ColumnReference build(); + } +} diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ForeignKey.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ForeignKey.java new file mode 100644 index 0000000000..2069e70233 --- /dev/null +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ForeignKey.java @@ -0,0 +1,111 @@ +/* + * Copyright 2023 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.auto.value.AutoValue; +import com.google.common.annotations.VisibleForTesting; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import javax.annotation.Nullable; + +@AutoValue +public abstract class ForeignKey implements Serializable { + public static ForeignKey.Builder newBuilder() { + return new AutoValue_ForeignKey.Builder(); + } + + static ForeignKey fromPb( + com.google.api.services.bigquery.model.TableConstraints.ForeignKeys foreignKey) { + ForeignKey.Builder builder = newBuilder(); + + if (foreignKey.getName() != null) { + builder.setName(foreignKey.getName()); + } + + if (foreignKey.getReferencedTable() != null) { + com.google.api.services.bigquery.model.TableConstraints.ForeignKeys.ReferencedTable + referencedTable = foreignKey.getReferencedTable(); + builder.setReferencedTable( + TableId.of( + referencedTable.getProjectId(), + referencedTable.getDatasetId(), + referencedTable.getTableId())); + } + + if (foreignKey.getColumnReferences() != null) { + ArrayList list = new ArrayList<>(); + for (com.google.api.services.bigquery.model.TableConstraints.ForeignKeys.ColumnReferences + columnReferences : foreignKey.getColumnReferences()) { + list.add(ColumnReference.fromPb(columnReferences)); + } + builder.setColumnReferences(list); + } + + return builder.build(); + } + + com.google.api.services.bigquery.model.TableConstraints.ForeignKeys toPb() { + + com.google.api.services.bigquery.model.TableConstraints.ForeignKeys foreignKey = + new com.google.api.services.bigquery.model.TableConstraints.ForeignKeys(); + foreignKey.setName(getName()); + TableId referencedTableId = getReferencedTable(); + foreignKey.setReferencedTable( + new com.google.api.services.bigquery.model.TableConstraints.ForeignKeys.ReferencedTable() + .setTableId(referencedTableId.getTable()) + .setDatasetId(referencedTableId.getDataset()) + .setProjectId(referencedTableId.getProject())); + ArrayList + columnReferences = new ArrayList<>(); + for (ColumnReference columnReference : getColumnReferences()) { + columnReferences.add(columnReference.toPb()); + } + foreignKey.setColumnReferences(columnReferences); + + return foreignKey; + } + + @Nullable + public abstract String getName(); + + @Nullable + public abstract TableId getReferencedTable(); + + @Nullable + public abstract List getColumnReferences(); + + /** Returns a builder for foreign key. */ + @VisibleForTesting + public abstract ForeignKey.Builder toBuilder(); + + @AutoValue.Builder + public abstract static class Builder { + + /** The name of the foreign key. * */ + public abstract ForeignKey.Builder setName(String name); + + /** The table referenced by this foreign key. * */ + public abstract ForeignKey.Builder setReferencedTable(TableId referencedTable); + + /** The set of column references for this foreign key. * */ + public abstract ForeignKey.Builder setColumnReferences(List columnReferences); + + /** Creates a {@code ForignKey} object. */ + public abstract ForeignKey build(); + } +} diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/PrimaryKey.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/PrimaryKey.java index 859f35f9d3..cdd2728533 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/PrimaryKey.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/PrimaryKey.java @@ -1,3 +1,19 @@ +/* + * Copyright 2023 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.auto.value.AutoValue; diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java index f346a0cae9..781e7e667c 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java @@ -23,7 +23,9 @@ import com.google.common.base.MoreObjects; import java.io.Serializable; import java.math.BigInteger; +import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; import javax.annotation.Nullable; /** @@ -164,6 +166,8 @@ public abstract static class Builder public abstract Builder setPrimaryKey(PrimaryKey primaryKey); + public abstract Builder setForeignKeys(List foreignKeys); + /** Creates a {@code StandardTableDefinition} object. */ public abstract StandardTableDefinition build(); } @@ -231,6 +235,13 @@ public abstract static class Builder @Nullable public abstract PrimaryKey getPrimaryKey(); + /** + * Returns foreign keys for this table. Returns {@code null} if no foreign keys exist in this + * table. + */ + @Nullable + public abstract List getForeignKeys(); + /** Returns a builder for a BigQuery standard table definition. */ public static Builder newBuilder() { return new AutoValue_StandardTableDefinition.Builder().setType(Type.TABLE); @@ -276,6 +287,16 @@ Table toPb() { tablePb.getTableConstraints().setPrimaryKey(getPrimaryKey().toPb()); } + if (getForeignKeys() != null) { + if (tablePb.getTableConstraints() == null) { + tablePb.setTableConstraints(new TableConstraints()); + } + + tablePb + .getTableConstraints() + .setForeignKeys( + getForeignKeys().stream().map(ForeignKey::toPb).collect(Collectors.toList())); + } return tablePb; } @@ -314,7 +335,15 @@ static StandardTableDefinition fromPb(Table tablePb) { builder.setNumLongTermBytes(tablePb.getNumLongTermBytes()); } if (tablePb.getTableConstraints() != null) { - builder.setPrimaryKey(PrimaryKey.fromPb(tablePb.getTableConstraints().getPrimaryKey())); + if (tablePb.getTableConstraints().getPrimaryKey() != null) { + builder.setPrimaryKey(PrimaryKey.fromPb(tablePb.getTableConstraints().getPrimaryKey())); + } + if (tablePb.getTableConstraints().getForeignKeys() != null) { + builder.setForeignKeys( + tablePb.getTableConstraints().getForeignKeys().stream() + .map(ForeignKey::fromPb) + .collect(Collectors.toList())); + } } return builder.setNumBytes(tablePb.getNumBytes()).setLocation(tablePb.getLocation()).build(); } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java index 0a80dc5d1f..485ca80ceb 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java @@ -174,6 +174,12 @@ public Builder setPrimaryKey(PrimaryKey primaryKey) { return this; } + @Override + public Builder setForeignKeys(List foreignKeys) { + infoBuilder.setForeignKeys(foreignKeys); + return this; + } + @Override public Table build() { return new Table(bigquery, infoBuilder); diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java index 17f1b7f21d..839f850db8 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java @@ -28,8 +28,10 @@ import com.google.common.base.MoreObjects; import java.io.Serializable; import java.math.BigInteger; +import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.stream.Collectors; /** * Google BigQuery table information. Use {@link StandardTableDefinition} to create simple BigQuery @@ -77,6 +79,7 @@ public Table apply(TableInfo tableInfo) { private final CloneDefinition cloneDefinition; private final PrimaryKey primaryKey; + private final List foreignKeys; /** A builder for {@code TableInfo} objects. */ public abstract static class Builder { @@ -146,6 +149,8 @@ public Builder setRequirePartitionFilter(Boolean requirePartitionFilter) { public abstract Builder setCloneDefinition(CloneDefinition cloneDefinition); public abstract Builder setPrimaryKey(PrimaryKey primaryKey); + + public abstract Builder setForeignKeys(List foreignKeys); } static class BuilderImpl extends Builder { @@ -169,6 +174,7 @@ static class BuilderImpl extends Builder { private String defaultCollation; private CloneDefinition cloneDefinition; private PrimaryKey primaryKey; + private List foreignKeys; BuilderImpl() {} @@ -192,6 +198,7 @@ static class BuilderImpl extends Builder { this.defaultCollation = tableInfo.defaultCollation; this.cloneDefinition = tableInfo.cloneDefinition; this.primaryKey = tableInfo.primaryKey; + this.foreignKeys = tableInfo.foreignKeys; } BuilderImpl(Table tablePb) { @@ -221,7 +228,15 @@ static class BuilderImpl extends Builder { this.cloneDefinition = CloneDefinition.fromPb(tablePb.getCloneDefinition()); } if (tablePb.getTableConstraints() != null) { - this.primaryKey = PrimaryKey.fromPb(tablePb.getTableConstraints().getPrimaryKey()); + if (tablePb.getTableConstraints().getPrimaryKey() != null) { + this.primaryKey = PrimaryKey.fromPb(tablePb.getTableConstraints().getPrimaryKey()); + } + if (tablePb.getTableConstraints().getForeignKeys() != null) { + this.foreignKeys = + tablePb.getTableConstraints().getForeignKeys().stream() + .map(ForeignKey::fromPb) + .collect(Collectors.toList()); + } } } @@ -337,6 +352,11 @@ public Builder setPrimaryKey(PrimaryKey primaryKey) { return this; } + public Builder setForeignKeys(List foreignKeys) { + this.foreignKeys = foreignKeys; + return this; + } + @Override public TableInfo build() { return new TableInfo(this); @@ -363,6 +383,7 @@ public TableInfo build() { this.defaultCollation = builder.defaultCollation; this.cloneDefinition = builder.cloneDefinition; this.primaryKey = builder.primaryKey; + this.foreignKeys = builder.foreignKeys; } /** Returns the hash of the table resource. */ @@ -477,6 +498,10 @@ public PrimaryKey getPrimaryKey() { return primaryKey; } + public List getForeignKeys() { + return foreignKeys; + } + /** Returns a builder for the table object. */ public Builder toBuilder() { return new BuilderImpl(this); @@ -504,6 +529,7 @@ public String toString() { .add("defaultCollation", defaultCollation) .add("cloneDefinition", cloneDefinition) .add("primaryKey", primaryKey) + .add("foreignKeys", foreignKeys) .toString(); } @@ -577,6 +603,14 @@ Table toPb() { } tablePb.getTableConstraints().setPrimaryKey(primaryKey.toPb()); } + if (foreignKeys != null) { + if (tablePb.getTableConstraints() == null) { + tablePb.setTableConstraints(new TableConstraints()); + } + tablePb + .getTableConstraints() + .setForeignKeys(foreignKeys.stream().map(ForeignKey::toPb).collect(Collectors.toList())); + } return tablePb; } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ForeignKeyTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ForeignKeyTest.java new file mode 100644 index 0000000000..5cb2b418f2 --- /dev/null +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ForeignKeyTest.java @@ -0,0 +1,95 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.bigquery; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Collections; +import org.junit.Test; + +public class ForeignKeyTest { + private static final TableId TABLE_ID = TableId.of("project", "dataset", "table"); + + private static final ColumnReference COLUMN_REFERENCE = + ColumnReference.newBuilder() + .setReferencingColumn("column1") + .setReferencedColumn("column2") + .build(); + private static final ForeignKey FOREIGN_KEY = + ForeignKey.newBuilder() + .setName("foreign_key") + .setReferencedTable(TABLE_ID) + .setColumnReferences(Collections.singletonList(COLUMN_REFERENCE)) + .build(); + + @Test + public void testToBuilder() { + compareForeignKeyDefinition(FOREIGN_KEY, FOREIGN_KEY.toBuilder().build()); + TableId referencedTable = TableId.of("project1", "dataset1", "table1"); + ArrayList columnReferences = new ArrayList<>(); + columnReferences.add( + ColumnReference.newBuilder() + .setReferencingColumn("from") + .setReferencedColumn("to") + .build()); + columnReferences.add( + ColumnReference.newBuilder() + .setReferencingColumn("from2") + .setReferencedColumn("to2") + .build()); + ForeignKey foreignKey = + FOREIGN_KEY + .toBuilder() + .setName("test") + .setReferencedTable(referencedTable) + .setColumnReferences(columnReferences) + .build(); + assertEquals("test", foreignKey.getName()); + assertEquals(referencedTable, foreignKey.getReferencedTable()); + assertEquals(columnReferences, foreignKey.getColumnReferences()); + } + + @Test + public void testBuilder() { + assertEquals("foreign_key", FOREIGN_KEY.getName()); + assertEquals(TABLE_ID, FOREIGN_KEY.getReferencedTable()); + assertEquals(Collections.singletonList(COLUMN_REFERENCE), FOREIGN_KEY.getColumnReferences()); + ForeignKey foreignKey = + FOREIGN_KEY + .newBuilder() + .setName("foreign_key") + .setReferencedTable(TABLE_ID) + .setColumnReferences(Collections.singletonList(COLUMN_REFERENCE)) + .build(); + assertEquals(FOREIGN_KEY, foreignKey); + } + + @Test + public void testToAndFromPb() { + ForeignKey foreignKey = FOREIGN_KEY.toBuilder().build(); + assertTrue(ForeignKey.fromPb(foreignKey.toPb()) instanceof ForeignKey); + compareForeignKeyDefinition(foreignKey, ForeignKey.fromPb(foreignKey.toPb())); + } + + private void compareForeignKeyDefinition(ForeignKey expected, ForeignKey value) { + assertEquals(expected.getName(), value.getName()); + assertEquals(expected.getReferencedTable(), value.getReferencedTable()); + assertEquals(expected.getColumnReferences(), value.getColumnReferences()); + } +} diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/PrimaryKeyTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/PrimaryKeyTest.java index 5863443975..2de87a0258 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/PrimaryKeyTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/PrimaryKeyTest.java @@ -25,8 +25,7 @@ public class PrimaryKeyTest { private static final List COLUMNS = Arrays.asList("column1", "column2"); - private static final PrimaryKey PRIMARY_KEY = - PrimaryKey.newBuilder().setColumns(COLUMNS).build(); + private static final PrimaryKey PRIMARY_KEY = PrimaryKey.newBuilder().setColumns(COLUMNS).build(); @Test public void testToBuilder() { @@ -39,8 +38,7 @@ public void testToBuilder() { @Test public void testBuilder() { assertEquals(COLUMNS, PRIMARY_KEY.getColumns()); - PrimaryKey primaryKey = - PRIMARY_KEY.newBuilder().setColumns(COLUMNS).build(); + PrimaryKey primaryKey = PRIMARY_KEY.newBuilder().setColumns(COLUMNS).build(); assertEquals(PRIMARY_KEY, primaryKey); } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index 16677efd6c..322361c93e 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -57,6 +57,7 @@ import com.google.cloud.bigquery.BigQuerySQLException; import com.google.cloud.bigquery.CloneDefinition; import com.google.cloud.bigquery.Clustering; +import com.google.cloud.bigquery.ColumnReference; import com.google.cloud.bigquery.Connection; import com.google.cloud.bigquery.ConnectionProperty; import com.google.cloud.bigquery.ConnectionSettings; @@ -74,6 +75,7 @@ import com.google.cloud.bigquery.FieldValue; import com.google.cloud.bigquery.FieldValue.Attribute; import com.google.cloud.bigquery.FieldValueList; +import com.google.cloud.bigquery.ForeignKey; import com.google.cloud.bigquery.FormatOptions; import com.google.cloud.bigquery.HivePartitioningOptions; import com.google.cloud.bigquery.InsertAllRequest; @@ -746,6 +748,9 @@ public class ITBigQueryTest { .build(); private static final Schema SESSION_TABLE_SCHEMA = Schema.of(ID_SCHEMA, FIRST_NAME_SCHEMA, LAST_NAME_SCHEMA, EMAIL_SCHEMA, PROFESSION_SCHEMA); + + private static final Schema CONSTRAINTS_TABLE_SCHEMA = + Schema.of(ID_SCHEMA, FIRST_NAME_SCHEMA, LAST_NAME_SCHEMA); private static final Path csvPath = FileSystems.getDefault().getPath("src/test/resources", "sessionTest.csv").toAbsolutePath(); @@ -5676,7 +5681,7 @@ public void testHivePartitioningOptionsFieldsFieldExistence() throws Interrupted } @Test - public void primaryKeyTest() { + public void testPrimaryKey() { String tableName = "test_primary_key"; TableId tableId = TableId.of(DATASET, tableName); PrimaryKey primaryKey = PrimaryKey.newBuilder().setColumns(Arrays.asList("ID")).build(); @@ -5684,45 +5689,29 @@ public void primaryKeyTest() { try { StandardTableDefinition tableDefinition = StandardTableDefinition.newBuilder() - .setSchema(Schema.of( - Field.newBuilder("ID", LegacySQLTypeName.STRING).setMode(Field.Mode.NULLABLE).build(), - Field.newBuilder("FirstName", LegacySQLTypeName.STRING) - .setMode(Field.Mode.NULLABLE) - .build(), - Field.newBuilder("LastName", LegacySQLTypeName.STRING) - .setMode(Field.Mode.NULLABLE) - .build())) + .setSchema(CONSTRAINTS_TABLE_SCHEMA) .setPrimaryKey(primaryKey) .build(); Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition)); assertNotNull(createdTable); Table remoteTable = bigquery.getTable(DATASET, tableName); assertEquals( - primaryKey, - remoteTable.getDefinition().getPrimaryKey()); + primaryKey, remoteTable.getDefinition().getPrimaryKey()); } finally { bigquery.delete(tableId); } } @Test - public void primaryKeyUpdateTest() { + public void testPrimaryKeyUpdate() { String tableName = "test_primary_key_update"; TableId tableId = TableId.of(DATASET, tableName); - PrimaryKey primaryKey = PrimaryKey.newBuilder().setColumns(Arrays.asList("FirstName", "LastName")).build(); + PrimaryKey primaryKey = + PrimaryKey.newBuilder().setColumns(Arrays.asList("FirstName", "LastName")).build(); try { StandardTableDefinition tableDefinition = - StandardTableDefinition.newBuilder() - .setSchema(Schema.of( - Field.newBuilder("ID", LegacySQLTypeName.STRING).setMode(Field.Mode.NULLABLE).build(), - Field.newBuilder("FirstName", LegacySQLTypeName.STRING) - .setMode(Field.Mode.NULLABLE) - .build(), - Field.newBuilder("LastName", LegacySQLTypeName.STRING) - .setMode(Field.Mode.NULLABLE) - .build())) - .build(); + StandardTableDefinition.newBuilder().setSchema(CONSTRAINTS_TABLE_SCHEMA).build(); Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition)); assertNotNull(createdTable); Table remoteTable = bigquery.getTable(DATASET, tableName); @@ -5732,10 +5721,142 @@ public void primaryKeyUpdateTest() { assertNotNull(updatedTable); Table remoteUpdatedTable = bigquery.getTable(DATASET, tableName); assertEquals( - primaryKey, - remoteUpdatedTable.getDefinition().getPrimaryKey()); + primaryKey, remoteUpdatedTable.getDefinition().getPrimaryKey()); } finally { bigquery.delete(tableId); } } + + @Test + public void testForeignKeys() { + String tableName1 = "test_foreign_key"; + String tableName2 = "test_foreign_key2"; + TableId tableId1 = TableId.of(DATASET, tableName1); + TableId tableId2 = TableId.of(DATASET, tableName2); + ColumnReference columnReference = + ColumnReference.newBuilder().setReferencingColumn("ID").setReferencedColumn("ID").build(); + + PrimaryKey primaryKey = + PrimaryKey.newBuilder().setColumns(Collections.singletonList("ID")).build(); + ForeignKey foreignKey = + ForeignKey.newBuilder() + .setName("foreign_key") + .setReferencedTable(tableId1) + .setColumnReferences(Collections.singletonList(columnReference)) + .build(); + + try { + StandardTableDefinition tableDefinition1 = + StandardTableDefinition.newBuilder() + .setSchema(CONSTRAINTS_TABLE_SCHEMA) + .setPrimaryKey(primaryKey) + .build(); + Table createdTable1 = bigquery.create(TableInfo.of(tableId1, tableDefinition1)); + assertNotNull(createdTable1); + + StandardTableDefinition tableDefinition2 = + StandardTableDefinition.newBuilder() + .setSchema(CONSTRAINTS_TABLE_SCHEMA) + .setForeignKeys(Collections.singletonList(foreignKey)) + .build(); + Table createdTable2 = bigquery.create(TableInfo.of(tableId2, tableDefinition2)); + assertNotNull(createdTable2); + Table remoteTable = bigquery.getTable(DATASET, tableName2); + assertEquals( + Collections.singletonList(foreignKey), + remoteTable.getDefinition().getForeignKeys()); + } finally { + bigquery.delete(tableId1); + bigquery.delete(tableId2); + } + } + + @Test + public void testForeignKeysUpdate() { + String tableName1 = "test_foreign_key"; + String tableName2 = "test_foreign_key2"; + String tableName3 = "test_foreign_key3"; + TableId tableId1 = TableId.of(DATASET, tableName1); + TableId tableId2 = TableId.of(DATASET, tableName2); + TableId tableId3 = TableId.of(DATASET, tableName3); + + ArrayList foreignKeys = new ArrayList<>(); + + // set up ID in table 1 as a foreign key to table 2 + ColumnReference columnReference12 = + ColumnReference.newBuilder().setReferencingColumn("ID").setReferencedColumn("ID").build(); + PrimaryKey primaryKey2 = + PrimaryKey.newBuilder().setColumns(Collections.singletonList("ID")).build(); + ForeignKey foreignKey1 = + ForeignKey.newBuilder() + .setName("foreign_key1") + .setReferencedTable(tableId2) + .setColumnReferences(Collections.singletonList(columnReference12)) + .build(); + foreignKeys.add(foreignKey1); + + // set up First and last names in table 1 as foreign keys to table 3 + ArrayList columnReferences13 = new ArrayList<>(); + columnReferences13.add( + ColumnReference.newBuilder() + .setReferencingColumn("FirstName") + .setReferencedColumn("FirstName") + .build()); + columnReferences13.add( + ColumnReference.newBuilder() + .setReferencingColumn("LastName") + .setReferencedColumn("LastName") + .build()); + + ArrayList primaryKey3Columns = new ArrayList<>(); + primaryKey3Columns.add("FirstName"); + primaryKey3Columns.add("LastName"); + + PrimaryKey primaryKey3 = PrimaryKey.newBuilder().setColumns(primaryKey3Columns).build(); + ForeignKey foreignKey2 = + ForeignKey.newBuilder() + .setName("foreign_key2") + .setReferencedTable(tableId3) + .setColumnReferences(columnReferences13) + .build(); + foreignKeys.add(foreignKey2); + + try { + StandardTableDefinition tableDefinition1 = + StandardTableDefinition.newBuilder().setSchema(CONSTRAINTS_TABLE_SCHEMA).build(); + Table createdTable1 = bigquery.create(TableInfo.of(tableId1, tableDefinition1)); + assertNotNull(createdTable1); + + StandardTableDefinition tableDefinition2 = + StandardTableDefinition.newBuilder() + .setSchema(CONSTRAINTS_TABLE_SCHEMA) + .setPrimaryKey(primaryKey2) + .build(); + Table createdTable2 = bigquery.create(TableInfo.of(tableId2, tableDefinition2)); + assertNotNull(createdTable2); + + StandardTableDefinition tableDefinition3 = + StandardTableDefinition.newBuilder() + .setSchema(CONSTRAINTS_TABLE_SCHEMA) + .setPrimaryKey(primaryKey3) + .build(); + Table createdTable3 = bigquery.create(TableInfo.of(tableId3, tableDefinition3)); + assertNotNull(createdTable3); + + Table remoteTable = bigquery.getTable(DATASET, tableName1); + assertNull(remoteTable.getDefinition().getForeignKeys()); + + Table updatedTable = remoteTable.toBuilder().setForeignKeys(foreignKeys).build().update(); + + assertNotNull(updatedTable); + Table remoteUpdatedTable = bigquery.getTable(DATASET, tableName1); + assertEquals( + foreignKeys, + remoteUpdatedTable.getDefinition().getForeignKeys()); + } finally { + bigquery.delete(tableId1); + bigquery.delete(tableId2); + bigquery.delete(tableId3); + } + } } From 0e4ef0d271e62c26da6386136513913f4af56f17 Mon Sep 17 00:00:00 2001 From: obada-ab Date: Wed, 14 Jun 2023 12:45:26 +0200 Subject: [PATCH 03/10] fix: add changes to clirr-ignored-differences --- .../clirr-ignored-differences.xml | 31 ++++++++++++++++++- .../cloud/bigquery/it/ITBigQueryTest.java | 9 +++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/google-cloud-bigquery/clirr-ignored-differences.xml b/google-cloud-bigquery/clirr-ignored-differences.xml index 5fe634cca7..198b39f6b3 100644 --- a/google-cloud-bigquery/clirr-ignored-differences.xml +++ b/google-cloud-bigquery/clirr-ignored-differences.xml @@ -44,10 +44,39 @@ com/google/cloud/bigquery/TableInfo* *DefaultCollation(*) - 7013 com/google/cloud/bigquery/TableInfo* *CloneDefinition(*) + + 7013 + com/google/cloud/bigquery/StandardTableDefinition* + *getForeignKeys(*) + + + 7013 + com/google/cloud/bigquery/StandardTableDefinition* + *getPrimaryKey(*) + + + 7013 + com/google/cloud/bigquery/StandardTableDefinition* + *setForeignKeys(*) + + + 7013 + com/google/cloud/bigquery/StandardTableDefinition* + *setPrimaryKey(*) + + + 7013 + com/google/cloud/bigquery/TableInfo* + *setForeignKeys(*) + + + 7013 + com/google/cloud/bigquery/TableInfo* + *setPrimaryKey(*) + \ No newline at end of file diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index 322361c93e..6cc7d239a1 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -750,7 +750,14 @@ public class ITBigQueryTest { Schema.of(ID_SCHEMA, FIRST_NAME_SCHEMA, LAST_NAME_SCHEMA, EMAIL_SCHEMA, PROFESSION_SCHEMA); private static final Schema CONSTRAINTS_TABLE_SCHEMA = - Schema.of(ID_SCHEMA, FIRST_NAME_SCHEMA, LAST_NAME_SCHEMA); + Schema.of( + Field.newBuilder("ID", LegacySQLTypeName.STRING).setMode(Mode.REQUIRED).build(), + Field.newBuilder("FirstName", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("LastName", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build()); private static final Path csvPath = FileSystems.getDefault().getPath("src/test/resources", "sessionTest.csv").toAbsolutePath(); From 1b01598d7f32fe1620d0d757ffaaf064ca013add Mon Sep 17 00:00:00 2001 From: obada-ab Date: Thu, 15 Jun 2023 12:06:19 +0200 Subject: [PATCH 04/10] fix: fix integration test for foreign key --- .../com/google/cloud/bigquery/ForeignKey.java | 18 +--- .../cloud/bigquery/it/ITBigQueryTest.java | 100 +++++++++--------- 2 files changed, 56 insertions(+), 62 deletions(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ForeignKey.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ForeignKey.java index 2069e70233..fdbb480cca 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ForeignKey.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ForeignKey.java @@ -19,8 +19,8 @@ import com.google.auto.value.AutoValue; import com.google.common.annotations.VisibleForTesting; import java.io.Serializable; -import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import javax.annotation.Nullable; @AutoValue @@ -48,12 +48,8 @@ static ForeignKey fromPb( } if (foreignKey.getColumnReferences() != null) { - ArrayList list = new ArrayList<>(); - for (com.google.api.services.bigquery.model.TableConstraints.ForeignKeys.ColumnReferences - columnReferences : foreignKey.getColumnReferences()) { - list.add(ColumnReference.fromPb(columnReferences)); - } - builder.setColumnReferences(list); + builder.setColumnReferences(foreignKey.getColumnReferences().stream().map(ColumnReference::fromPb).collect( + Collectors.toList())); } return builder.build(); @@ -70,12 +66,8 @@ com.google.api.services.bigquery.model.TableConstraints.ForeignKeys toPb() { .setTableId(referencedTableId.getTable()) .setDatasetId(referencedTableId.getDataset()) .setProjectId(referencedTableId.getProject())); - ArrayList - columnReferences = new ArrayList<>(); - for (ColumnReference columnReference : getColumnReferences()) { - columnReferences.add(columnReference.toPb()); - } - foreignKey.setColumnReferences(columnReferences); + foreignKey.setColumnReferences(getColumnReferences().stream().map(ColumnReference::toPb).collect( + Collectors.toList())); return foreignKey; } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index 6cc7d239a1..f928e1cfae 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -5736,10 +5736,11 @@ public void testPrimaryKeyUpdate() { @Test public void testForeignKeys() { - String tableName1 = "test_foreign_key"; - String tableName2 = "test_foreign_key2"; - TableId tableId1 = TableId.of(DATASET, tableName1); - TableId tableId2 = TableId.of(DATASET, tableName2); + String tableNamePk = "test_foreign_key"; + String tableNameFk = "test_foreign_key2"; + // TableIds referenced by foreign keys need project id to be specified + TableId tableIdPk = TableId.of(PROJECT_ID, DATASET, tableNamePk); + TableId tableIdFk = TableId.of(DATASET, tableNameFk); ColumnReference columnReference = ColumnReference.newBuilder().setReferencingColumn("ID").setReferencedColumn("ID").build(); @@ -5748,122 +5749,123 @@ public void testForeignKeys() { ForeignKey foreignKey = ForeignKey.newBuilder() .setName("foreign_key") - .setReferencedTable(tableId1) + .setReferencedTable(tableIdPk) .setColumnReferences(Collections.singletonList(columnReference)) .build(); try { - StandardTableDefinition tableDefinition1 = + StandardTableDefinition tableDefinitionPk = StandardTableDefinition.newBuilder() .setSchema(CONSTRAINTS_TABLE_SCHEMA) .setPrimaryKey(primaryKey) .build(); - Table createdTable1 = bigquery.create(TableInfo.of(tableId1, tableDefinition1)); - assertNotNull(createdTable1); + Table createdTablePk = bigquery.create(TableInfo.of(tableIdPk, tableDefinitionPk)); + assertNotNull(createdTablePk); - StandardTableDefinition tableDefinition2 = + StandardTableDefinition tableDefinitionFk = StandardTableDefinition.newBuilder() .setSchema(CONSTRAINTS_TABLE_SCHEMA) .setForeignKeys(Collections.singletonList(foreignKey)) .build(); - Table createdTable2 = bigquery.create(TableInfo.of(tableId2, tableDefinition2)); - assertNotNull(createdTable2); - Table remoteTable = bigquery.getTable(DATASET, tableName2); + Table createdTableFk = bigquery.create(TableInfo.of(tableIdFk, tableDefinitionFk)); + assertNotNull(createdTableFk); + Table remoteTable = bigquery.getTable(DATASET, tableNameFk); assertEquals( Collections.singletonList(foreignKey), remoteTable.getDefinition().getForeignKeys()); } finally { - bigquery.delete(tableId1); - bigquery.delete(tableId2); + bigquery.delete(tableIdPk); + bigquery.delete(tableIdFk); } } @Test public void testForeignKeysUpdate() { - String tableName1 = "test_foreign_key"; - String tableName2 = "test_foreign_key2"; - String tableName3 = "test_foreign_key3"; - TableId tableId1 = TableId.of(DATASET, tableName1); - TableId tableId2 = TableId.of(DATASET, tableName2); - TableId tableId3 = TableId.of(DATASET, tableName3); + String tableNameFk = "test_foreign_key"; + String tableNamePk1 = "test_foreign_key2"; + String tableNamePk2 = "test_foreign_key3"; + TableId tableIdFk = TableId.of(DATASET, tableNameFk); + // TableIds referenced by foreign keys need project id to be specified + TableId tableIdPk1 = TableId.of(PROJECT_ID, DATASET, tableNamePk1); + TableId tableIdPk2 = TableId.of(PROJECT_ID, DATASET, tableNamePk2); ArrayList foreignKeys = new ArrayList<>(); // set up ID in table 1 as a foreign key to table 2 - ColumnReference columnReference12 = + ColumnReference columnReferencePk1 = ColumnReference.newBuilder().setReferencingColumn("ID").setReferencedColumn("ID").build(); - PrimaryKey primaryKey2 = + PrimaryKey primaryKey1 = PrimaryKey.newBuilder().setColumns(Collections.singletonList("ID")).build(); ForeignKey foreignKey1 = ForeignKey.newBuilder() .setName("foreign_key1") - .setReferencedTable(tableId2) - .setColumnReferences(Collections.singletonList(columnReference12)) + .setReferencedTable(tableIdPk1) + .setColumnReferences(Collections.singletonList(columnReferencePk1)) .build(); foreignKeys.add(foreignKey1); // set up First and last names in table 1 as foreign keys to table 3 - ArrayList columnReferences13 = new ArrayList<>(); - columnReferences13.add( + ArrayList columnReferencesPk2 = new ArrayList<>(); + columnReferencesPk2.add( ColumnReference.newBuilder() .setReferencingColumn("FirstName") .setReferencedColumn("FirstName") .build()); - columnReferences13.add( + columnReferencesPk2.add( ColumnReference.newBuilder() .setReferencingColumn("LastName") .setReferencedColumn("LastName") .build()); - ArrayList primaryKey3Columns = new ArrayList<>(); - primaryKey3Columns.add("FirstName"); - primaryKey3Columns.add("LastName"); + ArrayList primaryKey2Columns = new ArrayList<>(); + primaryKey2Columns.add("FirstName"); + primaryKey2Columns.add("LastName"); - PrimaryKey primaryKey3 = PrimaryKey.newBuilder().setColumns(primaryKey3Columns).build(); + PrimaryKey primaryKey2 = PrimaryKey.newBuilder().setColumns(primaryKey2Columns).build(); ForeignKey foreignKey2 = ForeignKey.newBuilder() .setName("foreign_key2") - .setReferencedTable(tableId3) - .setColumnReferences(columnReferences13) + .setReferencedTable(tableIdPk2) + .setColumnReferences(columnReferencesPk2) .build(); foreignKeys.add(foreignKey2); try { - StandardTableDefinition tableDefinition1 = + StandardTableDefinition tableDefinitionFk = StandardTableDefinition.newBuilder().setSchema(CONSTRAINTS_TABLE_SCHEMA).build(); - Table createdTable1 = bigquery.create(TableInfo.of(tableId1, tableDefinition1)); - assertNotNull(createdTable1); + Table createdTableFk = bigquery.create(TableInfo.of(tableIdFk, tableDefinitionFk)); + assertNotNull(createdTableFk); - StandardTableDefinition tableDefinition2 = + StandardTableDefinition tableDefinitionPk1 = StandardTableDefinition.newBuilder() .setSchema(CONSTRAINTS_TABLE_SCHEMA) - .setPrimaryKey(primaryKey2) + .setPrimaryKey(primaryKey1) .build(); - Table createdTable2 = bigquery.create(TableInfo.of(tableId2, tableDefinition2)); - assertNotNull(createdTable2); + Table createdTablePk1 = bigquery.create(TableInfo.of(tableIdPk1, tableDefinitionPk1)); + assertNotNull(createdTablePk1); - StandardTableDefinition tableDefinition3 = + StandardTableDefinition tableDefinitionPk2 = StandardTableDefinition.newBuilder() .setSchema(CONSTRAINTS_TABLE_SCHEMA) - .setPrimaryKey(primaryKey3) + .setPrimaryKey(primaryKey2) .build(); - Table createdTable3 = bigquery.create(TableInfo.of(tableId3, tableDefinition3)); - assertNotNull(createdTable3); + Table createdTablePk2 = bigquery.create(TableInfo.of(tableIdPk2, tableDefinitionPk2)); + assertNotNull(createdTablePk2); - Table remoteTable = bigquery.getTable(DATASET, tableName1); + Table remoteTable = bigquery.getTable(DATASET, tableNameFk); assertNull(remoteTable.getDefinition().getForeignKeys()); Table updatedTable = remoteTable.toBuilder().setForeignKeys(foreignKeys).build().update(); assertNotNull(updatedTable); - Table remoteUpdatedTable = bigquery.getTable(DATASET, tableName1); + Table remoteUpdatedTable = bigquery.getTable(DATASET, tableNameFk); assertEquals( foreignKeys, remoteUpdatedTable.getDefinition().getForeignKeys()); } finally { - bigquery.delete(tableId1); - bigquery.delete(tableId2); - bigquery.delete(tableId3); + bigquery.delete(tableIdFk); + bigquery.delete(tableIdPk1); + bigquery.delete(tableIdPk2); } } } From e177688974115dedcb8770f660821398683d7785 Mon Sep 17 00:00:00 2001 From: obada-ab Date: Thu, 15 Jun 2023 13:38:59 +0200 Subject: [PATCH 05/10] chore: add sample for primary/foreign keys --- .../com/google/cloud/bigquery/ForeignKey.java | 10 ++- .../cloud/bigquery/it/ITBigQueryTest.java | 4 +- ...CreateTablesWithPrimaryAndForeignKeys.java | 88 +++++++++++++++++++ ...eateTablesWithPrimaryAndForeignKeysIT.java | 82 +++++++++++++++++ 4 files changed, 178 insertions(+), 6 deletions(-) create mode 100644 samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java create mode 100644 samples/snippets/src/test/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeysIT.java diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ForeignKey.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ForeignKey.java index fdbb480cca..899635e046 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ForeignKey.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ForeignKey.java @@ -48,8 +48,10 @@ static ForeignKey fromPb( } if (foreignKey.getColumnReferences() != null) { - builder.setColumnReferences(foreignKey.getColumnReferences().stream().map(ColumnReference::fromPb).collect( - Collectors.toList())); + builder.setColumnReferences( + foreignKey.getColumnReferences().stream() + .map(ColumnReference::fromPb) + .collect(Collectors.toList())); } return builder.build(); @@ -66,8 +68,8 @@ com.google.api.services.bigquery.model.TableConstraints.ForeignKeys toPb() { .setTableId(referencedTableId.getTable()) .setDatasetId(referencedTableId.getDataset()) .setProjectId(referencedTableId.getProject())); - foreignKey.setColumnReferences(getColumnReferences().stream().map(ColumnReference::toPb).collect( - Collectors.toList())); + foreignKey.setColumnReferences( + getColumnReferences().stream().map(ColumnReference::toPb).collect(Collectors.toList())); return foreignKey; } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index f928e1cfae..54e6534fa7 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -5791,7 +5791,7 @@ public void testForeignKeysUpdate() { ArrayList foreignKeys = new ArrayList<>(); - // set up ID in table 1 as a foreign key to table 2 + // set up ID in tableFk as a foreign key to tablePk1 ColumnReference columnReferencePk1 = ColumnReference.newBuilder().setReferencingColumn("ID").setReferencedColumn("ID").build(); PrimaryKey primaryKey1 = @@ -5804,7 +5804,7 @@ public void testForeignKeysUpdate() { .build(); foreignKeys.add(foreignKey1); - // set up First and last names in table 1 as foreign keys to table 3 + // set up First and last names in tableFk as foreign keys to TablePk2 ArrayList columnReferencesPk2 = new ArrayList<>(); columnReferencesPk2.add( ColumnReference.newBuilder() diff --git a/samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java b/samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java new file mode 100644 index 0000000000..4927c6fc2c --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java @@ -0,0 +1,88 @@ +/* + * Copyright 2023 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.example.bigquery; + +// [START bigquery_set_primary_and_foreign_keys] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.ColumnReference; +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.ForeignKey; +import com.google.cloud.bigquery.LegacySQLTypeName; +import com.google.cloud.bigquery.PrimaryKey; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardTableDefinition; +import com.google.cloud.bigquery.TableId; +import com.google.cloud.bigquery.TableInfo; +import java.util.Collections; + +// Create tables with primary/foreign key columns +public class CreateTablesWithPrimaryAndForeignKeys { + + private static final Schema PK_FK_SCHEMA = + Schema.of( + Field.newBuilder("ID", LegacySQLTypeName.STRING).setMode(Field.Mode.NULLABLE).build(), + Field.newBuilder("FirstName", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("LastName", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build()); + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + String datasetName = "MY_DATASET_NAME"; + String tableNamePk = "PK_TABLE"; + String tableNameFk = "FK_TABLE"; + createTablesWithPrimaryAndForeignKeys(datasetName, tableNamePk, tableNameFk); + } + + public static void createTablesWithPrimaryAndForeignKeys(String datasetName, String tableNamePk, String tableNameFk) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + // TableIds referenced by foreign keys need project id to be set + TableId tableIdPk = TableId.of(bigquery.getOptions().getProjectId(), datasetName, tableNamePk); + TableId tableIdFk = TableId.of(datasetName, tableNameFk); + + PrimaryKey primaryKey = PrimaryKey.newBuilder().setColumns(Collections.singletonList("ID")).build(); + + ColumnReference columnReference = ColumnReference.newBuilder().setReferencingColumn("ID").setReferencedColumn("ID").build(); + ForeignKey foreignKey = ForeignKey.newBuilder().setName("foreign_key").setColumnReferences(Collections.singletonList(columnReference)).setReferencedTable(tableIdPk).build(); + + // Create a table with a primary key + StandardTableDefinition tableDefinitionPk = + StandardTableDefinition.newBuilder().setSchema(PK_FK_SCHEMA).setPrimaryKey(primaryKey).build(); + TableInfo tableInfoPk = TableInfo.of(tableIdPk, tableDefinitionPk); + bigquery.create(tableInfoPk); + + // Create a table with a foreign key + StandardTableDefinition tableDefinitionFk = + StandardTableDefinition.newBuilder().setSchema(PK_FK_SCHEMA).setForeignKeys(Collections.singletonList(foreignKey)).build(); + TableInfo tableInfoFk = TableInfo.of(tableIdFk, tableDefinitionFk); + bigquery.create(tableInfoFk); + + System.out.println("Tables with primary and foreign keys created successfully."); + } catch (BigQueryException e) { + System.out.println("Tables not created \n" + e.toString()); + } + } +} +// [END bigquery_set_primary_and_foreign_keys] diff --git a/samples/snippets/src/test/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeysIT.java b/samples/snippets/src/test/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeysIT.java new file mode 100644 index 0000000000..578c7bd37d --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeysIT.java @@ -0,0 +1,82 @@ +/* + * Copyright 2023 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.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class CreateTablesWithPrimaryAndForeignKeysIT { + + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String tableNamePk; + private String tableNameFk; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME"); + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("BIGQUERY_DATASET_NAME"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + tableNamePk = "MY_TABLE_NAME_" + UUID.randomUUID().toString().replace("-", "_"); + tableNameFk = "MY_TABLE_NAME_" + UUID.randomUUID().toString().replace("-", "_"); + } + + @After + public void tearDown() { + // Clean up + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableNamePk); + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableNameFk); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, "\n" + bout.toString()); + } + + @Test + public void testCreateAndQueryRepeatedRecordField() { + CreateTablesWithPrimaryAndForeignKeys.createTablesWithPrimaryAndForeignKeys( + BIGQUERY_DATASET_NAME, tableNamePk, tableNameFk); + assertThat(bout.toString()) + .contains("Query with Array of struct parameters performed successfully."); + } +} From b6f09f9f8a6a1ef25e82bcf978e0f466747884c4 Mon Sep 17 00:00:00 2001 From: obada-ab Date: Thu, 15 Jun 2023 13:47:30 +0200 Subject: [PATCH 06/10] chore: reformat primary/foreign keys sample --- ...CreateTablesWithPrimaryAndForeignKeys.java | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java b/samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java index 4927c6fc2c..6048ebc82e 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java +++ b/samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java @@ -52,30 +52,46 @@ public static void main(String[] args) { createTablesWithPrimaryAndForeignKeys(datasetName, tableNamePk, tableNameFk); } - public static void createTablesWithPrimaryAndForeignKeys(String datasetName, String tableNamePk, String tableNameFk) { + public static void createTablesWithPrimaryAndForeignKeys( + String datasetName, String tableNamePk, String tableNameFk) { try { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); // TableIds referenced by foreign keys need project id to be set - TableId tableIdPk = TableId.of(bigquery.getOptions().getProjectId(), datasetName, tableNamePk); + TableId tableIdPk = TableId.of(bigquery.getOptions().getProjectId(), + datasetName, + tableNamePk); TableId tableIdFk = TableId.of(datasetName, tableNameFk); - PrimaryKey primaryKey = PrimaryKey.newBuilder().setColumns(Collections.singletonList("ID")).build(); + PrimaryKey primaryKey = + PrimaryKey.newBuilder().setColumns(Collections.singletonList("ID")).build(); - ColumnReference columnReference = ColumnReference.newBuilder().setReferencingColumn("ID").setReferencedColumn("ID").build(); - ForeignKey foreignKey = ForeignKey.newBuilder().setName("foreign_key").setColumnReferences(Collections.singletonList(columnReference)).setReferencedTable(tableIdPk).build(); + ColumnReference columnReference = + ColumnReference.newBuilder().setReferencingColumn("ID").setReferencedColumn("ID").build(); + ForeignKey foreignKey = + ForeignKey.newBuilder() + .setName("foreign_key") + .setColumnReferences(Collections.singletonList(columnReference)) + .setReferencedTable(tableIdPk) + .build(); // Create a table with a primary key StandardTableDefinition tableDefinitionPk = - StandardTableDefinition.newBuilder().setSchema(PK_FK_SCHEMA).setPrimaryKey(primaryKey).build(); + StandardTableDefinition.newBuilder() + .setSchema(PK_FK_SCHEMA) + .setPrimaryKey(primaryKey) + .build(); TableInfo tableInfoPk = TableInfo.of(tableIdPk, tableDefinitionPk); bigquery.create(tableInfoPk); // Create a table with a foreign key StandardTableDefinition tableDefinitionFk = - StandardTableDefinition.newBuilder().setSchema(PK_FK_SCHEMA).setForeignKeys(Collections.singletonList(foreignKey)).build(); + StandardTableDefinition.newBuilder() + .setSchema(PK_FK_SCHEMA) + .setForeignKeys(Collections.singletonList(foreignKey)) + .build(); TableInfo tableInfoFk = TableInfo.of(tableIdFk, tableDefinitionFk); bigquery.create(tableInfoFk); From 56d66adb3ecec73e632a333f2dff1b8e6df14e22 Mon Sep 17 00:00:00 2001 From: obada-ab Date: Thu, 15 Jun 2023 14:41:35 +0200 Subject: [PATCH 07/10] chore: correct primary/foreign key IT message assertion --- .../bigquery/CreateTablesWithPrimaryAndForeignKeysIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/src/test/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeysIT.java b/samples/snippets/src/test/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeysIT.java index 578c7bd37d..00ba8fa441 100644 --- a/samples/snippets/src/test/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeysIT.java +++ b/samples/snippets/src/test/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeysIT.java @@ -77,6 +77,6 @@ public void testCreateAndQueryRepeatedRecordField() { CreateTablesWithPrimaryAndForeignKeys.createTablesWithPrimaryAndForeignKeys( BIGQUERY_DATASET_NAME, tableNamePk, tableNameFk); assertThat(bout.toString()) - .contains("Query with Array of struct parameters performed successfully."); + .contains("Tables with primary and foreign keys created successfully."); } } From 14c673ac968948893442a48f0ed0a3ade7df9286 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Thu, 15 Jun 2023 14:06:04 +0000 Subject: [PATCH 08/10] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20?= =?UTF-8?q?post-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- README.md | 9 +++++---- .../bigquery/CreateTablesWithPrimaryAndForeignKeys.java | 5 ++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b7a5c463fc..371a1f93ea 100644 --- a/README.md +++ b/README.md @@ -53,20 +53,20 @@ If you are using Maven without the BOM, add this to your dependencies: If you are using Gradle 5.x or later, add this to your dependencies: ```Groovy -implementation platform('com.google.cloud:libraries-bom:26.16.0') +implementation platform('com.google.cloud:libraries-bom:26.17.0') implementation 'com.google.cloud:google-cloud-bigquery' ``` If you are using Gradle without BOM, add this to your dependencies: ```Groovy -implementation 'com.google.cloud:google-cloud-bigquery:2.27.0' +implementation 'com.google.cloud:google-cloud-bigquery:2.27.1' ``` If you are using SBT, add this to your dependencies: ```Scala -libraryDependencies += "com.google.cloud" % "google-cloud-bigquery" % "2.27.0" +libraryDependencies += "com.google.cloud" % "google-cloud-bigquery" % "2.27.1" ``` @@ -145,6 +145,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-bigquery/tree | Create Table Cmek | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/CreateTableCmek.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CreateTableCmek.java) | | Create Table External Hive Partitioned | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/CreateTableExternalHivePartitioned.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CreateTableExternalHivePartitioned.java) | | Create Table Without Schema | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/CreateTableWithoutSchema.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CreateTableWithoutSchema.java) | +| Create Tables With Primary And Foreign Keys | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java) | | Create View | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/CreateView.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CreateView.java) | | Dataset Exists | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/DatasetExists.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/DatasetExists.java) | | Ddl Create View | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/DdlCreateView.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/DdlCreateView.java) | @@ -350,7 +351,7 @@ Java is a registered trademark of Oracle and/or its affiliates. [kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-bigquery/java11.html [stability-image]: https://img.shields.io/badge/stability-stable-green [maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-bigquery.svg -[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-bigquery/2.27.0 +[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-bigquery/2.27.1 [authentication]: https://github.com/googleapis/google-cloud-java#authentication [auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes [predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles diff --git a/samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java b/samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java index 6048ebc82e..bbdf557f06 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java +++ b/samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java @@ -60,9 +60,8 @@ public static void createTablesWithPrimaryAndForeignKeys( BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); // TableIds referenced by foreign keys need project id to be set - TableId tableIdPk = TableId.of(bigquery.getOptions().getProjectId(), - datasetName, - tableNamePk); + TableId tableIdPk = + TableId.of(bigquery.getOptions().getProjectId(), datasetName, tableNamePk); TableId tableIdFk = TableId.of(datasetName, tableNameFk); PrimaryKey primaryKey = From c4a4ffc3a51ab694c2c28e5cc6f885f90db01c19 Mon Sep 17 00:00:00 2001 From: obada-ab Date: Fri, 16 Jun 2023 13:41:31 +0200 Subject: [PATCH 09/10] refactor: wrap primary and foreign key in TableConstraints --- .../clirr-ignored-differences.xml | 24 +--- .../com/google/cloud/bigquery/ForeignKey.java | 25 ++-- .../com/google/cloud/bigquery/PrimaryKey.java | 5 +- .../bigquery/StandardTableDefinition.java | 48 ++----- .../java/com/google/cloud/bigquery/Table.java | 10 +- .../cloud/bigquery/TableConstraints.java | 86 +++++++++++++ .../com/google/cloud/bigquery/TableInfo.java | 64 ++-------- .../cloud/bigquery/ColumnReferenceTest.java | 69 ++++++++++ .../cloud/bigquery/TableConstraintsTest.java | 119 ++++++++++++++++++ .../cloud/bigquery/it/ITBigQueryTest.java | 51 +++++--- ...CreateTablesWithPrimaryAndForeignKeys.java | 14 ++- 11 files changed, 362 insertions(+), 153 deletions(-) create mode 100644 google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableConstraints.java create mode 100644 google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ColumnReferenceTest.java create mode 100644 google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableConstraintsTest.java diff --git a/google-cloud-bigquery/clirr-ignored-differences.xml b/google-cloud-bigquery/clirr-ignored-differences.xml index 198b39f6b3..784a3c9626 100644 --- a/google-cloud-bigquery/clirr-ignored-differences.xml +++ b/google-cloud-bigquery/clirr-ignored-differences.xml @@ -52,31 +52,11 @@ 7013 com/google/cloud/bigquery/StandardTableDefinition* - *getForeignKeys(*) - - - 7013 - com/google/cloud/bigquery/StandardTableDefinition* - *getPrimaryKey(*) - - - 7013 - com/google/cloud/bigquery/StandardTableDefinition* - *setForeignKeys(*) - - - 7013 - com/google/cloud/bigquery/StandardTableDefinition* - *setPrimaryKey(*) - - - 7013 - com/google/cloud/bigquery/TableInfo* - *setForeignKeys(*) + *TableConstraints(*) 7013 com/google/cloud/bigquery/TableInfo* - *setPrimaryKey(*) + *TableConstraints(*) \ No newline at end of file diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ForeignKey.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ForeignKey.java index 899635e046..69c2f74346 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ForeignKey.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ForeignKey.java @@ -61,16 +61,21 @@ com.google.api.services.bigquery.model.TableConstraints.ForeignKeys toPb() { com.google.api.services.bigquery.model.TableConstraints.ForeignKeys foreignKey = new com.google.api.services.bigquery.model.TableConstraints.ForeignKeys(); - foreignKey.setName(getName()); - TableId referencedTableId = getReferencedTable(); - foreignKey.setReferencedTable( - new com.google.api.services.bigquery.model.TableConstraints.ForeignKeys.ReferencedTable() - .setTableId(referencedTableId.getTable()) - .setDatasetId(referencedTableId.getDataset()) - .setProjectId(referencedTableId.getProject())); - foreignKey.setColumnReferences( - getColumnReferences().stream().map(ColumnReference::toPb).collect(Collectors.toList())); - + if (getName() != null) { + foreignKey.setName(getName()); + } + if (getReferencedTable() != null) { + TableId referencedTableId = getReferencedTable(); + foreignKey.setReferencedTable( + new com.google.api.services.bigquery.model.TableConstraints.ForeignKeys.ReferencedTable() + .setTableId(referencedTableId.getTable()) + .setDatasetId(referencedTableId.getDataset()) + .setProjectId(referencedTableId.getProject())); + } + if (getColumnReferences() != null) { + foreignKey.setColumnReferences( + getColumnReferences().stream().map(ColumnReference::toPb).collect(Collectors.toList())); + } return foreignKey; } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/PrimaryKey.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/PrimaryKey.java index cdd2728533..a8474cf0fb 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/PrimaryKey.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/PrimaryKey.java @@ -43,8 +43,9 @@ com.google.api.services.bigquery.model.TableConstraints.PrimaryKey toPb() { com.google.api.services.bigquery.model.TableConstraints.PrimaryKey primaryKey = new com.google.api.services.bigquery.model.TableConstraints.PrimaryKey(); - primaryKey.setColumns(getColumns()); - + if (getColumns() != null) { + primaryKey.setColumns(getColumns()); + } return primaryKey; } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java index 781e7e667c..8242147df3 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java @@ -18,14 +18,11 @@ import com.google.api.services.bigquery.model.Streamingbuffer; import com.google.api.services.bigquery.model.Table; -import com.google.api.services.bigquery.model.TableConstraints; import com.google.auto.value.AutoValue; import com.google.common.base.MoreObjects; import java.io.Serializable; import java.math.BigInteger; -import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; import javax.annotation.Nullable; /** @@ -164,9 +161,7 @@ public abstract static class Builder */ public abstract Builder setClustering(Clustering clustering); - public abstract Builder setPrimaryKey(PrimaryKey primaryKey); - - public abstract Builder setForeignKeys(List foreignKeys); + public abstract Builder setTableConstraints(TableConstraints tableConstraints); /** Creates a {@code StandardTableDefinition} object. */ public abstract StandardTableDefinition build(); @@ -229,18 +224,11 @@ public abstract static class Builder public abstract Clustering getClustering(); /** - * Returns the primary key for this table. Returns {@code null} if no primary keys are set for - * this table. - */ - @Nullable - public abstract PrimaryKey getPrimaryKey(); - - /** - * Returns foreign keys for this table. Returns {@code null} if no foreign keys exist in this - * table. + * Returns the table constraints for this table. Returns {@code null} if no table constraints are + * set for this table. */ @Nullable - public abstract List getForeignKeys(); + public abstract TableConstraints getTableConstraints(); /** Returns a builder for a BigQuery standard table definition. */ public static Builder newBuilder() { @@ -280,22 +268,8 @@ Table toPb() { if (getClustering() != null) { tablePb.setClustering(getClustering().toPb()); } - if (getPrimaryKey() != null) { - if (tablePb.getTableConstraints() == null) { - tablePb.setTableConstraints(new TableConstraints()); - } - - tablePb.getTableConstraints().setPrimaryKey(getPrimaryKey().toPb()); - } - if (getForeignKeys() != null) { - if (tablePb.getTableConstraints() == null) { - tablePb.setTableConstraints(new TableConstraints()); - } - - tablePb - .getTableConstraints() - .setForeignKeys( - getForeignKeys().stream().map(ForeignKey::toPb).collect(Collectors.toList())); + if (getTableConstraints() != null) { + tablePb.setTableConstraints(getTableConstraints().toPb()); } return tablePb; } @@ -335,15 +309,7 @@ static StandardTableDefinition fromPb(Table tablePb) { builder.setNumLongTermBytes(tablePb.getNumLongTermBytes()); } if (tablePb.getTableConstraints() != null) { - if (tablePb.getTableConstraints().getPrimaryKey() != null) { - builder.setPrimaryKey(PrimaryKey.fromPb(tablePb.getTableConstraints().getPrimaryKey())); - } - if (tablePb.getTableConstraints().getForeignKeys() != null) { - builder.setForeignKeys( - tablePb.getTableConstraints().getForeignKeys().stream() - .map(ForeignKey::fromPb) - .collect(Collectors.toList())); - } + builder.setTableConstraints(TableConstraints.fromPb(tablePb.getTableConstraints())); } return builder.setNumBytes(tablePb.getNumBytes()).setLocation(tablePb.getLocation()).build(); } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java index 485ca80ceb..cf1dfb4f7f 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java @@ -169,14 +169,8 @@ public TableInfo.Builder setCloneDefinition(CloneDefinition cloneDefinition) { } @Override - public Builder setPrimaryKey(PrimaryKey primaryKey) { - infoBuilder.setPrimaryKey(primaryKey); - return this; - } - - @Override - public Builder setForeignKeys(List foreignKeys) { - infoBuilder.setForeignKeys(foreignKeys); + public Builder setTableConstraints(TableConstraints tableConstraints) { + infoBuilder.setTableConstraints(tableConstraints); return this; } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableConstraints.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableConstraints.java new file mode 100644 index 0000000000..ad30eafcc2 --- /dev/null +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableConstraints.java @@ -0,0 +1,86 @@ +/* + * Copyright 2023 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.auto.value.AutoValue; +import com.google.common.annotations.VisibleForTesting; +import java.io.Serializable; +import java.util.List; +import java.util.stream.Collectors; +import javax.annotation.Nullable; + +@AutoValue +public abstract class TableConstraints implements Serializable { + public static TableConstraints.Builder newBuilder() { + return new AutoValue_TableConstraints.Builder(); + } + + static TableConstraints fromPb( + com.google.api.services.bigquery.model.TableConstraints tableConstraints) { + TableConstraints.Builder builder = newBuilder(); + + if (tableConstraints.getForeignKeys() != null) { + builder.setForeignKeys( + tableConstraints.getForeignKeys().stream() + .map(ForeignKey::fromPb) + .collect(Collectors.toList())); + } + if (tableConstraints.getPrimaryKey() != null) { + builder.setPrimaryKey(PrimaryKey.fromPb(tableConstraints.getPrimaryKey())); + } + + return builder.build(); + } + + com.google.api.services.bigquery.model.TableConstraints toPb() { + + com.google.api.services.bigquery.model.TableConstraints tableConstraints = + new com.google.api.services.bigquery.model.TableConstraints(); + if (getForeignKeys() != null) { + tableConstraints.setForeignKeys( + getForeignKeys().stream().map(ForeignKey::toPb).collect(Collectors.toList())); + } + if (getPrimaryKey() != null) { + tableConstraints.setPrimaryKey(getPrimaryKey().toPb()); + } + + return tableConstraints; + } + + @Nullable + public abstract List getForeignKeys(); + + @Nullable + public abstract PrimaryKey getPrimaryKey(); + + /** Returns a builder for table constraints. */ + @VisibleForTesting + public abstract TableConstraints.Builder toBuilder(); + + @AutoValue.Builder + public abstract static class Builder { + + /** The list of foreign keys for the table constraints. * */ + public abstract TableConstraints.Builder setForeignKeys(List foreignKeys); + + /** The primary key for the table constraints. * */ + public abstract TableConstraints.Builder setPrimaryKey(PrimaryKey primaryKey); + + /** Creates a {@code TableConstraints} object. */ + public abstract TableConstraints build(); + } +} diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java index 839f850db8..9133c0da21 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java @@ -23,15 +23,12 @@ import com.google.api.client.util.Strings; import com.google.api.core.BetaApi; import com.google.api.services.bigquery.model.Table; -import com.google.api.services.bigquery.model.TableConstraints; import com.google.common.base.Function; import com.google.common.base.MoreObjects; import java.io.Serializable; import java.math.BigInteger; -import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.stream.Collectors; /** * Google BigQuery table information. Use {@link StandardTableDefinition} to create simple BigQuery @@ -78,8 +75,7 @@ public Table apply(TableInfo tableInfo) { private final String defaultCollation; private final CloneDefinition cloneDefinition; - private final PrimaryKey primaryKey; - private final List foreignKeys; + private final TableConstraints tableConstraints; /** A builder for {@code TableInfo} objects. */ public abstract static class Builder { @@ -148,9 +144,7 @@ public Builder setRequirePartitionFilter(Boolean requirePartitionFilter) { public abstract Builder setCloneDefinition(CloneDefinition cloneDefinition); - public abstract Builder setPrimaryKey(PrimaryKey primaryKey); - - public abstract Builder setForeignKeys(List foreignKeys); + public abstract Builder setTableConstraints(TableConstraints tableConstraints); } static class BuilderImpl extends Builder { @@ -173,8 +167,7 @@ static class BuilderImpl extends Builder { private Boolean requirePartitionFilter; private String defaultCollation; private CloneDefinition cloneDefinition; - private PrimaryKey primaryKey; - private List foreignKeys; + private TableConstraints tableConstraints; BuilderImpl() {} @@ -197,8 +190,7 @@ static class BuilderImpl extends Builder { this.requirePartitionFilter = tableInfo.requirePartitionFilter; this.defaultCollation = tableInfo.defaultCollation; this.cloneDefinition = tableInfo.cloneDefinition; - this.primaryKey = tableInfo.primaryKey; - this.foreignKeys = tableInfo.foreignKeys; + this.tableConstraints = tableInfo.tableConstraints; } BuilderImpl(Table tablePb) { @@ -228,15 +220,7 @@ static class BuilderImpl extends Builder { this.cloneDefinition = CloneDefinition.fromPb(tablePb.getCloneDefinition()); } if (tablePb.getTableConstraints() != null) { - if (tablePb.getTableConstraints().getPrimaryKey() != null) { - this.primaryKey = PrimaryKey.fromPb(tablePb.getTableConstraints().getPrimaryKey()); - } - if (tablePb.getTableConstraints().getForeignKeys() != null) { - this.foreignKeys = - tablePb.getTableConstraints().getForeignKeys().stream() - .map(ForeignKey::fromPb) - .collect(Collectors.toList()); - } + this.tableConstraints = TableConstraints.fromPb(tablePb.getTableConstraints()); } } @@ -347,13 +331,8 @@ public Builder setCloneDefinition(CloneDefinition cloneDefinition) { return this; } - public Builder setPrimaryKey(PrimaryKey primaryKey) { - this.primaryKey = primaryKey; - return this; - } - - public Builder setForeignKeys(List foreignKeys) { - this.foreignKeys = foreignKeys; + public Builder setTableConstraints(TableConstraints tableConstraints) { + this.tableConstraints = tableConstraints; return this; } @@ -382,8 +361,7 @@ public TableInfo build() { this.requirePartitionFilter = builder.requirePartitionFilter; this.defaultCollation = builder.defaultCollation; this.cloneDefinition = builder.cloneDefinition; - this.primaryKey = builder.primaryKey; - this.foreignKeys = builder.foreignKeys; + this.tableConstraints = builder.tableConstraints; } /** Returns the hash of the table resource. */ @@ -494,12 +472,8 @@ public CloneDefinition getCloneDefinition() { return cloneDefinition; } - public PrimaryKey getPrimaryKey() { - return primaryKey; - } - - public List getForeignKeys() { - return foreignKeys; + public TableConstraints getTableConstraints() { + return tableConstraints; } /** Returns a builder for the table object. */ @@ -528,8 +502,7 @@ public String toString() { .add("requirePartitionFilter", requirePartitionFilter) .add("defaultCollation", defaultCollation) .add("cloneDefinition", cloneDefinition) - .add("primaryKey", primaryKey) - .add("foreignKeys", foreignKeys) + .add("primaryKey", tableConstraints) .toString(); } @@ -597,19 +570,8 @@ Table toPb() { if (cloneDefinition != null) { tablePb.setCloneDefinition(cloneDefinition.toPb()); } - if (primaryKey != null) { - if (tablePb.getTableConstraints() == null) { - tablePb.setTableConstraints(new TableConstraints()); - } - tablePb.getTableConstraints().setPrimaryKey(primaryKey.toPb()); - } - if (foreignKeys != null) { - if (tablePb.getTableConstraints() == null) { - tablePb.setTableConstraints(new TableConstraints()); - } - tablePb - .getTableConstraints() - .setForeignKeys(foreignKeys.stream().map(ForeignKey::toPb).collect(Collectors.toList())); + if (tableConstraints != null) { + tablePb.setTableConstraints(tableConstraints.toPb()); } return tablePb; } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ColumnReferenceTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ColumnReferenceTest.java new file mode 100644 index 0000000000..7a6cac30f6 --- /dev/null +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ColumnReferenceTest.java @@ -0,0 +1,69 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.bigquery; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class ColumnReferenceTest { + private static final ColumnReference COLUMN_REFERENCE = + ColumnReference.newBuilder() + .setReferencingColumn("column1") + .setReferencedColumn("column2") + .build(); + + @Test + public void testToBuilder() { + compareColumnReferenceDefinition(COLUMN_REFERENCE, COLUMN_REFERENCE.toBuilder().build()); + ColumnReference columnReference = + COLUMN_REFERENCE + .toBuilder() + .setReferencingColumn("col1") + .setReferencedColumn("col2") + .build(); + assertEquals("col1", columnReference.getReferencingColumn()); + assertEquals("col2", columnReference.getReferencedColumn()); + } + + @Test + public void testBuilder() { + assertEquals("column1", COLUMN_REFERENCE.getReferencingColumn()); + assertEquals("column2", COLUMN_REFERENCE.getReferencedColumn()); + ColumnReference columnReference = + COLUMN_REFERENCE + .newBuilder() + .setReferencingColumn("column1") + .setReferencedColumn("column2") + .build(); + assertEquals(COLUMN_REFERENCE, columnReference); + } + + @Test + public void testToAndFromPb() { + ColumnReference columnReference = COLUMN_REFERENCE.toBuilder().build(); + assertTrue(ColumnReference.fromPb(columnReference.toPb()) instanceof ColumnReference); + compareColumnReferenceDefinition( + columnReference, ColumnReference.fromPb(columnReference.toPb())); + } + + private void compareColumnReferenceDefinition(ColumnReference expected, ColumnReference value) { + assertEquals(expected.getReferencingColumn(), value.getReferencingColumn()); + assertEquals(expected.getReferencedColumn(), value.getReferencedColumn()); + } +} diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableConstraintsTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableConstraintsTest.java new file mode 100644 index 0000000000..05f3bbf41c --- /dev/null +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableConstraintsTest.java @@ -0,0 +1,119 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.bigquery; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import org.junit.Test; + +public class TableConstraintsTest { + private static final List COLUMNS_PK = Arrays.asList("column1", "column2"); + private static final PrimaryKey PRIMARY_KEY = + PrimaryKey.newBuilder().setColumns(COLUMNS_PK).build(); + private static final TableId TABLE_ID_PK = TableId.of("project", "dataset", "table"); + + private static final ColumnReference COLUMN_REFERENCE = + ColumnReference.newBuilder() + .setReferencingColumn("column1") + .setReferencedColumn("column2") + .build(); + private static final ForeignKey FOREIGN_KEY = + ForeignKey.newBuilder() + .setName("foreign_key") + .setReferencedTable(TABLE_ID_PK) + .setColumnReferences(Collections.singletonList(COLUMN_REFERENCE)) + .build(); + + private static final TableConstraints TABLE_CONSTRAINTS = + TableConstraints.newBuilder() + .setPrimaryKey(PRIMARY_KEY) + .setForeignKeys(Collections.singletonList(FOREIGN_KEY)) + .build(); + + @Test + public void testToBuilder() { + compareTableConstraintsDefinition(TABLE_CONSTRAINTS, TABLE_CONSTRAINTS.toBuilder().build()); + List columnsPk = Arrays.asList("col1", "col2", "col3"); + PrimaryKey primaryKey = PrimaryKey.newBuilder().setColumns(columnsPk).build(); + TableId referencedTable = TableId.of("project1", "dataset1", "table1"); + TableId referencedTable2 = TableId.of("project2", "dataset2", "table2"); + ArrayList columnReferences = new ArrayList<>(); + columnReferences.add( + ColumnReference.newBuilder() + .setReferencingColumn("from") + .setReferencedColumn("to") + .build()); + columnReferences.add( + ColumnReference.newBuilder() + .setReferencingColumn("from2") + .setReferencedColumn("to2") + .build()); + ForeignKey foreignKey1 = + ForeignKey.newBuilder() + .setName("test") + .setReferencedTable(referencedTable) + .setColumnReferences(columnReferences) + .build(); + ForeignKey foreignKey2 = + ForeignKey.newBuilder() + .setName("test") + .setReferencedTable(referencedTable2) + .setColumnReferences(columnReferences) + .build(); + + TableConstraints tableConstraints = + TABLE_CONSTRAINTS + .toBuilder() + .setForeignKeys(Arrays.asList(foreignKey1, foreignKey2)) + .setPrimaryKey(primaryKey) + .build(); + assertEquals(Arrays.asList(foreignKey1, foreignKey2), tableConstraints.getForeignKeys()); + assertEquals(primaryKey, tableConstraints.getPrimaryKey()); + } + + @Test + public void testBuilder() { + assertEquals(Collections.singletonList(FOREIGN_KEY), TABLE_CONSTRAINTS.getForeignKeys()); + assertEquals(PRIMARY_KEY, TABLE_CONSTRAINTS.getPrimaryKey()); + TableConstraints tableConstraints = + TABLE_CONSTRAINTS + .newBuilder() + .setForeignKeys(Collections.singletonList(FOREIGN_KEY)) + .setPrimaryKey(PRIMARY_KEY) + .build(); + assertEquals(TABLE_CONSTRAINTS, tableConstraints); + } + + @Test + public void testToAndFromPb() { + TableConstraints tableConstraints = TABLE_CONSTRAINTS.toBuilder().build(); + assertTrue(TableConstraints.fromPb(tableConstraints.toPb()) instanceof TableConstraints); + compareTableConstraintsDefinition( + tableConstraints, TableConstraints.fromPb(tableConstraints.toPb())); + } + + private void compareTableConstraintsDefinition( + TableConstraints expected, TableConstraints value) { + assertEquals(expected.getForeignKeys(), value.getForeignKeys()); + assertEquals(expected.getPrimaryKey(), value.getPrimaryKey()); + } +} diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index 54e6534fa7..dbab63709f 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -115,6 +115,7 @@ import com.google.cloud.bigquery.StandardSQLTypeName; import com.google.cloud.bigquery.StandardTableDefinition; import com.google.cloud.bigquery.Table; +import com.google.cloud.bigquery.TableConstraints; import com.google.cloud.bigquery.TableDataWriteChannel; import com.google.cloud.bigquery.TableDefinition; import com.google.cloud.bigquery.TableId; @@ -5692,18 +5693,21 @@ public void testPrimaryKey() { String tableName = "test_primary_key"; TableId tableId = TableId.of(DATASET, tableName); PrimaryKey primaryKey = PrimaryKey.newBuilder().setColumns(Arrays.asList("ID")).build(); + TableConstraints tableConstraintsPk = + TableConstraints.newBuilder().setPrimaryKey(primaryKey).build(); try { StandardTableDefinition tableDefinition = StandardTableDefinition.newBuilder() .setSchema(CONSTRAINTS_TABLE_SCHEMA) - .setPrimaryKey(primaryKey) + .setTableConstraints(tableConstraintsPk) .build(); Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition)); assertNotNull(createdTable); Table remoteTable = bigquery.getTable(DATASET, tableName); assertEquals( - primaryKey, remoteTable.getDefinition().getPrimaryKey()); + tableConstraintsPk, + remoteTable.getDefinition().getTableConstraints()); } finally { bigquery.delete(tableId); } @@ -5715,6 +5719,8 @@ public void testPrimaryKeyUpdate() { TableId tableId = TableId.of(DATASET, tableName); PrimaryKey primaryKey = PrimaryKey.newBuilder().setColumns(Arrays.asList("FirstName", "LastName")).build(); + TableConstraints tableConstraintsPk = + TableConstraints.newBuilder().setPrimaryKey(primaryKey).build(); try { StandardTableDefinition tableDefinition = @@ -5722,13 +5728,15 @@ public void testPrimaryKeyUpdate() { Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition)); assertNotNull(createdTable); Table remoteTable = bigquery.getTable(DATASET, tableName); - assertNull(remoteTable.getDefinition().getPrimaryKey()); + assertNull(remoteTable.getDefinition().getTableConstraints()); - Table updatedTable = remoteTable.toBuilder().setPrimaryKey(primaryKey).build().update(); + Table updatedTable = + remoteTable.toBuilder().setTableConstraints(tableConstraintsPk).build().update(); assertNotNull(updatedTable); Table remoteUpdatedTable = bigquery.getTable(DATASET, tableName); assertEquals( - primaryKey, remoteUpdatedTable.getDefinition().getPrimaryKey()); + tableConstraintsPk, + remoteUpdatedTable.getDefinition().getTableConstraints()); } finally { bigquery.delete(tableId); } @@ -5746,18 +5754,23 @@ public void testForeignKeys() { PrimaryKey primaryKey = PrimaryKey.newBuilder().setColumns(Collections.singletonList("ID")).build(); + TableConstraints tableConstraintsPk = + TableConstraints.newBuilder().setPrimaryKey(primaryKey).build(); + ForeignKey foreignKey = ForeignKey.newBuilder() .setName("foreign_key") .setReferencedTable(tableIdPk) .setColumnReferences(Collections.singletonList(columnReference)) .build(); + TableConstraints tableConstraintsFk = + TableConstraints.newBuilder().setForeignKeys(Collections.singletonList(foreignKey)).build(); try { StandardTableDefinition tableDefinitionPk = StandardTableDefinition.newBuilder() .setSchema(CONSTRAINTS_TABLE_SCHEMA) - .setPrimaryKey(primaryKey) + .setTableConstraints(tableConstraintsPk) .build(); Table createdTablePk = bigquery.create(TableInfo.of(tableIdPk, tableDefinitionPk)); assertNotNull(createdTablePk); @@ -5765,14 +5778,14 @@ public void testForeignKeys() { StandardTableDefinition tableDefinitionFk = StandardTableDefinition.newBuilder() .setSchema(CONSTRAINTS_TABLE_SCHEMA) - .setForeignKeys(Collections.singletonList(foreignKey)) + .setTableConstraints(tableConstraintsFk) .build(); Table createdTableFk = bigquery.create(TableInfo.of(tableIdFk, tableDefinitionFk)); assertNotNull(createdTableFk); Table remoteTable = bigquery.getTable(DATASET, tableNameFk); assertEquals( - Collections.singletonList(foreignKey), - remoteTable.getDefinition().getForeignKeys()); + tableConstraintsFk, + remoteTable.getDefinition().getTableConstraints()); } finally { bigquery.delete(tableIdPk); bigquery.delete(tableIdFk); @@ -5796,6 +5809,9 @@ public void testForeignKeysUpdate() { ColumnReference.newBuilder().setReferencingColumn("ID").setReferencedColumn("ID").build(); PrimaryKey primaryKey1 = PrimaryKey.newBuilder().setColumns(Collections.singletonList("ID")).build(); + TableConstraints tableConstraintsPk1 = + TableConstraints.newBuilder().setPrimaryKey(primaryKey1).build(); + ForeignKey foreignKey1 = ForeignKey.newBuilder() .setName("foreign_key1") @@ -5822,6 +5838,8 @@ public void testForeignKeysUpdate() { primaryKey2Columns.add("LastName"); PrimaryKey primaryKey2 = PrimaryKey.newBuilder().setColumns(primaryKey2Columns).build(); + TableConstraints tableConstraintsPk2 = + TableConstraints.newBuilder().setPrimaryKey(primaryKey2).build(); ForeignKey foreignKey2 = ForeignKey.newBuilder() .setName("foreign_key2") @@ -5829,6 +5847,8 @@ public void testForeignKeysUpdate() { .setColumnReferences(columnReferencesPk2) .build(); foreignKeys.add(foreignKey2); + TableConstraints tableConstraintsFk = + TableConstraints.newBuilder().setForeignKeys(foreignKeys).build(); try { StandardTableDefinition tableDefinitionFk = @@ -5839,7 +5859,7 @@ public void testForeignKeysUpdate() { StandardTableDefinition tableDefinitionPk1 = StandardTableDefinition.newBuilder() .setSchema(CONSTRAINTS_TABLE_SCHEMA) - .setPrimaryKey(primaryKey1) + .setTableConstraints(tableConstraintsPk1) .build(); Table createdTablePk1 = bigquery.create(TableInfo.of(tableIdPk1, tableDefinitionPk1)); assertNotNull(createdTablePk1); @@ -5847,21 +5867,22 @@ public void testForeignKeysUpdate() { StandardTableDefinition tableDefinitionPk2 = StandardTableDefinition.newBuilder() .setSchema(CONSTRAINTS_TABLE_SCHEMA) - .setPrimaryKey(primaryKey2) + .setTableConstraints(tableConstraintsPk2) .build(); Table createdTablePk2 = bigquery.create(TableInfo.of(tableIdPk2, tableDefinitionPk2)); assertNotNull(createdTablePk2); Table remoteTable = bigquery.getTable(DATASET, tableNameFk); - assertNull(remoteTable.getDefinition().getForeignKeys()); + assertNull(remoteTable.getDefinition().getTableConstraints()); - Table updatedTable = remoteTable.toBuilder().setForeignKeys(foreignKeys).build().update(); + Table updatedTable = + remoteTable.toBuilder().setTableConstraints(tableConstraintsFk).build().update(); assertNotNull(updatedTable); Table remoteUpdatedTable = bigquery.getTable(DATASET, tableNameFk); assertEquals( - foreignKeys, - remoteUpdatedTable.getDefinition().getForeignKeys()); + tableConstraintsFk, + remoteUpdatedTable.getDefinition().getTableConstraints()); } finally { bigquery.delete(tableIdFk); bigquery.delete(tableIdPk1); diff --git a/samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java b/samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java index 6048ebc82e..b0c571f55a 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java +++ b/samples/snippets/src/main/java/com/example/bigquery/CreateTablesWithPrimaryAndForeignKeys.java @@ -16,7 +16,7 @@ package com.example.bigquery; -// [START bigquery_set_primary_and_foreign_keys] +// [START bigquery_create_tables_with_primary_and_foreign_keys] import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.BigQueryException; import com.google.cloud.bigquery.BigQueryOptions; @@ -27,8 +27,10 @@ import com.google.cloud.bigquery.PrimaryKey; import com.google.cloud.bigquery.Schema; import com.google.cloud.bigquery.StandardTableDefinition; +import com.google.cloud.bigquery.TableConstraints; import com.google.cloud.bigquery.TableId; import com.google.cloud.bigquery.TableInfo; +import java.util.Arrays; import java.util.Collections; // Create tables with primary/foreign key columns @@ -67,6 +69,8 @@ public static void createTablesWithPrimaryAndForeignKeys( PrimaryKey primaryKey = PrimaryKey.newBuilder().setColumns(Collections.singletonList("ID")).build(); + TableConstraints tableConstraintsPk = + TableConstraints.newBuilder().setPrimaryKey(primaryKey).build(); ColumnReference columnReference = ColumnReference.newBuilder().setReferencingColumn("ID").setReferencedColumn("ID").build(); @@ -76,12 +80,14 @@ public static void createTablesWithPrimaryAndForeignKeys( .setColumnReferences(Collections.singletonList(columnReference)) .setReferencedTable(tableIdPk) .build(); + TableConstraints tableConstraintsFk = + TableConstraints.newBuilder().setForeignKeys(Arrays.asList(foreignKey)).build(); // Create a table with a primary key StandardTableDefinition tableDefinitionPk = StandardTableDefinition.newBuilder() .setSchema(PK_FK_SCHEMA) - .setPrimaryKey(primaryKey) + .setTableConstraints(tableConstraintsPk) .build(); TableInfo tableInfoPk = TableInfo.of(tableIdPk, tableDefinitionPk); bigquery.create(tableInfoPk); @@ -90,7 +96,7 @@ public static void createTablesWithPrimaryAndForeignKeys( StandardTableDefinition tableDefinitionFk = StandardTableDefinition.newBuilder() .setSchema(PK_FK_SCHEMA) - .setForeignKeys(Collections.singletonList(foreignKey)) + .setTableConstraints(tableConstraintsFk) .build(); TableInfo tableInfoFk = TableInfo.of(tableIdFk, tableDefinitionFk); bigquery.create(tableInfoFk); @@ -101,4 +107,4 @@ public static void createTablesWithPrimaryAndForeignKeys( } } } -// [END bigquery_set_primary_and_foreign_keys] +// [END bigquery_create_tables_with_primary_and_foreign_keys] From 1de3f8d687d17e8e0f973596479ff3213e59a4dc Mon Sep 17 00:00:00 2001 From: obada-ab Date: Fri, 16 Jun 2023 13:59:48 +0200 Subject: [PATCH 10/10] fix: fix toString method of TableInfo --- .../src/main/java/com/google/cloud/bigquery/TableInfo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java index 9133c0da21..abfe21ec46 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java @@ -502,7 +502,7 @@ public String toString() { .add("requirePartitionFilter", requirePartitionFilter) .add("defaultCollation", defaultCollation) .add("cloneDefinition", cloneDefinition) - .add("primaryKey", tableConstraints) + .add("tableConstraints", tableConstraints) .toString(); }