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 bb5747c commit 1dde748
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
package org.elasticsearch.xpack.sql.expression.function.aggregate;

import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.Expressions.ParamOrdinal;
Expand All @@ -17,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 @@ -43,8 +43,8 @@ public Percentile replaceChildren(List<Expression> newChildren) {
@Override
protected TypeResolution resolveType() {
if (!percent.foldable()) {
throw new SqlIllegalArgumentException("2nd argument of PERCENTILE must be constant, received [{}]",
Expressions.name(percent));
return new TypeResolution(format(null, "2nd argument of PERCENTILE must be a constant, received [{}]",
Expressions.name(percent)));
}

TypeResolution resolution = super.resolveType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
package org.elasticsearch.xpack.sql.expression.function.aggregate;

import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.Expressions.ParamOrdinal;
Expand All @@ -17,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 @@ -43,8 +43,8 @@ public Expression replaceChildren(List<Expression> newChildren) {
@Override
protected TypeResolution resolveType() {
if (!value.foldable()) {
throw new SqlIllegalArgumentException("2nd argument of PERCENTILE_RANK must be constant, received [{}]",
Expressions.name(value));
return new TypeResolution(format(null, "2nd argument of PERCENTILE_RANK must be a constant, received [{}]",
Expressions.name(value)));
}

TypeResolution resolution = super.resolveType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.elasticsearch.xpack.sql.analysis.analyzer;

import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.TestUtils;
import org.elasticsearch.xpack.sql.analysis.AnalysisException;
import org.elasticsearch.xpack.sql.analysis.index.EsIndex;
Expand Down Expand Up @@ -539,19 +538,13 @@ public void testAggsInHistogram() {
}

public void testErrorMessageForPercentileWithSecondArgBasedOnAField() {
Analyzer analyzer = new Analyzer(TestUtils.TEST_CFG, new FunctionRegistry(), indexResolution, new Verifier(new Metrics()));
SqlIllegalArgumentException e = expectThrows(SqlIllegalArgumentException.class, () -> analyzer.analyze(parser.createStatement(
"SELECT PERCENTILE(int, ABS(int)) FROM test"), true));
assertEquals("2nd argument of PERCENTILE must be constant, received [ABS(int)]",
e.getMessage());
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() {
Analyzer analyzer = new Analyzer(TestUtils.TEST_CFG, new FunctionRegistry(), indexResolution, new Verifier(new Metrics()));
SqlIllegalArgumentException e = expectThrows(SqlIllegalArgumentException.class, () -> analyzer.analyze(parser.createStatement(
"SELECT PERCENTILE_RANK(int, ABS(int)) FROM test"), true));
assertEquals("2nd argument of PERCENTILE_RANK must be constant, received [ABS(int)]",
e.getMessage());
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 1dde748

Please sign in to comment.