Search in sources :

Example 1 with ArgumentTypeStrategy

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

the class FunctionSignatureTemplate method toInputTypeStrategy.

InputTypeStrategy toInputTypeStrategy() {
    final ArgumentTypeStrategy[] argumentStrategies = argumentTemplates.stream().map(FunctionArgumentTemplate::toArgumentTypeStrategy).toArray(ArgumentTypeStrategy[]::new);
    final InputTypeStrategy strategy;
    if (isVarArgs) {
        if (argumentNames == null) {
            strategy = InputTypeStrategies.varyingSequence(argumentStrategies);
        } else {
            strategy = InputTypeStrategies.varyingSequence(argumentNames, argumentStrategies);
        }
    } else {
        if (argumentNames == null) {
            strategy = InputTypeStrategies.sequence(argumentStrategies);
        } else {
            strategy = InputTypeStrategies.sequence(argumentNames, argumentStrategies);
        }
    }
    return strategy;
}
Also used : InputTypeStrategy(org.apache.flink.table.types.inference.InputTypeStrategy) ArgumentTypeStrategy(org.apache.flink.table.types.inference.ArgumentTypeStrategy)

Example 2 with ArgumentTypeStrategy

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

the class AndArgumentTypeStrategy method inferArgumentType.

@Override
public Optional<DataType> inferArgumentType(CallContext callContext, int argumentPos, boolean throwOnFailure) {
    final DataType actualDataType = callContext.getArgumentDataTypes().get(argumentPos);
    final LogicalType actualType = actualDataType.getLogicalType();
    Optional<DataType> closestDataType = Optional.empty();
    for (ArgumentTypeStrategy strategy : argumentStrategies) {
        final Optional<DataType> inferredDataType = strategy.inferArgumentType(callContext, argumentPos, throwOnFailure);
        // argument type does not match at all
        if (!inferredDataType.isPresent()) {
            return Optional.empty();
        }
        final LogicalType inferredType = inferredDataType.get().getLogicalType();
        // a more specific, casted argument type is available
        if (!supportsAvoidingCast(actualType, inferredType) && !closestDataType.isPresent()) {
            closestDataType = inferredDataType;
        }
    }
    if (closestDataType.isPresent()) {
        return closestDataType;
    }
    return Optional.of(actualDataType);
}
Also used : DataType(org.apache.flink.table.types.DataType) LogicalType(org.apache.flink.table.types.logical.LogicalType) ArgumentTypeStrategy(org.apache.flink.table.types.inference.ArgumentTypeStrategy)

Example 3 with ArgumentTypeStrategy

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

the class OrArgumentTypeStrategy method inferArgumentType.

@Override
public Optional<DataType> inferArgumentType(CallContext callContext, int argumentPos, boolean throwOnFailure) {
    final LogicalType actualType = callContext.getArgumentDataTypes().get(argumentPos).getLogicalType();
    Optional<DataType> closestDataType = Optional.empty();
    for (ArgumentTypeStrategy strategy : argumentStrategies) {
        final Optional<DataType> inferredDataType = strategy.inferArgumentType(callContext, argumentPos, false);
        // argument type does not match at all
        if (!inferredDataType.isPresent()) {
            continue;
        }
        final LogicalType inferredType = inferredDataType.get().getLogicalType();
        // we prefer a strategy that does not require an implicit cast
        if (supportsAvoidingCast(actualType, inferredType)) {
            return inferredDataType;
        } else // argument type requires a more specific, casted type
        if (!closestDataType.isPresent()) {
            closestDataType = inferredDataType;
        }
    }
    if (closestDataType.isPresent()) {
        return closestDataType;
    }
    // generate a helpful exception if possible
    if (throwOnFailure) {
        for (ArgumentTypeStrategy strategy : argumentStrategies) {
            strategy.inferArgumentType(callContext, argumentPos, true);
        }
    }
    return Optional.empty();
}
Also used : LogicalType(org.apache.flink.table.types.logical.LogicalType) DataType(org.apache.flink.table.types.DataType) ArgumentTypeStrategy(org.apache.flink.table.types.inference.ArgumentTypeStrategy)

Example 4 with ArgumentTypeStrategy

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

the class RepeatingSequenceInputTypeStrategy method inferInputTypes.

@Override
public Optional<List<DataType>> inferInputTypes(CallContext callContext, boolean throwOnFailure) {
    final List<DataType> dataTypes = callContext.getArgumentDataTypes();
    final List<DataType> inferredDataTypes = new ArrayList<>(dataTypes.size());
    for (int i = 0; i < callContext.getArgumentDataTypes().size(); i++) {
        final ArgumentTypeStrategy argumentStrategy = argumentStrategies.get(i % argumentStrategies.size());
        final Optional<DataType> inferredDataType = argumentStrategy.inferArgumentType(callContext, i, throwOnFailure);
        if (!inferredDataType.isPresent()) {
            return Optional.empty();
        }
        inferredDataTypes.add(inferredDataType.get());
    }
    return Optional.of(inferredDataTypes);
}
Also used : ArrayList(java.util.ArrayList) DataType(org.apache.flink.table.types.DataType) ArgumentTypeStrategy(org.apache.flink.table.types.inference.ArgumentTypeStrategy)

Example 5 with ArgumentTypeStrategy

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

the class SequenceInputTypeStrategy method inferInputTypes.

@Override
public Optional<List<DataType>> inferInputTypes(CallContext callContext, boolean throwOnFailure) {
    final List<DataType> dataTypes = callContext.getArgumentDataTypes();
    if (dataTypes.size() != argumentStrategies.size()) {
        return Optional.empty();
    }
    final List<DataType> inferredDataTypes = new ArrayList<>(dataTypes.size());
    for (int i = 0; i < argumentStrategies.size(); i++) {
        final ArgumentTypeStrategy argumentTypeStrategy = argumentStrategies.get(i);
        final Optional<DataType> inferredDataType = argumentTypeStrategy.inferArgumentType(callContext, i, throwOnFailure);
        if (!inferredDataType.isPresent()) {
            return Optional.empty();
        }
        inferredDataTypes.add(inferredDataType.get());
    }
    return Optional.of(inferredDataTypes);
}
Also used : ArrayList(java.util.ArrayList) DataType(org.apache.flink.table.types.DataType) ArgumentTypeStrategy(org.apache.flink.table.types.inference.ArgumentTypeStrategy)

Aggregations

ArgumentTypeStrategy (org.apache.flink.table.types.inference.ArgumentTypeStrategy)6 DataType (org.apache.flink.table.types.DataType)5 ArrayList (java.util.ArrayList)3 LogicalType (org.apache.flink.table.types.logical.LogicalType)2 InputTypeStrategy (org.apache.flink.table.types.inference.InputTypeStrategy)1