MDEV-39932 Accept aggregated outer columns in subquery#5210
Conversation
There was a problem hiding this comment.
Code Review
This pull request modifies Item_field::fix_outer_field to skip the ONLY_FULL_GROUP_BY check when an outer field is referenced within an aggregate function, and adds corresponding test cases. However, the feedback points out that the check !thd->lex->in_sum_func is too broad because it does not verify which query block the aggregate function belongs to. This can lead to incorrectly skipping validation when the outer field is referenced inside an inner subquery's aggregate function. A more precise check comparing aggr_select_lex with the current select block is recommended.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
grooverdan
left a comment
There was a problem hiding this comment.
minor changes. Then we'll get a final review from @mariadb-RexJohnston
gkodinov
left a comment
There was a problem hiding this comment.
This is a preliminary review. Thank you for your contribution!
Dan has some good suggestions. Please address these. Otherwise, good to go from my end.
|
Addressed all comments |
Almost - save and restore sql_mode. Even though its already forced to |
|
Oops forgot about that, should be fixed now |
|
Please rename columns in your tests so that the resultant column names aren't expressions (this can cause issues with running your tests inside views and procedures). Add tests where the outer field isn't in the select list, such as and this |
mariadb-RexJohnston
left a comment
There was a problem hiding this comment.
Please see previous comment left in PR.
Under ONLY_FULL_GROUP_BY, a query that aggregated an outer column inside a subquery was wrongly rejected. This is fixed in Item_field::fix_outer_field by not appending the field to select->join->non_agg_fields when thd->lex->in_sum_func is not null. Furthermore, in Item_sum::check_sum_func, for all outer fields that are not aggregated at their SELECT_LEX's nest level, we append these fields to sel->join->non_agg_fields in order to ensure proper aggregation after the Item_sum's nest_level is known.
Under ONLY_FULL_GROUP_BY, a query that aggregated an outer column inside a subquery was wrongly rejected. E.g.
failed with ER_WRONG_FIELD_WITH_GROUP even though t1_outer.a is aggregated by SUM().
This is fixed in
Item_field::fix_outer_fieldby not appending the field toselect->join->non_agg_fieldswhenthd->lex->in_sum_funcis not null. The test cases ingroup_by.testare updated to reflect this change, and a few extra test cases verifying correctness have been added.