From b0d4c499bc0297da2ed62ea34c51edea57ff898c Mon Sep 17 00:00:00 2001 From: kyotoYaho Date: Mon, 18 Mar 2019 14:55:38 +0800 Subject: [PATCH 1/3] KYLIN-3812 optimize the child CompareTupleFilter in a CompareTupleFilter --- .../kylin/metadata/filter/CompareTupleFilter.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core-metadata/src/main/java/org/apache/kylin/metadata/filter/CompareTupleFilter.java b/core-metadata/src/main/java/org/apache/kylin/metadata/filter/CompareTupleFilter.java index 1c1c409024d..1d0c7c3c267 100644 --- a/core-metadata/src/main/java/org/apache/kylin/metadata/filter/CompareTupleFilter.java +++ b/core-metadata/src/main/java/org/apache/kylin/metadata/filter/CompareTupleFilter.java @@ -74,6 +74,9 @@ private CompareTupleFilter(CompareTupleFilter another) { @Override public void addChild(TupleFilter child) { + if (child instanceof CompareTupleFilter) { + child = optimizeChildCompareTupleFilter((CompareTupleFilter) child); + } super.addChild(child); if (child instanceof ColumnTupleFilter) { ColumnTupleFilter columnFilter = (ColumnTupleFilter) child; @@ -283,6 +286,18 @@ public TupleFilter acceptOptimizeTransformer(FilterOptimizeTransformer transform return transformer.visit(this); } + private TupleFilter optimizeChildCompareTupleFilter(CompareTupleFilter child) { + FilterOptimizeTransformer transformer = new FilterOptimizeTransformer(); + TupleFilter result = child.acceptOptimizeTransformer(transformer); + if (result == ConstantTupleFilter.TRUE) { + // use string instead of boolean since it's encoded as string + result = new ConstantTupleFilter("true"); + } else if (result == ConstantTupleFilter.FALSE) { + result = new ConstantTupleFilter("false"); + } + return result; + } + @Override public boolean equals(Object o) { if (this == o) From 0f1790488b0c1733d9a214776f2a49744713bc94 Mon Sep 17 00:00:00 2001 From: kyotoYaho Date: Tue, 19 Mar 2019 09:57:09 +0800 Subject: [PATCH 2/3] KYLIN-3813 don't do push down when both of the children of CompareTupleFilter are CompareTupleFilter with column included --- .../kylin/metadata/expression/CaseTupleExpression.java | 3 ++- .../apache/kylin/metadata/filter/CompareTupleFilter.java | 7 ++++++- .../org/apache/kylin/query/relnode/OLAPAggregateRel.java | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/core-metadata/src/main/java/org/apache/kylin/metadata/expression/CaseTupleExpression.java b/core-metadata/src/main/java/org/apache/kylin/metadata/expression/CaseTupleExpression.java index 6b9034b3ccd..a6122e5074e 100644 --- a/core-metadata/src/main/java/org/apache/kylin/metadata/expression/CaseTupleExpression.java +++ b/core-metadata/src/main/java/org/apache/kylin/metadata/expression/CaseTupleExpression.java @@ -47,7 +47,8 @@ public CaseTupleExpression(List> whenList, Tu protected boolean ifAbleToPushDown() { if (ifAbleToPushDown == null) { for (Pair whenEntry : whenList) { - ifAbleToPushDown = whenEntry.getSecond().ifAbleToPushDown(); + ifAbleToPushDown = TupleFilter.isEvaluableRecursively(whenEntry.getFirst()) + && whenEntry.getSecond().ifAbleToPushDown(); if (!ifAbleToPushDown) { break; } diff --git a/core-metadata/src/main/java/org/apache/kylin/metadata/filter/CompareTupleFilter.java b/core-metadata/src/main/java/org/apache/kylin/metadata/filter/CompareTupleFilter.java index 1d0c7c3c267..6ae478f4075 100644 --- a/core-metadata/src/main/java/org/apache/kylin/metadata/filter/CompareTupleFilter.java +++ b/core-metadata/src/main/java/org/apache/kylin/metadata/filter/CompareTupleFilter.java @@ -38,6 +38,9 @@ public enum CompareResultType { AlwaysTrue, AlwaysFalse, Unknown } + // if the two children are both CompareTupleFilter, isNormal will be false + private boolean isNormal = true; + // operand 1 is either a column or a function private TblColRef column; private FunctionTupleFilter function; @@ -237,7 +240,7 @@ private boolean isConstant(TupleFilter filter) { @Override public boolean isEvaluable() { - return (column != null || (function != null && function.isEvaluable())) // + return isNormal && (column != null || (function != null && function.isEvaluable())) // && (!conditionValues.isEmpty() || operator == FilterOperatorEnum.ISNOTNULL || operator == FilterOperatorEnum.ISNULL) // && secondColumn == null; } @@ -294,6 +297,8 @@ private TupleFilter optimizeChildCompareTupleFilter(CompareTupleFilter child) { result = new ConstantTupleFilter("true"); } else if (result == ConstantTupleFilter.FALSE) { result = new ConstantTupleFilter("false"); + } else { + this.isNormal = false; } return result; } 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 ccbd72671c5..aa221786ce3 100755 --- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java +++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java @@ -278,7 +278,7 @@ void buildGroups() { TblColRef groupOutCol = inputColumnRowType.getColumnByIndex(i); if (tupleExpression instanceof ColumnTupleExpression) { this.groups.add(((ColumnTupleExpression) tupleExpression).getColumn()); - } else if (this.context.isDynamicColumnEnabled()) { + } else if (this.context.isDynamicColumnEnabled() && tupleExpression.ifForDynamicColumn()) { Pair, Set> cols = ExpressionColCollector.collectColumnsPair(tupleExpression); // push down only available for the innermost aggregation From 68a76564fc4194909539fc8a5fbdf221b38fa7af Mon Sep 17 00:00:00 2001 From: nichunen Date: Tue, 7 May 2019 16:53:28 +0800 Subject: [PATCH 3/3] KYLIN-3812, add it query test case --- .../resources/query/sql_casewhen/query04.sql | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) 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 00000000000..6ea8d51ecf0 --- /dev/null +++ b/kylin-it/src/test/resources/query/sql_casewhen/query04.sql @@ -0,0 +1,35 @@ +-- +-- 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 "TEST_KYLIN_FACT"."ORDER_ID", + case + when ("TEST_KYLIN_FACT"."LSTG_FORMAT_NAME"='Auction') = (1 = 1) then 'B' + when ("TEST_KYLIN_FACT"."LSTG_FORMAT_NAME"='FP-GTC') = (1 = 1) then 'C' + when ("TEST_KYLIN_FACT"."LSTG_FORMAT_NAME"='ABIN') = (1 = 1) then 'D' + else 'N' + end as phase +from TEST_KYLIN_FACT +where "TEST_KYLIN_FACT"."CAL_DT" between '2013-01-01' and '2013-01-02' +group by "TEST_KYLIN_FACT"."ORDER_ID", + case + when ("TEST_KYLIN_FACT"."LSTG_FORMAT_NAME"='Auction') = (1 = 1) then 'B' + when ("TEST_KYLIN_FACT"."LSTG_FORMAT_NAME"='FP-GTC') = (1 = 1) then 'C' + when ("TEST_KYLIN_FACT"."LSTG_FORMAT_NAME"='ABIN') = (1 = 1) then 'D' + else 'N' + end +