Skip to content
Permalink
Browse files
feat: add hourly partitioning support (#336)
Fixes b/154849483
  • Loading branch information
stephaniewang526 committed May 11, 2020
1 parent 9cb5ba3 commit 90f998040bbca5882ac3dbcdb4b157f26f48eb01
@@ -37,23 +37,23 @@ public abstract class TimePartitioning implements Serializable {
private static final long serialVersionUID = -8565064035346940951L;

/**
* The type of time partitioning. Currently, the only type supported is {@code DAY}, which will
* generate one partition per day based on data loading time.
* The supported types are DAY, which will generate one partition per day, and HOUR, which will
* generate one partition per hour. (Providing an empty string used to cause an error, but in
* OnePlatform the field will be treated as unset.)
*/
public enum Type {

/** Table is partitioned per day, based on data loading time. */
DAY
DAY,
/** Table is partitioned per hour, based on data loading time. */
HOUR
}

TimePartitioning() {
// Users cannot extend this, but AutoValue can.
}

/**
* Returns 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.
*/
/** Returns the time partitioning type. */
public abstract Type getType();

/**
@@ -96,19 +96,14 @@ public abstract static class Builder {
public abstract TimePartitioning build();
}

/**
* 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.
*/
/** Returns a {@code TimePartitioning} object given the time partitioning type. */
public static Builder newBuilder(Type type) {
return new AutoValue_TimePartitioning.Builder().setType(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. The partitions will not expire.
* Returns a {@code TimePartitioning} object given the time partitioning type. The partitions will
* not expire.
*/
public static TimePartitioning of(Type type) {
return newBuilder(type).build();
@@ -118,8 +113,7 @@ public static TimePartitioning of(Type type) {
* Returns a {@code TimePartitioning} object given the time partitioning type and the partition's
* expiration in milliseconds.
*
* @param type 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.
* @param type the time partitioning type.
* @param expirationMs the number of milliseconds for which to keep the storage for a partition
*/
public static TimePartitioning of(Type type, long expirationMs) {
@@ -26,37 +26,45 @@

public class TimePartitioningTest {

private static final Type TYPE = Type.DAY;
private static final Type TYPE_DAY = Type.DAY;
private static final Type TYPE_HOUR = Type.HOUR;
private static final long EXPIRATION_MS = 42;
private static final boolean REQUIRE_PARTITION_FILTER = false;
private static final String FIELD = "field";
private static final TimePartitioning TIME_PARTITIONING =
TimePartitioning.newBuilder(TYPE)
private static final TimePartitioning TIME_PARTITIONING_DAY =
TimePartitioning.newBuilder(TYPE_DAY)
.setExpirationMs(EXPIRATION_MS)
.setRequirePartitionFilter(REQUIRE_PARTITION_FILTER)
.setField(FIELD)
.build();
private static final TimePartitioning TIME_PARTITIONING_HOUR =
TimePartitioning.newBuilder(TYPE_HOUR)
.setExpirationMs(EXPIRATION_MS)
.setRequirePartitionFilter(REQUIRE_PARTITION_FILTER)
.setField(FIELD)
.build();

@Test
public void testOf() {
assertEquals(TYPE, TIME_PARTITIONING.getType());
assertEquals(EXPIRATION_MS, TIME_PARTITIONING.getExpirationMs().longValue());
assertEquals(REQUIRE_PARTITION_FILTER, TIME_PARTITIONING.getRequirePartitionFilter());
assertEquals(FIELD, TIME_PARTITIONING.getField());
TimePartitioning partitioning = TimePartitioning.of(TYPE);
assertEquals(TYPE, partitioning.getType());
assertEquals(TYPE_DAY, TIME_PARTITIONING_DAY.getType());
assertEquals(TYPE_HOUR, TIME_PARTITIONING_HOUR.getType());
assertEquals(EXPIRATION_MS, TIME_PARTITIONING_DAY.getExpirationMs().longValue());
assertEquals(REQUIRE_PARTITION_FILTER, TIME_PARTITIONING_DAY.getRequirePartitionFilter());
assertEquals(FIELD, TIME_PARTITIONING_DAY.getField());
TimePartitioning partitioning = TimePartitioning.of(TYPE_DAY);
assertEquals(TYPE_DAY, partitioning.getType());
assertNull(partitioning.getExpirationMs());
}

@Test
public void testBuilder() {
TimePartitioning partitioning = TimePartitioning.newBuilder(TYPE).build();
assertEquals(TYPE, partitioning.getType());
TimePartitioning partitioning = TimePartitioning.newBuilder(TYPE_DAY).build();
assertEquals(TYPE_DAY, partitioning.getType());
assertNull(partitioning.getExpirationMs());
assertNull(partitioning.getRequirePartitionFilter());
assertNull(partitioning.getField());
partitioning = TimePartitioning.newBuilder(TYPE).setExpirationMs(100L).build();
assertEquals(TYPE, partitioning.getType());
partitioning = TimePartitioning.newBuilder(TYPE_DAY).setExpirationMs(100L).build();
assertEquals(TYPE_DAY, partitioning.getType());
assertEquals(100, (long) partitioning.getExpirationMs());
assertNull(partitioning.getRequirePartitionFilter());
assertNull(partitioning.getField());
@@ -84,8 +92,9 @@ public void testTypeAndExpirationOf_Npe() {

@Test
public void testToAndFromPb() {
compareTimePartitioning(TIME_PARTITIONING, TimePartitioning.fromPb(TIME_PARTITIONING.toPb()));
TimePartitioning partitioning = TimePartitioning.of(TYPE);
compareTimePartitioning(
TIME_PARTITIONING_DAY, TimePartitioning.fromPb(TIME_PARTITIONING_DAY.toPb()));
TimePartitioning partitioning = TimePartitioning.of(TYPE_DAY);
compareTimePartitioning(partitioning, TimePartitioning.fromPb(partitioning.toPb()));
}

0 comments on commit 90f9980

Please sign in to comment.