Skip to content

Commit

Permalink
[Manual backport] Fix: CSV and Raw output, escape quotes (#3148)
Browse files Browse the repository at this point in the history
* Improve error handling for some more edge cases (#3080)

* Add failing tests

Signed-off-by: Simeon Widdis <[email protected]>

* Fix the first test

Signed-off-by: Simeon Widdis <[email protected]>

* Revise the tests

Signed-off-by: Simeon Widdis <[email protected]>

* Fix wildcard tests

Signed-off-by: Simeon Widdis <[email protected]>

* Add license header

Signed-off-by: Simeon Widdis <[email protected]>

* Fix rerunning SQL parsing

Signed-off-by: Simeon Widdis <[email protected]>

---------

Signed-off-by: Simeon Widdis <[email protected]>

* Fix: CSV and Raw output, escape quotes (#3063)

Fixes #3050

Signed-off-by: Mike Swierczek <[email protected]>
(cherry picked from commit cfe38d7)
Signed-off-by: Simeon Widdis <[email protected]>

* Fix merge conflict

Signed-off-by: Simeon Widdis <[email protected]>

---------

Signed-off-by: Simeon Widdis <[email protected]>
Signed-off-by: Mike Swierczek <[email protected]>
Signed-off-by: Simeon Widdis <[email protected]>
Co-authored-by: Mike Swierczek <[email protected]>
  • Loading branch information
Swiddis and Michael-S authored Nov 14, 2024
1 parent 1be7b30 commit ce6086b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ protected List<List<String>> formatData(List<List<String>> lines) {

protected String quoteIfRequired(String separator, String cell) {
final String quote = "\"";
return cell.contains(separator) ? quote + cell.replaceAll("\"", "\"\"") + quote : cell;
if (cell != null && (cell.contains(separator) || cell.contains(quote))) {
return quote + cell.replaceAll(quote, quote + quote) + quote;
} else {
return cell;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,12 @@ void quoteIfRequired() {
QueryResult response =
new QueryResult(
schema,
Arrays.asList(tupleValue(ImmutableMap.of("na,me", "John,Smith", ",,age", "30,,,"))));
String expected = "\"na,me\",\",,age\"%n" + "\"John,Smith\",\"30,,,\"";
assertEquals(format(expected), formatter.format(response));
Arrays.asList(
tupleValue(ImmutableMap.of("na,me", "John,Smith", ",,age", "30,,,")),
tupleValue(ImmutableMap.of("na,me", "\"Janice Jones", ",,age", "26\""))));
String expected =
"\"na,me\",\",,age\"%n\"John,Smith\",\"30,,,\"%n\"\"\"Janice Jones\",\"26\"\"\"";
assertEquals(format(expected), formatter.format(response));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,18 @@ void quoteIfRequired() {
QueryResult response =
new QueryResult(
schema,
Arrays.asList(tupleValue(ImmutableMap.of("na|me", "John|Smith", "||age", "30|||"))));
String expected = "\"na|me\"|\"||age\"%n" + "\"John|Smith\"|\"30|||\"";
Arrays.asList(
tupleValue(ImmutableMap.of("na|me", "John|Smith", "||age", "30|||")),
tupleValue(ImmutableMap.of("na|me", "Ja\"ne J\"ones", "||age", "\"40\""))));
String expected =
"\"na|me\"|\"||age\"%n"
+ "\"John|Smith\"|\"30|||\"%n"
+ "\"Ja\"\"ne J\"\"ones\"|\"\"\"40\"\"\"";
assertEquals(format(expected), getRawFormatter().format(response));
String expectedPretty = "\"na|me\" |\"||age\"%n" + "\"John|Smith\"|\"30|||\"";
String expectedPretty =
"\"na|me\" |\"||age\" %n"
+ "\"John|Smith\" |\"30|||\" %n"
+ "\"Ja\"\"ne J\"\"ones\"|\"\"\"40\"\"\"";
assertEquals(format(expectedPretty), getRawFormatterPretty().format(response));
}

Expand Down

0 comments on commit ce6086b

Please sign in to comment.