Skip to content

Conversation

@kasiafi
Copy link
Member

@kasiafi kasiafi commented Jan 16, 2023

COPARTITION is a keyword specific to table functions. It is neither reserved nor non-reserved according to SQL spec of 2016. Before this change, COPARTITION was a non-reserved word, leading to a failure in a case like:

SELECT * FROM TABLE(system.test_inputs_function(
input_1 => TABLE(VALUES 1, 2, 3),
input_2 => TABLE(VALUES 1, 1, null, null, 2, 2) t2(x2) PARTITION BY x2,
input_3 => TABLE(VALUES null, 2, 2, 2, 3) t3(x3) PARTITION BY x3,
input_4 => TABLE(VALUES 4, 5)
COPARTITION (t2, t3)))

because the COPARTITION clause was parsed as a table alias.

Release notes

(X) This is not user-visible or docs only and no release notes are required.

Table arguments are not yet supported. But maybe we should document the reserved word?

( ) Release notes are required, please propose a release note for me.
( ) Release notes are required, with the following suggested text:

COPARTITION is a keyword specific to table functions.
It is neither reserved nor non-reserved according to SQL spec of 2016.
Before this change, COPARTITION  was a non-reserved word, leading to
a failure in a case like:

SELECT * FROM TABLE(system.test_inputs_function(
                                input_1 => TABLE(VALUES 1, 2, 3),
                                input_2 => TABLE(VALUES 1, 1, null, null, 2, 2) t2(x2) PARTITION BY x2,
                                input_3 => TABLE(VALUES null, 2, 2, 2, 3) t3(x3) PARTITION BY x3,
                                input_4 => TABLE(VALUES 4, 5)
                                COPARTITION (t2, t3)))

because the COPARTITION clause was parsed as a table alias.
@cla-bot cla-bot bot added the cla-signed label Jan 16, 2023
@github-actions github-actions bot added the docs label Jan 16, 2023
@kasiafi kasiafi requested a review from martint January 16, 2023 19:12
@electrum
Copy link
Member

Is this an error in the SQL specification? Or is it possible to change the parsing so that it is only reserved in table function contexts? This would be the first instance of a reserved keyword in Trino that isn't reserved in the SQL specification.

@kasiafi
Copy link
Member Author

kasiafi commented Jan 17, 2023

@electrum I tried to find the information about COPARTITION being reserved or not, but couldn't find any. In SQL specification of 2016, COPARTITION is not listed as a <key word>. I don't know if they ever make corrections to the spec. If they do, I'd like to check the current version.

It is likely though that COPARTITION is not supposed to be a key word. If so, we should just let it parse as the table alias, and close this PR. It might be a little inconvenient to users, but it can be easily worked around by changing the order of arguments.

cc @martint

@kasiafi
Copy link
Member Author

kasiafi commented Feb 7, 2023

I'm closing this in favor of #16014

@kasiafi kasiafi closed this Feb 7, 2023
aditi-pandit pushed a commit to prestodb/presto that referenced this pull request Oct 23, 2025
Final SQL Parsing changes for TVF. Includes changes for unaliasing as
well as a change to prevent COPARTITION ambiguity.

Please check trinodb/trino#15734 for more
details.

Contains all changes under
`presto-parser/src/main/java/com/facebook/presto/sql`

New complete PR:
#26188

## Description
<!---Describe your changes in detail-->
Changes adapted from trino/PR#16014, 14175

## Motivation and Context
<!---Why is this change required? What problem does it solve?-->
<!---If it fixes an open issue, please link to the issue here.-->
The keyword COPARTITION is specific to table function invocation. It is
not a reserved keyword. In some cases, the word COPARTITION can be
ambiguously interpreted: either as table argument alias, or as part of
the COPARTITION clause.
To solve the ambiguity, we decided to fail queries using the word
"copartition" as table argument alias, while keeping the word
non-reserved and available to be used as identifier in other contexts.


## Test Plan
<!---Please fill in how you tested your change-->
testCopartitionInTableArgumentAlias in TestSqlParser.java

Tests ran to check for regressions:
TestSqlParser
TestSqlParserErrorHandling
TestAnalyzer
TestTableFunctionInvocation
TestTableFunctionRegistry

```
== NO RELEASE NOTE ==
```

Co-authored-by: kasiafi <[email protected]>
Co-authored-by: Xin Zhang <[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.

3 participants