Search in sources :

Example 6 with UserDefinedFunction

use of org.apache.flink.table.functions.UserDefinedFunction in project flink by apache.

the class BatchPhysicalPythonWindowAggregateRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    FlinkLogicalWindowAggregate agg = call.rel(0);
    RelNode input = agg.getInput();
    LogicalWindow window = agg.getWindow();
    if (!(window instanceof TumblingGroupWindow && AggregateUtil.hasTimeIntervalType(((TumblingGroupWindow) window).size()) || window instanceof SlidingGroupWindow && AggregateUtil.hasTimeIntervalType(((SlidingGroupWindow) window).size()) || window instanceof SessionGroupWindow)) {
        // sliding & tumbling count window and session window not supported
        throw new TableException("Window " + window + " is not supported right now.");
    }
    int[] groupSet = agg.getGroupSet().toArray();
    RelTraitSet traitSet = agg.getTraitSet().replace(FlinkConventions.BATCH_PHYSICAL());
    Tuple2<int[], Seq<AggregateCall>> auxGroupSetAndCallsTuple = AggregateUtil.checkAndSplitAggCalls(agg);
    int[] auxGroupSet = auxGroupSetAndCallsTuple._1;
    Seq<AggregateCall> aggCallsWithoutAuxGroupCalls = auxGroupSetAndCallsTuple._2;
    Tuple3<int[][], DataType[][], UserDefinedFunction[]> aggBufferTypesAndFunctions = AggregateUtil.transformToBatchAggregateFunctions(FlinkTypeFactory.toLogicalRowType(input.getRowType()), aggCallsWithoutAuxGroupCalls, null);
    UserDefinedFunction[] aggFunctions = aggBufferTypesAndFunctions._3();
    int inputTimeFieldIndex = AggregateUtil.timeFieldIndex(input.getRowType(), call.builder(), window.timeAttribute());
    RelDataType inputTimeFieldType = input.getRowType().getFieldList().get(inputTimeFieldIndex).getType();
    boolean inputTimeIsDate = inputTimeFieldType.getSqlTypeName() == SqlTypeName.DATE;
    RelTraitSet requiredTraitSet = agg.getTraitSet().replace(FlinkConventions.BATCH_PHYSICAL());
    if (groupSet.length != 0) {
        FlinkRelDistribution requiredDistribution = FlinkRelDistribution.hash(groupSet, false);
        requiredTraitSet = requiredTraitSet.replace(requiredDistribution);
    } else {
        requiredTraitSet = requiredTraitSet.replace(FlinkRelDistribution.SINGLETON());
    }
    RelCollation sortCollation = createRelCollation(groupSet, inputTimeFieldIndex);
    requiredTraitSet = requiredTraitSet.replace(sortCollation);
    RelNode newInput = RelOptRule.convert(input, requiredTraitSet);
    BatchPhysicalPythonGroupWindowAggregate windowAgg = new BatchPhysicalPythonGroupWindowAggregate(agg.getCluster(), traitSet, newInput, agg.getRowType(), newInput.getRowType(), groupSet, auxGroupSet, aggCallsWithoutAuxGroupCalls, aggFunctions, window, inputTimeFieldIndex, inputTimeIsDate, agg.getNamedProperties());
    call.transformTo(windowAgg);
}
Also used : TableException(org.apache.flink.table.api.TableException) UserDefinedFunction(org.apache.flink.table.functions.UserDefinedFunction) BatchPhysicalPythonGroupWindowAggregate(org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalPythonGroupWindowAggregate) RelDataType(org.apache.calcite.rel.type.RelDataType) RelTraitSet(org.apache.calcite.plan.RelTraitSet) AggregateCall(org.apache.calcite.rel.core.AggregateCall) FlinkRelDistribution(org.apache.flink.table.planner.plan.trait.FlinkRelDistribution) RelCollation(org.apache.calcite.rel.RelCollation) LogicalWindow(org.apache.flink.table.planner.plan.logical.LogicalWindow) TumblingGroupWindow(org.apache.flink.table.planner.plan.logical.TumblingGroupWindow) RelNode(org.apache.calcite.rel.RelNode) FlinkLogicalWindowAggregate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalWindowAggregate) DataType(org.apache.flink.table.types.DataType) RelDataType(org.apache.calcite.rel.type.RelDataType) SlidingGroupWindow(org.apache.flink.table.planner.plan.logical.SlidingGroupWindow) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow) Seq(scala.collection.Seq)

Example 7 with UserDefinedFunction

use of org.apache.flink.table.functions.UserDefinedFunction in project flink by apache.

the class LookupJoinUtil method getLookupFunction.

/**
 * Gets LookupFunction from temporal table according to the given lookup keys.
 */
public static UserDefinedFunction getLookupFunction(RelOptTable temporalTable, Collection<Integer> lookupKeys) {
    int[] lookupKeyIndicesInOrder = getOrderedLookupKeys(lookupKeys);
    if (temporalTable instanceof TableSourceTable) {
        // TODO: support nested lookup keys in the future,
        // currently we only support top-level lookup keys
        int[][] indices = IntStream.of(lookupKeyIndicesInOrder).mapToObj(i -> new int[] { i }).toArray(int[][]::new);
        LookupTableSource tableSource = (LookupTableSource) ((TableSourceTable) temporalTable).tableSource();
        LookupRuntimeProviderContext providerContext = new LookupRuntimeProviderContext(indices);
        LookupTableSource.LookupRuntimeProvider provider = tableSource.getLookupRuntimeProvider(providerContext);
        if (provider instanceof TableFunctionProvider) {
            return ((TableFunctionProvider<?>) provider).createTableFunction();
        } else if (provider instanceof AsyncTableFunctionProvider) {
            return ((AsyncTableFunctionProvider<?>) provider).createAsyncTableFunction();
        }
    }
    if (temporalTable instanceof LegacyTableSourceTable) {
        String[] lookupFieldNamesInOrder = IntStream.of(lookupKeyIndicesInOrder).mapToObj(temporalTable.getRowType().getFieldNames()::get).toArray(String[]::new);
        LegacyTableSourceTable<?> legacyTableSourceTable = (LegacyTableSourceTable<?>) temporalTable;
        LookupableTableSource<?> tableSource = (LookupableTableSource<?>) legacyTableSourceTable.tableSource();
        if (tableSource.isAsyncEnabled()) {
            return tableSource.getAsyncLookupFunction(lookupFieldNamesInOrder);
        } else {
            return tableSource.getLookupFunction(lookupFieldNamesInOrder);
        }
    }
    throw new TableException(String.format("table %s is neither TableSourceTable not LegacyTableSourceTable", temporalTable.getQualifiedName()));
}
Also used : IntStream(java.util.stream.IntStream) AsyncTableFunctionProvider(org.apache.flink.table.connector.source.AsyncTableFunctionProvider) JsonCreator(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonCreator) LegacyTableSourceTable(org.apache.flink.table.planner.plan.schema.LegacyTableSourceTable) JsonSubTypes(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonSubTypes) RexLiteral(org.apache.calcite.rex.RexLiteral) Collection(java.util.Collection) TableException(org.apache.flink.table.api.TableException) UserDefinedFunction(org.apache.flink.table.functions.UserDefinedFunction) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) LookupableTableSource(org.apache.flink.table.sources.LookupableTableSource) JsonProperty(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty) RelOptTable(org.apache.calcite.plan.RelOptTable) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) LookupTableSource(org.apache.flink.table.connector.source.LookupTableSource) LogicalType(org.apache.flink.table.types.logical.LogicalType) JsonTypeInfo(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonTypeInfo) Internal(org.apache.flink.annotation.Internal) TableFunctionProvider(org.apache.flink.table.connector.source.TableFunctionProvider) JsonIgnoreProperties(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonIgnoreProperties) LookupRuntimeProviderContext(org.apache.flink.table.runtime.connector.source.LookupRuntimeProviderContext) JsonTypeName(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonTypeName) TableException(org.apache.flink.table.api.TableException) LegacyTableSourceTable(org.apache.flink.table.planner.plan.schema.LegacyTableSourceTable) AsyncTableFunctionProvider(org.apache.flink.table.connector.source.AsyncTableFunctionProvider) AsyncTableFunctionProvider(org.apache.flink.table.connector.source.AsyncTableFunctionProvider) TableFunctionProvider(org.apache.flink.table.connector.source.TableFunctionProvider) LookupableTableSource(org.apache.flink.table.sources.LookupableTableSource) LookupRuntimeProviderContext(org.apache.flink.table.runtime.connector.source.LookupRuntimeProviderContext) LookupTableSource(org.apache.flink.table.connector.source.LookupTableSource) LegacyTableSourceTable(org.apache.flink.table.planner.plan.schema.LegacyTableSourceTable) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable)

Example 8 with UserDefinedFunction

use of org.apache.flink.table.functions.UserDefinedFunction 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

UserDefinedFunction (org.apache.flink.table.functions.UserDefinedFunction)8 TableException (org.apache.flink.table.api.TableException)3 ArrayList (java.util.ArrayList)2 RelOptTable (org.apache.calcite.plan.RelOptTable)2 RelTraitSet (org.apache.calcite.plan.RelTraitSet)2 RelCollation (org.apache.calcite.rel.RelCollation)2 RelNode (org.apache.calcite.rel.RelNode)2 AggregateCall (org.apache.calcite.rel.core.AggregateCall)2 FlinkRelDistribution (org.apache.flink.table.planner.plan.trait.FlinkRelDistribution)2 DataType (org.apache.flink.table.types.DataType)2 Seq (scala.collection.Seq)2 Collection (java.util.Collection)1 List (java.util.List)1 Objects (java.util.Objects)1 IntStream (java.util.stream.IntStream)1 ByteString (org.apache.calcite.avatica.util.ByteString)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RexLiteral (org.apache.calcite.rex.RexLiteral)1 DateString (org.apache.calcite.util.DateString)1 TimeString (org.apache.calcite.util.TimeString)1