77
88package org .elasticsearch .xpack .esql ;
99
10+ import org .elasticsearch .common .Strings ;
1011import org .elasticsearch .test .ESTestCase ;
1112
13+ import static org .elasticsearch .test .ESTestCase .randomBoolean ;
14+ import static org .elasticsearch .test .ESTestCase .randomFrom ;
15+ import static org .elasticsearch .test .ESTestCase .randomInt ;
16+ import static org .elasticsearch .test .ESTestCase .randomIntBetween ;
17+ import static org .elasticsearch .test .ESTestCase .randomList ;
18+ import static org .elasticsearch .test .ESTestCase .randomValueOtherThan ;
19+
1220public class IdentifierGenerator {
1321
1422 /**
@@ -22,7 +30,7 @@ public static String randomIdentifier() {
2230 * Generates one or several coma separated index patterns
2331 */
2432 public static String randomIndexPatterns (Feature ... features ) {
25- return maybeQuote (String .join ("," , ESTestCase . randomList (1 , 5 , () -> randomIndexPattern (features ))));
33+ return maybeQuote (String .join ("," , randomList (1 , 5 , () -> randomIndexPattern (features ))));
2634 }
2735
2836 /**
@@ -40,45 +48,61 @@ public static String randomIndexPattern(Feature... features) {
4048 index .append ('.' );
4149 }
4250 index .append (randomCharacterFrom (validFirstCharacters ));
43- for (int i = 0 ; i < ESTestCase . randomIntBetween (1 , 100 ); i ++) {
51+ for (int i = 0 ; i < randomIntBetween (1 , 100 ); i ++) {
4452 index .append (randomCharacterFrom (validCharacters ));
4553 }
4654 if (canAdd (Features .WILDCARD_PATTERN , features )) {
47- if (ESTestCase . randomBoolean ()) {
55+ if (randomBoolean ()) {
4856 index .append ('*' );
4957 } else {
50- index .insert (ESTestCase .randomIntBetween (0 , index .length () - 1 ), '*' );
58+ for (int i = 0 ; i < randomIntBetween (1 , 3 ); i ++) {
59+ index .insert (randomIntBetween (0 , index .length ()), '*' );
60+ }
5161 }
52- } else if (canAdd (Features .DATE_MATH , features )) {
62+ }
63+ if (canAdd (Features .DATE_MATH , features )) {
5364 // https://www.elastic.co/guide/en/elasticsearch/reference/8.17/api-conventions.html#api-date-math-index-names
5465 index .insert (0 , "<" );
5566 index .append ("-{now/" );
56- index .append (ESTestCase .randomFrom ("d" , "M" , "M-1M" ));
57- if (ESTestCase .randomBoolean ()) {
58- index .append ("{" ).append (ESTestCase .randomFrom ("yyyy.MM" , "yyyy.MM.dd" )).append ("}" );
67+ index .append (randomFrom ("d" , "M" , "M-1M" ));
68+ if (randomBoolean ()) {
69+ index .append ("{" ).append (switch (randomIntBetween (0 , 2 )) {
70+ case 0 -> "yyyy.MM" ;
71+ case 1 -> "yyyy.MM.dd" ;
72+ default -> "yyyy.MM.dd|" + Strings .format ("%+03d" , randomValueOtherThan (0 , () -> randomIntBetween (-18 , 18 ))) + ":00" ;
73+ }).append ("}" );
5974 }
6075 index .append ("}>" );
6176 }
77+ if (canAdd (Features .EXCLUDE_PATTERN , features )) {
78+ index .insert (0 , "-" );
79+ }
6280
6381 var pattern = maybeQuote (index .toString ());
6482 if (canAdd (Features .CROSS_CLUSTER , features )) {
6583 var cluster = randomIdentifier ();
6684 pattern = maybeQuote (cluster + ":" + pattern );
6785 }
86+
87+ if (pattern .contains ("|" ) && pattern .contains ("\" " ) == false ) {
88+ pattern = quote (pattern );
89+ }
90+
6891 return pattern ;
6992 }
7093
7194 private static char randomCharacterFrom (String str ) {
72- return str .charAt (ESTestCase . randomInt (str .length () - 1 ));
95+ return str .charAt (randomInt (str .length () - 1 ));
7396 }
7497
7598 public interface Feature {}
7699
77100 public enum Features implements Feature {
78101 CROSS_CLUSTER ,
102+ HIDDEN_INDEX ,
79103 WILDCARD_PATTERN ,
80- DATE_MATH ,
81- HIDDEN_INDEX
104+ EXCLUDE_PATTERN ,
105+ DATE_MATH
82106 }
83107
84108 private record ExcludedFeature (Feature feature ) implements Feature {}
@@ -96,18 +120,16 @@ private static boolean canAdd(Feature feature, Feature... features) {
96120 return false ;
97121 }
98122 }
99- return ESTestCase . randomBoolean ();
123+ return randomBoolean ();
100124 }
101125
102126 public static String maybeQuote (String term ) {
103- if (term .contains ("\" " )) {
104- return term ;
105- }
106- return switch (ESTestCase .randomIntBetween (0 , 5 )) {
107- case 0 -> "\" " + term + "\" " ;
108- case 1 -> "\" \" \" " + term + "\" \" \" " ;
109- default -> term ;// no quotes are more likely
110- };
127+ return randomBoolean () && term .contains ("\" " ) == false ? quote (term ) : term ;
128+ }
129+
130+ public static String quote (String term ) {
131+ var quote = randomFrom ("\" " , "\" \" \" " );
132+ return quote + term + quote ;
111133 }
112134
113135 public static String unquoteIndexPattern (String term ) {
0 commit comments