Skip to content

Commit

Permalink
ES|QL: fix validation of SORT by aggregate functions (elastic#117316) (
Browse files Browse the repository at this point in the history
  • Loading branch information
luigidellaquila authored Nov 22, 2024
1 parent 029287a commit acdd6ec
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/117316.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 117316
summary: Fix validation of SORT by aggregate functions
area: ES|QL
type: bug
issues: []

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions docs/reference/esql/functions/types/match_operator.asciidoc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ else if (p instanceof Lookup lookup) {
checkOperationsOnUnsignedLong(p, failures);
checkBinaryComparison(p, failures);
checkForSortableDataTypes(p, failures);
checkSort(p, failures);

checkFullTextQueryFunctions(p, failures);
});
Expand All @@ -232,6 +233,18 @@ else if (p instanceof Lookup lookup) {
return failures;
}

private void checkSort(LogicalPlan p, Set<Failure> failures) {
if (p instanceof OrderBy ob) {
ob.order().forEach(o -> {
o.forEachDown(Function.class, f -> {
if (f instanceof AggregateFunction) {
failures.add(fail(f, "Aggregate functions are not allowed in SORT [{}]", f.functionName()));
}
});
});
}
}

private static void checkFilterConditionType(LogicalPlan p, Set<Failure> localFailures) {
if (p instanceof Filter f) {
Expression condition = f.condition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1796,6 +1796,13 @@ public void testCategorizeWithinAggregations() {
);
}

public void testSortByAggregate() {
assertEquals("1:18: Aggregate functions are not allowed in SORT [COUNT]", error("ROW a = 1 | SORT count(*)"));
assertEquals("1:28: Aggregate functions are not allowed in SORT [COUNT]", error("ROW a = 1 | SORT to_string(count(*))"));
assertEquals("1:22: Aggregate functions are not allowed in SORT [MAX]", error("ROW a = 1 | SORT 1 + max(a)"));
assertEquals("1:18: Aggregate functions are not allowed in SORT [COUNT]", error("FROM test | SORT count(*)"));
}

private void query(String query) {
defaultAnalyzer.analyze(parser.createStatement(query));
}
Expand Down

0 comments on commit acdd6ec

Please sign in to comment.