Skip to content

Commit 2125697

Browse files
Remove folding from Percentile
1 parent 83b05c6 commit 2125697

File tree

2 files changed

+30
-2
lines changed
  • x-pack/plugin
    • esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/aggregate
    • src/yamlRestTest/resources/rest-api-spec/test/esql

2 files changed

+30
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import org.elasticsearch.compute.aggregation.PercentileDoubleAggregatorFunctionSupplier;
1616
import org.elasticsearch.compute.aggregation.PercentileIntAggregatorFunctionSupplier;
1717
import org.elasticsearch.compute.aggregation.PercentileLongAggregatorFunctionSupplier;
18+
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
1819
import org.elasticsearch.xpack.esql.core.expression.Expression;
19-
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
2020
import org.elasticsearch.xpack.esql.core.expression.Literal;
2121
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
2222
import org.elasticsearch.xpack.esql.core.tree.Source;
@@ -34,6 +34,7 @@
3434
import java.util.List;
3535

3636
import static java.util.Collections.singletonList;
37+
import static org.elasticsearch.core.Strings.format;
3738
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST;
3839
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND;
3940
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isFoldable;
@@ -169,7 +170,12 @@ protected AggregatorFunctionSupplier doubleSupplier() {
169170
}
170171

171172
private int percentileValue() {
172-
return ((Number) percentile.fold(FoldContext.small() /* TODO remove me */)).intValue();
173+
if (percentile() instanceof Literal literal) {
174+
return ((Number) literal.value()).intValue();
175+
}
176+
throw new EsqlIllegalArgumentException(
177+
format(null, "Percentile value must be a constant integer in [{}], found [{}]", source(), percentile)
178+
);
173179
}
174180

175181
@Override

x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/esql/230_folding.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,3 +559,25 @@ Query using TERM function on name but with non constant query:
559559
- match: { error.type: "verification_exception" }
560560
- contains: { error.reason: "second argument of [TERM(name, salary)] must be [string], found value [salary] type [integer]" }
561561

562+
---
563+
564+
Percentile with foldable value:
565+
- do:
566+
esql.query:
567+
body:
568+
query: |
569+
ROW x = 100 | STATS p = PERCENTILE(x, 19.8 + 13.4454545) | LIMIT 1
570+
- match: { columns.0.name: "p" }
571+
- match: { values.0.0: 100 }
572+
573+
---
574+
575+
Percentile with non-foldable value:
576+
- do:
577+
catch: bad_request
578+
esql.query:
579+
body:
580+
query: |
581+
FROM employees | STATS p = PERCENTILE(salary, salary + 1) | LIMIT 1
582+
- match: { error.type: "verification_exception" }
583+
- contains: { error.reason: "second argument of [PERCENTILE(salary, salary + 1)] must be a constant, received [salary + 1]" }

0 commit comments

Comments
 (0)