Skip to content

Commit 3fcd348

Browse files
authored
ESQL: make order in TOP optional (#135932)
- Make optional the 3rd param to the TOP agg function, and default it to "asc" if omitted - Add a capability - Add a csv test - Randomly drop the order field during unit test cases where the order is ascending - Documentation changes Resolves #133964
1 parent cebc1a8 commit 3fcd348

File tree

8 files changed

+217
-29
lines changed

8 files changed

+217
-29
lines changed

docs/reference/query-languages/esql/_snippets/functions/parameters/top.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/query-languages/esql/_snippets/functions/types/top.md

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/query-languages/esql/images/functions/top.svg

Lines changed: 1 addition & 1 deletion
Loading

docs/reference/query-languages/esql/kibana/definition/functions/top.json

Lines changed: 160 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,15 @@ 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+
305+
topWithOptionalOrder
306+
required_capability: agg_top
307+
required_capability: agg_top_with_optional_order_field
308+
309+
FROM employees | STATS top_salaries = TOP(salary, 3);
310+
311+
top_salaries:integer
312+
[25324, 25945, 25976]
313+
;
314+

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ public enum Cap {
307307
*/
308308
AGG_TOP_STRING_SUPPORT,
309309

310+
/**
311+
* Make optional the order field in the TOP agg command, and default it to "ASC".
312+
*/
313+
AGG_TOP_WITH_OPTIONAL_ORDER_FIELD,
314+
310315
/**
311316
* {@code CASE} properly handling multivalue conditions.
312317
*/

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.elasticsearch.xpack.esql.expression.function.Example;
3232
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
3333
import org.elasticsearch.xpack.esql.expression.function.FunctionType;
34+
import org.elasticsearch.xpack.esql.expression.function.OptionalArgument;
3435
import org.elasticsearch.xpack.esql.expression.function.Param;
3536
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
3637
import org.elasticsearch.xpack.esql.planner.ToAggregator;
@@ -50,7 +51,12 @@
5051
import static org.elasticsearch.xpack.esql.expression.Foldables.TypeResolutionValidator.forPostOptimizationValidation;
5152
import static org.elasticsearch.xpack.esql.expression.Foldables.TypeResolutionValidator.forPreOptimizationValidation;
5253

53-
public class Top extends AggregateFunction implements ToAggregator, SurrogateExpression, PostOptimizationVerificationAware {
54+
public class Top extends AggregateFunction
55+
implements
56+
OptionalArgument,
57+
ToAggregator,
58+
SurrogateExpression,
59+
PostOptimizationVerificationAware {
5460
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "Top", Top::new);
5561

5662
private static final String ORDER_ASC = "ASC";
@@ -71,12 +77,13 @@ public Top(
7177
) Expression field,
7278
@Param(name = "limit", type = { "integer" }, description = "The maximum number of values to collect.") Expression limit,
7379
@Param(
80+
optional = true,
7481
name = "order",
7582
type = { "keyword" },
76-
description = "The order to calculate the top values. Either `asc` or `desc`."
83+
description = "The order to calculate the top values. Either `asc` or `desc`, and defaults to `asc` if omitted."
7784
) Expression order
7885
) {
79-
this(source, field, Literal.TRUE, limit, order);
86+
this(source, field, Literal.TRUE, limit, order == null ? Literal.keyword(source, ORDER_ASC) : order);
8087
}
8188

8289
public Top(Source source, Expression field, Expression filter, Expression limit, Expression order) {

0 commit comments

Comments
 (0)