Skip to content

Commit

Permalink
SQL: Improve handling of invalid args for PERCENTILE/PERCENTILE_RANK (#…
Browse files Browse the repository at this point in the history
…37803)

Improve the Exception and the error message returned when 2nd argument
of PERCENTILE and PERCENTILE_RANK is not a constant.
  • Loading branch information
matriv committed Jan 24, 2019
1 parent a9861f4 commit 529f3ec
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;

import static java.util.Collections.singletonList;
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;

public class Percentile extends NumericAggregate implements EnclosedAgg {

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

@Override
protected TypeResolution resolveType() {
if (!percent.foldable()) {
return new TypeResolution(format(null, "2nd argument of PERCENTILE must be a constant, received [{}]",
Expressions.name(percent)));
}

TypeResolution resolution = super.resolveType();

if (TypeResolution.TYPE_RESOLVED.equals(resolution)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;

import static java.util.Collections.singletonList;
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;

public class PercentileRank extends AggregateFunction implements EnclosedAgg {

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

@Override
protected TypeResolution resolveType() {
if (!value.foldable()) {
return new TypeResolution(format(null, "2nd argument of PERCENTILE_RANK must be a constant, received [{}]",
Expressions.name(value)));
}

TypeResolution resolution = super.resolveType();
if (resolution.unresolved()) {
return resolution;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,4 +529,14 @@ public void testAggsInHistogram() {
assertEquals("1:47: Cannot use an aggregate [MAX] for grouping",
error("SELECT MAX(date) FROM test GROUP BY HISTOGRAM(MAX(int), 1)"));
}
}

public void testErrorMessageForPercentileWithSecondArgBasedOnAField() {
assertEquals("1:8: 2nd argument of PERCENTILE must be a constant, received [ABS(int)]",
error("SELECT PERCENTILE(int, ABS(int)) FROM test"));
}

public void testErrorMessageForPercentileRankWithSecondArgBasedOnAField() {
assertEquals("1:8: 2nd argument of PERCENTILE_RANK must be a constant, received [ABS(int)]",
error("SELECT PERCENTILE_RANK(int, ABS(int)) FROM test"));
}
}

0 comments on commit 529f3ec

Please sign in to comment.