Skip to content

Commit

Permalink
SnowflakeIO: do not quote empty fields (#33803)
Browse files Browse the repository at this point in the history
* SnowflakeIO: do not quote empty fields (fails with some types like integer)

* fixup! SnowflakeIO: do not quote empty fields (fails with some types like integer)

* fixup! SnowflakeIO: do not quote empty fields (fails with some types like integer)
  • Loading branch information
turb authored Jan 31, 2025
1 parent ebd8898 commit 2360711
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ public void processElement(ProcessContext context) {
if (o instanceof String) {
String field = (String) o;
field = field.replace("'", "''");
field = quoteField(field);
field = quoteNonEmptyField(field);

csvItems.add(field);
} else {
Expand All @@ -1187,12 +1187,18 @@ public void processElement(ProcessContext context) {
context.output(Joiner.on(",").useForNull("").join(csvItems));
}

private String quoteField(String field) {
return quoteField(field, this.quotationMark);
private String quoteNonEmptyField(String field) {
return quoteNonEmptyField(field, this.quotationMark);
}

private String quoteField(String field, String quotation) {
return String.format("%s%s%s", quotation, field, quotation);
private String quoteNonEmptyField(String field, String quotation) {
String quoted;
if (field.isEmpty()) {
quoted = field;
} else {
quoted = String.format("%s%s%s", quotation, field, quotation);
}
return quoted;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public static void setupAll() {
testDataInStrings.add("Third row with \"double\" quotation");
testDataInStrings.add("Third row with double one \" quotation");
testDataInStrings.add("Third row with double twice \"\" quotation");
testDataInStrings.add("");
}

@Before
Expand Down Expand Up @@ -208,7 +209,7 @@ public void writeToExternalWithDoubleQuotation() throws SnowflakeSQLException {
List<String> escapedTestData =
testDataInStrings.stream()
.map(e -> e.replace("'", "''"))
.map(e -> String.format("\"%s\"", e))
.map(e -> e.isEmpty() ? "" : String.format("\"%s\"", e))
.collect(Collectors.toList());
assertTrue(TestUtils.areListsEqual(escapedTestData, actualData));
}
Expand Down

0 comments on commit 2360711

Please sign in to comment.