Skip to content
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

ESQL: Improve agg verification #99827

Merged
Merged
Show file tree
Hide file tree
Changes from 7 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
6 changes: 6 additions & 0 deletions docs/changelog/99827.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 99827
summary: "ESQL: Fix NPE when aggregating literals"
area: ES|QL
type: bug
issues:
- 99751
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,10 @@ MEDIAN(salary):double | MEDIAN_ABSOLUTE_DEVIATION(salary):double
47003 | 10096.5
// end::median-absolute-deviation-result[]
;

medianViaExpression
from employees | stats p50 = percentile(salary_change, 25*2);

p50:double
0.75
;
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,24 @@ else if (p.resolved()) {
agg.aggregates().forEach(e -> {
var exp = e instanceof Alias ? ((Alias) e).child() : e;
if (exp instanceof AggregateFunction aggFunc) {
aggFunc.arguments().forEach(a -> {
// TODO: allow an expression?
if ((a instanceof FieldAttribute
|| a instanceof MetadataAttribute
|| a instanceof ReferenceAttribute
|| a instanceof Literal) == false) {
failures.add(
fail(
e,
"aggregate function's parameters must be an attribute or literal; found ["
+ a.sourceText()
+ "] of type ["
+ a.nodeName()
+ "]"
)
);
}
});
Expression field = aggFunc.field();
alex-spies marked this conversation as resolved.
Show resolved Hide resolved

// TODO: allow an expression?
if ((field instanceof FieldAttribute
|| field instanceof MetadataAttribute
|| field instanceof ReferenceAttribute
|| field instanceof Literal) == false) {
failures.add(
fail(
e,
"aggregate function's field must be an attribute or literal; found ["
+ field.sourceText()
+ "] of type ["
+ field.nodeName()
+ "]"
)
);
}
} else if (agg.groupings().contains(exp) == false) { // TODO: allow an expression?
failures.add(
fail(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ private void aggregatesToFactory(
}
sourceAttr = List.of(attr);
}

alex-spies marked this conversation as resolved.
Show resolved Hide resolved
} else if (mode == AggregateExec.Mode.FINAL) {
aggMode = AggregatorMode.FINAL;
if (grouping) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,25 @@ public void testAggsExpressionsInStatsAggs() {
error("from test | stats length(first_name), count(1) by first_name")
);
assertEquals(
"1:19: aggregate function's parameters must be an attribute or literal; found [emp_no / 2] of type [Div]",
"1:19: aggregate function's field must be an attribute or literal; found [emp_no / 2] of type [Div]",
error("from test | stats x = avg(emp_no / 2) by emp_no")
);
assertEquals(
"1:25: argument of [avg(first_name)] must be [numeric], found value [first_name] type [keyword]",
error("from test | stats count(avg(first_name)) by first_name")
);
assertEquals(
"1:19: aggregate function's parameters must be an attribute or literal; found [length(first_name)] of type [Length]",
"1:19: aggregate function's field must be an attribute or literal; found [length(first_name)] of type [Length]",
error("from test | stats count(length(first_name)) by first_name")
);
assertEquals(
"1:23: expected an aggregate function or group but got [emp_no + avg(emp_no)] of type [Add]",
error("from test | stats x = emp_no + avg(emp_no) by emp_no")
);
assertEquals(
"1:23: second argument of [percentile(languages, languages)] must be a constant, received [languages]",
error("from test | stats x = percentile(languages, languages) by emp_no")
);
}

public void testDoubleRenamingField() {
Expand Down