Skip to content

Commit

Permalink
feat: add primary key and foreign keys (#2744)
Browse files Browse the repository at this point in the history
Add support for creating/updating primary and foreign keys

Internal b/284849902
  • Loading branch information
obada-ab authored Jun 16, 2023
1 parent aa38428 commit afb571c
Show file tree
Hide file tree
Showing 16 changed files with 1,143 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
11 changes: 10 additions & 1 deletion google-cloud-bigquery/clirr-ignored-differences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,19 @@
<className>com/google/cloud/bigquery/TableInfo*</className>
<method>*DefaultCollation(*)</method>
</difference>

<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/bigquery/TableInfo*</className>
<method>*CloneDefinition(*)</method>
</difference>
<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/bigquery/StandardTableDefinition*</className>
<method>*TableConstraints(*)</method>
</difference>
<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/bigquery/TableInfo*</className>
<method>*TableConstraints(*)</method>
</difference>
</differences>
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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 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) {
builder.setColumnReferences(
foreignKey.getColumnReferences().stream()
.map(ColumnReference::fromPb)
.collect(Collectors.toList()));
}

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();
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;
}

@Nullable
public abstract String getName();

@Nullable
public abstract TableId getReferencedTable();

@Nullable
public abstract List<ColumnReference> 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<ColumnReference> columnReferences);

/** Creates a {@code ForignKey} object. */
public abstract ForeignKey build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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 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();
if (getColumns() != null) {
primaryKey.setColumns(getColumns());
}
return primaryKey;
}

@Nullable
public abstract List<String> 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<String> columns);

/** Creates a {@code PrimaryKey} object. */
public abstract PrimaryKey build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ public abstract static class Builder
*/
public abstract Builder setClustering(Clustering clustering);

public abstract Builder setTableConstraints(TableConstraints tableConstraints);

/** Creates a {@code StandardTableDefinition} object. */
public abstract StandardTableDefinition build();
}
Expand Down Expand Up @@ -221,6 +223,13 @@ public abstract static class Builder
@Nullable
public abstract Clustering getClustering();

/**
* Returns the table constraints for this table. Returns {@code null} if no table constraints are
* set for this table.
*/
@Nullable
public abstract TableConstraints getTableConstraints();

/** Returns a builder for a BigQuery standard table definition. */
public static Builder newBuilder() {
return new AutoValue_StandardTableDefinition.Builder().setType(Type.TABLE);
Expand Down Expand Up @@ -259,6 +268,9 @@ Table toPb() {
if (getClustering() != null) {
tablePb.setClustering(getClustering().toPb());
}
if (getTableConstraints() != null) {
tablePb.setTableConstraints(getTableConstraints().toPb());
}
return tablePb;
}

Expand Down Expand Up @@ -296,6 +308,9 @@ static StandardTableDefinition fromPb(Table tablePb) {
if (tablePb.getNumLongTermBytes() != null) {
builder.setNumLongTermBytes(tablePb.getNumLongTermBytes());
}
if (tablePb.getTableConstraints() != null) {
builder.setTableConstraints(TableConstraints.fromPb(tablePb.getTableConstraints()));
}
return builder.setNumBytes(tablePb.getNumBytes()).setLocation(tablePb.getLocation()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ public TableInfo.Builder setCloneDefinition(CloneDefinition cloneDefinition) {
return this;
}

@Override
public Builder setTableConstraints(TableConstraints tableConstraints) {
infoBuilder.setTableConstraints(tableConstraints);
return this;
}

@Override
public Table build() {
return new Table(bigquery, infoBuilder);
Expand Down
Loading

0 comments on commit afb571c

Please sign in to comment.