Search in sources :

Example 1 with Sum0AggFunction

use of org.apache.flink.table.planner.functions.aggfunctions.Sum0AggFunction in project flink by apache.

the class AggregatePushDownSpec method buildAggregateExpressions.

private static List<AggregateExpression> buildAggregateExpressions(RowType inputType, List<AggregateCall> aggregateCalls) {
    AggregateInfoList aggInfoList = AggregateUtil.transformToBatchAggregateInfoList(inputType, JavaScalaConversionUtil.toScala(aggregateCalls), null, null);
    if (aggInfoList.aggInfos().length == 0) {
        // no agg function need to be pushed down
        return Collections.emptyList();
    }
    List<AggregateExpression> aggExpressions = new ArrayList<>();
    for (AggregateInfo aggInfo : aggInfoList.aggInfos()) {
        List<FieldReferenceExpression> arguments = new ArrayList<>(1);
        for (int argIndex : aggInfo.argIndexes()) {
            DataType argType = TypeConversions.fromLogicalToDataType(inputType.getFields().get(argIndex).getType());
            FieldReferenceExpression field = new FieldReferenceExpression(inputType.getFieldNames().get(argIndex), argType, 0, argIndex);
            arguments.add(field);
        }
        if (aggInfo.function() instanceof AvgAggFunction) {
            Tuple2<Sum0AggFunction, CountAggFunction> sum0AndCountFunction = AggregateUtil.deriveSumAndCountFromAvg((AvgAggFunction) aggInfo.function());
            AggregateExpression sum0Expression = new AggregateExpression(sum0AndCountFunction._1(), arguments, null, aggInfo.externalResultType(), aggInfo.agg().isDistinct(), aggInfo.agg().isApproximate(), aggInfo.agg().ignoreNulls());
            aggExpressions.add(sum0Expression);
            AggregateExpression countExpression = new AggregateExpression(sum0AndCountFunction._2(), arguments, null, aggInfo.externalResultType(), aggInfo.agg().isDistinct(), aggInfo.agg().isApproximate(), aggInfo.agg().ignoreNulls());
            aggExpressions.add(countExpression);
        } else {
            AggregateExpression aggregateExpression = new AggregateExpression(aggInfo.function(), arguments, null, aggInfo.externalResultType(), aggInfo.agg().isDistinct(), aggInfo.agg().isApproximate(), aggInfo.agg().ignoreNulls());
            aggExpressions.add(aggregateExpression);
        }
    }
    return aggExpressions;
}
Also used : AggregateInfoList(org.apache.flink.table.planner.plan.utils.AggregateInfoList) CountAggFunction(org.apache.flink.table.planner.functions.aggfunctions.CountAggFunction) ArrayList(java.util.ArrayList) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) AggregateExpression(org.apache.flink.table.expressions.AggregateExpression) AvgAggFunction(org.apache.flink.table.planner.functions.aggfunctions.AvgAggFunction) DataType(org.apache.flink.table.types.DataType) AggregateInfo(org.apache.flink.table.planner.plan.utils.AggregateInfo) Sum0AggFunction(org.apache.flink.table.planner.functions.aggfunctions.Sum0AggFunction)

Aggregations

ArrayList (java.util.ArrayList)1 AggregateExpression (org.apache.flink.table.expressions.AggregateExpression)1 FieldReferenceExpression (org.apache.flink.table.expressions.FieldReferenceExpression)1 AvgAggFunction (org.apache.flink.table.planner.functions.aggfunctions.AvgAggFunction)1 CountAggFunction (org.apache.flink.table.planner.functions.aggfunctions.CountAggFunction)1 Sum0AggFunction (org.apache.flink.table.planner.functions.aggfunctions.Sum0AggFunction)1 AggregateInfo (org.apache.flink.table.planner.plan.utils.AggregateInfo)1 AggregateInfoList (org.apache.flink.table.planner.plan.utils.AggregateInfoList)1 DataType (org.apache.flink.table.types.DataType)1