This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch opt_memtable_speed
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/opt_memtable_speed by this 
push:
     new 540eadb3c1 [feature](Nereids): convert CASE WHEN to IF (#23109)
540eadb3c1 is described below

commit 540eadb3c136bcf59c1d075d8ce0c236bacb9c82
Author: xzj7019 <[email protected]>
AuthorDate: Thu Aug 17 16:36:32 2023 +0800

    [feature](Nereids): convert CASE WHEN to IF (#23109)
    
    Co-authored-by: zhongjian.xzj <[email protected]>
---
 .../rules/expression/ExpressionNormalization.java  |  6 ---
 .../rules/expression/ExpressionOptimization.java   |  5 ++-
 .../rules/expression/rules/CaseWhenToIf.java       | 49 ++++++++++++++++++++++
 3 files changed, 52 insertions(+), 8 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionNormalization.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionNormalization.java
index 3cfd84a00c..b59774dae5 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionNormalization.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionNormalization.java
@@ -29,7 +29,6 @@ import 
org.apache.doris.nereids.rules.expression.rules.SimplifyArithmeticRule;
 import org.apache.doris.nereids.rules.expression.rules.SimplifyCastRule;
 import org.apache.doris.nereids.rules.expression.rules.SimplifyNotExprRule;
 import 
org.apache.doris.nereids.rules.expression.rules.SupportJavaDateFormatter;
-import org.apache.doris.nereids.trees.expressions.Expression;
 
 import com.google.common.collect.ImmutableList;
 
@@ -60,10 +59,5 @@ public class ExpressionNormalization extends 
ExpressionRewrite {
     public ExpressionNormalization() {
         super(new ExpressionRuleExecutor(NORMALIZE_REWRITE_RULES));
     }
-
-    @Override
-    public Expression rewrite(Expression expression, ExpressionRewriteContext 
context) {
-        return super.rewrite(expression, context);
-    }
 }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java
index 78676ca307..34c6261c3c 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java
@@ -17,6 +17,7 @@
 
 package org.apache.doris.nereids.rules.expression;
 
+import org.apache.doris.nereids.rules.expression.rules.CaseWhenToIf;
 import org.apache.doris.nereids.rules.expression.rules.DistinctPredicatesRule;
 import org.apache.doris.nereids.rules.expression.rules.ExtractCommonFactorRule;
 import org.apache.doris.nereids.rules.expression.rules.OrToIn;
@@ -38,8 +39,8 @@ public class ExpressionOptimization extends ExpressionRewrite 
{
             SimplifyComparisonPredicate.INSTANCE,
             SimplifyDecimalV3Comparison.INSTANCE,
             SimplifyRange.INSTANCE,
-            OrToIn.INSTANCE
-
+            OrToIn.INSTANCE,
+            CaseWhenToIf.INSTANCE
     );
     private static final ExpressionRuleExecutor EXECUTOR = new 
ExpressionRuleExecutor(OPTIMIZE_REWRITE_RULES);
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/CaseWhenToIf.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/CaseWhenToIf.java
new file mode 100644
index 0000000000..6372338406
--- /dev/null
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/CaseWhenToIf.java
@@ -0,0 +1,49 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.rules.expression.rules;
+
+import org.apache.doris.nereids.rules.expression.AbstractExpressionRewriteRule;
+import org.apache.doris.nereids.rules.expression.ExpressionRewriteContext;
+import org.apache.doris.nereids.trees.expressions.CaseWhen;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.WhenClause;
+import org.apache.doris.nereids.trees.expressions.functions.scalar.If;
+import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
+
+/**
+ * Rewrite rule to convert CASE WHEN to IF.
+ * For example:
+ * CASE WHEN a > 1 THEN 1 ELSE 0 END -> IF(a > 1, 1, 0)
+ */
+public class CaseWhenToIf extends AbstractExpressionRewriteRule {
+
+    public static CaseWhenToIf INSTANCE = new CaseWhenToIf();
+
+    @Override
+    public Expression visitCaseWhen(CaseWhen caseWhen, 
ExpressionRewriteContext context) {
+        Expression expr = caseWhen;
+        if (caseWhen.getWhenClauses().size() == 1) {
+            WhenClause whenClause = caseWhen.getWhenClauses().get(0);
+            Expression operand = whenClause.getOperand();
+            Expression result = whenClause.getResult();
+            expr = new If(operand, result, 
caseWhen.getDefaultValue().orElse(new NullLiteral(result.getDataType())));
+        }
+        // TODO: traverse expr in CASE WHEN / If.
+        return expr;
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to