Skip to content

ESQL: Fix multi-value constant propagation after STATS#139442

Merged
ncordon merged 10 commits intoelastic:mainfrom
kanoshiou:fix-mv-constant-propagation-after-stats
Dec 29, 2025
Merged

ESQL: Fix multi-value constant propagation after STATS#139442
ncordon merged 10 commits intoelastic:mainfrom
kanoshiou:fix-mv-constant-propagation-after-stats

Conversation

@kanoshiou
Copy link
Contributor

This PR fixes an issue where multi-value constants used as GROUP BY keys are incorrectly propagated after the aggregation.

Problem

When a multi-value constant (e.g., [1, 2]) is used as a GROUP BY key, the STATS command explodes it into single values. However, the PropagateEvalFoldables optimization rule was propagating the original multi-value literal into expressions after the Aggregate node, which incorrectly treated the field as still being multi-valued.

For example, in the query:

ROW a = [1, 2] | STATS c = SUM(a) BY a | WHERE a > 1

Before the fix, a in the WHERE clause would be incorrectly replaced with the literal [1, 2] instead of the single-value result from GROUP BY.

Solution

Exclude multi-value (List) literals used as GROUP BY keys from constant propagation. This ensures that after aggregation, references to grouping keys correctly reflect their single-value nature.

Closes #135926

@elasticsearchmachine elasticsearchmachine added needs:triage Requires assignment of a team area label external-contributor Pull request authored by a developer outside the Elasticsearch team v9.3.0 labels Dec 12, 2025
kanoshiou and others added 4 commits December 12, 2025 22:22
…-stats

# Conflicts:
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlCapabilities.java
#	x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanOptimizerTests.java
# Conflicts:
#	x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanOptimizerTests.java
@alex-spies alex-spies added :Analytics/ES|QL AKA ESQL and removed needs:triage Requires assignment of a team area label labels Dec 16, 2025
@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Dec 16, 2025
@elasticsearchmachine
Copy link
Collaborator

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

@ncordon ncordon self-assigned this Dec 19, 2025
@ncordon ncordon added the >bug label Dec 19, 2025
@ncordon
Copy link
Contributor

ncordon commented Dec 19, 2025

buildkite test this please

@ncordon ncordon self-requested a review December 19, 2025 16:17
Copy link
Contributor

@ncordon ncordon left a comment

Choose a reason for hiding this comment

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

This looks good to me provided it passes the CI. Thanks a lot for the contribution @kanoshiou!

@ncordon
Copy link
Contributor

ncordon commented Dec 23, 2025

buildkite test this please

@ncordon
Copy link
Contributor

ncordon commented Dec 23, 2025

buildkite test this please

@kanoshiou
Copy link
Contributor Author

Thank you for your review, @ncordon! I believe these two CI failures are not related to this PR.

I’ve also added the two tests(25a49ab) requested in another PR(#139175 (comment)). Hey @bpintea — I’m not sure if these tests fully match what you had in mind; if not, I’m happy to adjust them.

@ncordon
Copy link
Contributor

ncordon commented Dec 24, 2025

buildkite test this please

1 similar comment
@ncordon
Copy link
Contributor

ncordon commented Dec 27, 2025

buildkite test this please

@ncordon
Copy link
Contributor

ncordon commented Dec 29, 2025

buildkite test this please

@ncordon ncordon merged commit ea3cae3 into elastic:main Dec 29, 2025
37 checks passed
@kanoshiou
Copy link
Contributor Author

Thanks for your review and the effort you put into this, @ncordon @mouhc1ne!

@ncordon
Copy link
Contributor

ncordon commented Dec 29, 2025

Thanks for your amazing contributions @kanoshiou! I'll backport this to relevant branches.

rjernst pushed a commit to rjernst/elasticsearch that referenced this pull request Dec 29, 2025
Kubik42 pushed a commit to Kubik42/elasticsearch that referenced this pull request Dec 30, 2025
ncordon pushed a commit to ncordon/elasticsearch that referenced this pull request Dec 30, 2025
ncordon added a commit to ncordon/elasticsearch that referenced this pull request Dec 30, 2025
ncordon pushed a commit to ncordon/elasticsearch that referenced this pull request Dec 30, 2025
ncordon pushed a commit to ncordon/elasticsearch that referenced this pull request Dec 30, 2025
ncordon pushed a commit to ncordon/elasticsearch that referenced this pull request Dec 30, 2025
ncordon pushed a commit to ncordon/elasticsearch that referenced this pull request Dec 30, 2025
ncordon added a commit to ncordon/elasticsearch that referenced this pull request Dec 30, 2025
ncordon pushed a commit to ncordon/elasticsearch that referenced this pull request Dec 30, 2025
ncordon added a commit to ncordon/elasticsearch that referenced this pull request Dec 30, 2025
ncordon pushed a commit to ncordon/elasticsearch that referenced this pull request Jan 2, 2026
ncordon added a commit to ncordon/elasticsearch that referenced this pull request Jan 2, 2026
elasticsearchmachine pushed a commit that referenced this pull request Jan 2, 2026
elasticsearchmachine pushed a commit that referenced this pull request Jan 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/ES|QL AKA ESQL >bug external-contributor Pull request authored by a developer outside the Elasticsearch team Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v9.4.0

5 participants