Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit 08b7dbf

Browse files
feat: add support for partitioning and clustering in MaterializedViewDefinition
1 parent b6ebd14 commit 08b7dbf

3 files changed

Lines changed: 108 additions & 2 deletions

File tree

google-cloud-bigquery/clirr-ignored-differences.xml

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,32 @@
44
<!-- TODO: REMOVE AFTER RELEASE -->
55
<difference>
66
<differenceType>7013</differenceType>
7-
<className>com/google/cloud/bigquery/RoutineInfo$Builder</className>
8-
<method>com.google.cloud.bigquery.RoutineInfo$Builder setReturnTableType(com.google.cloud.bigquery.StandardSQLTableType)</method>
7+
<className>com/google/cloud/bigquery/MaterializedViewDefinition</className>
8+
<method>com.google.cloud.bigquery.Clustering getClustering()</method>
9+
</difference>
10+
<difference>
11+
<differenceType>7013</differenceType>
12+
<className>com/google/cloud/bigquery/MaterializedViewDefinition</className>
13+
<method>com.google.cloud.bigquery.RangePartitioning getRangePartitioning()</method>
14+
</difference>
15+
<difference>
16+
<differenceType>7013</differenceType>
17+
<className>com/google/cloud/bigquery/MaterializedViewDefinition</className>
18+
<method>com.google.cloud.bigquery.TimePartitioning getTimePartitioning()</method>
19+
</difference>
20+
<difference>
21+
<differenceType>7013</differenceType>
22+
<className>com/google/cloud/bigquery/MaterializedViewDefinition$Builder</className>
23+
<method>com.google.cloud.bigquery.MaterializedViewDefinition$Builder setClustering(com.google.cloud.bigquery.Clustering)</method>
24+
</difference>
25+
<difference>
26+
<differenceType>7013</differenceType>
27+
<className>com/google/cloud/bigquery/MaterializedViewDefinition$Builder</className>
28+
<method>com.google.cloud.bigquery.MaterializedViewDefinition$Builder setRangePartitioning(com.google.cloud.bigquery.RangePartitioning)</method>
29+
</difference>
30+
<difference>
31+
<differenceType>7013</differenceType>
32+
<className>com/google/cloud/bigquery/MaterializedViewDefinition$Builder</className>
33+
<method>com.google.cloud.bigquery.MaterializedViewDefinition$Builder setTimePartitioning(com.google.cloud.bigquery.TimePartitioning)</method>
934
</difference>
1035
</differences>

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/MaterializedViewDefinition.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ public abstract static class Builder
5757
@Override
5858
public abstract Builder setType(Type type);
5959

60+
/**
61+
* Sets the time partitioning configuration for the table. If not set, the table is not
62+
* time-partitioned.
63+
*/
64+
public abstract Builder setTimePartitioning(TimePartitioning timePartitioning);
65+
66+
/**
67+
* Sets the range partitioning configuration for the table. Only one of timePartitioning and
68+
* rangePartitioning should be specified.
69+
*/
70+
public abstract Builder setRangePartitioning(RangePartitioning rangePartitioning);
71+
72+
/**
73+
* Set the clustering configuration for the table. If not set, the table is not clustered.
74+
* BigQuery supports clustering for both partitioned and non-partitioned tables.
75+
*/
76+
public abstract Builder setClustering(Clustering clustering);
77+
6078
/** Creates a {@code MaterializedViewDefinition} object. */
6179
@Override
6280
public abstract MaterializedViewDefinition build();
@@ -86,6 +104,27 @@ public abstract static class Builder
86104
@Nullable
87105
public abstract Long getRefreshIntervalMs();
88106

107+
/**
108+
* Returns the time partitioning configuration for this table. If {@code null}, the table is not
109+
* time-partitioned.
110+
*/
111+
@Nullable
112+
public abstract TimePartitioning getTimePartitioning();
113+
114+
/**
115+
* Returns the range partitioning configuration for this table. If {@code null}, the table is not
116+
* range-partitioned.
117+
*/
118+
@Nullable
119+
public abstract RangePartitioning getRangePartitioning();
120+
121+
/**
122+
* Returns the clustering configuration for this table. If {@code null}, the table is not
123+
* clustered.
124+
*/
125+
@Nullable
126+
public abstract Clustering getClustering();
127+
89128
/** Returns a builder for the {@code MaterializedViewDefinition} object. */
90129
public abstract Builder toBuilder();
91130

@@ -107,6 +146,15 @@ Table toPb() {
107146
materializedViewDefinition.setRefreshIntervalMs(getRefreshIntervalMs());
108147
}
109148
tablePb.setMaterializedView(materializedViewDefinition);
149+
if (getTimePartitioning() != null) {
150+
tablePb.setTimePartitioning(getTimePartitioning().toPb());
151+
}
152+
if (getRangePartitioning() != null) {
153+
tablePb.setRangePartitioning(getRangePartitioning().toPb());
154+
}
155+
if (getClustering() != null) {
156+
tablePb.setClustering(getClustering().toPb());
157+
}
110158
return tablePb;
111159
}
112160

@@ -149,6 +197,28 @@ static MaterializedViewDefinition fromPb(Table tablePb) {
149197
if (materializedViewDefinition.getRefreshIntervalMs() != null) {
150198
builder.setRefreshIntervalMs(materializedViewDefinition.getRefreshIntervalMs());
151199
}
200+
if (tablePb.getTimePartitioning() != null) {
201+
try {
202+
builder.setTimePartitioning(TimePartitioning.fromPb(tablePb.getTimePartitioning()));
203+
} catch (IllegalArgumentException e) {
204+
throw new IllegalArgumentException(
205+
"Illegal Argument - Got unexpected time partitioning "
206+
+ tablePb.getTimePartitioning().getType()
207+
+ " in project "
208+
+ tablePb.getTableReference().getProjectId()
209+
+ " in dataset "
210+
+ tablePb.getTableReference().getDatasetId()
211+
+ " in table "
212+
+ tablePb.getTableReference().getTableId(),
213+
e);
214+
}
215+
}
216+
if (tablePb.getRangePartitioning() != null) {
217+
builder.setRangePartitioning(RangePartitioning.fromPb(tablePb.getRangePartitioning()));
218+
}
219+
if (tablePb.getClustering() != null) {
220+
builder.setClustering(Clustering.fromPb(tablePb.getClustering()));
221+
}
152222
}
153223
return builder.build();
154224
}

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/MaterializedViewDefinitionTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertTrue;
2121

22+
import com.google.common.collect.ImmutableList;
2223
import org.junit.Test;
2324

2425
public class MaterializedViewDefinitionTest {
@@ -28,13 +29,19 @@ public class MaterializedViewDefinitionTest {
2829
private static final Boolean ENABLE_REFRESH = false;
2930
private static final Long REFRESH_INTERVAL_MS = 60000L;
3031
private static final Schema SCHEMA = Schema.of();
32+
private static final TimePartitioning TIME_PARTITIONING =
33+
TimePartitioning.of(TimePartitioning.Type.DAY, 42);
34+
private static final Clustering CLUSTERING =
35+
Clustering.newBuilder().setFields(ImmutableList.of("Foo", "Bar")).build();
3136
private static final MaterializedViewDefinition MATERIALIZED_VIEW_DEFINITION =
3237
MaterializedViewDefinition.newBuilder()
3338
.setSchema(SCHEMA)
3439
.setQuery(MATERIALIZED_VIEW_QUERY)
3540
.setLastRefreshTime(LAST_REFRESH_TIME)
3641
.setEnableRefresh(ENABLE_REFRESH)
3742
.setRefreshIntervalMs(REFRESH_INTERVAL_MS)
43+
.setClustering(CLUSTERING)
44+
.setTimePartitioning(TIME_PARTITIONING)
3845
.build();
3946

4047
@Test
@@ -68,6 +75,8 @@ public void testBuilder() {
6875
.setLastRefreshTime(LAST_REFRESH_TIME)
6976
.setEnableRefresh(ENABLE_REFRESH)
7077
.setRefreshIntervalMs(REFRESH_INTERVAL_MS)
78+
.setClustering(CLUSTERING)
79+
.setTimePartitioning(TIME_PARTITIONING)
7180
.build();
7281
assertEquals(MATERIALIZED_VIEW_DEFINITION, materializedViewDefinition);
7382
}
@@ -92,6 +101,8 @@ private void compareMaterializedView(
92101
assertEquals(expected.getLastRefreshTime(), actual.getLastRefreshTime());
93102
assertEquals(expected.getEnableRefresh(), actual.getEnableRefresh());
94103
assertEquals(expected.getRefreshIntervalMs(), actual.getRefreshIntervalMs());
104+
assertEquals(expected.getClustering(), actual.getClustering());
105+
assertEquals(expected.getTimePartitioning(), actual.getTimePartitioning());
95106
assertEquals(expected.toString(), actual.toString());
96107
assertEquals(expected.hashCode(), actual.hashCode());
97108
assertEquals(expected, actual);

0 commit comments

Comments
 (0)