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: Remove aliasing inside Eval #100238

Merged
merged 6 commits into from
Oct 5, 2023

Conversation

costin
Copy link
Member

@costin costin commented Oct 4, 2023

Evals that introduce aliased can be simplified by extracting them into a
project (and thus signaling there's no underlying processing).

The following eval:
eval x = a + 1, y = x, z = y + 1, y = z, w = y + 1
can be converted into:
eval x = a + 1, z = a + 1 + 1, w = a + 1 + 1 + 1 | project x, z, z as y, w

Fix #100174

@costin costin self-assigned this Oct 4, 2023
@elasticsearchmachine elasticsearchmachine added the Team:QL (Deprecated) Meta label for query languages team label Oct 4, 2023
@elasticsearchmachine
Copy link
Collaborator

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

@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/elasticsearch-esql (:Query Languages/ES|QL)

@costin costin force-pushed the fix/eval-as-alias-before-agg branch from defb7bd to 5ec7954 Compare October 4, 2023 02:56
* \_FieldExtractExec[salary{f}#20]
* \_EsQueryExec[test], query[{"esql_single_value":{"field":"emp_no","next":{"range":{"emp_no":{"lt":10050,"boost":1.0}}}}}]
* [_doc{f}#42], limit[], sort[] estimatedRowSize[16]
* \_EsStatsQueryExec[test], stats[Stat[name=salary, type=COUNT, query={
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a great example on how this PR helps - since the eval goes away, there's no field extraction needed.

@costin
Copy link
Member Author

costin commented Oct 4, 2023

Depending on the test runs, I'd like to get this PR in 8.11 since several queries with aggs end up simplified (see the extra tests).
Currently there's only one Csv test that fails due to the one channel per layout check which is triggered by projection vs the eval since the block is not copied but referred as is.

Limiter.ONCE,
new SubstituteSurrogates(),
new ReplaceRegexMatch(),
new ReplaceAliasingEvalWithProject()
Copy link
Member Author

Choose a reason for hiding this comment

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

The nice thing about the current state of the rule is that it only gets applied once.

Comment on lines -1521 to -1528
assertThat(aggregate.aggregates(), hasSize(1));
var alias = as(aggregate.aggregates().get(0), Alias.class);
var count = as(alias.child(), Count.class);
var eval = as(aggregate.child(), Eval.class);
assertThat(eval.fields(), hasSize(1));
var field = as(eval.fields().get(0), Alias.class);
assertThat(field.name(), is("x"));
var source = as(eval.child(), EsRelation.class);
Copy link
Member Author

Choose a reason for hiding this comment

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

Note how the intermediate evals were removed.

Comment on lines -1556 to -1558
var project = as(plan, Project.class);
var eval = as(project.child(), Eval.class);
var limit = as(eval.child(), Limit.class);
Copy link
Member Author

Choose a reason for hiding this comment

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

Same here

Evals that introduce aliased can be simplified by extracting them into a
 project (and thus signaling there's no underlying processing).

The following eval:
  eval x = a + 1, y = x, z = y + 1, y = z, w = y + 1
can be converted into:
  eval x = a + 1, z = a + 1 + 1, w = a + 1 + 1 | project x, z, z as y, w

Fix elastic#100174
@costin costin force-pushed the fix/eval-as-alias-before-agg branch from 5ec7954 to c7ccdfc Compare October 4, 2023 03:03
@astefan
Copy link
Contributor

astefan commented Oct 4, 2023

Your description is slightly wrong:

The following eval:
eval x = a + 1, y = x, z = y + 1, y = z, w = y + 1
can be converted into:
eval x = a + 1, z = a + 1 + 1, w = a + 1 + 1 | project x, z, z as y, w

it should be (notice w = a + 1 + 1 + 1 | project ...)

The following eval:
eval x = a + 1, y = x, z = y + 1, y = z, w = y + 1
can be converted into:
eval x = a + 1, z = a + 1 + 1, w = a + 1 + 1 + 1 | project x, z, z as y, w

@mattc58 mattc58 added v8.12.0 and removed v8.11.0 labels Oct 4, 2023
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, especially the simplifications (and performance improvements) it's adding to queries where evaluation of expressions is not performed when not actually needed.

I have concerns regarding the Layout check that has been commented, combined with the bug I referenced. I am not sure anymore why the check has been added in the first place. I've seen this check being commented in the past as well. Is there an issue waiting to be fixed/implemented for this check to stay as is?

@costin costin force-pushed the fix/eval-as-alias-before-agg branch from 247fb29 to c4457d9 Compare October 4, 2023 20:08
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

Copy link
Contributor

@bpintea bpintea left a comment

Choose a reason for hiding this comment

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

Nice.

if (prev != null) {
throw new IllegalArgumentException("Name [" + id + "] is on two channels [" + prev + "] and [" + next + "]");
}
// Do allow multiple name to point to the same channel - see https://github.com/elastic/elasticsearch/pull/100238
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Do allow multiple name to point to the same channel - see https://github.com/elastic/elasticsearch/pull/100238
// Do allow multiple name to point to the same channel - see https://github.com/elastic/elasticsearch/pull/100299

@elasticsearchmachine
Copy link
Collaborator

Hi @costin, I've created a changelog YAML for you.

@costin costin merged commit ad0a266 into elastic:main Oct 5, 2023
@costin costin deleted the fix/eval-as-alias-before-agg branch October 5, 2023 16:48
@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
8.11

costin added a commit to costin/elasticsearch that referenced this pull request Oct 5, 2023
Evals that introduce aliased can be simplified by extracting them into a
 project (and thus signaling there's no underlying processing).

The following eval:
  eval x = a + 1, y = x, z = y + 1, y = z, w = y + 1
can be converted into:
  eval x = a + 1, z = a + 1 + 1, w = a + 1 + 1 | project x, z, z as y, w

Fix elastic#100174
Fix elastic#100050
@nik9000
Copy link
Member

nik9000 commented Oct 5, 2023

I think this started failing #100356

elasticsearchmachine pushed a commit that referenced this pull request Oct 5, 2023
* ESQL: Remove aliasing inside Eval (#100238)

Evals that introduce aliased can be simplified by extracting them into a
 project (and thus signaling there's no underlying processing).

The following eval:
  eval x = a + 1, y = x, z = y + 1, y = z, w = y + 1
can be converted into:
  eval x = a + 1, z = a + 1 + 1, w = a + 1 + 1 | project x, z, z as y, w

Fix #100174
Fix #100050

* Incorporate #100357

* Temporarily disable failing test

Relates #100365

(cherry picked from commit 0ef4da2)

* ESQL: Temporarily disable huge concat tests (#100352)

We're working on these and we have a plan!

(cherry picked from commit acca114)

---------

Co-authored-by: Nik Everett <[email protected]>
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.

ESQL: Remove aliasing inside Eval
6 participants