Skip to content

Commit fa1438c

Browse files
committed
MDEV-8913 Derived queries with same column names as final projection causes issues when using Order By
find_item_in_list() now recognize view fields like a fields even if they rever to an expression. The problem of schema name do not taken into account for field with it and derived table fixed. Duplicating code removed
1 parent bf18631 commit fa1438c

File tree

3 files changed

+78
-30
lines changed

3 files changed

+78
-30
lines changed

mysql-test/r/view.result

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5699,6 +5699,47 @@ idAlbum strAlbum strMusicBrainzAlbumID strArtists strGenres iYear strMoods strSt
56995699
1 strAlbum1 strMusicBrainzAlbumID1 strArtists1 strGenres1 2000 NULL NULL NULL NULL NULL NULL NULL NULL 0 0 album 1 1 strArtist1 strMusicBrainzArtistID 0 0
57005700
drop view v1,v2;
57015701
drop table t1,t2,t3,t4;
5702+
#
5703+
# MDEV-8913: Derived queries with same column names as final
5704+
# projection causes issues when using Order By
5705+
#
5706+
create table t1 (field int);
5707+
insert into t1 values (10),(5),(3),(8),(20);
5708+
SELECT sq.f2 AS f1, sq.f1 AS f2
5709+
FROM ( SELECT field AS f1, 1 AS f2 FROM t1) AS sq
5710+
ORDER BY sq.f1;
5711+
f1 f2
5712+
1 3
5713+
1 5
5714+
1 8
5715+
1 10
5716+
1 20
5717+
create view v1 as SELECT field AS f1, 1 AS f2 FROM t1;
5718+
SELECT sq.f2 AS f1, sq.f1 AS f2
5719+
FROM v1 AS sq
5720+
ORDER BY sq.f1;
5721+
f1 f2
5722+
1 3
5723+
1 5
5724+
1 8
5725+
1 10
5726+
1 20
5727+
drop view v1;
5728+
create table t2 SELECT field AS f1, 1 AS f2 FROM t1;
5729+
SELECT
5730+
sq.f2 AS f1,
5731+
sq.f1 AS f2
5732+
FROM t2 AS sq
5733+
ORDER BY sq.f1;
5734+
f1 f2
5735+
1 3
5736+
1 5
5737+
1 8
5738+
1 10
5739+
1 20
5740+
drop table t1, t2;
5741+
SELECT 1 FROM (SELECT 1 as a) AS b HAVING (SELECT `SOME_GARBAGE`.b.a)=1;
5742+
ERROR 42S22: Unknown column 'SOME_GARBAGE.b.a' in 'field list'
57025743
# -----------------------------------------------------------------
57035744
# -- End of 10.0 tests.
57045745
# -----------------------------------------------------------------

mysql-test/t/view.test

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5602,6 +5602,38 @@ SELECT v1.*,v2.* FROM v1 LEFT JOIN v2 ON v1.idAlbum = v2.idAlbum WHERE v1.idAlbu
56025602
drop view v1,v2;
56035603
drop table t1,t2,t3,t4;
56045604

5605+
--echo #
5606+
--echo # MDEV-8913: Derived queries with same column names as final
5607+
--echo # projection causes issues when using Order By
5608+
--echo #
5609+
create table t1 (field int);
5610+
insert into t1 values (10),(5),(3),(8),(20);
5611+
5612+
SELECT sq.f2 AS f1, sq.f1 AS f2
5613+
FROM ( SELECT field AS f1, 1 AS f2 FROM t1) AS sq
5614+
ORDER BY sq.f1;
5615+
5616+
create view v1 as SELECT field AS f1, 1 AS f2 FROM t1;
5617+
5618+
SELECT sq.f2 AS f1, sq.f1 AS f2
5619+
FROM v1 AS sq
5620+
ORDER BY sq.f1;
5621+
5622+
drop view v1;
5623+
5624+
create table t2 SELECT field AS f1, 1 AS f2 FROM t1;
5625+
5626+
SELECT
5627+
sq.f2 AS f1,
5628+
sq.f1 AS f2
5629+
FROM t2 AS sq
5630+
ORDER BY sq.f1;
5631+
5632+
drop table t1, t2;
5633+
5634+
--error ER_BAD_FIELD_ERROR
5635+
SELECT 1 FROM (SELECT 1 as a) AS b HAVING (SELECT `SOME_GARBAGE`.b.a)=1;
5636+
56055637
--echo # -----------------------------------------------------------------
56065638
--echo # -- End of 10.0 tests.
56075639
--echo # -----------------------------------------------------------------

sql/sql_base.cc

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6423,6 +6423,7 @@ find_field_in_table_ref(THD *thd, TABLE_LIST *table_list,
64236423
*/
64246424
table_name && table_name[0] &&
64256425
(my_strcasecmp(table_alias_charset, table_list->alias, table_name) ||
6426+
(db_name && db_name[0] && (!table_list->db || !table_list->db[0])) ||
64266427
(db_name && db_name[0] && table_list->db && table_list->db[0] &&
64276428
(table_list->schema_table ?
64286429
my_strcasecmp(system_charset_info, db_name, table_list->db) :
@@ -6896,7 +6897,10 @@ find_item_in_list(Item *find, List<Item> &items, uint *counter,
68966897

68976898
for (uint i= 0; (item=li++); i++)
68986899
{
6899-
if (field_name && item->real_item()->type() == Item::FIELD_ITEM)
6900+
if (field_name &&
6901+
(item->real_item()->type() == Item::FIELD_ITEM ||
6902+
((item->type() == Item::REF_ITEM) &&
6903+
(((Item_ref *)item)->ref_type() == Item_ref::VIEW_REF))))
69006904
{
69016905
Item_ident *item_field= (Item_ident*) item;
69026906

@@ -7022,35 +7026,6 @@ find_item_in_list(Item *find, List<Item> &items, uint *counter,
70227026
break;
70237027
}
70247028
}
7025-
else if (table_name && item->type() == Item::REF_ITEM &&
7026-
((Item_ref *)item)->ref_type() == Item_ref::VIEW_REF)
7027-
{
7028-
/*
7029-
TODO:Here we process prefixed view references only. What we should
7030-
really do is process all types of Item_refs. But this will currently
7031-
lead to a clash with the way references to outer SELECTs (from the
7032-
HAVING clause) are handled in e.g. :
7033-
SELECT 1 FROM t1 AS t1_o GROUP BY a
7034-
HAVING (SELECT t1_o.a FROM t1 AS t1_i GROUP BY t1_i.a LIMIT 1).
7035-
Processing all Item_refs here will cause t1_o.a to resolve to itself.
7036-
We still need to process the special case of Item_direct_view_ref
7037-
because in the context of views they have the same meaning as
7038-
Item_field for tables.
7039-
*/
7040-
Item_ident *item_ref= (Item_ident *) item;
7041-
if (field_name && item_ref->name && item_ref->table_name &&
7042-
!my_strcasecmp(system_charset_info, item_ref->name, field_name) &&
7043-
!my_strcasecmp(table_alias_charset, item_ref->table_name,
7044-
table_name) &&
7045-
(!db_name || (item_ref->db_name &&
7046-
!strcmp (item_ref->db_name, db_name))))
7047-
{
7048-
found= li.ref();
7049-
*counter= i;
7050-
*resolution= RESOLVED_IGNORING_ALIAS;
7051-
break;
7052-
}
7053-
}
70547029
}
70557030
if (!found)
70567031
{

0 commit comments

Comments
 (0)