Skip to content

Commit

Permalink
Don't rewrite IF to CASE
Browse files Browse the repository at this point in the history
  • Loading branch information
Sreeni Viswanadha authored and Rongrong Zhong committed Apr 20, 2021
1 parent d0e46d5 commit 413584a
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@
import com.facebook.presto.sql.tree.ExpressionTreeRewriter;
import com.facebook.presto.sql.tree.Extract;
import com.facebook.presto.sql.tree.FunctionCall;
import com.facebook.presto.sql.tree.IfExpression;
import com.facebook.presto.sql.tree.IsNotNullPredicate;
import com.facebook.presto.sql.tree.IsNullPredicate;
import com.facebook.presto.sql.tree.NotExpression;
import com.facebook.presto.sql.tree.QualifiedName;
import com.facebook.presto.sql.tree.SearchedCaseExpression;
import com.facebook.presto.sql.tree.WhenClause;
import com.google.common.collect.ImmutableList;

import java.util.Optional;

public class CanonicalizeExpressionRewriter
{
private CanonicalizeExpressionRewriter() {}
Expand All @@ -49,17 +44,6 @@ public Expression rewriteIsNotNullPredicate(IsNotNullPredicate node, Void contex
return new NotExpression(new IsNullPredicate(value));
}

@Override
public Expression rewriteIfExpression(IfExpression node, Void context, ExpressionTreeRewriter<Void> treeRewriter)
{
Expression condition = treeRewriter.rewrite(node.getCondition(), context);
Expression trueValue = treeRewriter.rewrite(node.getTrueValue(), context);

Optional<Expression> falseValue = node.getFalseValue().map((value) -> treeRewriter.rewrite(value, context));

return new SearchedCaseExpression(ImmutableList.of(new WhenClause(condition, trueValue)), falseValue);
}

@Override
public Expression rewriteCurrentTime(CurrentTime node, Void context, ExpressionTreeRewriter<Void> treeRewriter)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1210,10 +1210,6 @@ public void testIf()
assertOptimizedEquals("IF(false, 1.01, 1.02)", "1.02");
assertOptimizedEquals("IF(true, 1234567890.123, 1.02)", "1234567890.123");
assertOptimizedEquals("IF(false, 1.01, 1234567890.123)", "1234567890.123");

// todo optimize case statement
assertOptimizedEquals("IF(unbound_boolean, 1 + 2, 3 + 4)", "CASE WHEN unbound_boolean THEN (1 + 2) ELSE (3 + 4) END");
assertOptimizedEquals("IF(unbound_boolean, BIGINT '1' + 2, 3 + 4)", "CASE WHEN unbound_boolean THEN (BIGINT '1' + 2) ELSE (3 + 4) END");
}

@Test
Expand Down Expand Up @@ -1351,9 +1347,6 @@ public void testBind()
@Test
public void testFailedExpressionOptimization()
{
assertOptimizedEquals("if(unbound_boolean, 1, 0 / 0)", "CASE WHEN unbound_boolean THEN 1 ELSE 0 / 0 END");
assertOptimizedEquals("if(unbound_boolean, 0 / 0, 1)", "CASE WHEN unbound_boolean THEN 0 / 0 ELSE 1 END");

assertOptimizedMatches("CASE unbound_long WHEN 1 THEN 1 WHEN 0 / 0 THEN 2 END",
"CASE unbound_long WHEN BIGINT '1' THEN 1 WHEN cast(fail(8, 'ignored failure message') as bigint) THEN 2 END");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ public void testRewriteIsNotNullPredicate()
assertRewritten("x is NOT NULL", "NOT (x IS NULL)");
}

@Test
public void testRewriteIfExpression()
{
assertRewritten("IF(x = 0, 0, 1)", "CASE WHEN x = 0 THEN 0 ELSE 1 END");
}

@Test
public void testRewriteCurrentTime()
{
Expand Down

0 comments on commit 413584a

Please sign in to comment.