Search in sources :

Example 1 with CompositeType

use of org.apache.flink.api.common.typeutils.CompositeType in project flink by apache.

the class TupleTypeInfoBase method getTypeAt.

@Override
public <X> TypeInformation<X> getTypeAt(String fieldExpression) {
    Matcher matcher = PATTERN_NESTED_FIELDS.matcher(fieldExpression);
    if (!matcher.matches()) {
        if (fieldExpression.equals(ExpressionKeys.SELECT_ALL_CHAR) || fieldExpression.equals(ExpressionKeys.SELECT_ALL_CHAR_SCALA)) {
            throw new InvalidFieldReferenceException("Wildcard expressions are not allowed here.");
        } else {
            throw new InvalidFieldReferenceException("Invalid format of tuple field expression \"" + fieldExpression + "\".");
        }
    }
    String fieldStr = matcher.group(1);
    Matcher fieldMatcher = PATTERN_FIELD.matcher(fieldStr);
    if (!fieldMatcher.matches()) {
        throw new RuntimeException("Invalid matcher pattern");
    }
    String field = fieldMatcher.group(2);
    int fieldPos = Integer.valueOf(field);
    if (fieldPos >= this.getArity()) {
        throw new InvalidFieldReferenceException("Tuple field expression \"" + fieldStr + "\" out of bounds of " + this.toString() + ".");
    }
    TypeInformation<X> fieldType = this.getTypeAt(fieldPos);
    String tail = matcher.group(5);
    if (tail == null) {
        // we found the type
        return fieldType;
    } else {
        if (fieldType instanceof CompositeType<?>) {
            return ((CompositeType<?>) fieldType).getTypeAt(tail);
        } else {
            throw new InvalidFieldReferenceException("Nested field expression \"" + tail + "\" not possible on atomic type " + fieldType + ".");
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) CompositeType(org.apache.flink.api.common.typeutils.CompositeType)

Example 2 with CompositeType

use of org.apache.flink.api.common.typeutils.CompositeType in project flink by apache.

the class TupleTypeInfoBase method getFlatFields.

@Override
public void getFlatFields(String fieldExpression, int offset, List<FlatFieldDescriptor> result) {
    Matcher matcher = PATTERN_NESTED_FIELDS_WILDCARD.matcher(fieldExpression);
    if (!matcher.matches()) {
        throw new InvalidFieldReferenceException("Invalid tuple field reference \"" + fieldExpression + "\".");
    }
    String field = matcher.group(0);
    if (field.equals(ExpressionKeys.SELECT_ALL_CHAR) || field.equals(ExpressionKeys.SELECT_ALL_CHAR_SCALA)) {
        // handle select all
        int keyPosition = 0;
        for (TypeInformation<?> type : types) {
            if (type instanceof CompositeType) {
                CompositeType<?> cType = (CompositeType<?>) type;
                cType.getFlatFields(String.valueOf(ExpressionKeys.SELECT_ALL_CHAR), offset + keyPosition, result);
                keyPosition += cType.getTotalFields() - 1;
            } else {
                result.add(new FlatFieldDescriptor(offset + keyPosition, type));
            }
            keyPosition++;
        }
    } else {
        String fieldStr = matcher.group(1);
        Matcher fieldMatcher = PATTERN_FIELD.matcher(fieldStr);
        if (!fieldMatcher.matches()) {
            throw new RuntimeException("Invalid matcher pattern");
        }
        field = fieldMatcher.group(2);
        int fieldPos = Integer.valueOf(field);
        if (fieldPos >= this.getArity()) {
            throw new InvalidFieldReferenceException("Tuple field expression \"" + fieldStr + "\" out of bounds of " + this.toString() + ".");
        }
        TypeInformation<?> fieldType = this.getTypeAt(fieldPos);
        String tail = matcher.group(5);
        if (tail == null) {
            if (fieldType instanceof CompositeType) {
                // forward offsets
                for (int i = 0; i < fieldPos; i++) {
                    offset += this.getTypeAt(i).getTotalFields();
                }
                // add all fields of composite type
                ((CompositeType<?>) fieldType).getFlatFields("*", offset, result);
            } else {
                // we found the field to add
                // compute flat field position by adding skipped fields
                int flatFieldPos = offset;
                for (int i = 0; i < fieldPos; i++) {
                    flatFieldPos += this.getTypeAt(i).getTotalFields();
                }
                result.add(new FlatFieldDescriptor(flatFieldPos, fieldType));
            }
        } else {
            if (fieldType instanceof CompositeType<?>) {
                // forward offset
                for (int i = 0; i < fieldPos; i++) {
                    offset += this.getTypeAt(i).getTotalFields();
                }
                ((CompositeType<?>) fieldType).getFlatFields(tail, offset, result);
            } else {
                throw new InvalidFieldReferenceException("Nested field expression \"" + tail + "\" not possible on atomic type " + fieldType + ".");
            }
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) CompositeType(org.apache.flink.api.common.typeutils.CompositeType)

Example 3 with CompositeType

use of org.apache.flink.api.common.typeutils.CompositeType in project flink by apache.

the class TypeExtractor method analyzePojo.

@SuppressWarnings("unchecked")
protected <OUT, IN1, IN2> TypeInformation<OUT> analyzePojo(Class<OUT> clazz, ArrayList<Type> typeHierarchy, ParameterizedType parameterizedType, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type) {
    if (!Modifier.isPublic(clazz.getModifiers())) {
        LOG.info("Class " + clazz.getName() + " is not public, cannot treat it as a POJO type. Will be handled as GenericType");
        return new GenericTypeInfo<OUT>(clazz);
    }
    // add the hierarchy of the POJO itself if it is generic
    if (parameterizedType != null) {
        getTypeHierarchy(typeHierarchy, parameterizedType, Object.class);
    } else // create a type hierarchy, if the incoming only contains the most bottom one or none.
    if (typeHierarchy.size() <= 1) {
        getTypeHierarchy(typeHierarchy, clazz, Object.class);
    }
    List<Field> fields = getAllDeclaredFields(clazz, false);
    if (fields.size() == 0) {
        LOG.info("No fields detected for " + clazz + ". Cannot be used as a PojoType. Will be handled as GenericType");
        return new GenericTypeInfo<OUT>(clazz);
    }
    List<PojoField> pojoFields = new ArrayList<PojoField>();
    for (Field field : fields) {
        Type fieldType = field.getGenericType();
        if (!isValidPojoField(field, clazz, typeHierarchy)) {
            LOG.info(clazz + " is not a valid POJO type");
            return null;
        }
        try {
            ArrayList<Type> fieldTypeHierarchy = new ArrayList<Type>(typeHierarchy);
            fieldTypeHierarchy.add(fieldType);
            TypeInformation<?> ti = createTypeInfoWithTypeHierarchy(fieldTypeHierarchy, fieldType, in1Type, in2Type);
            pojoFields.add(new PojoField(field, ti));
        } catch (InvalidTypesException e) {
            Class<?> genericClass = Object.class;
            if (isClassType(fieldType)) {
                genericClass = typeToClass(fieldType);
            }
            pojoFields.add(new PojoField(field, new GenericTypeInfo<OUT>((Class<OUT>) genericClass)));
        }
    }
    CompositeType<OUT> pojoType = new PojoTypeInfo<OUT>(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(clazz + " contains custom serialization methods we do not call.");
            return null;
        }
    }
    // Try retrieving the default constructor, if it does not have one
    // we cannot use this because the serializer uses it.
    Constructor 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 + " must have a default constructor to be used as a POJO.");
            return null;
        }
    }
    if (defaultConstructor != null && !Modifier.isPublic(defaultConstructor.getModifiers())) {
        LOG.info("The default constructor of " + clazz + " should be Public to be used as a POJO.");
        return null;
    }
    // everything is checked, we return the pojo
    return pojoType;
}
Also used : Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) GenericArrayType(java.lang.reflect.GenericArrayType) TypeExtractionUtils.isClassType(org.apache.flink.api.java.typeutils.TypeExtractionUtils.isClassType) Type(java.lang.reflect.Type) CompositeType(org.apache.flink.api.common.typeutils.CompositeType) ParameterizedType(java.lang.reflect.ParameterizedType) TypeExtractionUtils.typeToClass(org.apache.flink.api.java.typeutils.TypeExtractionUtils.typeToClass) InvalidTypesException(org.apache.flink.api.common.functions.InvalidTypesException)

Example 4 with CompositeType

use of org.apache.flink.api.common.typeutils.CompositeType in project flink by apache.

the class RowTypeInfo method getTypeAt.

@Override
public <X> TypeInformation<X> getTypeAt(String fieldExpression) {
    Matcher matcher = PATTERN_NESTED_FIELDS.matcher(fieldExpression);
    if (!matcher.matches()) {
        if (fieldExpression.equals(ExpressionKeys.SELECT_ALL_CHAR) || fieldExpression.equals(ExpressionKeys.SELECT_ALL_CHAR_SCALA)) {
            throw new InvalidFieldReferenceException("Wildcard expressions are not allowed here.");
        } else {
            throw new InvalidFieldReferenceException("Invalid format of Row field expression \"" + fieldExpression + "\".");
        }
    }
    String field = matcher.group(1);
    Matcher intFieldMatcher = PATTERN_INT_FIELD.matcher(field);
    int fieldIndex;
    if (intFieldMatcher.matches()) {
        // field expression is an integer
        fieldIndex = Integer.valueOf(field);
    } else {
        fieldIndex = this.getFieldIndex(field);
    }
    // fetch the field type will throw exception if the index is illegal
    TypeInformation<X> fieldType = this.getTypeAt(fieldIndex);
    String tail = matcher.group(3);
    if (tail == null) {
        // found the type
        return fieldType;
    } else {
        if (fieldType instanceof CompositeType) {
            return ((CompositeType<?>) fieldType).getTypeAt(tail);
        } else {
            throw new InvalidFieldReferenceException("Nested field expression \"" + tail + "\" not possible on atomic type " + fieldType + ".");
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) CompositeType(org.apache.flink.api.common.typeutils.CompositeType)

Example 5 with CompositeType

use of org.apache.flink.api.common.typeutils.CompositeType in project flink by apache.

the class PojoSubclassComparatorTest method createComparator.

@Override
protected TypeComparator<PojoContainingTuple> createComparator(boolean ascending) {
    Assert.assertTrue(type instanceof CompositeType);
    CompositeType<PojoContainingTuple> cType = (CompositeType<PojoContainingTuple>) type;
    ExpressionKeys<PojoContainingTuple> keys = new ExpressionKeys<PojoContainingTuple>(new String[] { "theTuple.*" }, cType);
    boolean[] orders = new boolean[keys.getNumberOfKeyFields()];
    Arrays.fill(orders, ascending);
    return cType.createComparator(keys.computeLogicalKeyPositions(), orders, 0, new ExecutionConfig());
}
Also used : ExpressionKeys(org.apache.flink.api.common.operators.Keys.ExpressionKeys) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) CompositeType(org.apache.flink.api.common.typeutils.CompositeType)

Aggregations

CompositeType (org.apache.flink.api.common.typeutils.CompositeType)12 Matcher (java.util.regex.Matcher)6 ArrayList (java.util.ArrayList)2 PublicEvolving (org.apache.flink.annotation.PublicEvolving)2 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)2 InvalidProgramException (org.apache.flink.api.common.InvalidProgramException)2 ExpressionKeys (org.apache.flink.api.common.operators.Keys.ExpressionKeys)2 Test (org.junit.Test)2 Constructor (java.lang.reflect.Constructor)1 Field (java.lang.reflect.Field)1 GenericArrayType (java.lang.reflect.GenericArrayType)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Aggregator (org.apache.flink.api.common.aggregators.Aggregator)1 ConvergenceCriterion (org.apache.flink.api.common.aggregators.ConvergenceCriterion)1