Skip to content

Commit

Permalink
SQL: prevent duplicate generation for repeated aggs
Browse files Browse the repository at this point in the history
Prevent generation of duplicate aggs caused by repetitive functions,
leading to invalid query.

Fix elastic#30287
  • Loading branch information
costin committed Aug 29, 2018
1 parent e1e8cf3 commit bdd1443
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ public Aggs addGroups(Collection<GroupByKey> groups) {
}

public Aggs addAgg(LeafAgg agg) {
if (metricAggs.contains(agg)) {
return this;
}
return new Aggs(groups, combine(metricAggs, agg), pipelineAggs);
}

Expand Down
10 changes: 9 additions & 1 deletion x-pack/qa/sql/src/main/resources/agg.sql-spec
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,12 @@ SELECT MIN(salary) min, MAX(salary) max, gender g, languages l, COUNT(*) c FROM
aggMultiWithHavingOnCount
SELECT MIN(salary) min, MAX(salary) max, gender g, COUNT(*) c FROM "test_emp" WHERE languages > 0 GROUP BY g HAVING c > 40 ORDER BY gender;
aggMultiGroupByMultiWithHavingOnCount
SELECT MIN(salary) min, MAX(salary) max, gender g, languages l, COUNT(*) c FROM "test_emp" WHERE languages > 0 GROUP BY g, languages HAVING c > 40 ORDER BY gender, languages;
SELECT MIN(salary) min, MAX(salary) max, gender g, languages l, COUNT(*) c FROM "test_emp" WHERE languages > 0 GROUP BY g, languages HAVING c > 40 ORDER BY gender, languages;

// repetion of same aggs to check whether the generated query contains duplicates or not
aggRepeatFunctionAcrossFields
SELECT MIN(emp_no) AS a, 1 + MIN(emp_no) AS b, ABS(MIN(emp_no)) AS c FROM test_emp;
aggRepeatFunctionBetweenSelectAndHaving
SELECT gender, COUNT(DISTINCT languages) AS c FROM test_emp GROUP BY gender HAVING count(DISTINCT languages) > 0 ORDER BY gender;


0 comments on commit bdd1443

Please sign in to comment.