Search in sources :

Example 1 with InvalidSemanticAnnotationException

use of org.apache.flink.api.common.operators.SemanticProperties.InvalidSemanticAnnotationException in project flink by apache.

the class SemanticPropUtil method getSemanticPropsDual.

public static DualInputSemanticProperties getSemanticPropsDual(Set<Annotation> set, TypeInformation<?> inType1, TypeInformation<?> inType2, TypeInformation<?> outType) {
    if (set == null) {
        return null;
    }
    Iterator<Annotation> it = set.iterator();
    String[] forwardedFirst = null;
    String[] forwardedSecond = null;
    String[] nonForwardedFirst = null;
    String[] nonForwardedSecond = null;
    String[] readFirst = null;
    String[] readSecond = null;
    while (it.hasNext()) {
        Annotation ann = it.next();
        if (ann instanceof ForwardedFieldsFirst) {
            forwardedFirst = ((ForwardedFieldsFirst) ann).value();
        } else if (ann instanceof ForwardedFieldsSecond) {
            forwardedSecond = ((ForwardedFieldsSecond) ann).value();
        } else if (ann instanceof NonForwardedFieldsFirst) {
            nonForwardedFirst = ((NonForwardedFieldsFirst) ann).value();
        } else if (ann instanceof NonForwardedFieldsSecond) {
            nonForwardedSecond = ((NonForwardedFieldsSecond) ann).value();
        } else if (ann instanceof ReadFieldsFirst) {
            readFirst = ((ReadFieldsFirst) ann).value();
        } else if (ann instanceof ReadFieldsSecond) {
            readSecond = ((ReadFieldsSecond) ann).value();
        } else if (ann instanceof ForwardedFields || ann instanceof NonForwardedFields || ann instanceof ReadFields) {
            throw new InvalidSemanticAnnotationException("Annotation " + ann.getClass() + " invalid for dual input function.");
        }
    }
    if (forwardedFirst != null || nonForwardedFirst != null || readFirst != null || forwardedSecond != null || nonForwardedSecond != null || readSecond != null) {
        DualInputSemanticProperties result = new DualInputSemanticProperties();
        getSemanticPropsDualFromString(result, forwardedFirst, forwardedSecond, nonForwardedFirst, nonForwardedSecond, readFirst, readSecond, inType1, inType2, outType);
        return result;
    }
    return null;
}
Also used : NonForwardedFields(org.apache.flink.api.java.functions.FunctionAnnotation.NonForwardedFields) ForwardedFields(org.apache.flink.api.java.functions.FunctionAnnotation.ForwardedFields) DualInputSemanticProperties(org.apache.flink.api.common.operators.DualInputSemanticProperties) Annotation(java.lang.annotation.Annotation) ReadFieldsFirst(org.apache.flink.api.java.functions.FunctionAnnotation.ReadFieldsFirst) ForwardedFieldsSecond(org.apache.flink.api.java.functions.FunctionAnnotation.ForwardedFieldsSecond) NonForwardedFieldsSecond(org.apache.flink.api.java.functions.FunctionAnnotation.NonForwardedFieldsSecond) NonForwardedFieldsFirst(org.apache.flink.api.java.functions.FunctionAnnotation.NonForwardedFieldsFirst) ForwardedFieldsFirst(org.apache.flink.api.java.functions.FunctionAnnotation.ForwardedFieldsFirst) NonForwardedFieldsFirst(org.apache.flink.api.java.functions.FunctionAnnotation.NonForwardedFieldsFirst) NonForwardedFieldsSecond(org.apache.flink.api.java.functions.FunctionAnnotation.NonForwardedFieldsSecond) NonForwardedFields(org.apache.flink.api.java.functions.FunctionAnnotation.NonForwardedFields) InvalidSemanticAnnotationException(org.apache.flink.api.common.operators.SemanticProperties.InvalidSemanticAnnotationException) ReadFieldsSecond(org.apache.flink.api.java.functions.FunctionAnnotation.ReadFieldsSecond) ReadFields(org.apache.flink.api.java.functions.FunctionAnnotation.ReadFields)

Example 2 with InvalidSemanticAnnotationException

use of org.apache.flink.api.common.operators.SemanticProperties.InvalidSemanticAnnotationException in project flink by apache.

the class SemanticPropUtil method getSemanticPropsSingle.

public static SingleInputSemanticProperties getSemanticPropsSingle(Set<Annotation> set, TypeInformation<?> inType, TypeInformation<?> outType) {
    if (set == null) {
        return null;
    }
    Iterator<Annotation> it = set.iterator();
    String[] forwarded = null;
    String[] nonForwarded = null;
    String[] read = null;
    while (it.hasNext()) {
        Annotation ann = it.next();
        if (ann instanceof ForwardedFields) {
            forwarded = ((ForwardedFields) ann).value();
        } else if (ann instanceof NonForwardedFields) {
            nonForwarded = ((NonForwardedFields) ann).value();
        } else if (ann instanceof ReadFields) {
            read = ((ReadFields) ann).value();
        } else if (ann instanceof ForwardedFieldsFirst || ann instanceof ForwardedFieldsSecond || ann instanceof NonForwardedFieldsFirst || ann instanceof NonForwardedFieldsSecond || ann instanceof ReadFieldsFirst || ann instanceof ReadFieldsSecond) {
            throw new InvalidSemanticAnnotationException("Annotation " + ann.getClass() + " invalid for single input function.");
        }
    }
    if (forwarded != null || nonForwarded != null || read != null) {
        SingleInputSemanticProperties result = new SingleInputSemanticProperties();
        getSemanticPropsSingleFromString(result, forwarded, nonForwarded, read, inType, outType);
        return result;
    }
    return null;
}
Also used : NonForwardedFields(org.apache.flink.api.java.functions.FunctionAnnotation.NonForwardedFields) ForwardedFields(org.apache.flink.api.java.functions.FunctionAnnotation.ForwardedFields) Annotation(java.lang.annotation.Annotation) ReadFieldsFirst(org.apache.flink.api.java.functions.FunctionAnnotation.ReadFieldsFirst) ForwardedFieldsSecond(org.apache.flink.api.java.functions.FunctionAnnotation.ForwardedFieldsSecond) NonForwardedFieldsSecond(org.apache.flink.api.java.functions.FunctionAnnotation.NonForwardedFieldsSecond) NonForwardedFieldsFirst(org.apache.flink.api.java.functions.FunctionAnnotation.NonForwardedFieldsFirst) ForwardedFieldsFirst(org.apache.flink.api.java.functions.FunctionAnnotation.ForwardedFieldsFirst) NonForwardedFieldsFirst(org.apache.flink.api.java.functions.FunctionAnnotation.NonForwardedFieldsFirst) NonForwardedFields(org.apache.flink.api.java.functions.FunctionAnnotation.NonForwardedFields) NonForwardedFieldsSecond(org.apache.flink.api.java.functions.FunctionAnnotation.NonForwardedFieldsSecond) InvalidSemanticAnnotationException(org.apache.flink.api.common.operators.SemanticProperties.InvalidSemanticAnnotationException) SingleInputSemanticProperties(org.apache.flink.api.common.operators.SingleInputSemanticProperties) ReadFields(org.apache.flink.api.java.functions.FunctionAnnotation.ReadFields) ReadFieldsSecond(org.apache.flink.api.java.functions.FunctionAnnotation.ReadFieldsSecond)

Example 3 with InvalidSemanticAnnotationException

use of org.apache.flink.api.common.operators.SemanticProperties.InvalidSemanticAnnotationException in project flink by apache.

the class SemanticPropUtil method parseForwardedFields.

private static void parseForwardedFields(SemanticProperties sp, String[] forwardedStr, TypeInformation<?> inType, TypeInformation<?> outType, int input, boolean skipIncompatibleTypes) {
    if (forwardedStr == null) {
        return;
    }
    for (String s : forwardedStr) {
        if (s == null) {
            continue;
        }
        // remove white characters
        s = s.replaceAll("\\s", "");
        Matcher wcMatcher = PATTERN_WILDCARD.matcher(s);
        // simple wildcard
        if (wcMatcher.matches()) {
            if (!inType.equals(outType)) {
                if (skipIncompatibleTypes) {
                    continue;
                } else {
                    throw new InvalidSemanticAnnotationException("Forwarded field annotation \"" + s + "\" with wildcard only allowed for identical input and output types.");
                }
            }
            for (int i = 0; i < inType.getTotalFields(); i++) {
                if (sp instanceof SingleInputSemanticProperties) {
                    ((SingleInputSemanticProperties) sp).addForwardedField(i, i);
                } else if (sp instanceof DualInputSemanticProperties) {
                    ((DualInputSemanticProperties) sp).addForwardedField(input, i, i);
                }
            }
            return;
        }
        // check format of annotation string
        Matcher matcher = PATTERN_ANNOTATION.matcher(s);
        if (!matcher.matches()) {
            throw new InvalidSemanticAnnotationException("Invalid format of forwarded field annotation \"" + s + "\".");
        }
        // add forward annotations "->"
        Matcher forwardMatcher = PATTERN_FORWARD.matcher(s);
        while (forwardMatcher.find()) {
            String sourceStr = forwardMatcher.group(2);
            String targetStr = forwardMatcher.group(6);
            try {
                // check type compatibility
                if (!areFieldsCompatible(sourceStr, inType, targetStr, outType, !skipIncompatibleTypes)) {
                    if (skipIncompatibleTypes) {
                        continue;
                    } else {
                        throw new InvalidSemanticAnnotationException("Referenced fields of forwarded field annotation \"" + s + "\" do not match.");
                    }
                }
                List<FlatFieldDescriptor> inFFDs = getFlatFields(sourceStr, inType);
                List<FlatFieldDescriptor> outFFDs = getFlatFields(targetStr, outType);
                if (sp instanceof SingleInputSemanticProperties) {
                    for (int i = 0; i < inFFDs.size(); i++) {
                        int sourceField = inFFDs.get(i).getPosition();
                        int targetField = outFFDs.get(i).getPosition();
                        ((SingleInputSemanticProperties) sp).addForwardedField(sourceField, targetField);
                    }
                } else if (sp instanceof DualInputSemanticProperties) {
                    for (int i = 0; i < inFFDs.size(); i++) {
                        int sourceField = inFFDs.get(i).getPosition();
                        int targetField = outFFDs.get(i).getPosition();
                        ((DualInputSemanticProperties) sp).addForwardedField(input, sourceField, targetField);
                    }
                }
            } catch (InvalidFieldReferenceException ifre) {
                throw new InvalidSemanticAnnotationException("Invalid field reference in forwarded field annotation \"" + sourceStr + "->" + targetStr + "\".", ifre);
            } catch (InvalidSemanticAnnotationException isae) {
                throw new InvalidSemanticAnnotationException("Forwarded field annotation \"" + sourceStr + "->" + targetStr + "\" could not be added.", isae);
            }
        }
        // remove forward annotations
        s = forwardMatcher.replaceAll("");
        // add forwarded annotations
        Matcher listMatcher = PATTERN_LIST.matcher(s);
        while (listMatcher.find()) {
            String list = listMatcher.group();
            Matcher fieldMatcher = PATTERN_FIELD.matcher(list);
            // for each nested field
            while (fieldMatcher.find()) {
                String fieldStr = fieldMatcher.group();
                try {
                    // check if field is compatible in input and output type
                    if (!areFieldsCompatible(fieldStr, inType, fieldStr, outType, !skipIncompatibleTypes)) {
                        if (skipIncompatibleTypes) {
                            continue;
                        } else {
                            throw new InvalidSemanticAnnotationException("Referenced fields of forwarded field annotation \"" + s + "\" do not match.");
                        }
                    }
                    // add flat field positions
                    List<FlatFieldDescriptor> inFFDs = getFlatFields(fieldStr, inType);
                    List<FlatFieldDescriptor> outFFDs = getFlatFields(fieldStr, outType);
                    for (int i = 0; i < inFFDs.size(); i++) {
                        int sourcePos = inFFDs.get(i).getPosition();
                        int targetPos = outFFDs.get(i).getPosition();
                        if (sp instanceof SingleInputSemanticProperties) {
                            ((SingleInputSemanticProperties) sp).addForwardedField(sourcePos, targetPos);
                        } else if (sp instanceof DualInputSemanticProperties) {
                            ((DualInputSemanticProperties) sp).addForwardedField(input, sourcePos, targetPos);
                        }
                    }
                } catch (InvalidFieldReferenceException ifre) {
                    throw new InvalidSemanticAnnotationException("Invalid field reference in forwarded field annotation \"" + fieldStr + "\".", ifre);
                } catch (InvalidSemanticAnnotationException isae) {
                    throw new InvalidSemanticAnnotationException("Forwarded field annotation \"" + fieldStr + "\" could not be added.", isae);
                }
            }
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) InvalidSemanticAnnotationException(org.apache.flink.api.common.operators.SemanticProperties.InvalidSemanticAnnotationException) SingleInputSemanticProperties(org.apache.flink.api.common.operators.SingleInputSemanticProperties) DualInputSemanticProperties(org.apache.flink.api.common.operators.DualInputSemanticProperties) FlatFieldDescriptor(org.apache.flink.api.common.typeutils.CompositeType.FlatFieldDescriptor) InvalidFieldReferenceException(org.apache.flink.api.common.typeutils.CompositeType.InvalidFieldReferenceException)

Example 4 with InvalidSemanticAnnotationException

use of org.apache.flink.api.common.operators.SemanticProperties.InvalidSemanticAnnotationException in project flink by apache.

the class SemanticPropUtil method parseReadFields.

private static void parseReadFields(SemanticProperties sp, String[] readFieldStrings, TypeInformation<?> inType, int input) {
    if (readFieldStrings == null) {
        return;
    }
    for (String s : readFieldStrings) {
        FieldSet readFields = new FieldSet();
        // remove white characters
        s = s.replaceAll("\\s", "");
        Matcher wcMatcher = PATTERN_WILDCARD.matcher(s);
        // simple wildcard
        if (wcMatcher.matches()) {
            // add all fields
            for (int i = 0; i < inType.getTotalFields(); i++) {
                readFields = readFields.addField(i);
            }
        } else {
            // process field list
            Matcher matcher = PATTERN_LIST.matcher(s);
            if (!matcher.matches()) {
                throw new InvalidSemanticAnnotationException("Invalid format of read field annotation \"" + s + "\".");
            }
            // process field
            matcher = PATTERN_FIELD.matcher(s);
            while (matcher.find()) {
                String fieldStr = matcher.group();
                try {
                    List<FlatFieldDescriptor> ffds = getFlatFields(fieldStr, inType);
                    // get and add flat field positions
                    for (FlatFieldDescriptor ffd : ffds) {
                        readFields = readFields.addField(ffd.getPosition());
                    }
                } catch (InvalidFieldReferenceException ifre) {
                    throw new InvalidSemanticAnnotationException("Invalid field reference in read field annotation \"" + fieldStr + "\".", ifre);
                }
            }
        }
        if (sp instanceof SingleInputSemanticProperties) {
            ((SingleInputSemanticProperties) sp).addReadFields(readFields);
        } else if (sp instanceof DualInputSemanticProperties) {
            ((DualInputSemanticProperties) sp).addReadFields(input, readFields);
        }
    }
}
Also used : FieldSet(org.apache.flink.api.common.operators.util.FieldSet) Matcher(java.util.regex.Matcher) InvalidSemanticAnnotationException(org.apache.flink.api.common.operators.SemanticProperties.InvalidSemanticAnnotationException) SingleInputSemanticProperties(org.apache.flink.api.common.operators.SingleInputSemanticProperties) DualInputSemanticProperties(org.apache.flink.api.common.operators.DualInputSemanticProperties) FlatFieldDescriptor(org.apache.flink.api.common.typeutils.CompositeType.FlatFieldDescriptor) InvalidFieldReferenceException(org.apache.flink.api.common.typeutils.CompositeType.InvalidFieldReferenceException)

Example 5 with InvalidSemanticAnnotationException

use of org.apache.flink.api.common.operators.SemanticProperties.InvalidSemanticAnnotationException in project flink by apache.

the class SemanticPropUtil method parseNonForwardedFields.

private static void parseNonForwardedFields(SemanticProperties sp, String[] nonForwardedStr, TypeInformation<?> inType, TypeInformation<?> outType, int input, boolean skipIncompatibleTypes) {
    if (nonForwardedStr == null) {
        return;
    }
    FieldSet excludedFields = new FieldSet();
    for (String s : nonForwardedStr) {
        // remove white characters
        s = s.replaceAll("\\s", "");
        if (s.equals("")) {
            continue;
        }
        if (!inType.equals(outType)) {
            if (skipIncompatibleTypes) {
                continue;
            } else {
                throw new InvalidSemanticAnnotationException("Non-forwarded fields annotation only allowed for identical input and output types.");
            }
        }
        Matcher matcher = PATTERN_LIST.matcher(s);
        if (!matcher.matches()) {
            throw new InvalidSemanticAnnotationException("Invalid format of non-forwarded fields annotation \"" + s + "\".");
        }
        // process individual fields
        matcher = PATTERN_FIELD.matcher(s);
        while (matcher.find()) {
            String fieldStr = matcher.group();
            try {
                // get and add all flat field positions
                List<FlatFieldDescriptor> inFFDs = getFlatFields(fieldStr, inType);
                for (FlatFieldDescriptor ffd : inFFDs) {
                    excludedFields = excludedFields.addField(ffd.getPosition());
                }
            } catch (InvalidFieldReferenceException ifre) {
                throw new InvalidSemanticAnnotationException("Invalid field reference in non-forwarded fields annotation \"" + fieldStr + "\".", ifre);
            }
        }
    }
    for (int i = 0; i < inType.getTotalFields(); i++) {
        if (!excludedFields.contains(i)) {
            if (sp instanceof SingleInputSemanticProperties) {
                ((SingleInputSemanticProperties) sp).addForwardedField(i, i);
            } else if (sp instanceof DualInputSemanticProperties) {
                ((DualInputSemanticProperties) sp).addForwardedField(input, i, i);
            }
        }
    }
}
Also used : FieldSet(org.apache.flink.api.common.operators.util.FieldSet) Matcher(java.util.regex.Matcher) InvalidSemanticAnnotationException(org.apache.flink.api.common.operators.SemanticProperties.InvalidSemanticAnnotationException) SingleInputSemanticProperties(org.apache.flink.api.common.operators.SingleInputSemanticProperties) DualInputSemanticProperties(org.apache.flink.api.common.operators.DualInputSemanticProperties) FlatFieldDescriptor(org.apache.flink.api.common.typeutils.CompositeType.FlatFieldDescriptor) InvalidFieldReferenceException(org.apache.flink.api.common.typeutils.CompositeType.InvalidFieldReferenceException)

Aggregations

InvalidSemanticAnnotationException (org.apache.flink.api.common.operators.SemanticProperties.InvalidSemanticAnnotationException)5 DualInputSemanticProperties (org.apache.flink.api.common.operators.DualInputSemanticProperties)4 SingleInputSemanticProperties (org.apache.flink.api.common.operators.SingleInputSemanticProperties)4 Matcher (java.util.regex.Matcher)3 FlatFieldDescriptor (org.apache.flink.api.common.typeutils.CompositeType.FlatFieldDescriptor)3 InvalidFieldReferenceException (org.apache.flink.api.common.typeutils.CompositeType.InvalidFieldReferenceException)3 Annotation (java.lang.annotation.Annotation)2 FieldSet (org.apache.flink.api.common.operators.util.FieldSet)2 ForwardedFields (org.apache.flink.api.java.functions.FunctionAnnotation.ForwardedFields)2 ForwardedFieldsFirst (org.apache.flink.api.java.functions.FunctionAnnotation.ForwardedFieldsFirst)2 ForwardedFieldsSecond (org.apache.flink.api.java.functions.FunctionAnnotation.ForwardedFieldsSecond)2 NonForwardedFields (org.apache.flink.api.java.functions.FunctionAnnotation.NonForwardedFields)2 NonForwardedFieldsFirst (org.apache.flink.api.java.functions.FunctionAnnotation.NonForwardedFieldsFirst)2 NonForwardedFieldsSecond (org.apache.flink.api.java.functions.FunctionAnnotation.NonForwardedFieldsSecond)2 ReadFields (org.apache.flink.api.java.functions.FunctionAnnotation.ReadFields)2 ReadFieldsFirst (org.apache.flink.api.java.functions.FunctionAnnotation.ReadFieldsFirst)2 ReadFieldsSecond (org.apache.flink.api.java.functions.FunctionAnnotation.ReadFieldsSecond)2