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

liyang pushed a commit to branch kylin5
in repository https://gitbox.apache.org/repos/asf/kylin.git

commit cd09148868fd2b4cb5a08d236e1a0c0e8dd5eaea
Author: Pengfei Zhan <dethr...@gmail.com>
AuthorDate: Wed Jul 5 17:50:34 2023 +0800

    KYLIN-5764 Sum expression supports if condition
---
 .../kylin/query/engine/SumExprPlannerTest.xml      |  63 ++++++
 .../resources/query/sql_sum_expr/query00-if.sql    |  45 +++++
 .../apache/kylin/query/util/AggExpressionUtil.java | 215 ++++-----------------
 3 files changed, 141 insertions(+), 182 deletions(-)

diff --git 
a/src/kylin-it/src/test/resources/org/apache/kylin/query/engine/SumExprPlannerTest.xml
 
b/src/kylin-it/src/test/resources/org/apache/kylin/query/engine/SumExprPlannerTest.xml
index 0796314ef7..5cd88d79e1 100644
--- 
a/src/kylin-it/src/test/resources/org/apache/kylin/query/engine/SumExprPlannerTest.xml
+++ 
b/src/kylin-it/src/test/resources/org/apache/kylin/query/engine/SumExprPlannerTest.xml
@@ -80,6 +80,69 @@ KapOLAPToEnumerableConverter
                           KapTableScan(table=[[DEFAULT, TEST_COUNTRY]], 
ctx=[], fields=[[0, 1, 2, 3]])
                         KapTableScan(table=[[DEFAULT, TEST_COUNTRY]], ctx=[], 
fields=[[0, 1, 2, 3]])
 ]]>
+        </Resource>
+        <Resource name="query00-if.planBefore">
+            <![CDATA[
+KapOLAPToEnumerableConverter
+  KapProjectRel(COL1=[$2], COL2=[$3], COL3=[$0], COL4=[$4], COL5=[$5], 
COL6=[*(3, $4)], COL7=[$6], COL8=[$7], COL9=[$1], COL91=[CASE(=($8, 0), 0, 
AND(<>($4, 0), =($8, 0)), 0, *($4, $8))], ctx=[])
+    KapAggregateRel(group-set=[[0, 1]], groups=[null], COL1=[COUNT()], 
COL2=[SUM($2)], COL4=[SUM($3)], COL5=[COUNT($3)], COL7=[SUM($4)], 
COL8=[SUM($5)], agg#6=[SUM($6)], ctx=[])
+      KapProjectRel(COL3=[$7], COL9=[$44], $f2=[IF(<=($44, 2012-12-31), *($8, 
4), 1)], PRICE=[$8], $f4=[*($8, 3)], $f5=[/($8, 2)], ITEM_COUNT=[$9], ctx=[])
+        KapFilterRel(condition=[=($3, 'FP-GTC')], ctx=[])
+          KapJoinRel(condition=[=($207, $213)], joinType=[inner], ctx=[])
+            KapJoinRel(condition=[=($202, $209)], joinType=[inner], ctx=[])
+              KapJoinRel(condition=[=($40, $204)], joinType=[inner], ctx=[])
+                KapJoinRel(condition=[=($7, $199)], joinType=[inner], ctx=[])
+                  KapJoinRel(condition=[=($6, $190)], joinType=[inner], ctx=[])
+                    KapJoinRel(condition=[=($5, $180)], joinType=[inner], 
ctx=[])
+                      KapJoinRel(condition=[AND(=($4, $144), =($5, $146))], 
joinType=[inner], ctx=[])
+                        KapJoinRel(condition=[=($2, $44)], joinType=[inner], 
ctx=[])
+                          KapJoinRel(condition=[=($1, $39)], joinType=[inner], 
ctx=[])
+                            KapTableScan(table=[[DEFAULT, TEST_KYLIN_FACT]], 
ctx=[], fields=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 
38]])
+                            KapTableScan(table=[[DEFAULT, TEST_ORDER]], 
ctx=[], fields=[[0, 1, 2, 3, 4]])
+                          KapTableScan(table=[[EDW, TEST_CAL_DT]], ctx=[], 
fields=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 
99]])
+                        KapTableScan(table=[[DEFAULT, 
TEST_CATEGORY_GROUPINGS]], ctx=[], fields=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 
31, 32, 33, 34, 35]])
+                      KapTableScan(table=[[EDW, TEST_SITES]], ctx=[], 
fields=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
+                    KapTableScan(table=[[EDW, TEST_SELLER_TYPE_DIM]], ctx=[], 
fields=[[0, 1, 2, 3, 4, 5, 6, 7, 8]])
+                  KapTableScan(table=[[DEFAULT, TEST_ACCOUNT]], ctx=[], 
fields=[[0, 1, 2, 3, 4]])
+                KapTableScan(table=[[DEFAULT, TEST_ACCOUNT]], ctx=[], 
fields=[[0, 1, 2, 3, 4]])
+              KapTableScan(table=[[DEFAULT, TEST_COUNTRY]], ctx=[], 
fields=[[0, 1, 2, 3]])
+            KapTableScan(table=[[DEFAULT, TEST_COUNTRY]], ctx=[], fields=[[0, 
1, 2, 3]])
+]]>
+        </Resource>
+        <Resource name="query00-if.planAfter">
+            <![CDATA[
+KapOLAPToEnumerableConverter
+  KapProjectRel(COL1=[$2], COL2=[$3], COL3=[$0], COL4=[$4], COL5=[$5], 
COL6=[*(3, $4)], COL7=[$6], COL8=[$7], COL9=[$1], COL91=[CASE(=($8, 0), 0, 
AND(<>($4, 0), =($8, 0)), 0, *($4, $8))], ctx=[])
+    KapAggregateRel(group-set=[[0, 1]], groups=[null], AGG$0=[$SUM0($2)], 
AGG$1=[SUM($3)], AGG$2=[SUM($4)], AGG$3=[$SUM0($5)], AGG$4=[SUM($6)], 
AGG$5=[SUM($7)], AGG$6=[SUM($8)], ctx=[])
+      KapProjectRel(SELLER_ID=[$0], CAL_DT0=[$1], COL1=[$3], $f3=[CASE(<=($2, 
2012-12-31), $9, $10)], COL4=[$4], COL5=[$5], COL7=[$6], COL8=[$7], $f8=[$8], 
ctx=[])
+        KapAggregateRel(group-set=[[0, 1, 2]], groups=[null], AGG$0=[SUM($3)], 
AGG$1=[SUM($4)], AGG$2=[SUM($5)], AGG$3=[SUM($6)], AGG$4=[SUM($7)], 
AGG$5=[SUM($8)], AGG$6=[SUM($9)], AGG$7=[SUM($10)], ctx=[])
+          KapProjectRel(SELLER_ID=[$0], CAL_DT0=[$1], CAL_DT00=[$2], 
COL1=[$3], COL4=[$4], COL5=[$5], $f6=[*($6, 3)], $f7=[/($7, 2)], $f8=[$8], 
$f9=[*($9, 4)], SUM_CASE$0$1=[$10], ctx=[])
+            KapAggregateRel(group-set=[[0, 1, 2]], groups=[null], 
TOP_AGG$0=[$SUM0($3)], TOP_AGG$1=[SUM($4)], TOP_AGG$2=[$SUM0($5)], 
TOP_AGG$3=[SUM($6)], TOP_AGG$4=[SUM($7)], TOP_AGG$5=[SUM($8)], 
TOP_AGG$6=[SUM($9)], TOP_AGG$7=[SUM($10)], ctx=[])
+              KapProjectRel(SELLER_ID=[$0], CAL_DT0=[$1], CAL_DT00=[$2], 
COL1=[$3], COL4=[$4], COL5=[$5], SUM_OP$0=[$6], SUM_OP$1=[$7], $f8=[$8], 
SUM_OP$2=[$9], $f10=[*(1, $10)], ctx=[])
+                KapAggregateRel(group-set=[[0, 1, 2]], groups=[null], 
COL1=[COUNT()], COL4=[SUM($3)], COL5=[COUNT($4)], SUM_OP$0=[SUM($5)], 
SUM_OP$1=[SUM($6)], agg#5=[SUM($7)], SUM_OP$2=[SUM($8)], SUM_CONST$7=[COUNT()], 
ctx=[])
+                  KapProjectRel(SELLER_ID=[$7], CAL_DT0=[$44], CAL_DT00=[$44], 
PRICE=[$8], PRICE0=[$8], PRICE01=[$8], PRICE012=[$8], ITEM_COUNT=[$9], 
PRICE0123=[$8], ctx=[])
+                    KapFilterRel(condition=[=($3, 'FP-GTC')], ctx=[])
+                      KapJoinRel(condition=[=($207, $213)], joinType=[inner], 
ctx=[])
+                        KapJoinRel(condition=[=($202, $209)], 
joinType=[inner], ctx=[])
+                          KapJoinRel(condition=[=($40, $204)], 
joinType=[inner], ctx=[])
+                            KapJoinRel(condition=[=($7, $199)], 
joinType=[inner], ctx=[])
+                              KapJoinRel(condition=[=($6, $190)], 
joinType=[inner], ctx=[])
+                                KapJoinRel(condition=[=($5, $180)], 
joinType=[inner], ctx=[])
+                                  KapJoinRel(condition=[AND(=($4, $144), =($5, 
$146))], joinType=[inner], ctx=[])
+                                    KapJoinRel(condition=[=($2, $44)], 
joinType=[inner], ctx=[])
+                                      KapJoinRel(condition=[=($1, $39)], 
joinType=[inner], ctx=[])
+                                        KapTableScan(table=[[DEFAULT, 
TEST_KYLIN_FACT]], ctx=[], fields=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 
33, 34, 35, 36, 37, 38]])
+                                        KapTableScan(table=[[DEFAULT, 
TEST_ORDER]], ctx=[], fields=[[0, 1, 2, 3, 4]])
+                                      KapTableScan(table=[[EDW, TEST_CAL_DT]], 
ctx=[], fields=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 
38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 
58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 
98, 99]])
+                                    KapTableScan(table=[[DEFAULT, 
TEST_CATEGORY_GROUPINGS]], ctx=[], fields=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 
31, 32, 33, 34, 35]])
+                                  KapTableScan(table=[[EDW, TEST_SITES]], 
ctx=[], fields=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
+                                KapTableScan(table=[[EDW, 
TEST_SELLER_TYPE_DIM]], ctx=[], fields=[[0, 1, 2, 3, 4, 5, 6, 7, 8]])
+                              KapTableScan(table=[[DEFAULT, TEST_ACCOUNT]], 
ctx=[], fields=[[0, 1, 2, 3, 4]])
+                            KapTableScan(table=[[DEFAULT, TEST_ACCOUNT]], 
ctx=[], fields=[[0, 1, 2, 3, 4]])
+                          KapTableScan(table=[[DEFAULT, TEST_COUNTRY]], 
ctx=[], fields=[[0, 1, 2, 3]])
+                        KapTableScan(table=[[DEFAULT, TEST_COUNTRY]], ctx=[], 
fields=[[0, 1, 2, 3]])
+]]>
+
         </Resource>
         <Resource name="query01.planBefore">
             <![CDATA[
diff --git a/src/kylin-it/src/test/resources/query/sql_sum_expr/query00-if.sql 
b/src/kylin-it/src/test/resources/query/sql_sum_expr/query00-if.sql
new file mode 100644
index 0000000000..78d6b589ab
--- /dev/null
+++ b/src/kylin-it/src/test/resources/query/sql_sum_expr/query00-if.sql
@@ -0,0 +1,45 @@
+--
+-- 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.
+--
+
+SELECT COUNT(*) AS COL1,
+       SUM(if ("TEST_CAL_DT"."CAL_DT" <= CAST('2012-12-31' AS DATE) , PRICE*4, 
 1) ) AS COL2,
+       SELLER_ID AS COL3,
+       SUM(PRICE) AS COL4,
+       COUNT(PRICE) AS COL5,
+       3*SUM(PRICE) AS COL6,
+       SUM(PRICE*3) AS COL7,
+       SUM(PRICE/2) AS COL8,
+       "TEST_CAL_DT"."CAL_DT" AS COL9,
+       CASE WHEN SUM(ITEM_COUNT) = 0 THEN 0
+        WHEN SUM(PRICE) <> 0 AND SUM(ITEM_COUNT) = 0 THEN 0
+        ELSE SUM(PRICE) * SUM(ITEM_COUNT)  END AS COL91
+
+FROM "DEFAULT".TEST_KYLIN_FACT AS TEST_KYLIN_FACT
+
+INNER JOIN "DEFAULT".TEST_ORDER AS TEST_ORDER ON TEST_KYLIN_FACT.ORDER_ID = 
TEST_ORDER.ORDER_ID
+INNER JOIN EDW.TEST_CAL_DT AS TEST_CAL_DT ON TEST_KYLIN_FACT.CAL_DT = 
TEST_CAL_DT.CAL_DT
+INNER JOIN "DEFAULT".TEST_CATEGORY_GROUPINGS AS TEST_CATEGORY_GROUPINGS ON 
TEST_KYLIN_FACT.LEAF_CATEG_ID = TEST_CATEGORY_GROUPINGS.LEAF_CATEG_ID AND 
TEST_KYLIN_FACT.LSTG_SITE_ID = TEST_CATEGORY_GROUPINGS.SITE_ID
+INNER JOIN EDW.TEST_SITES AS TEST_SITES ON TEST_KYLIN_FACT.LSTG_SITE_ID = 
TEST_SITES.SITE_ID
+INNER JOIN EDW.TEST_SELLER_TYPE_DIM AS TEST_SELLER_TYPE_DIM ON 
TEST_KYLIN_FACT.SLR_SEGMENT_CD = TEST_SELLER_TYPE_DIM.SELLER_TYPE_CD
+INNER JOIN "DEFAULT".TEST_ACCOUNT AS SELLER_ACCOUNT ON 
TEST_KYLIN_FACT.SELLER_ID = SELLER_ACCOUNT.ACCOUNT_ID
+INNER JOIN "DEFAULT".TEST_ACCOUNT AS BUYER_ACCOUNT ON TEST_ORDER.BUYER_ID = 
BUYER_ACCOUNT.ACCOUNT_ID
+INNER JOIN "DEFAULT".TEST_COUNTRY AS SELLER_COUNTRY ON 
SELLER_ACCOUNT.ACCOUNT_COUNTRY = SELLER_COUNTRY.COUNTRY
+INNER JOIN "DEFAULT".TEST_COUNTRY AS BUYER_COUNTRY ON 
BUYER_ACCOUNT.ACCOUNT_COUNTRY = BUYER_COUNTRY.COUNTRY
+
+WHERE LSTG_FORMAT_NAME='FP-GTC'
+GROUP BY SELLER_ID,"TEST_CAL_DT"."CAL_DT"
diff --git 
a/src/query/src/main/java/org/apache/kylin/query/util/AggExpressionUtil.java 
b/src/query/src/main/java/org/apache/kylin/query/util/AggExpressionUtil.java
index e157c5f77d..a44e905ab4 100644
--- a/src/query/src/main/java/org/apache/kylin/query/util/AggExpressionUtil.java
+++ b/src/query/src/main/java/org/apache/kylin/query/util/AggExpressionUtil.java
@@ -48,6 +48,9 @@ import 
org.apache.kylin.query.exception.SumExprUnSupportException;
 
 import com.google.common.collect.ImmutableList;
 
+import lombok.Getter;
+import lombok.Setter;
+
 public class AggExpressionUtil {
 
     private AggExpressionUtil() {
@@ -79,6 +82,8 @@ public class AggExpressionUtil {
         return hasAggInput(input);
     }
 
+    @Getter
+    @Setter
     public static class AggExpression {
         // original info
         private AggregateCall agg;
@@ -114,38 +119,6 @@ public class AggExpressionUtil {
             return agg;
         }
 
-        public RelDataType getType() {
-            return type;
-        }
-
-        public void setType(RelDataType type) {
-            this.type = type;
-        }
-
-        public void setExpression(RexNode expression) {
-            this.expression = expression;
-        }
-
-        public RexNode getExpression() {
-            return expression;
-        }
-
-        public void setSumCase() {
-            this.isSumCase = true;
-        }
-
-        public boolean isSumCase() {
-            return isSumCase;
-        }
-
-        public boolean isCountDistinctCase() {
-            return isCountDistinctCase;
-        }
-
-        public void setCountDistinctCase(boolean countDistinctCase) {
-            isCountDistinctCase = countDistinctCase;
-        }
-
         public void setConditionsList(List<RexNode> conditionsList) {
             this.conditions = conditionsList;
             this.bottomProjConditionsInput = 
RelOptUtil.InputFinder.bits(conditions, null).toArray();
@@ -153,73 +126,15 @@ public class AggExpressionUtil {
             this.topProjConditionsInput = 
newArray(bottomProjConditionsInput.length);
         }
 
-        public List<RexNode> getConditions() {
-            return conditions;
-        }
-
-        public int[] getBottomProjConditionsInput() {
-            return bottomProjConditionsInput;
-        }
-
-        public int[] getBottomAggConditionsInput() {
-            return bottomAggConditionsInput;
-        }
-
-        public int[] getTopProjConditionsInput() {
-            return topProjConditionsInput;
-        }
-
         public void setValuesList(List<RexNode> valuesList) {
             this.valuesList = valuesList;
             this.bottomAggValuesInput = newArray(valuesList.size());
             this.topProjValuesInput = newArray(valuesList.size());
         }
-
-        public List<RexNode> getValuesList() {
-            return valuesList;
-        }
-
-        public int[] getBottomAggValuesInput() {
-            return bottomAggValuesInput;
-        }
-
-        public int[] getTopProjValuesInput() {
-            return topProjValuesInput;
-        }
-
-        public void setCount() {
-            this.isCount = true;
-        }
-
-        public boolean isCount() {
-            return isCount;
-        }
-
-        public int[] getBottomProjInput() {
-            return bottomProjInput;
-        }
-
-        public int[] getBottomAggInput() {
-            return bottomAggInput;
-        }
-
-        public int[] getTopProjInput() {
-            return topProjInput;
-        }
-
-        public int[] getTopAggInput() {
-            return topAggInput;
-        }
-
-        public void setSumConst() {
-            this.isSumConst = true;
-        }
-
-        public boolean isSumConst() {
-            return isSumConst;
-        }
     }
 
+    @Getter
+    @Setter
     public static class GroupExpression {
         private RexNode expression;
 
@@ -229,75 +144,26 @@ public class AggExpressionUtil {
         private int[] bottomAggInput;
         private int[] topProjInput;
         private int[] topAggInput;
-
-        public RexNode getExpression() {
-            return expression;
-        }
-
-        public void setExpression(RexNode expression) {
-            this.expression = expression;
-        }
-
-        public boolean isLiteral() {
-            return isLiteral;
-        }
-
-        public void setLiteral() {
-            isLiteral = true;
-        }
-
-        public int[] getBottomProjInput() {
-            return bottomProjInput;
-        }
-
-        public void setBottomProjInput(int[] bottomProjInput) {
-            this.bottomProjInput = bottomProjInput;
-        }
-
-        public int[] getBottomAggInput() {
-            return bottomAggInput;
-        }
-
-        public void setBottomAggInput(int[] bottomAggInput) {
-            this.bottomAggInput = bottomAggInput;
-        }
-
-        public int[] getTopProjInput() {
-            return topProjInput;
-        }
-
-        public void setTopProjInput(int[] topProjInput) {
-            this.topProjInput = topProjInput;
-        }
-
-        public int[] getTopAggInput() {
-            return topAggInput;
-        }
-
-        public void setTopAggInput(int[] topAggInput) {
-            this.topAggInput = topAggInput;
-        }
     }
 
-    public static boolean supportAggregateFunction(AggregateCall call) {
-        if (call.isDistinct())
-            return false;
-        SqlKind kind = call.getAggregation().getKind();
+    public static boolean hasSumCaseWhen(AggregateCall call, RexNode exp) {
+        return isSum(call.getAggregation().getKind()) && 
(isCaseWhenCondition(exp) || isIfCondition(exp));
+    }
 
-        return SqlKind.SUM == kind || SqlKind.SUM0 == kind || SqlKind.COUNT == 
kind || SqlKind.MAX == kind
-                || SqlKind.MIN == kind;
+    public static boolean hasCountDistinctCaseWhen(AggregateCall call, RexNode 
exp) {
+        boolean isCountDistinct = call.getAggregation().getKind() == 
SqlKind.COUNT && call.isDistinct();
+        return isCountDistinct && (isCaseWhenCondition(exp) || 
isIfCondition(exp));
     }
 
-    public static boolean hasSumCaseWhen(AggregateCall call, RexNode 
expression) {
-        if (isSum(call.getAggregation().getKind())) {
-            return SqlKind.CASE == expression.getKind();
+    private static boolean isIfCondition(RexNode exp) {
+        if (exp instanceof RexCall) {
+            return ((RexCall) exp).getOperator().isName("IF");
         }
         return false;
     }
 
-    public static boolean hasCountDistinctCaseWhen(AggregateCall call, RexNode 
expression) {
-        return call.getAggregation().getKind() == SqlKind.COUNT && 
call.isDistinct()
-                && expression.getKind() == SqlKind.CASE;
+    private static boolean isCaseWhenCondition(RexNode expression) {
+        return expression.isA(SqlKind.CASE);
     }
 
     public static boolean isSum(SqlKind kind) {
@@ -312,7 +178,7 @@ public class AggExpressionUtil {
             AggExpression aggExpression = new AggExpression(call);
             aggExpressions.add(aggExpression);
             if (SqlKind.COUNT == call.getAggregation().getKind()) {
-                aggExpression.setCount();
+                aggExpression.setCount(true);
             }
             if (call.getArgList().isEmpty()) {
                 // COUNT(*)
@@ -327,7 +193,7 @@ public class AggExpressionUtil {
             int[] sourceInput = 
RelOptUtil.InputFinder.bits(expression).toArray();
             aggExpression.setExpression(expression);
             if (hasSumCaseWhen(call, expression)) {
-                aggExpression.setSumCase();
+                aggExpression.setSumCase(true);
                 List<RexNode> conditions = 
extractCaseWhenConditions(expression);
                 aggExpression.setConditionsList(conditions);
                 List<RexNode> valuesList = 
extractCaseThenElseValues(expression);
@@ -349,7 +215,7 @@ public class AggExpressionUtil {
                 aggExpression.topProjInput = newArray(1);
                 aggExpression.topAggInput = newArray(1);
             } else if (isSum(call.getAggregation().getKind()) && 
sourceInput.length == 0) {
-                aggExpression.setSumConst();
+                aggExpression.setSumConst(true);
                 aggExpression.bottomProjInput = newArray(0);
                 aggExpression.bottomAggInput = newArray(1);
                 aggExpression.topProjInput = newArray(1);
@@ -365,7 +231,7 @@ public class AggExpressionUtil {
     }
 
     private static List<RexNode> extractCaseWhenConditions(RexNode 
caseWhenExpr) {
-        assertCondition(caseWhenExpr instanceof RexCall, caseWhenExpr + " is 
not a case-when expression");
+        checkConditionExpr(caseWhenExpr);
         RexCall caseWhenCall = (RexCall) caseWhenExpr;
 
         List<RexNode> conditions = Lists.newArrayList();
@@ -378,9 +244,9 @@ public class AggExpressionUtil {
         return conditions;
     }
 
-    private static List<RexNode> extractCaseThenElseValues(RexNode 
caseWhenExpr) {
-        assertCondition(caseWhenExpr instanceof RexCall, caseWhenExpr + " is 
not a case-when expression");
-        RexCall caseWhenCall = (RexCall) caseWhenExpr;
+    private static List<RexNode> extractCaseThenElseValues(RexNode 
conditionExpr) {
+        checkConditionExpr(conditionExpr);
+        RexCall caseWhenCall = (RexCall) conditionExpr;
 
         List<RexNode> values = Lists.newArrayList();
         int operandsCnt = caseWhenCall.getOperands().size();
@@ -390,14 +256,14 @@ public class AggExpressionUtil {
             values.add(thenRexNode);
         }
 
-        RexNode elseRexNode = ((RexCall) 
caseWhenExpr).getOperands().get(operandsCnt - 1);
+        RexNode elseRexNode = ((RexCall) 
conditionExpr).getOperands().get(operandsCnt - 1);
         values.add(elseRexNode);
         return values;
     }
 
     public static class CollectRexVisitor extends RexVisitorImpl<RexCall> {
-        private Set<RexInputRef> rexInputRefs = Sets.newHashSet();
-        private Set<RexLiteral> rexLiterals = Sets.newHashSet();
+        private final Set<RexInputRef> rexInputRefs = Sets.newHashSet();
+        private final Set<RexLiteral> rexLiterals = Sets.newHashSet();
 
         CollectRexVisitor(boolean deep) {
             super(deep);
@@ -430,7 +296,7 @@ public class AggExpressionUtil {
             projectExpr.accept(visitor);
             GroupExpression groupExpr = new GroupExpression();
             if (visitor.isRexLiteral()) {
-                groupExpr.setLiteral();
+                groupExpr.setLiteral(true);
             }
             int[] sourceInput = 
RelOptUtil.InputFinder.bits(projectExpr).toArray();
             groupExpr.expression = projectExpr;
@@ -464,30 +330,15 @@ public class AggExpressionUtil {
         return Optional.of(ImmutableList.copyOf(groupSets));
     }
 
-    public static class InputRefCapacity extends RexVisitorImpl<RexCall> {
-
-        Set<RexInputRef> rexInputRefs = Sets.newHashSet();
-
-        protected InputRefCapacity(boolean deep) {
-            super(deep);
-        }
-
-        @Override
-        public RexCall visitInputRef(RexInputRef inputRef) {
-            rexInputRefs.add(inputRef);
-            return null;
-        }
-
-        public List<RexInputRef> getRexInputRef() {
-            return Lists.newArrayList(rexInputRefs);
-        }
-    }
-
     public static void assertCondition(boolean condition, String errorMsg) {
         if (!condition)
             throw new SumExprUnSupportException(errorMsg);
     }
 
+    private static void checkConditionExpr(RexNode conditionExpr) {
+        assertCondition(conditionExpr instanceof RexCall, conditionExpr + " is 
not a case-when or if expression");
+    }
+
     public static int[] generateAdjustments(int[] src, int[] dst) {
         AggExpressionUtil.assertCondition(src.length == dst.length, "Failed to 
generate adjustments");
         int maxRange = Arrays.stream(src).max().orElse(0);

Reply via email to