| 261 | */ |
| 262 | |
| 263 | bool Item_sum::register_sum_func(THD *thd, Item **ref) |
| 264 | { |
| 265 | nesting_map allow_sum_func= thd->lex->allow_sum_func; |
| 266 | |
| 267 | // Find the outer-most query block where this function can be aggregated. |
| 268 | |
| 269 | for (SELECT_LEX *sl= thd->lex->current_select->outer_select(); |
| 270 | sl && sl->nest_level >= max_arg_level; |
| 271 | sl= sl->outer_select()) |
| 272 | { |
| 273 | if (allow_sum_func & ((nesting_map)1 << sl->nest_level)) |
| 274 | { |
| 275 | aggr_level= sl->nest_level; |
| 276 | aggr_sel= sl; |
| 277 | } |
| 278 | /* |
| 279 | Cannot go above level zero. This might be possible in 5.6 because the |
| 280 | nest_level of the query blocks of a view are not adjusted when the view |
| 281 | is attached to the outer query. However, 5.7 adjusts nest_level |
| 282 | correctly, so this code is only necessary in 5.6. |
| 283 | */ |
| 284 | if (sl->nest_level == 0) |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | if (aggr_level >= 0) |
| 289 | { |
| 290 | ref_by= ref; |
| 291 | /* Add the object to the list of registered objects assigned to aggr_sel */ |
| 292 | if (!aggr_sel->inner_sum_func_list) |
| 293 | next= this; |
| 294 | else |
| 295 | { |
| 296 | next= aggr_sel->inner_sum_func_list->next; |
| 297 | aggr_sel->inner_sum_func_list->next= this; |
| 298 | } |
| 299 | aggr_sel->inner_sum_func_list= this; |
| 300 | aggr_sel->with_sum_func= true; |
| 301 | |
| 302 | /* |
| 303 | Mark Item_subselect(s) as containing aggregate function all the way up |
| 304 | to aggregate function's calculation context. |
| 305 | Note that we must not mark the Item of calculation context itself |
| 306 | because with_sum_func on the calculation context st_select_lex is |
| 307 | already set above. |
| 308 | |
| 309 | with_sum_func being set for an Item means that this Item refers |
| 310 | (somewhere in it, e.g. one of its arguments if it's a function) directly |
| 311 | or through intermediate items to an aggregate function that is calculated |
| 312 | in a context "outside" of the Item (e.g. in the current or outer select). |
| 313 | |
| 314 | with_sum_func being set for an st_select_lex means that this query block |
| 315 | has aggregate functions directly referenced (i.e. not through a subquery). |
| 316 | */ |
| 317 | for (SELECT_LEX *sl= thd->lex->current_select; |
| 318 | sl && sl != aggr_sel && sl->master_unit()->item; |
| 319 | sl= sl->outer_select()) |
| 320 | sl->master_unit()->item->with_sum_func= true; |
nothing calls this directly
no test coverage detected