You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2020/04/29 15:41:36 UTC

[GitHub] [incubator-doris] kangkaisen commented on a change in pull request #3396: (#3395)calculate 'case when expr' when possible

kangkaisen commented on a change in pull request #3396:
URL: https://github.com/apache/incubator-doris/pull/3396#discussion_r417402243



##########
File path: fe/src/test/java/org/apache/doris/planner/QueryPlanTest.java
##########
@@ -504,4 +504,106 @@ public void testDateTypeEquality() throws Exception {
         Catalog.getCurrentCatalog().getLoadManager().createLoadJobV1FromStmt(loadStmt, EtlJobType.HADOOP,
                 System.currentTimeMillis());
     }
+
+    private SelectStmt getAnalyzedAndRewritedStmt(String sql) throws Exception {
+        SelectStmt selectStmt =
+                (SelectStmt) UtFrameUtils.parseAndAnalyzeStmt(sql, connectContext);
+        selectStmt = (SelectStmt) UtFrameUtils.rewriteStmt(selectStmt, connectContext);
+        selectStmt = (SelectStmt) UtFrameUtils.reAnalyze(selectStmt, connectContext);
+        return selectStmt;
+    }
+
+    @Test
+    public void testConvertCaseWhenToConstant() throws Exception {
+        // basic test
+        String caseWhenSql = "select "
+                + "case when date_format(now(),'%H%i')  < 123 then 1 else 0 end as col "
+                + "from test.test1 "
+                + "where time = case when date_format(now(),'%H%i')  < 123 then date_format(date_sub(now(),2),'%Y%m%d') else date_format(date_sub(now(),1),'%Y%m%d') end";
+        SelectStmt selectStmt = getAnalyzedAndRewritedStmt(caseWhenSql);
+        Assert.assertTrue(!selectStmt.toSql().contains("CASE WHEN") && !selectStmt.toSql().contains("case when"));

Review comment:
       you could use `org.apache.commons.lang3.StringUtils.containsIgnoreCase`

##########
File path: fe/src/test/java/org/apache/doris/planner/QueryPlanTest.java
##########
@@ -504,4 +504,106 @@ public void testDateTypeEquality() throws Exception {
         Catalog.getCurrentCatalog().getLoadManager().createLoadJobV1FromStmt(loadStmt, EtlJobType.HADOOP,
                 System.currentTimeMillis());
     }
+
+    private SelectStmt getAnalyzedAndRewritedStmt(String sql) throws Exception {
+        SelectStmt selectStmt =
+                (SelectStmt) UtFrameUtils.parseAndAnalyzeStmt(sql, connectContext);
+        selectStmt = (SelectStmt) UtFrameUtils.rewriteStmt(selectStmt, connectContext);
+        selectStmt = (SelectStmt) UtFrameUtils.reAnalyze(selectStmt, connectContext);
+        return selectStmt;
+    }
+
+    @Test
+    public void testConvertCaseWhenToConstant() throws Exception {

Review comment:
       Please move this test to `ConstantExpressTest`

##########
File path: fe/src/main/java/org/apache/doris/analysis/CaseExpr.java
##########
@@ -251,4 +257,72 @@ public void analyzeImpl(Analyzer analyzer) throws AnalysisException {
         }
         return exprs;
     }
+
+    // this method just compare literal value and not completely consistent with be,for two cases
+    // 1 not deal float
+    // 2 just compare literal value with same type. for a example sql 'select case when 123 then '1' else '2' end as col'
+    //      for be will return '1', because be only regard 0 as false
+    //      but for current LiteralExpr.compareLiteral, `123`' won't be regard as true
+    //  the case which two values has different type left to be
+    public static Expr computeCaseExpr(CaseExpr expr) {
+        LiteralExpr caseExpr;
+        int startIndex = 0;
+        int size = expr.getChildren().size() - 1;
+        if (expr.hasCaseExpr()) {
+            // just deal literal here
+            // and avoid `float compute` in java,float should be dealt in be
+            Expr caseChildExpr = expr.getChild(0);
+            if (!caseChildExpr.isLiteral()
+                    || caseChildExpr instanceof DecimalLiteral || caseChildExpr instanceof FloatLiteral) {
+                return expr;
+            }
+            caseExpr = (LiteralExpr) expr.getChild(0);
+            startIndex++;
+            size--;
+        } else {
+            caseExpr = new BoolLiteral(true);
+        }
+
+        if (expr.hasElseExpr) {
+            size--;
+        }
+
+        //pre return when the `when expr` can't be converted to constants

Review comment:
       ```suggestion
           // Early return when the `when expr` can't be converted to constants
   ```

##########
File path: fe/src/test/java/org/apache/doris/planner/QueryPlanTest.java
##########
@@ -504,4 +504,106 @@ public void testDateTypeEquality() throws Exception {
         Catalog.getCurrentCatalog().getLoadManager().createLoadJobV1FromStmt(loadStmt, EtlJobType.HADOOP,
                 System.currentTimeMillis());
     }
+
+    private SelectStmt getAnalyzedAndRewritedStmt(String sql) throws Exception {

Review comment:
       Don't need to add this method. you could use `getSQLPlanOrErrorMsg` method.

##########
File path: fe/src/main/java/org/apache/doris/analysis/CaseExpr.java
##########
@@ -251,4 +257,72 @@ public void analyzeImpl(Analyzer analyzer) throws AnalysisException {
         }
         return exprs;
     }
+
+    // this method just compare literal value and not completely consistent with be,for two cases
+    // 1 not deal float
+    // 2 just compare literal value with same type. for a example sql 'select case when 123 then '1' else '2' end as col'
+    //      for be will return '1', because be only regard 0 as false
+    //      but for current LiteralExpr.compareLiteral, `123`' won't be regard as true
+    //  the case which two values has different type left to be
+    public static Expr computeCaseExpr(CaseExpr expr) {
+        LiteralExpr caseExpr;
+        int startIndex = 0;

Review comment:
       would better rename `startIndex ` and `size `.
   
   The `size` compute logic seems not consistent with BE ?
   
   ```
           int loop_start = has_case_expr() ? 1 : 0; \
           int loop_end = (has_else_expr()) ? num_children - 1 : num_children; \
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org