Skip to content

Commit

Permalink
SQL: prevent duplicate generation for repeated aggs (#33252)
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 #30287

(cherry picked from commit 83c3d7a)
(cherry picked from commit 61e0ce7)
  • Loading branch information
costin committed Aug 30, 2018
1 parent 4a548b0 commit 867a10a
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 867a10a

Please sign in to comment.