You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by di...@apache.org on 2023/02/14 09:13:03 UTC

[flink] branch release-1.15 updated: [FLINK-30864][FLINK-30885][CEP] Fix wrong NFA related to optional decoration at the beginning of group pattern

This is an automated email from the ASF dual-hosted git repository.

dianfu pushed a commit to branch release-1.15
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/release-1.15 by this push:
     new 1ac1e0d7dd0 [FLINK-30864][FLINK-30885][CEP] Fix wrong NFA related to optional decoration at the beginning of group pattern
1ac1e0d7dd0 is described below

commit 1ac1e0d7dd078a85cb0f127aa75e161e03b2fd41
Author: Juntao Hu <ma...@gmail.com>
AuthorDate: Mon Feb 6 20:14:54 2023 +0800

    [FLINK-30864][FLINK-30885][CEP] Fix wrong NFA related to optional decoration at the beginning of group pattern
    
    This closes #21866.
---
 .../apache/flink/cep/nfa/compiler/NFACompiler.java | 29 ++++----
 .../java/org/apache/flink/cep/nfa/GroupITCase.java | 80 ++++++++++++++++++++++
 2 files changed, 93 insertions(+), 16 deletions(-)

diff --git a/flink-libraries/flink-cep/src/main/java/org/apache/flink/cep/nfa/compiler/NFACompiler.java b/flink-libraries/flink-cep/src/main/java/org/apache/flink/cep/nfa/compiler/NFACompiler.java
index 0e9ed184557..38211dce865 100644
--- a/flink-libraries/flink-cep/src/main/java/org/apache/flink/cep/nfa/compiler/NFACompiler.java
+++ b/flink-libraries/flink-cep/src/main/java/org/apache/flink/cep/nfa/compiler/NFACompiler.java
@@ -553,9 +553,7 @@ public class NFACompiler {
                     proceedState,
                     takeCondition,
                     getIgnoreCondition(currentPattern),
-                    currentPattern
-                            .getQuantifier()
-                            .hasProperty(Quantifier.QuantifierProperty.OPTIONAL));
+                    isPatternOptional(currentPattern));
         }
 
         /**
@@ -599,14 +597,17 @@ public class NFACompiler {
          * pattern, the optional status depends on the group pattern.
          */
         private boolean isPatternOptional(Pattern<T, ?> pattern) {
-            if (headOfGroup(pattern)) {
-                return isCurrentGroupPatternFirstOfLoop()
-                        && currentGroupPattern
-                                .getQuantifier()
-                                .hasProperty(Quantifier.QuantifierProperty.OPTIONAL);
-            } else {
-                return pattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.OPTIONAL);
+            return pattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.OPTIONAL);
+        }
+
+        private boolean isHeadOfOptionalGroupPattern(Pattern<T, ?> pattern) {
+            if (!headOfGroup(pattern)) {
+                return false;
             }
+            return isCurrentGroupPatternFirstOfLoop()
+                    && currentGroupPattern
+                            .getQuantifier()
+                            .hasProperty(Quantifier.QuantifierProperty.OPTIONAL);
         }
 
         /**
@@ -660,11 +661,7 @@ public class NFACompiler {
             // if no element accepted the previous nots are still valid.
             final IterativeCondition<T> proceedCondition = getTrueFunction();
 
-            // for the first state of a group pattern, its PROCEED edge should point to the
-            // following state of
-            // that group pattern and the edge will be added at the end of creating the NFA for that
-            // group pattern
-            if (isOptional && !headOfGroup(currentPattern)) {
+            if (isOptional) {
                 if (currentPattern
                         .getQuantifier()
                         .hasProperty(Quantifier.QuantifierProperty.GREEDY)) {
@@ -689,7 +686,7 @@ public class NFACompiler {
 
             if (ignoreCondition != null) {
                 final State<T> ignoreState;
-                if (isOptional) {
+                if (isOptional || isHeadOfOptionalGroupPattern(currentPattern)) {
                     ignoreState = createState(currentPattern.getName(), State.StateType.Normal);
                     ignoreState.addTake(sink, takeCondition);
                     ignoreState.addIgnore(ignoreCondition);
diff --git a/flink-libraries/flink-cep/src/test/java/org/apache/flink/cep/nfa/GroupITCase.java b/flink-libraries/flink-cep/src/test/java/org/apache/flink/cep/nfa/GroupITCase.java
index bc76acb5757..c2086b7864a 100644
--- a/flink-libraries/flink-cep/src/test/java/org/apache/flink/cep/nfa/GroupITCase.java
+++ b/flink-libraries/flink-cep/src/test/java/org/apache/flink/cep/nfa/GroupITCase.java
@@ -1470,4 +1470,84 @@ public class GroupITCase extends TestLogger {
         assertEquals(1, nfaState.getPartialMatches().size());
         assertEquals("start", nfaState.getPartialMatches().peek().getCurrentStateName());
     }
+
+    @Test
+    public void testGroupStartsWithOptionalPattern() throws Exception {
+        List<StreamRecord<Event>> inputEvents = new ArrayList<>();
+
+        Event a = new Event(40, "a", 1.0);
+        Event c = new Event(41, "c", 2.0);
+        Event d = new Event(42, "d", 3.0);
+
+        inputEvents.add(new StreamRecord<>(a, 1));
+        inputEvents.add(new StreamRecord<>(c, 2));
+        inputEvents.add(new StreamRecord<>(d, 3));
+
+        // a (b? c) d
+        Pattern<Event, ?> pattern =
+                Pattern.<Event>begin("start")
+                        .where(SimpleCondition.of(value -> value.getName().equals("a")))
+                        .next(
+                                Pattern.<Event>begin("middle1")
+                                        .where(
+                                                SimpleCondition.of(
+                                                        value -> value.getName().equals("b")))
+                                        .optional()
+                                        .next("middle2")
+                                        .where(
+                                                SimpleCondition.of(
+                                                        value -> value.getName().equals("c"))))
+                        .next("d")
+                        .where(SimpleCondition.of(value -> value.getName().equals("d")));
+
+        NFA<Event> nfa = compile(pattern, false);
+
+        NFAState nfaState = nfa.createInitialNFAState();
+
+        NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).withNFAState(nfaState).build();
+        final List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(inputEvents);
+
+        comparePatterns(
+                resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(a, c, d)));
+    }
+
+    @Test
+    public void testFollowedByOptionalGroupPattern() throws Exception {
+        List<StreamRecord<Event>> inputEvents = new ArrayList<>();
+
+        Event a = new Event(40, "a", 1.0);
+        Event d1 = new Event(41, "d", 2.0);
+        Event d2 = new Event(42, "d", 3.0);
+
+        inputEvents.add(new StreamRecord<>(a, 1));
+        inputEvents.add(new StreamRecord<>(d1, 2));
+        inputEvents.add(new StreamRecord<>(d2, 3));
+
+        // a -> (b c)? d
+        Pattern<Event, ?> pattern =
+                Pattern.<Event>begin("start")
+                        .where(SimpleCondition.of(value -> value.getName().equals("a")))
+                        .followedBy(
+                                Pattern.<Event>begin("middle1")
+                                        .where(
+                                                SimpleCondition.of(
+                                                        value -> value.getName().equals("b")))
+                                        .next("middle2")
+                                        .where(
+                                                SimpleCondition.of(
+                                                        value -> value.getName().equals("c"))))
+                        .optional()
+                        .next("d")
+                        .where(SimpleCondition.of(value -> value.getName().equals("d")));
+
+        NFA<Event> nfa = compile(pattern, false);
+
+        NFAState nfaState = nfa.createInitialNFAState();
+
+        NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).withNFAState(nfaState).build();
+        final List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(inputEvents);
+
+        comparePatterns(
+                resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(a, d1)));
+    }
 }