Skip to content

Commit 4652260

Browse files
committed
MDEV-28616 Crash when using derived table over union with order by clause
This bug manifested itself when the server processed a query containing a derived table over union whose ORDER BY clause included a subquery with unresolvable column reference. For such a query the server crashed when trying to resolve column references in the ORDER BY clause used by union. For any union with ORDER BY clause an extra SELECT_LEX structure is created and it is attached to SELECT_LEX_UNIT structure of the union via the field fake_select_lex. The outer context for fake_select_lex must be the same as for other selects of the union. If the union is used in the FROM list of a derived table then the outer context for fake_select_lex must be set to NULL in line with other selects of the union. It was not done and it caused a crash when searching for possible resolution of an unresolvable column reference occurred in a subquery used in the ORDER BY clause. Approved by Oleksandr Byelkin <sanja@mariadb.com>
1 parent 2279ddd commit 4652260

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

mysql-test/main/derived.result

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,5 +1327,28 @@ a b
13271327
DROP VIEW v1;
13281328
DROP TABLE t1;
13291329
#
1330+
# MDEV-28616: derived table over union with order by clause that
1331+
# contains subquery with unresolvable column reference
1332+
#
1333+
SELECT 1 FROM (
1334+
SELECT 1 UNION SELECT 2 ORDER BY (SELECT 1 FROM DUAL WHERE xxx = 0)
1335+
) dt;
1336+
ERROR 42S22: Unknown column 'xxx' in 'where clause'
1337+
create table t1 (a int, b int);
1338+
insert into t1 values (3,8), (7,2), (1,4), (5,9);
1339+
create table t2 (a int, b int);
1340+
insert into t2 values (9,1), (7,3), (2,6);
1341+
create table t3 (c int, d int);
1342+
insert into t3 values (7,8), (1,2), (3,8);
1343+
select * from
1344+
(
1345+
select a,b from t1 where t1.a > 3
1346+
union
1347+
select a,b from t2 where t2.b < 6
1348+
order by (a - b / (select a + max(c) from t3 where d = x))
1349+
) dt;
1350+
ERROR 42S22: Unknown column 'x' in 'where clause'
1351+
drop table t1,t2,t3;
1352+
#
13301353
# End of 10.3 tests
13311354
#

mysql-test/main/derived.test

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,36 @@ SELECT * FROM v1 WHERE b > 0;
11371137
DROP VIEW v1;
11381138
DROP TABLE t1;
11391139

1140+
--echo #
1141+
--echo # MDEV-28616: derived table over union with order by clause that
1142+
--echo # contains subquery with unresolvable column reference
1143+
--echo #
1144+
1145+
--error ER_BAD_FIELD_ERROR
1146+
SELECT 1 FROM (
1147+
SELECT 1 UNION SELECT 2 ORDER BY (SELECT 1 FROM DUAL WHERE xxx = 0)
1148+
) dt;
1149+
1150+
create table t1 (a int, b int);
1151+
insert into t1 values (3,8), (7,2), (1,4), (5,9);
1152+
1153+
create table t2 (a int, b int);
1154+
insert into t2 values (9,1), (7,3), (2,6);
1155+
1156+
create table t3 (c int, d int);
1157+
insert into t3 values (7,8), (1,2), (3,8);
1158+
1159+
--error ER_BAD_FIELD_ERROR
1160+
select * from
1161+
(
1162+
select a,b from t1 where t1.a > 3
1163+
union
1164+
select a,b from t2 where t2.b < 6
1165+
order by (a - b / (select a + max(c) from t3 where d = x))
1166+
) dt;
1167+
1168+
drop table t1,t2,t3;
1169+
11401170
--echo #
11411171
--echo # End of 10.3 tests
11421172
--echo #

sql/sql_derived.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,9 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
771771
cursor->outer_join|= JOIN_TYPE_OUTER;
772772
}
773773
}
774+
// Prevent it for possible ORDER BY clause
775+
if (unit->fake_select_lex)
776+
unit->fake_select_lex->context.outer_context= 0;
774777

775778
/*
776779
Above cascade call of prepare is important for PS protocol, but after it

0 commit comments

Comments
 (0)