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

Push down NULLIF in PostgreSQL connector #11532

Merged
merged 2 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import io.trino.sql.tree.Node;
import io.trino.sql.tree.NodeRef;
import io.trino.sql.tree.NotExpression;
import io.trino.sql.tree.NullIfExpression;
import io.trino.sql.tree.NullLiteral;
import io.trino.sql.tree.QualifiedName;
import io.trino.sql.tree.StringLiteral;
Expand Down Expand Up @@ -91,6 +92,7 @@
import static io.trino.spi.expression.StandardFunctions.NEGATE_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.NOT_EQUAL_OPERATOR_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.NOT_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.NULLIF_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.OR_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.SUBTRACT_FUNCTION_NAME;
import static io.trino.spi.type.BooleanType.BOOLEAN;
Expand Down Expand Up @@ -222,6 +224,9 @@ protected Optional<Expression> translateCall(Call call)
if (IS_NULL_FUNCTION_NAME.equals(call.getFunctionName()) && call.getArguments().size() == 1) {
return translateIsNull(call.getArguments().get(0));
}
if (NULLIF_FUNCTION_NAME.equals(call.getFunctionName()) && call.getArguments().size() == 2) {
return translateNullIf(call.getArguments().get(0), call.getArguments().get(1));
}

// comparisons
if (call.getArguments().size() == 2) {
Expand Down Expand Up @@ -321,6 +326,17 @@ private Optional<Expression> translateComparison(ComparisonExpression.Operator o
new ComparisonExpression(operator, leftTranslated, rightTranslated)));
}

private Optional<Expression> translateNullIf(ConnectorExpression first, ConnectorExpression second)
{
Optional<Expression> firstExpression = translate(first);
Optional<Expression> secondExpression = translate(second);
if (firstExpression.isPresent() && secondExpression.isPresent()) {
return Optional.of(new NullIfExpression(firstExpression.get(), secondExpression.get()));
}

return Optional.empty();
}

private Optional<ComparisonExpression.Operator> comparisonOperatorForFunctionName(FunctionName functionName)
{
if (EQUAL_OPERATOR_FUNCTION_NAME.equals(functionName)) {
Expand Down Expand Up @@ -651,6 +667,17 @@ protected Optional<ConnectorExpression> visitLikePredicate(LikePredicate node, V
return Optional.empty();
}

@Override
protected Optional<ConnectorExpression> visitNullIfExpression(NullIfExpression node, Void context)
{
Optional<ConnectorExpression> firstValue = process(node.getFirst());
Optional<ConnectorExpression> secondValue = process(node.getSecond());
if (firstValue.isPresent() && secondValue.isPresent()) {
return Optional.of(new Call(typeOf(node), NULLIF_FUNCTION_NAME, ImmutableList.of(firstValue.get(), secondValue.get())));
}
return Optional.empty();
}

@Override
protected Optional<ConnectorExpression> visitSubscriptExpression(SubscriptExpression node, Void context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io.trino.sql.tree.LogicalExpression;
import io.trino.sql.tree.LongLiteral;
import io.trino.sql.tree.NotExpression;
import io.trino.sql.tree.NullIfExpression;
import io.trino.sql.tree.QualifiedName;
import io.trino.sql.tree.StringLiteral;
import io.trino.sql.tree.SubscriptExpression;
Expand All @@ -55,6 +56,7 @@
import static io.trino.spi.expression.StandardFunctions.LIKE_PATTERN_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.NEGATE_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.NOT_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.NULLIF_FUNCTION_NAME;
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.spi.type.BooleanType.BOOLEAN;
import static io.trino.spi.type.DecimalType.createDecimalType;
Expand Down Expand Up @@ -282,6 +284,20 @@ public void testTranslateIsNotNull()
List.of(new Call(BOOLEAN, IS_NULL_FUNCTION_NAME, List.of(new Variable("varchar_symbol_1", VARCHAR_TYPE))))));
}

@Test
public void testTranslateNullIf()
{
assertTranslationRoundTrips(
new NullIfExpression(
new SymbolReference("varchar_symbol_1"),
new SymbolReference("varchar_symbol_1")),
new Call(
VARCHAR_TYPE,
NULLIF_FUNCTION_NAME,
List.of(new Variable("varchar_symbol_1", VARCHAR_TYPE),
new Variable("varchar_symbol_1", VARCHAR_TYPE))));
}

@Test
public void testTranslateResolvedFunction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ private StandardFunctions() {}
public static final FunctionName NOT_FUNCTION_NAME = new FunctionName("$not");

public static final FunctionName IS_NULL_FUNCTION_NAME = new FunctionName("$is_null");
/**
* $nullif is a function accepting two arguments. Returns null if both values are the same, otherwise returns the first value.
*/
public static final FunctionName NULLIF_FUNCTION_NAME = new FunctionName("$nullif");
findepi marked this conversation as resolved.
Show resolved Hide resolved

public static final FunctionName EQUAL_OPERATOR_FUNCTION_NAME = new FunctionName("$equal");
public static final FunctionName NOT_EQUAL_OPERATOR_FUNCTION_NAME = new FunctionName("$not_equal");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ public PostgreSqlClient(
.map("$not($is_null(value))").to("value IS NOT NULL")
.map("$not(value: boolean)").to("NOT value")
.map("$is_null(value)").to("value IS NULL")
.map("$nullif(first, second)").to("NULLIF(first, second)")
findepi marked this conversation as resolved.
Show resolved Hide resolved
.build();

JdbcTypeHandle bigintTypeHandle = new JdbcTypeHandle(Types.BIGINT, Optional.of("bigint"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package io.trino.plugin.postgresql;

import com.google.common.collect.ImmutableMap;
import io.trino.plugin.jdbc.BaseJdbcConfig;
import io.trino.plugin.jdbc.ColumnMapping;
import io.trino.plugin.jdbc.DefaultQueryBuilder;
Expand All @@ -37,6 +38,7 @@
import io.trino.sql.tree.LikePredicate;
import io.trino.sql.tree.LogicalExpression;
import io.trino.sql.tree.NotExpression;
import io.trino.sql.tree.NullIfExpression;
import io.trino.sql.tree.StringLiteral;
import io.trino.sql.tree.SymbolReference;
import org.testng.annotations.DataProvider;
Expand Down Expand Up @@ -305,6 +307,20 @@ public void testConvertIsNotNull()
.hasValue("(\"c_varchar\") IS NOT NULL");
}

@Test
public void testConvertNullIf()
{
// nullif(a_varchar, b_varchar)
assertThat(JDBC_CLIENT.convertPredicate(SESSION,
translateToConnectorExpression(
new NullIfExpression(
new SymbolReference("a_varchar_symbol"),
new SymbolReference("b_varchar_symbol")),
ImmutableMap.of("a_varchar_symbol", VARCHAR_COLUMN.getColumnType(), "b_varchar_symbol", VARCHAR_COLUMN.getColumnType())),
ImmutableMap.of("a_varchar_symbol", VARCHAR_COLUMN, "b_varchar_symbol", VARCHAR_COLUMN)))
.hasValue("NULLIF((\"c_varchar\"), (\"c_varchar\"))");
}

@Test
public void testConvertNotExpression()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,27 @@ public void testIsNotNullPredicatePushdown()
}
}

@Test
public void testNullIfPredicatePushdown()
{
assertThat(query("SELECT nationkey FROM nation WHERE NULLIF(name, 'ALGERIA') IS NULL"))
.matches("VALUES BIGINT '0'")
.isFullyPushedDown();
ebyhr marked this conversation as resolved.
Show resolved Hide resolved

assertThat(query("SELECT name FROM nation WHERE NULLIF(nationkey, 0) IS NULL"))
.matches("VALUES CAST('ALGERIA' AS varchar(25))")
.isFullyPushedDown();

assertThat(query("SELECT nationkey FROM nation WHERE NULLIF(name, 'Algeria') IS NULL"))
.returnsEmptyResult()
.isFullyPushedDown();

// NULLIF returns the first argument because arguments aren't the same
assertThat(query("SELECT nationkey FROM nation WHERE NULLIF(name, 'Name not found') = name"))
.matches("SELECT nationkey FROM nation")
.isFullyPushedDown();
}

@Test
public void testNotExpressionPushdown()
{
Expand Down