Skip to content

Commit 5cfdf85

Browse files
authored
fix: load jobs preserve ascii control characters configuration (#3876)
* fix: load jobs preserve ascii control characters configuration * fix lint * fix NPE * fix NPE
1 parent 0e971b8 commit 5cfdf85

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

‎google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LoadJobConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ private Builder(com.google.api.services.bigquery.model.JobConfiguration configur
219219
this.nullMarker = loadConfigurationPb.getNullMarker();
220220
}
221221
if (loadConfigurationPb.getAllowJaggedRows() != null
222+
|| loadConfigurationPb.getPreserveAsciiControlCharacters() != null
222223
|| loadConfigurationPb.getAllowQuotedNewlines() != null
223224
|| loadConfigurationPb.getEncoding() != null
224225
|| loadConfigurationPb.getFieldDelimiter() != null
@@ -229,6 +230,10 @@ private Builder(com.google.api.services.bigquery.model.JobConfiguration configur
229230
.setEncoding(loadConfigurationPb.getEncoding())
230231
.setFieldDelimiter(loadConfigurationPb.getFieldDelimiter())
231232
.setQuote(loadConfigurationPb.getQuote());
233+
if (loadConfigurationPb.getPreserveAsciiControlCharacters() != null) {
234+
builder.setPreserveAsciiControlCharacters(
235+
loadConfigurationPb.getPreserveAsciiControlCharacters());
236+
}
232237
if (loadConfigurationPb.getAllowJaggedRows() != null) {
233238
builder.setAllowJaggedRows(loadConfigurationPb.getAllowJaggedRows());
234239
}
@@ -907,6 +912,7 @@ com.google.api.services.bigquery.model.JobConfiguration toPb() {
907912
.setAllowJaggedRows(csvOptions.allowJaggedRows())
908913
.setAllowQuotedNewlines(csvOptions.allowQuotedNewLines())
909914
.setEncoding(csvOptions.getEncoding())
915+
.setPreserveAsciiControlCharacters(csvOptions.getPreserveAsciiControlCharacters())
910916
.setQuote(csvOptions.getQuote());
911917
if (csvOptions.getSkipLeadingRows() != null) {
912918
// todo(mziccard) remove checked cast or comment when #1044 is closed

‎google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/LoadJobConfigurationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class LoadJobConfigurationTest {
3838
.setAllowJaggedRows(true)
3939
.setAllowQuotedNewLines(false)
4040
.setEncoding(StandardCharsets.UTF_8)
41+
.setPreserveAsciiControlCharacters(true)
4142
.build();
4243
private static final TableId TABLE_ID = TableId.of("dataset", "table");
4344
private static final CreateDisposition CREATE_DISPOSITION = CreateDisposition.CREATE_IF_NEEDED;

‎google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ public class ITBigQueryTest {
613613
private static final String LOAD_FILE_LARGE = "load_large.csv";
614614

615615
private static final String LOAD_FILE_FLEXIBLE_COLUMN_NAME = "load_flexible_column_name.csv";
616+
private static final String LOAD_FILE_NULL = "load_null.csv";
616617
private static final String JSON_LOAD_FILE = "load.json";
617618
private static final String JSON_LOAD_FILE_BQ_RESULTSET = "load_bq_resultset.json";
618619
private static final String JSON_LOAD_FILE_SIMPLE = "load_simple.json";
@@ -628,6 +629,7 @@ public class ITBigQueryTest {
628629
private static final TableId TABLE_ID_FASTQUERY_BQ_RESULTSET =
629630
TableId.of(DATASET, "fastquery_testing_bq_resultset");
630631
private static final String CSV_CONTENT = "StringValue1\nStringValue2\n";
632+
private static final String CSV_CONTENT_NULL = "String\0Value1\n";
631633
private static final String CSV_CONTENT_FLEXIBLE_COLUMN = "name,&ampersand\nrow_name,1";
632634

633635
private static final String JSON_CONTENT =
@@ -1080,6 +1082,9 @@ public static void beforeClass() throws InterruptedException, IOException {
10801082
storage.create(
10811083
BlobInfo.newBuilder(BUCKET, LOAD_FILE).setContentType("text/plain").build(),
10821084
CSV_CONTENT.getBytes(StandardCharsets.UTF_8));
1085+
storage.create(
1086+
BlobInfo.newBuilder(BUCKET, LOAD_FILE_NULL).setContentType("text/plain").build(),
1087+
CSV_CONTENT_NULL.getBytes(StandardCharsets.UTF_8));
10831088
storage.create(
10841089
BlobInfo.newBuilder(BUCKET, LOAD_FILE_FLEXIBLE_COLUMN_NAME)
10851090
.setContentType("text/plain")
@@ -6600,9 +6605,9 @@ public void testLocation() throws Exception {
66006605
}
66016606

66026607
@Test
6603-
public void testPreserveAsciiControlCharacters()
6608+
public void testWriteChannelPreserveAsciiControlCharacters()
66046609
throws InterruptedException, IOException, TimeoutException {
6605-
String destinationTableName = "test_preserve_ascii_control_characters";
6610+
String destinationTableName = "test_write_channel_preserve_ascii_control_characters";
66066611
TableId tableId = TableId.of(DATASET, destinationTableName);
66076612
WriteChannelConfiguration configuration =
66086613
WriteChannelConfiguration.newBuilder(tableId)
@@ -6625,6 +6630,26 @@ public void testPreserveAsciiControlCharacters()
66256630
assertTrue(bigquery.delete(tableId));
66266631
}
66276632

6633+
@Test
6634+
public void testLoadJobPreserveAsciiControlCharacters() throws InterruptedException {
6635+
String destinationTableName = "test_load_job_preserve_ascii_control_characters";
6636+
TableId destinationTable = TableId.of(DATASET, destinationTableName);
6637+
6638+
try {
6639+
LoadJobConfiguration configuration =
6640+
LoadJobConfiguration.newBuilder(destinationTable, "gs://" + BUCKET + "/" + LOAD_FILE_NULL)
6641+
.setFormatOptions(
6642+
CsvOptions.newBuilder().setPreserveAsciiControlCharacters(true).build())
6643+
.setSchema(SIMPLE_SCHEMA)
6644+
.build();
6645+
Job remoteLoadJob = bigquery.create(JobInfo.of(configuration));
6646+
remoteLoadJob = remoteLoadJob.waitFor();
6647+
assertNull(remoteLoadJob.getStatus().getError());
6648+
} finally {
6649+
assertTrue(bigquery.delete(destinationTable));
6650+
}
6651+
}
6652+
66286653
@Test
66296654
public void testReferenceFileSchemaUriForAvro() {
66306655
try {

0 commit comments

Comments
 (0)