Skip to content

Commit 318948a

Browse files
committed
Handle subqueries
1 parent 272680e commit 318948a

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

x-pack/plugin/esql/qa/server/multi-clusters/src/javaRestTest/java/org/elasticsearch/xpack/esql/ccq/MultiClusterSpecIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ static CsvSpecReader.CsvTestCase convertToRemoteIndices(CsvSpecReader.CsvTestCas
301301
String query = testCase.query;
302302
// If true, we're using *:index, otherwise we're using *:index,index
303303
boolean onlyRemotes = canUseRemoteIndicesOnly() && randomBoolean();
304-
testCase.query = EsqlTestUtils.addRemotes(testCase.query, LOOKUP_INDICES, onlyRemotes);
304+
testCase.query = EsqlTestUtils.addRemoteIndices(testCase.query, LOOKUP_INDICES, onlyRemotes);
305305

306306
int offset = testCase.query.length() - query.length();
307307
if (offset != 0) {

x-pack/plugin/esql/qa/testFixtures/src/main/java/org/elasticsearch/xpack/esql/EsqlTestUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,9 +1284,10 @@ private static PhysicalPlan ignoreIdsInPhysicalPlan(PhysicalPlan plan) {
12841284
});
12851285
}
12861286

1287-
public static String addRemotes(String query, Set<String> lookupIndices, boolean onlyRemotes) {
1287+
public static String addRemoteIndices(String query, Set<String> lookupIndices, boolean onlyRemotes) {
12881288
String[] commands = query.split("\\|");
1289-
String first = commands[0].trim();
1289+
// remove subqueries
1290+
String first = commands[0].split(",\\s+\\(")[0].trim();
12901291

12911292
// Split "SET a=b; FROM x" into "SET a=b" and "FROM x"
12921293
int lastSetDelimiterPosition = first.lastIndexOf(';');

x-pack/plugin/esql/qa/testFixtures/src/test/java/org/elasticsearch/xpack/esql/EsqlTestUtilsTests.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,54 +17,75 @@ public class EsqlTestUtilsTests extends ESTestCase {
1717

1818
public void testPromQL() {
1919
assertThat(
20-
EsqlTestUtils.addRemotes("PROMQL foo, bar step 1m (avg(baz))", Set.of(), false),
20+
EsqlTestUtils.addRemoteIndices("PROMQL foo, bar step 1m (avg(baz))", Set.of(), false),
2121
equalTo("PROMQL *:foo,foo,*:bar,bar step 1m (avg(baz))")
2222
);
2323
assertThat(
24-
EsqlTestUtils.addRemotes("PROMQL \"foo\", \"bar\" step 1m (avg(baz))", Set.of(), false),
24+
EsqlTestUtils.addRemoteIndices("PROMQL \"foo\", \"bar\" step 1m (avg(baz))", Set.of(), false),
2525
equalTo("PROMQL *:foo,foo,*:bar,bar step 1m (avg(baz))")
2626
);
2727
}
2828

2929
public void testPromQLDefaultIndex() {
3030
assertThat(
31-
EsqlTestUtils.addRemotes("PROMQL step 1m (avg(baz))", Set.of(), false),
31+
EsqlTestUtils.addRemoteIndices("PROMQL step 1m (avg(baz))", Set.of(), false),
3232
equalTo("PROMQL *:*,* step 1m (avg(baz))")
3333
);
3434
}
3535

3636
public void testSet() {
3737
assertThat(
38-
EsqlTestUtils.addRemotes("SET a=b; FROM foo | SORT bar", Set.of(), false),
38+
EsqlTestUtils.addRemoteIndices("SET a=b; FROM foo | SORT bar", Set.of(), false),
3939
equalTo("SET a=b; FROM *:foo,foo | SORT bar")
4040
);
4141
}
4242

4343
public void testMetadata() {
4444
assertThat(
45-
EsqlTestUtils.addRemotes("FROM foo METADATA _source | SORT bar", Set.of(), false),
45+
EsqlTestUtils.addRemoteIndices("FROM foo METADATA _source | SORT bar", Set.of(), false),
4646
equalTo("FROM *:foo,foo METADATA _source | SORT bar")
4747
);
4848
}
4949

5050
public void testTS() {
5151
assertThat(
52-
EsqlTestUtils.addRemotes("TS foo, \"bar\",baz | SORT bar", Set.of(), false),
52+
EsqlTestUtils.addRemoteIndices("TS foo, \"bar\",baz | SORT bar", Set.of(), false),
5353
equalTo("TS *:foo,foo,*:bar,bar,*:baz,baz | SORT bar")
5454
);
5555
}
5656

5757
public void testIndexPatternWildcard() {
5858
assertThat(
59-
EsqlTestUtils.addRemotes("TS fo* | SORT bar", Set.of(), false),
59+
EsqlTestUtils.addRemoteIndices("TS fo* | SORT bar", Set.of(), false),
6060
equalTo("TS *:fo*,fo* | SORT bar")
6161
);
6262
}
6363

6464
public void testDuplicateIndex() {
6565
assertThat(
66-
EsqlTestUtils.addRemotes("TS foo,bar,foo | SORT bar", Set.of(), false),
66+
EsqlTestUtils.addRemoteIndices("TS foo,bar,foo | SORT bar", Set.of(), false),
6767
equalTo("TS *:foo,foo,*:bar,bar,*:foo,foo | SORT bar")
6868
);
6969
}
70+
71+
public void testSubquery() {
72+
assertThat(
73+
EsqlTestUtils.addRemoteIndices("""
74+
FROM employees, (FROM employees_incompatible
75+
| ENRICH languages_policy on languages with language_name )
76+
metadata _index
77+
| EVAL emp_no = emp_no::long
78+
| WHERE emp_no >= 10091 AND emp_no < 10094
79+
| SORT _index, emp_no
80+
| KEEP _index, emp_no, languages, language_name""", Set.of(), false),
81+
equalTo("""
82+
FROM *:employees,employees, (FROM employees_incompatible
83+
| ENRICH languages_policy on languages with language_name )
84+
metadata _index
85+
| EVAL emp_no = emp_no::long
86+
| WHERE emp_no >= 10091 AND emp_no < 10094
87+
| SORT _index, emp_no
88+
| KEEP _index, emp_no, languages, language_name""")
89+
);
90+
}
7091
}

0 commit comments

Comments
 (0)