summaryrefslogtreecommitdiff
path: root/sql/item_sum.cc
diff options
context:
space:
mode:
authorMonty <monty@mariadb.org>2023-01-02 18:34:19 +0200
committerMonty <monty@mariadb.org>2023-01-03 19:44:19 +0200
commitd0603fc5ba4dc17a155a575edd79ae0fb9de3679 (patch)
treee4cdc657cd71347b091413f964890b8f8cec367e /sql/item_sum.cc
parent8b9b4ab3f59f86e1c8f6cd6a0e6b8916db61933d (diff)
downloadmariadb-git-d0603fc5ba4dc17a155a575edd79ae0fb9de3679.tar.gz
MDEV-30240 Wrong result upon aggregate function with SQL_BUFFER_RESULT
The problem was that when storing rows into a temporary table, MIN/MAX items that where marked as constants (as theire value had been computed at start of query) would be reset. Fixed by not reseting MIN/MAX items that are marked as const in Item_sum_min_max::clear().
Diffstat (limited to 'sql/item_sum.cc')
-rw-r--r--sql/item_sum.cc14
1 files changed, 12 insertions, 2 deletions
diff --git a/sql/item_sum.cc b/sql/item_sum.cc
index 1c17b5c6409..9baf945644e 100644
--- a/sql/item_sum.cc
+++ b/sql/item_sum.cc
@@ -2361,8 +2361,15 @@ Item *Item_sum_variance::result_item(THD *thd, Field *field)
void Item_sum_min_max::clear()
{
DBUG_ENTER("Item_sum_min_max::clear");
- value->clear();
- null_value= 1;
+ /*
+ We should not clear const items (from SELECT MIN(key) from t1) as then we would loose the
+ value cached in opt_sum_query() where we replace MIN/MAX/COUNT with constants.
+ */
+ if (!const_item())
+ {
+ value->clear();
+ null_value= 1;
+ }
DBUG_VOID_RETURN;
}
@@ -2475,9 +2482,12 @@ void Item_sum_min_max::no_rows_in_result()
/* We may be called here twice in case of ref field in function */
if (was_values)
{
+ bool org_const_item_cache= const_item_cache;
was_values= FALSE;
was_null_value= value->null_value;
+ const_item_cache= 0; // Ensure that clear works on const items
clear();
+ const_item_cache= org_const_item_cache;
}
DBUG_VOID_RETURN;
}