-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add support for PostgreSQL :: style casts
#25259
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
base: master
Are you sure you want to change the base?
Conversation
|
See #23795 (comment) |
@martint that sounds great. It means that we have no concern that something useful will be used for this. I don't see us ever implementing that part of the spec, so we can use this operator for something awesome. |
On the contrary. It's something I can envision supporting. E.g., you could do There are a few options for handling ambiguities:
|
|
What about syntax |
|
The ambiguity issue seems unlikely in practice, as you wouldn't declare a function with the same name as a type, except in the case where the function is an alias for a cast. For example, we support The PostgreSQL cast syntax is well-understood by users and is quite convenient, so I vote to support it. |
|
We can resolve the ambiguity by allowing it when the function is also a cast (as in the case of
|
|
The current behavior with literals is ambiguous to me. For instance, I expected the following statement returns timestamp type at a glance, but it returns varchar type. SELECT TIMESTAMP '2007-08-09 9:10:11 Europe/Berlin'::VARCHAR; |
I don't think that is ambigous. The first part of the expression is Type constructors do not support an expression after the type name, and only support a literal string. Then the second part casts the timestamp to a varchar. |
|
It is the same as if you wrote it with parentheses: SELECT (TIMESTAMP '2007-08-09 9:10:11 Europe/Berlin')::VARCHAR;The syntax for type constructors is special because they only support literals, not expressions. But I agree in this instance that is appears confusing, as the cast operator goes left-to-right instead of the typical inside-out for function calls. Visually, it's also confusing because the type constructor has a space inside of it, but there's no separation between the string literal and the cast operator. It's good to call out this edge case in the SQL syntax, but I don't have a good solution, other than mentioning it in the documentation. PostgreSQL should have the same issue. |
|
I expect people won't mix the syntax, and instead would do this: So the original expression would be: |
|
This pull request has gone a while without any activity. Ask for help on #core-dev on Trino slack. |
|
Closing this pull request, as it has been stale for six weeks. Feel free to re-open at any time. |
|
This pull request has gone a while without any activity. Ask for help on #core-dev on Trino slack. |
| LTE: '<='; | ||
| GT: '>'; | ||
| GTE: '>='; | ||
| DOUBLE_COLON: '::'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This token declaration isn't needed, as we don't compare the token value in the generated code.
|
@martint do we have a way forward for this change? I agree with what David said about ambiguity and casts. This syntax makes a lot of sense from usability perspective |
|
I'm generally for supporting this, but I think there are some cases we're overlooking in the analysis. First, here's a concrete example of where the semantic ambiguity arises. As long as we acknowledge that at some point there will be an ambiguity and we'll have to fail those queries, I'm ok. The The other cases we haven considered are those involving more complex types. In particular, how the syntax works for composite types such as array, map, row whose syntax is incompatible with expression syntax, so they can't be parsed using the same rules and can't use the same AST data structures. For example:
|
|
Do any of these ambiguities exist if we disallow functions with the same names as types, unless they are equivalent to casts?
What would be usefulness of having a no-arg
This would be a type named I understand that this could complicate parsing and analysis, but I can't imagine a scenario where it would be semantically ambiguous or otherwise cause a problem. |
|
This pull request has gone a while without any activity. Ask for help on #core-dev on Trino slack. |
|
Closing this pull request, as it has been stale for six weeks. Feel free to re-open at any time. |
Description
This PR proposes adding PostgreSQL style double colon
::casts to Trino. I find the standard SQL castCAST(something as BIGINT)is quite wordy and difficult to write compared to::especially when it is necessary to drop casts into existing queries (due the the strict nature of SQL type system). I find being able to drop a simple::typeon a symbol reference or function call to be super easy in PostgreSQL.Fixes #23795
Release notes
(X) Release notes are required, with the following suggested text: