diff --git a/x-pack/plugin/sql/qa/src/main/resources/agg.csv-spec b/x-pack/plugin/sql/qa/src/main/resources/agg.csv-spec index 7051353d78dda..1cf3acdcfa4a6 100644 --- a/x-pack/plugin/sql/qa/src/main/resources/agg.csv-spec +++ b/x-pack/plugin/sql/qa/src/main/resources/agg.csv-spec @@ -3,7 +3,7 @@ // singlePercentileWithoutComma -SELECT gender, PERCENTILE(emp_no, 97) p1 FROM test_emp GROUP BY gender; +SELECT gender, PERCENTILE(emp_no, 90 + 7) p1 FROM test_emp GROUP BY gender; gender:s | p1:d null |10019.0 @@ -48,7 +48,7 @@ M |10084.349 |10093.502 ; percentileRank -SELECT gender, PERCENTILE_RANK(emp_no, 10025) rank FROM test_emp GROUP BY gender; +SELECT gender, PERCENTILE_RANK(emp_no, 10000 + 25) rank FROM test_emp GROUP BY gender; gender:s | rank:d null |100.0 diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java index ade69463d1345..1865fd4eea126 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java @@ -159,7 +159,7 @@ protected Iterable.Batch> batches() { Batch label = new Batch("Set as Optimized", Limiter.ONCE, new SetAsOptimized()); - return Arrays.asList(aggregate, operators, local, label); + return Arrays.asList(operators, aggregate, local, label); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java index 44f50b53b5aa3..17b1eedf06d93 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java @@ -12,6 +12,7 @@ import org.elasticsearch.xpack.sql.analysis.index.EsIndex; import org.elasticsearch.xpack.sql.analysis.index.IndexResolution; import org.elasticsearch.xpack.sql.expression.function.FunctionRegistry; +import org.elasticsearch.xpack.sql.expression.function.aggregate.AggregateFunctionAttribute; import org.elasticsearch.xpack.sql.optimizer.Optimizer; import org.elasticsearch.xpack.sql.parser.SqlParser; import org.elasticsearch.xpack.sql.plan.physical.EsQueryExec; @@ -316,4 +317,24 @@ public void testConcatIsNotFoldedForNull() { assertEquals(1, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("keyword{f}#")); } + + public void testFoldingOfPercentileSecondArgument() { + PhysicalPlan p = plan("SELECT PERCENTILE(int, 1 + 2) FROM test"); + assertEquals(EsQueryExec.class, p.getClass()); + EsQueryExec ee = (EsQueryExec) p; + assertEquals(1, ee.output().size()); + assertEquals(AggregateFunctionAttribute.class, ee.output().get(0).getClass()); + AggregateFunctionAttribute afa = (AggregateFunctionAttribute) ee.output().get(0); + assertThat(afa.propertyPath(), endsWith("[3.0]")); + } + + public void testFoldingOfPercentileRankSecondArgument() { + PhysicalPlan p = plan("SELECT PERCENTILE_RANK(int, 1 + 2) FROM test"); + assertEquals(EsQueryExec.class, p.getClass()); + EsQueryExec ee = (EsQueryExec) p; + assertEquals(1, ee.output().size()); + assertEquals(AggregateFunctionAttribute.class, ee.output().get(0).getClass()); + AggregateFunctionAttribute afa = (AggregateFunctionAttribute) ee.output().get(0); + assertThat(afa.propertyPath(), endsWith("[3.0]")); + } }