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

Quote potentially ambiguous symbol references in EXPLAIN #21223

Merged
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,6 +13,7 @@
*/
package io.trino.sql.ir;

import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import io.trino.sql.planner.Symbol;
Expand All @@ -26,6 +27,13 @@

public final class ExpressionFormatter
{
private static final CharMatcher UNAMBIGUOUS_REFERENCE_NAME_CHARACTERS =
CharMatcher.inRange('a', 'z')
.or(CharMatcher.inRange('A', 'Z'))
.or(CharMatcher.inRange('0', '9'))
.or(CharMatcher.anyOf("_$"))
.precomputed();

private ExpressionFormatter() {}

public static String formatExpression(Expression expression)
Expand Down Expand Up @@ -105,7 +113,11 @@ protected String visitReference(Reference node, Void context)
if (symbolReferenceFormatter.isPresent()) {
return symbolReferenceFormatter.get().apply(node);
}
return node.name();
String name = node.name();
if (UNAMBIGUOUS_REFERENCE_NAME_CHARACTERS.matchesAllOf(name)) {
return name;
}
return "\"" + name.replace("\"", "\"\"") + "\"";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.sql.ir;

import org.junit.jupiter.api.Test;

import static io.trino.spi.type.BigintType.BIGINT;
import static org.assertj.core.api.Assertions.assertThat;

public class TestExpressionFormatter
{
@Test
public void testReference()
{
assertFormattedExpression(
new Reference(BIGINT, "abc"),
"abc");
assertFormattedExpression(
new Reference(BIGINT, "with a space"),
"\"with a space\"");
assertFormattedExpression(
new Reference(BIGINT, "with \" quote, $ dollar and ' apostrophe"),
"\"with \"\" quote, $ dollar and ' apostrophe\"");
}

private void assertFormattedExpression(Expression expression, String expected)
{
assertThat(ExpressionFormatter.formatExpression(expression)).as("formatted expression")
.isEqualTo(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import io.trino.sql.tree.Expression;
import io.trino.sql.tree.GenericLiteral;
import io.trino.sql.tree.Identifier;
import io.trino.sql.tree.IntervalLiteral;
import io.trino.sql.tree.StringLiteral;
import org.junit.jupiter.api.Test;
Expand All @@ -30,6 +31,20 @@

public class TestExpressionFormatter
{
@Test
public void testIdentifier()
{
assertFormattedExpression(
new Identifier("abc"),
"abc");
assertFormattedExpression(
new Identifier("with a space"),
"\"with a space\"");
assertFormattedExpression(
new Identifier("with \" quote, $ dollar and ' apostrophe"),
"\"with \"\" quote, $ dollar and ' apostrophe\"");
}

@Test
public void testStringLiteral()
{
Expand Down