Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@

package com.google.cloud.bigquery;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.MoreObjects.firstNonNull;

import com.google.api.client.util.Data;
import com.google.api.core.BetaApi;
import com.google.auto.value.AutoValue;
import com.google.common.base.MoreObjects;

import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.Objects;
import javax.annotation.Nullable;

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

/**
* Returns the number of milliseconds for which to keep the storage for a partition. When expired,
* the storage for the partition is reclaimed.
* the storage for the partition is reclaimed. If null, the partion does not expire.
*/
@Nullable
public abstract Long getExpirationMs();
Expand Down Expand Up @@ -115,7 +113,7 @@ public static Builder newBuilder(Type type) {
/**
* Returns a {@code TimePartitioning} object given the time partitioning type. Currently, the only
* type supported is {@link Type#DAY}, which will generate one partition per day based on data
* loading time.
* loading time. The partitions will not expire.
*/
public static TimePartitioning of(Type type) {
return newBuilder(type).build();
Expand All @@ -137,18 +135,22 @@ com.google.api.services.bigquery.model.TimePartitioning toPb() {
com.google.api.services.bigquery.model.TimePartitioning partitioningPb =
new com.google.api.services.bigquery.model.TimePartitioning();
partitioningPb.setType(getType().name());
partitioningPb.setExpirationMs(getExpirationMs());
partitioningPb.setExpirationMs(firstNonNull(getExpirationMs(), Data.NULL_LONG));
partitioningPb.setRequirePartitionFilter(getRequirePartitionFilter());
partitioningPb.setField(getField());
return partitioningPb;
}

static TimePartitioning fromPb(
com.google.api.services.bigquery.model.TimePartitioning partitioningPb) {
Long expirationMs = partitioningPb.getExpirationMs();
if (Data.isNull(expirationMs)) {
expirationMs = null;
}
return newBuilder(Type.valueOf(partitioningPb.getType()))
.setExpirationMs(partitioningPb.getExpirationMs())
.setField(partitioningPb.getField())
.setRequirePartitionFilter(partitioningPb.getRequirePartitionFilter())
.build();
.setExpirationMs(expirationMs)
.setField(partitioningPb.getField())
.setRequirePartitionFilter(partitioningPb.getRequirePartitionFilter())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,59 @@ public void testUpdateTable() {
assertThat(createdTable.delete()).isTrue();
}

@Test
public void testUpdateTimePartitioning() {
String tableName = "testUpdateTimePartitioning";
TableId tableId = TableId.of(DATASET, tableName);
StandardTableDefinition tableDefinition =
StandardTableDefinition.newBuilder()
.setSchema(TABLE_SCHEMA)
.setTimePartitioning(TimePartitioning.of(Type.DAY))
.build();

Table table = bigquery.create(TableInfo.of(tableId, tableDefinition));
assertThat(table.getDefinition()).isInstanceOf(StandardTableDefinition.class);
assertThat(
((StandardTableDefinition) table.getDefinition())
.getTimePartitioning()
.getExpirationMs())
.isNull();

table =
table
.toBuilder()
.setDefinition(
tableDefinition
.toBuilder()
.setTimePartitioning(TimePartitioning.of(Type.DAY, 42L))
.build())
.build()
.update(BigQuery.TableOption.fields(BigQuery.TableField.TIME_PARTITIONING));
assertThat(
((StandardTableDefinition) table.getDefinition())
.getTimePartitioning()
.getExpirationMs())
.isEqualTo(42L);

table =
table
.toBuilder()
.setDefinition(
tableDefinition
.toBuilder()
.setTimePartitioning(TimePartitioning.of(Type.DAY))
.build())
.build()
.update(BigQuery.TableOption.fields(BigQuery.TableField.TIME_PARTITIONING));
assertThat(
((StandardTableDefinition) table.getDefinition())
.getTimePartitioning()
.getExpirationMs())
.isNull();

table.delete();
}

@Test
public void testUpdateTableWithSelectedFields() {
String tableName = "test_update_with_selected_fields_table";
Expand Down