Skip to content

Commit b909754

Browse files
feat: add support for partitioning and clustering in MaterializedViewDefinition (#1301)
Towards #1300
1 parent 901b18a commit b909754

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ public abstract static class Builder
5757
@Override
5858
public abstract Builder setType(Type type);
5959

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

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

@@ -107,6 +147,15 @@ Table toPb() {
107147
materializedViewDefinition.setRefreshIntervalMs(getRefreshIntervalMs());
108148
}
109149
tablePb.setMaterializedView(materializedViewDefinition);
150+
if (getTimePartitioning() != null) {
151+
tablePb.setTimePartitioning(getTimePartitioning().toPb());
152+
}
153+
if (getRangePartitioning() != null) {
154+
tablePb.setRangePartitioning(getRangePartitioning().toPb());
155+
}
156+
if (getClustering() != null) {
157+
tablePb.setClustering(getClustering().toPb());
158+
}
110159
return tablePb;
111160
}
112161

@@ -149,6 +198,15 @@ static MaterializedViewDefinition fromPb(Table tablePb) {
149198
if (materializedViewDefinition.getRefreshIntervalMs() != null) {
150199
builder.setRefreshIntervalMs(materializedViewDefinition.getRefreshIntervalMs());
151200
}
201+
if (tablePb.getTimePartitioning() != null) {
202+
builder.setTimePartitioning(TimePartitioning.fromPb(tablePb.getTimePartitioning()));
203+
}
204+
if (tablePb.getRangePartitioning() != null) {
205+
builder.setRangePartitioning(RangePartitioning.fromPb(tablePb.getRangePartitioning()));
206+
}
207+
if (tablePb.getClustering() != null) {
208+
builder.setClustering(Clustering.fromPb(tablePb.getClustering()));
209+
}
152210
}
153211
return builder.build();
154212
}

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy