-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Push down IF in PostgreSQL connector #11533
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ | |
| import io.trino.sql.planner.TypeProvider; | ||
| import io.trino.sql.tree.ComparisonExpression; | ||
| import io.trino.sql.tree.Expression; | ||
| import io.trino.sql.tree.IfExpression; | ||
| import io.trino.sql.tree.IsNotNullPredicate; | ||
| import io.trino.sql.tree.IsNullPredicate; | ||
| import io.trino.sql.tree.LikePredicate; | ||
|
|
@@ -248,6 +249,56 @@ public void testConvertComparison(ComparisonExpression.Operator operator) | |
| throw new UnsupportedOperationException("Unsupported operator: " + operator); | ||
| } | ||
|
|
||
| @Test(dataProvider = "testConvertComparisonDataProvider") | ||
| public void testConvertIf(ComparisonExpression.Operator operator) | ||
|
Comment on lines
+252
to
+253
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the why do you need it here?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I put it to catch future changes. I will just use equality. |
||
| { | ||
| // if(c_bigint_symbol = 42, a_varchar, b_varchar) | ||
| Optional<String> convertedWithFalseArgument = JDBC_CLIENT.convertPredicate( | ||
| SESSION, | ||
| translateToConnectorExpression( | ||
| new IfExpression( | ||
| new ComparisonExpression( | ||
| operator, | ||
| new SymbolReference("c_bigint_symbol"), | ||
| LITERAL_ENCODER.toExpression(TEST_SESSION, 42L, BIGINT)), | ||
| new SymbolReference("a_varchar_symbol"), | ||
| new SymbolReference("b_varchar_symbol")), | ||
| ImmutableMap.of("c_bigint_symbol", BIGINT, "a_varchar_symbol", VARCHAR_COLUMN.getColumnType(), "b_varchar_symbol", VARCHAR_COLUMN.getColumnType())), | ||
| ImmutableMap.of("c_bigint_symbol", DOUBLE_COLUMN, "a_varchar_symbol", VARCHAR_COLUMN, "b_varchar_symbol", VARCHAR_COLUMN)); | ||
|
|
||
| // if(c_bigint_symbol = 42, a_varchar) | ||
| Optional<String> convertedWithoutFalseArgument = JDBC_CLIENT.convertPredicate( | ||
| SESSION, | ||
| translateToConnectorExpression( | ||
| new IfExpression( | ||
| new ComparisonExpression( | ||
| operator, | ||
| new SymbolReference("c_bigint_symbol"), | ||
| LITERAL_ENCODER.toExpression(TEST_SESSION, 42L, BIGINT)), | ||
| new SymbolReference("a_varchar_symbol"), | ||
| null), | ||
| ImmutableMap.of("c_bigint_symbol", BIGINT, "a_varchar_symbol", VARCHAR_COLUMN.getColumnType())), | ||
| ImmutableMap.of("c_bigint_symbol", DOUBLE_COLUMN, "a_varchar_symbol", VARCHAR_COLUMN)); | ||
|
|
||
| switch (operator) { | ||
| case EQUAL: | ||
| case NOT_EQUAL: | ||
| assertThat(convertedWithFalseArgument).hasValue(format("CASE WHEN ((\"c_double\") %s (42)) THEN (\"c_varchar\") ELSE (\"c_varchar\") END", operator.getValue())); | ||
| assertThat(convertedWithoutFalseArgument).hasValue(format("CASE WHEN ((\"c_double\") %s (42)) THEN (\"c_varchar\") END", operator.getValue())); | ||
| return; | ||
| case LESS_THAN: | ||
| case LESS_THAN_OR_EQUAL: | ||
| case GREATER_THAN: | ||
| case GREATER_THAN_OR_EQUAL: | ||
| case IS_DISTINCT_FROM: | ||
| // Not supported yet, even for bigint | ||
| assertThat(convertedWithFalseArgument).isEmpty(); | ||
| assertThat(convertedWithoutFalseArgument).isEmpty(); | ||
| return; | ||
| } | ||
| throw new UnsupportedOperationException("Unsupported operator: " + operator); | ||
| } | ||
|
|
||
| @DataProvider | ||
| public static Object[][] testConvertComparisonDataProvider() | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -827,6 +827,26 @@ public void testNotExpressionPushdown() | |
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testIfPredicatePushdown() | ||
| { | ||
| assertThat(query("SELECT nationkey FROM nation WHERE IF(name = 'ALGERIA', true, false)")) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| .matches("VALUES BIGINT '0'") | ||
| .isFullyPushedDown(); | ||
|
|
||
| assertThat(query("SELECT name FROM nation WHERE IF(nationkey = 0, true, false)")) | ||
| .matches("VALUES CAST('ALGERIA' AS varchar(25))") | ||
| .isFullyPushedDown(); | ||
|
|
||
| assertThat(query("SELECT name FROM nation WHERE IF(nationkey <> 0, true, false)")) | ||
| .matches("SELECT name FROM nation WHERE nationkey <> 0") | ||
| .isFullyPushedDown(); | ||
|
|
||
| assertThat(query("SELECT nationkey FROM nation WHERE IF(name = 'Algeria', true, false)")) | ||
| .returnsEmptyResult() | ||
| .isFullyPushedDown(); | ||
| } | ||
|
|
||
| @Override | ||
| protected String errorMessageForInsertIntoNotNullColumn(String columnName) | ||
| { | ||
|
|
||
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.
@martint and I discussed this and the conclusion was to model IF via CASE.
see #11699 (comment) for more details.