Skip to content

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

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ private static String grok(List<Column> previousOutput) {
}
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);
}
Expand All @@ -188,11 +188,11 @@ private static String dissect(List<Column> previousOutput) {
}
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);
}
Expand Down Expand Up @@ -302,7 +302,7 @@ private static String rename(List<Column> previousOutput) {

String newName;
if (names.isEmpty() || randomBoolean()) {
newName = randomAlphaOfLength(5);
newName = randomIdentifier();
names.add(newName);
} else {
newName = names.get(randomIntBetween(0, names.size() - 1));
Expand Down Expand Up @@ -376,7 +376,7 @@ private static String eval(List<Column> previousOutput) {
for (int i = 0; i < nFields; i++) {
String name;
if (randomBoolean()) {
name = randomAlphaOfLength(randomIntBetween(3, 10));
name = randomIdentifier();
} else {
name = randomName(previousOutput);
}
Expand All @@ -402,7 +402,7 @@ private static String stats(List<Column> previousOutput) {
for (int i = 0; i < nStats; i++) {
String name;
if (randomBoolean()) {
name = randomAlphaOfLength(randomIntBetween(3, 10));
name = randomIdentifier();
} else {
name = randomName(previousOutput);
}
Expand Down Expand Up @@ -501,7 +501,7 @@ private static String row() {
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(",");
Expand All @@ -526,4 +526,10 @@ private static String constantExpression() {

}

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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment that explains that having at least 8 chars prevents us from accidentally using the not keyword; maybe future us will want the 3 char identifiers back, and will have to exclude not specifically.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might borrow/import one from EsTestCase:

public static String randomIdentifier() {
return randomAlphaOfLengthBetween(8, 12).toLowerCase(Locale.ROOT);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say let's keep it as it is, the comment is the only real added value, but maybe it will save us from future headaches

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

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 {
Expand All @@ -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 .*",
Expand Down Expand Up @@ -85,7 +85,7 @@ public static void wipeTestData() throws IOException {
}
}

public void test() {
public void test() throws IOException {
List<String> indices = availableIndices();
List<LookupIdx> lookupIndices = lookupIndices();
List<CsvTestsDataLoader.EnrichConfig> policies = availableEnrichPolicies();
Expand Down Expand Up @@ -142,14 +142,11 @@ private List<EsqlQueryGenerator.Column> outputSchema(Map<String, Object> a) {
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) {}
Expand Down