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);
}
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;
}
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);
}
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);
}
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)));
}
Aggregations