From dc6a897961c311f981b150e4207ffc1390a219ef Mon Sep 17 00:00:00 2001 From: Monty Date: Thu, 3 Sep 2026 16:16:02 +0300 Subject: [PATCH] Trivial optimziations for group_concat - Remove some if - Reorder code - More code comments --- sql/item_sum.cc | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/sql/item_sum.cc b/sql/item_sum.cc index 6aa59638597..3c25d9f2e49 100644 --- a/sql/item_sum.cc +++ b/sql/item_sum.cc @@ -3877,13 +3877,11 @@ int dump_leaf_key(void* key_arg, element_count count __attribute__((unused)), uint max_length= table->in_use->gconcat_max_len(); String tmp((char *)table->record[1], table->s->reclength, default_charset_info); - String tmp2; uchar *key= (uchar *) key_arg; String *result= &item->result; Item **arg= item->args, **arg_end= item->args + item->arg_count_field; uint old_length= result->length(); - ulonglong *offset_limit= &item->copy_offset_limit; ulonglong *row_limit = &item->copy_row_limit; if (item->limit_clause && !(*row_limit)) { @@ -3891,16 +3889,15 @@ int dump_leaf_key(void* key_arg, element_count count __attribute__((unused)), item->walk_stopped= true; return 1; } - - tmp.length(0); - - if (item->limit_clause && (*offset_limit)) + if (item->copy_offset_limit) { + item->copy_offset_limit--; item->row_count++; - (*offset_limit)--; return 0; } + tmp.length(0); + if (!item->result_finalized) item->result_finalized= true; else @@ -4148,6 +4145,10 @@ Item *Item_func_group_concat::copy_or_same(THD* thd) } +/* + Clear is called for the first element in a new group +*/ + void Item_func_group_concat::clear() { result.length(0); @@ -4156,8 +4157,10 @@ void Item_func_group_concat::clear() warning_for_row= FALSE; result_cut= FALSE; result_finalized= false; + copy_offset_limit= 0; if (offset_limit) copy_offset_limit= offset_limit->val_int(); + /* copy_row_limit does not have to be zeroed if row_limit is not set */ if (row_limit) copy_row_limit= row_limit->val_int(); if (tree) @@ -4747,9 +4750,12 @@ String* Item_func_group_concat::val_str(String* str) result_cut= TRUE; } else if (row_limit && copy_row_limit == (ulonglong)row_limit->val_int()) + { + /* No rows copied; Empty result */ return &result; + } else - DBUG_ASSERT(false); // Can't happen + DBUG_ASSERT(false); // Can't happen. If it happens, no wrong result /* dump_leaf_key() sets this when it writes a row, but a group can end without one: an OFFSET can eat every row, and a walk that failed