Translate IN predicate to connector expression#11396
Translate IN predicate to connector expression#11396wendigo wants to merge 5 commits intotrinodb:masterfrom
Conversation
core/trino-spi/src/main/java/io/trino/spi/expression/StandardFunctions.java
Outdated
Show resolved
Hide resolved
core/trino-spi/src/main/java/io/trino/spi/expression/StandardFunctions.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
I think we need to think about efficiency of the representation, since IN list can be long and can be processed multiple times. This is the same problem as with large Domains, and with same performance challenges. See 82e8f93 and cc @raunaqmorarka
Now, I am aware that InListExpression doesn't have an efficient representation, but it can be evolved to have one. With ConnectorExpressions, such an evolution would be an SPI breaking change, so I'd suggest we consider performance in the first version already.
cc @martint
core/trino-main/src/main/java/io/trino/sql/planner/ConnectorExpressionTranslator.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/sql/planner/ConnectorExpressionTranslator.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/sql/planner/ConnectorExpressionTranslator.java
Outdated
Show resolved
Hide resolved
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/expression/RewriteIn.java
Outdated
Show resolved
Hide resolved
f886581 to
b44ef48
Compare
core/trino-main/src/main/java/io/trino/sql/planner/ConnectorExpressionTranslator.java
Outdated
Show resolved
Hide resolved
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/expression/RewriteIn.java
Outdated
Show resolved
Hide resolved
findepi
left a comment
There was a problem hiding this comment.
"Translate IN predicate to connector expression" LGTM
core/trino-spi/src/main/java/io/trino/spi/expression/ConnectorExpressions.java
Outdated
Show resolved
Hide resolved
core/trino-spi/src/main/java/io/trino/spi/expression/StandardFunctions.java
Outdated
Show resolved
Hide resolved
core/trino-spi/src/main/java/io/trino/spi/expression/StandardFunctions.java
Outdated
Show resolved
Hide resolved
...lugin-toolkit/src/main/java/io/trino/plugin/base/expression/ConnectorExpressionPatterns.java
Outdated
Show resolved
Hide resolved
...lugin-toolkit/src/main/java/io/trino/plugin/base/expression/ConnectorExpressionPatterns.java
Outdated
Show resolved
Hide resolved
findepi
left a comment
There was a problem hiding this comment.
"Rewrite connector IN expression in PostgreSQL connector"
...lugin-toolkit/src/main/java/io/trino/plugin/base/expression/ConnectorExpressionPatterns.java
Outdated
Show resolved
Hide resolved
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/expression/RewriteIn.java
Outdated
Show resolved
Hide resolved
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/expression/RewriteIn.java
Outdated
Show resolved
Hide resolved
...n/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestPostgreSqlConnectorTest.java
Outdated
Show resolved
Hide resolved
b44ef48 to
d624b09
Compare
core/trino-main/src/main/java/io/trino/sql/planner/ConnectorExpressionTranslator.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/sql/planner/ConnectorExpressionTranslator.java
Outdated
Show resolved
Hide resolved
core/trino-spi/src/main/java/io/trino/spi/expression/ConnectorExpressions.java
Outdated
Show resolved
Hide resolved
plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestPostgreSqlClient.java
Outdated
Show resolved
Hide resolved
plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestPostgreSqlClient.java
Outdated
Show resolved
Hide resolved
d624b09 to
871d94c
Compare
plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestPostgreSqlClient.java
Outdated
Show resolved
Hide resolved
plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestPostgreSqlClient.java
Outdated
Show resolved
Hide resolved
871d94c to
9138284
Compare
|
@findepi PTAL |
01875b6 to
e629253
Compare
|
Rebased & resolved conflicts. PTAL @findepi |
|
@wendigo did the CI run? seems not. can you add empty / rebase? |
|
@findepi fixed, after merge checkstyle violation :( |
...bc/src/main/java/io/trino/plugin/jdbc/expression/JdbcConnectorExpressionRewriterBuilder.java
Outdated
Show resolved
Hide resolved
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/expression/RewriteIn.java
Outdated
Show resolved
Hide resolved
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/expression/RewriteIn.java
Outdated
Show resolved
Hide resolved
plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestPostgreSqlClient.java
Outdated
Show resolved
Hide resolved
...n/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestPostgreSqlConnectorTest.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/sql/planner/ExpressionInterpreter.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/sql/planner/ExpressionInterpreter.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/test/java/io/trino/sql/TestExpressionInterpreter.java
Outdated
Show resolved
Hide resolved
|
AFAIU the "Improve NULL handling in IN predicates optimizations" commit prevents some problems/regressions that would be introduced by previous commits. is it correct? |
dd7cbb3 to
a22f682
Compare
|
All tests are passing. Seems in a good shape for next round of the review @findepi |
ea916b7 to
dd51415
Compare
|
@findepi rebased and solved conflicts. please review |
findepi
left a comment
There was a problem hiding this comment.
Please help me understand the purpose and the mechanics of "Improve NULL handling in IN predicates optimizations" commit
core/trino-main/src/main/java/io/trino/sql/planner/ExpressionInterpreter.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/sql/planner/ExpressionInterpreter.java
Outdated
Show resolved
Hide resolved
5114d72 to
666be07
Compare
core/trino-main/src/test/java/io/trino/sql/TestExpressionInterpreter.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/sql/planner/ExpressionInterpreter.java
Outdated
Show resolved
Hide resolved
666be07 to
6336f75
Compare
...in/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/expression/RewriteVarcharConstant.java
Outdated
Show resolved
Hide resolved
core/trino-spi/src/main/java/io/trino/spi/statistics/StatsUtil.java
Outdated
Show resolved
Hide resolved
d9cd56b to
9933b83
Compare
9933b83 to
94432ee
Compare
|
@findepi PTAL |
findepi
left a comment
There was a problem hiding this comment.
LGTM except of "Rewrite typed NULL char/varchar constants".
I'd suggest dropping this commit.
...in/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/expression/RewriteVarcharConstant.java
Show resolved
Hide resolved
...bc/src/main/java/io/trino/plugin/jdbc/expression/JdbcConnectorExpressionRewriterBuilder.java
Show resolved
Hide resolved
| return addStandardRules(identifierQuote, type -> Optional.empty()); | ||
| } | ||
|
|
||
| public JdbcConnectorExpressionRewriterBuilder addStandardRules(Function<String, String> identifierQuote, Function<Type, Optional<String>> typeMapping) |
There was a problem hiding this comment.
This is simple for PostgreSQL and varchar case, but generally this is tricky, since remote database and Trino have different type systems.
In case of
where x IN (a, b, c, NULL)
the NULL should be eliminated on the engine side (generic engine rule).
Do we have other cases where this is actually useful?
There was a problem hiding this comment.
x IN (NULL) is always false, right? Even for NULL IN (NULL) so eliminating NULLs is safe?
also x IN () (empty list) is also false
There was a problem hiding this comment.
x IN (NULL)is always false, right?
result is NULL
also
x IN ()(empty list) is also false
This is not allowed in SQL, but it it was allowed, it would be false.
There was a problem hiding this comment.
the NULL should be eliminated on the engine side (generic engine rule).
BTW such rule, if written, should go in separate PR.
in this PR please keep the tests exercising IN pushdown with NULLs (asserting that these doesn't get pushed down).
| { | ||
| if (type instanceof VarcharType) { | ||
| VarcharType varcharType = (VarcharType) type; | ||
| return varcharType.getLength().map(length -> format("varchar(%d)", length)).or(() -> Optional.of("varchar")); |
There was a problem hiding this comment.
Trino bounded varchar can be of length 2147483646, PostgreSQL has a different limit
| .with(functionName().equalTo(CAST_FUNCTION_NAME)) | ||
| .with(argument(0).capturedAs(ARGUMENT)); |
| "3, 'c'", | ||
| "4, 'd'", | ||
| "5, 'f'"))) { | ||
| assertThat(query("SELECT id FROM " + table.getName() + " WHERE CAST(id AS VARCHAR(1)) = '2' OR id2 = 'd'")) |
There was a problem hiding this comment.
Trino and PostgreSQL have different CAST behavior for BIGINT -> varchar(1) cast.
trino> SELECT CAST(BIGINT '12' AS varchar(1));
Query 20220509_075440_00000_3ce4u failed: Value 12 cannot be represented as varchar(1)
PostgreSQL
SELECT CAST(BIGINT '12' AS varchar(1));
1
There was a problem hiding this comment.
Postgres ended up going the Hive way? Does the SQL spec say anything about this?
Description
Adds translation for InPredicate and InListExpression to connector expression form.
New feature.
Core engine & postgres connector.
This enabled pushdown of IN predicates which are not expressible with Domain (i.e. references other symbols or contains function calls)
Related issues, pull requests, and links
Documentation
( ) No documentation is needed.
( ) Sufficient documentation is included in this PR.
( ) Documentation PR is available with #prnumber.
( ) Documentation issue #issuenumber is filed, and can be handled later.
Release notes
( ) No release notes entries required.
( ) Release notes entries required with the following suggested text: