Search in sources :

Example 91 with DataType

use of org.apache.flink.table.types.DataType in project flink by apache.

the class CastInputTypeStrategy method inferInputTypes.

@Override
public Optional<List<DataType>> inferInputTypes(CallContext callContext, boolean throwOnFailure) {
    // check for type literal
    if (!callContext.isArgumentLiteral(1) || !callContext.getArgumentValue(1, DataType.class).isPresent()) {
        return Optional.empty();
    }
    final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
    final LogicalType fromType = argumentDataTypes.get(0).getLogicalType();
    final LogicalType toType = argumentDataTypes.get(1).getLogicalType();
    // A hack to support legacy types. To be removed when we drop the legacy types.
    if (fromType instanceof LegacyTypeInformationType) {
        return Optional.of(argumentDataTypes);
    }
    if (!supportsExplicitCast(fromType, toType)) {
        if (throwOnFailure) {
            throw callContext.newValidationError("Unsupported cast from '%s' to '%s'.", fromType, toType);
        }
        return Optional.empty();
    }
    return Optional.of(argumentDataTypes);
}
Also used : DataType(org.apache.flink.table.types.DataType) LogicalType(org.apache.flink.table.types.logical.LogicalType) LegacyTypeInformationType(org.apache.flink.table.types.logical.LegacyTypeInformationType)

Example 92 with DataType

use of org.apache.flink.table.types.DataType in project flink by apache.

the class TypeInferenceUtil method inferOutputType.

/**
 * Infers an output type using the given {@link TypeStrategy}. It assumes that input arguments
 * have been adapted before if necessary.
 */
public static DataType inferOutputType(CallContext callContext, TypeStrategy outputTypeStrategy) {
    final Optional<DataType> potentialOutputType = outputTypeStrategy.inferType(callContext);
    if (!potentialOutputType.isPresent()) {
        throw new ValidationException("Could not infer an output type for the given arguments.");
    }
    final DataType outputType = potentialOutputType.get();
    if (isUnknown(outputType)) {
        throw new ValidationException("Could not infer an output type for the given arguments. Untyped NULL received.");
    }
    return outputType;
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) DataType(org.apache.flink.table.types.DataType)

Example 93 with DataType

use of org.apache.flink.table.types.DataType in project flink by apache.

the class DataTypeExtractor method extractArrayType.

@Nullable
private DataType extractArrayType(DataTypeTemplate template, List<Type> typeHierarchy, Type type) {
    // prefer BYTES over ARRAY<TINYINT> for byte[]
    if (type == byte[].class) {
        return DataTypes.BYTES();
    } else // for T[]
    if (type instanceof GenericArrayType) {
        final GenericArrayType genericArray = (GenericArrayType) type;
        return DataTypes.ARRAY(extractDataTypeOrRaw(template, typeHierarchy, genericArray.getGenericComponentType()));
    }
    final Class<?> clazz = toClass(type);
    if (clazz == null) {
        return null;
    }
    // for my.custom.Pojo[][]
    if (clazz.isArray()) {
        return DataTypes.ARRAY(extractDataTypeOrRaw(template, typeHierarchy, clazz.getComponentType()));
    }
    // data structures after conversion
    if (clazz != List.class) {
        return null;
    }
    if (!(type instanceof ParameterizedType)) {
        throw extractionError("The class '%s' needs generic parameters for an array type.", List.class.getName());
    }
    final ParameterizedType parameterizedType = (ParameterizedType) type;
    final DataType element = extractDataTypeOrRaw(template, typeHierarchy, parameterizedType.getActualTypeArguments()[0]);
    return DataTypes.ARRAY(element).bridgedTo(List.class);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) DataType(org.apache.flink.table.types.DataType) CollectionDataType(org.apache.flink.table.types.CollectionDataType) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) List(java.util.List) ArrayList(java.util.ArrayList) GenericArrayType(java.lang.reflect.GenericArrayType) Nullable(javax.annotation.Nullable)

Example 94 with DataType

use of org.apache.flink.table.types.DataType in project flink by apache.

the class DataTypeExtractor method extractMapType.

@Nullable
private DataType extractMapType(DataTypeTemplate template, List<Type> typeHierarchy, Type type) {
    final Class<?> clazz = toClass(type);
    // data structures after conversion
    if (clazz != Map.class) {
        return null;
    }
    if (!(type instanceof ParameterizedType)) {
        throw extractionError("The class '%s' needs generic parameters for a map type.", Map.class.getName());
    }
    final ParameterizedType parameterizedType = (ParameterizedType) type;
    final DataType key = extractDataTypeOrRaw(template, typeHierarchy, parameterizedType.getActualTypeArguments()[0]);
    final DataType value = extractDataTypeOrRaw(template, typeHierarchy, parameterizedType.getActualTypeArguments()[1]);
    return DataTypes.MAP(key, value);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) DataType(org.apache.flink.table.types.DataType) CollectionDataType(org.apache.flink.table.types.CollectionDataType) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) Map(java.util.Map) HashMap(java.util.HashMap) Nullable(javax.annotation.Nullable)

Example 95 with DataType

use of org.apache.flink.table.types.DataType in project flink by apache.

the class AggDecimalPlusTypeStrategy method inferType.

@Override
public Optional<DataType> inferType(CallContext callContext) {
    final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
    final LogicalType addend1 = argumentDataTypes.get(0).getLogicalType();
    final LogicalType addend2 = argumentDataTypes.get(1).getLogicalType();
    Preconditions.checkArgument(addend1.is(LogicalTypeRoot.DECIMAL), ERROR_MSG);
    Preconditions.checkArgument(addend2.is(LogicalTypeRoot.DECIMAL), ERROR_MSG);
    return Optional.of(fromLogicalToDataType(LogicalTypeMerging.findSumAggType(addend2)));
}
Also used : DataType(org.apache.flink.table.types.DataType) TypeConversions.fromLogicalToDataType(org.apache.flink.table.types.utils.TypeConversions.fromLogicalToDataType) LogicalType(org.apache.flink.table.types.logical.LogicalType)

Aggregations

DataType (org.apache.flink.table.types.DataType)260 Test (org.junit.Test)72 RowType (org.apache.flink.table.types.logical.RowType)59 LogicalType (org.apache.flink.table.types.logical.LogicalType)58 RowData (org.apache.flink.table.data.RowData)54 List (java.util.List)38 FieldsDataType (org.apache.flink.table.types.FieldsDataType)32 ValidationException (org.apache.flink.table.api.ValidationException)31 ArrayList (java.util.ArrayList)29 Collectors (java.util.stream.Collectors)24 AtomicDataType (org.apache.flink.table.types.AtomicDataType)24 Map (java.util.Map)23 Internal (org.apache.flink.annotation.Internal)23 TableException (org.apache.flink.table.api.TableException)23 HashMap (java.util.HashMap)22 GenericRowData (org.apache.flink.table.data.GenericRowData)22 Row (org.apache.flink.types.Row)22 TableSchema (org.apache.flink.table.api.TableSchema)20 TypeConversions.fromLogicalToDataType (org.apache.flink.table.types.utils.TypeConversions.fromLogicalToDataType)19 ResolvedSchema (org.apache.flink.table.catalog.ResolvedSchema)18