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

Cast operator :: #23795

Open
findepi opened this issue Oct 15, 2024 · 4 comments · May be fixed by #25259
Open

Cast operator :: #23795

findepi opened this issue Oct 15, 2024 · 4 comments · May be fixed by #25259
Labels

Comments

@findepi
Copy link
Member

findepi commented Oct 15, 2024

Add support for x::type cast operator as an alternative syntax for CAST(x AS type).

  • this is pure awesomeness from SQL readability perspective
  • at this point it's very unlikely that future SQL spec will have something conflicting, given that all PostgreSQL derivates and e.g. Snowflake, DuckDB support x::type syntax
@findepi findepi added enhancement New feature or request syntax-needs-review labels Oct 15, 2024
@vVv-AA
Copy link

vVv-AA commented Oct 18, 2024

Hi @findepi

Would like to give this shot.
Does the implementation require anything other than adding the operator to grammar, and handling the change in AstBuilder? (other than tests and documentation).

@findepi
Copy link
Member Author

findepi commented Oct 18, 2024

@vVv-AA implementation-wise I think it's exactly as you said, but @martint may know better.

@martint
Copy link
Member

martint commented Oct 18, 2024

The SQL spec reserves double colon for static method invocations, so that's incompatible with this proposed cast syntax.

<static method invocation> ::= <path-resolved user-defined type name> <double colon> <method name> [ <SQL argument list> ]

For example, given: foo::bar, it could be interpreted as:

  • Cast the value of column foo to type bar
  • Invoke method bar on type `foo.

@vVv-AA
Copy link

vVv-AA commented Oct 19, 2024

@martint If I understood correctly, this would make the grammar ambiguous?

Since many dbs are not following the spec and having :: do casting instead, would it somehow be possible to let the behavior somehow be pushed down to DBs so their can handle it as per their own implementations?

The shorthand ::, ?:: for cast and try_cast helps a lot with readability, but if :: does not work, would it be okay to have some custom one in trino, say :::, ?::: for lack of creativity on my part?

vVv-AA added a commit to vVv-AA/trino that referenced this issue Oct 25, 2024
Add type-cast operator `::`
@martint martint linked a pull request Mar 9, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

3 participants