Skip to content
Permalink
Browse files
fix: null type in timepartitioning frompb (#305)
  • Loading branch information
Praful Makani committed May 1, 2020
1 parent 050e708 commit 6315842dfa62a433a1ba507b861cf96a3d7cb03d
@@ -285,17 +285,6 @@ static StandardTableDefinition fromPb(Table tablePb) {
+ " in table "
+ tablePb.getTableReference().getTableId(),
e);
} catch (NullPointerException e) {
throw new NullPointerException(
"Null pointer - Got unexpected time partitioning "
+ tablePb.getTimePartitioning().toString()
+ " in project "
+ tablePb.getTableReference().getProjectId()
+ " in dataset "
+ tablePb.getTableReference().getDatasetId()
+ " in table "
+ tablePb.getTableReference().getTableId()
+ e.toString());
}
}
if (tablePb.getRangePartitioning() != null) {
@@ -142,7 +142,7 @@ static TimePartitioning fromPb(
if (Data.isNull(expirationMs)) {
expirationMs = null;
}
return newBuilder(Type.valueOf(partitioningPb.getType()))
return newBuilder(Type.valueOf(firstNonNull(partitioningPb.getType(), Type.DAY.name())))
.setExpirationMs(expirationMs)
.setField(partitioningPb.getField())
.setRequirePartitionFilter(partitioningPb.getRequirePartitionFilter())
@@ -116,11 +116,22 @@ public class BigQueryImplTest {
private static final Long TABLE_CREATION_TIME = 1546275600000L;
private static final TimePartitioning TIME_PARTITIONING =
TimePartitioning.of(TimePartitioning.Type.DAY, EXPIRATION_MS);
private static final com.google.api.services.bigquery.model.TimePartitioning PB_TIMEPARTITIONING =
new com.google.api.services.bigquery.model.TimePartitioning()
.setType(null)
.setField("timestampField");
private static final TimePartitioning TIME_PARTITIONING_NULL_TYPE =
TimePartitioning.fromPb(PB_TIMEPARTITIONING);
private static final StandardTableDefinition TABLE_DEFINITION_WITH_PARTITIONING =
StandardTableDefinition.newBuilder()
.setSchema(TABLE_SCHEMA)
.setTimePartitioning(TIME_PARTITIONING)
.build();
private static final StandardTableDefinition TABLE_DEFINITION_WITH_PARTITIONING_NULL_TYPE =
StandardTableDefinition.newBuilder()
.setSchema(TABLE_SCHEMA)
.setTimePartitioning(TIME_PARTITIONING_NULL_TYPE)
.build();
private static final RangePartitioning.Range RANGE =
RangePartitioning.Range.newBuilder().setStart(1L).setInterval(2L).setEnd(10L).build();
private static final RangePartitioning RANGE_PARTITIONING =
@@ -142,7 +153,10 @@ public class BigQueryImplTest {
TableInfo.newBuilder(TABLE_ID, TABLE_DEFINITION_WITH_PARTITIONING)
.setCreationTime(TABLE_CREATION_TIME)
.build();

private static final TableInfo TABLE_INFO_WITH_PARTITIONS_NULL_TYPE =
TableInfo.newBuilder(TABLE_ID, TABLE_DEFINITION_WITH_PARTITIONING_NULL_TYPE)
.setCreationTime(TABLE_CREATION_TIME)
.build();
private static final ModelId OTHER_MODEL_ID = ModelId.of(DATASET, OTHER_MODEL);
private static final ModelId MODEL_ID_WITH_PROJECT = ModelId.of(PROJECT, DATASET, MODEL);

@@ -912,6 +926,22 @@ public void testListTablesReturnedParameters() {
assertArrayEquals(tableList.toArray(), Iterables.toArray(page.getValues(), Table.class));
}

@Test
public void testListTablesReturnedParametersNullType() {
bigquery = options.getService();
ImmutableList<Table> tableList =
ImmutableList.of(
new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PARTITIONS_NULL_TYPE)));
Tuple<String, Iterable<com.google.api.services.bigquery.model.Table>> result =
Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION));
EasyMock.expect(bigqueryRpcMock.listTables(PROJECT, DATASET, TABLE_LIST_OPTIONS))
.andReturn(result);
EasyMock.replay(bigqueryRpcMock);
Page<Table> page = bigquery.listTables(DATASET, TABLE_LIST_PAGE_SIZE, TABLE_LIST_PAGE_TOKEN);
assertEquals(CURSOR, page.getNextPageToken());
assertArrayEquals(tableList.toArray(), Iterables.toArray(page.getValues(), Table.class));
}

@Test
public void testListTablesWithRangePartitioning() {
bigquery = options.getService();
@@ -152,34 +152,6 @@ public void testFromPbWithUnexpectedTimePartitioningTypeRaisesInvalidArgumentExc
fail("testFromPb illegal argument exception did not throw!");
}

@Test
public void testFromPbWithNullTimePartitioningTypeRaisesNullPointerException() {
Table invalidTable =
new Table()
.setType("TABLE")
.setTableReference(
new TableReference()
.setProjectId("NULL_PTR_TEST_PROJECT")
.setDatasetId("NULL_PTR_TEST_DATASET")
.setTableId("NULL_PTR_TEST_TABLE"))
.setTimePartitioning(
new com.google.api.services.bigquery.model.TimePartitioning().setType(null));
try {
StandardTableDefinition.fromPb(invalidTable);
} catch (NullPointerException ne) {
assertThat(
ne.getMessage(),
allOf(
containsString("Null pointer - Got unexpected time partitioning"),
containsString("null"),
containsString("NULL_PTR_TEST_PROJECT"),
containsString("NULL_PTR_TEST_DATASET"),
containsString("NULL_PTR_TEST_TABLE")));
return;
}
fail("testFromPb null pointer exception did not throw!");
}

@Test
public void testFromPbWithNullEstimatedRowsAndBytes() {
StandardTableDefinition.fromPb(

0 comments on commit 6315842

Please sign in to comment.