Skip to content

Commit 3bb20e3

Browse files
ES|QL: Fix stats by constant expression (#114899)
1 parent 5dec36e commit 3bb20e3

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

docs/changelog/114899.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 114899
2+
summary: "ES|QL: Fix stats by constant expression"
3+
area: ES|QL
4+
type: bug
5+
issues: []

x-pack/plugin/esql/qa/testFixtures/src/main/resources/stats.csv-spec

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,3 +2473,32 @@ c2:l |c2_f:l |m2:i |m2_f:i |c:l
24732473
1 |0 |2 |2 |19
24742474
1 |1 |5 |5 |21
24752475
;
2476+
2477+
2478+
statsByConstantExpressionNoAggs
2479+
required_capability: fix_stats_by_foldable_expression
2480+
FROM employees | eval x = [1,2,3], y = 5 + 6 | stats by y+1
2481+
;
2482+
2483+
y+1:integer
2484+
12
2485+
;
2486+
2487+
2488+
statsByConstantExpressionNoAggsWithAlias
2489+
required_capability: fix_stats_by_foldable_expression
2490+
FROM employees | eval x = [1,2,3], y = 5 + 6 | stats by yy = y+1
2491+
;
2492+
2493+
yy:integer
2494+
12
2495+
;
2496+
2497+
statsByConstantExpression
2498+
required_capability: fix_stats_by_foldable_expression
2499+
FROM employees | eval x = [1,2,3], y = 5 + 6 | stats m = max(y) by y+1
2500+
;
2501+
2502+
m:integer | y+1:integer
2503+
11 | 12
2504+
;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlCapabilities.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,12 @@ public enum Cap {
385385
/**
386386
* Allow filter per individual aggregation.
387387
*/
388-
PER_AGG_FILTERING;
388+
PER_AGG_FILTERING,
389+
390+
/**
391+
* Fix for https://github.com/elastic/elasticsearch/issues/114714
392+
*/
393+
FIX_STATS_BY_FOLDABLE_EXPRESSION;
389394

390395
private final boolean snapshotOnly;
391396
private final FeatureFlag featureFlag;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/logical/Aggregate.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.common.io.stream.StreamInput;
1212
import org.elasticsearch.common.io.stream.StreamOutput;
1313
import org.elasticsearch.xpack.esql.core.capabilities.Resolvables;
14+
import org.elasticsearch.xpack.esql.core.expression.Alias;
1415
import org.elasticsearch.xpack.esql.core.expression.Attribute;
1516
import org.elasticsearch.xpack.esql.core.expression.AttributeSet;
1617
import org.elasticsearch.xpack.esql.core.expression.Expression;
@@ -157,7 +158,13 @@ protected AttributeSet computeReferences() {
157158
}
158159

159160
public static AttributeSet computeReferences(List<? extends NamedExpression> aggregates, List<? extends Expression> groupings) {
160-
return Expressions.references(groupings).combine(Expressions.references(aggregates));
161+
AttributeSet result = Expressions.references(groupings).combine(Expressions.references(aggregates));
162+
for (Expression grouping : groupings) {
163+
if (grouping instanceof Alias) {
164+
result.remove(((Alias) grouping).toAttribute());
165+
}
166+
}
167+
return result;
161168
}
162169

163170
@Override

0 commit comments

Comments
 (0)