Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ protected String visitTableVersion(TableVersionExpression node, Void context)
static String formatStringLiteral(String s)
{
s = s.replace("'", "''");
if (CharMatcher.inRange((char) 0x20, (char) 0x7E).matchesAllOf(s)) {
if (CharMatcher.inRange((char) 0x20, (char) 0x7E).or(CharMatcher.anyOf("\n\r\t")).matchesAllOf(s)) {
return "'" + s + "'";
}

Expand All @@ -720,7 +720,7 @@ static String formatStringLiteral(String s)
while (iterator.hasNext()) {
int codePoint = iterator.nextInt();
checkArgument(codePoint >= 0, "Invalid UTF-8 encoding in characters: %s", s);
if (isAsciiPrintable(codePoint)) {
if (isAsciiPrintable(codePoint) || isCommonWhitespace(codePoint)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Check for surrogate pairs and non-BMP code points.

Casting codePoint to char does not support non-BMP Unicode characters. Please handle code points above U+FFFF to prevent data loss or formatting errors.

char ch = (char) codePoint;
if (ch == '\\') {
builder.append(ch);
Expand Down Expand Up @@ -797,6 +797,11 @@ private static boolean isAsciiPrintable(int codePoint)
return true;
}

private static boolean isCommonWhitespace(int codePoint)
{
return codePoint == '\n' || codePoint == '\r' || codePoint == '\t';
}

private static String formatGroupingSet(List<Expression> groupingSet, Optional<List<Expression>> parameters)
{
return format("(%s)", Joiner.on(", ").join(groupingSet.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ public void testStringFormatter()
assertSqlFormatter("U&'!+10FFFF!6d4B!8Bd5ABC!6d4B!8Bd5' UESCAPE '!'", "U&'\\+10FFFF\\6D4B\\8BD5ABC\\6D4B\\8BD5'");
assertSqlFormatter("U&'\\+10FFFF\\6D4B\\8BD5\\0041\\0042\\0043\\6D4B\\8BD5'", "U&'\\+10FFFF\\6D4B\\8BD5ABC\\6D4B\\8BD5'");
assertSqlFormatter("U&'\\\\abc\\6D4B'''", "U&'\\\\abc\\6D4B'''");

assertSqlFormatter("'line1\nline2'", "'line1\nline2'");
assertSqlFormatter("'line1\tcolumn2'", "'line1\tcolumn2'");
assertSqlFormatter("'line1\r\nline2'", "'line1\r\nline2'");
assertSqlFormatter("'def hello():\n return ''world'''", "'def hello():\n return ''world'''");
}

@Test
Expand Down
Loading