Search in sources :

Example 6 with BasicTypeInfo

use of org.apache.flink.api.common.typeinfo.BasicTypeInfo 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).");
    }
}
Also used : TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) TupleTypeInfoBase(org.apache.flink.api.java.typeutils.TupleTypeInfoBase) PrimitiveArrayTypeInfo(org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo) BasicTypeInfo(org.apache.flink.api.common.typeinfo.BasicTypeInfo) BasicArrayTypeInfo(org.apache.flink.api.common.typeinfo.BasicArrayTypeInfo) CompositeType(org.apache.flink.api.common.typeutils.CompositeType) Internal(org.apache.flink.annotation.Internal)

Aggregations

BasicTypeInfo (org.apache.flink.api.common.typeinfo.BasicTypeInfo)6 ArrayList (java.util.ArrayList)3 BasicArrayTypeInfo (org.apache.flink.api.common.typeinfo.BasicArrayTypeInfo)3 PrimitiveArrayTypeInfo (org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo)3 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)3 CompositeType (org.apache.flink.api.common.typeutils.CompositeType)3 Internal (org.apache.flink.annotation.Internal)2 InvalidTypesException (org.apache.flink.api.common.functions.InvalidTypesException)2 FlatFieldDescriptor (org.apache.flink.api.common.typeutils.CompositeType.FlatFieldDescriptor)2 TupleTypeInfoBase (org.apache.flink.api.java.typeutils.TupleTypeInfoBase)2 Field (java.lang.reflect.Field)1 GenericArrayType (java.lang.reflect.GenericArrayType)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 BigInteger (java.math.BigInteger)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 RichMapFunction (org.apache.flink.api.common.functions.RichMapFunction)1 SqlTimeTypeInfo (org.apache.flink.api.common.typeinfo.SqlTimeTypeInfo)1