Skip to content

MDEV-39932 Accept aggregated outer columns in subquery#5210

Open
jaeheonshim wants to merge 1 commit into
MariaDB:10.11from
jaeheonshim:MDEV-39932
Open

MDEV-39932 Accept aggregated outer columns in subquery#5210
jaeheonshim wants to merge 1 commit into
MariaDB:10.11from
jaeheonshim:MDEV-39932

Conversation

@jaeheonshim

Copy link
Copy Markdown

Under ONLY_FULL_GROUP_BY, a query that aggregated an outer column inside a subquery was wrongly rejected. E.g.

SELECT (SELECT SUM(t1_outer.a) FROM t1 AS t1_inner LIMIT 1)
  FROM t1 AS t1_outer GROUP BY t1_outer.b;

failed with ER_WRONG_FIELD_WITH_GROUP even though t1_outer.a is aggregated by SUM().

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. The test cases in group_by.test are updated to reflect this change, and a few extra test cases verifying correctness have been added.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread sql/item.cc
@jaeheonshim jaeheonshim marked this pull request as ready for review June 10, 2026 04:46

@grooverdan grooverdan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor changes. Then we'll get a final review from @mariadb-RexJohnston

Comment thread mysql-test/main/group_by.test Outdated
Comment thread sql/item_sum.cc Outdated
Comment thread sql/item_sum.cc Outdated
@grooverdan grooverdan added GSoC External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. labels Jun 10, 2026

@gkodinov gkodinov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jaeheonshim

Copy link
Copy Markdown
Author

Addressed all comments

@grooverdan

Copy link
Copy Markdown
Member

Addressed all comments

Almost - save and restore sql_mode.

set @save_sql_mode = @@sql_mode;
set sql_mode='ONLY_FULL_GROUP_BY';
...
set sql_mode= @save_sql_mode;

Even though its already forced to ='' as a "restoration" early in the test.

@jaeheonshim

Copy link
Copy Markdown
Author

Oops forgot about that, should be fixed now

@mariadb-RexJohnston

Copy link
Copy Markdown
Member

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

SELECT (SELECT SUM(t1_inner.a) FROM t1 AS t1_inner where t1_inner.a = t1_outer.a LIMIT 1)
  FROM t1 AS t1_outer GROUP BY t1_outer.b;

and this

SELECT (SELECT SUM(t1_inner.a) FROM t1 AS t1_inner group by t1_inner.b having t1_outer.a > 1 LIMIT 1)
  FROM t1 AS t1_outer GROUP BY t1_outer.b;

@mariadb-RexJohnston mariadb-RexJohnston left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. GSoC

Development

Successfully merging this pull request may close these issues.

6 participants