Skip to content

Commit 0d00690

Browse files
committed
SQL: Improve handling of invalid args for PERCENTILE/PERCENTILE_RANK (#37803)
Improve the Exception and the error message returned when 2nd argument of PERCENTILE and PERCENTILE_RANK is not a constant.
1 parent fa76a84 commit 0d00690

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.List;
1717

1818
import static java.util.Collections.singletonList;
19+
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
1920

2021
public class Percentile extends NumericAggregate implements EnclosedAgg {
2122

@@ -41,6 +42,11 @@ public Percentile replaceChildren(List<Expression> newChildren) {
4142

4243
@Override
4344
protected TypeResolution resolveType() {
45+
if (!percent.foldable()) {
46+
return new TypeResolution(format(null, "2nd argument of PERCENTILE must be a constant, received [{}]",
47+
Expressions.name(percent)));
48+
}
49+
4450
TypeResolution resolution = super.resolveType();
4551

4652
if (TypeResolution.TYPE_RESOLVED.equals(resolution)) {

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/aggregate/PercentileRank.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.List;
1717

1818
import static java.util.Collections.singletonList;
19+
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
1920

2021
public class PercentileRank extends AggregateFunction implements EnclosedAgg {
2122

@@ -41,6 +42,11 @@ public Expression replaceChildren(List<Expression> newChildren) {
4142

4243
@Override
4344
protected TypeResolution resolveType() {
45+
if (!value.foldable()) {
46+
return new TypeResolution(format(null, "2nd argument of PERCENTILE_RANK must be a constant, received [{}]",
47+
Expressions.name(value)));
48+
}
49+
4450
TypeResolution resolution = super.resolveType();
4551
if (resolution.unresolved()) {
4652
return resolution;

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/VerifierErrorMessagesTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,14 @@ public void testInvalidTypeForFunction_WithFourArgs() {
366366
assertEquals("1:8: [INSERT] fourth argument must be [string], found value [3] type [integer]",
367367
error("SELECT INSERT('text', 1, 2, 3)"));
368368
}
369-
}
369+
370+
public void testErrorMessageForPercentileWithSecondArgBasedOnAField() {
371+
assertEquals("1:8: 2nd argument of PERCENTILE must be a constant, received [ABS(int)]",
372+
error("SELECT PERCENTILE(int, ABS(int)) FROM test"));
373+
}
374+
375+
public void testErrorMessageForPercentileRankWithSecondArgBasedOnAField() {
376+
assertEquals("1:8: 2nd argument of PERCENTILE_RANK must be a constant, received [ABS(int)]",
377+
error("SELECT PERCENTILE_RANK(int, ABS(int)) FROM test"));
378+
}
379+
}

0 commit comments

Comments
 (0)