[#766] Fix incorrect results if sub query HAVING uses GROUP BY column from another sub query in the outer query FROM/JOIN
Background
-
A recent commit d5b3c861 (as part of !1022 (merged)) fixed the #766 (closed) query mentioned in the issue description on gitlab but a similar issue exists when a subquery is used instead of a table in the FROM/JOIN list.
-
See below example where instead of
FROM customers cin the first query, the second query usesFROM (select * from customers) c. The two usages are identical but the second query output is5 rowsinstead of the expected3 rows.OCTO> SELECT ALL c.customer_id FROM customers c WHERE EXISTS (SELECT 1 FROM orders GROUP BY order_id having c.customer_id > 2) GROUP BY customer_id; CUSTOMER_ID 3 4 5 (3 rows) OCTO> SELECT ALL c.customer_id FROM (select * from customers) c WHERE EXISTS (SELECT 1 FROM orders GROUP BY order_id having c.customer_id > 2) GROUP BY customer_id; CUSTOMER_ID 1 2 3 4 5 (5 rows)
Issue
-
d5b3c861 fixed
src/m_templates/tmpl_column_reference.ctemplatebut the same code is duplicated for theLP_DERIVED_COLUMNcase insrc/m_templates/tmpl_print_expression.ctemplateand was not fixed then.
Fix
-
A new
src/m_templates/tmpl_column_reference_common.ctemplatefile now has code that is common to bothtmpl_column_reference.ctemplateand theLP_DERIVED_COLUMNcase oftmpl_print_expression.ctemplate. This is now invoked from both the callers thereby avoiding code duplication. -
Since the common code now has the fix done in d5b3c861, the above issue for the
LP_DERIVED_COLUMNcase is automatically fixed as part of the code move.
Misc
-
Enhanced comments in
src/optimization_transforms/lp_replace_derived_table_references.cfor whyderived_columnfield is needed. -
Removed code in
src/optimization_transforms/lp_generate_xref_keys.cthat was long commented out and only likely to be confusing to a future reader.
Test
- The existing
test_group_by/TGB14subtest has been enhanced to verify the fixes in this commit.