浏览代码

ES|QL: generative tests - fix identifiers generation and source field mapping (#126514)

Luigi Dell'Aquila 6 月之前
父节点
当前提交
41297680c7

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

@@ -160,11 +160,11 @@ public class EsqlQueryGenerator {
             }
             result.append("%{WORD:");
             if (randomBoolean()) {
-                result.append(randomAlphaOfLength(5));
+                result.append(randomIdentifier());
             } else {
                 String fieldName = randomRawName(previousOutput);
                 if (fieldName.isEmpty()) { // it's a bug, managed later, skipping for now
-                    fieldName = randomAlphaOfLength(5);
+                    fieldName = randomIdentifier();
                 }
                 result.append(fieldName);
             }
@@ -188,11 +188,11 @@ public class EsqlQueryGenerator {
             }
             result.append("%{");
             if (randomBoolean()) {
-                result.append(randomAlphaOfLength(5));
+                result.append(randomIdentifier());
             } else {
                 String fieldName = randomRawName(previousOutput);
                 if (fieldName.isEmpty()) { // it's a bug, managed later, skipping for now
-                    fieldName = randomAlphaOfLength(5);
+                    fieldName = randomIdentifier();
                 }
                 result.append(fieldName);
             }
@@ -302,7 +302,7 @@ public class EsqlQueryGenerator {
 
             String newName;
             if (names.isEmpty() || randomBoolean()) {
-                newName = randomAlphaOfLength(5);
+                newName = randomIdentifier();
                 names.add(newName);
             } else {
                 newName = names.get(randomIntBetween(0, names.size() - 1));
@@ -376,7 +376,7 @@ public class EsqlQueryGenerator {
         for (int i = 0; i < nFields; i++) {
             String name;
             if (randomBoolean()) {
-                name = randomAlphaOfLength(randomIntBetween(3, 10));
+                name = randomIdentifier();
             } else {
                 name = randomName(previousOutput);
             }
@@ -402,7 +402,7 @@ public class EsqlQueryGenerator {
         for (int i = 0; i < nStats; i++) {
             String name;
             if (randomBoolean()) {
-                name = randomAlphaOfLength(randomIntBetween(3, 10));
+                name = randomIdentifier();
             } else {
                 name = randomName(previousOutput);
             }
@@ -501,7 +501,7 @@ public class EsqlQueryGenerator {
         StringBuilder cmd = new StringBuilder("row ");
         int nFields = randomIntBetween(1, 10);
         for (int i = 0; i < nFields; i++) {
-            String name = randomAlphaOfLength(randomIntBetween(3, 10));
+            String name = randomIdentifier();
             String expression = constantExpression();
             if (i > 0) {
                 cmd.append(",");
@@ -526,4 +526,10 @@ public class EsqlQueryGenerator {
 
     }
 
+    private static String randomIdentifier() {
+        // Let's create identifiers that are long enough to avoid collisions with reserved keywords.
+        // There could be a smarter way (introspection on the lexer class?), but probably it's not worth the effort
+        return randomAlphaOfLength(randomIntBetween(8, 12));
+    }
+
 }

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

@@ -25,6 +25,7 @@ import java.util.stream.Collectors;
 
 import static org.elasticsearch.xpack.esql.CsvTestsDataLoader.CSV_DATASET_MAP;
 import static org.elasticsearch.xpack.esql.CsvTestsDataLoader.ENRICH_POLICIES;
+import static org.elasticsearch.xpack.esql.CsvTestsDataLoader.availableDatasetsForEs;
 import static org.elasticsearch.xpack.esql.CsvTestsDataLoader.loadDataSetIntoEs;
 
 public abstract class GenerativeRestTest extends ESRestTestCase {
@@ -41,7 +42,6 @@ public abstract class GenerativeRestTest extends ESRestTestCase {
         "Unbounded sort not supported yet",
         "The field names are too complex to process", // field_caps problem
         "must be \\[any type except counter types\\]", // TODO refine the generation of count()
-        "mismatched input .* expecting", // identifier generator needs to be refined, this happens when an identifier is a reserved keyword
 
         // warnings
         "Field '.*' shadowed by field at line .*",
@@ -85,7 +85,7 @@ public abstract class GenerativeRestTest extends ESRestTestCase {
         }
     }
 
-    public void test() {
+    public void test() throws IOException {
         List<String> indices = availableIndices();
         List<LookupIdx> lookupIndices = lookupIndices();
         List<CsvTestsDataLoader.EnrichConfig> policies = availableEnrichPolicies();
@@ -142,14 +142,11 @@ public abstract class GenerativeRestTest extends ESRestTestCase {
         return cols.stream().map(x -> new EsqlQueryGenerator.Column(x.get("name"), x.get("type"))).collect(Collectors.toList());
     }
 
-    private List<String> availableIndices() {
-        return new ArrayList<>(
-            CSV_DATASET_MAP.entrySet()
-                .stream()
-                .filter(x -> x.getValue().requiresInferenceEndpoint() == false)
-                .map(Map.Entry::getKey)
-                .toList()
-        );
+    private List<String> availableIndices() throws IOException {
+        return availableDatasetsForEs(client(), true, supportsSourceFieldMapping()).stream()
+            .filter(x -> x.requiresInferenceEndpoint() == false)
+            .map(x -> x.indexName())
+            .toList();
     }
 
     record LookupIdx(String idxName, String key, String keyType) {}