use of org.apache.flink.api.common.functions.InvalidTypesException in project flink by apache.
the class UdfOperatorUtils method analyzeDualInputUdf.
public static void analyzeDualInputUdf(TwoInputUdfOperator<?, ?, ?, ?> operator, Class<?> udfBaseClass, String defaultName, Function udf, Keys<?> key1, Keys<?> key2) {
final CodeAnalysisMode mode = operator.getExecutionEnvironment().getConfig().getCodeAnalysisMode();
if (mode != CodeAnalysisMode.DISABLE && !udf.getClass().isAnnotationPresent(FunctionAnnotation.SkipCodeAnalysis.class)) {
final String operatorName = operator.getName() != null ? operator.getName() : udfBaseClass.getSimpleName() + " at " + defaultName;
try {
final UdfAnalyzer analyzer = new UdfAnalyzer(udfBaseClass, udf.getClass(), operatorName, operator.getInput1Type(), operator.getInput2Type(), operator.getResultType(), key1, key2, mode == CodeAnalysisMode.OPTIMIZE);
final boolean success = analyzer.analyze();
if (success) {
if (mode == CodeAnalysisMode.OPTIMIZE && !(operator.udfWithForwardedFieldsFirstAnnotation(udf.getClass()) || operator.udfWithForwardedFieldsSecondAnnotation(udf.getClass()))) {
analyzer.addSemanticPropertiesHints();
operator.setSemanticProperties((DualInputSemanticProperties) analyzer.getSemanticProperties());
operator.setAnalyzedUdfSemanticsFlag();
} else if (mode == CodeAnalysisMode.HINT) {
analyzer.addSemanticPropertiesHints();
}
analyzer.printToLogger(LOG);
}
} catch (InvalidTypesException e) {
LOG.debug("Unable to do code analysis due to missing type information.", e);
} catch (CodeAnalyzerException e) {
LOG.debug("Code analysis failed.", e);
}
}
}
use of org.apache.flink.api.common.functions.InvalidTypesException in project flink by apache.
the class TypeExtractor method getUnaryOperatorReturnType.
// --------------------------------------------------------------------------------------------
// Generic extraction methods
// --------------------------------------------------------------------------------------------
/**
* Returns the unary 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 inputTypeArgumentIndex Index of input generic type in the base class specification
* (ignored if inType is null)
* @param outputTypeArgumentIndex Index of output generic type in the base class specification
* @param lambdaOutputTypeArgumentIndices Table of indices of the type argument specifying the
* input type. See example.
* @param inType Type of the input elements (In case of an iterable, it is the element type) or
* null
* @param functionName Function name
* @param allowMissing Can the type information be missing (this generates a MissingTypeInfo for
* postponing an exception)
* @param <IN> Input type
* @param <OUT> Output type
* @return TypeInformation of the return type of the function
*/
@SuppressWarnings("unchecked")
@PublicEvolving
public static <IN, OUT> TypeInformation<OUT> getUnaryOperatorReturnType(Function function, Class<?> baseClass, int inputTypeArgumentIndex, int outputTypeArgumentIndex, int[] lambdaOutputTypeArgumentIndices, TypeInformation<IN> inType, String functionName, boolean allowMissing) {
Preconditions.checkArgument(inType == null || inputTypeArgumentIndex >= 0, "Input 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) {
// parameters must be accessed from behind, since JVM can add additional parameters
// e.g. when using local variables inside lambda function
// paramLen is the total number of parameters of the provided lambda, it includes
// parameters added through closure
final int paramLen = exec.getParameterTypes().length;
final Method sam = TypeExtractionUtils.getSingleAbstractMethod(baseClass);
// number of parameters the SAM of implemented interface has; the parameter indexing
// applies to this range
final int baseParametersLen = sam.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, inType, null);
} else {
if (inType != null) {
validateInputType(baseClass, function.getClass(), inputTypeArgumentIndex, inType);
}
return new TypeExtractor().privateCreateTypeInfo(baseClass, function.getClass(), outputTypeArgumentIndex, inType, null);
}
} 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 validateInputType.
// --------------------------------------------------------------------------------------------
// Validate input
// --------------------------------------------------------------------------------------------
private static void validateInputType(Class<?> baseClass, Class<?> clazz, int inputParamPos, TypeInformation<?> inTypeInfo) {
List<Type> typeHierarchy = new ArrayList<>();
// try to get generic parameter
Type inType;
try {
inType = getParameterType(baseClass, typeHierarchy, clazz, inputParamPos);
} catch (InvalidTypesException e) {
// skip input validation e.g. for raw types
return;
}
try {
validateInfo(typeHierarchy, inType, inTypeInfo);
} catch (InvalidTypesException e) {
throw new InvalidTypesException("Input mismatch: " + e.getMessage(), e);
}
}
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(List<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.");
}
// 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<>(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 (!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);
}
}
}
use of org.apache.flink.api.common.functions.InvalidTypesException in project flink by apache.
the class TypeExtractor method privateGetForObject.
@SuppressWarnings({ "unchecked", "rawtypes" })
private <X> TypeInformation<X> privateGetForObject(X value) {
checkNotNull(value);
// check if type information can be produced using a factory
final List<Type> typeHierarchy = new ArrayList<>();
typeHierarchy.add(value.getClass());
final TypeInformation<X> typeFromFactory = createTypeInfoFromFactory(value.getClass(), typeHierarchy, null, null);
if (typeFromFactory != null) {
return typeFromFactory;
}
// check if we can extract the types from tuples, otherwise work with the class
if (value instanceof Tuple) {
Tuple t = (Tuple) value;
int numFields = t.getArity();
if (numFields != countFieldsInClass(value.getClass())) {
// not a tuple since it has more fields.
return analyzePojo(value.getClass(), new ArrayList<>(), null, // we immediately call analyze Pojo here, because
null);
// there is currently no other type that can handle such a class.
}
TypeInformation<?>[] infos = new TypeInformation[numFields];
for (int i = 0; i < numFields; i++) {
Object field = t.getField(i);
if (field == null) {
throw new InvalidTypesException("Automatic type extraction is not possible on candidates with null values. " + "Please specify the types directly.");
}
infos[i] = privateGetForObject(field);
}
return new TupleTypeInfo(value.getClass(), infos);
} else if (value instanceof Row) {
Row row = (Row) value;
int arity = row.getArity();
for (int i = 0; i < arity; i++) {
if (row.getField(i) == null) {
LOG.warn("Cannot extract type of Row field, because of Row field[" + i + "] is null. " + "Should define RowTypeInfo explicitly.");
return privateGetForClass((Class<X>) value.getClass(), new ArrayList<>());
}
}
TypeInformation<?>[] typeArray = new TypeInformation<?>[arity];
for (int i = 0; i < arity; i++) {
typeArray[i] = TypeExtractor.getForObject(row.getField(i));
}
return (TypeInformation<X>) new RowTypeInfo(typeArray);
} else {
return privateGetForClass((Class<X>) value.getClass(), new ArrayList<>());
}
}
Aggregations