Skip to content

Commit 3c2725c

Browse files
authored
bigquery: allow user to null partition expiration (#3353)
1 parent ceeda43 commit 3c2725c

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed

google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimePartitioning.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616

1717
package com.google.cloud.bigquery;
1818

19-
import static com.google.common.base.Preconditions.checkNotNull;
19+
import static com.google.common.base.MoreObjects.firstNonNull;
2020

21+
import com.google.api.client.util.Data;
2122
import com.google.api.core.BetaApi;
2223
import com.google.auto.value.AutoValue;
23-
import com.google.common.base.MoreObjects;
24-
25-
import javax.annotation.Nullable;
2624
import java.io.Serializable;
27-
import java.util.Objects;
25+
import javax.annotation.Nullable;
2826

2927
/**
3028
* Objects of this class allow to configure table partitioning based on time. By dividing a large
@@ -63,7 +61,7 @@ public enum Type {
6361

6462
/**
6563
* Returns the number of milliseconds for which to keep the storage for a partition. When expired,
66-
* the storage for the partition is reclaimed.
64+
* the storage for the partition is reclaimed. If null, the partion does not expire.
6765
*/
6866
@Nullable
6967
public abstract Long getExpirationMs();
@@ -115,7 +113,7 @@ public static Builder newBuilder(Type type) {
115113
/**
116114
* Returns a {@code TimePartitioning} object given the time partitioning type. Currently, the only
117115
* type supported is {@link Type#DAY}, which will generate one partition per day based on data
118-
* loading time.
116+
* loading time. The partitions will not expire.
119117
*/
120118
public static TimePartitioning of(Type type) {
121119
return newBuilder(type).build();
@@ -137,18 +135,22 @@ com.google.api.services.bigquery.model.TimePartitioning toPb() {
137135
com.google.api.services.bigquery.model.TimePartitioning partitioningPb =
138136
new com.google.api.services.bigquery.model.TimePartitioning();
139137
partitioningPb.setType(getType().name());
140-
partitioningPb.setExpirationMs(getExpirationMs());
138+
partitioningPb.setExpirationMs(firstNonNull(getExpirationMs(), Data.NULL_LONG));
141139
partitioningPb.setRequirePartitionFilter(getRequirePartitionFilter());
142140
partitioningPb.setField(getField());
143141
return partitioningPb;
144142
}
145143

146144
static TimePartitioning fromPb(
147145
com.google.api.services.bigquery.model.TimePartitioning partitioningPb) {
146+
Long expirationMs = partitioningPb.getExpirationMs();
147+
if (Data.isNull(expirationMs)) {
148+
expirationMs = null;
149+
}
148150
return newBuilder(Type.valueOf(partitioningPb.getType()))
149-
.setExpirationMs(partitioningPb.getExpirationMs())
150-
.setField(partitioningPb.getField())
151-
.setRequirePartitionFilter(partitioningPb.getRequirePartitionFilter())
152-
.build();
151+
.setExpirationMs(expirationMs)
152+
.setField(partitioningPb.getField())
153+
.setRequirePartitionFilter(partitioningPb.getRequirePartitionFilter())
154+
.build();
153155
}
154156
}

google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,59 @@ public void testUpdateTable() {
591591
assertThat(createdTable.delete()).isTrue();
592592
}
593593

594+
@Test
595+
public void testUpdateTimePartitioning() {
596+
String tableName = "testUpdateTimePartitioning";
597+
TableId tableId = TableId.of(DATASET, tableName);
598+
StandardTableDefinition tableDefinition =
599+
StandardTableDefinition.newBuilder()
600+
.setSchema(TABLE_SCHEMA)
601+
.setTimePartitioning(TimePartitioning.of(Type.DAY))
602+
.build();
603+
604+
Table table = bigquery.create(TableInfo.of(tableId, tableDefinition));
605+
assertThat(table.getDefinition()).isInstanceOf(StandardTableDefinition.class);
606+
assertThat(
607+
((StandardTableDefinition) table.getDefinition())
608+
.getTimePartitioning()
609+
.getExpirationMs())
610+
.isNull();
611+
612+
table =
613+
table
614+
.toBuilder()
615+
.setDefinition(
616+
tableDefinition
617+
.toBuilder()
618+
.setTimePartitioning(TimePartitioning.of(Type.DAY, 42L))
619+
.build())
620+
.build()
621+
.update(BigQuery.TableOption.fields(BigQuery.TableField.TIME_PARTITIONING));
622+
assertThat(
623+
((StandardTableDefinition) table.getDefinition())
624+
.getTimePartitioning()
625+
.getExpirationMs())
626+
.isEqualTo(42L);
627+
628+
table =
629+
table
630+
.toBuilder()
631+
.setDefinition(
632+
tableDefinition
633+
.toBuilder()
634+
.setTimePartitioning(TimePartitioning.of(Type.DAY))
635+
.build())
636+
.build()
637+
.update(BigQuery.TableOption.fields(BigQuery.TableField.TIME_PARTITIONING));
638+
assertThat(
639+
((StandardTableDefinition) table.getDefinition())
640+
.getTimePartitioning()
641+
.getExpirationMs())
642+
.isNull();
643+
644+
table.delete();
645+
}
646+
594647
@Test
595648
public void testUpdateTableWithSelectedFields() {
596649
String tableName = "test_update_with_selected_fields_table";

0 commit comments

Comments
 (0)