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

Make sure that quoted identifiers remain quoted in formatter #11171

Merged
merged 1 commit into from Mar 1, 2022

Conversation

sergey-melnychuk
Copy link

The SqlFormatter effectively drops quotes from the quoted identifiers when formatting 'SET SESSION' statement (https://github.com/trinodb/trino/blob/master/core/trino-parser/src/main/java/io/trino/sql/SqlFormatter.java#L1514).

This causes valid queries to fail: e.g. SET SESSION "name-suffix".property = 'value' throws io.trino.spi.TrinoException: Formatted query does not parse even when identifier (name-suffix) is quoted.

@cla-bot cla-bot bot added the cla-signed label Feb 23, 2022
Copy link
Member

@kasiafi kasiafi left a comment

Choose a reason for hiding this comment

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

SqlFormatter has a dedicated method for formatting QualifiedName:

static String formatName(QualifiedName name)
It should be used to format SetSession's name (and some other QualifiedNamess, e.g. in ResetSession). That should fix the issue.

Please note that neither of the formatting solutions respects the proper identifier semantics. However, the method I suggest is closer and easier to fix, since it delegates the formatting to Identifier.

@sergey-melnychuk
Copy link
Author

Thank you, @kasiafi , the SqlFormatter.formatName is exactly what I was looking for.

@sergey-melnychuk sergey-melnychuk marked this pull request as ready for review February 24, 2022 10:39
@sergey-melnychuk sergey-melnychuk force-pushed the sm/fix-identifier-formatting branch 2 times, most recently from 78b793f to c6e3fb1 Compare February 24, 2022 11:54
Copy link
Member

@kasiafi kasiafi left a comment

Choose a reason for hiding this comment

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

As a side-effect, this change alters the output of SqlFormatter regarding QualifiedNames in SetSession and ResetSession: before they were lowercased; after -- they are not.
It has no practical meaning, because the affected QualifiedNames are compared and resolved case-insensitive. However, please add this info to the commit message.

Many other Nodes have fields of type QualifiedName, too:
AddColumn
Analyze
Call
Comment
CreateMaterializedView
CreateSchema
CreateTable
CreateTableAsSelect
CreateView
Deny
DropColumn
DropMaterializedView
DropSchema
DropTable
DropView
FunctionCall
Grant
LikeClause
RenameColumn
RenameMaterializedView
RenameSchema
RenameTable
RenameView
ResetSession
Revoke
SetAuthorizationStatement
SetProperties
SetSession
ShowColumns
ShowCreate
Table
TruncateTable

Some of those are formatted with the formatName method, and some use toString. Could you please file an issue to check and fix the other occurrences in SqlFormatter and ExpressionFormatter?

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.
@hashhar
Copy link
Member

hashhar commented Mar 1, 2022

CI hit failure uploading test report to GitHub. nothing actually failed.

@hashhar hashhar merged commit 38d7d5b into trinodb:master Mar 1, 2022
@hashhar hashhar mentioned this pull request Mar 1, 2022
@mosabua mosabua deleted the sm/fix-identifier-formatting branch March 1, 2022 17:32
@github-actions github-actions bot added this to the 372 milestone Mar 1, 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 <sergey.melnychuk@starburstdata.com>
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.

None yet

3 participants