Skip to content

ESQL: fix COUNT filter pushdown#117503

Merged
bpintea merged 4 commits intoelastic:mainfrom
bpintea:fix/count_with_filter
Nov 27, 2024
Merged

ESQL: fix COUNT filter pushdown#117503
bpintea merged 4 commits intoelastic:mainfrom
bpintea:fix/count_with_filter

Conversation

@bpintea
Copy link
Copy Markdown
Contributor

@bpintea bpintea commented Nov 25, 2024

If COUNT agg has a filter applied, this must also be push down to source. This currently does not happen, but this issue is masked currently by two factors:

  • a logical optimisation, ExtractAggregateCommonFilter that extracts the filter out of the STATS entirely (and pushes it to source then from a WHERE);
  • the phisical plan optimisation implementing the push down, PushStatsToSource, currently only applies if there's just one agg function to push down.

However, this fix needs to be applied since:

  • it's still present in versions prior to ExtractAggregateCommonFilter introduction;
  • the defect might resurface when the restriction in PushStatsToSource is lifted.

Fixes #115522.

@bpintea bpintea added >bug auto-backport Automatically create backport pull requests when merged :Analytics/ES|QL AKA ESQL v9.0.0 v8.16.2 v8.17.1 v8.18.0 labels Nov 25, 2024
@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Nov 25, 2024
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Contributor

@alex-spies alex-spies left a comment

Choose a reason for hiding this comment

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

Thanks @bpintea , LGTM!

I have an optional suggestion for more csv tests; since the code path that's added in this PR is only exercised when ExtractAggregateCommonFilter is not enabled (so, in the 8.16 backport) they are not really relevant on main, but should also not hurt by covering more edge cases.

required_capability: per_agg_filtering
from employees
| stats c1 = count(emp_no) where emp_no < 10042
| stats c1 = count(hire_date) where emp_no < 10042
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hm, maybe it'd be better to add a new test and leave this alone - just so we have more edge case coverage?

Moreover, I think it'd be great to have a test where the field that's counted has missing/null and multi values - because otherwise, the query might as well use count(*) and disregard the field name.

@bpintea
Copy link
Copy Markdown
Contributor Author

bpintea commented Nov 27, 2024

Thanks, Alex!

@bpintea bpintea merged commit 560e0c5 into elastic:main Nov 27, 2024
@bpintea bpintea deleted the fix/count_with_filter branch November 27, 2024 13:59
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

💔 Backport failed

Status Branch Result
8.16 Commit could not be cherrypicked due to conflicts
8.17 Commit could not be cherrypicked due to conflicts
8.x Commit could not be cherrypicked due to conflicts

You can use sqren/backport to manually backport by running backport --upstream elastic/elasticsearch --pr 117503

cbuescher pushed a commit to cbuescher/elasticsearch that referenced this pull request Nov 27, 2024
If `COUNT` agg has a filter applied, this must also be push down to source. This currently does not happen, but this issue is masked currently by two factors:
* a logical optimisation, `ExtractAggregateCommonFilter` that extracts the filter out of the STATS entirely (and pushes it to source then from a `WHERE`);
* the phisical plan optimisation implementing the  push down, `PushStatsToSource`, currently only applies if there's just one agg function to push down.

However, this fix needs to be applied since:
* it's still present in versions prior to `ExtractAggregateCommonFilter` introduction;
* the defect might resurface when the restriction in `PushStatsToSource` is lifted.

Fixes elastic#115522.
bpintea added a commit to bpintea/elasticsearch that referenced this pull request Nov 27, 2024
If `COUNT` agg has a filter applied, this must also be push down to source. This currently does not happen, but this issue is masked currently by two factors:
* a logical optimisation, `ExtractAggregateCommonFilter` that extracts the filter out of the STATS entirely (and pushes it to source then from a `WHERE`);
* the phisical plan optimisation implementing the  push down, `PushStatsToSource`, currently only applies if there's just one agg function to push down.

However, this fix needs to be applied since:
* it's still present in versions prior to `ExtractAggregateCommonFilter` introduction;
* the defect might resurface when the restriction in `PushStatsToSource` is lifted.

Fixes elastic#115522.

(cherry picked from commit 560e0c5)
bpintea added a commit to bpintea/elasticsearch that referenced this pull request Nov 27, 2024
If `COUNT` agg has a filter applied, this must also be push down to source. This currently does not happen, but this issue is masked currently by two factors:
* a logical optimisation, `ExtractAggregateCommonFilter` that extracts the filter out of the STATS entirely (and pushes it to source then from a `WHERE`);
* the phisical plan optimisation implementing the  push down, `PushStatsToSource`, currently only applies if there's just one agg function to push down.

However, this fix needs to be applied since:
* it's still present in versions prior to `ExtractAggregateCommonFilter` introduction;
* the defect might resurface when the restriction in `PushStatsToSource` is lifted.

Fixes elastic#115522.

(cherry picked from commit 560e0c5)
bpintea added a commit to bpintea/elasticsearch that referenced this pull request Nov 27, 2024
If `COUNT` agg has a filter applied, this must also be push down to source. This currently does not happen, but this issue is masked currently by two factors:
* a logical optimisation, `ExtractAggregateCommonFilter` that extracts the filter out of the STATS entirely (and pushes it to source then from a `WHERE`);
* the phisical plan optimisation implementing the  push down, `PushStatsToSource`, currently only applies if there's just one agg function to push down.

However, this fix needs to be applied since:
* it's still present in versions prior to `ExtractAggregateCommonFilter` introduction;
* the defect might resurface when the restriction in `PushStatsToSource` is lifted.

Fixes elastic#115522.

(cherry picked from commit 560e0c5)
elasticsearchmachine pushed a commit that referenced this pull request Nov 28, 2024
* ESQL: fix COUNT filter pushdown (#117503)

If `COUNT` agg has a filter applied, this must also be push down to source. This currently does not happen, but this issue is masked currently by two factors:
* a logical optimisation, `ExtractAggregateCommonFilter` that extracts the filter out of the STATS entirely (and pushes it to source then from a `WHERE`);
* the phisical plan optimisation implementing the  push down, `PushStatsToSource`, currently only applies if there's just one agg function to push down.

However, this fix needs to be applied since:
* it's still present in versions prior to `ExtractAggregateCommonFilter` introduction;
* the defect might resurface when the restriction in `PushStatsToSource` is lifted.

Fixes #115522.

(cherry picked from commit 560e0c5)

* 8.16 adaptation
elasticsearchmachine pushed a commit that referenced this pull request Nov 28, 2024
* ESQL: fix COUNT filter pushdown (#117503)

If `COUNT` agg has a filter applied, this must also be push down to source. This currently does not happen, but this issue is masked currently by two factors:
* a logical optimisation, `ExtractAggregateCommonFilter` that extracts the filter out of the STATS entirely (and pushes it to source then from a `WHERE`);
* the phisical plan optimisation implementing the  push down, `PushStatsToSource`, currently only applies if there's just one agg function to push down.

However, this fix needs to be applied since:
* it's still present in versions prior to `ExtractAggregateCommonFilter` introduction;
* the defect might resurface when the restriction in `PushStatsToSource` is lifted.

Fixes #115522.

(cherry picked from commit 560e0c5)

* 8.17 adaptation
elasticsearchmachine pushed a commit that referenced this pull request Nov 28, 2024
* ESQL: fix COUNT filter pushdown (#117503)

If `COUNT` agg has a filter applied, this must also be push down to source. This currently does not happen, but this issue is masked currently by two factors:
* a logical optimisation, `ExtractAggregateCommonFilter` that extracts the filter out of the STATS entirely (and pushes it to source then from a `WHERE`);
* the phisical plan optimisation implementing the  push down, `PushStatsToSource`, currently only applies if there's just one agg function to push down.

However, this fix needs to be applied since:
* it's still present in versions prior to `ExtractAggregateCommonFilter` introduction;
* the defect might resurface when the restriction in `PushStatsToSource` is lifted.

Fixes #115522.

(cherry picked from commit 560e0c5)

* revert merge artefact

* 8.x adaptation
alexey-ivanov-es pushed a commit to alexey-ivanov-es/elasticsearch that referenced this pull request Nov 28, 2024
If `COUNT` agg has a filter applied, this must also be push down to source. This currently does not happen, but this issue is masked currently by two factors:
* a logical optimisation, `ExtractAggregateCommonFilter` that extracts the filter out of the STATS entirely (and pushes it to source then from a `WHERE`);
* the phisical plan optimisation implementing the  push down, `PushStatsToSource`, currently only applies if there's just one agg function to push down.

However, this fix needs to be applied since:
* it's still present in versions prior to `ExtractAggregateCommonFilter` introduction;
* the defect might resurface when the restriction in `PushStatsToSource` is lifted.

Fixes elastic#115522.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/ES|QL AKA ESQL auto-backport Automatically create backport pull requests when merged >bug Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v8.16.2 v8.17.1 v8.18.0 v9.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ESQL: unfolded counting fails with filtered agg

3 participants