Preserve quotedness in QualifiedName#19827
Conversation
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 <praveenkrishna@tutanota.com>
|
|
@agrawalreetika Added the tests as mentioned in #19752 |
|
@prestodb/committers Hi, can someone help with the review for this PR? |
rschlussel
left a comment
There was a problem hiding this comment.
Last time we had this change, it needed to be reverted do to a bug in creating the qualified name for views. Can you check that the issue here is resolved and add a test #17446
| private QualifiedName getQualifiedName(ShowCreate node, QualifiedObjectName objectName) | ||
| { | ||
| List<Identifier> parts = node.getName().getOriginalParts(); | ||
| Identifier tableName = (parts.size() > 2) ? parts.get(2) : ((parts.size() > 1) ? parts.get(1) : parts.get(0)); |
There was a problem hiding this comment.
can simplify to Identifier tableName = parts.get(parts.size() -1)
| { | ||
| List<Identifier> parts = node.getName().getOriginalParts(); | ||
| Identifier tableName = (parts.size() > 2) ? parts.get(2) : ((parts.size() > 1) ? parts.get(1) : parts.get(0)); | ||
| Identifier schemaName = (parts.size() > 2) ? parts.get(1) : ((parts.size() > 1) ? parts.get(0) : new Identifier(objectName.getSchemaName())); |
There was a problem hiding this comment.
Identifier schemaName = parts.size() > 1 ? : parts.get(parts.size() - 2) : new Identifier(objectName.getSchemaName())
|
@rschlussel I just had a look at the PR you mentioned above and I have already fixed the issue seen due to which it was reverted. I have already added some tests to test this PR in AbstractTestDistributedQueries#testViewWithReservedKeywords. Let me know if you meant writing a test for generating qualified name for a view. |
Thanks, I see now you've fixed it. I think we need the test to also run SHOW CREATE VIEW on the view that it created to exercise the codepath you fixed. |
| assetQuery("SELECT id\nFROM\n \"public\".\"order\"\"2\"\n"); | ||
| } | ||
|
|
||
| private void assetQuery(String query) |
There was a problem hiding this comment.
typo: asset -> assert, but maybe give this function a more descriptive name so you don't have to read the code for this function to understand what the test is doing, e.g. assertFormattedEqualsOriginal
| @@ -76,7 +74,20 @@ public Identifier getField() | |||
| */ | |||
| public static QualifiedName getQualifiedName(DereferenceExpression expression) | |||
There was a problem hiding this comment.
I know you aren't changing the signature here, but in Presto we usually avoid returning nulls anywhere, and prefer explicitly using optionals if something might not be there. Looks like there are a bunch of usages of this function that aren't checking for null return values. Would be nice as a separate commit to switch to using optional instead of returning null to be sure this is used correctly throughout.
| { | ||
| assetQuery("SELECT id\nFROM\n public.orders\n"); | ||
| assetQuery("SELECT id\nFROM\n \"public\".\"order\"\n"); | ||
| assetQuery("SELECT id\nFROM\n \"public\".\"order\"\"2\"\n"); |
There was a problem hiding this comment.
what kind of format is this last test. looks like
SELECT id FROM "public"."order""2"?
Can you add a test for a three part schema?
Also can you add a test where only one or two parts of the qualified name are quoted
|
This is already covered as part of #25528 |
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
Test plan
Before this PR, presto throws an error when using a reserved keyword in a prepared statement or while creating a view.
After this PR, queries runs successfully.