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 fa76a84 commit 0d00690
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 @@ -366,4 +366,14 @@ public void testInvalidTypeForFunction_WithFourArgs() {
assertEquals("1:8: [INSERT] fourth argument must be [string], found value [3] type [integer]",
error("SELECT INSERT('text', 1, 2, 3)"));
}
}

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 0d00690

Please sign in to comment.