Skip to content

Commit 83335f7

Browse files
mikekappongad
authored andcommitted
---
yaml --- r: 9307 b: refs/heads/master c: cc1c6df h: refs/heads/master i: 9305: 1de43f5 9303: 2154887
1 parent 2ce06c3 commit 83335f7

3 files changed

Lines changed: 85 additions & 37 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 167ef273c2554d712e29d0e2f0c7873db9a7d84b
2+
refs/heads/master: cc1c6dfc9a3449edf019e31ab718713db81ddd39
33
refs/heads/travis: 47e4fee4fd5af9b2a8ce46f23c72ec95f9b195b2
44
refs/heads/gh-pages: 1e525ea89a8c2133b947a8f1e3306079d006152d
55
refs/tags/0.0.9: 22f1839238f66c39e67ed4dfdcd273b1ae2e8444

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

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

21+
import com.google.api.core.BetaApi;
22+
import com.google.auto.value.AutoValue;
2123
import com.google.common.base.MoreObjects;
2224

25+
import javax.annotation.Nullable;
2326
import java.io.Serializable;
2427
import java.util.Objects;
2528

@@ -30,13 +33,11 @@
3033
*
3134
* @see <a href="https://cloud.google.com/bigquery/docs/partitioned-tables">Partitioned Tables</a>
3235
*/
33-
public final class TimePartitioning implements Serializable {
36+
@AutoValue
37+
public abstract class TimePartitioning implements Serializable {
3438

3539
private static final long serialVersionUID = -8565064035346940951L;
3640

37-
private final Type type;
38-
private final Long expirationMs;
39-
4041
/**
4142
* The type of time partitioning. Currently, the only type supported is {@code DAY}, which will
4243
* generate one partition per day based on data loading time.
@@ -49,47 +50,66 @@ public enum Type {
4950
DAY
5051
}
5152

52-
private TimePartitioning(Type type, Long expirationMs) {
53-
this.type = checkNotNull(type);
54-
this.expirationMs = expirationMs;
53+
TimePartitioning() {
54+
// Users cannot extend this, but AutoValue can.
5555
}
5656

57-
5857
/**
5958
* Returns the time partitioning type. Currently, the only type supported is {@link Type#DAY},
6059
* which will generate one partition per day based on data loading time.
6160
*/
62-
public Type getType() {
63-
return type;
64-
}
61+
public abstract Type getType();
6562

6663

6764
/**
6865
* Returns the number of milliseconds for which to keep the storage for a partition. When expired,
6966
* the storage for the partition is reclaimed.
7067
*/
71-
public Long getExpirationMs() {
72-
return expirationMs;
73-
}
68+
@Nullable
69+
public abstract Long getExpirationMs();
7470

75-
@Override
76-
public String toString() {
77-
return MoreObjects.toStringHelper(this)
78-
.add("type", type)
79-
.add("expirationMs", expirationMs)
80-
.toString();
81-
}
8271

83-
@Override
84-
public int hashCode() {
85-
return Objects.hash(type, expirationMs);
72+
/**
73+
* If not set, the table is partitioned by pseudo column '_PARTITIONTIME'; if set, the table is
74+
* partitioned by this field.
75+
*/
76+
@BetaApi
77+
@Nullable
78+
public abstract String getField();
79+
80+
81+
/**
82+
* If set to true, queries over this table require a partition filter (that can be used for
83+
* partition elimination) to be specified.
84+
*/
85+
@BetaApi
86+
@Nullable
87+
public abstract Boolean getRequirePartitionFilter();
88+
89+
public abstract Builder toBuilder();
90+
91+
@AutoValue.Builder
92+
public abstract static class Builder {
93+
abstract Builder setType(Type type);
94+
95+
public abstract Builder setExpirationMs(Long expirationMs);
96+
97+
@BetaApi
98+
public abstract Builder setRequirePartitionFilter(Boolean requirePartitionFilter);
99+
100+
@BetaApi
101+
public abstract Builder setField(String field);
102+
103+
public abstract TimePartitioning build();
86104
}
87105

88-
@Override
89-
public boolean equals(Object obj) {
90-
return obj == this
91-
|| obj instanceof TimePartitioning
92-
&& Objects.equals(toPb(), ((TimePartitioning) obj).toPb());
106+
/**
107+
* Returns a {@code TimePartitioning} object given the time partitioning type. Currently, the only
108+
* type supported is {@link Type#DAY}, which will generate one partition per day based on data
109+
* loading time.
110+
*/
111+
public static Builder newBuilder(Type type) {
112+
return new AutoValue_TimePartitioning.Builder().setType(type);
93113
}
94114

95115
/**
@@ -98,7 +118,7 @@ public boolean equals(Object obj) {
98118
* loading time.
99119
*/
100120
public static TimePartitioning of(Type type) {
101-
return new TimePartitioning(type, null);
121+
return newBuilder(type).build();
102122
}
103123

104124
/**
@@ -110,20 +130,25 @@ public static TimePartitioning of(Type type) {
110130
* @param expirationMs the number of milliseconds for which to keep the storage for a partition
111131
*/
112132
public static TimePartitioning of(Type type, long expirationMs) {
113-
return new TimePartitioning(type, expirationMs);
133+
return newBuilder(type).setExpirationMs(expirationMs).build();
114134
}
115135

116136
com.google.api.services.bigquery.model.TimePartitioning toPb() {
117137
com.google.api.services.bigquery.model.TimePartitioning partitioningPb =
118138
new com.google.api.services.bigquery.model.TimePartitioning();
119-
partitioningPb.setType(type.name());
120-
partitioningPb.setExpirationMs(expirationMs);
139+
partitioningPb.setType(getType().name());
140+
partitioningPb.setExpirationMs(getExpirationMs());
141+
partitioningPb.setRequirePartitionFilter(getRequirePartitionFilter());
142+
partitioningPb.setField(getField());
121143
return partitioningPb;
122144
}
123145

124146
static TimePartitioning fromPb(
125147
com.google.api.services.bigquery.model.TimePartitioning partitioningPb) {
126-
return new TimePartitioning(
127-
Type.valueOf(partitioningPb.getType()), partitioningPb.getExpirationMs());
148+
return newBuilder(Type.valueOf(partitioningPb.getType()))
149+
.setExpirationMs(partitioningPb.getExpirationMs())
150+
.setField(partitioningPb.getField())
151+
.setRequirePartitionFilter(partitioningPb.getRequirePartitionFilter())
152+
.build();
128153
}
129154
}

trunk/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimePartitioningTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@ public class TimePartitioningTest {
2929

3030
private static final Type TYPE = Type.DAY;
3131
private static final long EXPIRATION_MS = 42;
32+
private static final boolean REQUIRE_PARTITION_FILTER = false;
33+
private static final String FIELD = "field";
3234
private static final TimePartitioning TIME_PARTITIONING =
33-
TimePartitioning.of(TYPE, EXPIRATION_MS);
35+
TimePartitioning.newBuilder(TYPE)
36+
.setExpirationMs(EXPIRATION_MS)
37+
.setRequirePartitionFilter(REQUIRE_PARTITION_FILTER)
38+
.setField(FIELD)
39+
.build();
3440

3541
@Rule
3642
public ExpectedException thrown = ExpectedException.none();
@@ -39,11 +45,26 @@ public class TimePartitioningTest {
3945
public void testOf() {
4046
assertEquals(TYPE, TIME_PARTITIONING.getType());
4147
assertEquals(EXPIRATION_MS, TIME_PARTITIONING.getExpirationMs().longValue());
48+
assertEquals(REQUIRE_PARTITION_FILTER, TIME_PARTITIONING.getRequirePartitionFilter());
49+
assertEquals(FIELD, TIME_PARTITIONING.getField());
4250
TimePartitioning partitioning = TimePartitioning.of(TYPE);
4351
assertEquals(TYPE, partitioning.getType());
4452
assertNull(partitioning.getExpirationMs());
4553
}
4654

55+
@Test
56+
public void testBuilder() {
57+
TimePartitioning partitioning = TimePartitioning.newBuilder(TYPE).build();
58+
assertEquals(TYPE, partitioning.getType());
59+
assertNull(partitioning.getExpirationMs());
60+
assertNull(partitioning.getRequirePartitionFilter());
61+
assertNull(partitioning.getField());
62+
partitioning = TimePartitioning.newBuilder(TYPE).setExpirationMs(100L).build();
63+
assertEquals(TYPE, partitioning.getType());
64+
assertEquals(100, (long) partitioning.getExpirationMs());
65+
assertNull(partitioning.getRequirePartitionFilter());
66+
assertNull(partitioning.getField());
67+
}
4768

4869
@Test
4970
public void testTypeOf_Npe() {
@@ -68,6 +89,8 @@ private void compareTimePartitioning(TimePartitioning expected, TimePartitioning
6889
assertEquals(expected, value);
6990
assertEquals(expected.getType(), value.getType());
7091
assertEquals(expected.getExpirationMs(), value.getExpirationMs());
92+
assertEquals(expected.getRequirePartitionFilter(), value.getRequirePartitionFilter());
93+
assertEquals(expected.getField(), value.getField());
7194
assertEquals(expected.hashCode(), value.hashCode());
7295
assertEquals(expected.toString(), value.toString());
7396
}

0 commit comments

Comments
 (0)