QuerySpecification { public void collect(QuerySpecificationContext query, String queryString) { query.accept(new QuerySpecificationCollector(queryString)); }  void collect(QuerySpecificationContext query, String queryString);  }
@Test void can_collect_group_by_items_in_group_by_clause() { QuerySpecification querySpec = collect( "SELECT name, ABS(age) FROM test GROUP BY name, ABS(age)"); assertEquals( ImmutableList.of( qualifiedName("name"), function("ABS", qualifiedName("age"))), querySpec.getGroupByItems()); }
@Test void can_collect_select_items_in_select_clause() { QuerySpecification querySpec = collect( "SELECT name, ABS(age) FROM test"); assertEquals( ImmutableList.of( qualifiedName("name"), function("ABS", qualifiedName("age"))), querySpec.getSelectItems()); }
@Test void can_collect_aggregators_in_select_clause() { QuerySpecification querySpec = collect( "SELECT name, AVG(age), SUM(balance) FROM test GROUP BY name"); assertEquals( ImmutableSet.of( alias("AVG(age)", aggregate("AVG", qualifiedName("age"))), alias("SUM(balance)", aggregate("SUM", qualifiedName("balance")))), querySpec.getAggregators()); }
@Test void can_collect_nested_aggregators_in_select_clause() { QuerySpecification querySpec = collect( "SELECT name, ABS(1 + AVG(age)) FROM test GROUP BY name"); assertEquals( ImmutableSet.of( alias("AVG(age)", aggregate("AVG", qualifiedName("age")))), querySpec.getAggregators()); }
@Test void can_collect_alias_in_select_clause() { QuerySpecification querySpec = collect( "SELECT name AS n FROM test GROUP BY n"); assertEquals( ImmutableMap.of("n", qualifiedName("name")), querySpec.getSelectItemsByAlias()); }
@Test void should_deduplicate_same_aggregators() { QuerySpecification querySpec = collect( "SELECT AVG(age), AVG(balance), AVG(age) FROM test GROUP BY name"); assertEquals( ImmutableSet.of( alias("AVG(age)", aggregate("AVG", qualifiedName("age"))), alias("AVG(balance)", aggregate("AVG", qualifiedName("balance")))), querySpec.getAggregators()); }