Skip to content

Commit 34fb926

Browse files
author
Sreeharsha Ramanavarapu
committed
Bug #22328395: REGRESSION: ?UNKNOWN COLUMN? FOR OUTER
COMPUTED VALUES USED INSIDE A SUBQUERY ISSUE: ------ In the fix for Bug# 19823076, a subquery in the SELECT list was no longer allowed to refer to an alias of a column in the outer query. This was justified, since it was not supported by the standard sql syntax. SOLUTION: --------- Given that this is an extension used frequently, the restriction introduced in Bug# 19823076 is now removed.
1 parent a688940 commit 34fb926

19 files changed

+403
-169
lines changed

mysql-test/include/order_by.inc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,6 @@ SELECT a + 1 AS num FROM t1 GROUP BY 30 - num;
585585
SELECT a + 1 AS num FROM t1 HAVING 30 - num;
586586
--error ER_BAD_FIELD_ERROR
587587
SELECT a + 1 AS num, num + 1 FROM t1;
588-
--error ER_BAD_FIELD_ERROR
589588
SELECT a + 1 AS num, (select num + 2 FROM t1 LIMIT 1) FROM t1;
590589
--error ER_BAD_FIELD_ERROR
591590
SELECT a.a + 1 AS num FROM t1 a JOIN t1 b ON num = b.a;

mysql-test/include/subquery.inc

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ SELECT (SELECT 1) UNION SELECT (SELECT 2);
2424
explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2);
2525
SELECT (SELECT (SELECT 0 UNION SELECT 0));
2626
explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0));
27-
-- error ER_BAD_FIELD_ERROR
27+
-- error ER_ILLEGAL_REFERENCE
2828
SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a;
29-
-- error ER_BAD_FIELD_ERROR
29+
-- error ER_ILLEGAL_REFERENCE
3030
SELECT (SELECT 1 FROM (SELECT 1) as b HAVING b=1) as a,(SELECT 1 FROM (SELECT 1) as c HAVING a=1) as b;
3131
SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a;
32-
-- error ER_BAD_FIELD_ERROR
32+
-- error ER_ILLEGAL_REFERENCE
3333
SELECT (SELECT a) as a;
3434
EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
3535
SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
@@ -71,7 +71,6 @@ SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
7171
-- error ER_OPERAND_COLUMNS
7272
SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a);
7373

74-
-- error ER_BAD_FIELD_ERROR
7574
SELECT 1 as a,(SELECT a+a) b,(SELECT b);
7675

7776
create table t1 (a int);
@@ -81,7 +80,7 @@ create table t4 (a int not null, b int not null);
8180
insert into t1 values (2);
8281
insert into t2 values (1,7),(2,7);
8382
insert into t4 values (4,8),(3,8),(5,9);
84-
-- error ER_BAD_FIELD_ERROR
83+
-- error ER_ILLEGAL_REFERENCE
8584
select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1;
8685
select (select a from t1 where t1.a=t2.a), a from t2;
8786
select (select a from t1 where t1.a=t2.b), a from t2;
@@ -295,7 +294,7 @@ CREATE TABLE `t1` (
295294
UNIQUE KEY `numreponse` (`numreponse`),
296295
KEY `pseudo` (`pseudo`,`numeropost`)
297296
) ENGINE=MyISAM;
298-
-- error ER_BAD_FIELD_ERROR
297+
-- error ER_ILLEGAL_REFERENCE
299298
SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a;
300299
-- error ER_BAD_FIELD_ERROR
301300
SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a;
@@ -615,15 +614,18 @@ select * from t1;
615614

616615
drop table t1, t2, t3;
617616

618-
--error ER_BAD_FIELD_ERROR
619617
SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
620618
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;
621619
SHOW CREATE TABLE t1;
622620
drop table t1;
623-
--error ER_BAD_FIELD_ERROR
624621
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
625-
--error ER_BAD_FIELD_ERROR
622+
SHOW CREATE TABLE t1;
623+
DROP TABLE t1;
624+
626625
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
626+
SHOW CREATE TABLE t1;
627+
DROP TABLE t1;
628+
627629
CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
628630
select * from t1;
629631
SHOW CREATE TABLE t1;
@@ -1574,15 +1576,14 @@ drop table t1;
15741576
#
15751577
create table t1 (a integer);
15761578
insert into t1 values (1);
1577-
-- error ER_BAD_FIELD_ERROR
1579+
-- error ER_ILLEGAL_REFERENCE
15781580
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ;
1579-
-- error ER_BAD_FIELD_ERROR
1581+
-- error ER_ILLEGAL_REFERENCE
15801582
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
1581-
-- error ER_BAD_FIELD_ERROR
15821583
select 1 as xx, 1 = ALL ( select 1 from t1 where 1 = xx );
1583-
-- error ER_BAD_FIELD_ERROR
1584+
-- error ER_ILLEGAL_REFERENCE
15841585
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
1585-
-- error ER_BAD_FIELD_ERROR
1586+
-- error ER_ILLEGAL_REFERENCE
15861587
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL;
15871588
drop table t1;
15881589

@@ -3916,6 +3917,11 @@ SELECT a, ROW(11, 12) = (SELECT a, 12), ROW(11, 12) IN (SELECT a, 12) FROM t1 GR
39163917
SELECT a, ROW(11, 12) = (SELECT a, 22), ROW(11, 12) IN (SELECT a, 22) FROM t1;
39173918
SELECT a, ROW(11, 12) = (SELECT a, 12), ROW(11, 12) IN (SELECT a, 12) FROM t1;
39183919

3920+
# The x alias is used below to workaround bug #40674. Regression tests for
3921+
# sum function on outer column in subselect from dual:
3922+
SELECT a AS x, ROW(11, 12) = (SELECT MAX(x), 22), ROW(11, 12) IN (SELECT MAX(x), 22) FROM t1;
3923+
SELECT a AS x, ROW(11, 12) = (SELECT MAX(x), 12), ROW(11, 12) IN (SELECT MAX(x), 12) FROM t1;
3924+
39193925
DROP TABLE t1;
39203926

39213927
--echo # both columns should be same

mysql-test/r/group_by.result

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,38 +1816,39 @@ DROP TABLE t2;
18161816
CREATE TABLE t1 ( a INT, b INT );
18171817
SELECT b c, (SELECT a FROM t1 WHERE b = c)
18181818
FROM t1;
1819-
ERROR 42S22: Unknown column 'c' in 'where clause'
1819+
c (SELECT a FROM t1 WHERE b = c)
18201820
SELECT b c, (SELECT a FROM t1 WHERE b = c)
18211821
FROM t1
18221822
HAVING b = 10;
1823-
ERROR 42S22: Unknown column 'c' in 'where clause'
1823+
c (SELECT a FROM t1 WHERE b = c)
18241824
SELECT MAX(b) c, (SELECT a FROM t1 WHERE b = c)
18251825
FROM t1
18261826
HAVING b = 10;
1827-
ERROR 42S22: Unknown column 'c' in 'where clause'
1827+
ERROR 42S22: Reference 'c' not supported (reference to group function)
18281828
SET @old_sql_mode = @@sql_mode;
18291829
SET @@sql_mode='ONLY_FULL_GROUP_BY';
18301830
Warnings:
18311831
Warning 3090 Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.
18321832
SELECT b c, (SELECT a FROM t1 WHERE b = c)
18331833
FROM t1;
1834-
ERROR 42S22: Unknown column 'c' in 'where clause'
1834+
c (SELECT a FROM t1 WHERE b = c)
18351835
SELECT b c, (SELECT a FROM t1 WHERE b = c)
18361836
FROM t1
18371837
HAVING b = 10;
1838-
ERROR 42S22: Unknown column 'c' in 'where clause'
1838+
c (SELECT a FROM t1 WHERE b = c)
18391839
SELECT MAX(b) c, (SELECT a FROM t1 WHERE b = c)
18401840
FROM t1
18411841
HAVING b = 10;
1842-
ERROR 42S22: Unknown column 'c' in 'where clause'
1842+
ERROR 42S22: Reference 'c' not supported (reference to group function)
18431843
INSERT INTO t1 VALUES (1, 1);
18441844
SELECT b c, (SELECT a FROM t1 WHERE b = c)
18451845
FROM t1;
1846-
ERROR 42S22: Unknown column 'c' in 'where clause'
1846+
c (SELECT a FROM t1 WHERE b = c)
1847+
1 1
18471848
INSERT INTO t1 VALUES (2, 1);
18481849
SELECT b c, (SELECT a FROM t1 WHERE b = c)
18491850
FROM t1;
1850-
ERROR 42S22: Unknown column 'c' in 'where clause'
1851+
ERROR 21000: Subquery returns more than 1 row
18511852
DROP TABLE t1;
18521853
SET @@sql_mode = @old_sql_mode;
18531854
Warnings:

mysql-test/r/order_by_all.result

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,9 @@ num
932932
SELECT a + 1 AS num, num + 1 FROM t1;
933933
ERROR 42S22: Unknown column 'num' in 'field list'
934934
SELECT a + 1 AS num, (select num + 2 FROM t1 LIMIT 1) FROM t1;
935-
ERROR 42S22: Unknown column 'num' in 'field list'
935+
num (select num + 2 FROM t1 LIMIT 1)
936+
2 4
937+
3 5
936938
SELECT a.a + 1 AS num FROM t1 a JOIN t1 b ON num = b.a;
937939
ERROR 42S22: Unknown column 'num' in 'on clause'
938940
DROP TABLE t1;

mysql-test/r/order_by_icp_mrr.result

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,9 @@ num
932932
SELECT a + 1 AS num, num + 1 FROM t1;
933933
ERROR 42S22: Unknown column 'num' in 'field list'
934934
SELECT a + 1 AS num, (select num + 2 FROM t1 LIMIT 1) FROM t1;
935-
ERROR 42S22: Unknown column 'num' in 'field list'
935+
num (select num + 2 FROM t1 LIMIT 1)
936+
2 4
937+
3 5
936938
SELECT a.a + 1 AS num FROM t1 a JOIN t1 b ON num = b.a;
937939
ERROR 42S22: Unknown column 'num' in 'on clause'
938940
DROP TABLE t1;

mysql-test/r/order_by_none.result

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,9 @@ num
931931
SELECT a + 1 AS num, num + 1 FROM t1;
932932
ERROR 42S22: Unknown column 'num' in 'field list'
933933
SELECT a + 1 AS num, (select num + 2 FROM t1 LIMIT 1) FROM t1;
934-
ERROR 42S22: Unknown column 'num' in 'field list'
934+
num (select num + 2 FROM t1 LIMIT 1)
935+
2 4
936+
3 5
935937
SELECT a.a + 1 AS num FROM t1 a JOIN t1 b ON num = b.a;
936938
ERROR 42S22: Unknown column 'num' in 'on clause'
937939
DROP TABLE t1;

mysql-test/r/subquery_all.result

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ Warning 1681 'EXTENDED' is deprecated and will be removed in a future release.
4343
Note 1249 Select 2 was reduced during optimization
4444
Note 1003 /* select#1 */ select (/* select#3 */ select 0 union /* select#4 */ select 0) AS `(SELECT (SELECT 0 UNION SELECT 0))`
4545
SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a;
46-
ERROR 42S22: Unknown column 'a' in 'having clause'
46+
ERROR 42S22: Reference 'a' not supported (forward reference in item list)
4747
SELECT (SELECT 1 FROM (SELECT 1) as b HAVING b=1) as a,(SELECT 1 FROM (SELECT 1) as c HAVING a=1) as b;
48-
ERROR 42S22: Unknown column 'b' in 'having clause'
48+
ERROR 42S22: Reference 'b' not supported (forward reference in item list)
4949
SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a;
5050
(SELECT 1) MAX(1)
5151
1 1
5252
SELECT (SELECT a) as a;
53-
ERROR 42S22: Unknown column 'a' in 'field list'
53+
ERROR 42S22: Reference 'a' not supported (forward reference in item list)
5454
EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
5555
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
5656
1 PRIMARY <derived2> NULL system NULL NULL NULL NULL 1 100.00 NULL
@@ -143,7 +143,8 @@ SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
143143
SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a);
144144
ERROR 21000: Operand should contain 1 column(s)
145145
SELECT 1 as a,(SELECT a+a) b,(SELECT b);
146-
ERROR 42S22: Unknown column 'a' in 'field list'
146+
a b (SELECT b)
147+
1 2 2
147148
create table t1 (a int);
148149
create table t2 (a int, b int);
149150
create table t3 (a int);
@@ -152,7 +153,7 @@ insert into t1 values (2);
152153
insert into t2 values (1,7),(2,7);
153154
insert into t4 values (4,8),(3,8),(5,9);
154155
select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1;
155-
ERROR 42S22: Unknown column 'a1' in 'where clause'
156+
ERROR 42S22: Reference 'a1' not supported (forward reference in item list)
156157
select (select a from t1 where t1.a=t2.a), a from t2;
157158
(select a from t1 where t1.a=t2.a) a
158159
NULL 1
@@ -546,7 +547,7 @@ UNIQUE KEY `numreponse` (`numreponse`),
546547
KEY `pseudo` (`pseudo`,`numeropost`)
547548
) ENGINE=MyISAM;
548549
SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a;
549-
ERROR 42S22: Unknown column 'a' in 'having clause'
550+
ERROR 42S22: Reference 'numreponse' not supported (forward reference in item list)
550551
SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a;
551552
ERROR 42S22: Unknown column 'a' in 'having clause'
552553
SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a;
@@ -1094,7 +1095,8 @@ mot topic date pseudo
10941095
joce 1 0000-00-00 joce
10951096
drop table t1, t2, t3;
10961097
SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
1097-
ERROR 42S22: Unknown column 'a' in 'field list'
1098+
a (SELECT a)
1099+
1 1
10981100
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;
10991101
SHOW CREATE TABLE t1;
11001102
Table Create Table
@@ -1104,9 +1106,21 @@ t1 CREATE TABLE `t1` (
11041106
) ENGINE=MyISAM DEFAULT CHARSET=latin1
11051107
drop table t1;
11061108
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
1107-
ERROR 42S22: Unknown column 'a' in 'field list'
1109+
SHOW CREATE TABLE t1;
1110+
Table Create Table
1111+
t1 CREATE TABLE `t1` (
1112+
`a` int(1) NOT NULL DEFAULT '0',
1113+
`(SELECT a)` int(1) NOT NULL DEFAULT '0'
1114+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1115+
DROP TABLE t1;
11081116
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
1109-
ERROR 42S22: Unknown column 'a' in 'field list'
1117+
SHOW CREATE TABLE t1;
1118+
Table Create Table
1119+
t1 CREATE TABLE `t1` (
1120+
`a` int(1) NOT NULL DEFAULT '0',
1121+
`(SELECT a+0)` int(3) NOT NULL DEFAULT '0'
1122+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1123+
DROP TABLE t1;
11101124
CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
11111125
select * from t1;
11121126
a
@@ -2492,15 +2506,16 @@ drop table t1;
24922506
create table t1 (a integer);
24932507
insert into t1 values (1);
24942508
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ;
2495-
ERROR 42S22: Unknown column 'xx' in 'where clause'
2509+
ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
24962510
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
2497-
ERROR 42S22: Unknown column 'xx' in 'where clause'
2511+
ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
24982512
select 1 as xx, 1 = ALL ( select 1 from t1 where 1 = xx );
2499-
ERROR 42S22: Unknown column 'xx' in 'where clause'
2513+
xx 1 = ALL ( select 1 from t1 where 1 = xx )
2514+
1 1
25002515
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
2501-
ERROR 42S22: Unknown column 'xx' in 'where clause'
2516+
ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
25022517
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL;
2503-
ERROR 42S22: Unknown column 'xx' in 'where clause'
2518+
ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
25042519
drop table t1;
25052520
CREATE TABLE t1 (
25062521
categoryId int(11) NOT NULL,
@@ -5113,6 +5128,16 @@ a ROW(11, 12) = (SELECT a, 12) ROW(11, 12) IN (SELECT a, 12)
51135128
1 0 0
51145129
2 0 0
51155130
11 1 1
5131+
SELECT a AS x, ROW(11, 12) = (SELECT MAX(x), 22), ROW(11, 12) IN (SELECT MAX(x), 22) FROM t1;
5132+
x ROW(11, 12) = (SELECT MAX(x), 22) ROW(11, 12) IN (SELECT MAX(x), 22)
5133+
1 0 0
5134+
2 0 0
5135+
11 0 0
5136+
SELECT a AS x, ROW(11, 12) = (SELECT MAX(x), 12), ROW(11, 12) IN (SELECT MAX(x), 12) FROM t1;
5137+
x ROW(11, 12) = (SELECT MAX(x), 12) ROW(11, 12) IN (SELECT MAX(x), 12)
5138+
1 0 0
5139+
2 0 0
5140+
11 1 1
51165141
DROP TABLE t1;
51175142
# both columns should be same
51185143
SELECT ROW(1,2) = (SELECT NULL, NULL), ROW(1,2) IN (SELECT NULL, NULL);

0 commit comments

Comments
 (0)