diff --git a/x-pack/plugin/sql/qa/src/main/resources/agg.sql-spec b/x-pack/plugin/sql/qa/src/main/resources/agg.sql-spec index cdd5c2215b6..9751696ff47 100644 --- a/x-pack/plugin/sql/qa/src/main/resources/agg.sql-spec +++ b/x-pack/plugin/sql/qa/src/main/resources/agg.sql-spec @@ -559,3 +559,9 @@ aggMultiWithHavingUsingInAndNullHandling SELECT MIN(salary) min, MAX(salary) max, gender g, COUNT(*) c FROM "test_emp" WHERE languages > 0 GROUP BY g HAVING max IN(74999, null, 74600) ORDER BY gender; aggMultiGroupByMultiWithHavingUsingInAndNullHandling SELECT MIN(salary) min, MAX(salary) max, gender g, languages l, COUNT(*) c FROM "test_emp" WHERE languages > 0 GROUP BY g, languages HAVING max IN (74500, null, 74600) ORDER BY gender, languages; + +// group by with literal +implicitGroupByWithLiteral +SELECT 10, MAX("salary") FROM test_emp; +groupByWithLiteralAndCount +SELECT 20, COUNT(*) from test_emp GROUP BY gender ORDER BY 2; 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 6da981eba34..26c7ef2b399 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 @@ -371,6 +371,24 @@ public class QueryFolderTests extends ESTestCase { assertThat(ee.output().get(1).toString(), startsWith("a{r}")); } + public void testSelectLiteralWithGroupBy() { + PhysicalPlan p = plan("SELECT 1, MAX(int) FROM test"); + assertEquals(EsQueryExec.class, p.getClass()); + EsQueryExec ee = (EsQueryExec) p; + assertEquals(2, ee.output().size()); + assertEquals(Arrays.asList("1", "MAX(int)"), Expressions.names(ee.output())); + assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), + containsString("\"max\":{\"field\":\"int\"")); + + p = plan("SELECT 1, count(*) FROM test GROUP BY int"); + assertEquals(EsQueryExec.class, p.getClass()); + ee = (EsQueryExec) p; + assertEquals(2, ee.output().size()); + assertEquals(Arrays.asList("1", "count(*)"), Expressions.names(ee.output())); + assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), + containsString("\"terms\":{\"field\":\"int\"")); + } + public void testConcatIsNotFoldedForNull() { PhysicalPlan p = plan("SELECT keyword FROM test WHERE CONCAT(keyword, null) IS NULL"); assertEquals(LocalExec.class, p.getClass());