Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show original characters for string literals instead of escaping #18256

Merged
merged 2 commits into from
Aug 10, 2023
Merged
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 @@ -13,7 +13,6 @@
*/
package io.trino.sql;

import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import io.trino.sql.tree.AllColumns;
Expand Down Expand Up @@ -112,10 +111,8 @@
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.PrimitiveIterator;
import java.util.function.Function;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Iterables.getOnlyElement;
import static io.trino.sql.ReservedIdentifiers.reserved;
Expand Down Expand Up @@ -1053,35 +1050,7 @@ private String visitListagg(FunctionCall node)

static String formatStringLiteral(String s)
{
s = s.replace("'", "''");
if (CharMatcher.inRange((char) 0x20, (char) 0x7E).matchesAllOf(s)) {
return "'" + s + "'";
}

StringBuilder builder = new StringBuilder();
builder.append("U&'");
PrimitiveIterator.OfInt iterator = s.codePoints().iterator();
while (iterator.hasNext()) {
int codePoint = iterator.nextInt();
checkArgument(codePoint >= 0, "Invalid UTF-8 encoding in characters: %s", s);
if (isAsciiPrintable(codePoint)) {
char ch = (char) codePoint;
if (ch == '\\') {
builder.append(ch);
}
builder.append(ch);
}
else if (codePoint <= 0xFFFF) {
builder.append('\\');
builder.append(format("%04X", codePoint));
}
else {
builder.append("\\+");
builder.append(format("%06X", codePoint));
}
}
builder.append("'");
return builder.toString();
return "'" + s.replace("'", "''") + "'";
}

public static String formatOrderBy(OrderBy orderBy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
*/
package io.trino.sql;

import io.trino.sql.tree.CharLiteral;
import io.trino.sql.tree.Expression;
import io.trino.sql.tree.GenericLiteral;
import io.trino.sql.tree.IntervalLiteral;
import io.trino.sql.tree.StringLiteral;
import org.junit.jupiter.api.Test;

import java.util.Optional;
Expand All @@ -28,6 +31,48 @@

public class TestExpressionFormatter
{
@Test
public void testStringLiteral()
{
assertFormattedExpression(
new StringLiteral("test"),
"'test'");
assertFormattedExpression(
new StringLiteral("攻殻機動隊"),
"'攻殻機動隊'");
assertFormattedExpression(
new StringLiteral("😂"),
"'😂'");
}

@Test
public void testCharLiteral()
{
assertFormattedExpression(
new CharLiteral("test"),
"CHAR 'test'");
assertFormattedExpression(
new CharLiteral("攻殻機動隊"),
"CHAR '攻殻機動隊'");
assertFormattedExpression(
new CharLiteral("😂"),
"CHAR '😂'");
}

@Test
public void testGenericLiteral()
{
assertFormattedExpression(
new GenericLiteral("VARCHAR", "test"),
"VARCHAR 'test'");
assertFormattedExpression(
new GenericLiteral("VARCHAR", "攻殻機動隊"),
"VARCHAR '攻殻機動隊'");
assertFormattedExpression(
new GenericLiteral("VARCHAR", "😂"),
"VARCHAR '😂'");
}

@Test
public void testIntervalLiteral()
{
Expand Down
Loading