-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Avoid unnecessary determinization in index pattern conflict checks #128362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e6e613c
090da15
268dc56
f0e324d
304b65f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 128362 | ||
summary: Avoid unnecessary determinization in index pattern conflict checks | ||
area: Indices APIs | ||
type: bug | ||
issues: [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,16 +10,20 @@ | |
|
||
import org.apache.lucene.util.automaton.Automaton; | ||
import org.apache.lucene.util.automaton.CharacterRunAutomaton; | ||
import org.apache.lucene.util.automaton.Operations; | ||
import org.apache.lucene.util.automaton.TooComplexToDeterminizeException; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Locale; | ||
import java.util.Random; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
import static org.elasticsearch.test.LambdaMatchers.falseWith; | ||
import static org.elasticsearch.test.LambdaMatchers.trueWith; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class RegexTests extends ESTestCase { | ||
|
@@ -250,4 +254,14 @@ public void testThousandsAndLongPattern() throws IOException { | |
assertTrue(predicate.test(patterns[i])); | ||
} | ||
} | ||
|
||
public void testIntersectNonDeterminizedAutomaton() { | ||
String[] patterns = randomArray(20, 100, size -> new String[size], () -> "*" + randomAlphanumericOfLength(10) + "*"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add some minor detailing about how these size-related values were chosen? If I'm not mistaken, don't they relate to the default determinisation limit that comes from Lucene? |
||
Automaton a = Regex.simpleMatchToNonDeterminizedAutomaton(patterns); | ||
Automaton b = Regex.simpleMatchToNonDeterminizedAutomaton(Arrays.copyOfRange(patterns, patterns.length / 2, patterns.length)); | ||
assertFalse(Operations.isEmpty(Operations.intersection(a, b))); | ||
IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> assertMatchesAll(a, "my_test")); | ||
assertThat(exc.getMessage(), containsString("deterministic")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit pick: can we improve the expectation around the exception's error message? Readers like me could be curious about what kind of message to expect. |
||
expectThrows(TooComplexToDeterminizeException.class, () -> Regex.simpleMatchToAutomaton(patterns)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
small drive-by comment: should add to the javadocs when one or the other should be used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, Luca. I've raised a similar question internally within the relevant discussion thread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++, 268dc56
I also added the variant for the single pattern case since to be complete.