Search in sources :

Example 16 with TypeInference

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

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 17 with TypeInference

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

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 18 with TypeInference

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

the class FunctionCatalogOperatorTable method convertToBridgingSqlFunction.

private Optional<SqlFunction> convertToBridgingSqlFunction(@Nullable SqlFunctionCategory category, ContextResolvedFunction resolvedFunction) {
    final FunctionDefinition definition = resolvedFunction.getDefinition();
    if (!verifyFunctionKind(category, resolvedFunction)) {
        return Optional.empty();
    }
    final TypeInference typeInference;
    try {
        typeInference = definition.getTypeInference(dataTypeFactory);
    } catch (Throwable t) {
        throw new ValidationException(String.format("An error occurred in the type inference logic of function '%s'.", resolvedFunction), t);
    }
    if (typeInference.getOutputTypeStrategy() == TypeStrategies.MISSING) {
        return Optional.empty();
    }
    final SqlFunction function;
    if (definition.getKind() == FunctionKind.AGGREGATE || definition.getKind() == FunctionKind.TABLE_AGGREGATE) {
        function = BridgingSqlAggFunction.of(dataTypeFactory, typeFactory, SqlKind.OTHER_FUNCTION, resolvedFunction, typeInference);
    } else {
        function = BridgingSqlFunction.of(dataTypeFactory, typeFactory, SqlKind.OTHER_FUNCTION, resolvedFunction, typeInference);
    }
    return Optional.of(function);
}
Also used : TypeInference(org.apache.flink.table.types.inference.TypeInference) ValidationException(org.apache.flink.table.api.ValidationException) BuiltInFunctionDefinition(org.apache.flink.table.functions.BuiltInFunctionDefinition) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableFunctionDefinition(org.apache.flink.table.functions.TableFunctionDefinition) ScalarFunctionDefinition(org.apache.flink.table.functions.ScalarFunctionDefinition) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) HiveAggSqlFunction(org.apache.flink.table.planner.functions.utils.HiveAggSqlFunction) SqlFunction(org.apache.calcite.sql.SqlFunction) BridgingSqlFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction) HiveTableSqlFunction(org.apache.flink.table.planner.functions.utils.HiveTableSqlFunction)

Example 19 with TypeInference

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

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)

Example 20 with TypeInference

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

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)

Aggregations

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