Commit e61b4454 authored by timour@mysql.com's avatar timour@mysql.com

Merge of WL#1972 "Evaluate HAVING before SELECT select-list"

parents f65e0547 9a4ffb5a
drop table if exists t1,t2; drop table if exists t1,t2,t3;
create table t1 (a int); create table t1 (a int);
select count(a) as b from t1 where a=0 having b > 0; select count(a) as b from t1 where a=0 having b > 0;
b b
...@@ -128,3 +128,203 @@ id description c ...@@ -128,3 +128,203 @@ id description c
1 test 0 1 test 0
2 test2 0 2 test2 0
drop table t1,t2,t3; drop table t1,t2,t3;
create table t1 (col1 int, col2 varchar(5), col_t1 int);
create table t2 (col1 int, col2 varchar(5), col_t2 int);
create table t3 (col1 int, col2 varchar(5), col_t3 int);
insert into t1 values(10,'hello',10);
insert into t1 values(20,'hello',20);
insert into t1 values(30,'hello',30);
insert into t1 values(10,'bye',10);
insert into t1 values(10,'sam',10);
insert into t1 values(10,'bob',10);
insert into t2 select * from t1;
insert into t3 select * from t1;
select count(*) from t1 group by col1 having col1 = 10;
count(*)
4
select count(*) as count_col1 from t1 group by col1 having col1 = 10;
count_col1
4
select count(*) as count_col1 from t1 as tmp1 group by col1 having col1 = 10;
count_col1
4
select count(*) from t1 group by col2 having col2 = 'hello';
count(*)
3
select count(*) from t1 group by col2 having col1 = 10;
ERROR 42S22: Unknown column 'col1' in 'having clause'
select col1 as count_col1 from t1 as tmp1 group by col1 having col1 = 10;
count_col1
10
select col1 as count_col1 from t1 as tmp1 group by col1 having count_col1 = 10;
count_col1
10
select col1 as count_col1 from t1 as tmp1 group by count_col1 having col1 = 10;
count_col1
10
select col1 as count_col1 from t1 as tmp1 group by count_col1 having count_col1 = 10;
count_col1
10
select col1 as count_col1,col2 from t1 as tmp1 group by col1,col2 having col1 = 10;
count_col1 col2
10 bob
10 bye
10 hello
10 sam
select col1 as count_col1,col2 from t1 as tmp1 group by col1,col2 having count_col1 = 10;
count_col1 col2
10 bob
10 bye
10 hello
10 sam
select col1 as count_col1,col2 from t1 as tmp1 group by col1,col2 having col2 = 'hello';
count_col1 col2
10 hello
20 hello
30 hello
select col1 as count_col1,col2 as group_col2 from t1 as tmp1 group by col1,col2 having group_col2 = 'hello';
count_col1 group_col2
10 hello
20 hello
30 hello
select sum(col1) as co12 from t1 group by col2 having col2 10;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '10' at line 1
select sum(col1) as co2, count(col2) as cc from t1 group by col1 having col1 =10;
co2 cc
40 4
select t2.col2 from t2 group by t2.col1, t2.col2 having t1.col1 <= 10;
ERROR 42S22: Unknown column 't1.col1' in 'having clause'
select t1.col1 from t1
where t1.col2 in
(select t2.col2 from t2
group by t2.col1, t2.col2 having t2.col1 <= 10);
col1
10
20
30
10
10
10
select t1.col1 from t1
where t1.col2 in
(select t2.col2 from t2
group by t2.col1, t2.col2
having t2.col1 <=
(select min(t3.col1) from t3));
col1
10
20
30
10
10
10
select t1.col1 from t1
where t1.col2 in
(select t2.col2 from t2
group by t2.col1, t2.col2 having t1.col1 <= 10);
col1
10
10
10
10
select t1.col1 as tmp_col from t1
where t1.col2 in
(select t2.col2 from t2
group by t2.col1, t2.col2 having tmp_col <= 10);
tmp_col
10
10
10
10
select t1.col1 from t1
where t1.col2 in
(select t2.col2 from t2
group by t2.col1, t2.col2 having col_t1 <= 10);
col1
10
10
10
10
select sum(col1) from t1
group by col_t1
having (select col_t1 from t2 where col_t1 = col_t2 order by col_t2 limit 1);
sum(col1)
40
20
30
select t1.col1 from t1
where t1.col2 in
(select t2.col2 from t2
group by t2.col1, t2.col2 having col_t1 <= 10)
having col_t1 <= 20;
ERROR 42S22: Unknown column 'col_t1' in 'having clause'
select t1.col1 from t1
where t1.col2 in
(select t2.col2 from t2
group by t2.col1, t2.col2 having col_t1 <= 10)
group by col_t1
having col_t1 <= 20;
col1
10
select col_t1, sum(col1) from t1
group by col_t1
having col_t1 > 10 and
exists (select sum(t2.col1) from t2
group by t2.col2 having t2.col2 > 'b');
col_t1 sum(col1)
20 20
30 30
select sum(col1) from t1
group by col_t1
having col_t1 in (select sum(t2.col1) from t2
group by t2.col2, t2.col1 having t2.col1 = t1.col1);
ERROR 42S22: Unknown column 't1.col1' in 'having clause'
select sum(col1) from t1
group by col_t1
having col_t1 in (select sum(t2.col1) from t2
group by t2.col2, t2.col1 having t2.col1 = col_t1);
sum(col1)
40
20
30
select t1.col1, t2.col1 from t1, t2 where t1.col1 = t2.col1
group by t1.col1, t2.col1 having col1 = 2;
ERROR 23000: Column 'col1' in having clause is ambiguous
select t1.col1*10+t2.col1 from t1,t2 where t1.col1=t2.col1
group by t1.col1, t2.col1 having col1 = 2;
ERROR 23000: Column 'col1' in having clause is ambiguous
drop table t1, t2, t3;
create table t1 (s1 int);
insert into t1 values (1),(2),(3);
select count(*) from t1 group by s1 having s1 is null;
count(*)
select s1*0 as s1 from t1 group by s1 having s1 <> 0;
s1
0
0
0
Warnings:
Warning 1052 Column 's1' in having clause is ambiguous
select s1*0 from t1 group by s1 having s1 = 0;
s1*0
select s1 from t1 group by 1 having 1 = 0;
s1
select count(s1) from t1 group by s1 having count(1+1)=2;
count(s1)
select count(s1) from t1 group by s1 having s1*0=0;
count(s1)
1
1
1
select * from t1 a, t1 b group by a.s1 having s1 is null;
ERROR 23000: Column 's1' in having clause is ambiguous
drop table t1;
create table t1 (s1 char character set latin1 collate latin1_german1_ci);
insert into t1 values ('ü'),('y');
Warnings:
Warning 1265 Data truncated for column 's1' at row 1
select s1,count(s1) from t1
group by s1 collate latin1_swedish_ci having s1 = 'y';
s1 count(s1)
y 1
drop table t1;
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# #
--disable_warnings --disable_warnings
drop table if exists t1,t2; drop table if exists t1,t2,t3;
--enable_warnings --enable_warnings
create table t1 (a int); create table t1 (a int);
...@@ -122,3 +122,196 @@ from t1 a left join t3 b on a.id=b.order_id ...@@ -122,3 +122,196 @@ from t1 a left join t3 b on a.id=b.order_id
group by a.id, a.description group by a.id, a.description
having (a.description is not null) and (c=0); having (a.description is not null) and (c=0);
drop table t1,t2,t3; drop table t1,t2,t3;
#
# Tests for WL#1972 CORRECT EVALUATION OF COLUMN REFERENCES IN THE HAVING CLAUSE
# Per the SAP VERI tests and WL#1972, MySQL must ensure that HAVING can
# correctly evaluate column references from the GROUP BY clause, even if the
# same references are not also found in the select list.
#
# set global sql_mode='ansi';
# set session sql_mode='ansi';
create table t1 (col1 int, col2 varchar(5), col_t1 int);
create table t2 (col1 int, col2 varchar(5), col_t2 int);
create table t3 (col1 int, col2 varchar(5), col_t3 int);
insert into t1 values(10,'hello',10);
insert into t1 values(20,'hello',20);
insert into t1 values(30,'hello',30);
insert into t1 values(10,'bye',10);
insert into t1 values(10,'sam',10);
insert into t1 values(10,'bob',10);
insert into t2 select * from t1;
insert into t3 select * from t1;
select count(*) from t1 group by col1 having col1 = 10;
select count(*) as count_col1 from t1 group by col1 having col1 = 10;
select count(*) as count_col1 from t1 as tmp1 group by col1 having col1 = 10;
select count(*) from t1 group by col2 having col2 = 'hello';
--error 1054
select count(*) from t1 group by col2 having col1 = 10;
select col1 as count_col1 from t1 as tmp1 group by col1 having col1 = 10;
select col1 as count_col1 from t1 as tmp1 group by col1 having count_col1 = 10;
select col1 as count_col1 from t1 as tmp1 group by count_col1 having col1 = 10;
# ANSI: should return SQLSTATE 42000 Syntax error or access violation
# MySQL: returns 10 - because of GROUP BY name resolution
select col1 as count_col1 from t1 as tmp1 group by count_col1 having count_col1 = 10;
# ANSI: should return SQLSTATE 42000 Syntax error or access violation
# MySQL: returns 10 - because of GROUP BY name resolution
select col1 as count_col1,col2 from t1 as tmp1 group by col1,col2 having col1 = 10;
select col1 as count_col1,col2 from t1 as tmp1 group by col1,col2 having count_col1 = 10;
select col1 as count_col1,col2 from t1 as tmp1 group by col1,col2 having col2 = 'hello';
select col1 as count_col1,col2 as group_col2 from t1 as tmp1 group by col1,col2 having group_col2 = 'hello';
--error 1064
select sum(col1) as co12 from t1 group by col2 having col2 10;
select sum(col1) as co2, count(col2) as cc from t1 group by col1 having col1 =10;
--error 1054
select t2.col2 from t2 group by t2.col1, t2.col2 having t1.col1 <= 10;
#
# queries with nested sub-queries
#
# the having column is resolved in the same query
select t1.col1 from t1
where t1.col2 in
(select t2.col2 from t2
group by t2.col1, t2.col2 having t2.col1 <= 10);
select t1.col1 from t1
where t1.col2 in
(select t2.col2 from t2
group by t2.col1, t2.col2
having t2.col1 <=
(select min(t3.col1) from t3));
# the having column is resolved in the SELECT clause of the outer query -
# works in ANSI
select t1.col1 from t1
where t1.col2 in
(select t2.col2 from t2
group by t2.col1, t2.col2 having t1.col1 <= 10);
# the having column is resolved in the SELECT clause of the outer query -
# error in ANSI, works with MySQL extension
select t1.col1 as tmp_col from t1
where t1.col2 in
(select t2.col2 from t2
group by t2.col1, t2.col2 having tmp_col <= 10);
# the having column is resolved in the FROM clause of the outer query -
# works in ANSI
select t1.col1 from t1
where t1.col2 in
(select t2.col2 from t2
group by t2.col1, t2.col2 having col_t1 <= 10);
# Item_field must be resolved in the same way as Item_ref
select sum(col1) from t1
group by col_t1
having (select col_t1 from t2 where col_t1 = col_t2 order by col_t2 limit 1);
# nested queries with HAVING, inner having column resolved in outer FROM clause
# the outer having column is not referenced in GROUP BY which results in an error
--error 1054
select t1.col1 from t1
where t1.col2 in
(select t2.col2 from t2
group by t2.col1, t2.col2 having col_t1 <= 10)
having col_t1 <= 20;
# both having columns are resolved in the GROUP clause of the outer query
select t1.col1 from t1
where t1.col2 in
(select t2.col2 from t2
group by t2.col1, t2.col2 having col_t1 <= 10)
group by col_t1
having col_t1 <= 20;
#
# nested HAVING clauses
#
# non-correlated subqueries
select col_t1, sum(col1) from t1
group by col_t1
having col_t1 > 10 and
exists (select sum(t2.col1) from t2
group by t2.col2 having t2.col2 > 'b');
# correlated subqueries - inner having column 't1.col2' resolves to
# the outer FROM clause, which cannot be used because the outer query
# is grouped
--error 1054
select sum(col1) from t1
group by col_t1
having col_t1 in (select sum(t2.col1) from t2
group by t2.col2, t2.col1 having t2.col1 = t1.col1);
# correlated subqueries - inner having column 'col_t1' resolves to
# the outer GROUP clause
select sum(col1) from t1
group by col_t1
having col_t1 in (select sum(t2.col1) from t2
group by t2.col2, t2.col1 having t2.col1 = col_t1);
#
# queries with joins and ambiguous column names
#
--error 1052
select t1.col1, t2.col1 from t1, t2 where t1.col1 = t2.col1
group by t1.col1, t2.col1 having col1 = 2;
--error 1052
select t1.col1*10+t2.col1 from t1,t2 where t1.col1=t2.col1
group by t1.col1, t2.col1 having col1 = 2;
drop table t1, t2, t3;
# More queries to test ANSI compatibility
create table t1 (s1 int);
insert into t1 values (1),(2),(3);
select count(*) from t1 group by s1 having s1 is null;
select s1*0 as s1 from t1 group by s1 having s1 <> 0;
# ANSI requires: 3 rows
# MySQL returns: 0 rows - because of GROUP BY name resolution
select s1*0 from t1 group by s1 having s1 = 0;
select s1 from t1 group by 1 having 1 = 0;
select count(s1) from t1 group by s1 having count(1+1)=2;
# ANSI requires: 3 rows
# MySQL returns: 0 rows - because of GROUP BY name resolution
select count(s1) from t1 group by s1 having s1*0=0;
-- error 1052
select * from t1 a, t1 b group by a.s1 having s1 is null;
# ANSI requires: 0 rows
# MySQL returns:
# "ERROR 1052 (23000): Column 's1' in having clause is ambiguous"
# I think the column is ambiguous in ANSI too.
# It is the same as:
# select a.s1, b.s1 from t1 a, t1 b group by a.s1 having s1 is null;
# currently we first check SELECT, thus s1 is ambiguous.
drop table t1;
create table t1 (s1 char character set latin1 collate latin1_german1_ci);
insert into t1 values ('ü'),('y');
select s1,count(s1) from t1
group by s1 collate latin1_swedish_ci having s1 = 'y';
# ANSI requires: 1 row, with count(s1) = 2
# MySQL returns: 1 row, with count(s1) = 1
drop table t1;
This diff is collapsed.
...@@ -651,8 +651,13 @@ void execute_init_command(THD *thd, sys_var_str *init_command_var, ...@@ -651,8 +651,13 @@ void execute_init_command(THD *thd, sys_var_str *init_command_var,
rw_lock_t *var_mutex); rw_lock_t *var_mutex);
extern const Field *not_found_field; extern const Field *not_found_field;
extern const Field *view_ref_found; extern const Field *view_ref_found;
enum find_item_error_report_type {REPORT_ALL_ERRORS, REPORT_EXCEPT_NOT_FOUND,
IGNORE_ERRORS, REPORT_EXCEPT_NON_UNIQUE,
IGNORE_EXCEPT_NON_UNIQUE};
Field *find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables, Field *find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables,
Item **ref, bool report_error, Item **ref,
find_item_error_report_type report_error,
bool check_privileges); bool check_privileges);
Field * Field *
find_field_in_table(THD *thd, TABLE_LIST *table_list, find_field_in_table(THD *thd, TABLE_LIST *table_list,
...@@ -771,8 +776,6 @@ TABLE *unlink_open_table(THD *thd,TABLE *list,TABLE *find); ...@@ -771,8 +776,6 @@ TABLE *unlink_open_table(THD *thd,TABLE *list,TABLE *find);
SQL_SELECT *make_select(TABLE *head, table_map const_tables, SQL_SELECT *make_select(TABLE *head, table_map const_tables,
table_map read_tables, COND *conds, int *error, table_map read_tables, COND *conds, int *error,
bool allow_null_cond= false); bool allow_null_cond= false);
enum find_item_error_report_type {REPORT_ALL_ERRORS, REPORT_EXCEPT_NOT_FOUND,
IGNORE_ERRORS};
extern const Item **not_found_item; extern const Item **not_found_item;
Item ** find_item_in_list(Item *item, List<Item> &items, uint *counter, Item ** find_item_in_list(Item *item, List<Item> &items, uint *counter,
find_item_error_report_type report_error, find_item_error_report_type report_error,
......
...@@ -615,7 +615,7 @@ db_show_routine_status(THD *thd, int type, const char *wild) ...@@ -615,7 +615,7 @@ db_show_routine_status(THD *thd, int type, const char *wild)
Item_field *field= new Item_field("mysql", "proc", Item_field *field= new Item_field("mysql", "proc",
used_field->field_name); used_field->field_name);
if (!(used_field->field= find_field_in_tables(thd, field, &tables, if (!(used_field->field= find_field_in_tables(thd, field, &tables,
0, TRUE, 1))) 0, REPORT_ALL_ERRORS, 1)))
{ {
res= SP_INTERNAL_ERROR; res= SP_INTERNAL_ERROR;
goto err_case1; goto err_case1;
......
...@@ -2225,25 +2225,32 @@ Field *find_field_in_real_table(THD *thd, TABLE *table, ...@@ -2225,25 +2225,32 @@ Field *find_field_in_real_table(THD *thd, TABLE *table,
find_field_in_tables() find_field_in_tables()
thd Pointer to current thread structure thd Pointer to current thread structure
item Field item that should be found item Field item that should be found
tables Tables for scanning tables Tables to be searched for item
ref if view field is found, pointer to view item will ref If 'item' is resolved to a view field, ref is set to
be returned via this parameter point to the found view field
report_error If FALSE then do not report error if item not found report_error Degree of error reporting:
and return not_found_field - IGNORE_ERRORS then do not report any error
- IGNORE_EXCEPT_NON_UNIQUE report only non-unique
fields, suppress all other errors
- REPORT_EXCEPT_NON_UNIQUE report all other errors
except when non-unique fields were found
- REPORT_ALL_ERRORS
check_privileges need to check privileges check_privileges need to check privileges
RETURN VALUES RETURN VALUES
0 Field is not found or field is not unique- error 0 If error: the found field is not unique, or there are
message is reported no sufficient access priviliges for the found field,
not_found_field Function was called with report_error == FALSE and or the field is qualified with non-existing table.
field was not found. no error message reported. not_found_field The function was called with report_error ==
view_ref_found view field is found, item passed through ref parameter (IGNORE_ERRORS || IGNORE_EXCEPT_NON_UNIQUE) and a
found field field was not found.
view_ref_found View field is found, item passed through ref parameter
found field If a item was resolved to some field
*/ */
Field * Field *
find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables, find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables,
Item **ref, bool report_error, Item **ref, find_item_error_report_type report_error,
bool check_privileges) bool check_privileges)
{ {
Field *found=0; Field *found=0;
...@@ -2261,8 +2268,8 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables, ...@@ -2261,8 +2268,8 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables,
TABLE_LIST *tables is not changed during query execution (which TABLE_LIST *tables is not changed during query execution (which
is true for all queries except RENAME but luckily RENAME doesn't is true for all queries except RENAME but luckily RENAME doesn't
use fields...) so we can rely on reusing pointer to its member. use fields...) so we can rely on reusing pointer to its member.
With this optimisation we also miss case when addition of one more With this optimization we also miss case when addition of one more
field makes some prepared query ambiguous and so erronous, but we field makes some prepared query ambiguous and so erroneous, but we
accept this trade off. accept this trade off.
*/ */
found= find_field_in_real_table(thd, item->cached_table->table, found= find_field_in_real_table(thd, item->cached_table->table,
...@@ -2283,7 +2290,7 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables, ...@@ -2283,7 +2290,7 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables,
if (db && lower_case_table_names) if (db && lower_case_table_names)
{ {
/* /*
convert database to lower case for comparision. convert database to lower case for comparison.
We can't do this in Item_field as this would change the We can't do this in Item_field as this would change the
'name' of the item which may be used in the select list 'name' of the item which may be used in the select list
*/ */
...@@ -2320,8 +2327,10 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables, ...@@ -2320,8 +2327,10 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables,
return find; return find;
if (found) if (found)
{ {
my_printf_error(ER_NON_UNIQ_ERROR,ER(ER_NON_UNIQ_ERROR),MYF(0), if (report_error == REPORT_ALL_ERRORS ||
item->full_name(),thd->where); report_error == IGNORE_EXCEPT_NON_UNIQUE)
my_printf_error(ER_NON_UNIQ_ERROR,ER(ER_NON_UNIQ_ERROR),MYF(0),
item->full_name(),thd->where);
return (Field*) 0; return (Field*) 0;
} }
found=find; found=find;
...@@ -2330,7 +2339,8 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables, ...@@ -2330,7 +2339,8 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables,
} }
if (found) if (found)
return found; return found;
if (!found_table && report_error) if (!found_table && (report_error == REPORT_ALL_ERRORS ||
report_error == REPORT_EXCEPT_NON_UNIQUE))
{ {
char buff[NAME_LEN*2+1]; char buff[NAME_LEN*2+1];
if (db && db[0]) if (db && db[0])
...@@ -2338,28 +2348,30 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables, ...@@ -2338,28 +2348,30 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables,
strxnmov(buff,sizeof(buff)-1,db,".",table_name,NullS); strxnmov(buff,sizeof(buff)-1,db,".",table_name,NullS);
table_name=buff; table_name=buff;
} }
if (report_error) if (report_error == REPORT_ALL_ERRORS ||
{ report_error == REPORT_EXCEPT_NON_UNIQUE)
my_printf_error(ER_UNKNOWN_TABLE, ER(ER_UNKNOWN_TABLE), MYF(0), my_printf_error(ER_UNKNOWN_TABLE, ER(ER_UNKNOWN_TABLE), MYF(0),
table_name, thd->where); table_name, thd->where);
}
else else
return (Field*) not_found_field; return (Field*) not_found_field;
} }
else else
if (report_error) if (report_error == REPORT_ALL_ERRORS ||
report_error == REPORT_EXCEPT_NON_UNIQUE)
my_printf_error(ER_BAD_FIELD_ERROR,ER(ER_BAD_FIELD_ERROR),MYF(0), my_printf_error(ER_BAD_FIELD_ERROR,ER(ER_BAD_FIELD_ERROR),MYF(0),
item->full_name(),thd->where); item->full_name(),thd->where);
else else
return (Field*) not_found_field; return (Field*) not_found_field;
return (Field*) 0; return (Field*) 0;
} }
bool allow_rowid= tables && !tables->next_local; // Only one table bool allow_rowid= tables && !tables->next_local; // Only one table
for (; tables ; tables= tables->next_local) for (; tables ; tables= tables->next_local)
{ {
if (!tables->table) if (!tables->table)
{ {
if (report_error) if (report_error == REPORT_ALL_ERRORS ||
report_error == REPORT_EXCEPT_NON_UNIQUE)
my_printf_error(ER_BAD_FIELD_ERROR,ER(ER_BAD_FIELD_ERROR),MYF(0), my_printf_error(ER_BAD_FIELD_ERROR,ER(ER_BAD_FIELD_ERROR),MYF(0),
item->full_name(),thd->where); item->full_name(),thd->where);
return (Field*) not_found_field; return (Field*) not_found_field;
...@@ -2384,8 +2396,10 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables, ...@@ -2384,8 +2396,10 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables,
{ {
if (!thd->where) // Returns first found if (!thd->where) // Returns first found
break; break;
my_printf_error(ER_NON_UNIQ_ERROR,ER(ER_NON_UNIQ_ERROR),MYF(0), if (report_error == REPORT_ALL_ERRORS ||
name,thd->where); report_error == IGNORE_EXCEPT_NON_UNIQUE)
my_printf_error(ER_NON_UNIQ_ERROR,ER(ER_NON_UNIQ_ERROR),MYF(0),
name,thd->where);
return (Field*) 0; return (Field*) 0;
} }
found=field; found=field;
...@@ -2393,7 +2407,8 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables, ...@@ -2393,7 +2407,8 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables,
} }
if (found) if (found)
return found; return found;
if (report_error) if (report_error == REPORT_ALL_ERRORS ||
report_error == REPORT_EXCEPT_NON_UNIQUE)
my_printf_error(ER_BAD_FIELD_ERROR, ER(ER_BAD_FIELD_ERROR), my_printf_error(ER_BAD_FIELD_ERROR, ER(ER_BAD_FIELD_ERROR),
MYF(0), item->full_name(), thd->where); MYF(0), item->full_name(), thd->where);
else else
...@@ -2432,7 +2447,7 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables, ...@@ -2432,7 +2447,7 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables,
found field found field
*/ */
// Special Item pointer for find_item_in_list returning /* Special Item pointer to serve as a return value from find_item_in_list(). */
const Item **not_found_item= (const Item**) 0x1; const Item **not_found_item= (const Item**) 0x1;
...@@ -2830,7 +2845,8 @@ bool get_key_map_from_key_list(key_map *map, TABLE *table, ...@@ -2830,7 +2845,8 @@ bool get_key_map_from_key_list(key_map *map, TABLE *table,
any_privileges 0 If we should ensure that we have SELECT privileges any_privileges 0 If we should ensure that we have SELECT privileges
for all columns for all columns
1 If any privilege is ok 1 If any privilege is ok
allocate_view_names if true view names will be copied to current Item_arena memory (made for SP/PS) allocate_view_names if true view names will be copied to current Item_arena
memory (made for SP/PS)
RETURN RETURN
0 ok 0 ok
'it' is updated to point at last inserted 'it' is updated to point at last inserted
......
...@@ -88,7 +88,7 @@ static bool init_fields(THD *thd, TABLE_LIST *tables, ...@@ -88,7 +88,7 @@ static bool init_fields(THD *thd, TABLE_LIST *tables,
Item_field *field= new Item_field("mysql", find_fields->table_name, Item_field *field= new Item_field("mysql", find_fields->table_name,
find_fields->field_name); find_fields->field_name);
if (!(find_fields->field= find_field_in_tables(thd, field, tables, if (!(find_fields->field= find_field_in_tables(thd, field, tables,
0, TRUE, 1))) 0, REPORT_ALL_ERRORS, 1)))
DBUG_RETURN(1); DBUG_RETURN(1);
} }
DBUG_RETURN(0); DBUG_RETURN(0);
......
...@@ -11168,24 +11168,53 @@ cp_buffer_from_ref(TABLE_REF *ref) ...@@ -11168,24 +11168,53 @@ cp_buffer_from_ref(TABLE_REF *ref)
*****************************************************************************/ *****************************************************************************/
/* /*
Find order/group item in requested columns and change the item to point at Resolve an ORDER BY or GROUP BY column reference.
it. If item doesn't exists, add it first in the field list
Return 0 if ok. SYNOPSIS
find_order_in_list()
thd [in] Pointer to current thread structure
ref_pointer_array [in/out] All select, group and order by fields
tables [in] List of tables to search in (usually FROM clause)
order [in] Column reference to be resolved
fields [in] List of fields to search in (usually SELECT list)
all_fields [in/out] All select, group and order by fields
is_group_field [in] True if order is a GROUP field, false if
ORDER by field
DESCRIPTION
Given a column reference (represented by 'order') from a GROUP BY or ORDER
BY clause, find the actual column it represents. If the column being
resolved is from the GROUP BY clause, the procedure searches the SELECT
list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
the ORDER BY clause, only the SELECT list is being searched.
If 'order' is resolved to an Item, then order->item is set to the found
Item. If there is no item for the found column (that is, it was resolved
into a table field), order->item is 'fixed' and is added to all_fields and
ref_pointer_array.
RETURN
0 if OK
1 if error occurred
*/ */
static int static int
find_order_in_list(THD *thd, Item **ref_pointer_array, find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
TABLE_LIST *tables,ORDER *order, List<Item> &fields, ORDER *order, List<Item> &fields, List<Item> &all_fields,
List<Item> &all_fields) bool is_group_field)
{ {
Item *it= *order->item; Item *order_item= *order->item; /* The item from the GROUP/ORDER caluse. */
if (it->type() == Item::INT_ITEM) Item::Type order_item_type;
Item **select_item; /* The corresponding item from the SELECT clause. */
Field *from_field; /* The corresponding field from the FROM clause. */
if (order_item->type() == Item::INT_ITEM)
{ /* Order by position */ { /* Order by position */
uint count= (uint) it->val_int(); uint count= (uint) order_item->val_int();
if (!count || count > fields.elements) if (!count || count > fields.elements)
{ {
my_printf_error(ER_BAD_FIELD_ERROR,ER(ER_BAD_FIELD_ERROR), my_printf_error(ER_BAD_FIELD_ERROR,ER(ER_BAD_FIELD_ERROR),
MYF(0), it->full_name(), thd->where); MYF(0), order_item->full_name(), thd->where);
return 1; return 1;
} }
order->item= ref_pointer_array + count - 1; order->item= ref_pointer_array + count - 1;
...@@ -11194,38 +11223,68 @@ find_order_in_list(THD *thd, Item **ref_pointer_array, ...@@ -11194,38 +11223,68 @@ find_order_in_list(THD *thd, Item **ref_pointer_array,
order->counter_used= 1; order->counter_used= 1;
return 0; return 0;
} }
/* Lookup the current GROUP/ORDER field in the SELECT clause. */
uint counter; uint counter;
bool unaliased; bool unaliased;
Item **item= find_item_in_list(it, fields, &counter, select_item= find_item_in_list(order_item, fields, &counter,
REPORT_EXCEPT_NOT_FOUND, &unaliased); REPORT_EXCEPT_NOT_FOUND, &unaliased);
if (!item) if (!select_item)
return 1; return 1; /* Some error occured. */
if (item != (Item **)not_found_item)
/* Check whether the resolved field is not ambiguos. */
if (select_item != not_found_item)
{ {
/* /*
If we have found field not by its alias in select list but by its If we have found field not by its alias in select list but by its
original field name, we should additionaly check if we have conflict original field name, we should additionaly check if we have conflict
for this name (in case if we would perform lookup in all tables). for this name (in case if we would perform lookup in all tables).
*/ */
if (unaliased && !it->fixed && it->fix_fields(thd, tables, order->item)) if (unaliased && !order_item->fixed && order_item->fix_fields(thd, tables, order->item))
return 1; return 1;
order->item= ref_pointer_array + counter; /* Lookup the current GROUP field in the FROM clause. */
order->in_field_list=1; order_item_type= order_item->type();
return 0; if (is_group_field &&
order_item_type == Item::FIELD_ITEM || order_item_type == Item::REF_ITEM)
{
Item **view_ref= NULL;
from_field= find_field_in_tables(thd, (Item_ident*) order_item, tables,
view_ref, IGNORE_ERRORS, TRUE);
if(!from_field)
from_field= (Field*) not_found_field;
}
else
from_field= (Field*) not_found_field;
if (from_field == not_found_field ||
from_field && from_field != view_ref_found &&
(*select_item)->type() == Item::FIELD_ITEM &&
((Item_field*) (*select_item))->field->eq(from_field))
/*
If there is no such field in the FROM clause, or it is the same field as
the one found in the SELECT clause, then use the Item created for the
SELECT field. As a result if there was a derived field that 'shadowed'
a table field with the same name, the table field will be chosen over
the derived field.
*/
{
order->item= ref_pointer_array + counter;
order->in_field_list=1;
return 0;
}
} }
order->in_field_list=0; order->in_field_list=0;
/* /*
We check it->fixed because Item_func_group_concat can put We check order_item->fixed because Item_func_group_concat can put
arguments for which fix_fields already was called. arguments for which fix_fields already was called.
'it' reassigned in if condition because fix_field can change it. 'it' reassigned in if condition because fix_field can change it.
*/ */
if (!it->fixed && if (!order_item->fixed &&
(it->fix_fields(thd, tables, order->item) || (order_item->fix_fields(thd, tables, order->item) ||
(it= *order->item)->check_cols(1) || (order_item= *order->item)->check_cols(1) ||
thd->is_fatal_error)) thd->is_fatal_error))
return 1; // Wrong field return 1; // Wrong field
uint el= all_fields.elements; uint el= all_fields.elements;
...@@ -11235,6 +11294,7 @@ find_order_in_list(THD *thd, Item **ref_pointer_array, ...@@ -11235,6 +11294,7 @@ find_order_in_list(THD *thd, Item **ref_pointer_array,
return 0; return 0;
} }
/* /*
Change order to point at item in select list. If item isn't a number Change order to point at item in select list. If item isn't a number
and doesn't exits in the select list, add it the the field list. and doesn't exits in the select list, add it the the field list.
...@@ -11247,7 +11307,7 @@ int setup_order(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables, ...@@ -11247,7 +11307,7 @@ int setup_order(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
for (; order; order=order->next) for (; order; order=order->next)
{ {
if (find_order_in_list(thd, ref_pointer_array, tables, order, fields, if (find_order_in_list(thd, ref_pointer_array, tables, order, fields,
all_fields)) all_fields, FALSE))
return 1; return 1;
} }
return 0; return 0;
...@@ -11299,7 +11359,7 @@ setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables, ...@@ -11299,7 +11359,7 @@ setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
for (; order; order=order->next) for (; order; order=order->next)
{ {
if (find_order_in_list(thd, ref_pointer_array, tables, order, fields, if (find_order_in_list(thd, ref_pointer_array, tables, order, fields,
all_fields)) all_fields, TRUE))
return 1; return 1;
(*order->item)->marker=1; /* Mark found */ (*order->item)->marker=1; /* Mark found */
if ((*order->item)->with_sum_func) if ((*order->item)->with_sum_func)
......
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