Skip to content

ESQL: Preserve single aggregate when all attributes are pruned #126397

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

Merged
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
6 changes: 6 additions & 0 deletions docs/changelog/126397.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 126397
summary: "ESQL: Preserve single aggregate when all attributes are pruned"
area: ES|QL
type: bug
issues:
- 126392
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,39 @@ foo:keyword
;


evalAfterAvgUsingSameName
required_capability: remove_empty_attribute_in_merging_output
from employees
| stats avg = avg(salary)
| eval avg = 12
;

avg:integer
12
;

evalAfterStatsUsingSameName
required_capability: remove_empty_attribute_in_merging_output
from employees
| stats count = count(emp_no), median = median(salary), top_salaries = TOP(salary, 3, "desc")
| keep median, top_salaries
| rename top_salaries as median
| eval median = 12
;

median:integer
12
;

evalAfterStatsUsingSameName2
required_capability: remove_empty_attribute_in_merging_output
ROW foo = [10, 11, 12]
| mv_expand foo
| stats sum = sum(foo), max = max(foo)
| rename sum as max
| eval max = 13
;

max:integer
13
;
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ public enum Cap {
*/
RENAME_SEQUENTIAL_PROCESSING,

/**
* Support for removing empty attribute in merging output.
* See <a href="https://github.com/elastic/elasticsearch/issues/126392"> ESQL: EVAL after STATS produces an empty column #126392 </a>
*/
REMOVE_EMPTY_ATTRIBUTE_IN_MERGING_OUTPUT,

/**
* Fix for union-types when some indexes are missing the required field. Done in #111932.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.elasticsearch.compute.data.BlockUtils;
import org.elasticsearch.index.IndexMode;
import org.elasticsearch.xpack.esql.core.expression.AttributeSet;
import org.elasticsearch.xpack.esql.core.expression.EmptyAttribute;
import org.elasticsearch.xpack.esql.core.expression.Expressions;
import org.elasticsearch.xpack.esql.core.expression.NamedExpression;
import org.elasticsearch.xpack.esql.core.util.Holder;
Expand Down Expand Up @@ -77,7 +76,7 @@ public LogicalPlan apply(LogicalPlan plan) {
if (aggregate.groupings().isEmpty()) {
p = new LocalRelation(
aggregate.source(),
List.of(new EmptyAttribute(aggregate.source())),
List.of(Expressions.attribute(aggregate.aggregates().getFirst())),
Copy link
Member

Choose a reason for hiding this comment

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

I was thinking about something like List.of(aggregate.output().get(0)), your way does a similar job too.

LocalSupplier.of(
new Block[] { BlockUtils.constantBlock(PlannerUtils.NON_BREAKING_BLOCK_FACTORY, null, 1) }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2784,6 +2784,24 @@ private static List<String> orderNames(TopN topN) {
return topN.order().stream().map(o -> as(o.child(), NamedExpression.class).name()).toList();
}

/**
* Expects
* Eval[[2[INTEGER] AS x]]
* \_Limit[1000[INTEGER],false]
* \_LocalRelation[[{e}#9],[ConstantNullBlock[positions=1]]]
*/
public void testEvalAfterStats() {
var plan = optimizedPlan("""
ROW foo = 1
| STATS x = max(foo)
| EVAL x = 2
""");
var eval = as(plan, Eval.class);
var limit = as(eval.child(), Limit.class);
var localRelation = as(limit.child(), LocalRelation.class);
assertThat(Expressions.names(eval.output()), contains("x"));
}

public void testCombineLimitWithOrderByThroughFilterAndEval() {
LogicalPlan plan = optimizedPlan("""
from test
Expand Down
Loading