-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add support for PostgreSQL-style shorthand casts #27392
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
Open
martint
wants to merge
1
commit into
trinodb:master
Choose a base branch
from
martint:cast-shorthand
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
core/trino-main/src/test/java/io/trino/operator/scalar/TestStaticMethodCall.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.trino.operator.scalar; | ||
|
|
||
| import io.trino.spi.type.DoubleType; | ||
| import io.trino.spi.type.VarcharType; | ||
| import io.trino.sql.query.QueryAssertions; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.TestInstance; | ||
| import org.junit.jupiter.api.parallel.Execution; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; | ||
| import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT; | ||
|
|
||
| @TestInstance(PER_CLASS) | ||
| @Execution(CONCURRENT) | ||
| public class TestStaticMethodCall | ||
| { | ||
| private QueryAssertions assertions; | ||
|
|
||
| @BeforeAll | ||
| public void init() | ||
| { | ||
| assertions = new QueryAssertions(); | ||
| } | ||
|
|
||
| @AfterAll | ||
| public void teardown() | ||
| { | ||
| assertions.close(); | ||
| assertions = null; | ||
| } | ||
|
|
||
| @Test | ||
| void testPostgreSqlStyleCast() | ||
| { | ||
| assertThat(assertions.expression("1::double")) | ||
martint marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .hasType(DoubleType.DOUBLE) | ||
| .isEqualTo(1.0); | ||
|
|
||
| assertThat(assertions.expression("1::varchar")) | ||
| .hasType(VarcharType.VARCHAR) | ||
martint marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .isEqualTo("1"); | ||
|
|
||
| assertThatThrownBy(() -> assertions.expression("1::varchar(100)").evaluate()) | ||
| .hasMessage("line 1:13: Static method calls are not supported"); | ||
|
|
||
| assertThat(assertions.expression("(a + b)::double") | ||
| .binding("a", "1") | ||
| .binding("b", "2")) | ||
| .hasType(DoubleType.DOUBLE) | ||
| .isEqualTo(3.0); | ||
|
|
||
| assertThatThrownBy(() -> assertions.expression("1::decimal(3, 2)").evaluate()) | ||
| .hasMessage("line 1:13: Static method calls are not supported"); | ||
| } | ||
|
|
||
| @Test | ||
| void testCall() | ||
| { | ||
| assertThatThrownBy(() -> assertions.expression("1::double(2)").evaluate()) | ||
| .hasMessage("line 1:13: Static method calls are not supported"); | ||
|
|
||
| assertThatThrownBy(() -> assertions.expression("1::foo").evaluate()) | ||
| .hasMessage("line 1:13: Static method calls are not supported"); | ||
|
|
||
| assertThatThrownBy(() -> assertions.expression("integer::foo").evaluate()) | ||
| .hasMessage("line 1:19: Static method calls are not supported"); | ||
|
|
||
| assertThatThrownBy(() -> assertions.expression("integer::foo(1, 2)").evaluate()) | ||
| .hasMessage("line 1:19: Static method calls are not supported"); | ||
|
|
||
| assertThat(assertions.query("SELECT bigint::real FROM (VALUES 1) AS t(bigint)")) | ||
| .failure() | ||
| .hasMessage("line 1:14: Static method calls are not supported"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
use
typeon the rhs?AFAIR engines allow
a::timestamp with time zoneand likelya::double precisiontooThere 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.
Can’t use type, and hence the limitation I mentioned in the description. The standard SQL syntax requires this to be a function invocation, so it has to look like it.
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.
to me, "syntactically ambiguous with static method calls" means that
foo::bar(1)is ambiguous in a sense that it can be two different thingsfooto typebar(1)barinvocation on typefoowith parameters1it is surprising, but it seems to me that
foo::baris also ambiguous because argument braces are optionalnow,
foo::timestampalready departs from the specfoo::timestamp with time zonealso departs from the spec, but is consistent withfoo::timestamp. And is not ambiguous.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.
Exactly. That's a syntactic ambiguity that's easy to solve during analysis. We parse all occurrences of
::as static method calls and decide later if it was meant to be a call or a cast depending on what's on the left side of the expression. If it's a type-producing expression (currently, an identifier matching a type name), then we treat it as a static method call. Otherwise, we treat it as a cast.Right, but to be able to parse that we'd need to allow either
identifier ('(' (expression (',' expression)*)? ')')ortypeon the right side, which introduces all sorts of problems. See my comment below.Also, as an aside, note that there's another ambiguous usage (I mention it in the comment below, too) with:
In Trino, that could be interpreted either as
SELECT xxx::double AS precisionor as a cast todouble precision. PostgreSQL doesn't have that problem sincedoubleis not a valid type. Which goes to say, sometimes we can't just adopt a language feature from another database, since there are intricate interactions between all features that need to be taken into account.