Search in sources :

Example 1 with PatternProcessFunctionRunner

use of org.apache.flink.table.runtime.operators.match.PatternProcessFunctionRunner in project flink by apache.

the class StreamExecMatch method translateToPlanInternal.

@SuppressWarnings("unchecked")
@Override
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
    final ExecEdge inputEdge = getInputEdges().get(0);
    final Transformation<RowData> inputTransform = (Transformation<RowData>) inputEdge.translateToPlan(planner);
    final RowType inputRowType = (RowType) inputEdge.getOutputType();
    checkOrderKeys(inputRowType);
    final EventComparator<RowData> eventComparator = createEventComparator(config, inputRowType);
    final Transformation<RowData> timestampedInputTransform = translateOrder(inputTransform, inputRowType);
    final Tuple2<Pattern<RowData, RowData>, List<String>> cepPatternAndNames = translatePattern(matchSpec, config.getTableConfig(), planner.getRelBuilder(), inputRowType);
    final Pattern<RowData, RowData> cepPattern = cepPatternAndNames.f0;
    // TODO remove this once it is supported in CEP library
    if (NFACompiler.canProduceEmptyMatches(cepPattern)) {
        throw new TableException("Patterns that can produce empty matches are not supported. There must be at least one non-optional state.");
    }
    // TODO remove this once it is supported in CEP library
    if (cepPattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.GREEDY)) {
        throw new TableException("Greedy quantifiers are not allowed as the last element of a Pattern yet. " + "Finish your pattern with either a simple variable or reluctant quantifier.");
    }
    if (matchSpec.isAllRows()) {
        throw new TableException("All rows per match mode is not supported yet.");
    }
    final int[] partitionKeys = matchSpec.getPartition().getFieldIndices();
    final SortSpec.SortFieldSpec timeOrderField = matchSpec.getOrderKeys().getFieldSpec(0);
    final LogicalType timeOrderFieldType = inputRowType.getTypeAt(timeOrderField.getFieldIndex());
    final boolean isProctime = TypeCheckUtils.isProcTime(timeOrderFieldType);
    final InternalTypeInfo<RowData> inputTypeInfo = (InternalTypeInfo<RowData>) inputTransform.getOutputType();
    final TypeSerializer<RowData> inputSerializer = inputTypeInfo.createSerializer(planner.getExecEnv().getConfig());
    final NFACompiler.NFAFactory<RowData> nfaFactory = NFACompiler.compileFactory(cepPattern, false);
    final MatchCodeGenerator generator = new MatchCodeGenerator(new CodeGeneratorContext(config.getTableConfig()), planner.getRelBuilder(), // nullableInput
    false, JavaScalaConversionUtil.toScala(cepPatternAndNames.f1), JavaScalaConversionUtil.toScala(Optional.empty()), CodeGenUtils.DEFAULT_COLLECTOR_TERM());
    generator.bindInput(inputRowType, CodeGenUtils.DEFAULT_INPUT1_TERM(), JavaScalaConversionUtil.toScala(Optional.empty()));
    final PatternProcessFunctionRunner patternProcessFunction = generator.generateOneRowPerMatchExpression((RowType) getOutputType(), partitionKeys, matchSpec.getMeasures());
    final CepOperator<RowData, RowData, RowData> operator = new CepOperator<>(inputSerializer, isProctime, nfaFactory, eventComparator, cepPattern.getAfterMatchSkipStrategy(), patternProcessFunction, null);
    final OneInputTransformation<RowData, RowData> transform = ExecNodeUtil.createOneInputTransformation(timestampedInputTransform, createTransformationMeta(MATCH_TRANSFORMATION, config), operator, InternalTypeInfo.of(getOutputType()), timestampedInputTransform.getParallelism());
    final RowDataKeySelector selector = KeySelectorUtil.getRowDataSelector(partitionKeys, inputTypeInfo);
    transform.setStateKeySelector(selector);
    transform.setStateKeyType(selector.getProducedType());
    if (inputsContainSingleton()) {
        transform.setParallelism(1);
        transform.setMaxParallelism(1);
    }
    return transform;
}
Also used : OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) Transformation(org.apache.flink.api.dag.Transformation) ExecEdge(org.apache.flink.table.planner.plan.nodes.exec.ExecEdge) RowType(org.apache.flink.table.types.logical.RowType) LogicalType(org.apache.flink.table.types.logical.LogicalType) NFACompiler(org.apache.flink.cep.nfa.compiler.NFACompiler) RowData(org.apache.flink.table.data.RowData) RowDataKeySelector(org.apache.flink.table.runtime.keyselector.RowDataKeySelector) List(java.util.List) ArrayList(java.util.ArrayList) Pattern(org.apache.flink.cep.pattern.Pattern) TableException(org.apache.flink.table.api.TableException) CodeGeneratorContext(org.apache.flink.table.planner.codegen.CodeGeneratorContext) InternalTypeInfo(org.apache.flink.table.runtime.typeutils.InternalTypeInfo) MatchCodeGenerator(org.apache.flink.table.planner.codegen.MatchCodeGenerator) PatternProcessFunctionRunner(org.apache.flink.table.runtime.operators.match.PatternProcessFunctionRunner) CepOperator(org.apache.flink.cep.operator.CepOperator) SortSpec(org.apache.flink.table.planner.plan.nodes.exec.spec.SortSpec)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 Transformation (org.apache.flink.api.dag.Transformation)1 NFACompiler (org.apache.flink.cep.nfa.compiler.NFACompiler)1 CepOperator (org.apache.flink.cep.operator.CepOperator)1 Pattern (org.apache.flink.cep.pattern.Pattern)1 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)1 TableException (org.apache.flink.table.api.TableException)1 RowData (org.apache.flink.table.data.RowData)1 CodeGeneratorContext (org.apache.flink.table.planner.codegen.CodeGeneratorContext)1 MatchCodeGenerator (org.apache.flink.table.planner.codegen.MatchCodeGenerator)1 ExecEdge (org.apache.flink.table.planner.plan.nodes.exec.ExecEdge)1 SortSpec (org.apache.flink.table.planner.plan.nodes.exec.spec.SortSpec)1 RowDataKeySelector (org.apache.flink.table.runtime.keyselector.RowDataKeySelector)1 PatternProcessFunctionRunner (org.apache.flink.table.runtime.operators.match.PatternProcessFunctionRunner)1 InternalTypeInfo (org.apache.flink.table.runtime.typeutils.InternalTypeInfo)1 LogicalType (org.apache.flink.table.types.logical.LogicalType)1 RowType (org.apache.flink.table.types.logical.RowType)1