danmuzi commented on code in PR #16382:
URL: https://github.com/apache/lucene/pull/16382#discussion_r3560206034
##########
lucene/core/src/test/org/apache/lucene/search/TestWildcardQuery.java:
##########
@@ -235,6 +238,50 @@ public void testEscapes() throws Exception {
indexStore.close();
}
+ private static Automaton det(Automaton a) {
+ return Operations.determinize(a,
Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);
+ }
+
+ public void testRepeatedAsterisksCollapse() {
+ // Consecutive '*' must collapse to a single any-string automaton: same
+ // language as a single '*', and the same (tiny) shape, not a huge one.
+ Automaton single = det(WildcardQuery.toAutomaton(new Term("field", "*")));
+ Automaton repeated = det(WildcardQuery.toAutomaton(new Term("field",
"*".repeat(1000))));
+
+ assertTrue(AutomatonTestUtil.sameLanguage(single, repeated));
+ assertEquals(single.getNumStates(), repeated.getNumStates());
+ assertEquals(single.getNumTransitions(), repeated.getNumTransitions());
+ }
+
+ public void testRepeatedAsterisksCollapseWithEscapesAndLiterals() {
+ // Collapsing applies only to unescaped, consecutive '*'. An escaped '\*'
+ // stays a literal and the surrounding literals / '?' are untouched, so
+ // "a\*b***c?" matches the same language as its single-'*' form "a\*b*c?".
+ Automaton collapsed = det(WildcardQuery.toAutomaton(new Term("field",
"a\\*b***c?")));
+ Automaton reference = det(WildcardQuery.toAutomaton(new Term("field",
"a\\*b*c?")));
+
+ assertTrue(AutomatonTestUtil.sameLanguage(reference, collapsed));
+ assertEquals(reference.getNumStates(), collapsed.getNumStates());
+ assertEquals(reference.getNumTransitions(), collapsed.getNumTransitions());
+
+ // Escaped '\*' is a literal, so "\*\*\*" matches the string "***", not the
+ // any-string '*' — it must NOT be collapsed.
+ Automaton single = det(WildcardQuery.toAutomaton(new Term("field", "*")));
+ Automaton literalStars = det(WildcardQuery.toAutomaton(new Term("field",
"\\*\\*\\*")));
+ assertFalse(AutomatonTestUtil.sameLanguage(single, literalStars));
+ }
+
+ public void testInterleavedStarQuestionDoesNotBlowUp() {
+ // '*?' repeated does not collapse (the '*' are not consecutive), but it
+ // must still build a linearly-sized automaton rather than exploding the
way
+ // repeated '*' used to before this fix (gh-16134).
Review Comment:
```suggestion
// repeated '*' used to before this fix (GITHUB#16134).
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]