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

Preserve quotedness for Identifier when formatting Query #80

Merged
merged 1 commit into from
Feb 4, 2019

Conversation

Praveen2112
Copy link
Member

Presto doesn't maintain the quotedness of an identifier when using SqlQueryFormatter so it results in throwing parsing error when we run prepare query of the syntax [https://github.com/prestodb/presto/issues/10739]. This PR solves that above issue

@cla-bot cla-bot bot added the cla-signed label Jan 26, 2019
@martint martint self-assigned this Jan 28, 2019
@@ -76,7 +76,7 @@ public Identifier getField()
*/
public static QualifiedName getQualifiedName(DereferenceExpression expression)
{
List<String> parts = tryParseParts(expression.base, expression.field.getValue().toLowerCase(Locale.ENGLISH));
List<Identifier> parts = tryParseParts(expression.base, expression.field.getValue().toLowerCase(Locale.ENGLISH));
Copy link
Member

Choose a reason for hiding this comment

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

It shouldn't be necessary to call toLowerCase(). The DereferenceExpression already deals with Identifiers

Copy link
Member

Choose a reason for hiding this comment

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

In fact, I don't think the tryParseParts method needs to exist anymore in its current form.

@@ -1844,7 +1845,7 @@ public void testShowStats()
final String[] tableNames = {"t", "s.t", "c.s.t"};

for (String fullName : tableNames) {
QualifiedName qualifiedName = QualifiedName.of(Arrays.asList(fullName.split("\\.")));
QualifiedName qualifiedName = QualifiedName.of(Arrays.asList(fullName.split("\\.")).stream().map(Identifier::new).collect(Collectors.toList()));
Copy link
Member

Choose a reason for hiding this comment

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

This is a little hard to read. I'd pull it out into a makeQualifiedName. Also, I'd structure it as:

List<Identifier> parts = Arrays.stream(fullName.split("\\."))
        .map(Identifier::new)
        .collect(Collectors.toList());

return QualifiedName.of(parts);

@@ -1855,7 +1856,7 @@ public void testShowStatsForQuery()
final String[] tableNames = {"t", "s.t", "c.s.t"};

for (String fullName : tableNames) {
QualifiedName qualifiedName = QualifiedName.of(Arrays.asList(fullName.split("\\.")));
QualifiedName qualifiedName = QualifiedName.of(Arrays.asList(fullName.split("\\.")).stream().map(Identifier::new).collect(Collectors.toList()));
Copy link
Member

Choose a reason for hiding this comment

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

Same here

@@ -734,7 +736,7 @@ public VerifierConfig setShadowWrites(boolean shadowWrites)

public QualifiedName getShadowTestTablePrefix()
{
return QualifiedName.of(Splitter.on(".").splitToList(shadowTestTablePrefix));
return QualifiedName.of(Splitter.on(".").splitToList(shadowTestTablePrefix).stream().map(Identifier::new).collect(Collectors.toList()));
Copy link
Member

Choose a reason for hiding this comment

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

This can be changed to Arrays.stream(fullName.split("\\.")).xxx like the calls in TestSqlParser and formatted across multiple lines to make it more readable.

@@ -747,7 +749,7 @@ public VerifierConfig setShadowTestTablePrefix(String prefix)

public QualifiedName getShadowControlTablePrefix()
{
return QualifiedName.of(Splitter.on(".").splitToList(shadowControlTablePrefix));
return QualifiedName.of(Splitter.on(".").splitToList(shadowControlTablePrefix).stream().map(Identifier::new).collect(Collectors.toList()));
Copy link
Member

Choose a reason for hiding this comment

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

Same here

import static io.prestosql.sql.SqlFormatterUtil.getFormattedSql;
import static org.testng.Assert.assertEquals;

public class TestSqlFormatterUtil
Copy link
Member

Choose a reason for hiding this comment

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

Call this TestSqlFormatter

@@ -373,7 +374,7 @@ protected Node visitShowCreate(ShowCreate node, Void context)
}

Query query = parseView(viewDefinition.get().getOriginalSql(), objectName, node);
String sql = formatSql(new CreateView(createQualifiedName(objectName), query, false, Optional.empty()), Optional.of(parameters)).trim();
String sql = formatSql(new CreateView(createQualifiedName(objectName, MetadataUtil.getDelimitedList(node.getName())), query, false, Optional.empty()), Optional.of(parameters)).trim();
Copy link
Member

Choose a reason for hiding this comment

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

This seems unnecessary. It looks like you could just do: new CreateView(node.getName(), ...) instead of trying to recreate the QualifiedName from the QualifiedObjectName that was derived from node.getName().

If you do that, you should also be able to remove MetadataUtil.getDelimitedList(), which I'm not fond of.

@martint martint assigned Praveen2112 and unassigned martint Jan 30, 2019
Copy link
Member

@martint martint left a comment

Choose a reason for hiding this comment

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

Looks good. Can you rebase and fix the merge conflicts? I'll merge it once that's taken care of.

@Praveen2112
Copy link
Member Author

@martint I have updated the code and resolved the conflicts.

@martint
Copy link
Member

martint commented Feb 3, 2019

@Praveen2112, there are some build failures. Can you take a look?

We maintain Identifier in QualifiedName for preserving its quotedness.
@martint martint merged commit d5dfc99 into trinodb:master Feb 4, 2019
@martint martint mentioned this pull request Feb 4, 2019
6 tasks
v-jizhang added a commit to v-jizhang/presto that referenced this pull request Feb 22, 2022
Cherry-pick of trinodb/trino#80

Presto doesn't maintain the quotedness of an identifier when
using SqlQueryFormatter so it results in throwing parsing error
when we run prepare query of the syntax
[prestodb#10739].
This PR solves that above issue.

Co-authored-by: praveenkrishna <[email protected]>
zhenxiao pushed a commit to prestodb/presto that referenced this pull request Feb 24, 2022
Cherry-pick of trinodb/trino#80

Presto doesn't maintain the quotedness of an identifier when
using SqlQueryFormatter so it results in throwing parsing error
when we run prepare query of the syntax
[#10739].
This PR solves that above issue.

Co-authored-by: praveenkrishna <[email protected]>
imjalpreet added a commit to imjalpreet/presto that referenced this pull request Jun 8, 2023
Cherry-pick of trinodb/trino#80

Presto doesn't maintain the quotedness of an identifier when
using SqlQueryFormatter so it results in throwing parsing error
if quoted table name is a reserved word
Co-authored-by: praveenkrishna <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants