Search in sources :

Example 1 with AggSqlFunction

use of org.apache.flink.table.planner.functions.utils.AggSqlFunction in project flink by apache.

the class CommonPythonUtil method extractPythonAggregateFunctionInfosFromAggregateCall.

public static Tuple2<int[], PythonFunctionInfo[]> extractPythonAggregateFunctionInfosFromAggregateCall(AggregateCall[] aggCalls) {
    Map<Integer, Integer> inputNodes = new LinkedHashMap<>();
    List<PythonFunctionInfo> pythonFunctionInfos = new ArrayList<>();
    for (AggregateCall aggregateCall : aggCalls) {
        List<Integer> inputs = new ArrayList<>();
        List<Integer> argList = aggregateCall.getArgList();
        for (Integer arg : argList) {
            if (inputNodes.containsKey(arg)) {
                inputs.add(inputNodes.get(arg));
            } else {
                Integer inputOffset = inputNodes.size();
                inputs.add(inputOffset);
                inputNodes.put(arg, inputOffset);
            }
        }
        PythonFunction pythonFunction = null;
        SqlAggFunction aggregateFunction = aggregateCall.getAggregation();
        if (aggregateFunction instanceof AggSqlFunction) {
            pythonFunction = (PythonFunction) ((AggSqlFunction) aggregateFunction).aggregateFunction();
        } else if (aggregateFunction instanceof BridgingSqlAggFunction) {
            pythonFunction = (PythonFunction) ((BridgingSqlAggFunction) aggregateFunction).getDefinition();
        }
        PythonFunctionInfo pythonFunctionInfo = new PythonAggregateFunctionInfo(pythonFunction, inputs.toArray(), aggregateCall.filterArg, aggregateCall.isDistinct());
        pythonFunctionInfos.add(pythonFunctionInfo);
    }
    int[] udafInputOffsets = inputNodes.keySet().stream().mapToInt(i -> i).toArray();
    return Tuple2.of(udafInputOffsets, pythonFunctionInfos.toArray(new PythonFunctionInfo[0]));
}
Also used : TypeInference(org.apache.flink.table.types.inference.TypeInference) MapView(org.apache.flink.table.api.dataview.MapView) SumAggFunction(org.apache.flink.table.planner.functions.aggfunctions.SumAggFunction) DataType(org.apache.flink.table.types.DataType) Sum0AggFunction(org.apache.flink.table.planner.functions.aggfunctions.Sum0AggFunction) Arrays(java.util.Arrays) Tuple2(org.apache.flink.api.java.tuple.Tuple2) DataViewSpec(org.apache.flink.table.runtime.dataview.DataViewSpec) StructuredType(org.apache.flink.table.types.logical.StructuredType) LastValueWithRetractAggFunction(org.apache.flink.table.runtime.functions.aggregate.LastValueWithRetractAggFunction) ListViewSpec(org.apache.flink.table.runtime.dataview.ListViewSpec) CountAggFunction(org.apache.flink.table.planner.functions.aggfunctions.CountAggFunction) BigDecimal(java.math.BigDecimal) PythonFunction(org.apache.flink.table.functions.python.PythonFunction) RexNode(org.apache.calcite.rex.RexNode) MaxAggFunction(org.apache.flink.table.planner.functions.aggfunctions.MaxAggFunction) Map(java.util.Map) FirstValueAggFunction(org.apache.flink.table.runtime.functions.aggregate.FirstValueAggFunction) Method(java.lang.reflect.Method) TableSqlFunction(org.apache.flink.table.planner.functions.utils.TableSqlFunction) BuiltInPythonAggregateFunction(org.apache.flink.table.functions.python.BuiltInPythonAggregateFunction) TableConfig(org.apache.flink.table.api.TableConfig) ListAggWsWithRetractAggFunction(org.apache.flink.table.runtime.functions.aggregate.ListAggWsWithRetractAggFunction) RexLiteral(org.apache.calcite.rex.RexLiteral) AggregateInfoList(org.apache.flink.table.planner.plan.utils.AggregateInfoList) MapViewSpec(org.apache.flink.table.runtime.dataview.MapViewSpec) UserDefinedFunction(org.apache.flink.table.functions.UserDefinedFunction) Count1AggFunction(org.apache.flink.table.planner.functions.aggfunctions.Count1AggFunction) FirstValueWithRetractAggFunction(org.apache.flink.table.runtime.functions.aggregate.FirstValueWithRetractAggFunction) InvocationTargetException(java.lang.reflect.InvocationTargetException) Objects(java.util.Objects) AggSqlFunction(org.apache.flink.table.planner.functions.utils.AggSqlFunction) List(java.util.List) ListAggFunction(org.apache.flink.table.planner.functions.aggfunctions.ListAggFunction) BridgingSqlAggFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlAggFunction) LogicalType(org.apache.flink.table.types.logical.LogicalType) PythonAggregateFunctionInfo(org.apache.flink.table.functions.python.PythonAggregateFunctionInfo) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) RexCall(org.apache.calcite.rex.RexCall) IntStream(java.util.stream.IntStream) MinAggFunction(org.apache.flink.table.planner.functions.aggfunctions.MinAggFunction) ListAggWithRetractAggFunction(org.apache.flink.table.runtime.functions.aggregate.ListAggWithRetractAggFunction) DummyStreamExecutionEnvironment(org.apache.flink.table.planner.utils.DummyStreamExecutionEnvironment) MinWithRetractAggFunction(org.apache.flink.table.runtime.functions.aggregate.MinWithRetractAggFunction) RowType(org.apache.flink.table.types.logical.RowType) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) DataView(org.apache.flink.table.api.dataview.DataView) FieldsDataType(org.apache.flink.table.types.FieldsDataType) ConfigOption(org.apache.flink.configuration.ConfigOption) SqlOperator(org.apache.calcite.sql.SqlOperator) ListView(org.apache.flink.table.api.dataview.ListView) AggregateInfo(org.apache.flink.table.planner.plan.utils.AggregateInfo) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) AvgAggFunction(org.apache.flink.table.planner.functions.aggfunctions.AvgAggFunction) MaxWithRetractAggFunction(org.apache.flink.table.runtime.functions.aggregate.MaxWithRetractAggFunction) Configuration(org.apache.flink.configuration.Configuration) TableException(org.apache.flink.table.api.TableException) SumWithRetractAggFunction(org.apache.flink.table.planner.functions.aggfunctions.SumWithRetractAggFunction) PythonFunctionInfo(org.apache.flink.table.functions.python.PythonFunctionInfo) Field(java.lang.reflect.Field) LastValueAggFunction(org.apache.flink.table.runtime.functions.aggregate.LastValueAggFunction) BridgingSqlFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction) ScalarSqlFunction(org.apache.flink.table.planner.functions.utils.ScalarSqlFunction) AggregateCall(org.apache.calcite.rel.core.AggregateCall) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) PythonFunctionInfo(org.apache.flink.table.functions.python.PythonFunctionInfo) ArrayList(java.util.ArrayList) BridgingSqlAggFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlAggFunction) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) BridgingSqlAggFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlAggFunction) LinkedHashMap(java.util.LinkedHashMap) AggregateCall(org.apache.calcite.rel.core.AggregateCall) PythonAggregateFunctionInfo(org.apache.flink.table.functions.python.PythonAggregateFunctionInfo) PythonFunction(org.apache.flink.table.functions.python.PythonFunction) AggSqlFunction(org.apache.flink.table.planner.functions.utils.AggSqlFunction)

Example 2 with AggSqlFunction

use of org.apache.flink.table.planner.functions.utils.AggSqlFunction in project flink by apache.

the class SqlAggFunctionVisitor method createLegacySqlTableAggregateFunction.

private SqlAggFunction createLegacySqlTableAggregateFunction(@Nullable FunctionIdentifier identifier, TableAggregateFunctionDefinition definition) {
    final TableAggregateFunction<?, ?> aggFunc = definition.getTableAggregateFunction();
    final FunctionIdentifier adjustedIdentifier;
    if (identifier != null) {
        adjustedIdentifier = identifier;
    } else {
        adjustedIdentifier = FunctionIdentifier.of(generateInlineFunctionName(aggFunc));
    }
    return new AggSqlFunction(adjustedIdentifier, aggFunc.toString(), aggFunc, fromLegacyInfoToDataType(definition.getResultTypeInfo()), fromLegacyInfoToDataType(definition.getAccumulatorTypeInfo()), ShortcutUtils.unwrapTypeFactory(relBuilder), false, scala.Option.empty());
}
Also used : FunctionIdentifier(org.apache.flink.table.functions.FunctionIdentifier) AggSqlFunction(org.apache.flink.table.planner.functions.utils.AggSqlFunction)

Example 3 with AggSqlFunction

use of org.apache.flink.table.planner.functions.utils.AggSqlFunction in project flink by apache.

the class SqlAggFunctionVisitor method createLegacySqlAggregateFunction.

private SqlAggFunction createLegacySqlAggregateFunction(@Nullable FunctionIdentifier identifier, AggregateFunctionDefinition definition) {
    final AggregateFunction<?, ?> aggFunc = definition.getAggregateFunction();
    final FunctionIdentifier adjustedIdentifier;
    if (identifier != null) {
        adjustedIdentifier = identifier;
    } else {
        adjustedIdentifier = FunctionIdentifier.of(generateInlineFunctionName(aggFunc));
    }
    return new AggSqlFunction(adjustedIdentifier, aggFunc.toString(), aggFunc, fromLegacyInfoToDataType(definition.getResultTypeInfo()), fromLegacyInfoToDataType(definition.getAccumulatorTypeInfo()), ShortcutUtils.unwrapTypeFactory(relBuilder), aggFunc.getRequirements().contains(FunctionRequirement.OVER_WINDOW_ONLY), scala.Option.empty());
}
Also used : FunctionIdentifier(org.apache.flink.table.functions.FunctionIdentifier) AggSqlFunction(org.apache.flink.table.planner.functions.utils.AggSqlFunction)

Example 4 with AggSqlFunction

use of org.apache.flink.table.planner.functions.utils.AggSqlFunction in project flink by apache.

the class RexNodeJsonSerializer method serializeSqlOperator.

// --------------------------------------------------------------------------------------------
/**
 * Logic shared with {@link AggregateCallJsonSerializer}.
 */
static void serializeSqlOperator(SqlOperator operator, JsonGenerator gen, SerializerProvider serializerProvider, boolean serializeCatalogObjects) throws IOException {
    if (operator.getSyntax() != SqlSyntax.FUNCTION) {
        gen.writeStringField(FIELD_NAME_SYNTAX, calciteToSerializable(operator.getSyntax()).getValue());
    }
    if (operator instanceof BridgingSqlFunction) {
        final BridgingSqlFunction function = (BridgingSqlFunction) operator;
        serializeBridgingSqlFunction(function.getName(), function.getResolvedFunction(), gen, serializerProvider, serializeCatalogObjects);
    } else if (operator instanceof BridgingSqlAggFunction) {
        final BridgingSqlAggFunction function = (BridgingSqlAggFunction) operator;
        serializeBridgingSqlFunction(function.getName(), function.getResolvedFunction(), gen, serializerProvider, serializeCatalogObjects);
    } else if (operator instanceof ScalarSqlFunction || operator instanceof TableSqlFunction || operator instanceof AggSqlFunction) {
        throw legacyException(operator.toString());
    } else {
        // We assume that all regular SqlOperators are internal. Only the function definitions
        // stack is exposed to the user and can thus be external.
        gen.writeStringField(FIELD_NAME_INTERNAL_NAME, BuiltInSqlOperator.toQualifiedName(operator));
    }
}
Also used : ScalarSqlFunction(org.apache.flink.table.planner.functions.utils.ScalarSqlFunction) TableSqlFunction(org.apache.flink.table.planner.functions.utils.TableSqlFunction) BridgingSqlFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction) BridgingSqlAggFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlAggFunction) AggSqlFunction(org.apache.flink.table.planner.functions.utils.AggSqlFunction)

Aggregations

AggSqlFunction (org.apache.flink.table.planner.functions.utils.AggSqlFunction)4 FunctionIdentifier (org.apache.flink.table.functions.FunctionIdentifier)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 IntStream (java.util.stream.IntStream)1 AggregateCall (org.apache.calcite.rel.core.AggregateCall)1 RexCall (org.apache.calcite.rex.RexCall)1 RexLiteral (org.apache.calcite.rex.RexLiteral)1 RexNode (org.apache.calcite.rex.RexNode)1 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)1 SqlOperator (org.apache.calcite.sql.SqlOperator)1 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)1