Skip to content

Conversation

@bpintea
Copy link
Contributor

@bpintea bpintea commented Feb 9, 2021

This PR introduces a distinction between the failures caused by WHERE-filtering with aggs
vs HAVING-filtering with missing aggs and improving the error messaging around that.

Closes #57125.

Distinguish between the case where the filtering is a WHERE with aggs
appart from a HAVING with missing aggs.
@elasticmachine elasticmachine added the Team:QL (Deprecated) Meta label for query languages team label Feb 9, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-ql (Team:QL)

Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

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

LGTM. Left a comment.

} else {
localFailures.add(
fail(e, "Cannot use WHERE filtering on aggregate function [{}], use HAVING instead", Expressions.name(e)));

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: extra new line.


assertEquals("1:35: [int] field must appear in the GROUP BY clause or be used in an aggregate function",
error("SELECT int FROM test HAVING int = count(1)"));
// Note: "SELECT int FROM test HAVING int = 1" works, though it normally shouldn't; to correct this out, we'd need to qualify the
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add a test with accept(<query>) and a TODO? to have it more clear?

Copy link
Member

Choose a reason for hiding this comment

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

+1. I'd like to see a few more tests with it working and not working especially when dealing with non-naked expressions such as field inside a scalar function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A few more queries added under a new test.

Copy link
Member

@costin costin left a comment

Choose a reason for hiding this comment

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

LGTM

}

public void testHavingInAggs() {
assertEquals("1:29: [int] field must appear in the GROUP BY clause or be used in an aggregate function",
Copy link
Member

Choose a reason for hiding this comment

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

Good catch for queries with implicit grouping.

fail(e, "Cannot use WHERE filtering on aggregate function [{}], use HAVING instead", Expressions.name(e)));
if (filterChild instanceof Project) {
filter.condition().forEachDown(FieldAttribute.class,
fa -> localFailures.add(
Copy link
Member

Choose a reason for hiding this comment

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

Slight adjustment " or in an aggregate function" (drop be used)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adjusted.


assertEquals("1:35: [int] field must appear in the GROUP BY clause or be used in an aggregate function",
error("SELECT int FROM test HAVING int = count(1)"));
// Note: "SELECT int FROM test HAVING int = 1" works, though it normally shouldn't; to correct this out, we'd need to qualify the
Copy link
Member

Choose a reason for hiding this comment

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

+1. I'd like to see a few more tests with it working and not working especially when dealing with non-naked expressions such as field inside a scalar function.

- error message slight adjustment
- adding one more test.
public void testHavingAsWhere() {
// TODO: this query works, though it normally shouldn't; a check about it could only be enforced if the Filter would be qualified
// (WHERE vs HAVING). Otoh, this "extra flexibility" shouldn't be harmful atp.
accept("SELECT int FROM test HAVING int = 1");
Copy link
Contributor

Choose a reason for hiding this comment

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

thx!

Copy link
Contributor

@astefan astefan left a comment

Choose a reason for hiding this comment

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

LGTM

@bpintea bpintea merged commit ab26062 into elastic:master Feb 10, 2021
@bpintea bpintea deleted the fix/having_with_aggs_message branch February 10, 2021 08:05
bpintea added a commit to bpintea/elasticsearch that referenced this pull request Feb 10, 2021
…8763)

* Enhance error msg on filtering check against aggs

Distinguish between the case where the filtering is a WHERE with aggs
appart from a HAVING with missing aggs.

(cherry picked from commit ab26062)
bpintea added a commit that referenced this pull request Feb 10, 2021
…68803)

* Enhance error msg on filtering check against aggs

Distinguish between the case where the filtering is a WHERE with aggs
appart from a HAVING with missing aggs.

(cherry picked from commit ab26062)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL: use of agg function in HAVING with no grouping emits wrong error message

6 participants