Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions mysql-test/main/subselect4.result
Original file line number Diff line number Diff line change
Expand Up @@ -3373,4 +3373,27 @@ CREATE TABLE t(c INT);
SELECT (SELECT 0 GROUP BY c HAVING (SELECT c)) FROM t GROUP BY c;
(SELECT 0 GROUP BY c HAVING (SELECT c))
DROP TABLE t;
#
# MDEV-38476 Wrong Result (Empty Set) with derived_merge=on using AVG() on text column in HAVING clause
#
CREATE TABLE t1(c1 TEXT);
INSERT INTO t1(c1) VALUES ('a'),('ab'),('cc');
SELECT MAX(ca1) FROM (SELECT c1 AS ca1 FROM t1) AS ta1
GROUP BY ca1
HAVING AVG(ca1) RLIKE (TRUE & CHARACTER_LENGTH(ca1));
MAX(ca1)
ab
cc
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: 'a'
Warning 1292 Truncated incorrect DOUBLE value: 'ab'
Warning 1292 Truncated incorrect DOUBLE value: 'cc'
TRUNCATE t1;
INSERT INTO t1 (c1) VALUES (NULL), ('abc');
SELECT ca1 FROM (SELECT c1 AS ca1, c1 AS ca2 FROM t1) AS ta1
GROUP BY ca2
HAVING (ca2 = ANY (SELECT c1 FROM t1)) IS NOT TRUE;
ca1
NULL
DROP TABLE t1;
# End of 10.11 tests
18 changes: 18 additions & 0 deletions mysql-test/main/subselect4.test
Original file line number Diff line number Diff line change
Expand Up @@ -2695,4 +2695,22 @@ CREATE TABLE t(c INT);
SELECT (SELECT 0 GROUP BY c HAVING (SELECT c)) FROM t GROUP BY c;
DROP TABLE t;

--echo #
--echo # MDEV-38476 Wrong Result (Empty Set) with derived_merge=on using AVG() on text column in HAVING clause
--echo #

CREATE TABLE t1(c1 TEXT);
INSERT INTO t1(c1) VALUES ('a'),('ab'),('cc');
SELECT MAX(ca1) FROM (SELECT c1 AS ca1 FROM t1) AS ta1
GROUP BY ca1
HAVING AVG(ca1) RLIKE (TRUE & CHARACTER_LENGTH(ca1));

TRUNCATE t1;
INSERT INTO t1 (c1) VALUES (NULL), ('abc');
SELECT ca1 FROM (SELECT c1 AS ca1, c1 AS ca2 FROM t1) AS ta1
GROUP BY ca2
HAVING (ca2 = ANY (SELECT c1 FROM t1)) IS NOT TRUE;

DROP TABLE t1;

--echo # End of 10.11 tests
25 changes: 19 additions & 6 deletions sql/item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5782,8 +5782,6 @@ resolve_ref_in_select_and_group(THD *thd, Item_ident *ref, SELECT_LEX *select)
DBUG_ASSERT((*select_ref)->fixed());
return &select->ref_pointer_array[counter];
}
if (group_by_ref && (*group_by_ref)->type() == Item::REF_ITEM)
return ((Item_ref*)(*group_by_ref))->ref;
if (group_by_ref)
return group_by_ref;
DBUG_ASSERT(FALSE);
Expand Down Expand Up @@ -6063,8 +6061,24 @@ Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference)
return -1; /* Some error occurred (e.g. ambiguous names). */
if (ref != not_found_item)
{
DBUG_ASSERT(*ref && (*ref)->fixed());
prev_subselect_item->used_tables_and_const_cache_join(*ref);
DBUG_ASSERT(*ref);

/*
If this item isn't yet fixed, it is an Item_outer_ref, wrapping an
item. If that item is fixed, we can fix this wrapper now. Otherwise
it will need to wait until the fix_inner_refs() in JOIN::prepare()
*/
if (!(*ref)->fixed() &&
(*ref)->type() == Item::REF_ITEM)
{
Item_ref *item_ref= static_cast<Item_ref *>(*ref);
Item *refref= *(item_ref->ref);
if (refref->fixed())
(*ref)->fix_fields_if_needed( thd, reference);
}
if ((*ref)->fixed())
prev_subselect_item->used_tables_and_const_cache_join(*ref);

break;
}
}
Expand Down Expand Up @@ -6105,8 +6119,7 @@ Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference)
Item *save;
Item_ref *rf;

/* Should have been checked in resolve_ref_in_select_and_group(). */
DBUG_ASSERT(*ref && (*ref)->fixed());
DBUG_ASSERT(*ref);
/*
Here, a subset of actions performed by Item_ref::set_properties
is not enough. So we pass ptr to NULL into Item_[direct]_ref
Expand Down