-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Add support for PostgreSQL ::
style casts
#25259
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 the 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:
|
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::type
on 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: