more resonable errors about name resolving in subqueries (BUG#1483)

parent 8313e0da
...@@ -1349,7 +1349,7 @@ create table t2 (s1 int); ...@@ -1349,7 +1349,7 @@ create table t2 (s1 int);
insert into t1 values (1); insert into t1 values (1);
insert into t2 values (1); insert into t2 values (1);
update t1 set s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A); update t1 set s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A);
ERROR 42S02: Unknown table 'x' in field list ERROR 42S22: Unknown column 'x.s1' in 'field list'
DROP TABLE t1, t2; DROP TABLE t1, t2;
create table t1 (a int) type=innodb; create table t1 (a int) type=innodb;
create table t2 (a int) type=innodb; create table t2 (a int) type=innodb;
...@@ -1439,3 +1439,12 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -1439,3 +1439,12 @@ id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 3 Using where 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 Using where
2 SUBQUERY t2 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort 2 SUBQUERY t2 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort
drop table if exists t2, t3; drop table if exists t2, t3;
create table t1 (s1 int);
create table t2 (s1 int);
select * from t1 where (select count(*) from t2 where t1.s2) = 1;
ERROR 42S22: Unknown column 't1.s2' in 'where clause'
select * from t1 where (select count(*) from t2 group by t1.s2) = 1;
ERROR 42S22: Unknown column 't1.s2' in 'group statement'
select count(*) from t2 group by t1.s2;
ERROR 42S02: Unknown table 't1' in group statement
drop table t1, t2;
...@@ -693,7 +693,7 @@ INSERT INTO t1 VALUES (1); ...@@ -693,7 +693,7 @@ INSERT INTO t1 VALUES (1);
UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i)); UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i));
-- error 1111 -- error 1111
UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i)); UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
-- error 1109 -- error 1054
UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t); UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t);
drop table t1; drop table t1;
...@@ -887,7 +887,7 @@ create table t1 (s1 int); ...@@ -887,7 +887,7 @@ create table t1 (s1 int);
create table t2 (s1 int); create table t2 (s1 int);
insert into t1 values (1); insert into t1 values (1);
insert into t2 values (1); insert into t2 values (1);
-- error 1109 -- error 1054
update t1 set s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A); update t1 set s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A);
DROP TABLE t1, t2; DROP TABLE t1, t2;
...@@ -965,3 +965,16 @@ insert into t2 values (2,2), (2,1), (3,3), (3,1); ...@@ -965,3 +965,16 @@ insert into t2 values (2,2), (2,1), (3,3), (3,1);
select * from t3 where a > all (select max(b) from t2 group by a); select * from t3 where a > all (select max(b) from t2 group by a);
explain select * from t3 where a > all (select max(b) from t2 group by a); explain select * from t3 where a > all (select max(b) from t2 group by a);
drop table if exists t2, t3; drop table if exists t2, t3;
#
# unresolved field error
#
create table t1 (s1 int);
create table t2 (s1 int);
-- error 1054
select * from t1 where (select count(*) from t2 where t1.s2) = 1;
-- error 1054
select * from t1 where (select count(*) from t2 group by t1.s2) = 1;
-- error 1109
select count(*) from t2 group by t1.s2;
drop table t1, t2;
\ No newline at end of file
...@@ -794,6 +794,7 @@ bool Item_field::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref) ...@@ -794,6 +794,7 @@ bool Item_field::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref)
if (!field) // If field is not checked if (!field) // If field is not checked
{ {
TABLE_LIST *where= 0; TABLE_LIST *where= 0;
bool upward_lookup= 0;
Field *tmp= (Field *)not_found_field; Field *tmp= (Field *)not_found_field;
if ((tmp= find_field_in_tables(thd, this, tables, &where, 0)) == if ((tmp= find_field_in_tables(thd, this, tables, &where, 0)) ==
not_found_field) not_found_field)
...@@ -821,6 +822,7 @@ bool Item_field::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref) ...@@ -821,6 +822,7 @@ bool Item_field::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref)
sl; sl;
sl= sl->outer_select()) sl= sl->outer_select())
{ {
upward_lookup= 1;
table_list= (last= sl)->get_table_list(); table_list= (last= sl)->get_table_list();
if (sl->resolve_mode == SELECT_LEX::INSERT_MODE && table_list) if (sl->resolve_mode == SELECT_LEX::INSERT_MODE && table_list)
{ {
...@@ -846,8 +848,14 @@ bool Item_field::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref) ...@@ -846,8 +848,14 @@ bool Item_field::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref)
return 1; return 1;
else if (tmp == not_found_field && refer == (Item **)not_found_item) else if (tmp == not_found_field && refer == (Item **)not_found_item)
{ {
// call to return error code if (upward_lookup)
// We can't say exactly what absend table or field
my_printf_error(ER_BAD_FIELD_ERROR, ER(ER_BAD_FIELD_ERROR), MYF(0),
full_name(), thd->where);
else
// Call to report error
find_field_in_tables(thd, this, tables, &where, 1); find_field_in_tables(thd, this, tables, &where, 1);
return -1; return -1;
} }
else if (refer != (Item **)not_found_item) else if (refer != (Item **)not_found_item)
...@@ -1350,6 +1358,7 @@ bool Item_ref::fix_fields(THD *thd,TABLE_LIST *tables, Item **reference) ...@@ -1350,6 +1358,7 @@ bool Item_ref::fix_fields(THD *thd,TABLE_LIST *tables, Item **reference)
if (!ref) if (!ref)
{ {
TABLE_LIST *where= 0, *table_list; TABLE_LIST *where= 0, *table_list;
bool upward_lookup= 0;
SELECT_LEX *sl= thd->lex.current_select->outer_select(); SELECT_LEX *sl= thd->lex.current_select->outer_select();
/* /*
Finding only in current select will be performed for selects that have Finding only in current select will be performed for selects that have
...@@ -1367,6 +1376,7 @@ bool Item_ref::fix_fields(THD *thd,TABLE_LIST *tables, Item **reference) ...@@ -1367,6 +1376,7 @@ bool Item_ref::fix_fields(THD *thd,TABLE_LIST *tables, Item **reference)
REPORT_ALL_ERRORS))) == REPORT_ALL_ERRORS))) ==
(Item **)not_found_item) (Item **)not_found_item)
{ {
upward_lookup= 1;
Field *tmp= (Field*) not_found_field; Field *tmp= (Field*) not_found_field;
/* /*
We can't find table field in table list of current select, We can't find table field in table list of current select,
...@@ -1408,6 +1418,11 @@ bool Item_ref::fix_fields(THD *thd,TABLE_LIST *tables, Item **reference) ...@@ -1408,6 +1418,11 @@ bool Item_ref::fix_fields(THD *thd,TABLE_LIST *tables, Item **reference)
return -1; return -1;
else if (ref == (Item **)not_found_item && tmp == not_found_field) else if (ref == (Item **)not_found_item && tmp == not_found_field)
{ {
if (upward_lookup)
// We can't say exactly what absend (table or field)
my_printf_error(ER_BAD_FIELD_ERROR, ER(ER_BAD_FIELD_ERROR), MYF(0),
full_name(), thd->where);
else
// Call to report error // Call to report error
find_item_in_list(this, find_item_in_list(this,
*(thd->lex.current_select->get_item_list()), *(thd->lex.current_select->get_item_list()),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment