Java Code Examples for org.apache.kylin.metadata.model.FunctionDesc#isCount()

The following examples show how to use org.apache.kylin.metadata.model.FunctionDesc#isCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TopNMeasureType.java    From kylin with Apache License 2.0 6 votes vote down vote up
private boolean isTopNCompatibleSum(FunctionDesc topN, FunctionDesc sum) {
    if (sum == null)
        return false;

    if (!isTopN(topN))
        return false;

    TblColRef topnNumCol = getTopNNumericColumn(topN);

    if (topnNumCol == null) {
        if (sum.isCount())
            return true;

        return false;
    }

    if (sum.isSum() == false)
        return false;

    if (sum.getParameter() == null || sum.getParameter().getColRefs() == null
            || sum.getParameter().getColRefs().size() == 0)
        return false;

    TblColRef sumCol = sum.getParameter().getColRefs().get(0);
    return sumCol.equals(topnNumCol);
}
 
Example 2
Source File: FunctionRule.java    From Kylin with Apache License 2.0 6 votes vote down vote up
private void validateReturnType(ValidateContext context, CubeDesc cube, FunctionDesc funcDesc) {

        String func = funcDesc.getExpression();
        DataType rtype = funcDesc.getReturnDataType();

        if (funcDesc.isCount()) {
            if (rtype.isIntegerFamily() == false) {
                context.addResult(ResultLevel.ERROR, "Return type for function " + func + " must be one of " + DataType.INTEGER_FAMILY);
            }
        } else if (funcDesc.isCountDistinct()) {
            if (rtype.isHLLC() == false && funcDesc.isHolisticCountDistinct() == false) {
                context.addResult(ResultLevel.ERROR, "Return type for function " + func + " must be hllc(10), hllc(12) etc.");
            }
        } else if (funcDesc.isMax() || funcDesc.isMin() || funcDesc.isSum()) {
            if (rtype.isNumberFamily() == false) {
                context.addResult(ResultLevel.ERROR, "Return type for function " + func + " must be one of " + DataType.NUMBER_FAMILY);
            }
        } else {
            if (StringUtils.equalsIgnoreCase(KylinConfig.getInstanceFromEnv().getProperty(KEY_IGNORE_UNKNOWN_FUNC, "false"), "false")) {
                context.addResult(ResultLevel.ERROR, "Unrecognized function: [" + func + "]");
            }
        }

    }
 
Example 3
Source File: CubeCapabilityChecker.java    From Kylin with Apache License 2.0 6 votes vote down vote up
private static boolean isWeaklyMatchedWithAggregations(Collection<FunctionDesc> aggregations, Collection<TblColRef> metricColumns, CubeInstance cube) {
    CubeDesc cubeDesc = cube.getDescriptor();
    Collection<FunctionDesc> cubeFuncs = cubeDesc.listAllFunctions();

    boolean matched = true;
    for (FunctionDesc functionDesc : aggregations) {
        if (cubeFuncs.contains(functionDesc))
            continue;

        // only inverted-index cube does not have count, and let calcite handle in this case
        if (functionDesc.isCount())
            continue;

        if (functionDesc.isCountDistinct()) // calcite can not handle distinct count
            matched = false;

        TblColRef col = functionDesc.selectTblColRef(metricColumns, cubeDesc.getFactTable());
        if (col == null || !cubeDesc.listDimensionColumnsIncludingDerived().contains(col)) {
            matched = false;
        }
    }
    return matched;
}
 
Example 4
Source File: OLAPAggregateRel.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
FunctionDesc findInMeasures(FunctionDesc aggFunc, List<MeasureDesc> measures) {
    for (MeasureDesc m : measures) {
        if (aggFunc.equals(m.getFunction())) {
            return m.getFunction();
        }
    }

    // no count(col) measure found, use count(1) to replace it.
    if (aggFunc.isCount()) {
        FunctionDesc func = findCountConstantFunc(measures);
        if (func != null)
            return func;
    }

    return aggFunc;
}
 
Example 5
Source File: OLAPAggregateRel.java    From kylin with Apache License 2.0 6 votes vote down vote up
FunctionDesc findInMeasures(FunctionDesc aggFunc, List<MeasureDesc> measures) {
    for (MeasureDesc m : measures) {
        if (aggFunc.equals(m.getFunction())) {
            return m.getFunction();
        }
    }

    // no count(col) measure found, use count(1) to replace it.
    if (aggFunc.isCount()) {
        FunctionDesc func = findCountConstantFunc(measures);
        if (func != null)
            return func;
    }

    return aggFunc;
}
 
Example 6
Source File: SegmentMemoryStore.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private Object buildValueOf(int idxOfMeasure, List<String> row) {
    MeasureDesc measure = parsedStreamingCubeInfo.measureDescs[idxOfMeasure];
    FunctionDesc function = measure.getFunction();
    int[] colIdxOnFlatTable = parsedStreamingCubeInfo.intermediateTableDesc.getMeasureColumnIndexes()[idxOfMeasure];

    int paramCount = function.getParameterCount();
    String[] inputToMeasure = new String[paramCount];

    // pick up parameter values
    ParameterDesc param = function.getParameter();
    int paramColIdx = 0; // index among parameters of column type
    for (int i = 0; i < paramCount; i++, param = param.getNextParameter()) {
        String value;
        if (function.isCount()) {
            value = "1";
        } else if (param.isColumnType()) {
            value = row.get(colIdxOnFlatTable[paramColIdx++]);
        } else {
            value = param.getValue();
        }
        inputToMeasure[i] = value;
    }
    return parsedStreamingCubeInfo.measureIngesters[idxOfMeasure].valueOf(inputToMeasure, measure, dictionaryMap);
}
 
Example 7
Source File: TopNMeasureType.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private boolean isTopNCompatibleSum(FunctionDesc topN, FunctionDesc sum) {
    if (sum == null)
        return false;

    if (!isTopN(topN))
        return false;

    TblColRef topnNumCol = getTopNNumericColumn(topN);

    if (topnNumCol == null) {
        if (sum.isCount())
            return true;

        return false;
    }

    if (sum.isSum() == false)
        return false;

    if (sum.getParameter() == null || sum.getParameter().getColRefs() == null
            || sum.getParameter().getColRefs().size() == 0)
        return false;

    TblColRef sumCol = sum.getParameter().getColRefs().get(0);
    return sumCol.equals(topnNumCol);
}
 
Example 8
Source File: KeyValueBuilder.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public String[] buildValueOf(int idxOfMeasure, String[] row) {
    MeasureDesc measure = cubeDesc.getMeasures().get(idxOfMeasure);
    FunctionDesc function = measure.getFunction();
    int[] colIdxOnFlatTable = flatDesc.getMeasureColumnIndexes()[idxOfMeasure];

    int paramCount = function.getParameterCount();
    List<String> inputToMeasure = Lists.newArrayListWithExpectedSize(paramCount);

    // pick up parameter values
    ParameterDesc param = function.getParameter();
    int colParamIdx = 0; // index among parameters of column type
    for (int i = 0; i < paramCount; i++, param = param.getNextParameter()) {
        String value;
        if (param.isColumnType()) {
            value = getCell(colIdxOnFlatTable[colParamIdx++], row);
            if (function.isCount() && value == null) {
                value = ZERO;
            } else if (function.isCount()) {
                value = ONE;
            }
        } else {
            value = param.getValue();
            if (function.isCount()) {
                value = ONE;
            }
        }
        inputToMeasure.add(value);
    }

    return inputToMeasure.toArray(new String[inputToMeasure.size()]);
}
 
Example 9
Source File: CubeCapabilityChecker.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
private static void tryDimensionAsMeasures(Collection<FunctionDesc> unmatchedAggregations, CapabilityResult result,
        Set<TblColRef> dimCols) {

    Iterator<FunctionDesc> it = unmatchedAggregations.iterator();
    while (it.hasNext()) {
        FunctionDesc functionDesc = it.next();

        // let calcite handle count
        if (functionDesc.isCount()) {
            logger.warn("No count measure found for column {}, will use count(1) to replace it, please note that it will count all value(include null value)", functionDesc.getParameter() == null ? "" : functionDesc.getParameter().getColRef().getName());
            it.remove();
            continue;
        }

        // calcite can do aggregation from columns on-the-fly
        ParameterDesc parameterDesc = functionDesc.getParameter();
        if (parameterDesc == null) {
            continue;
        }
        List<TblColRef> neededCols = parameterDesc.getColRefs();
        if (neededCols.size() > 0 && dimCols.containsAll(neededCols)
                && FunctionDesc.BUILT_IN_AGGREGATIONS.contains(functionDesc.getExpression())) {
            result.influences.add(new CapabilityResult.DimensionAsMeasure(functionDesc));
            it.remove();
            continue;
        }
    }
}
 
Example 10
Source File: OLAPAggregateRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private AggregateCall rewriteAggregateCall(AggregateCall aggCall, FunctionDesc func) {
    // rebuild function
    String callName = getSqlFuncName(aggCall);
    RelDataType fieldType = aggCall.getType();
    SqlAggFunction newAgg = aggCall.getAggregation();

    Map<String, Class<?>> udafMap = func.getMeasureType().getRewriteCalciteAggrFunctions();
    if (func.isCount()) {
        newAgg = SqlStdOperatorTable.SUM0;
    } else if (udafMap != null && udafMap.containsKey(callName)) {
        newAgg = createCustomAggFunction(callName, fieldType, udafMap.get(callName));
    }

    // rebuild parameters
    List<Integer> newArgList = Lists.newArrayList(aggCall.getArgList());
    if (udafMap != null && udafMap.containsKey(callName)) {
        newArgList = truncArgList(newArgList, udafMap.get(callName));
    }
    if (func.needRewriteField()) {
        RelDataTypeField field = getInput().getRowType().getField(func.getRewriteFieldName(), true, false);
        if (newArgList.isEmpty()) {
            newArgList.add(field.getIndex());
        } else {
            // TODO: only the first column got overwritten
            newArgList.set(0, field.getIndex());
        }
    }

    // rebuild aggregate call
    AggregateCall newAggCall = new AggregateCall(newAgg, false, newArgList, fieldType, callName);

    return newAggCall;
}
 
Example 11
Source File: CubeCapabilityChecker.java    From kylin with Apache License 2.0 5 votes vote down vote up
private static void tryDimensionAsMeasures(Collection<FunctionDesc> unmatchedAggregations, CapabilityResult result,
        Set<TblColRef> dimCols) {

    Iterator<FunctionDesc> it = unmatchedAggregations.iterator();
    while (it.hasNext()) {
        FunctionDesc functionDesc = it.next();

        // let calcite handle count
        if (functionDesc.isCount()) {
            logger.warn("No count measure found for column {}, will use count(1) to replace it, please note that it will count all value(include null value)", functionDesc.getParameter() == null ? "" : functionDesc.getParameter().getColRef().getName());
            it.remove();
            continue;
        }

        // calcite can do aggregation from columns on-the-fly
        ParameterDesc parameterDesc = functionDesc.getParameter();
        if (parameterDesc == null) {
            continue;
        }

        List<TblColRef> neededCols = functionDesc instanceof ExpressionDynamicFunctionDesc
                ? Lists.newArrayList(ExpressionColCollector
                        .collectColumns(((ExpressionDynamicFunctionDesc) functionDesc).getTupleExpression()))
                : parameterDesc.getColRefs();
        if (neededCols.size() > 0 && dimCols.containsAll(neededCols)
                && FunctionDesc.BUILT_IN_AGGREGATIONS.contains(functionDesc.getExpression())) {
            result.influences.add(new CapabilityResult.DimensionAsMeasure(functionDesc));
            it.remove();
            continue;
        }
    }
}
 
Example 12
Source File: EndpointTupleIterator.java    From Kylin with Apache License 2.0 5 votes vote down vote up
/**
 * measure comes from query engine, does not contain enough information
 *
 * @param measures
 * @param columns
 */
private void rewriteMeasureParameters(List<FunctionDesc> measures, List<TblColRef> columns) {
    for (FunctionDesc functionDesc : measures) {
        if (functionDesc.isCount()) {
            functionDesc.setReturnType("bigint");
            functionDesc.setReturnDataType(DataType.getInstance(functionDesc.getReturnType()));
        } else {
            boolean updated = false;
            for (TblColRef column : columns) {
                if (column.isSameAs(factTableName, functionDesc.getParameter().getValue())) {
                    if (functionDesc.isCountDistinct()) {
                        //TODO: default precision might need be configurable
                        String iiDefaultHLLC = "hllc10";
                        functionDesc.setReturnType(iiDefaultHLLC);
                        functionDesc.setReturnDataType(DataType.getInstance(iiDefaultHLLC));
                    } else {
                        functionDesc.setReturnType(column.getColumn().getType().toString());
                        functionDesc.setReturnDataType(DataType.getInstance(functionDesc.getReturnType()));
                    }
                    functionDesc.getParameter().setColRefs(ImmutableList.of(column));
                    updated = true;
                    break;
                }
            }
            if (!updated) {
                throw new RuntimeException("Func " + functionDesc + " is not related to any column in fact table " + factTableName);
            }
        }
    }
}
 
Example 13
Source File: KeyValueBuilder.java    From kylin with Apache License 2.0 5 votes vote down vote up
public String[] buildValueOf(int idxOfMeasure, String[] row) {
    MeasureDesc measure = cubeDesc.getMeasures().get(idxOfMeasure);
    FunctionDesc function = measure.getFunction();
    int[] colIdxOnFlatTable = flatDesc.getMeasureColumnIndexes()[idxOfMeasure];

    int paramCount = function.getParameterCount();
    List<String> inputToMeasure = Lists.newArrayListWithExpectedSize(paramCount);

    // pick up parameter values
    ParameterDesc param = function.getParameter();
    int colParamIdx = 0; // index among parameters of column type
    for (int i = 0; i < paramCount; i++, param = param.getNextParameter()) {
        String value;
        if (param.isColumnType()) {
            value = getCell(colIdxOnFlatTable[colParamIdx++], row);
            if (function.isCount() && value == null) {
                value = ZERO;
            } else if (function.isCount()) {
                value = ONE;
            }
        } else {
            value = param.getValue();
            if (function.isCount()) {
                value = ONE;
            }
        }
        inputToMeasure.add(value);
    }
    if (BitmapMapMeasureType.DATATYPE_BITMAP_MAP.equalsIgnoreCase(function.getReturnType())) {
        inputToMeasure.add(segmentStartTime);
    }

    return inputToMeasure.toArray(new String[inputToMeasure.size()]);
}
 
Example 14
Source File: EndpointAggregators.java    From Kylin with Apache License 2.0 5 votes vote down vote up
public static EndpointAggregators fromFunctions(TableRecordInfo tableInfo, List<FunctionDesc> metrics) {
    String[] funcNames = new String[metrics.size()];
    String[] dataTypes = new String[metrics.size()];
    MetricInfo[] metricInfos = new MetricInfo[metrics.size()];

    for (int i = 0; i < metrics.size(); i++) {
        FunctionDesc functionDesc = metrics.get(i);

        //TODO: what if funcionDesc's type is different from tablDesc? cause scale difference
        funcNames[i] = functionDesc.getExpression();
        dataTypes[i] = functionDesc.getReturnType();

        if (functionDesc.isCount()) {
            metricInfos[i] = new MetricInfo(MetricType.Count);
        } else if (functionDesc.isDimensionAsMetric()) {
            metricInfos[i] = new MetricInfo(MetricType.DimensionAsMetric);
        } else {
            int index = tableInfo.findFactTableColumn(functionDesc.getParameter().getValue());
            if (index < 0) {
                throw new IllegalStateException("Column " + functionDesc.getParameter().getValue() + " is not found in II");
            }

            if (functionDesc.isCountDistinct()) {
                metricInfos[i] = new MetricInfo(MetricType.DistinctCount, index, functionDesc.getReturnDataType().getPrecision());
            } else {
                metricInfos[i] = new MetricInfo(MetricType.Normal, index);
            }
        }
    }

    return new EndpointAggregators(funcNames, dataTypes, metricInfos, tableInfo.getDigest());
}
 
Example 15
Source File: BaseCuboidMapper.java    From Kylin with Apache License 2.0 5 votes vote down vote up
private byte[] getValueBytes(SplittedBytes[] splitBuffers, int measureIdx) {
    MeasureDesc desc = cubeDesc.getMeasures().get(measureIdx);
    FunctionDesc func = desc.getFunction();
    ParameterDesc paramDesc = func.getParameter();
    int[] flatTableIdx = intermediateTableDesc.getMeasureColumnIndexes()[measureIdx];

    byte[] result = null;

    // constant
    if (flatTableIdx == null) {
        result = Bytes.toBytes(paramDesc.getValue());
    }
    // column values
    else {
        // for multiple columns, their values are joined
        for (int i = 0; i < flatTableIdx.length; i++) {
            SplittedBytes split = splitBuffers[flatTableIdx[i]];
            if (result == null) {
                result = Arrays.copyOf(split.value, split.length);
            } else {
                byte[] newResult = new byte[result.length + split.length];
                System.arraycopy(result, 0, newResult, 0, result.length);
                System.arraycopy(split.value, 0, newResult, result.length, split.length);
                result = newResult;
            }
        }
    }

    if (func.isCount() || func.isHolisticCountDistinct()) {
        // note for holistic count distinct, this value will be ignored
        result = ONE;
    }

    if (isNull(result)) {
        result = null;
    }

    return result;
}
 
Example 16
Source File: OLAPAggregateRel.java    From Kylin with Apache License 2.0 5 votes vote down vote up
private AggregateCall rewriteAggregateCall(AggregateCall aggCall, FunctionDesc func) {

        // rebuild parameters
        List<Integer> newArgList = new ArrayList<Integer>(1);
        String fieldName = func.getRewriteFieldName();
        RelDataTypeField field = getChild().getRowType().getField(fieldName, true);
        newArgList.add(field.getIndex());

        // rebuild function
        RelDataType fieldType = aggCall.getType();
        Aggregation newAgg = aggCall.getAggregation();
        if (func.isCountDistinct()) {
            newAgg = createHyperLogLogAggFunction(fieldType);
        } else if (func.isCount()) {
            newAgg = new SqlSumEmptyIsZeroAggFunction(fieldType);
        }

        // rebuild aggregate call
        AggregateCall newAggCall = new AggregateCall(newAgg, false, newArgList, fieldType, newAgg.getName());

        // To make sure specified type matches the inferReturnType, or otherwise
        // there will be assertion failure in optiq
        // The problem is BIGINT != BIGINT NOT NULL
        // Details see https://github.scm.corp.ebay.com/Kylin/Kylin/issues/323
        SqlAggFunction aggFunction = (SqlAggFunction) newAggCall.getAggregation();
        AggCallBinding callBinding = newAggCall.createBinding(this);
        RelDataType inferReturnType = aggFunction.inferReturnType(callBinding);

        return new AggregateCall(newAgg, false, newArgList, inferReturnType, newAgg.getName());
    }
 
Example 17
Source File: OLAPAggregateRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private AggregateCall rewriteAggregateCall(AggregateCall aggCall, FunctionDesc func) {
    // rebuild function
    String callName = getSqlFuncName(aggCall);
    RelDataType fieldType = aggCall.getType();
    SqlAggFunction newAgg = aggCall.getAggregation();

    Map<String, Class<?>> udafMap = func.getMeasureType().getRewriteCalciteAggrFunctions();
    if (func.isCount()) {
        newAgg = SqlStdOperatorTable.SUM0;
    } else if (udafMap != null && udafMap.containsKey(callName)) {
        newAgg = createCustomAggFunction(callName, fieldType, udafMap.get(callName));
    }

    // rebuild parameters
    List<Integer> newArgList = Lists.newArrayList(aggCall.getArgList());
    if (udafMap != null && udafMap.containsKey(callName)) {
        newArgList = truncArgList(newArgList, udafMap.get(callName));
    }
    if (func.needRewriteField()) {
        RelDataTypeField field = getInput().getRowType().getField(func.getRewriteFieldName(), true, false);
        if (newArgList.isEmpty()) {
            newArgList.add(field.getIndex());
        } else {
            // TODO: only the first column got overwritten
            newArgList.set(0, field.getIndex());
        }
    }

    // rebuild aggregate call
    AggregateCall newAggCall = new AggregateCall(newAgg, false, newArgList, fieldType, callName);

    return newAggCall;
}
 
Example 18
Source File: FunctionRule.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public void validate(CubeDesc cube, ValidateContext context) {
    List<MeasureDesc> measures = cube.getMeasures();

    if (validateMeasureNamesDuplicated(measures, context)) {
        return;
    }

    List<FunctionDesc> countStarFuncs = new ArrayList<FunctionDesc>();

    Iterator<MeasureDesc> it = measures.iterator();
    while (it.hasNext()) {
        MeasureDesc measure = it.next();
        FunctionDesc func = measure.getFunction();
        ParameterDesc parameter = func.getParameter();
        if (parameter == null) {
            context.addResult(ResultLevel.ERROR, "Must define parameter for function " + func.getExpression() + " in " + measure.getName());
            return;
        }

        String type = func.getParameter().getType();
        String value = func.getParameter().getValue();
        if (StringUtils.isEmpty(type)) {
            context.addResult(ResultLevel.ERROR, "Must define type for parameter type " + func.getExpression() + " in " + measure.getName());
            return;
        }
        if (StringUtils.isEmpty(value)) {
            context.addResult(ResultLevel.ERROR, "Must define type for parameter value " + func.getExpression() + " in " + measure.getName());
            return;
        }
        if (StringUtils.isEmpty(func.getReturnType())) {
            context.addResult(ResultLevel.ERROR, "Must define return type for function " + func.getExpression() + " in " + measure.getName());
            return;
        }

        if (StringUtils.equalsIgnoreCase(FunctionDesc.PARAMETER_TYPE_COLUMN, type)) {
            validateColumnParameter(context, cube, value);
        } else if (StringUtils.equals(FunctionDesc.PARAMETER_TYPE_CONSTANT, type)) {
            validateCostantParameter(context, cube, value);
        }

        try {
            func.getMeasureType().validate(func);
        } catch (IllegalArgumentException ex) {
            context.addResult(ResultLevel.ERROR, ex.getMessage());
        }

        if (func.isCount() && func.getParameter().isConstant())
            countStarFuncs.add(func);

        if (TopNMeasureType.FUNC_TOP_N.equalsIgnoreCase(func.getExpression())) {
            if (parameter.getNextParameter() == null) {
                context.addResult(ResultLevel.ERROR, "Must define at least 2 parameters for function " + func.getExpression() + " in " + measure.getName());
                return;
            }

            ParameterDesc groupByCol = parameter.getNextParameter();
            List<String> duplicatedCol = Lists.newArrayList();
            while (groupByCol != null) {
                String embeded_groupby = groupByCol.getValue();
                for (DimensionDesc dimensionDesc : cube.getDimensions()) {
                    if (dimensionDesc.getColumn() != null && dimensionDesc.getColumn().equalsIgnoreCase(embeded_groupby)) {
                        duplicatedCol.add(embeded_groupby);
                    }
                }
                groupByCol = groupByCol.getNextParameter();
            }

        }
    }


    if (countStarFuncs.size() != 1) {
        context.addResult(ResultLevel.ERROR, "Must define one and only one count(1) function, but there are "
                + countStarFuncs.size() + " -- " + countStarFuncs);
    }
}
 
Example 19
Source File: TopNMeasureType.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public void adjustSqlDigest(List<MeasureDesc> measureDescs, SQLDigest sqlDigest) {
    // If sqlDiegest is already adjusted, then not to adjust it again.
    if (sqlDigest.isBorrowedContext) {
        return;
    }

    if (sqlDigest.aggregations.size() > 1) {
        return;
    }

    for (MeasureDesc measureDesc : measureDescs) {
        if (!sqlDigest.involvedMeasure.contains(measureDesc)) {
            continue;
        }
        FunctionDesc topnFunc = measureDesc.getFunction();
        List<TblColRef> topnLiteralCol = getTopNLiteralColumn(topnFunc);

        if (sqlDigest.groupbyColumns.containsAll(topnLiteralCol) == false) {
            continue;
        }

        if (sqlDigest.aggregations.size() > 0) {
            FunctionDesc origFunc = sqlDigest.aggregations.iterator().next();
            if (origFunc.isSum() == false && origFunc.isCount() == false) {
                logger.warn("When query with topN, only SUM/Count function is allowed.");
                return;
            }

            if (isTopNCompatibleSum(measureDesc.getFunction(), origFunc) == false) {
                continue;
            }

            // topN not totally match, but have cuboid can answer, not use topN to adjust
            // topN totally match or (topN fuzzy match, but no cuboid can answer), use topN to adjust
            if (!totallyMatchTopN(sqlDigest) && cuboidCanAnswer) {
                continue;
            }

            logger.info("Rewrite function " + origFunc + " to " + topnFunc);
        }


        sqlDigest.aggregations = Lists.newArrayList(topnFunc);
        sqlDigest.groupbyColumns.removeAll(topnLiteralCol);
        sqlDigest.metricColumns.addAll(topnLiteralCol);
        break;
    }
}
 
Example 20
Source File: TopNMeasureType.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Override
public void adjustSqlDigest(List<MeasureDesc> measureDescs, SQLDigest sqlDigest) {
    // If sqlDiegest is already adjusted, then not to adjust it again.
    if (sqlDigest.isBorrowedContext) {
        return;
    }

    if (sqlDigest.aggregations.size() > 1) {
        return;
    }

    for (MeasureDesc measureDesc : measureDescs) {
        if (!sqlDigest.involvedMeasure.contains(measureDesc)) {
            continue;
        }
        FunctionDesc topnFunc = measureDesc.getFunction();
        List<TblColRef> topnLiteralCol = getTopNLiteralColumn(topnFunc);

        if (sqlDigest.groupbyColumns.containsAll(topnLiteralCol) == false) {
            continue;
        }

        if (sqlDigest.aggregations.size() > 0) {
            FunctionDesc origFunc = sqlDigest.aggregations.iterator().next();
            if (origFunc.isSum() == false && origFunc.isCount() == false) {
                logger.warn("When query with topN, only SUM/Count function is allowed.");
                return;
            }

            if (isTopNCompatibleSum(measureDesc.getFunction(), origFunc) == false) {
                continue;
            }

            // topN not totally match, but have cuboid can answer, not use topN to adjust
            // topN totally match or (topN fuzzy match, but no cuboid can answer), use topN to adjust
            if (!totallyMatchTopN(sqlDigest) && cuboidCanAnswer) {
                continue;
            }

            logger.info("Rewrite function " + origFunc + " to " + topnFunc);
        }


        sqlDigest.aggregations = Lists.newArrayList(topnFunc);
        sqlDigest.groupbyColumns.removeAll(topnLiteralCol);
        sqlDigest.metricColumns.addAll(topnLiteralCol);
        break;
    }
}