From fa8f2c3151b603b0bbf4cae0da47aeb63064cd61 Mon Sep 17 00:00:00 2001 From: Sergey Melnychuk Date: Thu, 24 Feb 2022 11:37:44 +0100 Subject: [PATCH] Make sure that quoted identifiers remain quoted in formatter The side-effect of this change is that formatted statements for SetSession and ResetSession are no longer lowercased as before. However QualifiedName instances are resolved and compared case-insensitive, thus such side-effect does not lead to any consequences. --- .../src/main/java/io/trino/sql/SqlFormatter.java | 4 ++-- .../java/io/trino/sql/parser/TestSqlParser.java | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/core/trino-parser/src/main/java/io/trino/sql/SqlFormatter.java b/core/trino-parser/src/main/java/io/trino/sql/SqlFormatter.java index 9739346d85ea..f4271da6fcc0 100644 --- a/core/trino-parser/src/main/java/io/trino/sql/SqlFormatter.java +++ b/core/trino-parser/src/main/java/io/trino/sql/SqlFormatter.java @@ -1511,7 +1511,7 @@ protected Void visitTruncateTable(TruncateTable node, Integer indent) public Void visitSetSession(SetSession node, Integer indent) { builder.append("SET SESSION ") - .append(node.getName()) + .append(formatName(node.getName())) .append(" = ") .append(formatExpression(node.getValue())); @@ -1522,7 +1522,7 @@ public Void visitSetSession(SetSession node, Integer indent) public Void visitResetSession(ResetSession node, Integer indent) { builder.append("RESET SESSION ") - .append(node.getName()); + .append(formatName(node.getName())); return null; } diff --git a/core/trino-parser/src/test/java/io/trino/sql/parser/TestSqlParser.java b/core/trino-parser/src/test/java/io/trino/sql/parser/TestSqlParser.java index 945bd69b53ee..ede1d4762209 100644 --- a/core/trino-parser/src/test/java/io/trino/sql/parser/TestSqlParser.java +++ b/core/trino-parser/src/test/java/io/trino/sql/parser/TestSqlParser.java @@ -1009,6 +1009,20 @@ public void testResetSession() assertStatement("RESET SESSION foo", new ResetSession(QualifiedName.of("foo"))); } + @Test + public void testSessionIdentifiers() + { + assertStatement("SET SESSION \"foo-bar\".baz = 'x'", + new SetSession(QualifiedName.of("foo-bar", "baz"), new StringLiteral("x"))); + assertStatementIsInvalid("SET SESSION foo-bar.name = 'value'") + .withMessage("line 1:16: mismatched input '-'. Expecting: '.', '='"); + + assertStatement("RESET SESSION \"foo-bar\".baz", + new ResetSession(QualifiedName.of("foo-bar", "baz"))); + assertStatementIsInvalid("RESET SESSION foo-bar.name") + .withMessage("line 1:18: mismatched input '-'. Expecting: '.', "); + } + @Test public void testShowSession() {