From 413584a51b0d6b70653e2492d620b0c9338fb7b5 Mon Sep 17 00:00:00 2001 From: Sreeni Viswanadha Date: Sat, 20 Feb 2021 17:06:36 -0800 Subject: [PATCH] Don't rewrite IF to CASE --- .../rule/CanonicalizeExpressionRewriter.java | 16 ---------------- .../presto/sql/TestExpressionInterpreter.java | 7 ------- .../rule/TestCanonicalizeExpressionRewriter.java | 6 ------ 3 files changed, 29 deletions(-) diff --git a/presto-main/src/main/java/com/facebook/presto/sql/planner/iterative/rule/CanonicalizeExpressionRewriter.java b/presto-main/src/main/java/com/facebook/presto/sql/planner/iterative/rule/CanonicalizeExpressionRewriter.java index 544510e286d6..e4a1ecf2f4ae 100644 --- a/presto-main/src/main/java/com/facebook/presto/sql/planner/iterative/rule/CanonicalizeExpressionRewriter.java +++ b/presto-main/src/main/java/com/facebook/presto/sql/planner/iterative/rule/CanonicalizeExpressionRewriter.java @@ -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() {} @@ -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 treeRewriter) - { - Expression condition = treeRewriter.rewrite(node.getCondition(), context); - Expression trueValue = treeRewriter.rewrite(node.getTrueValue(), context); - - Optional 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 treeRewriter) { diff --git a/presto-main/src/test/java/com/facebook/presto/sql/TestExpressionInterpreter.java b/presto-main/src/test/java/com/facebook/presto/sql/TestExpressionInterpreter.java index 7787573c075f..0591320a237b 100644 --- a/presto-main/src/test/java/com/facebook/presto/sql/TestExpressionInterpreter.java +++ b/presto-main/src/test/java/com/facebook/presto/sql/TestExpressionInterpreter.java @@ -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 @@ -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"); diff --git a/presto-main/src/test/java/com/facebook/presto/sql/planner/iterative/rule/TestCanonicalizeExpressionRewriter.java b/presto-main/src/test/java/com/facebook/presto/sql/planner/iterative/rule/TestCanonicalizeExpressionRewriter.java index fd3563da4e0e..ffd4afcbd660 100644 --- a/presto-main/src/test/java/com/facebook/presto/sql/planner/iterative/rule/TestCanonicalizeExpressionRewriter.java +++ b/presto-main/src/test/java/com/facebook/presto/sql/planner/iterative/rule/TestCanonicalizeExpressionRewriter.java @@ -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() {