From b97f1ca12675d6ea4772c60578922fe1cc2409ee Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Wed, 5 Feb 2020 12:32:40 +0100 Subject: [PATCH] SQL: [Tests] add tests for literals and GROUP BY (#51878) Add unit and integration tests where literals are SELECTed in combination with GROUP BY and possibly aggregate functions. Relates to #41411 and #34583 which have been fixed. --- .../sql/qa/src/main/resources/agg.sql-spec | 6 ++++++ .../xpack/sql/planner/QueryFolderTests.java | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) 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 cdd5c2215b6e5..9751696ff47ce 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 6da981eba349b..26c7ef2b3990b 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 void testGroupKeyTypes_DateTime() { 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());