use of org.apache.flink.api.common.functions.InvalidTypesException in project flink by apache.
the class TypeExtractor method analyzePojo.
@SuppressWarnings("unchecked")
protected <OUT, IN1, IN2> TypeInformation<OUT> analyzePojo(Type type, List<Type> typeHierarchy, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type) {
Class<OUT> clazz = typeToClass(type);
if (!Modifier.isPublic(clazz.getModifiers())) {
LOG.info("Class " + clazz.getName() + " is not public so it cannot be used as a POJO type " + "and must be processed as GenericType. Please read the Flink documentation " + "on \"Data Types & Serialization\" for details of the effect on performance.");
return new GenericTypeInfo<>(clazz);
}
// add the hierarchy of the POJO
getTypeHierarchy(typeHierarchy, type, Object.class);
List<Field> fields = getAllDeclaredFields(clazz, false);
if (fields.size() == 0) {
LOG.info("No fields were detected for " + clazz + " so it cannot be used as a POJO type " + "and must be processed as GenericType. Please read the Flink documentation " + "on \"Data Types & Serialization\" for details of the effect on performance.");
return new GenericTypeInfo<>(clazz);
}
List<PojoField> pojoFields = new ArrayList<>();
for (Field field : fields) {
Type fieldType = field.getGenericType();
if (!isValidPojoField(field, clazz, typeHierarchy) && clazz != Row.class) {
LOG.info("Class " + clazz + " cannot be used as a POJO type because not all fields are valid POJO fields, " + "and must be processed as GenericType. Please read the Flink documentation " + "on \"Data Types & Serialization\" for details of the effect on performance.");
return null;
}
try {
final TypeInformation<?> typeInfo;
List<Type> fieldTypeHierarchy = new ArrayList<>(typeHierarchy);
TypeInfoFactory factory = getTypeInfoFactory(field);
if (factory != null) {
typeInfo = createTypeInfoFromFactory(fieldType, in1Type, in2Type, fieldTypeHierarchy, factory, fieldType);
} else {
fieldTypeHierarchy.add(fieldType);
typeInfo = createTypeInfoWithTypeHierarchy(fieldTypeHierarchy, fieldType, in1Type, in2Type);
}
pojoFields.add(new PojoField(field, typeInfo));
} catch (InvalidTypesException e) {
Class<?> genericClass = Object.class;
if (isClassType(fieldType)) {
genericClass = typeToClass(fieldType);
}
pojoFields.add(new PojoField(field, new GenericTypeInfo<>((Class<OUT>) genericClass)));
}
}
CompositeType<OUT> pojoType = new PojoTypeInfo<>(clazz, pojoFields);
//
// Validate the correctness of the pojo.
// returning "null" will result create a generic type information.
//
List<Method> methods = getAllDeclaredMethods(clazz);
for (Method method : methods) {
if (method.getName().equals("readObject") || method.getName().equals("writeObject")) {
LOG.info("Class " + clazz + " contains custom serialization methods we do not call, so it cannot be used as a POJO type " + "and must be processed as GenericType. Please read the Flink documentation " + "on \"Data Types & Serialization\" for details of the effect on performance.");
return null;
}
}
// Try retrieving the default constructor, if it does not have one
// we cannot use this because the serializer uses it.
Constructor<OUT> defaultConstructor = null;
try {
defaultConstructor = clazz.getDeclaredConstructor();
} catch (NoSuchMethodException e) {
if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) {
LOG.info(clazz + " is abstract or an interface, having a concrete " + "type can increase performance.");
} else {
LOG.info(clazz + " is missing a default constructor so it cannot be used as a POJO type " + "and must be processed as GenericType. Please read the Flink documentation " + "on \"Data Types & Serialization\" for details of the effect on performance.");
return null;
}
}
if (defaultConstructor != null && !Modifier.isPublic(defaultConstructor.getModifiers())) {
LOG.info("The default constructor of " + clazz + " is not Public so it cannot be used as a POJO type " + "and must be processed as GenericType. Please read the Flink documentation " + "on \"Data Types & Serialization\" for details of the effect on performance.");
return null;
}
// everything is checked, we return the pojo
return pojoType;
}
use of org.apache.flink.api.common.functions.InvalidTypesException in project flink by apache.
the class TypeExtractor method getTypeInfoFactory.
// --------------------------------------------------------------------------------------------
// Utility methods
// --------------------------------------------------------------------------------------------
/**
* Returns the type information factory for a type using the factory registry or annotations.
*/
@Internal
@SuppressWarnings("unchecked")
public static <OUT> TypeInfoFactory<OUT> getTypeInfoFactory(Type t) {
final Class<?> factoryClass;
if (!isClassType(t) || !typeToClass(t).isAnnotationPresent(TypeInfo.class)) {
return null;
}
final TypeInfo typeInfoAnnotation = typeToClass(t).getAnnotation(TypeInfo.class);
factoryClass = typeInfoAnnotation.value();
// check for valid factory class
if (!TypeInfoFactory.class.isAssignableFrom(factoryClass)) {
throw new InvalidTypesException("TypeInfo annotation does not specify a valid TypeInfoFactory.");
}
// instantiate
return (TypeInfoFactory<OUT>) InstantiationUtil.instantiate(factoryClass);
}
use of org.apache.flink.api.common.functions.InvalidTypesException in project flink by apache.
the class TypeExtractor method getBinaryOperatorReturnType.
/**
* Returns the binary operator's return type.
*
* <p>This method can extract a type in 4 different ways:
*
* <p>1. By using the generics of the base class like MyFunction<X, Y, Z, IN, OUT>. This is what
* outputTypeArgumentIndex (in this example "4") is good for.
*
* <p>2. By using input type inference SubMyFunction<T, String, String, String, T>. This is what
* inputTypeArgumentIndex (in this example "0") and inType is good for.
*
* <p>3. By using the static method that a compiler generates for Java lambdas. This is what
* lambdaOutputTypeArgumentIndices is good for. Given that MyFunction has the following single
* abstract method:
*
* <pre>
* <code>
* void apply(IN value, Collector<OUT> value)
* </code>
* </pre>
*
* <p>Lambda type indices allow the extraction of a type from lambdas. To extract the output
* type <b>OUT</b> from the function one should pass {@code new int[] {1, 0}}. "1" for selecting
* the parameter and 0 for the first generic in this type. Use {@code TypeExtractor.NO_INDEX}
* for selecting the return type of the lambda for extraction or if the class cannot be a lambda
* because it is not a single abstract method interface.
*
* <p>4. By using interfaces such as {@link TypeInfoFactory} or {@link ResultTypeQueryable}.
*
* <p>See also comments in the header of this class.
*
* @param function Function to extract the return type from
* @param baseClass Base class of the function
* @param input1TypeArgumentIndex Index of first input generic type in the class specification
* (ignored if in1Type is null)
* @param input2TypeArgumentIndex Index of second input generic type in the class specification
* (ignored if in2Type is null)
* @param outputTypeArgumentIndex Index of output generic type in the class specification
* @param lambdaOutputTypeArgumentIndices Table of indices of the type argument specifying the
* output type. See example.
* @param in1Type Type of the left side input elements (In case of an iterable, it is the
* element type)
* @param in2Type Type of the right side input elements (In case of an iterable, it is the
* element type)
* @param functionName Function name
* @param allowMissing Can the type information be missing (this generates a MissingTypeInfo for
* postponing an exception)
* @param <IN1> Left side input type
* @param <IN2> Right side input type
* @param <OUT> Output type
* @return TypeInformation of the return type of the function
*/
@SuppressWarnings("unchecked")
@PublicEvolving
public static <IN1, IN2, OUT> TypeInformation<OUT> getBinaryOperatorReturnType(Function function, Class<?> baseClass, int input1TypeArgumentIndex, int input2TypeArgumentIndex, int outputTypeArgumentIndex, int[] lambdaOutputTypeArgumentIndices, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type, String functionName, boolean allowMissing) {
Preconditions.checkArgument(in1Type == null || input1TypeArgumentIndex >= 0, "Input 1 type argument index was not provided");
Preconditions.checkArgument(in2Type == null || input2TypeArgumentIndex >= 0, "Input 2 type argument index was not provided");
Preconditions.checkArgument(outputTypeArgumentIndex >= 0, "Output type argument index was not provided");
Preconditions.checkArgument(lambdaOutputTypeArgumentIndices != null, "Indices for output type arguments within lambda not provided");
// explicit result type has highest precedence
if (function instanceof ResultTypeQueryable) {
return ((ResultTypeQueryable<OUT>) function).getProducedType();
}
// perform extraction
try {
final LambdaExecutable exec;
try {
exec = checkAndExtractLambda(function);
} catch (TypeExtractionException e) {
throw new InvalidTypesException("Internal error occurred.", e);
}
if (exec != null) {
final Method sam = TypeExtractionUtils.getSingleAbstractMethod(baseClass);
final int baseParametersLen = sam.getParameterTypes().length;
// parameters must be accessed from behind, since JVM can add additional parameters
// e.g. when using local variables inside lambda function
final int paramLen = exec.getParameterTypes().length;
final Type output;
if (lambdaOutputTypeArgumentIndices.length > 0) {
output = TypeExtractionUtils.extractTypeFromLambda(baseClass, exec, lambdaOutputTypeArgumentIndices, paramLen, baseParametersLen);
} else {
output = exec.getReturnType();
TypeExtractionUtils.validateLambdaType(baseClass, output);
}
return new TypeExtractor().privateCreateTypeInfo(output, in1Type, in2Type);
} else {
if (in1Type != null) {
validateInputType(baseClass, function.getClass(), input1TypeArgumentIndex, in1Type);
}
if (in2Type != null) {
validateInputType(baseClass, function.getClass(), input2TypeArgumentIndex, in2Type);
}
return new TypeExtractor().privateCreateTypeInfo(baseClass, function.getClass(), outputTypeArgumentIndex, in1Type, in2Type);
}
} catch (InvalidTypesException e) {
if (allowMissing) {
return (TypeInformation<OUT>) new MissingTypeInfo(functionName != null ? functionName : function.toString(), e);
} else {
throw e;
}
}
}
use of org.apache.flink.api.common.functions.InvalidTypesException in project flink by apache.
the class TypeExtractor method createTypeInfoFromFactory.
/**
* Creates type information using a given factory.
*/
@SuppressWarnings("unchecked")
private <IN1, IN2, OUT> TypeInformation<OUT> createTypeInfoFromFactory(Type t, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type, List<Type> factoryHierarchy, TypeInfoFactory<? super OUT> factory, Type factoryDefiningType) {
// infer possible type parameters from input
final Map<String, TypeInformation<?>> genericParams;
if (factoryDefiningType instanceof ParameterizedType) {
genericParams = new HashMap<>();
final ParameterizedType paramDefiningType = (ParameterizedType) factoryDefiningType;
final Type[] args = typeToClass(paramDefiningType).getTypeParameters();
final TypeInformation<?>[] subtypeInfo = createSubTypesInfo(t, paramDefiningType, factoryHierarchy, in1Type, in2Type, true);
assert subtypeInfo != null;
for (int i = 0; i < subtypeInfo.length; i++) {
genericParams.put(args[i].toString(), subtypeInfo[i]);
}
} else {
genericParams = Collections.emptyMap();
}
final TypeInformation<OUT> createdTypeInfo = (TypeInformation<OUT>) factory.createTypeInfo(t, genericParams);
if (createdTypeInfo == null) {
throw new InvalidTypesException("TypeInfoFactory returned invalid TypeInformation 'null'");
}
return createdTypeInfo;
}
use of org.apache.flink.api.common.functions.InvalidTypesException in project flink by apache.
the class TypeExtractor method validateInfo.
@SuppressWarnings("unchecked")
private static void validateInfo(ArrayList<Type> typeHierarchy, Type type, TypeInformation<?> typeInfo) {
if (type == null) {
throw new InvalidTypesException("Unknown Error. Type is null.");
}
if (typeInfo == null) {
throw new InvalidTypesException("Unknown Error. TypeInformation is null.");
}
if (!(type instanceof TypeVariable<?>)) {
// check for Java Basic Types
if (typeInfo instanceof BasicTypeInfo) {
TypeInformation<?> actual;
// check if basic type at all
if (!(type instanceof Class<?>) || (actual = BasicTypeInfo.getInfoFor((Class<?>) type)) == null) {
throw new InvalidTypesException("Basic type expected.");
}
// check if correct basic type
if (!typeInfo.equals(actual)) {
throw new InvalidTypesException("Basic type '" + typeInfo + "' expected but was '" + actual + "'.");
}
} else // check for Java SQL time types
if (typeInfo instanceof SqlTimeTypeInfo) {
TypeInformation<?> actual;
// check if SQL time type at all
if (!(type instanceof Class<?>) || (actual = SqlTimeTypeInfo.getInfoFor((Class<?>) type)) == null) {
throw new InvalidTypesException("SQL time type expected.");
}
// check if correct SQL time type
if (!typeInfo.equals(actual)) {
throw new InvalidTypesException("SQL time type '" + typeInfo + "' expected but was '" + actual + "'.");
}
} else // check for Java Tuples
if (typeInfo instanceof TupleTypeInfo) {
// check if tuple at all
if (!(isClassType(type) && Tuple.class.isAssignableFrom(typeToClass(type)))) {
throw new InvalidTypesException("Tuple type expected.");
}
// do not allow usage of Tuple as type
if (isClassType(type) && typeToClass(type).equals(Tuple.class)) {
throw new InvalidTypesException("Concrete subclass of Tuple expected.");
}
// go up the hierarchy until we reach immediate child of Tuple (with or without generics)
while (!(isClassType(type) && typeToClass(type).getSuperclass().equals(Tuple.class))) {
typeHierarchy.add(type);
type = typeToClass(type).getGenericSuperclass();
}
if (type == Tuple0.class) {
return;
}
// check if immediate child of Tuple has generics
if (type instanceof Class<?>) {
throw new InvalidTypesException("Parameterized Tuple type expected.");
}
TupleTypeInfo<?> tti = (TupleTypeInfo<?>) typeInfo;
Type[] subTypes = ((ParameterizedType) type).getActualTypeArguments();
if (subTypes.length != tti.getArity()) {
throw new InvalidTypesException("Tuple arity '" + tti.getArity() + "' expected but was '" + subTypes.length + "'.");
}
for (int i = 0; i < subTypes.length; i++) {
validateInfo(new ArrayList<Type>(typeHierarchy), subTypes[i], tti.getTypeAt(i));
}
} else // check for primitive array
if (typeInfo instanceof PrimitiveArrayTypeInfo) {
Type component;
// check if array at all
if (!(type instanceof Class<?> && ((Class<?>) type).isArray() && (component = ((Class<?>) type).getComponentType()) != null) && !(type instanceof GenericArrayType && (component = ((GenericArrayType) type).getGenericComponentType()) != null)) {
throw new InvalidTypesException("Array type expected.");
}
if (component instanceof TypeVariable<?>) {
component = materializeTypeVariable(typeHierarchy, (TypeVariable<?>) component);
if (component instanceof TypeVariable) {
return;
}
}
if (!(component instanceof Class<?> && ((Class<?>) component).isPrimitive())) {
throw new InvalidTypesException("Primitive component expected.");
}
} else // check for basic array
if (typeInfo instanceof BasicArrayTypeInfo<?, ?>) {
Type component;
// check if array at all
if (!(type instanceof Class<?> && ((Class<?>) type).isArray() && (component = ((Class<?>) type).getComponentType()) != null) && !(type instanceof GenericArrayType && (component = ((GenericArrayType) type).getGenericComponentType()) != null)) {
throw new InvalidTypesException("Array type expected.");
}
if (component instanceof TypeVariable<?>) {
component = materializeTypeVariable(typeHierarchy, (TypeVariable<?>) component);
if (component instanceof TypeVariable) {
return;
}
}
validateInfo(typeHierarchy, component, ((BasicArrayTypeInfo<?, ?>) typeInfo).getComponentInfo());
} else // check for object array
if (typeInfo instanceof ObjectArrayTypeInfo<?, ?>) {
// check if array at all
if (!(type instanceof Class<?> && ((Class<?>) type).isArray()) && !(type instanceof GenericArrayType)) {
throw new InvalidTypesException("Object array type expected.");
}
// check component
Type component;
if (type instanceof Class<?>) {
component = ((Class<?>) type).getComponentType();
} else {
component = ((GenericArrayType) type).getGenericComponentType();
}
if (component instanceof TypeVariable<?>) {
component = materializeTypeVariable(typeHierarchy, (TypeVariable<?>) component);
if (component instanceof TypeVariable) {
return;
}
}
validateInfo(typeHierarchy, component, ((ObjectArrayTypeInfo<?, ?>) typeInfo).getComponentInfo());
} else // check for value
if (typeInfo instanceof ValueTypeInfo<?>) {
// check if value at all
if (!(type instanceof Class<?> && Value.class.isAssignableFrom((Class<?>) type))) {
throw new InvalidTypesException("Value type expected.");
}
TypeInformation<?> actual;
// check value type contents
if (!((ValueTypeInfo<?>) typeInfo).equals(actual = ValueTypeInfo.getValueTypeInfo((Class<? extends Value>) type))) {
throw new InvalidTypesException("Value type '" + typeInfo + "' expected but was '" + actual + "'.");
}
} else // check for POJO
if (typeInfo instanceof PojoTypeInfo) {
Class<?> clazz = null;
if (!(isClassType(type) && ((PojoTypeInfo<?>) typeInfo).getTypeClass() == (clazz = typeToClass(type)))) {
throw new InvalidTypesException("POJO type '" + ((PojoTypeInfo<?>) typeInfo).getTypeClass().getCanonicalName() + "' expected but was '" + clazz.getCanonicalName() + "'.");
}
} else // check for Enum
if (typeInfo instanceof EnumTypeInfo) {
if (!(type instanceof Class<?> && Enum.class.isAssignableFrom((Class<?>) type))) {
throw new InvalidTypesException("Enum type expected.");
}
// check enum type contents
if (!(typeInfo.getTypeClass() == type)) {
throw new InvalidTypesException("Enum type '" + typeInfo.getTypeClass().getCanonicalName() + "' expected but was '" + typeToClass(type).getCanonicalName() + "'.");
}
} else // check for generic object
if (typeInfo instanceof GenericTypeInfo<?>) {
Class<?> clazz = null;
if (!(isClassType(type) && (clazz = typeToClass(type)).isAssignableFrom(((GenericTypeInfo<?>) typeInfo).getTypeClass()))) {
throw new InvalidTypesException("Generic type '" + ((GenericTypeInfo<?>) typeInfo).getTypeClass().getCanonicalName() + "' or a subclass of it expected but was '" + clazz.getCanonicalName() + "'.");
}
} else // check for Writable
{
validateIfWritable(typeInfo, type);
}
} else {
type = materializeTypeVariable(typeHierarchy, (TypeVariable<?>) type);
if (!(type instanceof TypeVariable)) {
validateInfo(typeHierarchy, type, typeInfo);
}
}
}
Aggregations