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: Fix layout when aggregating with aliases #117832

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ h:d | languages:i
1.41 | null
;

groupByAlias#[skip:-8.13.99,reason:muted, see https://github.com/elastic/elasticsearch/issues/117770]
groupByAlias
from employees | rename languages as l | keep l, height | stats m = min(height) by l | sort l;

m:d | l:i
Expand Down Expand Up @@ -951,7 +951,7 @@ c:l
49
;

countFieldWithGrouping#[skip:-8.13.99,reason:muted, see https://github.com/elastic/elasticsearch/issues/117784]
countFieldWithGrouping
from employees | rename languages as l | where emp_no < 10050 | stats c = count(emp_no) by l | sort l;

c:l | l:i
Expand All @@ -963,7 +963,7 @@ c:l | l:i
10 | null
;

countFieldWithAliasWithGrouping#[skip:-8.13.99,reason:muted, see https://github.com/elastic/elasticsearch/issues/117784]
countFieldWithAliasWithGrouping
from employees | rename languages as l | eval e = emp_no | where emp_no < 10050 | stats c = count(e) by l | sort l;

c:l | l:i
Expand All @@ -982,7 +982,7 @@ c:l
49
;

countEvalExpWithGrouping#[skip:-8.13.99,reason:muted, see https://github.com/elastic/elasticsearch/issues/117784]
countEvalExpWithGrouping
from employees | rename languages as l | eval e = case(emp_no < 10050, emp_no, null) | stats c = count(e) by l | sort l;

c:l | l:i
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ id:i |name:s |version:v |o:v
13 |lllll |null |null
;

countVersion#[skip:-8.13.99,reason:muted, see https://github.com/elastic/elasticsearch/issues/117784]
countVersion
FROM apps | RENAME name AS k | STATS v = COUNT(version) BY k | SORT k;

v:l | k:s
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be merged to main and then backported? To avoid conflicts, and ensure we support "everything everywhere".

I see you commented to fix it here, and then remove the obsolete path in main. But I wonder if it could cause issues in the future. Also, if it fails in <8.13, shouldn't 9.x also support it? Or is 8.13 the last compatible 8.x version?

Copy link
Contributor

Choose a reason for hiding this comment

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

Nevermind, I see the other PR is identical

Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,14 @@ public final PhysicalOperation groupingPhysicalOperation(
* - before stats (keep x = a | stats by x) which requires the partial input to use a's channel
* - after stats (stats by a | keep x = a) which causes the output layout to refer to the follow-up alias
*/
// TODO: This is likely required only for pre-8.14 node compatibility; confirm and remove if possible.
// Since https://github.com/elastic/elasticsearch/pull/104958, it shouldn't be possible to have aliases in the aggregates
// which the groupings refer to. Except for `BY CATEGORIZE(field)`, which remains as alias in the grouping, all aliases
// should've become EVALs before or after the STATS.
for (NamedExpression agg : aggregates) {
if (agg instanceof Alias a) {
if (a.child() instanceof Attribute attr) {
if (groupAttribute.id().equals(attr.id())) {
if (sourceGroupAttribute.id().equals(attr.id())) {
Copy link
Contributor

@ivancea ivancea Dec 2, 2024

Choose a reason for hiding this comment

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

sourceGroupAttribute == groupAttribute for anything not using Categorize. So, how was this failing for non-Categorize cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This line I just updated for alignment with the rest. It's the change below that actually fixes things.

groupAttributeLayout.nameIds().add(a.id());
// TODO: investigate whether a break could be used since it shouldn't be possible to have multiple
// attributes pointing to the same attribute
Expand All @@ -133,8 +137,8 @@ public final PhysicalOperation groupingPhysicalOperation(
// is in the output form
// if the group points to an alias declared in the aggregate, use the alias child as source
else if (aggregatorMode.isOutputPartial()) {
if (groupAttribute.semanticEquals(a.toAttribute())) {
groupAttribute = attr;
if (sourceGroupAttribute.semanticEquals(a.toAttribute())) {
sourceGroupAttribute = attr;
break;
Comment on lines -136 to 142
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The groupAttribute was mutated, but we use the sourceGroupAttribute to determine the right channel to pass to the groupSpecs. We need to mutate the sourceGroupAttribute instead.

}
}
Expand Down