Search in sources :

Example 1 with AggregateInfo

use of org.apache.flink.table.planner.plan.utils.AggregateInfo 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)

Example 2 with AggregateInfo

use of org.apache.flink.table.planner.plan.utils.AggregateInfo in project flink by apache.

the class CommonPythonUtil method extractPythonAggregateFunctionInfos.

public static Tuple2<PythonAggregateFunctionInfo[], DataViewSpec[][]> extractPythonAggregateFunctionInfos(AggregateInfoList pythonAggregateInfoList, AggregateCall[] aggCalls) {
    List<PythonAggregateFunctionInfo> pythonAggregateFunctionInfoList = new ArrayList<>();
    List<DataViewSpec[]> dataViewSpecList = new ArrayList<>();
    AggregateInfo[] aggInfos = pythonAggregateInfoList.aggInfos();
    for (int i = 0; i < aggInfos.length; i++) {
        AggregateInfo aggInfo = aggInfos[i];
        UserDefinedFunction function = aggInfo.function();
        if (function instanceof PythonFunction) {
            pythonAggregateFunctionInfoList.add(new PythonAggregateFunctionInfo((PythonFunction) function, Arrays.stream(aggInfo.argIndexes()).boxed().toArray(), aggCalls[i].filterArg, aggCalls[i].isDistinct()));
            TypeInference typeInference = function.getTypeInference(null);
            dataViewSpecList.add(extractDataViewSpecs(i, typeInference.getAccumulatorTypeStrategy().get().inferType(null).get()));
        } else {
            int filterArg = -1;
            boolean distinct = false;
            if (i < aggCalls.length) {
                filterArg = aggCalls[i].filterArg;
                distinct = aggCalls[i].isDistinct();
            }
            pythonAggregateFunctionInfoList.add(new PythonAggregateFunctionInfo(getBuiltInPythonAggregateFunction(function), Arrays.stream(aggInfo.argIndexes()).boxed().toArray(), filterArg, distinct));
            // The data views of the built in Python Aggregate Function are different from Java
            // side, we will create the spec at Python side.
            dataViewSpecList.add(new DataViewSpec[0]);
        }
    }
    return Tuple2.of(pythonAggregateFunctionInfoList.toArray(new PythonAggregateFunctionInfo[0]), dataViewSpecList.toArray(new DataViewSpec[0][0]));
}
Also used : TypeInference(org.apache.flink.table.types.inference.TypeInference) PythonAggregateFunctionInfo(org.apache.flink.table.functions.python.PythonAggregateFunctionInfo) UserDefinedFunction(org.apache.flink.table.functions.UserDefinedFunction) DataViewSpec(org.apache.flink.table.runtime.dataview.DataViewSpec) ArrayList(java.util.ArrayList) PythonFunction(org.apache.flink.table.functions.python.PythonFunction) AggregateInfo(org.apache.flink.table.planner.plan.utils.AggregateInfo)

Aggregations

ArrayList (java.util.ArrayList)2 AggregateInfo (org.apache.flink.table.planner.plan.utils.AggregateInfo)2 AggregateExpression (org.apache.flink.table.expressions.AggregateExpression)1 FieldReferenceExpression (org.apache.flink.table.expressions.FieldReferenceExpression)1 UserDefinedFunction (org.apache.flink.table.functions.UserDefinedFunction)1 PythonAggregateFunctionInfo (org.apache.flink.table.functions.python.PythonAggregateFunctionInfo)1 PythonFunction (org.apache.flink.table.functions.python.PythonFunction)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 AggregateInfoList (org.apache.flink.table.planner.plan.utils.AggregateInfoList)1 DataViewSpec (org.apache.flink.table.runtime.dataview.DataViewSpec)1 DataType (org.apache.flink.table.types.DataType)1 TypeInference (org.apache.flink.table.types.inference.TypeInference)1