Skip to content

Commit 93b9e19

Browse files
ES|QL: fix validation of SORT by aggregate functions (elastic#117316)
1 parent 157b00c commit 93b9e19

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

docs/changelog/117316.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 117316
2+
summary: Fix validation of SORT by aggregate functions
3+
area: ES|QL
4+
type: bug
5+
issues: []

docs/reference/esql/functions/kibana/definition/match_operator.json

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/esql/functions/types/match_operator.asciidoc

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/analysis/Verifier.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ else if (p instanceof Lookup lookup) {
215215
checkOperationsOnUnsignedLong(p, failures);
216216
checkBinaryComparison(p, failures);
217217
checkForSortableDataTypes(p, failures);
218+
checkSort(p, failures);
218219

219220
checkFullTextQueryFunctions(p, failures);
220221
});
@@ -232,6 +233,18 @@ else if (p instanceof Lookup lookup) {
232233
return failures;
233234
}
234235

236+
private void checkSort(LogicalPlan p, Set<Failure> failures) {
237+
if (p instanceof OrderBy ob) {
238+
ob.order().forEach(o -> {
239+
o.forEachDown(Function.class, f -> {
240+
if (f instanceof AggregateFunction) {
241+
failures.add(fail(f, "Aggregate functions are not allowed in SORT [{}]", f.functionName()));
242+
}
243+
});
244+
});
245+
}
246+
}
247+
235248
private static void checkFilterConditionType(LogicalPlan p, Set<Failure> localFailures) {
236249
if (p instanceof Filter f) {
237250
Expression condition = f.condition();

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/analysis/VerifierTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,13 @@ public void testCategorizeWithinAggregations() {
17911791
);
17921792
}
17931793

1794+
public void testSortByAggregate() {
1795+
assertEquals("1:18: Aggregate functions are not allowed in SORT [COUNT]", error("ROW a = 1 | SORT count(*)"));
1796+
assertEquals("1:28: Aggregate functions are not allowed in SORT [COUNT]", error("ROW a = 1 | SORT to_string(count(*))"));
1797+
assertEquals("1:22: Aggregate functions are not allowed in SORT [MAX]", error("ROW a = 1 | SORT 1 + max(a)"));
1798+
assertEquals("1:18: Aggregate functions are not allowed in SORT [COUNT]", error("FROM test | SORT count(*)"));
1799+
}
1800+
17941801
private void query(String query) {
17951802
defaultAnalyzer.analyze(parser.createStatement(query));
17961803
}

0 commit comments

Comments
 (0)