Search in sources :

Example 1 with ExpressionKeys

use of org.apache.flink.api.common.operators.Keys.ExpressionKeys in project flink by apache.

the class UdfAnalyzer method analyze.

public boolean analyze() throws CodeAnalyzerException {
    if (state == STATE_END_OF_ANALYZING) {
        throw new IllegalStateException("Analyzing is already done.");
    }
    boolean discardReturnValues = false;
    if (isIterableInput) {
        if (keys1 == null || (keys2 == null && isBinary)) {
            throw new IllegalArgumentException("This type of function requires key information for analysis.");
        } else if (!(keys1 instanceof ExpressionKeys) || (!(keys2 instanceof ExpressionKeys) && isBinary)) {
            // TODO currently only ExpressionKeys are supported as keys
            discardReturnValues = true;
        }
    }
    try {
        final Object[] mn = findMethodNode(internalUdfClassName, baseClassMethod);
        final NestedMethodAnalyzer nma = new NestedMethodAnalyzer(this, (String) mn[1], (MethodNode) mn[0], null, MAX_NESTING, true);
        final TaggedValue result = nma.analyze();
        setState(STATE_END_OF_ANALYZING);
        // special case: FilterFunction
        if (isFilterFunction) {
            discardReturnValues = true;
            // check for input modification
            if (!filterInputCopy.equals(filterInputRef)) {
                addHintOrThrowException("Function modifies the input. This can lead to unexpected behaviour during runtime.");
            }
        }
        if (!discardReturnValues) {
            // merge return values of a collector
            if (hasCollector) {
                returnValue = mergeReturnValues(collectorValues);
            } else {
                returnValue = result;
            }
            // or is a reduce function
            if ((isIterableInput || isReduceFunction) && returnValue != null) {
                if (returnValue.canContainFields()) {
                    removeUngroupedInputsFromContainer(returnValue);
                } else if (returnValue.isInput() && !returnValue.isGrouped()) {
                    returnValue = null;
                }
            }
        } else // any return value is invalid
        {
            returnValue = null;
        }
    } catch (Exception e) {
        Throwable cause = e.getCause();
        while (cause != null && !(cause instanceof CodeErrorException)) {
            cause = cause.getCause();
        }
        if ((cause != null && cause instanceof CodeErrorException) || e instanceof CodeErrorException) {
            throw new CodeErrorException("Function code contains obvious errors. " + "If you think the code analysis is wrong at this point you can " + "disable the entire code analyzer in ExecutionConfig or add" + " @SkipCodeAnalysis to your function to disable the analysis.", (cause != null) ? cause : e);
        }
        throw new CodeAnalyzerException("Exception occurred during code analysis.", e);
    }
    return true;
}
Also used : ExpressionKeys(org.apache.flink.api.common.operators.Keys.ExpressionKeys) UdfAnalyzerUtils.convertTypeInfoToTaggedValue(org.apache.flink.api.java.sca.UdfAnalyzerUtils.convertTypeInfoToTaggedValue)

Example 2 with ExpressionKeys

use of org.apache.flink.api.common.operators.Keys.ExpressionKeys in project flink by apache.

the class ExpressionKeysTest method testAreCompatible8.

@Test
public void testAreCompatible8() throws Keys.IncompatibleKeysException {
    TypeInformation<String> t1 = BasicTypeInfo.STRING_TYPE_INFO;
    TypeInformation<Pojo2> t2 = TypeExtractor.getForClass(Pojo2.class);
    ExpressionKeys<String> ek1 = new ExpressionKeys<>("*", t1);
    Keys<Pojo2> ek2 = new Keys.SelectorFunctionKeys<>(new KeySelector1(), t2, BasicTypeInfo.STRING_TYPE_INFO);
    Assert.assertTrue(ek1.areCompatible(ek2));
}
Also used : KeySelector1(org.apache.flink.api.common.operators.SelectorFunctionKeysTest.KeySelector1) ExpressionKeys(org.apache.flink.api.common.operators.Keys.ExpressionKeys) Test(org.junit.Test)

Example 3 with ExpressionKeys

use of org.apache.flink.api.common.operators.Keys.ExpressionKeys in project flink by apache.

the class ExpressionKeysTest method testAreCompatible9.

@Test
public void testAreCompatible9() throws Keys.IncompatibleKeysException {
    TypeInformation<Tuple3<String, Long, Integer>> t1 = new TupleTypeInfo<>(BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.LONG_TYPE_INFO, BasicTypeInfo.INT_TYPE_INFO);
    TypeInformation<PojoWithMultiplePojos> t2 = TypeExtractor.getForClass(PojoWithMultiplePojos.class);
    ExpressionKeys<Tuple3<String, Long, Integer>> ek1 = new ExpressionKeys<>(new int[] { 2, 0 }, t1);
    Keys<PojoWithMultiplePojos> ek2 = new Keys.SelectorFunctionKeys<>(new KeySelector3(), t2, new TupleTypeInfo<Tuple2<Integer, String>>(BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO));
    Assert.assertTrue(ek1.areCompatible(ek2));
}
Also used : KeySelector3(org.apache.flink.api.common.operators.SelectorFunctionKeysTest.KeySelector3) ExpressionKeys(org.apache.flink.api.common.operators.Keys.ExpressionKeys) Tuple2(org.apache.flink.api.java.tuple.Tuple2) Tuple3(org.apache.flink.api.java.tuple.Tuple3) TupleTypeInfo(org.apache.flink.api.java.typeutils.TupleTypeInfo) Test(org.junit.Test)

Example 4 with ExpressionKeys

use of org.apache.flink.api.common.operators.Keys.ExpressionKeys 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)

Example 5 with ExpressionKeys

use of org.apache.flink.api.common.operators.Keys.ExpressionKeys in project flink by apache.

the class GroupReduceOperator method translateToDataFlow.

// --------------------------------------------------------------------------------------------
// Translation
// --------------------------------------------------------------------------------------------
@Override
@SuppressWarnings("unchecked")
protected GroupReduceOperatorBase<?, OUT, ?> translateToDataFlow(Operator<IN> input) {
    String name = getName() != null ? getName() : "GroupReduce at " + defaultName;
    // wrap CombineFunction in GroupCombineFunction if combinable
    if (combinable && function instanceof CombineFunction<?, ?>) {
        this.function = function instanceof RichGroupReduceFunction<?, ?> ? new RichCombineToGroupCombineWrapper((RichGroupReduceFunction<?, ?>) function) : new CombineToGroupCombineWrapper((CombineFunction<?, ?>) function);
    }
    // distinguish between grouped reduce and non-grouped reduce
    if (grouper == null) {
        // non grouped reduce
        UnaryOperatorInformation<IN, OUT> operatorInfo = new UnaryOperatorInformation<>(getInputType(), getResultType());
        GroupReduceOperatorBase<IN, OUT, GroupReduceFunction<IN, OUT>> po = new GroupReduceOperatorBase<>(function, operatorInfo, new int[0], name);
        po.setCombinable(combinable);
        po.setInput(input);
        // the parallelism for a non grouped reduce can only be 1
        po.setParallelism(1);
        return po;
    }
    if (grouper.getKeys() instanceof SelectorFunctionKeys) {
        @SuppressWarnings("unchecked") SelectorFunctionKeys<IN, ?> selectorKeys = (SelectorFunctionKeys<IN, ?>) grouper.getKeys();
        if (grouper instanceof SortedGrouping) {
            SortedGrouping<IN> sortedGrouping = (SortedGrouping<IN>) grouper;
            SelectorFunctionKeys<IN, ?> sortKeys = sortedGrouping.getSortSelectionFunctionKey();
            Ordering groupOrder = sortedGrouping.getGroupOrdering();
            PlanUnwrappingSortedReduceGroupOperator<IN, OUT, ?, ?> po = translateSelectorFunctionSortedReducer(selectorKeys, sortKeys, groupOrder, function, getResultType(), name, input, isCombinable());
            po.setParallelism(this.getParallelism());
            po.setCustomPartitioner(grouper.getCustomPartitioner());
            return po;
        } else {
            PlanUnwrappingReduceGroupOperator<IN, OUT, ?> po = translateSelectorFunctionReducer(selectorKeys, function, getResultType(), name, input, isCombinable());
            po.setParallelism(this.getParallelism());
            po.setCustomPartitioner(grouper.getCustomPartitioner());
            return po;
        }
    } else if (grouper.getKeys() instanceof ExpressionKeys) {
        int[] logicalKeyPositions = grouper.getKeys().computeLogicalKeyPositions();
        UnaryOperatorInformation<IN, OUT> operatorInfo = new UnaryOperatorInformation<>(getInputType(), getResultType());
        GroupReduceOperatorBase<IN, OUT, GroupReduceFunction<IN, OUT>> po = new GroupReduceOperatorBase<>(function, operatorInfo, logicalKeyPositions, name);
        po.setCombinable(combinable);
        po.setInput(input);
        po.setParallelism(getParallelism());
        po.setCustomPartitioner(grouper.getCustomPartitioner());
        // set group order
        if (grouper instanceof SortedGrouping) {
            SortedGrouping<IN> sortedGrouper = (SortedGrouping<IN>) grouper;
            int[] sortKeyPositions = sortedGrouper.getGroupSortKeyPositions();
            Order[] sortOrders = sortedGrouper.getGroupSortOrders();
            Ordering o = new Ordering();
            for (int i = 0; i < sortKeyPositions.length; i++) {
                o.appendOrdering(sortKeyPositions[i], null, sortOrders[i]);
            }
            po.setGroupOrder(o);
        }
        return po;
    } else {
        throw new UnsupportedOperationException("Unrecognized key type.");
    }
}
Also used : RichGroupReduceFunction(org.apache.flink.api.common.functions.RichGroupReduceFunction) SelectorFunctionKeys(org.apache.flink.api.common.operators.Keys.SelectorFunctionKeys) RichGroupReduceFunction(org.apache.flink.api.common.functions.RichGroupReduceFunction) GroupReduceFunction(org.apache.flink.api.common.functions.GroupReduceFunction) ExpressionKeys(org.apache.flink.api.common.operators.Keys.ExpressionKeys) RichCombineToGroupCombineWrapper(org.apache.flink.api.java.operators.translation.RichCombineToGroupCombineWrapper) CombineToGroupCombineWrapper(org.apache.flink.api.java.operators.translation.CombineToGroupCombineWrapper) UnaryOperatorInformation(org.apache.flink.api.common.operators.UnaryOperatorInformation) RichCombineToGroupCombineWrapper(org.apache.flink.api.java.operators.translation.RichCombineToGroupCombineWrapper) GroupReduceOperatorBase(org.apache.flink.api.common.operators.base.GroupReduceOperatorBase) Ordering(org.apache.flink.api.common.operators.Ordering)

Aggregations

ExpressionKeys (org.apache.flink.api.common.operators.Keys.ExpressionKeys)7 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)3 Test (org.junit.Test)3 CompositeType (org.apache.flink.api.common.typeutils.CompositeType)2 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)2 TupleTypeInfo (org.apache.flink.api.java.typeutils.TupleTypeInfo)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 GroupReduceFunction (org.apache.flink.api.common.functions.GroupReduceFunction)1 RichGroupReduceFunction (org.apache.flink.api.common.functions.RichGroupReduceFunction)1 IncompatibleKeysException (org.apache.flink.api.common.operators.Keys.IncompatibleKeysException)1 SelectorFunctionKeys (org.apache.flink.api.common.operators.Keys.SelectorFunctionKeys)1 Ordering (org.apache.flink.api.common.operators.Ordering)1 KeySelector1 (org.apache.flink.api.common.operators.SelectorFunctionKeysTest.KeySelector1)1 KeySelector3 (org.apache.flink.api.common.operators.SelectorFunctionKeysTest.KeySelector3)1 UnaryOperatorInformation (org.apache.flink.api.common.operators.UnaryOperatorInformation)1 GroupReduceOperatorBase (org.apache.flink.api.common.operators.base.GroupReduceOperatorBase)1 FlatFieldDescriptor (org.apache.flink.api.common.typeutils.CompositeType.FlatFieldDescriptor)1 CombineToGroupCombineWrapper (org.apache.flink.api.java.operators.translation.CombineToGroupCombineWrapper)1 RichCombineToGroupCombineWrapper (org.apache.flink.api.java.operators.translation.RichCombineToGroupCombineWrapper)1