Skip to content

Commit 4129768

Browse files
ES|QL: generative tests - fix identifiers generation and source field mapping (#126514)
1 parent b692d1a commit 4129768

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

x-pack/plugin/esql/qa/server/src/main/java/org/elasticsearch/xpack/esql/qa/rest/generative/EsqlQueryGenerator.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ private static String grok(List<Column> previousOutput) {
160160
}
161161
result.append("%{WORD:");
162162
if (randomBoolean()) {
163-
result.append(randomAlphaOfLength(5));
163+
result.append(randomIdentifier());
164164
} else {
165165
String fieldName = randomRawName(previousOutput);
166166
if (fieldName.isEmpty()) { // it's a bug, managed later, skipping for now
167-
fieldName = randomAlphaOfLength(5);
167+
fieldName = randomIdentifier();
168168
}
169169
result.append(fieldName);
170170
}
@@ -188,11 +188,11 @@ private static String dissect(List<Column> previousOutput) {
188188
}
189189
result.append("%{");
190190
if (randomBoolean()) {
191-
result.append(randomAlphaOfLength(5));
191+
result.append(randomIdentifier());
192192
} else {
193193
String fieldName = randomRawName(previousOutput);
194194
if (fieldName.isEmpty()) { // it's a bug, managed later, skipping for now
195-
fieldName = randomAlphaOfLength(5);
195+
fieldName = randomIdentifier();
196196
}
197197
result.append(fieldName);
198198
}
@@ -302,7 +302,7 @@ private static String rename(List<Column> previousOutput) {
302302

303303
String newName;
304304
if (names.isEmpty() || randomBoolean()) {
305-
newName = randomAlphaOfLength(5);
305+
newName = randomIdentifier();
306306
names.add(newName);
307307
} else {
308308
newName = names.get(randomIntBetween(0, names.size() - 1));
@@ -376,7 +376,7 @@ private static String eval(List<Column> previousOutput) {
376376
for (int i = 0; i < nFields; i++) {
377377
String name;
378378
if (randomBoolean()) {
379-
name = randomAlphaOfLength(randomIntBetween(3, 10));
379+
name = randomIdentifier();
380380
} else {
381381
name = randomName(previousOutput);
382382
}
@@ -402,7 +402,7 @@ private static String stats(List<Column> previousOutput) {
402402
for (int i = 0; i < nStats; i++) {
403403
String name;
404404
if (randomBoolean()) {
405-
name = randomAlphaOfLength(randomIntBetween(3, 10));
405+
name = randomIdentifier();
406406
} else {
407407
name = randomName(previousOutput);
408408
}
@@ -501,7 +501,7 @@ private static String row() {
501501
StringBuilder cmd = new StringBuilder("row ");
502502
int nFields = randomIntBetween(1, 10);
503503
for (int i = 0; i < nFields; i++) {
504-
String name = randomAlphaOfLength(randomIntBetween(3, 10));
504+
String name = randomIdentifier();
505505
String expression = constantExpression();
506506
if (i > 0) {
507507
cmd.append(",");
@@ -526,4 +526,10 @@ private static String constantExpression() {
526526

527527
}
528528

529+
private static String randomIdentifier() {
530+
// Let's create identifiers that are long enough to avoid collisions with reserved keywords.
531+
// There could be a smarter way (introspection on the lexer class?), but probably it's not worth the effort
532+
return randomAlphaOfLength(randomIntBetween(8, 12));
533+
}
534+
529535
}

x-pack/plugin/esql/qa/server/src/main/java/org/elasticsearch/xpack/esql/qa/rest/generative/GenerativeRestTest.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import static org.elasticsearch.xpack.esql.CsvTestsDataLoader.CSV_DATASET_MAP;
2727
import static org.elasticsearch.xpack.esql.CsvTestsDataLoader.ENRICH_POLICIES;
28+
import static org.elasticsearch.xpack.esql.CsvTestsDataLoader.availableDatasetsForEs;
2829
import static org.elasticsearch.xpack.esql.CsvTestsDataLoader.loadDataSetIntoEs;
2930

3031
public abstract class GenerativeRestTest extends ESRestTestCase {
@@ -41,7 +42,6 @@ public abstract class GenerativeRestTest extends ESRestTestCase {
4142
"Unbounded sort not supported yet",
4243
"The field names are too complex to process", // field_caps problem
4344
"must be \\[any type except counter types\\]", // TODO refine the generation of count()
44-
"mismatched input .* expecting", // identifier generator needs to be refined, this happens when an identifier is a reserved keyword
4545

4646
// warnings
4747
"Field '.*' shadowed by field at line .*",
@@ -85,7 +85,7 @@ public static void wipeTestData() throws IOException {
8585
}
8686
}
8787

88-
public void test() {
88+
public void test() throws IOException {
8989
List<String> indices = availableIndices();
9090
List<LookupIdx> lookupIndices = lookupIndices();
9191
List<CsvTestsDataLoader.EnrichConfig> policies = availableEnrichPolicies();
@@ -142,14 +142,11 @@ private List<EsqlQueryGenerator.Column> outputSchema(Map<String, Object> a) {
142142
return cols.stream().map(x -> new EsqlQueryGenerator.Column(x.get("name"), x.get("type"))).collect(Collectors.toList());
143143
}
144144

145-
private List<String> availableIndices() {
146-
return new ArrayList<>(
147-
CSV_DATASET_MAP.entrySet()
148-
.stream()
149-
.filter(x -> x.getValue().requiresInferenceEndpoint() == false)
150-
.map(Map.Entry::getKey)
151-
.toList()
152-
);
145+
private List<String> availableIndices() throws IOException {
146+
return availableDatasetsForEs(client(), true, supportsSourceFieldMapping()).stream()
147+
.filter(x -> x.requiresInferenceEndpoint() == false)
148+
.map(x -> x.indexName())
149+
.toList();
153150
}
154151

155152
record LookupIdx(String idxName, String key, String keyType) {}

0 commit comments

Comments
 (0)