Search in sources :

Example 1 with TypeInference

use of org.apache.flink.table.types.inference.TypeInference in project flink by apache.

the class SqlAggFunctionVisitor method createSqlAggFunction.

private SqlAggFunction createSqlAggFunction(CallExpression call) {
    final FunctionDefinition definition = call.getFunctionDefinition();
    // legacy
    if (definition instanceof AggregateFunctionDefinition) {
        return createLegacySqlAggregateFunction(call.getFunctionIdentifier().orElse(null), (AggregateFunctionDefinition) definition);
    } else if (definition instanceof TableAggregateFunctionDefinition) {
        return createLegacySqlTableAggregateFunction(call.getFunctionIdentifier().orElse(null), (TableAggregateFunctionDefinition) definition);
    }
    // new stack
    final DataTypeFactory dataTypeFactory = ShortcutUtils.unwrapContext(relBuilder).getCatalogManager().getDataTypeFactory();
    final TypeInference typeInference = definition.getTypeInference(dataTypeFactory);
    return BridgingSqlAggFunction.of(dataTypeFactory, ShortcutUtils.unwrapTypeFactory(relBuilder), SqlKind.OTHER_FUNCTION, ContextResolvedFunction.fromCallExpression(call), typeInference);
}
Also used : TypeInference(org.apache.flink.table.types.inference.TypeInference) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory)

Example 2 with TypeInference

use of org.apache.flink.table.types.inference.TypeInference in project flink by apache.

the class FunctionDefinitionConvertRule method convert.

@Override
public Optional<RexNode> convert(CallExpression call, ConvertContext context) {
    final FunctionDefinition definition = call.getFunctionDefinition();
    // built-in functions without implementation are handled separately
    if (definition instanceof BuiltInFunctionDefinition) {
        final BuiltInFunctionDefinition builtInFunction = (BuiltInFunctionDefinition) definition;
        if (!builtInFunction.hasRuntimeImplementation()) {
            return Optional.empty();
        }
    }
    final TypeInference typeInference = definition.getTypeInference(context.getDataTypeFactory());
    if (typeInference.getOutputTypeStrategy() == TypeStrategies.MISSING) {
        return Optional.empty();
    }
    switch(definition.getKind()) {
        case SCALAR:
        case TABLE:
            final List<RexNode> args = call.getChildren().stream().map(context::toRexNode).collect(Collectors.toList());
            final BridgingSqlFunction sqlFunction = BridgingSqlFunction.of(context.getDataTypeFactory(), context.getTypeFactory(), SqlKind.OTHER_FUNCTION, ContextResolvedFunction.fromCallExpression(call), typeInference);
            return Optional.of(context.getRelBuilder().call(sqlFunction, args));
        default:
            return Optional.empty();
    }
}
Also used : TypeInference(org.apache.flink.table.types.inference.TypeInference) BuiltInFunctionDefinition(org.apache.flink.table.functions.BuiltInFunctionDefinition) BridgingSqlFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction) BuiltInFunctionDefinition(org.apache.flink.table.functions.BuiltInFunctionDefinition) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) RexNode(org.apache.calcite.rex.RexNode)

Example 3 with TypeInference

use of org.apache.flink.table.types.inference.TypeInference in project flink by apache.

the class LastDatedValueFunction method getTypeInference.

// --------------------------------------------------------------------------------------------
// Planning
// --------------------------------------------------------------------------------------------
/**
 * Declares the {@link TypeInference} of this function. It specifies:
 *
 * <ul>
 *   <li>which argument types are supported when calling this function,
 *   <li>which {@link DataType#getConversionClass()} should be used when calling the JVM method
 *       {@link #accumulate(Accumulator, Object, LocalDate)} during runtime,
 *   <li>a similar strategy how to derive an accumulator type,
 *   <li>and a similar strategy how to derive the output type.
 * </ul>
 */
@Override
public TypeInference getTypeInference(DataTypeFactory typeFactory) {
    return TypeInference.newBuilder().inputTypeStrategy(InputTypeStrategies.sequence(InputTypeStrategies.ANY, InputTypeStrategies.explicit(DataTypes.DATE()))).accumulatorTypeStrategy(callContext -> {
        final DataType argDataType = callContext.getArgumentDataTypes().get(0);
        final DataType accDataType = DataTypes.STRUCTURED(Accumulator.class, DataTypes.FIELD("value", argDataType), DataTypes.FIELD("date", DataTypes.DATE()));
        return Optional.of(accDataType);
    }).outputTypeStrategy(callContext -> {
        final DataType argDataType = callContext.getArgumentDataTypes().get(0);
        final DataType outputDataType = DataTypes.ROW(DataTypes.FIELD("value", argDataType), DataTypes.FIELD("date", DataTypes.DATE()));
        return Optional.of(outputDataType);
    }).build();
}
Also used : DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory) TypeInference(org.apache.flink.table.types.inference.TypeInference) DataType(org.apache.flink.table.types.DataType) LocalDate(java.time.LocalDate) InputTypeStrategies(org.apache.flink.table.types.inference.InputTypeStrategies) Optional(java.util.Optional) DataTypes(org.apache.flink.table.api.DataTypes) AggregateFunction(org.apache.flink.table.functions.AggregateFunction) Row(org.apache.flink.types.Row) DataType(org.apache.flink.table.types.DataType)

Example 4 with TypeInference

use of org.apache.flink.table.types.inference.TypeInference in project flink by apache.

the class BridgingSqlAggFunction method of.

/**
 * Creates an instance of a aggregate function during translation.
 */
public static BridgingSqlAggFunction of(FlinkContext context, FlinkTypeFactory typeFactory, ContextResolvedFunction resolvedFunction) {
    final DataTypeFactory dataTypeFactory = context.getCatalogManager().getDataTypeFactory();
    final TypeInference typeInference = resolvedFunction.getDefinition().getTypeInference(dataTypeFactory);
    return of(dataTypeFactory, typeFactory, SqlKind.OTHER_FUNCTION, resolvedFunction, typeInference);
}
Also used : TypeInference(org.apache.flink.table.types.inference.TypeInference) BridgingUtils.createSqlReturnTypeInference(org.apache.flink.table.planner.functions.bridging.BridgingUtils.createSqlReturnTypeInference) BridgingUtils.createSqlOperandTypeInference(org.apache.flink.table.planner.functions.bridging.BridgingUtils.createSqlOperandTypeInference) DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory)

Example 5 with TypeInference

use of org.apache.flink.table.types.inference.TypeInference in project flink by apache.

the class BridgingSqlFunction method of.

/**
 * Creates an instance of a scalar or table function during translation.
 */
public static BridgingSqlFunction of(FlinkContext context, FlinkTypeFactory typeFactory, ContextResolvedFunction resolvedFunction) {
    final DataTypeFactory dataTypeFactory = context.getCatalogManager().getDataTypeFactory();
    final TypeInference typeInference = resolvedFunction.getDefinition().getTypeInference(dataTypeFactory);
    return of(dataTypeFactory, typeFactory, SqlKind.OTHER_FUNCTION, resolvedFunction, typeInference);
}
Also used : TypeInference(org.apache.flink.table.types.inference.TypeInference) BridgingUtils.createSqlReturnTypeInference(org.apache.flink.table.planner.functions.bridging.BridgingUtils.createSqlReturnTypeInference) BridgingUtils.createSqlOperandTypeInference(org.apache.flink.table.planner.functions.bridging.BridgingUtils.createSqlOperandTypeInference) DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory)

Aggregations

TypeInference (org.apache.flink.table.types.inference.TypeInference)7 DataTypeFactory (org.apache.flink.table.catalog.DataTypeFactory)4 FunctionDefinition (org.apache.flink.table.functions.FunctionDefinition)3 AggregateFunctionDefinition (org.apache.flink.table.functions.AggregateFunctionDefinition)2 BuiltInFunctionDefinition (org.apache.flink.table.functions.BuiltInFunctionDefinition)2 BridgingSqlFunction (org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction)2 BridgingUtils.createSqlOperandTypeInference (org.apache.flink.table.planner.functions.bridging.BridgingUtils.createSqlOperandTypeInference)2 BridgingUtils.createSqlReturnTypeInference (org.apache.flink.table.planner.functions.bridging.BridgingUtils.createSqlReturnTypeInference)2 LocalDate (java.time.LocalDate)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 RexNode (org.apache.calcite.rex.RexNode)1 SqlFunction (org.apache.calcite.sql.SqlFunction)1 DataTypes (org.apache.flink.table.api.DataTypes)1 ValidationException (org.apache.flink.table.api.ValidationException)1 AggregateFunction (org.apache.flink.table.functions.AggregateFunction)1 ScalarFunctionDefinition (org.apache.flink.table.functions.ScalarFunctionDefinition)1 TableAggregateFunctionDefinition (org.apache.flink.table.functions.TableAggregateFunctionDefinition)1 TableFunctionDefinition (org.apache.flink.table.functions.TableFunctionDefinition)1 UserDefinedFunction (org.apache.flink.table.functions.UserDefinedFunction)1