From 30c6934c46002f62396fdbd62a47bba6a1bbabab Mon Sep 17 00:00:00 2001 From: etherge Date: Sun, 19 Feb 2017 16:49:08 +0800 Subject: [PATCH] KYLIN-2341 support sum(case.. when..) --- .../test/resources/query/sql_casewhen/query04.sql | 30 ++++++++++++++++++++++ .../kylin/query/relnode/OLAPAggregateRel.java | 2 +- .../apache/kylin/query/relnode/OLAPProjectRel.java | 13 ++++++++-- 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 kylin-it/src/test/resources/query/sql_casewhen/query04.sql diff --git a/kylin-it/src/test/resources/query/sql_casewhen/query04.sql b/kylin-it/src/test/resources/query/sql_casewhen/query04.sql new file mode 100644 index 0000000..646da0a --- /dev/null +++ b/kylin-it/src/test/resources/query/sql_casewhen/query04.sql @@ -0,0 +1,30 @@ +-- +-- 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 + SUM(CASE + WHEN lstg_format_name LIKE 'Other%' THEN price + ELSE 0 + END) AS gmv +FROM + test_kylin_fact + INNER JOIN + edw.test_cal_dt AS test_cal_dt ON test_kylin_fact.cal_dt = test_cal_dt.cal_dt + INNER JOIN + 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 \ No newline at end of file diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java index 8d7c597..08b0621 100644 --- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java +++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java @@ -260,7 +260,7 @@ public class OLAPAggregateRel extends Aggregate implements OLAPRel { implementor.visitChild(this, getInput()); // only rewrite the innermost aggregation - if (!this.afterAggregate) { + if (!this.afterAggregate && this.context.aggregations.size() > 0) { // rewrite the aggCalls this.rewriteAggCalls = new ArrayList(aggCalls.size()); for (int i = 0; i < this.aggCalls.size(); i++) { diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPProjectRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPProjectRel.java index 03b9ddd..6bab757 100644 --- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPProjectRel.java +++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPProjectRel.java @@ -37,9 +37,9 @@ import org.apache.calcite.rel.RelNode; import org.apache.calcite.rel.core.Project; import org.apache.calcite.rel.metadata.RelMetadataQuery; import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeFactory.FieldInfoBuilder; import org.apache.calcite.rel.type.RelDataTypeField; import org.apache.calcite.rel.type.RelDataTypeFieldImpl; -import org.apache.calcite.rel.type.RelDataTypeFactory.FieldInfoBuilder; import org.apache.calcite.rex.RexCall; import org.apache.calcite.rex.RexInputRef; import org.apache.calcite.rex.RexLiteral; @@ -201,12 +201,21 @@ public class OLAPProjectRel extends Project implements OLAPRel { return translateFirstRexInputRef(call, inputColumnRowType, fieldName, sourceCollector); } } else if (operator instanceof SqlCaseOperator) { + this.context.afterAggregate = true; + TblColRef column = null; for (RexNode operand : call.getOperands()) { if (operand instanceof RexInputRef) { RexInputRef inputRef = (RexInputRef) operand; - return translateRexInputRef(inputRef, inputColumnRowType, fieldName, sourceCollector); + column = translateRexInputRef(inputRef, inputColumnRowType, fieldName, sourceCollector); + } else if (operand instanceof RexLiteral) { + RexLiteral literal = (RexLiteral) operand; + translateRexLiteral(literal); + } else if (operand instanceof RexCall) { + translateRexCall((RexCall) operand, inputColumnRowType, fieldName, sourceCollector); } } + if (column != null) + return column; } for (RexNode operand : call.getOperands()) { -- 2.9.3 (Apple Git-75)