From 3c03a6b94fc8709b1665a04e281e898bc53c07ee Mon Sep 17 00:00:00 2001 From: lidongsjtu Date: Sun, 18 Oct 2015 11:21:13 +0800 Subject: [PATCH] KYLIN-1075 select [MeasureCol] from [FactTbl] is not supported --- .../kylin/query/enumerator/OLAPEnumerator.java | 61 ++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/query/src/main/java/org/apache/kylin/query/enumerator/OLAPEnumerator.java b/query/src/main/java/org/apache/kylin/query/enumerator/OLAPEnumerator.java index 38883f0..2f94d61 100644 --- a/query/src/main/java/org/apache/kylin/query/enumerator/OLAPEnumerator.java +++ b/query/src/main/java/org/apache/kylin/query/enumerator/OLAPEnumerator.java @@ -29,6 +29,11 @@ import org.apache.kylin.metadata.filter.CompareTupleFilter; import org.apache.kylin.metadata.filter.TimeConditionLiteralsReplacer; import org.apache.kylin.metadata.filter.TupleFilter; import org.apache.kylin.metadata.filter.TupleFilterSerializer; +import org.apache.kylin.metadata.model.FunctionDesc; +import org.apache.kylin.metadata.model.MeasureDesc; +import org.apache.kylin.metadata.model.ParameterDesc; +import org.apache.kylin.metadata.model.TblColRef; +import org.apache.kylin.metadata.realization.SQLDigest; import org.apache.kylin.metadata.tuple.ITuple; import org.apache.kylin.metadata.tuple.ITupleIterator; import org.apache.kylin.query.relnode.OLAPContext; @@ -110,6 +115,9 @@ public class OLAPEnumerator implements Enumerator { olapContext.filter = modifyTimeLiterals(olapContext.filter); olapContext.resetSQLDigest(); + // hack simple select query for better result + hackNoGroupByAggregation(); + // query storage engine IStorageQuery storageEngine = StorageFactory.createQuery(olapContext.realization); ITupleIterator iterator = storageEngine.search(olapContext.storageContext, olapContext.getSQLDigest(), olapContext.returnTupleInfo); @@ -160,4 +168,57 @@ public class OLAPEnumerator implements Enumerator { int threshold = Integer.valueOf(propThreshold); olapContext.storageContext.setThreshold(threshold); } + + // Hack for better results + private void hackNoGroupByAggregation() { + SQLDigest sqlDigest = olapContext.getSQLDigest(); + + // If no group by and metric found, then it's simple query like select ... from ... where ..., + // But we have no raw data stored, in order to return better results, we hack to output sum of metric data, + if (sqlDigest.groupbyColumns.isEmpty() && sqlDigest.metricColumns.isEmpty()) { + logger.warn("No group by and aggregation found in this query, will hack some result for better look of output..."); + + // If it's select * from ..., + // We need to retrieve cube to manually add columns into sqlDigest, + // So that we have full-columns results as output. + boolean isSelectAll = sqlDigest.allColumns.isEmpty() || sqlDigest.allColumns.equals(sqlDigest.filterColumns); + for (TblColRef cubeColRef : olapContext.realization.getAllColumns()) { + if (cubeColRef.getTable().equals(sqlDigest.factTable) && (olapContext.realization.getAllDimensions().contains(cubeColRef) || isSelectAll)) { + sqlDigest.allColumns.add(cubeColRef); + } + } + + for (TblColRef colRef : sqlDigest.allColumns) { + // For dimension columns, take them as group by columns. + if (olapContext.realization.getAllDimensions().contains(colRef)) { + sqlDigest.groupbyColumns.add(colRef); + } + // For measure columns, take them as metric columns with aggregation function SUM(). + else { + ParameterDesc colParameter = new ParameterDesc(); + colParameter.setType("column"); + colParameter.setValue(colRef.getName()); + FunctionDesc sumFunc = new FunctionDesc(); + sumFunc.setExpression("SUM"); + sumFunc.setParameter(colParameter); + + boolean measureHasSum = false; + for (MeasureDesc colMeasureDesc : olapContext.realization.getMeasures()) { + if (colMeasureDesc.getFunction().equals(sumFunc)) { + measureHasSum = true; + break; + } + } + if (measureHasSum) { + sqlDigest.aggregations.add(sumFunc); + } + else { + logger.warn("SUM is not defined for measure column " + colRef + ", output will be meaningless."); + } + + sqlDigest.metricColumns.add(colRef); + } + } + } + } } -- 2.3.8 (Apple Git-58)