Skip to content

Commit d7b952d

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

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from random import random
2+
3+
for N in [1, 10, 100, 1000]:
4+
5+
avgs = []
6+
7+
for i in range(10000):
8+
s = 0
9+
c = 0
10+
11+
for j in range(N):
12+
if random() < 0.5:
13+
s += 1
14+
c += 1
15+
if random() < 0.5:
16+
s += 2+3
17+
c += 2
18+
19+
if c > 0:
20+
avgs.append(s/c)
21+
22+
avg = sum(avgs) / len(avgs)
23+
err = (sum((x - avg) ** 2 for x in avgs) ** 0.5 / len(avgs))
24+
print(N, avg, '+/-', err)

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/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)