use of org.apache.flink.api.common.typeinfo.TypeInformation in project flink by apache.
the class FieldAccessorFactory method getAccessor.
/**
* Creates a {@link FieldAccessor} for the given field position, which can be used to get and set
* the specified field on instances of this type.
*
* @param pos The field position (zero-based)
* @param config Configuration object
* @param <F> The type of the field to access
* @return The created FieldAccessor
*/
@Internal
public static <T, F> FieldAccessor<T, F> getAccessor(TypeInformation<T> typeInfo, int pos, ExecutionConfig config) {
// In case of arrays
if (typeInfo instanceof BasicArrayTypeInfo || typeInfo instanceof PrimitiveArrayTypeInfo) {
return new FieldAccessor.ArrayFieldAccessor<>(pos, typeInfo);
// In case of basic types
} else if (typeInfo instanceof BasicTypeInfo) {
if (pos != 0) {
throw new CompositeType.InvalidFieldReferenceException("The " + ((Integer) pos).toString() + ". field selected on a " + "basic type (" + typeInfo.toString() + "). A field expression on a basic type can only select " + "the 0th field (which means selecting the entire basic type).");
}
@SuppressWarnings("unchecked") FieldAccessor<T, F> result = (FieldAccessor<T, F>) new FieldAccessor.SimpleFieldAccessor<>(typeInfo);
return result;
// In case of case classes
} else if (typeInfo.isTupleType() && ((TupleTypeInfoBase) typeInfo).isCaseClass()) {
TupleTypeInfoBase tupleTypeInfo = (TupleTypeInfoBase) typeInfo;
@SuppressWarnings("unchecked") TypeInformation<F> fieldTypeInfo = (TypeInformation<F>) tupleTypeInfo.getTypeAt(pos);
return new FieldAccessor.RecursiveProductFieldAccessor<>(pos, typeInfo, new FieldAccessor.SimpleFieldAccessor<>(fieldTypeInfo), config);
// In case of tuples
} else if (typeInfo.isTupleType()) {
@SuppressWarnings("unchecked") FieldAccessor<T, F> result = new FieldAccessor.SimpleTupleFieldAccessor(pos, typeInfo);
return result;
// Default case, PojoType is directed to this statement
} else {
throw new CompositeType.InvalidFieldReferenceException("Cannot reference field by position on " + typeInfo.toString() + "Referencing a field by position is supported on tuples, case classes, and arrays. " + "Additionally, you can select the 0th field of a primitive/basic type (e.g. int).");
}
}
Aggregations