Skip to content

Commit 728f283

Browse files
authored
Merge pull request #492 from AdaptiveScale/cherrypick-release/0.15/PLUGIN-409
CherryPick for release/0.15
2 parents 151cd16 + 6fc0f54 commit 728f283

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/main/java/io/cdap/plugin/gcp/bigquery/sink/AbstractBigQuerySink.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,14 +333,14 @@ private Schema getTableSchema(
333333
return BigQueryUtil.getTableSchema(bqSchema, collector);
334334
}
335335

336-
protected void validateInsertSchema(Table table, Schema tableSchema, FailureCollector collector) {
336+
protected void validateInsertSchema(Table table, @Nullable Schema tableSchema, FailureCollector collector) {
337337
com.google.cloud.bigquery.Schema bqSchema = table.getDefinition().getSchema();
338338
if (bqSchema == null || bqSchema.getFields().isEmpty()) {
339339
// Table is created without schema, so no further validation is required.
340340
return;
341341
}
342342

343-
if (getConfig().isTruncateTableSet()) {
343+
if (getConfig().isTruncateTableSet() || tableSchema == null) {
344344
//no validation required for schema if truncate table is set.
345345
// BQ will overwrite the schema for normal tables when write disposition is WRITE_TRUNCATE
346346
//note - If write to single partition is supported in future, schema validation will be necessary
@@ -389,10 +389,10 @@ protected void validateInsertSchema(Table table, Schema tableSchema, FailureColl
389389
protected void validateSchema(
390390
String tableName,
391391
com.google.cloud.bigquery.Schema bqSchema,
392-
Schema tableSchema,
392+
@Nullable Schema tableSchema,
393393
boolean allowSchemaRelaxation,
394394
FailureCollector collector) {
395-
if (bqSchema == null || bqSchema.getFields().isEmpty()) {
395+
if (bqSchema == null || bqSchema.getFields().isEmpty() || tableSchema == null) {
396396
// Table is created without schema, so no further validation is required.
397397
return;
398398
}

src/test/java/io/cdap/plugin/gcp/bigquery/sink/BigQuerySinkTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,24 @@ public void testSchemaValidationAllowSchemaRelaxationNoTruncateException() throw
230230
Assert.assertEquals(0, collector.getValidationFailures().size());
231231
}
232232

233+
@Test
234+
public void testSchemaValidationNullInputSchema() throws NoSuchFieldException {
235+
BigQuerySink sink = getValidationTestSink(false);
236+
MockFailureCollector collector = new MockFailureCollector("bqsink");
237+
Table table = getTestSchema();
238+
sink.validateSchema(
239+
table.getTableId().getTable(),
240+
table.getDefinition().getSchema(),
241+
null,
242+
true,
243+
collector);
244+
sink.validateInsertSchema(
245+
table,
246+
null,
247+
collector);
248+
Assert.assertEquals(0, collector.getValidationFailures().size());
249+
}
250+
233251
private Table getTestSchema() {
234252
Table table = mock(Table.class);
235253
TableId tableId = TableId.of("test", "testds", "testtab");

0 commit comments

Comments
 (0)