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 #17186

Merged
merged 2 commits into from
Feb 24, 2022

Conversation

v-jizhang
Copy link
Contributor

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
[https://github.com//issues/10739]. This PR
solves that above issue.

Co-authored-by: praveenkrishna [email protected]

Test plan - Added tests

== RELEASE NOTES ==

General Changes
* Preserve quotedness for Identifier when formatting Query

Comment on lines 462 to 465
Identifier tableName = parts.get(0);
Identifier schemaName = (parts.size() > 1) ? parts.get(1) : new Identifier(objectName.getSchemaName());
Identifier catalogName = (parts.size() > 2) ? parts.get(2) : new Identifier(objectName.getCatalogName());
String sql = formatSql(new CreateView(QualifiedName.of(ImmutableList.of(catalogName, schemaName, tableName)), query, false, Optional.empty()), Optional.of(parameters)).trim();
Copy link
Contributor

Choose a reason for hiding this comment

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

This logic can be moved into some methods and reused.
i.e. getTableName, getSchemaName, getCatalogName

@v-jizhang
Copy link
Contributor Author

The failed test is unrelated.

Copy link
Contributor

@swapsmagic swapsmagic left a comment

Choose a reason for hiding this comment

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

one minor comment, rest looks good.

@@ -161,7 +161,7 @@ public void testPreparedStatementsSpecialCharacters()
.put(PRESTO_LANGUAGE, "zh-TW")
.put(PRESTO_TIME_ZONE, "Asia/Taipei")
.put(PRESTO_CLIENT_INFO, "null")
.put(PRESTO_PREPARED_STATEMENT, "query1=select * from tbl:ns")
.put(PRESTO_PREPARED_STATEMENT, "query1=select * from \"tbl:ns\"")
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the reason for having this query with delimiter? If it's needed, we should have it generated using the parser so if in future we change the delimiter, this test won't break.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please the commit message here

@swapsmagic
Copy link
Contributor

Also please get the failed test pass by re-running the tests.

@rubenssoto
Copy link

Is there any chance of being merged in the next version? Very important fix for us.

Thank you!

@jbapple
Copy link
Contributor

jbapple commented Feb 22, 2022

Looks like this is missing one followup from @v-jizhang

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]>
Cherry-pick of trinodb/trino#6380:

Identifiers with characters such as @ or : are not being treated as
delimited identifiers, which causes issues when the planner creates
synthetic identifiers as part of the plan IR expressions. When the
IR expressions are serialized, they are not being properly quoted,
which causes failures when parsing the plan on the work side.

For example, for a query such as:

    SELECT try("a@b") FROM t

The planner creates an expression of the form:

    "$internal$try"("$INTERNAL$BIND"("a@b", (a@b_0) -> "a@b_0"))

The argument to the lambda expression is not properly quoted.

Co-authored-by: Martin Traverso <[email protected]>
@v-jizhang
Copy link
Contributor Author

@jbapple All tests passed.

Copy link
Collaborator

@zhenxiao zhenxiao 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

@zhenxiao zhenxiao merged commit d370d62 into prestodb:master Feb 24, 2022
v-jizhang added a commit to v-jizhang/presto that referenced this pull request Mar 4, 2022
Cherry-pick of trinodb/trino#11171
The SqlFormatter effectively drops quotes from the quoted identifiers
when formatting 'SET SESSION' statement.
This causes valid queries to fail: e.g. SET SESSION
"name-suffix".property = 'value' PrestoException: Formatted query does
not parse even when identifier (name-suffix) is quoted.

Follow up prestodb#17186

Co-authored-by: Sergey Melnychuk <[email protected]>
@varungajjala varungajjala mentioned this pull request Mar 22, 2022
9 tasks
@asjadsyed asjadsyed mentioned this pull request Mar 23, 2022
9 tasks
@asjadsyed asjadsyed mentioned this pull request Apr 1, 2022
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants