55 */
66package org .elasticsearch .xpack .sql .analysis .analyzer ;
77
8+ import java .util .stream .Collectors ;
89import org .elasticsearch .test .ESTestCase ;
910import org .elasticsearch .xpack .ql .QlIllegalArgumentException ;
11+ import org .elasticsearch .xpack .ql .expression .Alias ;
1012import org .elasticsearch .xpack .ql .expression .Attribute ;
13+ import org .elasticsearch .xpack .ql .expression .Expression ;
1114import org .elasticsearch .xpack .ql .expression .Expressions ;
1215import org .elasticsearch .xpack .ql .expression .FieldAttribute ;
1316import org .elasticsearch .xpack .ql .expression .NamedExpression ;
1417import org .elasticsearch .xpack .ql .expression .function .FunctionRegistry ;
1518import org .elasticsearch .xpack .ql .index .EsIndex ;
1619import org .elasticsearch .xpack .ql .index .IndexResolution ;
20+ import org .elasticsearch .xpack .ql .plan .logical .Aggregate ;
1721import org .elasticsearch .xpack .ql .plan .logical .LogicalPlan ;
1822import org .elasticsearch .xpack .ql .plan .logical .Project ;
1923import org .elasticsearch .xpack .ql .type .EsField ;
3135import static org .elasticsearch .xpack .ql .type .DataTypes .TEXT ;
3236import static org .elasticsearch .xpack .sql .types .SqlTypesTests .loadMapping ;
3337import static org .hamcrest .CoreMatchers .instanceOf ;
38+ import static org .hamcrest .Matchers .contains ;
3439import static org .hamcrest .Matchers .hasItem ;
3540import static org .hamcrest .Matchers .hasItems ;
3641import static org .hamcrest .Matchers .hasSize ;
@@ -178,13 +183,13 @@ public void testFieldAmbiguity() {
178183 VerificationException ex = expectThrows (VerificationException .class , () -> plan ("SELECT test.bar FROM test" ));
179184 assertEquals (
180185 "Found 1 problem\n line 1:8: Reference [test.bar] is ambiguous (to disambiguate use quotes or qualifiers); "
181- + "matches any of [\" test\" .\" bar\" , \" test\" .\" test.bar\" ]" ,
186+ + "matches any of [line 1:22 [ \" test\" .\" bar\" ], line 1:22 [ \" test\" .\" test.bar\" ] ]" ,
182187 ex .getMessage ());
183188
184189 ex = expectThrows (VerificationException .class , () -> plan ("SELECT test.test FROM test" ));
185190 assertEquals (
186191 "Found 1 problem\n line 1:8: Reference [test.test] is ambiguous (to disambiguate use quotes or qualifiers); "
187- + "matches any of [\" test\" .\" test\" , \" test\" .\" test.test\" ]" ,
192+ + "matches any of [line 1:23 [ \" test\" .\" test\" ], line 1:23 [ \" test\" .\" test.test\" ] ]" ,
188193 ex .getMessage ());
189194
190195 LogicalPlan plan = plan ("SELECT test.test FROM test AS x" );
@@ -201,4 +206,75 @@ public void testFieldAmbiguity() {
201206 assertThat (attribute .qualifier (), is ("test" ));
202207 assertThat (attribute .name (), is ("test.test" ));
203208 }
209+
210+ public void testAggregations () {
211+ Map <String , EsField > mapping = TypesTests .loadMapping ("mapping-basic.json" );
212+ EsIndex index = new EsIndex ("test" , mapping );
213+ getIndexResult = IndexResolution .valid (index );
214+ analyzer = new Analyzer (SqlTestUtils .TEST_CFG , functionRegistry , getIndexResult , verifier );
215+
216+ LogicalPlan plan = plan ("SELECT sum(salary) AS s FROM test" );
217+ assertThat (plan , instanceOf (Aggregate .class ));
218+
219+ Aggregate aggregate = (Aggregate ) plan ;
220+ assertThat (aggregate .aggregates (), hasSize (1 ));
221+ NamedExpression attribute = aggregate .aggregates ().get (0 );
222+ assertThat (attribute , instanceOf (Alias .class ));
223+ assertThat (attribute .name (), is ("s" ));
224+ assertThat (aggregate .groupings (), hasSize (0 ));
225+
226+ plan = plan ("SELECT gender AS g, sum(salary) AS s FROM test GROUP BY g" );
227+ assertThat (plan , instanceOf (Aggregate .class ));
228+
229+ aggregate = (Aggregate ) plan ;
230+ List <? extends NamedExpression > aggregates = aggregate .aggregates ();
231+ assertThat (aggregates , hasSize (2 ));
232+ assertThat (aggregates .get (0 ), instanceOf (Alias .class ));
233+ assertThat (aggregates .get (1 ), instanceOf (Alias .class ));
234+ List <String > names = aggregate .aggregates ().stream ().map (NamedExpression ::name ).collect (Collectors .toList ());
235+ assertThat (names , contains ("g" , "s" ));
236+
237+ List <Expression > groupings = aggregate .groupings ();
238+ assertThat (groupings , hasSize (1 ));
239+ FieldAttribute grouping = (FieldAttribute ) groupings .get (0 );
240+ assertThat (grouping .name (), is ("gender" ));
241+ }
242+
243+ public void testGroupByAmbiguity () {
244+ Map <String , EsField > mapping = TypesTests .loadMapping ("mapping-basic.json" );
245+ EsIndex index = new EsIndex ("test" , mapping );
246+ getIndexResult = IndexResolution .valid (index );
247+ analyzer = new Analyzer (SqlTestUtils .TEST_CFG , functionRegistry , getIndexResult , verifier );
248+
249+ VerificationException ex = expectThrows (VerificationException .class ,
250+ () -> plan ("SELECT gender AS g, sum(salary) AS g FROM test GROUP BY g" ));
251+ assertEquals (
252+ "Found 1 problem\n line 1:57: Reference [g] is ambiguous (to disambiguate use quotes or qualifiers); " +
253+ "matches any of [line 1:8 [g], line 1:21 [g]]" ,
254+ ex .getMessage ());
255+
256+ ex = expectThrows (VerificationException .class ,
257+ () -> plan ("SELECT gender AS g, max(salary) AS g, min(salary) AS g FROM test GROUP BY g" ));
258+ assertEquals (
259+ "Found 1 problem\n line 1:75: Reference [g] is ambiguous (to disambiguate use quotes or qualifiers); " +
260+ "matches any of [line 1:8 [g], line 1:21 [g], line 1:39 [g]]" ,
261+ ex .getMessage ());
262+
263+ ex = expectThrows (VerificationException .class ,
264+ () -> plan ("SELECT gender AS g, last_name AS g, sum(salary) AS s FROM test GROUP BY g" ));
265+ assertEquals (
266+ "Found 1 problem\n line 1:73: Reference [g] is ambiguous (to disambiguate use quotes or qualifiers); " +
267+ "matches any of [line 1:8 [g], line 1:21 [g]]" ,
268+ ex .getMessage ());
269+
270+ ex = expectThrows (VerificationException .class ,
271+ () -> plan ("SELECT gender AS g, last_name AS g, min(salary) AS m, max(salary) as m FROM test GROUP BY g, m" ));
272+ assertEquals (
273+ "Found 2 problems\n " +
274+ "line 1:91: Reference [g] is ambiguous (to disambiguate use quotes or qualifiers); "
275+ + "matches any of [line 1:8 [g], line 1:21 [g]]\n " +
276+ "line 1:94: Reference [m] is ambiguous (to disambiguate use quotes or qualifiers); "
277+ + "matches any of [line 1:37 [m], line 1:55 [m]]" ,
278+ ex .getMessage ());
279+ }
204280}
0 commit comments