Search in sources :

Example 76 with LogicalType

use of org.apache.flink.table.types.logical.LogicalType in project flink by apache.

the class MappingTypeStrategy method inferType.

@Override
public Optional<DataType> inferType(CallContext callContext) {
    final List<LogicalType> actualTypes = callContext.getArgumentDataTypes().stream().map(DataType::getLogicalType).collect(Collectors.toList());
    for (Map.Entry<InputTypeStrategy, TypeStrategy> mapping : mappings.entrySet()) {
        final InputTypeStrategy inputStrategy = mapping.getKey();
        final TypeStrategy strategy = mapping.getValue();
        if (!inputStrategy.getArgumentCount().isValidCount(actualTypes.size())) {
            continue;
        }
        final Optional<List<DataType>> inferredDataTypes = inputStrategy.inferInputTypes(callContext, false);
        if (!inferredDataTypes.isPresent()) {
            continue;
        }
        final List<LogicalType> inferredTypes = inferredDataTypes.get().stream().map(DataType::getLogicalType).collect(Collectors.toList());
        if (supportsAvoidingCast(actualTypes, inferredTypes)) {
            return strategy.inferType(callContext);
        }
    }
    return Optional.empty();
}
Also used : InputTypeStrategy(org.apache.flink.table.types.inference.InputTypeStrategy) InputTypeStrategy(org.apache.flink.table.types.inference.InputTypeStrategy) TypeStrategy(org.apache.flink.table.types.inference.TypeStrategy) LogicalType(org.apache.flink.table.types.logical.LogicalType) List(java.util.List) Map(java.util.Map)

Example 77 with LogicalType

use of org.apache.flink.table.types.logical.LogicalType 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 78 with LogicalType

use of org.apache.flink.table.types.logical.LogicalType in project flink by apache.

the class RootArgumentTypeStrategy 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();
    if (Objects.equals(expectedNullability, Boolean.FALSE) && actualType.isNullable()) {
        if (throwOnFailure) {
            throw callContext.newValidationError("Unsupported argument type. Expected nullable type of root '%s' but actual type was '%s'.", expectedRoot, actualType);
        }
        return Optional.empty();
    }
    return findDataType(callContext, throwOnFailure, actualDataType, expectedRoot, expectedNullability);
}
Also used : DataType(org.apache.flink.table.types.DataType) StrategyUtils.findDataType(org.apache.flink.table.types.inference.strategies.StrategyUtils.findDataType) LogicalType(org.apache.flink.table.types.logical.LogicalType)

Example 79 with LogicalType

use of org.apache.flink.table.types.logical.LogicalType in project flink by apache.

the class StringConcatTypeStrategy method inferType.

@Override
public Optional<DataType> inferType(CallContext callContext) {
    final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
    final LogicalType type1 = argumentDataTypes.get(0).getLogicalType();
    final LogicalType type2 = argumentDataTypes.get(1).getLogicalType();
    int length = getLength(type1) + getLength(type2);
    // handle overflow
    if (length < 0) {
        length = CharType.MAX_LENGTH;
    }
    final LogicalType minimumType;
    if (type1.is(LogicalTypeFamily.CHARACTER_STRING) || type2.is(LogicalTypeFamily.CHARACTER_STRING)) {
        minimumType = new CharType(false, length);
    } else if (type1.is(LogicalTypeFamily.BINARY_STRING) || type2.is(LogicalTypeFamily.BINARY_STRING)) {
        minimumType = new BinaryType(false, length);
    } else {
        return Optional.empty();
    }
    // deal with nullability handling and varying semantics
    return findCommonType(Arrays.asList(type1, type2, minimumType)).map(TypeConversions::fromLogicalToDataType);
}
Also used : BinaryType(org.apache.flink.table.types.logical.BinaryType) TypeConversions(org.apache.flink.table.types.utils.TypeConversions) DataType(org.apache.flink.table.types.DataType) LogicalType(org.apache.flink.table.types.logical.LogicalType) CharType(org.apache.flink.table.types.logical.CharType)

Example 80 with LogicalType

use of org.apache.flink.table.types.logical.LogicalType in project flink by apache.

the class DataTypeConversionClassTransformation method transform.

@Override
public DataType transform(DataType dataType) {
    LogicalType logicalType = dataType.getLogicalType();
    Class<?> conversionClass = conversions.get(logicalType.getTypeRoot());
    if (conversionClass != null) {
        return dataType.bridgedTo(conversionClass);
    } else {
        return dataType;
    }
}
Also used : LogicalType(org.apache.flink.table.types.logical.LogicalType)

Aggregations

LogicalType (org.apache.flink.table.types.logical.LogicalType)192 DataType (org.apache.flink.table.types.DataType)53 RowType (org.apache.flink.table.types.logical.RowType)53 RowData (org.apache.flink.table.data.RowData)45 List (java.util.List)29 ArrayList (java.util.ArrayList)28 TableException (org.apache.flink.table.api.TableException)25 TimestampType (org.apache.flink.table.types.logical.TimestampType)25 Internal (org.apache.flink.annotation.Internal)21 IntType (org.apache.flink.table.types.logical.IntType)21 Map (java.util.Map)20 ValidationException (org.apache.flink.table.api.ValidationException)20 ArrayType (org.apache.flink.table.types.logical.ArrayType)19 DecimalType (org.apache.flink.table.types.logical.DecimalType)19 LocalZonedTimestampType (org.apache.flink.table.types.logical.LocalZonedTimestampType)17 Test (org.junit.Test)17 BigIntType (org.apache.flink.table.types.logical.BigIntType)16 LegacyTypeInformationType (org.apache.flink.table.types.logical.LegacyTypeInformationType)16 GenericRowData (org.apache.flink.table.data.GenericRowData)15 Arrays (java.util.Arrays)14