Skip to content

Commit 0123d95

Browse files
committed
Propagate filter in TOP(field, 1, order) surrogate.
1 parent 7c957c3 commit 0123d95

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,17 @@ FROM books
300300
The Lord of the Rings Poster Collection: Six Paintings by Alan Lee (No. 1) | he Lo | [J. R. R. Tolkien, Alan Lee] | Alan Lee
301301
A Gentle Creature and Other Stories: White Nights, A Gentle Creature, and The Dream of a Ridiculous Man (The World's Classics) | Gent | [W. J. Leatherbarrow, Fyodor Dostoevsky, Alan Myers] | Alan Myers
302302
;
303+
304+
topWithConditions
305+
required_capability: stats_top_1_with_condition_fixed
306+
307+
FROM employees
308+
| STATS min1 = TOP(emp_no, 1, "ASC") WHERE emp_no > 10010,
309+
min2 = TOP(emp_no, 2, "ASC") WHERE emp_no > 10010,
310+
max1 = TOP(emp_no, 1, "DESC") WHERE emp_no < 10080,
311+
max2 = TOP(emp_no, 2, "DESC") WHERE emp_no < 10080
312+
;
313+
314+
min1:integer | min2:integer | max1:integer | max2:integer
315+
10011 | [10011, 10012] | 10079 | [10079, 10078]
316+
;

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
@@ -1457,7 +1457,12 @@ public enum Cap {
14571457
/**
14581458
* Support for the Present function
14591459
*/
1460-
FN_PRESENT;
1460+
FN_PRESENT,
1461+
1462+
/**
1463+
* Bugfix for STATS TOP(field, 1, order) WHERE condition.
1464+
*/
1465+
STATS_TOP_1_WITH_CONDITION_FIXED;
14611466

14621467
private final boolean enabled;
14631468

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/aggregate/Top.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ public Expression surrogate() {
289289
var s = source();
290290
if (orderField() instanceof Literal && limitField() instanceof Literal && limitValue() == 1) {
291291
if (orderValue()) {
292-
return new Min(s, field());
292+
return new Min(s, field(), filter());
293293
} else {
294-
return new Max(s, field());
294+
return new Max(s, field(), filter());
295295
}
296296
}
297297
return null;

0 commit comments

Comments
 (0)