Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: add support for partitioning and clustering in MaterializedView…
…Definition (#1301)

Towards #1300
  • Loading branch information
stephaniewang526 committed May 14, 2021
1 parent 901b18a commit b909754
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
29 changes: 27 additions & 2 deletions google-cloud-bigquery/clirr-ignored-differences.xml
Expand Up @@ -4,7 +4,32 @@
<!-- TODO: REMOVE AFTER RELEASE -->
<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/bigquery/RoutineInfo$Builder</className>
<method>com.google.cloud.bigquery.RoutineInfo$Builder setReturnTableType(com.google.cloud.bigquery.StandardSQLTableType)</method>
<className>com/google/cloud/bigquery/MaterializedViewDefinition</className>
<method>com.google.cloud.bigquery.Clustering getClustering()</method>
</difference>
<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/bigquery/MaterializedViewDefinition</className>
<method>com.google.cloud.bigquery.RangePartitioning getRangePartitioning()</method>
</difference>
<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/bigquery/MaterializedViewDefinition</className>
<method>com.google.cloud.bigquery.TimePartitioning getTimePartitioning()</method>
</difference>
<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/bigquery/MaterializedViewDefinition$Builder</className>
<method>com.google.cloud.bigquery.MaterializedViewDefinition$Builder setClustering(com.google.cloud.bigquery.Clustering)</method>
</difference>
<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/bigquery/MaterializedViewDefinition$Builder</className>
<method>com.google.cloud.bigquery.MaterializedViewDefinition$Builder setRangePartitioning(com.google.cloud.bigquery.RangePartitioning)</method>
</difference>
<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/bigquery/MaterializedViewDefinition$Builder</className>
<method>com.google.cloud.bigquery.MaterializedViewDefinition$Builder setTimePartitioning(com.google.cloud.bigquery.TimePartitioning)</method>
</difference>
</differences>
Expand Up @@ -57,6 +57,25 @@ public abstract static class Builder
@Override
public abstract Builder setType(Type type);

/**
* Sets the time partitioning configuration for the materialized view. If not set, the
* materialized view is not time-partitioned.
*/
public abstract Builder setTimePartitioning(TimePartitioning timePartitioning);

/**
* Sets the range partitioning configuration for the materialized view. Only one of
* timePartitioning and rangePartitioning should be specified.
*/
public abstract Builder setRangePartitioning(RangePartitioning rangePartitioning);

/**
* Set the clustering configuration for the materialized view. If not set, the materialized view
* is not clustered. BigQuery supports clustering for both partitioned and non-partitioned
* materialized views.
*/
public abstract Builder setClustering(Clustering clustering);

/** Creates a {@code MaterializedViewDefinition} object. */
@Override
public abstract MaterializedViewDefinition build();
Expand Down Expand Up @@ -86,6 +105,27 @@ public abstract static class Builder
@Nullable
public abstract Long getRefreshIntervalMs();

/**
* Returns the time partitioning configuration for this table. If {@code null}, the table is not
* time-partitioned.
*/
@Nullable
public abstract TimePartitioning getTimePartitioning();

/**
* Returns the range partitioning configuration for this table. If {@code null}, the table is not
* range-partitioned.
*/
@Nullable
public abstract RangePartitioning getRangePartitioning();

/**
* Returns the clustering configuration for this table. If {@code null}, the table is not
* clustered.
*/
@Nullable
public abstract Clustering getClustering();

/** Returns a builder for the {@code MaterializedViewDefinition} object. */
public abstract Builder toBuilder();

Expand All @@ -107,6 +147,15 @@ Table toPb() {
materializedViewDefinition.setRefreshIntervalMs(getRefreshIntervalMs());
}
tablePb.setMaterializedView(materializedViewDefinition);
if (getTimePartitioning() != null) {
tablePb.setTimePartitioning(getTimePartitioning().toPb());
}
if (getRangePartitioning() != null) {
tablePb.setRangePartitioning(getRangePartitioning().toPb());
}
if (getClustering() != null) {
tablePb.setClustering(getClustering().toPb());
}
return tablePb;
}

Expand Down Expand Up @@ -149,6 +198,15 @@ static MaterializedViewDefinition fromPb(Table tablePb) {
if (materializedViewDefinition.getRefreshIntervalMs() != null) {
builder.setRefreshIntervalMs(materializedViewDefinition.getRefreshIntervalMs());
}
if (tablePb.getTimePartitioning() != null) {
builder.setTimePartitioning(TimePartitioning.fromPb(tablePb.getTimePartitioning()));
}
if (tablePb.getRangePartitioning() != null) {
builder.setRangePartitioning(RangePartitioning.fromPb(tablePb.getRangePartitioning()));
}
if (tablePb.getClustering() != null) {
builder.setClustering(Clustering.fromPb(tablePb.getClustering()));
}
}
return builder.build();
}
Expand Down
Expand Up @@ -19,6 +19,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.common.collect.ImmutableList;
import org.junit.Test;

public class MaterializedViewDefinitionTest {
Expand All @@ -28,13 +29,19 @@ public class MaterializedViewDefinitionTest {
private static final Boolean ENABLE_REFRESH = false;
private static final Long REFRESH_INTERVAL_MS = 60000L;
private static final Schema SCHEMA = Schema.of();
private static final TimePartitioning TIME_PARTITIONING =
TimePartitioning.of(TimePartitioning.Type.DAY, 42);
private static final Clustering CLUSTERING =
Clustering.newBuilder().setFields(ImmutableList.of("Foo", "Bar")).build();
private static final MaterializedViewDefinition MATERIALIZED_VIEW_DEFINITION =
MaterializedViewDefinition.newBuilder()
.setSchema(SCHEMA)
.setQuery(MATERIALIZED_VIEW_QUERY)
.setLastRefreshTime(LAST_REFRESH_TIME)
.setEnableRefresh(ENABLE_REFRESH)
.setRefreshIntervalMs(REFRESH_INTERVAL_MS)
.setClustering(CLUSTERING)
.setTimePartitioning(TIME_PARTITIONING)
.build();

@Test
Expand Down Expand Up @@ -68,6 +75,8 @@ public void testBuilder() {
.setLastRefreshTime(LAST_REFRESH_TIME)
.setEnableRefresh(ENABLE_REFRESH)
.setRefreshIntervalMs(REFRESH_INTERVAL_MS)
.setClustering(CLUSTERING)
.setTimePartitioning(TIME_PARTITIONING)
.build();
assertEquals(MATERIALIZED_VIEW_DEFINITION, materializedViewDefinition);
}
Expand All @@ -92,6 +101,8 @@ private void compareMaterializedView(
assertEquals(expected.getLastRefreshTime(), actual.getLastRefreshTime());
assertEquals(expected.getEnableRefresh(), actual.getEnableRefresh());
assertEquals(expected.getRefreshIntervalMs(), actual.getRefreshIntervalMs());
assertEquals(expected.getClustering(), actual.getClustering());
assertEquals(expected.getTimePartitioning(), actual.getTimePartitioning());
assertEquals(expected.toString(), actual.toString());
assertEquals(expected.hashCode(), actual.hashCode());
assertEquals(expected, actual);
Expand Down

0 comments on commit b909754

Please sign in to comment.