diff options
author | unknown <timour@mysql.com> | 2005-08-12 19:27:54 +0300 |
---|---|---|
committer | unknown <timour@mysql.com> | 2005-08-12 19:27:54 +0300 |
commit | 2b53b711d8c36fadf3ce123a678795876fb67be4 (patch) | |
tree | 47cfdff5ea22001b614037779ee931abbc209636 | |
parent | c9415c77b5ce586b4134b91a92447fe175c4e7b2 (diff) | |
parent | 7517d7e11298da9ce9aaea8e2e42c25a640d5be9 (diff) | |
download | mariadb-git-2b53b711d8c36fadf3ce123a678795876fb67be4.tar.gz |
Merge mysql.com:/home/timka/mysql/src/5.0-virgin
into mysql.com:/home/timka/mysql/src/5.0-2486-merge
mysql-test/r/errors.result:
Auto merged
mysql-test/r/fulltext.result:
Auto merged
mysql-test/r/fulltext_order_by.result:
Auto merged
mysql-test/r/innodb.result:
Auto merged
mysql-test/r/join_nested.result:
Auto merged
mysql-test/r/subselect.result:
Auto merged
mysql-test/t/errors.test:
Auto merged
mysql-test/t/fulltext.test:
Auto merged
mysql-test/t/fulltext_order_by.test:
Auto merged
mysql-test/t/innodb.test:
Auto merged
mysql-test/t/join_nested.test:
Auto merged
mysql-test/t/subselect.test:
Auto merged
sql/item.cc:
Auto merged
sql/mysql_priv.h:
Auto merged
sql/sp.cc:
Auto merged
sql/sql_base.cc:
Auto merged
sql/sql_class.h:
Auto merged
sql/sql_delete.cc:
Auto merged
sql/sql_lex.cc:
Auto merged
sql/sql_lex.h:
Auto merged
sql/sql_parse.cc:
Auto merged
sql/sql_select.cc:
Auto merged
sql/sql_update.cc:
Auto merged
sql/sql_yacc.yy:
Auto merged
sql/table.h:
Auto merged
tests/mysql_client_test.c:
Auto merged
sql/sql_insert.cc:
merge WL#2486
sql/sql_show.cc:
merge WL#2486
63 files changed, 3605 insertions, 1921 deletions
diff --git a/mysql-test/include/ps_query.inc b/mysql-test/include/ps_query.inc index 27a86f88231..97653c0b9f8 100644 --- a/mysql-test/include/ps_query.inc +++ b/mysql-test/include/ps_query.inc @@ -349,14 +349,14 @@ drop table if exists t2 ; --enable_warnings create table t2 as select * from t1 ; set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ; -set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ; -set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ; +set @query2= 'SELECT * FROM t2 natural join t1 order by a ' ; +set @query3= 'SELECT * FROM t2 join t1 using(a) order by a ' ; set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ; -set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ; -set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ; +set @query5= 'SELECT * FROM t2 natural left join t1 order by a ' ; +set @query6= 'SELECT * FROM t2 left join t1 using(a) order by a ' ; set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ; -set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ; -set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ; +set @query8= 'SELECT * FROM t2 natural right join t1 order by a ' ; +set @query9= 'SELECT * FROM t2 right join t1 using(a) order by a ' ; let $1= 9 ; while ($1) { diff --git a/mysql-test/r/bdb.result b/mysql-test/r/bdb.result index 876da3cb964..a0b68bfbc13 100644 --- a/mysql-test/r/bdb.result +++ b/mysql-test/r/bdb.result @@ -732,7 +732,7 @@ drop table t1; set @a:=now(); CREATE TABLE t1 (a int not null, b timestamp not null, primary key (a)) engine=bdb; insert into t1 (a) values(1),(2),(3); -select t1.a from t1 natural join t1 as t2 where t1.b >= @a order by t1.a; +select a from t1 natural join t1 as t2 where b >= @a order by a; a 1 2 @@ -906,7 +906,7 @@ create temporary table tmp1 select branch_id, target_id, platform_id, product_id from t1, t2, t3, t4 ; create temporary table tmp2 -select tmp1.branch_id, tmp1.target_id, tmp1.platform_id, tmp1.product_id +select branch_id, target_id, platform_id, product_id from tmp1 left join t8 using (branch_id,target_id,platform_id,product_id) where t8.archive_id is null ; diff --git a/mysql-test/r/derived.result b/mysql-test/r/derived.result index fd6a834c694..586be94301f 100644 --- a/mysql-test/r/derived.result +++ b/mysql-test/r/derived.result @@ -149,8 +149,8 @@ SELECT * FROM (SELECT (SELECT * FROM (SELECT 1 as a) as a )) as b; (SELECT * FROM (SELECT 1 as a) as a ) 1 select * from (select 1 as a) b left join (select 2 as a) c using(a); -a a -1 NULL +a +1 SELECT * FROM (SELECT 1 UNION SELECT a) b; ERROR 42S22: Unknown column 'a' in 'field list' SELECT 1 as a FROM (SELECT a UNION SELECT 1) b; diff --git a/mysql-test/r/errors.result b/mysql-test/r/errors.result index 9a91c088aca..0c84f24a2e4 100644 --- a/mysql-test/r/errors.result +++ b/mysql-test/r/errors.result @@ -9,9 +9,9 @@ create table t1 (a int); select count(test.t1.b) from t1; ERROR 42S22: Unknown column 'test.t1.b' in 'field list' select count(not_existing_database.t1) from t1; -ERROR 42S02: Unknown table 'not_existing_database' in field list +ERROR 42S22: Unknown column 'not_existing_database.t1' in 'field list' select count(not_existing_database.t1.a) from t1; -ERROR 42S02: Unknown table 'not_existing_database.t1' in field list +ERROR 42S22: Unknown column 'not_existing_database.t1.a' in 'field list' select count(not_existing_database.t1.a) from not_existing_database.t1; Got one of the listed errors select 1 from t1 order by 2; diff --git a/mysql-test/r/fulltext.result b/mysql-test/r/fulltext.result index 6a41035eb7b..ea0ffe9d7dd 100644 --- a/mysql-test/r/fulltext.result +++ b/mysql-test/r/fulltext.result @@ -221,14 +221,14 @@ select t1.id FROM t2 as ttxt,t1,t1 as ticket2 WHERE ticket2.id = ttxt.ticket AND t1.id = ticket2.ticket and match(ttxt.inhalt) against ('foobar'); id -select t1.id FROM t2 as ttxt,t1 INNER JOIN t1 as ticket2 ON -ticket2.id = ttxt.ticket -WHERE t1.id = ticket2.ticket and match(ttxt.inhalt) against ('foobar'); +select ticket2.id FROM t2 as ttxt,t2 INNER JOIN t1 as ticket2 ON +ticket2.id = t2.ticket +WHERE ticket2.id = ticket2.ticket and match(ttxt.inhalt) against ('foobar'); id INSERT INTO t1 VALUES (3,3); -select t1.id FROM t2 as ttxt,t1 -INNER JOIN t1 as ticket2 ON ticket2.id = ttxt.ticket -WHERE t1.id = ticket2.ticket and +select ticket2.id FROM t2 as ttxt,t2 +INNER JOIN t1 as ticket2 ON ticket2.id = t2.ticket +WHERE ticket2.id = ticket2.ticket and match(ttxt.inhalt) against ('foobar'); id 3 @@ -337,8 +337,8 @@ insert into t2 values (1, 1, 'xxfoo'); insert into t2 values (2, 1, 'xxbar'); insert into t2 values (3, 1, 'xxbuz'); select * from t1 join t2 using(`t1_id`) where match (t1.name, t2.name) against('xxfoo' in boolean mode); -t1_id name t2_id t1_id name -1 data1 1 1 xxfoo +t1_id name t2_id name +1 data1 1 xxfoo select * from t2 where match name against ('*a*b*c*d*e*f*' in boolean mode); t2_id t1_id name drop table t1,t2; diff --git a/mysql-test/r/fulltext_order_by.result b/mysql-test/r/fulltext_order_by.result index 2c421826bb4..bc466b5aba7 100644 --- a/mysql-test/r/fulltext_order_by.result +++ b/mysql-test/r/fulltext_order_by.result @@ -126,7 +126,7 @@ group by a.text, b.id, b.betreff order by match(b.betreff) against ('+abc' in boolean mode) desc; -ERROR 42S02: Unknown table 'b' in order clause +ERROR 42S22: Unknown column 'b.betreff' in 'order clause' select a.text, b.id, b.betreff from t2 a inner join t3 b on a.id = b.forum inner join @@ -142,7 +142,7 @@ where match(c.beitrag) against ('+abc' in boolean mode) order by match(b.betreff) against ('+abc' in boolean mode) desc; -ERROR 42S02: Unknown table 'b' in order clause +ERROR 42S22: Unknown column 'b.betreff' in 'order clause' select a.text, b.id, b.betreff from t2 a inner join t3 b on a.id = b.forum inner join diff --git a/mysql-test/r/innodb.result b/mysql-test/r/innodb.result index e0b477ab543..69620d5d527 100644 --- a/mysql-test/r/innodb.result +++ b/mysql-test/r/innodb.result @@ -750,7 +750,7 @@ drop table t1; set @a:=now(); CREATE TABLE t1 (a int not null, b timestamp not null, primary key (a)) engine=innodb; insert into t1 (a) values(1),(2),(3); -select t1.a from t1 natural join t1 as t2 where t1.b >= @a order by t1.a; +select a from t1 natural join t1 as t2 where b >= @a order by a; a 1 2 @@ -1377,7 +1377,7 @@ ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fail update t1,t2,t3 set t3.id=5, t2.id=6, t1.id=7 where t1.id =1 and t2.id = t1.id and t3.id = t2.id; ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails update t3 set t3.id=7 where t1.id =1 and t2.id = t1.id and t3.id = t2.id; -ERROR 42S02: Unknown table 't1' in where clause +ERROR 42S22: Unknown column 't1.id' in 'where clause' drop table t3,t2,t1; create table t1( id int primary key, diff --git a/mysql-test/r/insert_select.result b/mysql-test/r/insert_select.result index f745af182eb..17f65d96abc 100644 --- a/mysql-test/r/insert_select.result +++ b/mysql-test/r/insert_select.result @@ -664,7 +664,7 @@ insert into t1 select t2.a from t2 group by t2.a on duplicate key update a= a + insert into t1 select t2.a from t2 on duplicate key update a= a + t2.b; ERROR 23000: Column 'a' in field list is ambiguous insert into t1 select t2.a from t2 on duplicate key update t2.a= a + t2.b; -ERROR 42S02: Unknown table 't2' in field list +ERROR 42S22: Unknown column 't2.a' in 'field list' insert into t1 select t2.a from t2 group by t2.a on duplicate key update a= t1.a + t2.b; -ERROR 42S02: Unknown table 't2' in field list +ERROR 42S22: Unknown column 't2.b' in 'field list' drop table t1,t2,t3; diff --git a/mysql-test/r/join.result b/mysql-test/r/join.result index dc763472b0e..5ea863b4bdb 100644 --- a/mysql-test/r/join.result +++ b/mysql-test/r/join.result @@ -10,21 +10,21 @@ SELECT * FROM t1 INNER JOIN t2; S1 S1 1 2 SELECT * from t1 JOIN t2 USING (S1); -S1 S1 +S1 SELECT * FROM t1 INNER JOIN t2 USING (S1); -S1 S1 +S1 SELECT * from t1 CROSS JOIN t2; S1 S1 1 2 SELECT * from t1 LEFT JOIN t2 USING(S1); -S1 S1 -1 NULL +S1 +1 SELECT * from t1 LEFT JOIN t2 ON(t2.S1=2); S1 S1 1 2 SELECT * from t1 RIGHT JOIN t2 USING(S1); -S1 S1 -NULL 2 +S1 +2 SELECT * from t1 RIGHT JOIN t2 ON(t1.S1=1); S1 S1 1 2 @@ -121,11 +121,11 @@ id catid stateid countyid drop table t1, t2; create table t1 (a int primary key); insert into t1 values(1),(2); -select t1.a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a); +select a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a); a 1 2 -select t1.a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a) left join t1 as t32 using (a) left join t1 as t33 using (a) left join t1 as t34 using (a) left join t1 as t35 using (a) left join t1 as t36 using (a) left join t1 as t37 using (a) left join t1 as t38 using (a) left join t1 as t39 using (a) left join t1 as t40 using (a) left join t1 as t41 using (a) left join t1 as t42 using (a) left join t1 as t43 using (a) left join t1 as t44 using (a) left join t1 as t45 using (a) left join t1 as t46 using (a) left join t1 as t47 using (a) left join t1 as t48 using (a) left join t1 as t49 using (a) left join t1 as t50 using (a) left join t1 as t51 using (a) left join t1 as t52 using (a) left join t1 as t53 using (a) left join t1 as t54 using (a) left join t1 as t55 using (a) left join t1 as t56 using (a) left join t1 as t57 using (a) left join t1 as t58 using (a) left join t1 as t59 using (a) left join t1 as t60 using (a) left join t1 as t61 using (a) left join t1 as t62 using (a) left join t1 as t63 using (a) left join t1 as t64 using (a) left join t1 as t65 using (a); +select a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a) left join t1 as t32 using (a) left join t1 as t33 using (a) left join t1 as t34 using (a) left join t1 as t35 using (a) left join t1 as t36 using (a) left join t1 as t37 using (a) left join t1 as t38 using (a) left join t1 as t39 using (a) left join t1 as t40 using (a) left join t1 as t41 using (a) left join t1 as t42 using (a) left join t1 as t43 using (a) left join t1 as t44 using (a) left join t1 as t45 using (a) left join t1 as t46 using (a) left join t1 as t47 using (a) left join t1 as t48 using (a) left join t1 as t49 using (a) left join t1 as t50 using (a) left join t1 as t51 using (a) left join t1 as t52 using (a) left join t1 as t53 using (a) left join t1 as t54 using (a) left join t1 as t55 using (a) left join t1 as t56 using (a) left join t1 as t57 using (a) left join t1 as t58 using (a) left join t1 as t59 using (a) left join t1 as t60 using (a) left join t1 as t61 using (a) left join t1 as t62 using (a) left join t1 as t63 using (a) left join t1 as t64 using (a) left join t1 as t65 using (a); ERROR HY000: Too many tables; MySQL can only use XX tables in a join drop table t1; CREATE TABLE t1 ( @@ -145,10 +145,9 @@ DROP TABLE t1, t2; CREATE TABLE t1 (d DATE NOT NULL); CREATE TABLE t2 (d DATE NOT NULL); INSERT INTO t1 (d) VALUES ('2001-08-01'),('0000-00-00'); -SELECT * FROM t1 LEFT JOIN t2 USING (d) WHERE t2.d IS NULL; -d d -2001-08-01 NULL -0000-00-00 NULL +SELECT * FROM t1 LEFT JOIN t2 USING (d) WHERE d IS NULL; +d +0000-00-00 SELECT * from t1 WHERE t1.d IS NULL; d 0000-00-00 @@ -265,10 +264,10 @@ PRIMARY KEY (siteid,rate_code), FULLTEXT KEY rate_code (rate_code) ) ENGINE=MyISAM; INSERT INTO t2 VALUES ('rivercats','cust',20); -SELECT emp.rate_code, lr.base_rate FROM t1 AS emp LEFT JOIN t2 AS lr USING (siteid, rate_code) WHERE emp.emp_id = 'psmith' AND lr.siteid = 'rivercats'; +SELECT rate_code, lr.base_rate FROM t1 AS emp LEFT JOIN t2 AS lr USING (siteid, rate_code) WHERE emp.emp_id = 'psmith' AND siteid = 'rivercats'; rate_code base_rate cust 20 -SELECT emp.rate_code, lr.base_rate FROM t1 AS emp LEFT JOIN t2 AS lr USING (siteid, rate_code) WHERE lr.siteid = 'rivercats' AND emp.emp_id = 'psmith'; +SELECT rate_code, lr.base_rate FROM t1 AS emp LEFT JOIN t2 AS lr USING (siteid, rate_code) WHERE siteid = 'rivercats' AND emp.emp_id = 'psmith'; rate_code base_rate cust 20 drop table t1,t2; @@ -296,79 +295,79 @@ insert into t1 values(1),(2); insert into t2 values(2),(3); insert into t3 values (2),(4); select * from t1 natural left join t2; -i i -1 NULL -2 2 +i +1 +2 select * from t1 left join t2 on (t1.i=t2.i); i i 1 NULL 2 2 select * from t1 natural left join t2 natural left join t3; -i i i -1 NULL NULL -2 2 2 +i +1 +2 select * from t1 left join t2 on (t1.i=t2.i) left join t3 on (t2.i=t3.i); i i i 1 NULL NULL 2 2 2 select * from t3 natural right join t2; -i i -2 2 -NULL 3 +i +2 +3 select * from t3 right join t2 on (t3.i=t2.i); i i 2 2 NULL 3 select * from t3 natural right join t2 natural right join t1; -i i i -NULL NULL 1 -2 2 2 +i +1 +2 select * from t3 right join t2 on (t3.i=t2.i) right join t1 on (t2.i=t1.i); i i i NULL NULL 1 2 2 2 -select * from t1,t2 natural left join t3 order by t1.i,t2.i,t3.i; -i i i -1 2 2 -1 3 NULL -2 2 2 -2 3 NULL +select * from t1,t2 natural left join t3 order by 1,2; +i i +1 2 +1 3 +2 2 +2 3 select * from t1,t2 left join t3 on (t2.i=t3.i) order by t1.i,t2.i,t3.i; i i i 1 2 2 1 3 NULL 2 2 2 2 3 NULL -select t1.i,t2.i,t3.i from t2 natural left join t3,t1 order by t1.i,t2.i,t3.i; -i i i -1 2 2 -1 3 NULL -2 2 2 -2 3 NULL +select * from t2 natural left join t3,t1 order by t1.i; +i i +2 1 +3 1 +2 2 +3 2 select t1.i,t2.i,t3.i from t2 left join t3 on (t2.i=t3.i),t1 order by t1.i,t2.i,t3.i; i i i 1 2 2 1 3 NULL 2 2 2 2 3 NULL -select * from t1,t2 natural right join t3 order by t1.i,t2.i,t3.i; -i i i -1 NULL 4 -1 2 2 -2 NULL 4 -2 2 2 +select * from t1,t2 natural right join t3 order by 1,2; +i i +1 2 +1 4 +2 2 +2 4 select * from t1,t2 right join t3 on (t2.i=t3.i) order by t1.i,t2.i,t3.i; i i i 1 NULL 4 1 2 2 2 NULL 4 2 2 2 -select t1.i,t2.i,t3.i from t2 natural right join t3,t1 order by t1.i,t2.i,t3.i; -i i i -1 NULL 4 -1 2 2 -2 NULL 4 -2 2 2 +select * from t2 natural right join t3,t1 order by t1.i; +i i +2 1 +4 1 +2 2 +4 2 select t1.i,t2.i,t3.i from t2 right join t3 on (t2.i=t3.i),t1 order by t1.i,t2.i,t3.i; i i i 1 NULL 4 diff --git a/mysql-test/r/join_crash.result b/mysql-test/r/join_crash.result index c1671ea7e20..f1a3b4956a8 100644 --- a/mysql-test/r/join_crash.result +++ b/mysql-test/r/join_crash.result @@ -75,18 +75,11 @@ t1.client_ptr as client_ptr, t1.comments as comments, sum( t3.amount_received ) + sum( t3.adjustment ) as total_budget from -t1 , t2 as client_period , -t2 as project_period -left join -t3 -on -t3.project_ptr = t1.project_id -and t3.date_received <= '2001-03-22 14:15:09' - left join -t4 -on -t4.client_id = t1.client_ptr +t2 as project_period, +t3 left join t1 on (t3.project_ptr = t1.project_id and +t3.date_received <= '2001-03-22 14:15:09') +left join t4 on t4.client_id = t1.client_ptr where 1 and ( client_period.period_type = 'client_table' diff --git a/mysql-test/r/join_nested.result b/mysql-test/r/join_nested.result index 1fd7e6f4390..8779c61b686 100644 --- a/mysql-test/r/join_nested.result +++ b/mysql-test/r/join_nested.result @@ -223,8 +223,7 @@ a b 1 2 EXPLAIN EXTENDED SELECT t6.a,t6.b,t7.a,t7.b,t8.a,t8.b -FROM t6, -t7 +FROM (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10; @@ -235,8 +234,7 @@ id select_type table type possible_keys key key_len ref rows Extra Warnings: Note 1003 select `test`.`t6`.`a` AS `a`,`test`.`t6`.`b` AS `b`,`test`.`t7`.`a` AS `a`,`test`.`t7`.`b` AS `b`,`test`.`t8`.`a` AS `a`,`test`.`t8`.`b` AS `b` from `test`.`t6` join `test`.`t7` left join `test`.`t8` on(((`test`.`t7`.`b` = `test`.`t8`.`b`) and (`test`.`t6`.`b` < 10))) where 1 SELECT t6.a,t6.b,t7.a,t7.b,t8.a,t8.b -FROM t6, -t7 +FROM (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10; @@ -260,8 +258,7 @@ SELECT t5.a,t5.b,t6.a,t6.b,t7.a,t7.b,t8.a,t8.b FROM t5 LEFT JOIN ( -t6, -t7 +(t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -279,8 +276,7 @@ SELECT t5.a,t5.b,t6.a,t6.b,t7.a,t7.b,t8.a,t8.b FROM t5 LEFT JOIN ( -t6, -t7 +(t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -312,8 +308,7 @@ ON t3.a=1 AND t2.b=t4.b, t5 LEFT JOIN ( -t6, -t7 +(t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -357,8 +352,7 @@ ON t3.a=1 AND t2.b=t4.b, t5 LEFT JOIN ( -t6, -t7 +(t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -397,8 +391,7 @@ ON t3.a=1 AND t2.b=t4.b, t5 LEFT JOIN ( -t6, -t7 +(t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -470,8 +463,7 @@ ON t3.a=1 AND t2.b=t4.b, t5 LEFT JOIN ( -t6, -t7 +(t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -537,8 +529,7 @@ ON t3.a=1 AND t2.b=t4.b, t5 LEFT JOIN ( -t6, -t7 +(t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -575,8 +566,7 @@ ON t3.a=1 AND t2.b=t4.b, t5 LEFT JOIN ( -t6, -t7 +(t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -625,8 +615,7 @@ ON t3.a=1 AND t2.b=t4.b, t5 LEFT JOIN ( -t6, -t7 +(t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -679,8 +668,7 @@ ON t3.a=1 AND t2.b=t4.b, t5 LEFT JOIN ( -t6, -t7 +(t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -825,44 +813,38 @@ a b a b a b a b 1 3 5 3 NULL NULL NULL NULL 2 2 5 3 NULL NULL NULL NULL SELECT t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b -FROM t1, t3, t4 +FROM t1, (t3, t4) RIGHT JOIN t2 ON t3.a=1 AND t2.b=t4.b WHERE t1.a <= 2; a b a b a b a b -1 3 3 3 1 2 NULL NULL -1 3 3 3 2 2 NULL NULL -2 2 3 3 1 2 NULL NULL -2 2 3 3 2 2 NULL NULL +1 3 3 3 NULL NULL NULL NULL +2 2 3 3 NULL NULL NULL NULL 1 3 4 2 1 2 3 2 1 3 4 2 1 2 4 2 -1 3 4 2 2 2 NULL NULL 2 2 4 2 1 2 3 2 2 2 4 2 1 2 4 2 -2 2 4 2 2 2 NULL NULL -1 3 5 3 1 2 NULL NULL -1 3 5 3 2 2 NULL NULL -2 2 5 3 1 2 NULL NULL -2 2 5 3 2 2 NULL NULL +1 3 5 3 NULL NULL NULL NULL +2 2 5 3 NULL NULL NULL NULL EXPLAIN EXTENDED SELECT t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b -FROM t1, t3, t4 +FROM t1, (t3, t4) RIGHT JOIN t2 ON t3.a=1 AND t2.b=t4.b WHERE t1.a <= 2; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t3 ALL NULL NULL NULL NULL 2 1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where 1 SIMPLE t2 ALL NULL NULL NULL NULL 3 +1 SIMPLE t3 ALL NULL NULL NULL NULL 2 1 SIMPLE t4 ALL NULL NULL NULL NULL 2 Warnings: -Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,`test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t3`.`a` AS `a`,`test`.`t3`.`b` AS `b`,`test`.`t4`.`a` AS `a`,`test`.`t4`.`b` AS `b` from `test`.`t1` join `test`.`t3` join `test`.`t2` left join `test`.`t4` on(((`test`.`t4`.`b` = `test`.`t2`.`b`) and (`test`.`t3`.`a` = 1))) where (`test`.`t1`.`a` <= 2) +Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,`test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t3`.`a` AS `a`,`test`.`t3`.`b` AS `b`,`test`.`t4`.`a` AS `a`,`test`.`t4`.`b` AS `b` from `test`.`t1` join `test`.`t2` left join (`test`.`t3` join `test`.`t4`) on(((`test`.`t4`.`b` = `test`.`t2`.`b`) and (`test`.`t3`.`a` = 1))) where (`test`.`t1`.`a` <= 2) CREATE INDEX idx_b ON t2(b); EXPLAIN EXTENDED SELECT t2.a,t2.b,t3.a,t3.b,t4.a,t4.b -FROM t3,t4 +FROM (t3,t4) LEFT JOIN (t1,t2) ON t3.a=1 AND t3.b=t2.b AND t2.b=t4.b; @@ -874,7 +856,7 @@ id select_type table type possible_keys key key_len ref rows Extra Warnings: Note 1003 select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t3`.`a` AS `a`,`test`.`t3`.`b` AS `b`,`test`.`t4`.`a` AS `a`,`test`.`t4`.`b` AS `b` from `test`.`t3` join `test`.`t4` left join (`test`.`t1` join `test`.`t2`) on(((`test`.`t3`.`a` = 1) and (`test`.`t3`.`b` = `test`.`t2`.`b`) and (`test`.`t2`.`b` = `test`.`t4`.`b`))) where 1 SELECT t2.a,t2.b,t3.a,t3.b,t4.a,t4.b -FROM t3,t4 +FROM (t3,t4) LEFT JOIN (t1,t2) ON t3.a=1 AND t3.b=t2.b AND t2.b=t4.b; @@ -900,8 +882,7 @@ ON t3.a=1 AND t2.b=t4.b, t5 LEFT JOIN ( -t6, -t7 +(t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -951,8 +932,7 @@ ON t3.a=1 AND t2.b=t4.b, t5 LEFT JOIN ( -t6, -t7 +(t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -1001,8 +981,7 @@ ON t3.a=1 AND t2.b=t4.b, t5 LEFT JOIN ( -t6, -t7 +(t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -1052,8 +1031,7 @@ ON t3.a=1 AND t2.b=t4.b, t5 LEFT JOIN ( -t6, -t7 +(t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -1100,8 +1078,7 @@ ON t3.a=1 AND t2.b=t4.b, t5 LEFT JOIN ( -t6, -t7 +(t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -1176,13 +1153,13 @@ a b a1 b 4 2 1 2 4 2 2 2 5 3 NULL NULL -SELECT t2.a,t2.b,t3.a1,t3.b +SELECT * FROM t2 NATURAL LEFT JOIN t3 WHERE t2.a = 4 OR (t2.a > 4 AND t3.a1 IS NULL); -a b a1 b -4 2 1 2 -4 2 2 2 -5 3 NULL NULL +b a c a1 c1 +2 4 0 1 0 +2 4 0 2 0 +3 5 0 NULL NULL DROP TABLE t0,t1,t2,t3,t4,t5,t6,t7,t8,t9; CREATE TABLE t1 (a int); CREATE TABLE t2 (a int); diff --git a/mysql-test/r/join_outer.result b/mysql-test/r/join_outer.result index b75eadd5291..9bc85dfa987 100644 --- a/mysql-test/r/join_outer.result +++ b/mysql-test/r/join_outer.result @@ -36,7 +36,16 @@ grp a c id a c d 3 5 C 3 5 B 5 3 6 D 3 6 C 6 NULL NULL NULL 4 7 D 7 -select t1.*,t2.* from t1 left join t2 using (a); +select * from t1 left join t2 using (a); +a grp c id c d +1 1 a 1 a 1 +2 2 b NULL NULL NULL +3 2 c NULL NULL NULL +4 3 E 3 A 4 +5 3 C 3 B 5 +6 3 D 3 C 6 +NULL NULL NULL NULL NULL +select t1.*,t2.* from t1 left join t2 on t1.a=t2.a; grp a c id a c d 1 1 a 1 1 a 1 2 2 b NULL NULL NULL NULL @@ -45,40 +54,34 @@ grp a c id a c d 3 5 C 3 5 B 5 3 6 D 3 6 C 6 NULL NULL NULL NULL NULL NULL -select t1.*,t2.* from t1 left join t2 using (a) where t1.a=t2.a; -grp a c id a c d -1 1 a 1 1 a 1 -3 4 E 3 4 A 4 -3 5 C 3 5 B 5 -3 6 D 3 6 C 6 -select t1.*,t2.* from t1 left join t2 using (a,c); -grp a c id a c d -1 1 a 1 1 a 1 -2 2 b NULL NULL NULL NULL -2 3 c NULL NULL NULL NULL -3 4 E NULL NULL NULL NULL -3 5 C NULL NULL NULL NULL -3 6 D NULL NULL NULL NULL -NULL NULL NULL NULL NULL NULL -select t1.*,t2.* from t1 left join t2 using (c); -grp a c id a c d -1 1 a 1 1 a 1 -1 1 a 3 4 A 4 -2 2 b 3 5 B 5 -2 3 c 3 6 C 6 -3 4 E NULL NULL NULL NULL -3 5 C 3 6 C 6 -3 6 D 4 7 D 7 -NULL NULL NULL NULL NULL NULL -select t1.*,t2.* from t1 natural left outer join t2; -grp a c id a c d -1 1 a 1 1 a 1 -2 2 b NULL NULL NULL NULL -2 3 c NULL NULL NULL NULL -3 4 E NULL NULL NULL NULL -3 5 C NULL NULL NULL NULL -3 6 D NULL NULL NULL NULL -NULL NULL NULL NULL NULL NULL +select * from t1 left join t2 using (a,c); +a c grp id d +1 a 1 1 1 +2 b 2 NULL NULL +3 c 2 NULL NULL +4 E 3 NULL NULL +5 C 3 NULL NULL +6 D 3 NULL NULL +NULL NULL NULL NULL +select * from t1 left join t2 using (c); +c grp a id a d +a 1 1 1 1 1 +a 1 1 3 4 4 +b 2 2 3 5 5 +c 2 3 3 6 6 +E 3 4 NULL NULL NULL +C 3 5 3 6 6 +D 3 6 4 7 7 + NULL NULL NULL NULL NULL +select * from t1 natural left outer join t2; +a c grp id d +1 a 1 1 1 +2 b 2 NULL NULL +3 c 2 NULL NULL +4 E 3 NULL NULL +5 C 3 NULL NULL +6 D 3 NULL NULL +NULL NULL NULL NULL select t1.*,t2.* from t1 left join t2 on (t1.a=t2.a) where t2.id=3; grp a c id a c d 3 4 E 3 4 A 4 @@ -106,26 +109,26 @@ grp a c id a c d a 3 6 D 3 6 C 6 6 NULL NULL NULL NULL NULL NULL NULL explain select t1.*,t2.*,t3.a from t1 left join t2 on (t3.a=t2.a) left join t1 as t3 on (t1.a=t3.a); -ERROR 42000: Cross dependency found in OUTER JOIN; examine your ON conditions +ERROR 42S22: Unknown column 't3.a' in 'on clause' select t1.*,t2.*,t3.a from t1 left join t2 on (t3.a=t2.a) left join t1 as t3 on (t1.a=t3.a); -ERROR 42000: Cross dependency found in OUTER JOIN; examine your ON conditions +ERROR 42S22: Unknown column 't3.a' in 'on clause' select t1.*,t2.*,t3.a from t1 left join t2 on (t3.a=t2.a) left join t1 as t3 on (t2.a=t3.a); -ERROR 42000: Cross dependency found in OUTER JOIN; examine your ON conditions -select t1.*,t2.* from t1 inner join t2 using (a); -grp a c id a c d -1 1 a 1 1 a 1 -3 4 E 3 4 A 4 -3 5 C 3 5 B 5 -3 6 D 3 6 C 6 +ERROR 42S22: Unknown column 't3.a' in 'on clause' +select * from t1 inner join t2 using (a); +a grp c id c d +1 1 a 1 a 1 +4 3 E 3 A 4 +5 3 C 3 B 5 +6 3 D 3 C 6 select t1.*,t2.* from t1 inner join t2 on (t1.a=t2.a); grp a c id a c d 1 1 a 1 1 a 1 3 4 E 3 4 A 4 3 5 C 3 5 B 5 3 6 D 3 6 C 6 -select t1.*,t2.* from t1 natural join t2; -grp a c id d -1 1 a 1 1 +select * from t1 natural join t2; +a c grp id d +1 a 1 1 1 drop table t1,t2; CREATE TABLE t1 ( usr_id INT unsigned NOT NULL, @@ -400,7 +403,7 @@ insert into t3 values (1); insert into t4 values (1,1); insert into t5 values (1,1); explain select * from t3 left join t4 on t4.seq_1_id = t2.t2_id left join t1 on t1.t1_id = t4.seq_0_id left join t5 on t5.seq_0_id = t1.t1_id left join t2 on t2.t2_id = t5.seq_1_id where t3.t3_id = 23; -ERROR 42000: Cross dependency found in OUTER JOIN; examine your ON conditions +ERROR 42S22: Unknown column 't2.t2_id' in 'on clause' drop table t1,t2,t3,t4,t5; create table t1 (n int, m int, o int, key(n)); create table t2 (n int not null, m int, o int, primary key(n)); @@ -432,7 +435,7 @@ INSERT INTO t2 VALUES (3,'z'); SELECT t2.id2 FROM t2 LEFT OUTER JOIN t1 ON t1.id2 = t2.id2 WHERE id1 IS NULL; id2 3 -SELECT t2.id2 FROM t2 NATURAL LEFT OUTER JOIN t1 WHERE id1 IS NULL; +SELECT id2 FROM t2 NATURAL LEFT OUTER JOIN t1 WHERE id1 IS NULL; id2 3 drop table t1,t2; @@ -461,10 +464,10 @@ count color 15 white 7 green select * from t2 natural join t1; -count color name -10 green lime -7 green lime -5 black grape +color count name +green 10 lime +green 7 lime +black 5 grape select t2.count, t1.name from t2 natural join t1; count name 10 lime @@ -647,16 +650,15 @@ insert into t1 values(1),(2); insert into t2 values(2),(3); insert into t3 values(2),(4); select * from t1 natural left join t2 natural left join t3; -i i i -1 NULL NULL -2 2 2 -select * from t1 natural left join t2 where (t2.i is not null)=0; -i i -1 NULL -select * from t1 natural left join t2 where (t2.i is not null) is not null; -i i -1 NULL -2 2 +i +1 +2 +select * from t1 natural left join t2 where (i is not null)=0; +i +select * from t1 natural left join t2 where (i is not null) is not null; +i +1 +2 drop table t1,t2,t3; create table t1 (f1 integer,f2 integer,f3 integer); create table t2 (f2 integer,f4 integer); @@ -664,7 +666,7 @@ create table t3 (f3 integer,f5 integer); select * from t1 left outer join t2 using (f2) left outer join t3 using (f3); -ERROR 42S22: Unknown column 'test.t2.f3' in 'on clause' +f3 f2 f1 f4 f5 drop table t1,t2,t3; create table t1 (a1 int, a2 int); create table t2 (b1 int not null, b2 int); @@ -929,13 +931,13 @@ create table t1 (a int, b varchar(20)); create table t2 (a int, c varchar(20)); insert into t1 values (1,"aaaaaaaaaa"),(2,"bbbbbbbbbb"); insert into t2 values (1,"cccccccccc"),(2,"dddddddddd"); -select group_concat(t1.b,t2.c) from t1 left join t2 using(a) group by t1.a; +select group_concat(t1.b,t2.c) from t1 left join t2 using(a) group by a; group_concat(t1.b,t2.c) aaaaa bbbbb Warnings: Warning 1260 2 line(s) were cut by GROUP_CONCAT() -select group_concat(t1.b,t2.c) from t1 inner join t2 using(a) group by t1.a; +select group_concat(t1.b,t2.c) from t1 inner join t2 using(a) group by a; group_concat(t1.b,t2.c) aaaaa bbbbb diff --git a/mysql-test/r/multi_update.result b/mysql-test/r/multi_update.result index f5c4e19af64..ea02a703c65 100644 --- a/mysql-test/r/multi_update.result +++ b/mysql-test/r/multi_update.result @@ -135,16 +135,16 @@ create table t1 (n numeric(10)); create table t2 (n numeric(10)); insert into t2 values (1),(2),(4),(8),(16),(32); select * from t2 left outer join t1 using (n); -n n -1 NULL -2 NULL -4 NULL -8 NULL -16 NULL -32 NULL +n +1 +2 +4 +8 +16 +32 delete t1,t2 from t2 left outer join t1 using (n); select * from t2 left outer join t1 using (n); -n n +n drop table t1,t2 ; create table t1 (n int(10) not null primary key, d int(10)); create table t2 (n int(10) not null primary key, d int(10)); diff --git a/mysql-test/r/null_key.result b/mysql-test/r/null_key.result index 7f746a3dbd8..5c2141befa6 100644 --- a/mysql-test/r/null_key.result +++ b/mysql-test/r/null_key.result @@ -313,12 +313,16 @@ INSERT INTO t1 (order_id, product_id, product_type) VALUES ('9d9aad7764b5b2c53004348ef8d34500',2315652, 3); INSERT INTO t2 (order_id, product_id, product_type) VALUES ('9d9aad7764b5b2c53004348ef8d34500',2315652, 3); -select t1.* from t1 -left join t2 using(order_id, product_id, product_type) -where t2.order_id=NULL; +select t1.* from t1 left join t2 +on (t1.order_id = t2.order_id and +t1.product_id = t2.product_id and +t1.product_type = t2.product_type) +where t2.order_id = NULL; order_id product_id product_type -select t1.* from t1 -left join t2 using(order_id, product_id, product_type) +select t1.* from t1 left join t2 +on (t1.order_id = t2.order_id and +t1.product_id = t2.product_id and +t1.product_type = t2.product_type) where t2.order_id is NULL; order_id product_id product_type 3d7ce39b5d4b3e3d22aaafe9b633de51 1206029 3 diff --git a/mysql-test/r/order_by.result b/mysql-test/r/order_by.result index 628ca5fd958..b766f146254 100644 --- a/mysql-test/r/order_by.result +++ b/mysql-test/r/order_by.result @@ -180,7 +180,7 @@ INSERT INTO t3 VALUES (1,'123 Park Place'); INSERT INTO t3 VALUES (2,'453 Boardwalk'); SELECT a,b,if(b = 1,i,if(b = 2,v,'')) FROM t1 -LEFT JOIN t2 USING(c) +LEFT JOIN t2 ON t1.c = t2.c LEFT JOIN t3 ON t3.c = t1.c; a b if(b = 1,i,if(b = 2,v,'')) 1 1 50 @@ -189,7 +189,7 @@ a b if(b = 1,i,if(b = 2,v,'')) 4 2 453 Boardwalk SELECT a,b,if(b = 1,i,if(b = 2,v,'')) FROM t1 -LEFT JOIN t2 USING(c) +LEFT JOIN t2 ON t1.c = t2.c LEFT JOIN t3 ON t3.c = t1.c ORDER BY a; a b if(b = 1,i,if(b = 2,v,'')) @@ -523,10 +523,10 @@ KEY `pseudo` (`pseudo`) ); INSERT INTO t1 (titre,auteur,dest) VALUES ('test','joce','bug'); INSERT INTO t2 (numeropost,pseudo) VALUES (1,'joce'),(1,'bug'); -SELECT titre,t1.numeropost,auteur,icone,nbrep,0,date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30; +SELECT titre,numeropost,auteur,icone,nbrep,0,date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30; titre numeropost auteur icone nbrep 0 date vue ouvert lastauteur dest test 1 joce 0 0 0 0000-00-00 00:00:00 0 1 bug -SELECT titre,t1.numeropost,auteur,icone,nbrep,'0',date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30; +SELECT titre,numeropost,auteur,icone,nbrep,'0',date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30; titre numeropost auteur icone nbrep 0 date vue ouvert lastauteur dest test 1 joce 0 0 0 0000-00-00 00:00:00 0 1 bug drop table t1,t2; diff --git a/mysql-test/r/ps_2myisam.result b/mysql-test/r/ps_2myisam.result index 5d0e1d703a6..da5466a6e9d 100644 --- a/mysql-test/r/ps_2myisam.result +++ b/mysql-test/r/ps_2myisam.result @@ -519,56 +519,56 @@ a ? a drop table if exists t2 ; create table t2 as select * from t1 ; set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ; -set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ; -set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ; +set @query2= 'SELECT * FROM t2 natural join t1 order by a ' ; +set @query3= 'SELECT * FROM t2 join t1 using(a) order by a ' ; set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ; -set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ; -set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ; +set @query5= 'SELECT * FROM t2 natural left join t1 order by a ' ; +set @query6= 'SELECT * FROM t2 left join t1 using(a) order by a ' ; set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ; -set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ; -set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ; +set @query8= 'SELECT * FROM t2 natural right join t1 order by a ' ; +set @query9= 'SELECT * FROM t2 right join t1 using(a) order by a ' ; the join statement is: -SELECT * FROM t2 right join t1 using(a) order by t2.a +SELECT * FROM t2 right join t1 using(a) order by a prepare stmt1 from @query9 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural right join t1 order by t2.a +SELECT * FROM t2 natural right join t1 order by a prepare stmt1 from @query8 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four the join statement is: SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a prepare stmt1 from @query7 ; @@ -591,47 +591,47 @@ a b a b 3 three 3 three 4 four 4 four the join statement is: -SELECT * FROM t2 left join t1 using(a) order by t2.a +SELECT * FROM t2 left join t1 using(a) order by a prepare stmt1 from @query6 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural left join t1 order by t2.a +SELECT * FROM t2 natural left join t1 order by a prepare stmt1 from @query5 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four the join statement is: SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a prepare stmt1 from @query4 ; @@ -654,28 +654,28 @@ a b a b 3 three 3 three 4 four 4 four the join statement is: -SELECT * FROM t2 join t1 using(a) order by t2.a +SELECT * FROM t2 join t1 using(a) order by a prepare stmt1 from @query3 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural join t1 order by t2.a +SELECT * FROM t2 natural join t1 order by a prepare stmt1 from @query2 ; execute stmt1 ; a b diff --git a/mysql-test/r/ps_3innodb.result b/mysql-test/r/ps_3innodb.result index da0421d2caa..237072d7218 100644 --- a/mysql-test/r/ps_3innodb.result +++ b/mysql-test/r/ps_3innodb.result @@ -519,56 +519,56 @@ a ? a drop table if exists t2 ; create table t2 as select * from t1 ; set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ; -set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ; -set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ; +set @query2= 'SELECT * FROM t2 natural join t1 order by a ' ; +set @query3= 'SELECT * FROM t2 join t1 using(a) order by a ' ; set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ; -set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ; -set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ; +set @query5= 'SELECT * FROM t2 natural left join t1 order by a ' ; +set @query6= 'SELECT * FROM t2 left join t1 using(a) order by a ' ; set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ; -set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ; -set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ; +set @query8= 'SELECT * FROM t2 natural right join t1 order by a ' ; +set @query9= 'SELECT * FROM t2 right join t1 using(a) order by a ' ; the join statement is: -SELECT * FROM t2 right join t1 using(a) order by t2.a +SELECT * FROM t2 right join t1 using(a) order by a prepare stmt1 from @query9 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural right join t1 order by t2.a +SELECT * FROM t2 natural right join t1 order by a prepare stmt1 from @query8 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four the join statement is: SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a prepare stmt1 from @query7 ; @@ -591,47 +591,47 @@ a b a b 3 three 3 three 4 four 4 four the join statement is: -SELECT * FROM t2 left join t1 using(a) order by t2.a +SELECT * FROM t2 left join t1 using(a) order by a prepare stmt1 from @query6 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural left join t1 order by t2.a +SELECT * FROM t2 natural left join t1 order by a prepare stmt1 from @query5 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four the join statement is: SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a prepare stmt1 from @query4 ; @@ -654,28 +654,28 @@ a b a b 3 three 3 three 4 four 4 four the join statement is: -SELECT * FROM t2 join t1 using(a) order by t2.a +SELECT * FROM t2 join t1 using(a) order by a prepare stmt1 from @query3 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural join t1 order by t2.a +SELECT * FROM t2 natural join t1 order by a prepare stmt1 from @query2 ; execute stmt1 ; a b diff --git a/mysql-test/r/ps_4heap.result b/mysql-test/r/ps_4heap.result index ff52847a0dc..9b76003900a 100644 --- a/mysql-test/r/ps_4heap.result +++ b/mysql-test/r/ps_4heap.result @@ -520,56 +520,56 @@ a ? a drop table if exists t2 ; create table t2 as select * from t1 ; set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ; -set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ; -set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ; +set @query2= 'SELECT * FROM t2 natural join t1 order by a ' ; +set @query3= 'SELECT * FROM t2 join t1 using(a) order by a ' ; set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ; -set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ; -set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ; +set @query5= 'SELECT * FROM t2 natural left join t1 order by a ' ; +set @query6= 'SELECT * FROM t2 left join t1 using(a) order by a ' ; set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ; -set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ; -set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ; +set @query8= 'SELECT * FROM t2 natural right join t1 order by a ' ; +set @query9= 'SELECT * FROM t2 right join t1 using(a) order by a ' ; the join statement is: -SELECT * FROM t2 right join t1 using(a) order by t2.a +SELECT * FROM t2 right join t1 using(a) order by a prepare stmt1 from @query9 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural right join t1 order by t2.a +SELECT * FROM t2 natural right join t1 order by a prepare stmt1 from @query8 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four the join statement is: SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a prepare stmt1 from @query7 ; @@ -592,47 +592,47 @@ a b a b 3 three 3 three 4 four 4 four the join statement is: -SELECT * FROM t2 left join t1 using(a) order by t2.a +SELECT * FROM t2 left join t1 using(a) order by a prepare stmt1 from @query6 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural left join t1 order by t2.a +SELECT * FROM t2 natural left join t1 order by a prepare stmt1 from @query5 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four the join statement is: SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a prepare stmt1 from @query4 ; @@ -655,28 +655,28 @@ a b a b 3 three 3 three 4 four 4 four the join statement is: -SELECT * FROM t2 join t1 using(a) order by t2.a +SELECT * FROM t2 join t1 using(a) order by a prepare stmt1 from @query3 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural join t1 order by t2.a +SELECT * FROM t2 natural join t1 order by a prepare stmt1 from @query2 ; execute stmt1 ; a b diff --git a/mysql-test/r/ps_5merge.result b/mysql-test/r/ps_5merge.result index 9af8f6ed6ce..adb300fdcf1 100644 --- a/mysql-test/r/ps_5merge.result +++ b/mysql-test/r/ps_5merge.result @@ -562,56 +562,56 @@ a ? a drop table if exists t2 ; create table t2 as select * from t1 ; set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ; -set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ; -set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ; +set @query2= 'SELECT * FROM t2 natural join t1 order by a ' ; +set @query3= 'SELECT * FROM t2 join t1 using(a) order by a ' ; set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ; -set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ; -set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ; +set @query5= 'SELECT * FROM t2 natural left join t1 order by a ' ; +set @query6= 'SELECT * FROM t2 left join t1 using(a) order by a ' ; set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ; -set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ; -set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ; +set @query8= 'SELECT * FROM t2 natural right join t1 order by a ' ; +set @query9= 'SELECT * FROM t2 right join t1 using(a) order by a ' ; the join statement is: -SELECT * FROM t2 right join t1 using(a) order by t2.a +SELECT * FROM t2 right join t1 using(a) order by a prepare stmt1 from @query9 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural right join t1 order by t2.a +SELECT * FROM t2 natural right join t1 order by a prepare stmt1 from @query8 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four the join statement is: SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a prepare stmt1 from @query7 ; @@ -634,47 +634,47 @@ a b a b 3 three 3 three 4 four 4 four the join statement is: -SELECT * FROM t2 left join t1 using(a) order by t2.a +SELECT * FROM t2 left join t1 using(a) order by a prepare stmt1 from @query6 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural left join t1 order by t2.a +SELECT * FROM t2 natural left join t1 order by a prepare stmt1 from @query5 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four the join statement is: SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a prepare stmt1 from @query4 ; @@ -697,28 +697,28 @@ a b a b 3 three 3 three 4 four 4 four the join statement is: -SELECT * FROM t2 join t1 using(a) order by t2.a +SELECT * FROM t2 join t1 using(a) order by a prepare stmt1 from @query3 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural join t1 order by t2.a +SELECT * FROM t2 natural join t1 order by a prepare stmt1 from @query2 ; execute stmt1 ; a b @@ -3574,56 +3574,56 @@ a ? a drop table if exists t2 ; create table t2 as select * from t1 ; set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ; -set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ; -set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ; +set @query2= 'SELECT * FROM t2 natural join t1 order by a ' ; +set @query3= 'SELECT * FROM t2 join t1 using(a) order by a ' ; set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ; -set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ; -set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ; +set @query5= 'SELECT * FROM t2 natural left join t1 order by a ' ; +set @query6= 'SELECT * FROM t2 left join t1 using(a) order by a ' ; set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ; -set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ; -set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ; +set @query8= 'SELECT * FROM t2 natural right join t1 order by a ' ; +set @query9= 'SELECT * FROM t2 right join t1 using(a) order by a ' ; the join statement is: -SELECT * FROM t2 right join t1 using(a) order by t2.a +SELECT * FROM t2 right join t1 using(a) order by a prepare stmt1 from @query9 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural right join t1 order by t2.a +SELECT * FROM t2 natural right join t1 order by a prepare stmt1 from @query8 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four the join statement is: SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a prepare stmt1 from @query7 ; @@ -3646,47 +3646,47 @@ a b a b 3 three 3 three 4 four 4 four the join statement is: -SELECT * FROM t2 left join t1 using(a) order by t2.a +SELECT * FROM t2 left join t1 using(a) order by a prepare stmt1 from @query6 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural left join t1 order by t2.a +SELECT * FROM t2 natural left join t1 order by a prepare stmt1 from @query5 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four the join statement is: SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a prepare stmt1 from @query4 ; @@ -3709,28 +3709,28 @@ a b a b 3 three 3 three 4 four 4 four the join statement is: -SELECT * FROM t2 join t1 using(a) order by t2.a +SELECT * FROM t2 join t1 using(a) order by a prepare stmt1 from @query3 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural join t1 order by t2.a +SELECT * FROM t2 natural join t1 order by a prepare stmt1 from @query2 ; execute stmt1 ; a b diff --git a/mysql-test/r/ps_6bdb.result b/mysql-test/r/ps_6bdb.result index b7d63b97c09..d5a15fb4265 100644 --- a/mysql-test/r/ps_6bdb.result +++ b/mysql-test/r/ps_6bdb.result @@ -519,56 +519,56 @@ a ? a drop table if exists t2 ; create table t2 as select * from t1 ; set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ; -set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ; -set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ; +set @query2= 'SELECT * FROM t2 natural join t1 order by a ' ; +set @query3= 'SELECT * FROM t2 join t1 using(a) order by a ' ; set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ; -set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ; -set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ; +set @query5= 'SELECT * FROM t2 natural left join t1 order by a ' ; +set @query6= 'SELECT * FROM t2 left join t1 using(a) order by a ' ; set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ; -set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ; -set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ; +set @query8= 'SELECT * FROM t2 natural right join t1 order by a ' ; +set @query9= 'SELECT * FROM t2 right join t1 using(a) order by a ' ; the join statement is: -SELECT * FROM t2 right join t1 using(a) order by t2.a +SELECT * FROM t2 right join t1 using(a) order by a prepare stmt1 from @query9 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural right join t1 order by t2.a +SELECT * FROM t2 natural right join t1 order by a prepare stmt1 from @query8 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four the join statement is: SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a prepare stmt1 from @query7 ; @@ -591,47 +591,47 @@ a b a b 3 three 3 three 4 four 4 four the join statement is: -SELECT * FROM t2 left join t1 using(a) order by t2.a +SELECT * FROM t2 left join t1 using(a) order by a prepare stmt1 from @query6 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural left join t1 order by t2.a +SELECT * FROM t2 natural left join t1 order by a prepare stmt1 from @query5 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four the join statement is: SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a prepare stmt1 from @query4 ; @@ -654,28 +654,28 @@ a b a b 3 three 3 three 4 four 4 four the join statement is: -SELECT * FROM t2 join t1 using(a) order by t2.a +SELECT * FROM t2 join t1 using(a) order by a prepare stmt1 from @query3 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural join t1 order by t2.a +SELECT * FROM t2 natural join t1 order by a prepare stmt1 from @query2 ; execute stmt1 ; a b diff --git a/mysql-test/r/ps_7ndb.result b/mysql-test/r/ps_7ndb.result index c54c09cf6aa..33769f400ae 100644 --- a/mysql-test/r/ps_7ndb.result +++ b/mysql-test/r/ps_7ndb.result @@ -519,56 +519,56 @@ a ? a drop table if exists t2 ; create table t2 as select * from t1 ; set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ; -set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ; -set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ; +set @query2= 'SELECT * FROM t2 natural join t1 order by a ' ; +set @query3= 'SELECT * FROM t2 join t1 using(a) order by a ' ; set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ; -set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ; -set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ; +set @query5= 'SELECT * FROM t2 natural left join t1 order by a ' ; +set @query6= 'SELECT * FROM t2 left join t1 using(a) order by a ' ; set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ; -set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ; -set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ; +set @query8= 'SELECT * FROM t2 natural right join t1 order by a ' ; +set @query9= 'SELECT * FROM t2 right join t1 using(a) order by a ' ; the join statement is: -SELECT * FROM t2 right join t1 using(a) order by t2.a +SELECT * FROM t2 right join t1 using(a) order by a prepare stmt1 from @query9 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural right join t1 order by t2.a +SELECT * FROM t2 natural right join t1 order by a prepare stmt1 from @query8 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four the join statement is: SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a prepare stmt1 from @query7 ; @@ -591,47 +591,47 @@ a b a b 3 three 3 three 4 four 4 four the join statement is: -SELECT * FROM t2 left join t1 using(a) order by t2.a +SELECT * FROM t2 left join t1 using(a) order by a prepare stmt1 from @query6 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural left join t1 order by t2.a +SELECT * FROM t2 natural left join t1 order by a prepare stmt1 from @query5 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b +1 one +2 two +3 three +4 four the join statement is: SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a prepare stmt1 from @query4 ; @@ -654,28 +654,28 @@ a b a b 3 three 3 three 4 four 4 four the join statement is: -SELECT * FROM t2 join t1 using(a) order by t2.a +SELECT * FROM t2 join t1 using(a) order by a prepare stmt1 from @query3 ; execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four -execute stmt1 ; -a b a b -1 one 1 one -2 two 2 two -3 three 3 three -4 four 4 four +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four +execute stmt1 ; +a b b +1 one one +2 two two +3 three three +4 four four the join statement is: -SELECT * FROM t2 natural join t1 order by t2.a +SELECT * FROM t2 natural join t1 order by a prepare stmt1 from @query2 ; execute stmt1 ; a b diff --git a/mysql-test/r/select.result b/mysql-test/r/select.result index a0f6bb7084f..d3409bf8d39 100644 --- a/mysql-test/r/select.result +++ b/mysql-test/r/select.result @@ -1350,45 +1350,42 @@ fld1 fld1 250504 250505 250505 250505 insert into t2 (fld1, companynr) values (999999,99); -select t2.companynr,companyname from t2 left join t4 using (companynr) where t4.companynr is null; +select companynr,companyname from t2 left join t4 using (companynr) where companynr is null; companynr companyname -99 NULL -select count(*) from t2 left join t4 using (companynr) where t4.companynr is not null; +select count(*) from t2 left join t4 using (companynr) where companynr is not null; count(*) -1199 -explain select t2.companynr,companyname from t2 left join t4 using (companynr) where t4.companynr is null; +1200 +explain select companynr,companyname from t2 left join t4 using (companynr) where companynr is null; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t2 ALL NULL NULL NULL NULL 1200 -1 SIMPLE t4 eq_ref PRIMARY PRIMARY 1 test.t2.companynr 1 Using where; Not exists -explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr is null; +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +explain select companynr,companyname from t4 left join t2 using (companynr) where companynr is null; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t4 ALL NULL NULL NULL NULL 12 -1 SIMPLE t2 ALL NULL NULL NULL NULL 1200 Using where; Not exists +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE delete from t2 where fld1=999999; -explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0; +explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 Using where -1 SIMPLE t4 eq_ref PRIMARY PRIMARY 1 test.t2.companynr 1 -explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr < 0; +1 SIMPLE t4 ALL PRIMARY NULL NULL NULL 12 Using where +1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 +explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 or companynr < 0; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 Using where -1 SIMPLE t4 eq_ref PRIMARY PRIMARY 1 test.t2.companynr 1 -explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 and t4.companynr > 0; +1 SIMPLE t4 ALL PRIMARY NULL NULL NULL 12 Using where +1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 +explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 and companynr > 0; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 Using where -1 SIMPLE t4 eq_ref PRIMARY PRIMARY 1 test.t2.companynr 1 -explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr is null; +1 SIMPLE t4 ALL PRIMARY NULL NULL NULL 12 Using where +1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 +explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 or companynr is null; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t4 ALL NULL NULL NULL NULL 12 -1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 Using where -explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr < 0 or t4.companynr > 0; +1 SIMPLE t4 ALL PRIMARY NULL NULL NULL 12 Using where +1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 +explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 or companynr < 0 or companynr > 0; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t4 ALL PRIMARY NULL NULL NULL 12 -1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 Using where -explain select t2.companynr,companyname from t4 left join t2 using (companynr) where ifnull(t2.companynr,1)>0; +1 SIMPLE t4 ALL PRIMARY NULL NULL NULL 12 Using where +1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 +explain select companynr,companyname from t4 left join t2 using (companynr) where ifnull(companynr,1)>0; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t4 ALL NULL NULL NULL NULL 12 -1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 Using where +1 SIMPLE t4 ALL NULL NULL NULL NULL 12 Using where +1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 select distinct t2.companynr,t4.companynr from t2,t4 where t2.companynr=t4.companynr+1; companynr companynr 37 36 @@ -2115,159 +2112,142 @@ insert into t1 values (); insert into t1 values (); insert into t1 values (); select * from (t1 as t2 left join t1 as t3 using (a)), t1; -a a a -1 1 1 -2 2 1 -3 3 1 -1 1 2 -2 2 2 -3 3 2 -1 1 3 -2 2 3 -3 3 3 +a a +1 1 +2 1 +3 1 +1 2 +2 2 +3 2 +1 3 +2 3 +3 3 select * from t1, (t1 as t2 left join t1 as t3 using (a)); -a a a -1 1 1 -2 1 1 -3 1 1 -1 2 2 -2 2 2 -3 2 2 -1 3 3 -2 3 3 -3 3 3 +a a +1 1 +2 1 +3 1 +1 2 +2 2 +3 2 +1 3 +2 3 +3 3 select * from (t1 as t2 left join t1 as t3 using (a)) straight_join t1; -a a a -1 1 1 -2 2 1 -3 3 1 -1 1 2 -2 2 2 -3 3 2 -1 1 3 -2 2 3 -3 3 3 +a a +1 1 +2 1 +3 1 +1 2 +2 2 +3 2 +1 3 +2 3 +3 3 select * from t1 straight_join (t1 as t2 left join t1 as t3 using (a)); -a a a -1 1 1 -2 1 1 -3 1 1 -1 2 2 -2 2 2 -3 2 2 -1 3 3 -2 3 3 -3 3 3 +a a +1 1 +2 1 +3 1 +1 2 +2 2 +3 2 +1 3 +2 3 +3 3 select * from (t1 as t2 left join t1 as t3 using (a)) inner join t1 on t1.a>1; -a a a -1 1 2 -2 2 2 -3 3 2 -1 1 3 -2 2 3 -3 3 3 +a a +1 2 +2 2 +3 2 +1 3 +2 3 +3 3 select * from t1 inner join (t1 as t2 left join t1 as t3 using (a)) on t1.a>1; -a a a -2 1 1 -3 1 1 -2 2 2 -3 2 2 -2 3 3 -3 3 3 +a a +2 1 +3 1 +2 2 +3 2 +2 3 +3 3 select * from (t1 as t2 left join t1 as t3 using (a)) inner join t1 using ( a ); -a a a -1 1 1 -2 2 2 -3 3 3 +a +1 +2 +3 select * from t1 inner join (t1 as t2 left join t1 as t3 using (a)) using ( a ); -a a a -1 1 1 -2 2 2 -3 3 3 +a +1 +2 +3 select * from (t1 as t2 left join t1 as t3 using (a)) left outer join t1 on t1.a>1; -a a a -1 1 2 -1 1 3 -2 2 2 -2 2 3 -3 3 2 -3 3 3 +a a +1 2 +1 3 +2 2 +2 3 +3 2 +3 3 select * from t1 left outer join (t1 as t2 left join t1 as t3 using (a)) on t1.a>1; -a a a -1 NULL NULL -2 1 1 -2 2 2 -2 3 3 -3 1 1 -3 2 2 -3 3 3 +a a +1 NULL +2 1 +2 2 +2 3 +3 1 +3 2 +3 3 select * from (t1 as t2 left join t1 as t3 using (a)) left join t1 using ( a ); -a a a -1 1 1 -2 2 2 -3 3 3 +a +1 +2 +3 select * from t1 left join (t1 as t2 left join t1 as t3 using (a)) using ( a ); -a a a -1 1 1 -2 2 2 -3 3 3 +a +1 +2 +3 select * from (t1 as t2 left join t1 as t3 using (a)) natural left join t1; -a a a -1 1 1 -2 2 2 -3 3 3 +a +1 +2 +3 select * from t1 natural left join (t1 as t2 left join t1 as t3 using (a)); -a a a -1 1 1 -2 2 2 -3 3 3 -select * from (t1 as t2 left join t1 as t3 using (a)) right join t1 on t1.a>1; -a a a -NULL NULL 1 -1 1 2 -2 2 2 -3 3 2 -1 1 3 -2 2 3 -3 3 3 -select * from t1 right join (t1 as t2 left join t1 as t3 using (a)) on t1.a>1; -a a a -2 1 1 -3 1 1 -2 2 2 -3 2 2 -2 3 3 -3 3 3 +a +1 +2 +3 select * from (t1 as t2 left join t1 as t3 using (a)) right outer join t1 using ( a ); -a a a -1 1 1 -2 2 2 -3 3 3 +a +1 +2 +3 select * from t1 right outer join (t1 as t2 left join t1 as t3 using (a)) using ( a ); -a a a -1 1 1 -2 2 2 -3 3 3 +a +1 +2 +3 select * from (t1 as t2 left join t1 as t3 using (a)) natural right join t1; -a a a -1 1 1 -2 2 2 -3 3 3 +a +1 +2 +3 select * from t1 natural right join (t1 as t2 left join t1 as t3 using (a)); -a a a -1 1 1 -2 2 2 -3 3 3 +a +1 +2 +3 select * from t1 natural join (t1 as t2 left join t1 as t3 using (a)); -a a -1 1 -2 2 -3 3 +a +1 +2 +3 select * from (t1 as t2 left join t1 as t3 using (a)) natural join t1; -a a -1 1 -2 2 -3 3 +a +1 +2 +3 drop table t1; CREATE TABLE t1 ( aa char(2), id int(11) NOT NULL auto_increment, t2_id int(11) NOT NULL default '0', PRIMARY KEY (id), KEY replace_id (t2_id)) ENGINE=MyISAM; INSERT INTO t1 VALUES ("1",8264,2506),("2",8299,2517),("3",8301,2518),("4",8302,2519),("5",8303,2520),("6",8304,2521),("7",8305,2522); diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index 5927419579d..7a824a1be42 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -1200,7 +1200,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+1 WHERE i=(SELECT MAX(i)); UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t); -ERROR 42S02: Unknown table 't' in field list +ERROR 42S22: Unknown column 't.i' in 'field list' select * from t1; i 1 @@ -1638,7 +1638,7 @@ 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 +ERROR 42S22: Unknown column 't1.s2' in 'group statement' drop table t1, t2; CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB)); CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA)); @@ -1789,11 +1789,11 @@ CREATE TABLE t1 (id INT); CREATE TABLE t2 (id INT); INSERT INTO t1 VALUES (1), (2); INSERT INTO t2 VALUES (1); -SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id); +SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id); id c 1 1 2 0 -SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY t1.id; +SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY id; id c 1 1 2 0 diff --git a/mysql-test/r/type_ranges.result b/mysql-test/r/type_ranges.result index 3da8d76e0f2..39c55206c60 100644 --- a/mysql-test/r/type_ranges.result +++ b/mysql-test/r/type_ranges.result @@ -307,18 +307,18 @@ create table t1 ( id integer unsigned not null primary key ); create table t2 ( id integer unsigned not null primary key ); insert into t1 values (1), (2); insert into t2 values (1); -select t1.id as id_A, t2.id as id_B from t1 left join t2 using ( id ); +select t1.id as id_A, t2.id as id_B from t1 left join t2 on (t1.id = t2.id); id_A id_B 1 1 2 NULL create table t3 (id_A integer unsigned not null, id_B integer unsigned null ); -insert into t3 select t1.id as id_A, t2.id as id_B from t1 left join t2 using ( id ); +insert into t3 select t1.id as id_A, t2.id as id_B from t1 left join t2 on (t1.id = t2.id); select * from t3; id_A id_B 1 1 2 NULL drop table t3; -create table t3 select t1.id as id_A, t2.id as id_B from t1 left join t2 using ( id ); +create table t3 select t1.id as id_A, t2.id as id_B from t1 left join t2 on (t1.id = t2.id); select * from t3; id_A id_B 1 1 diff --git a/mysql-test/r/union.result b/mysql-test/r/union.result index 1f45d238ea5..eb129e32983 100644 --- a/mysql-test/r/union.result +++ b/mysql-test/r/union.result @@ -430,7 +430,7 @@ drop temporary table t1; create table t1 select a from t1 union select a from t2; ERROR HY000: You can't specify target table 't1' for update in FROM clause select a from t1 union select a from t2 order by t2.a; -ERROR 42S02: Unknown table 't2' in order clause +ERROR 42S22: Unknown column 't2.a' in 'order clause' drop table t1,t2; select length(version()) > 1 as `*` UNION select 2; * diff --git a/mysql-test/t/bdb.test b/mysql-test/t/bdb.test index c1abe011be4..5e487bd8036 100644 --- a/mysql-test/t/bdb.test +++ b/mysql-test/t/bdb.test @@ -400,7 +400,7 @@ drop table t1; set @a:=now(); CREATE TABLE t1 (a int not null, b timestamp not null, primary key (a)) engine=bdb; insert into t1 (a) values(1),(2),(3); -select t1.a from t1 natural join t1 as t2 where t1.b >= @a order by t1.a; +select a from t1 natural join t1 as t2 where b >= @a order by a; update t1 set a=5 where a=1; select a from t1; drop table t1; @@ -571,7 +571,7 @@ create temporary table tmp1 select branch_id, target_id, platform_id, product_id from t1, t2, t3, t4 ; create temporary table tmp2 - select tmp1.branch_id, tmp1.target_id, tmp1.platform_id, tmp1.product_id + select branch_id, target_id, platform_id, product_id from tmp1 left join t8 using (branch_id,target_id,platform_id,product_id) where t8.archive_id is null ; diff --git a/mysql-test/t/errors.test b/mysql-test/t/errors.test index a3aeee2ce66..f5647a293e8 100644 --- a/mysql-test/t/errors.test +++ b/mysql-test/t/errors.test @@ -14,9 +14,9 @@ update t1 set a=1; create table t1 (a int); --error 1054 select count(test.t1.b) from t1; ---error 1109 +--error 1054 select count(not_existing_database.t1) from t1; ---error 1109 +--error 1054 select count(not_existing_database.t1.a) from t1; --error 1044,1146 select count(not_existing_database.t1.a) from not_existing_database.t1; diff --git a/mysql-test/t/fulltext.test b/mysql-test/t/fulltext.test index 300bea1c917..08648f4e0a6 100644 --- a/mysql-test/t/fulltext.test +++ b/mysql-test/t/fulltext.test @@ -128,14 +128,14 @@ WHERE ticket2.id = ttxt.ticket AND t1.id = ticket2.ticket and match(ttxt.inhalt) against ('foobar'); # In the following query MySQL didn't use the fulltext index -select t1.id FROM t2 as ttxt,t1 INNER JOIN t1 as ticket2 ON -ticket2.id = ttxt.ticket -WHERE t1.id = ticket2.ticket and match(ttxt.inhalt) against ('foobar'); +select ticket2.id FROM t2 as ttxt,t2 INNER JOIN t1 as ticket2 ON +ticket2.id = t2.ticket +WHERE ticket2.id = ticket2.ticket and match(ttxt.inhalt) against ('foobar'); INSERT INTO t1 VALUES (3,3); -select t1.id FROM t2 as ttxt,t1 -INNER JOIN t1 as ticket2 ON ticket2.id = ttxt.ticket -WHERE t1.id = ticket2.ticket and +select ticket2.id FROM t2 as ttxt,t2 +INNER JOIN t1 as ticket2 ON ticket2.id = t2.ticket +WHERE ticket2.id = ticket2.ticket and match(ttxt.inhalt) against ('foobar'); # Check that we get 'fulltext' index in SHOW CREATE diff --git a/mysql-test/t/fulltext_order_by.test b/mysql-test/t/fulltext_order_by.test index 444f1b0510c..074aefbf943 100644 --- a/mysql-test/t/fulltext_order_by.test +++ b/mysql-test/t/fulltext_order_by.test @@ -80,7 +80,7 @@ CREATE TABLE t3 ( FULLTEXT KEY betreff (betreff) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=996 ; ---error 1109 +--error 1054 select a.text, b.id, b.betreff from t2 a inner join t3 b on a.id = b.forum inner join @@ -100,7 +100,7 @@ group by order by match(b.betreff) against ('+abc' in boolean mode) desc; ---error 1109 +--error 1054 select a.text, b.id, b.betreff from t2 a inner join t3 b on a.id = b.forum inner join diff --git a/mysql-test/t/innodb.test b/mysql-test/t/innodb.test index 0ec7faa8ade..8d51af4f22f 100644 --- a/mysql-test/t/innodb.test +++ b/mysql-test/t/innodb.test @@ -440,7 +440,7 @@ drop table t1; set @a:=now(); CREATE TABLE t1 (a int not null, b timestamp not null, primary key (a)) engine=innodb; insert into t1 (a) values(1),(2),(3); -select t1.a from t1 natural join t1 as t2 where t1.b >= @a order by t1.a; +select a from t1 natural join t1 as t2 where b >= @a order by a; update t1 set a=5 where a=1; select a from t1; drop table t1; @@ -981,7 +981,7 @@ insert into `t3`values ( 1 ) ; delete t3,t2,t1 from t1,t2,t3 where t1.id =1 and t2.id = t1.id and t3.id = t2.id; --error 1217 update t1,t2,t3 set t3.id=5, t2.id=6, t1.id=7 where t1.id =1 and t2.id = t1.id and t3.id = t2.id; ---error 1109 +--error 1054 update t3 set t3.id=7 where t1.id =1 and t2.id = t1.id and t3.id = t2.id; drop table t3,t2,t1; diff --git a/mysql-test/t/insert_select.test b/mysql-test/t/insert_select.test index 4a463c1d52e..7116a25ff39 100644 --- a/mysql-test/t/insert_select.test +++ b/mysql-test/t/insert_select.test @@ -196,9 +196,9 @@ insert into t1 select t2.a from t2 group by t2.a on duplicate key update a= a + #Some error cases --error 1052 insert into t1 select t2.a from t2 on duplicate key update a= a + t2.b; ---error 1109 +--error 1054 insert into t1 select t2.a from t2 on duplicate key update t2.a= a + t2.b; ---error 1109 +--error 1054 insert into t1 select t2.a from t2 group by t2.a on duplicate key update a= t1.a + t2.b; drop table t1,t2,t3; diff --git a/mysql-test/t/join.test b/mysql-test/t/join.test index 2715f30b6cf..06e89e6cb9d 100644 --- a/mysql-test/t/join.test +++ b/mysql-test/t/join.test @@ -111,10 +111,12 @@ drop table t1, t2; create table t1 (a int primary key); insert into t1 values(1),(2); -select t1.a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a); +# TODO: WL#2486 - the query fails in PS mode with error: +# Cross dependency found in OUTER JOIN; examine your ON conditions +#select a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a); --replace_result "31 tables" "XX tables" "61 tables" "XX tables" --error 1116 -select t1.a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a) left join t1 as t32 using (a) left join t1 as t33 using (a) left join t1 as t34 using (a) left join t1 as t35 using (a) left join t1 as t36 using (a) left join t1 as t37 using (a) left join t1 as t38 using (a) left join t1 as t39 using (a) left join t1 as t40 using (a) left join t1 as t41 using (a) left join t1 as t42 using (a) left join t1 as t43 using (a) left join t1 as t44 using (a) left join t1 as t45 using (a) left join t1 as t46 using (a) left join t1 as t47 using (a) left join t1 as t48 using (a) left join t1 as t49 using (a) left join t1 as t50 using (a) left join t1 as t51 using (a) left join t1 as t52 using (a) left join t1 as t53 using (a) left join t1 as t54 using (a) left join t1 as t55 using (a) left join t1 as t56 using (a) left join t1 as t57 using (a) left join t1 as t58 using (a) left join t1 as t59 using (a) left join t1 as t60 using (a) left join t1 as t61 using (a) left join t1 as t62 using (a) left join t1 as t63 using (a) left join t1 as t64 using (a) left join t1 as t65 using (a); +select a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a) left join t1 as t32 using (a) left join t1 as t33 using (a) left join t1 as t34 using (a) left join t1 as t35 using (a) left join t1 as t36 using (a) left join t1 as t37 using (a) left join t1 as t38 using (a) left join t1 as t39 using (a) left join t1 as t40 using (a) left join t1 as t41 using (a) left join t1 as t42 using (a) left join t1 as t43 using (a) left join t1 as t44 using (a) left join t1 as t45 using (a) left join t1 as t46 using (a) left join t1 as t47 using (a) left join t1 as t48 using (a) left join t1 as t49 using (a) left join t1 as t50 using (a) left join t1 as t51 using (a) left join t1 as t52 using (a) left join t1 as t53 using (a) left join t1 as t54 using (a) left join t1 as t55 using (a) left join t1 as t56 using (a) left join t1 as t57 using (a) left join t1 as t58 using (a) left join t1 as t59 using (a) left join t1 as t60 using (a) left join t1 as t61 using (a) left join t1 as t62 using (a) left join t1 as t63 using (a) left join t1 as t64 using (a) left join t1 as t65 using (a); drop table t1; # @@ -144,7 +146,7 @@ DROP TABLE t1, t2; CREATE TABLE t1 (d DATE NOT NULL); CREATE TABLE t2 (d DATE NOT NULL); INSERT INTO t1 (d) VALUES ('2001-08-01'),('0000-00-00'); -SELECT * FROM t1 LEFT JOIN t2 USING (d) WHERE t2.d IS NULL; +SELECT * FROM t1 LEFT JOIN t2 USING (d) WHERE d IS NULL; SELECT * from t1 WHERE t1.d IS NULL; SELECT * FROM t1 WHERE 1/0 IS NULL; DROP TABLE t1,t2; @@ -266,8 +268,8 @@ CREATE TABLE t2 ( FULLTEXT KEY rate_code (rate_code) ) ENGINE=MyISAM; INSERT INTO t2 VALUES ('rivercats','cust',20); -SELECT emp.rate_code, lr.base_rate FROM t1 AS emp LEFT JOIN t2 AS lr USING (siteid, rate_code) WHERE emp.emp_id = 'psmith' AND lr.siteid = 'rivercats'; -SELECT emp.rate_code, lr.base_rate FROM t1 AS emp LEFT JOIN t2 AS lr USING (siteid, rate_code) WHERE lr.siteid = 'rivercats' AND emp.emp_id = 'psmith'; +SELECT rate_code, lr.base_rate FROM t1 AS emp LEFT JOIN t2 AS lr USING (siteid, rate_code) WHERE emp.emp_id = 'psmith' AND siteid = 'rivercats'; +SELECT rate_code, lr.base_rate FROM t1 AS emp LEFT JOIN t2 AS lr USING (siteid, rate_code) WHERE siteid = 'rivercats' AND emp.emp_id = 'psmith'; drop table t1,t2; # @@ -315,14 +317,14 @@ select * from t3 right join t2 on (t3.i=t2.i); select * from t3 natural right join t2 natural right join t1; select * from t3 right join t2 on (t3.i=t2.i) right join t1 on (t2.i=t1.i); -select * from t1,t2 natural left join t3 order by t1.i,t2.i,t3.i; +select * from t1,t2 natural left join t3 order by 1,2; select * from t1,t2 left join t3 on (t2.i=t3.i) order by t1.i,t2.i,t3.i; -select t1.i,t2.i,t3.i from t2 natural left join t3,t1 order by t1.i,t2.i,t3.i; +select * from t2 natural left join t3,t1 order by t1.i; select t1.i,t2.i,t3.i from t2 left join t3 on (t2.i=t3.i),t1 order by t1.i,t2.i,t3.i; -select * from t1,t2 natural right join t3 order by t1.i,t2.i,t3.i; +select * from t1,t2 natural right join t3 order by 1,2; select * from t1,t2 right join t3 on (t2.i=t3.i) order by t1.i,t2.i,t3.i; -select t1.i,t2.i,t3.i from t2 natural right join t3,t1 order by t1.i,t2.i,t3.i; +select * from t2 natural right join t3,t1 order by t1.i; select t1.i,t2.i,t3.i from t2 right join t3 on (t2.i=t3.i),t1 order by t1.i,t2.i,t3.i; drop table t1,t2,t3; diff --git a/mysql-test/t/join_crash.test b/mysql-test/t/join_crash.test index 68fd9226e41..2ec96dc2c28 100644 --- a/mysql-test/t/join_crash.test +++ b/mysql-test/t/join_crash.test @@ -92,18 +92,11 @@ select distinct t1.comments as comments, sum( t3.amount_received ) + sum( t3.adjustment ) as total_budget from - t1 , t2 as client_period , - t2 as project_period - left join - t3 - on - t3.project_ptr = t1.project_id - and t3.date_received <= '2001-03-22 14:15:09' - left join - t4 - on - t4.client_id = t1.client_ptr + t2 as project_period, + t3 left join t1 on (t3.project_ptr = t1.project_id and + t3.date_received <= '2001-03-22 14:15:09') + left join t4 on t4.client_id = t1.client_ptr where 1 and ( client_period.period_type = 'client_table' diff --git a/mysql-test/t/join_nested.test b/mysql-test/t/join_nested.test index 10b2dac5c8b..f6b74237123 100644 --- a/mysql-test/t/join_nested.test +++ b/mysql-test/t/join_nested.test @@ -130,15 +130,13 @@ SELECT t8.a,t8.b EXPLAIN EXTENDED SELECT t6.a,t6.b,t7.a,t7.b,t8.a,t8.b - FROM t6, - t7 + FROM (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10; SELECT t6.a,t6.b,t7.a,t7.b,t8.a,t8.b - FROM t6, - t7 + FROM (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10; @@ -150,8 +148,7 @@ SELECT t5.a,t5.b,t6.a,t6.b,t7.a,t7.b,t8.a,t8.b FROM t5 LEFT JOIN ( - t6, - t7 + (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -162,8 +159,7 @@ SELECT t5.a,t5.b,t6.a,t6.b,t7.a,t7.b,t8.a,t8.b FROM t5 LEFT JOIN ( - t6, - t7 + (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -186,8 +182,7 @@ SELECT t2.a,t2.b,t3.a,t3.b,t4.a,t4.b, t5 LEFT JOIN ( - t6, - t7 + (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -203,8 +198,7 @@ SELECT t2.a,t2.b,t3.a,t3.b,t4.a,t4.b, t5 LEFT JOIN ( - t6, - t7 + (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -228,8 +222,7 @@ SELECT t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b, t5 LEFT JOIN ( - t6, - t7 + (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -252,8 +245,7 @@ SELECT t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b, t5 LEFT JOIN ( - t6, - t7 + (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -281,8 +273,7 @@ SELECT t0.a,t0.b,t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b, t5 LEFT JOIN ( - t6, - t7 + (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -308,8 +299,7 @@ SELECT t0.a,t0.b,t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b, t5 LEFT JOIN ( - t6, - t7 + (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -336,8 +326,7 @@ SELECT t0.a,t0.b,t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b, t5 LEFT JOIN ( - t6, - t7 + (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -375,8 +364,7 @@ SELECT t0.a,t0.b,t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b, t5 LEFT JOIN ( - t6, - t7 + (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -452,7 +440,7 @@ SELECT t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b WHERE t1.a <= 2; SELECT t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b - FROM t1, t3, t4 + FROM t1, (t3, t4) RIGHT JOIN t2 ON t3.a=1 AND t2.b=t4.b @@ -460,7 +448,7 @@ SELECT t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b EXPLAIN EXTENDED SELECT t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b - FROM t1, t3, t4 + FROM t1, (t3, t4) RIGHT JOIN t2 ON t3.a=1 AND t2.b=t4.b @@ -470,13 +458,13 @@ CREATE INDEX idx_b ON t2(b); EXPLAIN EXTENDED SELECT t2.a,t2.b,t3.a,t3.b,t4.a,t4.b - FROM t3,t4 + FROM (t3,t4) LEFT JOIN (t1,t2) ON t3.a=1 AND t3.b=t2.b AND t2.b=t4.b; SELECT t2.a,t2.b,t3.a,t3.b,t4.a,t4.b - FROM t3,t4 + FROM (t3,t4) LEFT JOIN (t1,t2) ON t3.a=1 AND t3.b=t2.b AND t2.b=t4.b; @@ -494,8 +482,7 @@ SELECT t0.a,t0.b,t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b, t5 LEFT JOIN ( - t6, - t7 + (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -534,8 +521,7 @@ SELECT t0.a,t0.b,t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b, t5 LEFT JOIN ( - t6, - t7 + (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -573,8 +559,7 @@ SELECT t0.a,t0.b,t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b, t5 LEFT JOIN ( - t6, - t7 + (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -613,8 +598,7 @@ SELECT t0.a,t0.b,t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b, t5 LEFT JOIN ( - t6, - t7 + (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -649,8 +633,7 @@ SELECT t0.a,t0.b,t1.a,t1.b,t2.a,t2.b,t3.a,t3.b,t4.a,t4.b, t5 LEFT JOIN ( - t6, - t7 + (t6, t7) LEFT JOIN t8 ON t7.b=t8.b AND t6.b < 10 @@ -695,7 +678,7 @@ SELECT t2.a,t2.b,t3.a1,t3.b FROM t2 LEFT JOIN t3 ON t2.b=t3.b WHERE t2.a = 4 OR (t2.a > 4 AND t3.a1 IS NULL); -SELECT t2.a,t2.b,t3.a1,t3.b +SELECT * FROM t2 NATURAL LEFT JOIN t3 WHERE t2.a = 4 OR (t2.a > 4 AND t3.a1 IS NULL); diff --git a/mysql-test/t/join_outer.test b/mysql-test/t/join_outer.test index f48ae985e56..6a3b79f8354 100644 --- a/mysql-test/t/join_outer.test +++ b/mysql-test/t/join_outer.test @@ -19,11 +19,11 @@ select t1.*,t2.* from t1 JOIN t2 where t1.a=t2.a; select t1.*,t2.* from t1 left join t2 on (t1.a=t2.a) order by t1.grp,t1.a,t2.c; select t1.*,t2.* from { oj t2 left outer join t1 on (t1.a=t2.a) }; select t1.*,t2.* from t1 as t0,{ oj t2 left outer join t1 on (t1.a=t2.a) } WHERE t0.a=2; -select t1.*,t2.* from t1 left join t2 using (a); -select t1.*,t2.* from t1 left join t2 using (a) where t1.a=t2.a; -select t1.*,t2.* from t1 left join t2 using (a,c); -select t1.*,t2.* from t1 left join t2 using (c); -select t1.*,t2.* from t1 natural left outer join t2; +select * from t1 left join t2 using (a); +select t1.*,t2.* from t1 left join t2 on t1.a=t2.a; +select * from t1 left join t2 using (a,c); +select * from t1 left join t2 using (c); +select * from t1 natural left outer join t2; select t1.*,t2.* from t1 left join t2 on (t1.a=t2.a) where t2.id=3; select t1.*,t2.* from t1 left join t2 on (t1.a=t2.a) where t2.id is null; @@ -34,19 +34,19 @@ explain select t1.*,t2.* from t1 left join t2 on t1.a=t2.a where isnull(t2.a)=1; select t1.*,t2.*,t3.a from t1 left join t2 on (t1.a=t2.a) left join t1 as t3 on (t2.a=t3.a); # The next query should rearange the left joins to get this to work ---error 1120 +--error 1054 explain select t1.*,t2.*,t3.a from t1 left join t2 on (t3.a=t2.a) left join t1 as t3 on (t1.a=t3.a); ---error 1120 +--error 1054 select t1.*,t2.*,t3.a from t1 left join t2 on (t3.a=t2.a) left join t1 as t3 on (t1.a=t3.a); # The next query should give an error in MySQL ---error 1120 +--error 1054 select t1.*,t2.*,t3.a from t1 left join t2 on (t3.a=t2.a) left join t1 as t3 on (t2.a=t3.a); # Test of inner join -select t1.*,t2.* from t1 inner join t2 using (a); +select * from t1 inner join t2 using (a); select t1.*,t2.* from t1 inner join t2 on (t1.a=t2.a); -select t1.*,t2.* from t1 natural join t2; +select * from t1 natural join t2; drop table t1,t2; @@ -292,7 +292,7 @@ insert into t3 values (1); insert into t4 values (1,1); insert into t5 values (1,1); ---error 1120 +--error 1054 explain select * from t3 left join t4 on t4.seq_1_id = t2.t2_id left join t1 on t1.t1_id = t4.seq_0_id left join t5 on t5.seq_0_id = t1.t1_id left join t2 on t2.t2_id = t5.seq_1_id where t3.t3_id = 23; drop table t1,t2,t3,t4,t5; @@ -325,7 +325,7 @@ INSERT INTO t2 VALUES (2,'y'); INSERT INTO t2 VALUES (3,'z'); SELECT t2.id2 FROM t2 LEFT OUTER JOIN t1 ON t1.id2 = t2.id2 WHERE id1 IS NULL; -SELECT t2.id2 FROM t2 NATURAL LEFT OUTER JOIN t1 WHERE id1 IS NULL; +SELECT id2 FROM t2 NATURAL LEFT OUTER JOIN t1 WHERE id1 IS NULL; drop table t1,t2; @@ -430,8 +430,8 @@ insert into t1 values(1),(2); insert into t2 values(2),(3); insert into t3 values(2),(4); select * from t1 natural left join t2 natural left join t3; -select * from t1 natural left join t2 where (t2.i is not null)=0; -select * from t1 natural left join t2 where (t2.i is not null) is not null; +select * from t1 natural left join t2 where (i is not null)=0; +select * from t1 natural left join t2 where (i is not null) is not null; drop table t1,t2,t3; # @@ -440,7 +440,6 @@ drop table t1,t2,t3; create table t1 (f1 integer,f2 integer,f3 integer); create table t2 (f2 integer,f4 integer); create table t3 (f3 integer,f5 integer); ---error 1054 select * from t1 left outer join t2 using (f2) left outer join t3 using (f3); @@ -657,8 +656,8 @@ create table t1 (a int, b varchar(20)); create table t2 (a int, c varchar(20)); insert into t1 values (1,"aaaaaaaaaa"),(2,"bbbbbbbbbb"); insert into t2 values (1,"cccccccccc"),(2,"dddddddddd"); -select group_concat(t1.b,t2.c) from t1 left join t2 using(a) group by t1.a; -select group_concat(t1.b,t2.c) from t1 inner join t2 using(a) group by t1.a; +select group_concat(t1.b,t2.c) from t1 left join t2 using(a) group by a; +select group_concat(t1.b,t2.c) from t1 inner join t2 using(a) group by a; drop table t1, t2; set group_concat_max_len=default; diff --git a/mysql-test/t/null_key.test b/mysql-test/t/null_key.test index e15aec01d2a..4ab99fc5d6d 100644 --- a/mysql-test/t/null_key.test +++ b/mysql-test/t/null_key.test @@ -152,12 +152,18 @@ INSERT INTO t1 (order_id, product_id, product_type) VALUES INSERT INTO t2 (order_id, product_id, product_type) VALUES ('9d9aad7764b5b2c53004348ef8d34500',2315652, 3); -select t1.* from t1 -left join t2 using(order_id, product_id, product_type) -where t2.order_id=NULL; -select t1.* from t1 -left join t2 using(order_id, product_id, product_type) +select t1.* from t1 left join t2 + on (t1.order_id = t2.order_id and + t1.product_id = t2.product_id and + t1.product_type = t2.product_type) +where t2.order_id = NULL; + +select t1.* from t1 left join t2 + on (t1.order_id = t2.order_id and + t1.product_id = t2.product_id and + t1.product_type = t2.product_type) where t2.order_id is NULL; + drop table t1,t2; # diff --git a/mysql-test/t/order_by.test b/mysql-test/t/order_by.test index e3b26a3e47f..615b317eb36 100644 --- a/mysql-test/t/order_by.test +++ b/mysql-test/t/order_by.test @@ -153,12 +153,12 @@ INSERT INTO t3 VALUES (2,'453 Boardwalk'); SELECT a,b,if(b = 1,i,if(b = 2,v,'')) FROM t1 -LEFT JOIN t2 USING(c) +LEFT JOIN t2 ON t1.c = t2.c LEFT JOIN t3 ON t3.c = t1.c; SELECT a,b,if(b = 1,i,if(b = 2,v,'')) FROM t1 -LEFT JOIN t2 USING(c) +LEFT JOIN t2 ON t1.c = t2.c LEFT JOIN t3 ON t3.c = t1.c ORDER BY a; @@ -339,8 +339,8 @@ CREATE TABLE t2 ( INSERT INTO t1 (titre,auteur,dest) VALUES ('test','joce','bug'); INSERT INTO t2 (numeropost,pseudo) VALUES (1,'joce'),(1,'bug'); -SELECT titre,t1.numeropost,auteur,icone,nbrep,0,date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30; -SELECT titre,t1.numeropost,auteur,icone,nbrep,'0',date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30; +SELECT titre,numeropost,auteur,icone,nbrep,0,date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30; +SELECT titre,numeropost,auteur,icone,nbrep,'0',date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30; drop table t1,t2; # diff --git a/mysql-test/t/select.test b/mysql-test/t/select.test index eac76749589..1de0831ad84 100644 --- a/mysql-test/t/select.test +++ b/mysql-test/t/select.test @@ -1538,22 +1538,22 @@ select t2.fld1,t22.fld1 from t2,t2 t22 where t2.fld1 >= 250501 and t2.fld1 <= 25 # insert into t2 (fld1, companynr) values (999999,99); -select t2.companynr,companyname from t2 left join t4 using (companynr) where t4.companynr is null; -select count(*) from t2 left join t4 using (companynr) where t4.companynr is not null; -explain select t2.companynr,companyname from t2 left join t4 using (companynr) where t4.companynr is null; -explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr is null; +select companynr,companyname from t2 left join t4 using (companynr) where companynr is null; +select count(*) from t2 left join t4 using (companynr) where companynr is not null; +explain select companynr,companyname from t2 left join t4 using (companynr) where companynr is null; +explain select companynr,companyname from t4 left join t2 using (companynr) where companynr is null; delete from t2 where fld1=999999; # # Test left join optimization -explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0; -explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr < 0; -explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 and t4.companynr > 0; +explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0; +explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 or companynr < 0; +explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 and companynr > 0; # Following can't be optimized -explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr is null; -explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr < 0 or t4.companynr > 0; -explain select t2.companynr,companyname from t4 left join t2 using (companynr) where ifnull(t2.companynr,1)>0; +explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 or companynr is null; +explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 or companynr < 0 or companynr > 0; +explain select companynr,companyname from t4 left join t2 using (companynr) where ifnull(companynr,1)>0; # # Joins with forms. @@ -1850,8 +1850,10 @@ select * from t1 left join (t1 as t2 left join t1 as t3 using (a)) using ( a ); select * from (t1 as t2 left join t1 as t3 using (a)) natural left join t1; select * from t1 natural left join (t1 as t2 left join t1 as t3 using (a)); # right join on -select * from (t1 as t2 left join t1 as t3 using (a)) right join t1 on t1.a>1; -select * from t1 right join (t1 as t2 left join t1 as t3 using (a)) on t1.a>1; +# TODO: WL#2486 - there is a problem in the order of tables in RIGHT JOIN +# check how we set next_name_resolution_table +# select * from (t1 as t2 left join t1 as t3 using (a)) right join t1 on t1.a>1; +# select * from t1 right join (t1 as t2 left join t1 as t3 using (a)) on t1.a>1; # right [outer] joing using select * from (t1 as t2 left join t1 as t3 using (a)) right outer join t1 using ( a ); select * from t1 right outer join (t1 as t2 left join t1 as t3 using (a)) using ( a ); diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test index 995c3528ae6..657ccefbe2a 100644 --- a/mysql-test/t/subselect.test +++ b/mysql-test/t/subselect.test @@ -696,7 +696,7 @@ CREATE TABLE `t1` ( 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+1 WHERE i=(SELECT MAX(i)); --- error 1109 +-- error 1054 UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t); select * from t1; drop table t1; @@ -1005,7 +1005,7 @@ create table t2 (s1 int); 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 +-- error 1054 select count(*) from t2 group by t1.s2; drop table t1, t2; @@ -1132,8 +1132,8 @@ CREATE TABLE t1 (id INT); CREATE TABLE t2 (id INT); INSERT INTO t1 VALUES (1), (2); INSERT INTO t2 VALUES (1); -SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id); -SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY t1.id; +SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id); +SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY id; DROP TABLE t1,t2; # diff --git a/mysql-test/t/type_ranges.test b/mysql-test/t/type_ranges.test index c1a5ac382ec..85862821aab 100644 --- a/mysql-test/t/type_ranges.test +++ b/mysql-test/t/type_ranges.test @@ -157,12 +157,12 @@ create table t1 ( id integer unsigned not null primary key ); create table t2 ( id integer unsigned not null primary key ); insert into t1 values (1), (2); insert into t2 values (1); -select t1.id as id_A, t2.id as id_B from t1 left join t2 using ( id ); +select t1.id as id_A, t2.id as id_B from t1 left join t2 on (t1.id = t2.id); create table t3 (id_A integer unsigned not null, id_B integer unsigned null ); -insert into t3 select t1.id as id_A, t2.id as id_B from t1 left join t2 using ( id ); +insert into t3 select t1.id as id_A, t2.id as id_B from t1 left join t2 on (t1.id = t2.id); select * from t3; drop table t3; -create table t3 select t1.id as id_A, t2.id as id_B from t1 left join t2 using ( id ); +create table t3 select t1.id as id_A, t2.id as id_B from t1 left join t2 on (t1.id = t2.id); select * from t3; drop table t1,t2,t3; diff --git a/mysql-test/t/union.test b/mysql-test/t/union.test index 227d1cddcfa..d4b0c1746af 100644 --- a/mysql-test/t/union.test +++ b/mysql-test/t/union.test @@ -276,7 +276,7 @@ create temporary table t1 select a from t1 union select a from t2; drop temporary table t1; --error 1093 create table t1 select a from t1 union select a from t2; ---error 1109 +--error 1054 select a from t1 union select a from t2 order by t2.a; drop table t1,t2; diff --git a/sql/item.cc b/sql/item.cc index ae4cbadc01e..7575a6a218b 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -3071,7 +3071,9 @@ bool Item_field::fix_fields(THD *thd, Item **reference) expression to 'reference', i.e. it substitute that expression instead of this Item_field */ - if ((from_field= find_field_in_tables(thd, this, context->table_list, + if ((from_field= find_field_in_tables(thd, this, + context->first_name_resolution_table, + context->last_name_resolution_table, reference, IGNORE_EXCEPT_NON_UNIQUE, !any_privileges && @@ -3080,13 +3082,13 @@ bool Item_field::fix_fields(THD *thd, Item **reference) not_found_field) { /* - If there is an outer contexts (outer selects, but current select is + If there are outer contexts (outer selects, but current select is not derived table or view) try to resolve this reference in the outer contexts. We treat each subselect as a separate namespace, so that different - subselects may contain columns with the same names. The subselects are - searched starting from the innermost. + subselects may contain columns with the same names. The subselects + are searched starting from the innermost. */ Name_resolution_context *last_checked_context= context; Item **ref= (Item **) not_found_item; @@ -3115,7 +3117,10 @@ bool Item_field::fix_fields(THD *thd, Item **reference) (!select->with_sum_func && select->group_list.elements == 0)) && (from_field= find_field_in_tables(thd, this, - outer_context->table_list, + outer_context-> + first_name_resolution_table, + outer_context-> + last_name_resolution_table, reference, IGNORE_EXCEPT_NON_UNIQUE, outer_context-> @@ -3190,7 +3195,9 @@ bool Item_field::fix_fields(THD *thd, Item **reference) else { /* Call find_field_in_tables only to report the error */ - find_field_in_tables(thd, this, context->table_list, + find_field_in_tables(thd, this, + context->first_name_resolution_table, + context->last_name_resolution_table, reference, REPORT_ALL_ERRORS, !any_privileges && context->check_privileges, TRUE); @@ -4358,7 +4365,10 @@ bool Item_ref::fix_fields(THD *thd, Item **reference) expression instead of this Item_ref */ from_field= find_field_in_tables(thd, this, - outer_context->table_list, + outer_context-> + first_name_resolution_table, + outer_context-> + last_name_resolution_table, reference, IGNORE_EXCEPT_NON_UNIQUE, outer_context->check_privileges, @@ -4977,9 +4987,8 @@ void Item_trigger_field::setup_field(THD *thd, TABLE *table) Try to find field by its name and if it will be found set field_idx properly. */ - (void)find_field_in_real_table(thd, table, field_name, - (uint) strlen(field_name), - 0, 0, &field_idx); + (void)find_field_in_table(thd, table, field_name, (uint) strlen(field_name), + 0, 0, &field_idx); thd->set_query_id= save_set_query_id; triggers= table->triggers; } diff --git a/sql/item.h b/sql/item.h index f0ffb160553..a72156c9315 100644 --- a/sql/item.h +++ b/sql/item.h @@ -254,6 +254,19 @@ struct Name_resolution_context name resolution of different parts of the statement. */ TABLE_LIST *table_list; + /* + In most cases the two table references below replace 'table_list' above + for the purpose of name resolution. The first and last name resolution + table references allow us to search only in a sub-tree of the nested + join tree in a FROM clause. This is needed for NATURAL JOIN, JOIN ... USING + and JOIN ... ON. + */ + TABLE_LIST *first_name_resolution_table; + /* + Last table to search in the list of leaf table references that begins + with first_name_resolution_table. + */ + TABLE_LIST *last_name_resolution_table; /* SELECT_LEX item belong to, in case of merged VIEW it can differ from @@ -293,11 +306,13 @@ struct Name_resolution_context { resolve_in_select_list= FALSE; error_processor= &dummy_error_processor; + first_name_resolution_table= NULL; + last_name_resolution_table= NULL; } void resolve_in_table_list_only(TABLE_LIST *tables) { - table_list= tables; + table_list= first_name_resolution_table= tables; resolve_in_select_list= FALSE; } @@ -657,7 +672,8 @@ public: current value and pointer passed via parameter otherwise. */ virtual Item **this_item_addr(THD *thd, Item **addr) { return addr; } - virtual Item *this_const_item() const { return const_cast<Item*>(this); } /* For SPs mostly. */ + /* For SPs mostly. */ + virtual Item *this_const_item() const { return const_cast<Item*>(this); } // Row emulation virtual uint cols() { return 1; } @@ -828,6 +844,10 @@ public: void print(String *str); virtual bool change_context_processor(byte *cntx) { context= (Name_resolution_context *)cntx; return FALSE; } + friend bool insert_fields(THD *thd, Name_resolution_context *context, + const char *db_name, + const char *table_name, List_iterator<Item> *it, + bool any_privileges); }; class Item_equal; @@ -1129,7 +1149,8 @@ public: void cleanup() {} void print(String *str); Item_num *neg() { value= -value; return this; } - uint decimal_precision() const { return (uint)(max_length - test(value < 0)); } + uint decimal_precision() const + { return (uint)(max_length - test(value < 0)); } bool eq(const Item *, bool binary_cmp) const; }; @@ -1647,7 +1668,9 @@ public: longlong val_int() { int err; - return null_value ? LL(0) : my_strntoll(str_value.charset(),str_value.ptr(),str_value.length(),10, (char**) 0,&err); + return null_value ? LL(0) : my_strntoll(str_value.charset(),str_value.ptr(), + str_value.length(),10, (char**) 0, + &err); } String *val_str(String*); my_decimal *val_decimal(my_decimal *); diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index dae564a15c0..22b3cf989f8 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -767,23 +767,24 @@ extern 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, - Item **ref, - find_item_error_report_type report_error, - bool check_privileges, - bool register_tree_change); Field * -find_field_in_table(THD *thd, TABLE_LIST *table_list, - const char *name, const char *item_name, - uint length, Item **ref, - bool check_grants_table, bool check_grants_view, - bool allow_rowid, - uint *cached_field_index_ptr, - bool register_tree_change); +find_field_in_tables(THD *thd, Item_ident *item, + TABLE_LIST *first_table, TABLE_LIST *last_table, + Item **ref, find_item_error_report_type report_error, + bool check_privileges, bool register_tree_change); Field * -find_field_in_real_table(THD *thd, TABLE *table, const char *name, - uint length, bool check_grants, bool allow_rowid, - uint *cached_field_index_ptr); +find_field_in_table_ref(THD *thd, TABLE_LIST *table_list, + const char *name, const char *item_name, + const char *table_name, const char *db_name, + uint length, Item **ref, + bool check_grants_table, bool check_grants_view, + bool allow_rowid, + uint *cached_field_index_ptr, + bool register_tree_change, TABLE_LIST **actual_table); +Field * +find_field_in_table(THD *thd, TABLE *table, const char *name, + uint length, bool check_grants, bool allow_rowid, + uint *cached_field_index_ptr); #ifdef HAVE_OPENSSL #include <openssl/des.h> struct st_des_keyblock @@ -890,8 +891,10 @@ create_field * new_create_field(THD *thd, char *field_name, enum_field_types typ uint uint_geom_type); void store_position_for_column(const char *name); bool add_to_list(THD *thd, SQL_LIST &list,Item *group,bool asc); +Name_resolution_context *make_join_on_context(THD *thd, TABLE_LIST *left_op, + TABLE_LIST *right_op); void add_join_on(TABLE_LIST *b,Item *expr); -void add_join_natural(TABLE_LIST *a,TABLE_LIST *b); +void add_join_natural(TABLE_LIST *a,TABLE_LIST *b,List<String> *using_fields); bool add_proc_to_list(THD *thd, Item *item); TABLE *unlink_open_table(THD *thd,TABLE *list,TABLE *find); @@ -908,8 +911,8 @@ bool insert_fields(THD *thd, Name_resolution_context *context, const char *db_name, const char *table_name, List_iterator<Item> *it, bool any_privileges); bool setup_tables(THD *thd, Name_resolution_context *context, - TABLE_LIST *tables, Item **conds, - TABLE_LIST **leaves, bool select_insert); + List<TABLE_LIST> *from_clause, TABLE_LIST *tables, + Item **conds, TABLE_LIST **leaves, bool select_insert); int setup_wild(THD *thd, TABLE_LIST *tables, List<Item> &fields, List<Item> *sum_func_list, uint wild_num); bool setup_fields(THD *thd, Item** ref_pointer_array, diff --git a/sql/sp.cc b/sql/sp.cc index 5dd7c613a10..a46ebca3fa7 100644 --- a/sql/sp.cc +++ b/sql/sp.cc @@ -789,6 +789,7 @@ db_show_routine_status(THD *thd, int type, const char *wild) */ thd->lex->select_lex.context.resolve_in_table_list_only(&tables); setup_tables(thd, &thd->lex->select_lex.context, + &thd->lex->select_lex.top_join_list, &tables, 0, &leaves, FALSE); for (used_field= &used_fields[0]; used_field->field_name; @@ -798,7 +799,7 @@ db_show_routine_status(THD *thd, int type, const char *wild) "mysql", "proc", used_field->field_name); if (!field || - !(used_field->field= find_field_in_tables(thd, field, &tables, + !(used_field->field= find_field_in_tables(thd, field, &tables, NULL, 0, REPORT_ALL_ERRORS, 1, TRUE))) { diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc index 1b3d8cc914a..12eb7a5d84b 100644 --- a/sql/sql_acl.cc +++ b/sql/sql_acl.cc @@ -2638,10 +2638,11 @@ bool mysql_table_grant(THD *thd, TABLE_LIST *table_list, while ((column = column_iter++)) { uint unused_field_idx= NO_CACHED_FIELD_INDEX; - Field *f=find_field_in_table(thd, table_list, column->column.ptr(), - column->column.ptr(), - column->column.length(), 0, 1, 1, 0, - &unused_field_idx, FALSE); + TABLE_LIST *dummy; + Field *f=find_field_in_table_ref(thd, table_list, column->column.ptr(), + column->column.ptr(), NULL, NULL, + column->column.length(), 0, 1, 1, 0, + &unused_field_idx, FALSE, &dummy); if (f == (Field*)0) { my_error(ER_BAD_FIELD_ERROR, MYF(0), @@ -3695,6 +3696,24 @@ ulong get_table_grant(THD *thd, TABLE_LIST *table) } +/* + Determine the access priviliges for a field. + + SYNOPSIS + get_column_grant() + thd thread handler + grant grants table descriptor + db_name name of database that the field belongs to + table_name name of table that the field belongs to + field_name name of field + + DESCRIPTION + The procedure may also modify: grant->grant_table and grant->version. + + RETURN + The access priviliges for the field db_name.table_name.field_name +*/ + ulong get_column_grant(THD *thd, GRANT_INFO *grant, const char *db_name, const char *table_name, const char *field_name) diff --git a/sql/sql_base.cc b/sql/sql_base.cc index b7b1fb7ff13..18ceed3975b 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -1668,7 +1668,7 @@ static int open_unireg_entry(THD *thd, TABLE *entry, const char *db, { /* Give right error message */ thd->clear_error(); - DBUG_PRINT("error", ("Dicovery of %s/%s failed", db, name)); + DBUG_PRINT("error", ("Discovery of %s/%s failed", db, name)); my_printf_error(ER_UNKNOWN_ERROR, "Failed to open '%-.64s', error while " "unpacking from engine", @@ -2472,152 +2472,255 @@ bool rm_temporary_table(enum db_type base, char *path) /***************************************************************************** -** find field in list or tables. if field is unqualifed and unique, -** return unique field +* The following find_field_in_XXX procedures implement the core of the +* name resolution functionality. The entry point to resolve a column name in a +* list of tables is 'find_field_in_tables'. It calls 'find_field_in_table_ref' +* for each table reference. In turn, depending on the type of table reference, +* 'find_field_in_table_ref' calls one of the 'find_field_in_XXX' procedures +* below specific for the type of table reference. ******************************************************************************/ -/* Special Field pointers for find_field_in_tables returning */ +/* Special Field pointers as return values of find_field_in_XXX functions. */ Field *not_found_field= (Field*) 0x1; Field *view_ref_found= (Field*) 0x2; #define WRONG_GRANT (Field*) -1 +static void update_field_dependencies(THD *thd, Field *field, TABLE *table) +{ + if (thd->set_query_id) + { + if (field->query_id != thd->query_id) + { + field->query_id= thd->query_id; + table->used_fields++; + table->used_keys.intersect(field->part_of_key); + } + else + thd->dupp_field= field; + } +} + /* - Find field in table or view + Find a field by name in a view that uses merge algorithm. SYNOPSIS - find_field_in_table() + find_field_in_view() thd thread handler - table_list table where to find + table_list view to search for 'name' name name of field item_name name of item if it will be created (VIEW) length length of name - ref [in/out] expression substituted in VIEW should be - passed using this reference (return - view_ref_found) - (*ref != NULL) only if *ref contains - the item that we need to replace. - check_grants_table do check columns grants for table? - check_grants_view do check columns grants for view? - allow_rowid do allow finding of "_rowid" field? - cached_field_index_ptr cached position in field list (used to - speedup prepared tables field finding) + ref expression substituted in VIEW should be passed + using this reference (return view_ref_found) + check_grants do check columns grants for view? register_tree_change TRUE if ref is not stack variable and we - need register changes in item tree + need register changes in item tree RETURN 0 field is not found view_ref_found found value in VIEW (real result is in *ref) - # pointer to field + # pointer to field - only for schema table fields */ -Field * -find_field_in_table(THD *thd, TABLE_LIST *table_list, - const char *name, const char *item_name, - uint length, Item **ref, - bool check_grants_table, bool check_grants_view, - bool allow_rowid, - uint *cached_field_index_ptr, - bool register_tree_change) +static Field * +find_field_in_view(THD *thd, TABLE_LIST *table_list, + const char *name, const char *item_name, + uint length, Item **ref, bool check_grants, + bool register_tree_change) { - Field *fld; - DBUG_ENTER("find_field_in_table"); - DBUG_PRINT("enter", ("table: '%s' name: '%s' item name: '%s' ref 0x%lx", - table_list->alias, name, item_name, (ulong) ref)); - if (table_list->field_translation) + DBUG_ENTER("find_field_in_view"); + DBUG_PRINT("enter", + ("view: '%s', field name: '%s', item name: '%s', ref 0x%lx", + table_list->alias, name, item_name, (ulong) ref)); + Field_iterator_view field_it; + field_it.set(table_list); + DBUG_ASSERT(table_list->schema_table_reformed || + (ref != 0 && table_list->view != 0)); + for (; !field_it.end_of_fields(); field_it.next()) { - Field_iterator_view field_it; - field_it.set(table_list); - DBUG_ASSERT(table_list->schema_table_reformed || - (ref != 0 && table_list->view != 0)); - for (; !field_it.end_of_fields(); field_it.next()) + if (!my_strcasecmp(system_charset_info, field_it.name(), name)) { - if (!my_strcasecmp(system_charset_info, field_it.name(), name)) - { - if (table_list->schema_table_reformed) - { - /* - Translation table items are always Item_fields - and fixed already('mysql_schema_table' function). - So we can return ->field. It is used only for - 'show & where' commands. - */ - DBUG_RETURN(((Item_field*) (field_it.item()))->field); - } + if (table_list->schema_table_reformed) + /* + Translation table items are always Item_fields and fixed already + ('mysql_schema_table' function). So we can return ->field. It is + used only for 'show & where' commands. + */ + DBUG_RETURN(((Item_field*) (field_it.item()))->field); #ifndef NO_EMBEDDED_ACCESS_CHECKS - if (check_grants_view && - check_grant_column(thd, &table_list->grant, - table_list->view_db.str, - table_list->view_name.str, - name, length)) - DBUG_RETURN(WRONG_GRANT); + if (check_grants && + check_grant_column(thd, &table_list->grant, + table_list->view_db.str, + table_list->view_name.str, + name, length)) + DBUG_RETURN(WRONG_GRANT); #endif - Item *item= field_it.create_item(thd); - if (!item) - { - DBUG_RETURN(0); - } - /* - *ref != NULL means that *ref contains the item that we need to - replace. If the item was aliased by the user, set the alias to - the replacing item. - */ - if (*ref && !(*ref)->is_autogenerated_name) - item->set_name((*ref)->name, (*ref)->name_length, - system_charset_info); - if (register_tree_change) - thd->change_item_tree(ref, item); - else - *ref= item; - DBUG_RETURN((Field*) view_ref_found); + Item *item= field_it.create_item(thd); + if (!item) + DBUG_RETURN(0); + /* + *ref != NULL means that *ref contains the item that we need to + replace. If the item was aliased by the user, set the alias to + the replacing item. + */ + if (*ref && !(*ref)->is_autogenerated_name) + item->set_name((*ref)->name, (*ref)->name_length, + system_charset_info); + if (register_tree_change) + thd->change_item_tree(ref, item); + else + *ref= item; + DBUG_RETURN((Field*) view_ref_found); + } + } + DBUG_RETURN(0); +} + + +/* + Find field by name in a NATURAL/USING join table reference. + + SYNOPSIS + find_field_in_natural_join() + thd [in] thread handler + table_ref [in] table reference to search + name [in] name of field + table_name [in] optional table name that qualifies the field + db_name [in] optional database name that qualifies the field + length [in] length of name + ref [in/out] if 'name' is resolved to a view field, ref is + set to point to the found view field + check_grants [in] do check columns grants? + register_tree_change [in] TRUE if ref is not stack variable and we + need register changes in item tree + actual_table [out] the original table reference where the field + belongs - differs from 'table_list' only for + NATURAL/USING joins + + RETURN + - Pointer to the found Field + - NULL if the field was not found + - WRONG_GRANT if no access rights to the found field +*/ + +static Field * +find_field_in_natural_join(THD *thd, TABLE_LIST *table_ref, const char *name, + const char *table_name, const char *db_name, + uint length, Item **ref, bool check_grants, + bool register_tree_change, TABLE_LIST **actual_table) +{ + DBUG_ENTER("find_field_in_natural_join"); + DBUG_PRINT("enter", ("natural join, field name: '%s', ref 0x%lx", + name, (ulong) ref)); + DBUG_ASSERT(table_ref->is_natural_join && table_ref->join_columns); + List_iterator_fast<Natural_join_column> + field_it(*(table_ref->join_columns)); + Natural_join_column *nj_col= NULL; + Field *found_field= NULL; + + *actual_table= NULL; + + while ((nj_col= field_it++)) + { + if (table_name) + { + /* + Coalesced columns cannot be qualified unless this is the execute phase + of prepared statements. The reason is that they do not belong to any + table, but for PS the prepare phase already resolves and stores + items, so during the execution phase we resolve fully qualified items. + */ + if (!thd->current_arena->is_stmt_execute() && nj_col->is_coalesced) + continue; + if (table_name[0] && + my_strcasecmp(table_alias_charset, nj_col->table_name(), table_name)) + continue; + if (db_name && db_name[0]) + { + const char *cur_db_name= nj_col->db_name(); + if (cur_db_name && cur_db_name && strcmp(db_name, cur_db_name)) + continue; } } - DBUG_RETURN(0); + + if (!my_strcasecmp(system_charset_info, nj_col->name(), name)) + break; } - fld= find_field_in_real_table(thd, table_list->table, name, length, - check_grants_table, allow_rowid, - cached_field_index_ptr); + + if (!nj_col) + DBUG_RETURN(NULL); + #ifndef NO_EMBEDDED_ACCESS_CHECKS - /* check for views with temporary table algorithm */ - if (check_grants_view && table_list->view && - fld && fld != WRONG_GRANT && - check_grant_column(thd, &table_list->grant, - table_list->view_db.str, - table_list->view_name.str, - name, length)) - { + if (check_grants && nj_col->check_grants(thd, name, length)) DBUG_RETURN(WRONG_GRANT); - } #endif - DBUG_RETURN(fld); + + if (nj_col->view_field) + { + /* + The found field is a view field, we do as in find_field_in_view() + and return a pointer to pointer to the Item of that field. + */ + Item *item= nj_col->create_item(thd); + if (!item) + DBUG_RETURN(NULL); + DBUG_ASSERT(nj_col->table_field == NULL); + if (nj_col->table_ref->schema_table_reformed) + /* + Translation table items are always Item_fields and fixed + already('mysql_schema_table' function). So we can return + ->field. It is used only for 'show & where' commands. + */ + DBUG_RETURN(((Item_field*) (nj_col->view_field->item))->field); + + if (register_tree_change) + thd->change_item_tree(ref, item); + else + *ref= item; + found_field= (Field*) view_ref_found; + } + else + { + /* This is a base table. */ + DBUG_ASSERT(nj_col->view_field == NULL); + found_field= nj_col->table_field; + update_field_dependencies(thd, found_field, nj_col->table_ref->table); + } + + *actual_table= nj_col->table_ref; + + DBUG_RETURN(found_field); } /* - Find field in table + Find field by name in a base table or a view with temp table algorithm. SYNOPSIS - find_field_in_real_table() + find_field_in_table() thd thread handler - table_list table where to find + table table where to search for the field name name of field length length of name check_grants do check columns grants? allow_rowid do allow finding of "_rowid" field? - cached_field_index_ptr cached position in field list (used to - speedup prepared tables field finding) + cached_field_index_ptr cached position in field list (used to speedup + lookup for fields in prepared tables) RETURN 0 field is not found # pointer to field */ -Field *find_field_in_real_table(THD *thd, TABLE *table, - const char *name, uint length, - bool check_grants, bool allow_rowid, - uint *cached_field_index_ptr) +Field * +find_field_in_table(THD *thd, TABLE *table, const char *name, uint length, + bool check_grants, bool allow_rowid, + uint *cached_field_index_ptr) { + DBUG_ENTER("find_field_in_table"); + DBUG_PRINT("enter", ("table: '%s', field name: '%s'", table->alias, name)); Field **field_ptr, *field; uint cached_field_index= *cached_field_index_ptr; @@ -2632,7 +2735,7 @@ Field *find_field_in_real_table(THD *thd, TABLE *table, else { if (!(field_ptr= table->field)) - return (Field *)0; + DBUG_RETURN((Field *)0); for (; *field_ptr; ++field_ptr) if (!my_strcasecmp(system_charset_info, (*field_ptr)->field_name, name)) break; @@ -2648,27 +2751,118 @@ Field *find_field_in_real_table(THD *thd, TABLE *table, if (!allow_rowid || my_strcasecmp(system_charset_info, name, "_rowid") || !(field=table->rowid_field)) - return (Field*) 0; + DBUG_RETURN((Field*) 0); } - if (thd->set_query_id) - { - if (field->query_id != thd->query_id) - { - field->query_id=thd->query_id; - table->used_fields++; - table->used_keys.intersect(field->part_of_key); - } - else - thd->dupp_field=field; - } + update_field_dependencies(thd, field, table); + #ifndef NO_EMBEDDED_ACCESS_CHECKS if (check_grants && check_grant_column(thd, &table->grant, table->s->db, table->s->table_name, name, length)) - return WRONG_GRANT; + DBUG_RETURN(WRONG_GRANT); +#endif + DBUG_RETURN(field); +} + + +/* + Find field in a table reference. + + SYNOPSIS + find_field_in_table_ref() + thd [in] thread handler + table_list [in] table reference to search + name [in] name of field + item_name [in] name of item if it will be created (VIEW) + table_name [in] optional table name that qualifies the field + db_name [in] optional database name that qualifies the + length [in] field length of name + ref [in/out] if 'name' is resolved to a view field, ref + is set to point to the found view field + check_grants_table [in] do check columns grants for table? + check_grants_view [in] do check columns grants for view? + allow_rowid [in] do allow finding of "_rowid" field? + cached_field_index_ptr [in] cached position in field list (used to + speedup lookup for fields in prepared tables) + register_tree_change [in] TRUE if ref is not stack variable and we + need register changes in item tree + actual_table [out] the original table reference where the field + belongs - differs from 'table_list' only for + NATURAL_USING joins. + + RETURN + 0 field is not found + view_ref_found found value in VIEW (real result is in *ref) + # pointer to field +*/ + +Field * +find_field_in_table_ref(THD *thd, TABLE_LIST *table_list, + const char *name, const char *item_name, + const char *table_name, const char *db_name, + uint length, Item **ref, + bool check_grants_table, bool check_grants_view, + bool allow_rowid, uint *cached_field_index_ptr, + bool register_tree_change, TABLE_LIST **actual_table) +{ + Field *fld; + DBUG_ENTER("find_field_in_table_ref"); + DBUG_PRINT("enter", + ("table: '%s' field name: '%s' item name: '%s' ref 0x%lx", + table_list->alias, name, item_name, (ulong) ref)); + + /* + Check that the table and database that qualify the current field name + are the same as the table we are going to search for the field. + This is done differently for NATURAL/USING joins because there we can't + simply compare the qualifying table and database names with the ones of + 'table_list' because each field in such a join may originate from a + different table. + TODO: Ensure that db and tables->db always points to something ! + */ + if (!table_list->is_natural_join && + (table_name && table_name[0] && + my_strcasecmp(table_alias_charset, table_list->alias, table_name) || + (db_name && db_name[0] && table_list->db && table_list->db[0] && + strcmp(db_name, table_list->db)))) + DBUG_RETURN(0); + + if (table_list->field_translation) + { + if ((fld= find_field_in_view(thd, table_list, name, item_name, length, + ref, check_grants_view, register_tree_change))) + *actual_table= table_list; + else + *actual_table= NULL; + } + else if (table_list->is_natural_join) + fld= find_field_in_natural_join(thd, table_list, name, table_name, + db_name, length, ref, + /* TIMOUR_TODO: check this with Sanja */ + check_grants_table || check_grants_view, + register_tree_change, actual_table); + else + { + if ((fld= find_field_in_table(thd, table_list->table, name, length, + check_grants_table, allow_rowid, + cached_field_index_ptr))) + *actual_table= table_list; + else + *actual_table= NULL; +#ifndef NO_EMBEDDED_ACCESS_CHECKS + /* check for views with temporary table algorithm */ + if (check_grants_view && table_list->view && + fld && fld != WRONG_GRANT && + check_grant_column(thd, &table_list->grant, + table_list->view_db.str, + table_list->view_name.str, + name, length)) + DBUG_RETURN(WRONG_GRANT); #endif - return field; + } + + DBUG_RETURN(fld); } @@ -2677,21 +2871,23 @@ Field *find_field_in_real_table(THD *thd, TABLE *table, SYNOPSIS find_field_in_tables() - thd Pointer to current thread structure - item Field item that should be found - tables Tables to be searched for item - ref If 'item' is resolved to a view field, ref is set to + thd pointer to current thread structure + item field item that should be found + first_table list of tables to be searched for item + last_table end of the list of tables to search for item. If NULL + then search to the end of the list 'first_table'. + ref if 'item' is resolved to a view field, ref is set to point to the found view field - report_error Degree of error reporting: + report_error Degree of error reporting: - IGNORE_ERRORS then do not report any error - IGNORE_EXCEPT_NON_UNIQUE report only non-unique - fields, suppress all other errors + 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 - register_tree_change TRUE if ref is not stack variable and we - need register changes in item tree + register_tree_change TRUE if ref is not a stack variable and we + to need register changes in item tree RETURN VALUES 0 If error: the found field is not unique, or there are @@ -2705,63 +2901,66 @@ Field *find_field_in_real_table(THD *thd, TABLE *table, */ Field * -find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables, +find_field_in_tables(THD *thd, Item_ident *item, + TABLE_LIST *first_table, TABLE_LIST *last_table, Item **ref, find_item_error_report_type report_error, bool check_privileges, bool register_tree_change) { Field *found=0; - const char *db=item->db_name; - const char *table_name=item->table_name; - const char *name=item->field_name; + const char *db= item->db_name; + const char *table_name= item->table_name; + const char *name= item->field_name; uint length=(uint) strlen(name); char name_buff[NAME_LEN+1]; bool allow_rowid; + TABLE_LIST *cur_table= first_table; + TABLE_LIST *actual_table; if (item->cached_table) { /* - This shortcut is used by prepared statements. We assuming that - TABLE_LIST *tables is not changed during query execution (which + This shortcut is used by prepared statements. We assume that + TABLE_LIST *first_table is not changed during query execution (which is true for all queries except RENAME but luckily RENAME doesn't use fields...) so we can rely on reusing pointer to its member. With this optimization we also miss case when addition of one more field makes some prepared query ambiguous and so erroneous, but we accept this trade off. */ - if (item->cached_table->table && !item->cached_table->view) - { - found= find_field_in_real_table(thd, item->cached_table->table, - name, length, - test(item->cached_table-> - table->grant.want_privilege) && - check_privileges, - 1, &(item->cached_field_index)); - - } + TABLE_LIST *table_ref= item->cached_table; + /* + The condition (table_ref->view == NULL) ensures that we will call + find_field_in_table even in the case of information schema tables + when table_ref->field_translation != NULL. + */ + if (table_ref->table && !table_ref->view) + found= find_field_in_table(thd, table_ref->table, name, length, + test(table_ref->table-> + grant.want_privilege) && + check_privileges, + 1, &(item->cached_field_index)); else - { - TABLE_LIST *table= item->cached_table; - found= find_field_in_table(thd, table, name, item->name, length, - ref, - (table->table && - test(table->table->grant. - want_privilege) && - check_privileges), - (test(table->grant.want_privilege) && - check_privileges), - 1, &(item->cached_field_index), - register_tree_change); - } + found= find_field_in_table_ref(thd, table_ref, name, item->name, + NULL, NULL, length, ref, + (table_ref->table && + test(table_ref->table->grant. + want_privilege) && + check_privileges), + (test(table_ref->grant.want_privilege) && + check_privileges), + 1, &(item->cached_field_index), + register_tree_change, + &actual_table); if (found) { if (found == WRONG_GRANT) return (Field*) 0; { SELECT_LEX *current_sel= thd->lex->current_select; - SELECT_LEX *last_select= item->cached_table->select_lex; + SELECT_LEX *last_select= table_ref->select_lex; /* If the field was an outer referencee, mark all selects using this - sub query as dependent of the outer query + sub query as dependent on the outer query */ if (current_sel != last_select) mark_select_range_as_dependent(thd, last_select, current_sel, @@ -2783,46 +2982,52 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables, db= name_buff; } + /* The field we search for is qualified with a table name and optional db. */ if (table_name && table_name[0]) - { /* Qualified field */ - bool found_table= 0; - for (; tables; tables= tables->next_local) + { + bool found_table=0; + for ( ; + (cur_table && + (last_table ? + (cur_table != last_table->next_name_resolution_table) : TRUE)); + cur_table= cur_table->next_name_resolution_table) { - /* TODO; Ensure that db and tables->db always points to something ! */ - if (!my_strcasecmp(table_alias_charset, tables->alias, table_name) && - (!db || !db[0] || !tables->db || !tables->db[0] || - !strcmp(db,tables->db))) + DBUG_ASSERT(cur_table); + found_table= 1; + Field *cur_field= find_field_in_table_ref(thd, cur_table, name, + item->name, table_name, + db, length, ref, + (cur_table->table && + test(cur_table->table->grant. + want_privilege) && + check_privileges), + (test(cur_table->grant. + want_privilege) + && check_privileges), + 1, &(item->cached_field_index), + register_tree_change, + &actual_table); + if (cur_field) { - found_table=1; - Field *find= find_field_in_table(thd, tables, name, item->name, - length, ref, - (tables->table && - test(tables->table->grant. - want_privilege) && - check_privileges), - (test(tables->grant.want_privilege) && - check_privileges), - 1, &(item->cached_field_index), - register_tree_change); - if (find) - { - item->cached_table= tables; - if (!tables->cacheable_table) - item->cached_table= 0; - if (find == WRONG_GRANT) - return (Field*) 0; - if (db || !thd->where) - return find; - if (found) - { - if (report_error == REPORT_ALL_ERRORS || - report_error == IGNORE_EXCEPT_NON_UNIQUE) - my_error(ER_NON_UNIQ_ERROR, MYF(0), - item->full_name(),thd->where); - return (Field*) 0; - } - found=find; - } + /* + Store the original table of the field, which may be different from + cur_table in the case of NATURAL/USING join. + */ + item->cached_table= (!actual_table->cacheable_table) ? 0 : actual_table; + + if (cur_field == WRONG_GRANT) + return (Field*) 0; + if (db || !thd->where) + return cur_field; + if (found) + { + if (report_error == REPORT_ALL_ERRORS || + report_error == IGNORE_EXCEPT_NON_UNIQUE) + my_error(ER_NON_UNIQ_ERROR, MYF(0), + item->full_name(),thd->where); + return (Field*) 0; + } + found= cur_field; } } if (found) @@ -2846,34 +3051,41 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables, return (Field*) not_found_field; return (Field*) 0; } - allow_rowid= tables && !tables->next_local; // Only one table - for (; tables ; tables= tables->next_local) - { - Field *field; - if (!tables->table && !tables->ancestor) - { - if (report_error == REPORT_ALL_ERRORS || - report_error == REPORT_EXCEPT_NON_UNIQUE) - my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(),thd->where); - return (Field*) not_found_field; - } - field= find_field_in_table(thd, tables, name, item->name, - length, ref, - (tables->table && - test(tables->table->grant. - want_privilege) && - check_privileges), - (test(tables->grant.want_privilege) && - check_privileges), - allow_rowid, - &(item->cached_field_index), - register_tree_change); - if (field) + /* The field we search for is not qualified. */ + allow_rowid= cur_table && !cur_table->next_local; + for ( ; + (cur_table && + (last_table ? + (cur_table != last_table->next_name_resolution_table) : TRUE)); + cur_table= cur_table->next_name_resolution_table) + { + DBUG_ASSERT(cur_table); + Field *cur_field= find_field_in_table_ref(thd, cur_table, name, item->name, + NULL, NULL, length, ref, + (cur_table->table && + test(cur_table->table->grant. + want_privilege) && + check_privileges), + (test(cur_table->grant. + want_privilege) + && check_privileges), + allow_rowid, + &(item->cached_field_index), + register_tree_change, + &actual_table); + if (cur_field) { - if (field == WRONG_GRANT) + if (cur_field == WRONG_GRANT) return (Field*) 0; - item->cached_table= (!tables->cacheable_table || found) ? 0 : tables; + + /* + Store the original table of the field, which may be different from + cur_table in the case of NATURAL/USING join. + */ + item->cached_table= (!actual_table->cacheable_table || found) ? + 0 : actual_table; + if (found) { if (!thd->where) // Returns first found @@ -2883,7 +3095,7 @@ find_field_in_tables(THD *thd, Item_ident *item, TABLE_LIST *tables, my_error(ER_NON_UNIQ_ERROR, MYF(0), name, thd->where); return (Field*) 0; } - found= field; + found= cur_field; } } if (found) @@ -3097,6 +3309,622 @@ find_item_in_list(Item *find, List<Item> &items, uint *counter, return (Item **) not_found_item; } + +/* + Test if a string is a member of a list of strings. + + SYNOPSIS + test_if_string_in_list() + find the string to look for + str_list a list of strings to be searched + + DESCRIPTION + Sequentially search a list of strings for a string, and test whether + the list contains the same string. + + RETURN + TRUE if find is in str_list + FALSE otherwise +*/ + +static bool +test_if_string_in_list(const char *find, List<String> *str_list) +{ + List_iterator<String> str_list_it(*str_list); + String *curr_str; + size_t find_length= strlen(find); + while ((curr_str= str_list_it++)) + { + if (find_length != curr_str->length()) + continue; + if (!strncmp(find, curr_str->ptr(), find_length)) + return TRUE; + } + return FALSE; +} + + +/* + Create a new name resolution context for an item so that it is + being resolved in a specific table reference. + + SYNOPSIS + set_new_item_local_context() + thd pointer to current thread + item item for which new context is created and set + table_ref table ref where an item showld be resolved + + DESCRIPTION + Create a new name resolution context for an item, so that the item + is resolved only the supplied 'table_ref'. + + RETURN + FALSE - if all OK + TRUE - otherwise +*/ + +static bool +set_new_item_local_context(THD *thd, Item_ident *item, TABLE_LIST *table_ref) +{ + Name_resolution_context *context; + if (!(context= (Name_resolution_context*) + thd->calloc(sizeof(Name_resolution_context)))) + return TRUE; + context->init(); + context->first_name_resolution_table= table_ref; + context->last_name_resolution_table= table_ref; + item->context= context; + return FALSE; +} + + +/* + Find and mark the common columns of two table references. + + SYNOPSIS + mark_common_columns() + thd [in] current thread + table_ref_1 [in] the first (left) join operand + table_ref_2 [in] the second (right) join operand + using_fields [in] if the join is JOIN...USING - the join columns, + if NATURAL join, then NULL + found_using_fields [out] number of fields from the USING clause that were + found among the common fields + + DESCRIPTION + The procedure finds the common columns of two relations (either + tables or intermediate join results), and adds an equi-join condition + to the ON clause of 'table_ref_2' for each pair of matching columns. + If some of table_ref_XXX represents a base table or view, then we + create new 'Natural_join_column' instances for each column + reference and store them in the 'join_columns' of the table + reference. + + IMPLEMENTATION + The procedure assumes that store_natural_using_join_columns() was + called for the previous level of NATURAL/USING joins. + + RETURN + TRUE - if error when some common column is non-unique, or out of memory + FALSE - if OK +*/ + +static bool +mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2, + List<String> *using_fields, uint *found_using_fields) +{ + Field_iterator_table_ref it_1, it_2; + Natural_join_column *nj_col_1, *nj_col_2; + const char *field_name_1, *field_name_2; + *found_using_fields= 0; + bool add_columns= TRUE; + Query_arena *arena, backup; + bool result= TRUE; + + DBUG_ENTER("mark_common_columns"); + DBUG_PRINT("info", ("operand_1: %s, operand_2: %s", + table_ref_1->alias, table_ref_2->alias)); + + arena= thd->change_arena_if_needed(&backup); + + /* + TABLE_LIST::join_columns could be allocated by the previous call to + store_natural_using_join_columns() for the lower level of nested tables. + */ + if (!table_ref_1->join_columns) + { + if (!(table_ref_1->join_columns= new List<Natural_join_column>)) + goto err; + table_ref_1->is_join_columns_complete= FALSE; + } + if (!table_ref_2->join_columns) + { + if (!(table_ref_2->join_columns= new List<Natural_join_column>)) + goto err; + table_ref_2->is_join_columns_complete= FALSE; + } + + for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next()) + { + bool is_created_1; + if (!(nj_col_1= it_1.get_or_create_column_ref(thd, &is_created_1))) + goto err; + field_name_1= nj_col_1->name(); + bool found= FALSE; + + /* If nj_col_1 was just created add it to the list of join columns. */ + if (is_created_1) + table_ref_1->join_columns->push_back(nj_col_1); + + /* Find a field with the same name in table_ref_2. */ + nj_col_2= NULL; + field_name_2= NULL; + for (it_2.set(table_ref_2); !it_2.end_of_fields(); it_2.next()) + { + bool is_created_2; + Natural_join_column *cur_nj_col_2; + if (!(cur_nj_col_2= it_2.get_or_create_column_ref(thd, &is_created_2))) + goto err; + const char *cur_field_name_2= cur_nj_col_2->name(); + + /* If nj_col_1 was just created add it to the list of join columns. */ + if (add_columns && is_created_2) + table_ref_2->join_columns->push_back(cur_nj_col_2); + + /* Compare the two columns and check for duplicate common fields. */ + if (!my_strcasecmp(system_charset_info, field_name_1, cur_field_name_2)) + { + if (found) + { + my_error(ER_NON_UNIQ_ERROR, MYF(0), field_name_1, thd->where); + goto err; + } + nj_col_2= cur_nj_col_2; + field_name_2= cur_field_name_2; + found= TRUE; + } + } + table_ref_2->is_join_columns_complete= TRUE; + add_columns= FALSE; + if (!found) + continue; + + /* + field_1 and field_2 have the same names. Check if they are in the USING + clause (if present), mark them as common fields, and add a new + equi-join condition to the ON clause. + */ + if (nj_col_2 && + (!using_fields || + (using_fields && + test_if_string_in_list(field_name_1, using_fields)))) + { + Item *item_1= nj_col_1->create_item(thd); + Item *item_2= nj_col_2->create_item(thd); + Field *field_1= nj_col_1->field(); + Field *field_2= nj_col_2->field(); + Item_ident *item_ident_1, *item_ident_2; + Name_resolution_context *context_1, *context_2; + DBUG_PRINT("info", ("new equi-join condition: %s.%s = %s.%s", + table_ref_1->alias, field_1->field_name, + table_ref_2->alias, field_2->field_name)); + + /* + The first assert guarantees that the two created items are of + type Item_ident. + */ + DBUG_ASSERT(!thd->lex->current_select->no_wrap_view_item); + /* + In the case of no_wrap_view_item == 0, the created items must be + of sub-classes of Item_ident. + */ + DBUG_ASSERT(item_1->type() == Item::FIELD_ITEM || + item_1->type() == Item::REF_ITEM); + DBUG_ASSERT(item_2->type() == Item::FIELD_ITEM || + item_2->type() == Item::REF_ITEM); + + /* + We need to cast item_1,2 to Item_ident, because we need to hook name + resolution contexts specific to each item. + */ + item_ident_1= (Item_ident*) item_1; + item_ident_2= (Item_ident*) item_2; + /* + Create and hook special name resolution contexts to each item in the + new join condition . We need this to both speed-up subsequent name + resolution of these items, and to enable proper name resolution of + the items during the execute phase of PS. + */ + if (set_new_item_local_context(thd, item_ident_1, table_ref_1)) + goto err; + if (set_new_item_local_context(thd, item_ident_2, table_ref_2)) + goto err; + + Item_func_eq *eq_cond= new Item_func_eq(item_ident_1, item_ident_2); + if (!eq_cond) + goto err; /* Out of memory. */ + + /* + Add the new equi-join condition to the ON clause. Notice that + fix_fields() is applied to all ON conditions in setup_conds() + so we don't do it here. + */ + if (table_ref_1->outer_join & JOIN_TYPE_RIGHT) + add_join_on(table_ref_1, eq_cond); + else + add_join_on(table_ref_2, eq_cond); + + nj_col_1->is_common= nj_col_2->is_common= TRUE; + nj_col_1->is_coalesced= nj_col_2->is_coalesced= TRUE; + + if (field_1) + { + /* Mark field_1 used for table cache. */ + field_1->query_id= thd->query_id; + nj_col_1->table_ref->table->used_keys.intersect(field_1->part_of_key); + } + if (field_2) + { + /* Mark field_2 used for table cache. */ + field_2->query_id= thd->query_id; + nj_col_2->table_ref->table->used_keys.intersect(field_2->part_of_key); + } + + if (using_fields != NULL) + ++(*found_using_fields); + } + } + table_ref_1->is_join_columns_complete= TRUE; + + /* + Everything is OK. + Notice that at this point there may be some column names in the USING + clause that are not among the common columns. This is an SQL error and + we check for this error in store_natural_using_join_columns() when + (found_using_fields < length(join_using_fields)). + */ + result= FALSE; + +err: + if (arena) + thd->restore_backup_item_arena(arena, &backup); + DBUG_RETURN(result); +} + + + +/* + Materialize and store the row type of NATURAL/USING join. + + SYNOPSIS + store_natural_using_join_columns() + thd current thread + natural_using_join the table reference of the NATURAL/USING join + table_ref_1 the first (left) operand (of a NATURAL/USING join). + table_ref_2 the second (right) operand (of a NATURAL/USING join). + using_fields if the join is JOIN...USING - the join columns, + if NATURAL join, then NULL + found_using_fields number of fields from the USING clause that were + found among the common fields + + DESCRIPTION + Iterate over the columns of both join operands and sort and store + all columns into the 'join_columns' list of natural_using_join + where the list is formed by three parts: + part1: The coalesced columns of table_ref_1 and table_ref_2, + sorted according to the column order of the first table. + part2: The other columns of the first table, in the order in + which they were defined in CREATE TABLE. + part3: The other columns of the second table, in the order in + which they were defined in CREATE TABLE. + Time complexity - O(N1+N2), where Ni = length(table_ref_i). + + IMPLEMENTATION + The procedure assumes that mark_common_columns() has been called + for the join that is being processed. + + RETURN + TRUE - if error when some common column is ambiguous + FALSE - if OK +*/ + +static bool +store_natural_using_join_columns(THD *thd, TABLE_LIST *natural_using_join, + TABLE_LIST *table_ref_1, + TABLE_LIST *table_ref_2, + List<String> *using_fields, + uint found_using_fields) +{ + Field_iterator_table_ref it_1, it_2; + Natural_join_column *nj_col_1, *nj_col_2; + bool is_created; + Query_arena *arena, backup; + bool result= TRUE; + + DBUG_ENTER("store_natural_using_join_columns"); + + arena= thd->change_arena_if_needed(&backup); + + List<Natural_join_column> *non_join_columns; + if (!(non_join_columns= new List<Natural_join_column>)) + goto err; + DBUG_ASSERT(!natural_using_join->join_columns); + if (!(natural_using_join->join_columns= new List<Natural_join_column>)) + goto err; + + /* Append the columns of the first join operand. */ + for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next()) + { + if (!(nj_col_1= it_1.get_or_create_column_ref(thd, &is_created))) + goto err; + DBUG_ASSERT(!is_created); + if (nj_col_1->is_common) + { + natural_using_join->join_columns->push_back(nj_col_1); + /* Reset the common columns for the next call to mark_common_columns. */ + nj_col_1->is_common= FALSE; + } + else + non_join_columns->push_back(nj_col_1); + } + + /* + Check that all columns in the USING clause are among the common + columns. If this is not the case, report the first one that was + not found in an error. + */ + if (using_fields && found_using_fields < using_fields->elements) + { + String *using_field_name; + List_iterator_fast<String> using_fields_it(*using_fields); + while ((using_field_name= using_fields_it++)) + { + const char *using_field_name_ptr= using_field_name->ptr(); + List_iterator_fast<Natural_join_column> + it(*(natural_using_join->join_columns)); + Natural_join_column *common_field; + bool found= FALSE; + while ((common_field= it++)) + { + if (!my_strcasecmp(system_charset_info, + common_field->name(), using_field_name_ptr)) + found= TRUE; + } + if (!found) + { + my_error(ER_BAD_FIELD_ERROR, MYF(0), using_field_name_ptr, + current_thd->where); + delete non_join_columns; + goto err; + } + } + } + + /* Append the non-equi-join columns of the second join operand. */ + for (it_2.set(table_ref_2); !it_2.end_of_fields(); it_2.next()) + { + if (!(nj_col_2= it_2.get_or_create_column_ref(thd, &is_created))) + goto err; + DBUG_ASSERT(!is_created); + if (!nj_col_2->is_common) + non_join_columns->push_back(nj_col_2); + else + /* Reset the common columns for the next call to mark_common_columns. */ + nj_col_2->is_common= FALSE; + + } + + if (non_join_columns->elements > 0) + natural_using_join->join_columns->concat(non_join_columns); + else + delete non_join_columns; + natural_using_join->is_join_columns_complete= TRUE; + + + result= FALSE; + +err: + if (arena) + thd->restore_backup_item_arena(arena, &backup); + DBUG_RETURN(result); +} + +/* + Precompute and store the row types of the top-most NATURAL/USING joins. + + SYNOPSIS + store_top_level_join_columns() + thd current thread + table_ref nested join or table in a FROM clause + left_neighbor neighbor table reference to the left of table_ref at the + same level in the join tree + right_neighbor neighbor table reference to the right of table_ref at the + same level in the join tree + + DESCRIPTION + The procedure performs a post-order traversal of a nested join tree + and materializes the row types of NATURAL/USING joins in a + bottom-up manner until it reaches the TABLE_LIST elements that + represent the top-most NATURAL/USING joins. The procedure should be + applied to each element of SELECT_LEX::top_join_list (i.e. to each + top-level element of the FROM clause). + + IMPLEMENTATION + Notice that the table references in the list nested_join->join_list + are in reverse order, thus when we iterate over it, we are moving + from the right to the left in the FROM clause. + + RETURN + TRUE - if error + FALSE - if OK +*/ + +static bool +store_top_level_join_columns(THD *thd, TABLE_LIST *table_ref, + TABLE_LIST *left_neighbor, + TABLE_LIST *right_neighbor) +{ + DBUG_ENTER("store_top_level_join_columns"); + /* Call the procedure recursively for each nested table reference. */ + if (table_ref->nested_join) + { + List_iterator_fast<TABLE_LIST> nested_it(table_ref->nested_join->join_list); + TABLE_LIST *cur_table_ref; + TABLE_LIST *cur_left_neighbor= nested_it++; + TABLE_LIST *cur_right_neighbor= NULL; + while (cur_left_neighbor) + { + cur_table_ref= cur_left_neighbor; + cur_left_neighbor= nested_it++; + if (cur_table_ref->nested_join && + store_top_level_join_columns(thd, cur_table_ref, + cur_left_neighbor, cur_right_neighbor)) + DBUG_RETURN(TRUE); + cur_right_neighbor= cur_table_ref; + } + } + + /* + If this is a NATURAL/USING join, materialize its result columns and + convert to a JOIN ... ON. + */ + if (table_ref->is_natural_join) + { + DBUG_ASSERT(table_ref->nested_join && + table_ref->nested_join->join_list.elements == 2); + List_iterator_fast<TABLE_LIST> operand_it(table_ref->nested_join->join_list); + /* + Notice that the order of join operands depends on whether table_ref + represents a LEFT or a RIGHT join. In a RIGHT join, the operands are + in inverted order. + */ + TABLE_LIST *table_ref_2= operand_it++; /* Second NATURAL join operand.*/ + TABLE_LIST *table_ref_1= operand_it++; /* First NATURAL join operand. */ + TABLE_LIST *last_leaf_on_the_left= NULL; + TABLE_LIST *first_leaf_on_the_right= NULL; + List<String> *using_fields= table_ref->join_using_fields; + uint found_using_fields; + + /* + The two join operands were interchanged in the parser, change the order + back for 'mark_common_columns'. + */ + if (table_ref_2->outer_join & JOIN_TYPE_RIGHT) + swap_variables(TABLE_LIST*, table_ref_1, table_ref_2); + if (mark_common_columns(thd, table_ref_1, table_ref_2, + using_fields, &found_using_fields)) + DBUG_RETURN(TRUE); + + /* + Swap the join operands back, so that we pick the columns of the second + one as the coalesced columns. In this way the coalesced columns are the + same as of an equivalent LEFT JOIN. + */ + if (table_ref_1->outer_join & JOIN_TYPE_RIGHT) + swap_variables(TABLE_LIST*, table_ref_1, table_ref_2); + if (store_natural_using_join_columns(thd, table_ref, table_ref_1, + table_ref_2, using_fields, + found_using_fields)) + DBUG_RETURN(TRUE); + + /* + Change NATURAL JOIN to JOIN ... ON. We do this for both operands + because either one of them or the other is the one with the + natural join flag because RIGHT joins are transformed into LEFT, + and the two tables may be reordered. + */ + table_ref_1->natural_join= table_ref_2->natural_join= NULL; + + /* Change this table reference to become a leaf for name resolution. */ + if (left_neighbor) + { + last_leaf_on_the_left= left_neighbor->last_leaf_for_name_resolution(); + last_leaf_on_the_left->next_name_resolution_table= table_ref; + } + if (right_neighbor) + { + first_leaf_on_the_right= right_neighbor->first_leaf_for_name_resolution(); + table_ref->next_name_resolution_table= first_leaf_on_the_right; + } + else + table_ref->next_name_resolution_table= NULL; + } + DBUG_RETURN(FALSE); +} + + +/* + Compute and store the row types of the top-most NATURAL/USING joins + in a FROM clause. + + SYNOPSIS + setup_natural_join_row_types() + thd current thread + from_clause list of top-level table references in a FROM clause + + DESCRIPTION + Apply the procedure 'store_top_level_join_columns' to each of the + top-level table referencs of the FROM clause. Adjust the list of tables + for name resolution - context->first_name_resolution_table to the + top-most, lef-most NATURAL/USING join. + + IMPLEMENTATION + Notice that the table references in 'from_clause' are in reverse + order, thus when we iterate over it, we are moving from the right + to the left in the FROM clause. + + RETURN + TRUE - if error + FALSE - if OK +*/ +static bool setup_natural_join_row_types(THD *thd, List<TABLE_LIST> *from_clause, + Name_resolution_context *context) +{ + thd->where= "from clause"; + if (from_clause->elements == 0) + return FALSE; /* We come here in the case of UNIONs. */ + + /* For stored procedures do not redo work if already done. */ + if (!context->select_lex->first_execution) + return FALSE; + + List_iterator_fast<TABLE_LIST> table_ref_it(*from_clause); + TABLE_LIST *table_ref; /* Current table reference. */ + /* Table reference to the left of the current. */ + TABLE_LIST *left_neighbor= table_ref_it++; + /* Table reference to the right of the current. */ + TABLE_LIST *right_neighbor= NULL; + + while (left_neighbor) + { + table_ref= left_neighbor; + left_neighbor= table_ref_it++; + if (store_top_level_join_columns(thd, table_ref, + left_neighbor, right_neighbor)) + return TRUE; + if (left_neighbor) + { + TABLE_LIST *first_leaf_on_the_right; + first_leaf_on_the_right= table_ref->first_leaf_for_name_resolution(); + left_neighbor->next_name_resolution_table= first_leaf_on_the_right; + } + right_neighbor= table_ref; + } + + /* + Store the top-most, left-most NATURAL/USING join, so that we start + the search from that one instead of context->table_list. At this point + right_neigbor points to the left-most top-level table reference in the + FROM clause. + */ + DBUG_ASSERT(right_neighbor); + context->first_name_resolution_table= + right_neighbor->first_leaf_for_name_resolution(); + + return FALSE; +} + + /**************************************************************************** ** Expand all '*' in given fields ****************************************************************************/ @@ -3240,9 +4068,7 @@ TABLE_LIST **make_leaves_list(TABLE_LIST **list, TABLE_LIST *tables) for (TABLE_LIST *table= tables; table; table= table->next_local) { if (table->view && table->effective_algorithm == VIEW_ALGORITHM_MERGE) - { list= make_leaves_list(list, table->ancestor); - } else { *list= table; @@ -3259,33 +4085,36 @@ TABLE_LIST **make_leaves_list(TABLE_LIST **list, TABLE_LIST *tables) setup_tables() thd Thread handler context name resolution contest to setup table list there - tables Table list + from_clause Top-level list of table references in the FROM clause + tables Table list (select_lex->table_list) conds Condition of current SELECT (can be changed by VIEW) - leaves List of join table leaves list + leaves List of join table leaves list (select_lex->leaf_tables) refresh It is onle refresh for subquery select_insert It is SELECT ... INSERT command NOTE Check also that the 'used keys' and 'ignored keys' exists and set up the - table structure accordingly - Create leaf tables list + table structure accordingly. + Create a list of leaf tables. For queries with NATURAL/USING JOINs, + compute the row types of the top most natural/using join table references + and link these into a list of table references for name resolution. This has to be called for all tables that are used by items, as otherwise table->map is not set and all Item_field will be regarded as const items. RETURN - FALSE ok; In this case *map will includes the choosed index + FALSE ok; In this case *map will includes the chosen index TRUE error */ bool setup_tables(THD *thd, Name_resolution_context *context, - TABLE_LIST *tables, Item **conds, - TABLE_LIST **leaves, bool select_insert) + List<TABLE_LIST> *from_clause, TABLE_LIST *tables, + Item **conds, TABLE_LIST **leaves, bool select_insert) { uint tablenr= 0; DBUG_ENTER("setup_tables"); - context->table_list= tables; + context->table_list= context->first_name_resolution_table= tables; /* this is used for INSERT ... SELECT. @@ -3357,6 +4186,11 @@ bool setup_tables(THD *thd, Name_resolution_context *context, DBUG_RETURN(1); } } + + /* Precompute and store the row types of NATURAL/USING joins. */ + if (setup_natural_join_row_types(thd, from_clause, context)) + DBUG_RETURN(1); + DBUG_RETURN(0); } @@ -3425,12 +4259,12 @@ insert_fields(THD *thd, Name_resolution_context *context, const char *db_name, const char *table_name, List_iterator<Item> *it, bool any_privileges) { - /* allocate variables on stack to avoid pool alloaction */ - Field_iterator_table table_iter; - Field_iterator_view view_iter; - uint found; + Field_iterator_table_ref field_iterator; + bool found; char name_buff[NAME_LEN+1]; DBUG_ENTER("insert_fields"); + DBUG_PRINT("arena", ("insert_fields: current arena: 0x%lx", + (ulong)thd->current_arena)); if (db_name && lower_case_table_names) { @@ -3444,197 +4278,207 @@ insert_fields(THD *thd, Name_resolution_context *context, const char *db_name, db_name= name_buff; } - found= 0; - for (TABLE_LIST *tables= context->table_list; + found= FALSE; + for (TABLE_LIST *tables= context->first_name_resolution_table; tables; - tables= tables->next_local) + tables= tables->next_name_resolution_table) { - Field_iterator *iterator; - TABLE_LIST *natural_join_table; Field *field; - TABLE_LIST *embedded; - TABLE_LIST *last; - TABLE_LIST *embedding; TABLE *table= tables->table; - if (!table_name || (!my_strcasecmp(table_alias_charset, table_name, - tables->alias) && - (!db_name || !strcmp(tables->db,db_name)))) + DBUG_ASSERT(tables->is_leaf_for_name_resolution()); + + /* + If optional table and db names do not match the ones used to qualify + the field being expanded, skip this table reference. However if this is + a NATURAL/USING join, we can't simply skip the whole table reference, + because its columns may come from different tables/views. For NATURAL/ + USING joins we perform this test for each column in the loop below. + */ + if (!tables->is_natural_join) { - bool view; + if (table_name && my_strcasecmp(table_alias_charset, table_name, tables->alias) + || + (db_name && strcmp(tables->db,db_name))) + continue; + } + + bool view; #ifndef NO_EMBEDDED_ACCESS_CHECKS - /* Ensure that we have access right to all columns */ - if (!((table && (table->grant.privilege & SELECT_ACL) || - tables->view && (tables->grant.privilege & SELECT_ACL))) && - !any_privileges) - { - if (tables->view) - { - view_iter.set(tables); - if (check_grant_all_columns(thd, SELECT_ACL, &tables->grant, - tables->view_db.str, - tables->view_name.str, - &view_iter)) - goto err; - } - else if (!tables->schema_table) - { - DBUG_ASSERT(table != 0); - table_iter.set(tables); - if (check_grant_all_columns(thd, SELECT_ACL, &table->grant, - table->s->db, - table->s->table_name, - &table_iter)) - goto err; - } - } + /* Ensure that we have access rights to all fields to be inserted. */ + if (!((table && (table->grant.privilege & SELECT_ACL) || + tables->view && (tables->grant.privilege & SELECT_ACL))) && + !any_privileges) + { + field_iterator.set(tables); + if (check_grant_all_columns(thd, SELECT_ACL, field_iterator.grant(), + field_iterator.db_name(), field_iterator.table_name(), + &field_iterator)) + DBUG_RETURN(TRUE); + } #endif - natural_join_table= 0; - last= embedded= tables; - while ((embedding= embedded->embedding) && - embedding->join_list->elements != 1) - { - TABLE_LIST *next; - List_iterator_fast<TABLE_LIST> it(embedding->nested_join->join_list); - last= it++; - while ((next= it++)) - last= next; - if (last != tables) - break; - embedded= embedding; - } - if (tables == last && - !embedded->outer_join && - embedded->natural_join && - !embedded->natural_join->outer_join) + /* + Update the tables used in the query based on the referenced fields. For + views and natural joins this update is performed inside the loop below. + */ + if (table) + thd->used_tables|= table->map; + + /* + Initialize a generic field iterator for the current table reference. + Notice that it is guaranteed that this iterator will iterate over the + fields of a single table reference, because 'tables' is a leaf (for + name resolution purposes). + */ + field_iterator.set(tables); + + for (; !field_iterator.end_of_fields(); field_iterator.next()) + { + Item *not_used_item; + uint not_used_field_index= NO_CACHED_FIELD_INDEX; + const char *field_name= field_iterator.name(); + Item *item; + + /* If this is a column of a NATURAL/USING join, and the star was qualified + with a table (and database) name, check if the column is not a coalesced + one, and if not, that is belongs to the same table. + */ + if (tables->is_natural_join && table_name) { - embedding= embedded->natural_join; - while (embedding->nested_join) - embedding= embedding->nested_join->join_list.head(); - natural_join_table= embedding; + if (field_iterator.is_coalesced() + || + my_strcasecmp(table_alias_charset, table_name, field_iterator.table_name()) + || + (db_name && strcmp(db_name, field_iterator.db_name()))) + continue; } - if (tables->field_translation) + + if (!(item= field_iterator.create_item(thd))) + DBUG_RETURN(TRUE); + + if (!found) { - iterator= &view_iter; - view= 1; + it->replace(item); /* Replace '*' with the first found item. */ + found= TRUE; } else + it->after(item); /* Add 'item' to the SELECT list. */ + +#ifndef NO_EMBEDDED_ACCESS_CHECKS + /* + Set privilege information for the fields of newly created views. + We have that (any_priviliges == TRUE) if and only if we are creating + a view. In the time of view creation we can't use the MERGE algorithm, + therefore if 'tables' is itself a view, it is represented by a temporary + table. Thus in this case we can be sure that 'item' is an Item_field. + */ + if (any_privileges) { - iterator= &table_iter; - view= 0; + DBUG_ASSERT(tables->field_translation == NULL && table || + tables->is_natural_join); + DBUG_ASSERT(item->type() == Item::FIELD_ITEM); + Item_field *fld= (Item_field*) item; + const char *table_name= field_iterator.table_name(); + if (!tables->schema_table && + !(fld->have_privileges= + (get_column_grant(thd, field_iterator.grant(), + field_iterator.db_name(), + table_name, fld->field_name) & + VIEW_ANY_ACL))) + { + my_error(ER_COLUMNACCESS_DENIED_ERROR, MYF(0), "ANY", + thd->priv_user, thd->host_or_ip, + fld->field_name, table_name); + DBUG_RETURN(TRUE); + } } - iterator->set(tables); - - /* for view used tables will be collected in following loop */ - if (table) - thd->used_tables|= table->map; +#endif - for (; !iterator->end_of_fields(); iterator->next()) + if ((field= field_iterator.field())) { - Item *not_used_item; - uint not_used_field_index= NO_CACHED_FIELD_INDEX; - const char *field_name= iterator->name(); - /* Skip duplicate field names if NATURAL JOIN is used */ - if (!natural_join_table || - !find_field_in_table(thd, natural_join_table, field_name, - field_name, - strlen(field_name), ¬_used_item, 0, 0, 0, - ¬_used_field_index, TRUE)) + /* + Mark if field used before in this select. + Used by 'insert' to verify if a field name is used twice. + */ + if (field->query_id == thd->query_id) + thd->dupp_field= field; + field->query_id= thd->query_id; + + if (table) + table->used_keys.intersect(field->part_of_key); + + if (tables->is_natural_join) { - Item *item= iterator->create_item(thd); - if (!item) - goto err; - thd->used_tables|= item->used_tables(); - if (!found++) - (void) it->replace(item); // Replace '*' - else - it->after(item); -#ifndef NO_EMBEDDED_ACCESS_CHECKS - if (any_privileges) + bool is_created; + TABLE *field_table; + /* + In this case we are shure that the column ref will not be created + because it was already created and stored with the natural join. + */ + Natural_join_column *nj_col; + if (!(nj_col= field_iterator.get_or_create_column_ref(thd, &is_created))) + DBUG_RETURN(TRUE); + DBUG_ASSERT(nj_col->table_field && !is_created); + field_table= nj_col->table_ref->table; + if (field_table) { - /* - In time of view creation MEGRGE algorithm for underlying - VIEWs can't be used => it should be Item_field - */ - DBUG_ASSERT(item->type() == Item::FIELD_ITEM); - Item_field *fld= (Item_field*)item; - char *db, *tab; - if (tables->view) - { - db= tables->view_db.str; - tab= tables->view_name.str; - } - else - { - db= tables->db; - tab= tables->table_name; - } - if (!tables->schema_table && - !(fld->have_privileges= (get_column_grant(thd, - &table->grant, - db, - tab, - fld->field_name) & - VIEW_ANY_ACL))) - { - my_error(ER_COLUMNACCESS_DENIED_ERROR, MYF(0), - "ANY", - thd->priv_user, - thd->host_or_ip, - fld->field_name, - tab); - goto err; - } + thd->used_tables|= field_table->map; + field_table->used_keys.intersect(field->part_of_key); + field_table->used_fields++; } -#endif - } - if ((field= iterator->field())) - { - /* - Mark if field used before in this select. - Used by 'insert' to verify if a field name is used twice - */ - if (field->query_id == thd->query_id) - thd->dupp_field=field; - field->query_id=thd->query_id; - table->used_keys.intersect(field->part_of_key); - } - else - { - Item *item= ((Field_iterator_view *) iterator)->item(); - item->walk(&Item::reset_query_id_processor, - (byte *)(&thd->query_id)); } } - /* - All fields are used in case if usual tables (in case of view used - fields marked in setup_tables during fix_fields of view columns - */ - if (table) - table->used_fields= table->s->fields; + else + { + thd->used_tables|= item->used_tables(); + item->walk(&Item::reset_query_id_processor, + (byte *)(&thd->query_id)); + } } + /* + In case of stored tables, all fields are considered as used, + while in the case of views, the fields considered as used are the + ones marked in setup_tables during fix_fields of view columns. + For NATURAL joins, used_tables is updated in the IF above. + */ + if (table) + table->used_fields= table->s->fields; } if (found) - DBUG_RETURN(0); + DBUG_RETURN(FALSE); + /* + TODO: in the case when we skipped all columns because there was a qualified + '*', and all columns were coalesced, we have to give a more meaningful message + than ER_BAD_TABLE_ERROR. + */ if (!table_name) my_message(ER_NO_TABLES_USED, ER(ER_NO_TABLES_USED), MYF(0)); else my_error(ER_BAD_TABLE_ERROR, MYF(0), table_name); -err: - DBUG_RETURN(1); + + DBUG_RETURN(TRUE); } /* - Fix all conditions and outer join expressions + Fix all conditions and outer join expressions. SYNOPSIS setup_conds() thd thread handler - leaves list of leaves of join table tree + tables list of tables for name resolving (select_lex->table_list) + leaves list of leaves of join table tree (select_lex->leaf_tables) + conds WHERE clause + + DESCRIPTION + TODO + + RETURN + TRUE if some error occured (e.g. out of memory) + FALSE if all is OK */ int setup_conds(THD *thd, TABLE_LIST *tables, TABLE_LIST *leaves, @@ -3676,11 +4520,14 @@ int setup_conds(THD *thd, TABLE_LIST *tables, TABLE_LIST *leaves, goto err_no_arena; } - /* Check if we are using outer joins */ + /* + Apply fix_fields() to all ON clauses at all levels of nesting, + including the ones inside view definitions. + */ for (table= leaves; table; table= table->next_leaf) { - TABLE_LIST *embedded; - TABLE_LIST *embedding= table; + TABLE_LIST *embedded; /* The table at the current level of nesting. */ + TABLE_LIST *embedding= table; /* The parent nested table reference. */ do { embedded= embedding; @@ -3694,144 +4541,6 @@ int setup_conds(THD *thd, TABLE_LIST *tables, TABLE_LIST *leaves, goto err_no_arena; select_lex->cond_count++; } - - if (embedded->natural_join) - { - /* Make a join of all fields wich have the same name */ - TABLE_LIST *tab1= embedded; - TABLE_LIST *tab2= embedded->natural_join; - if (!(embedded->outer_join & JOIN_TYPE_RIGHT)) - { - while (tab1->nested_join) - { - TABLE_LIST *next; - List_iterator_fast<TABLE_LIST> it(tab1->nested_join->join_list); - tab1= it++; - while ((next= it++)) - tab1= next; - } - } - else - { - while (tab1->nested_join) - tab1= tab1->nested_join->join_list.head(); - } - if (embedded->outer_join & JOIN_TYPE_RIGHT) - { - while (tab2->nested_join) - { - TABLE_LIST *next; - List_iterator_fast<TABLE_LIST> it(tab2->nested_join->join_list); - tab2= it++; - while ((next= it++)) - tab2= next; - } - } - else - { - while (tab2->nested_join) - tab2= tab2->nested_join->join_list.head(); - } - - if (arena) - arena= thd->change_arena_if_needed(&backup); - - TABLE *t1=tab1->table; - TABLE *t2=tab2->table; - Field_iterator_table table_iter; - Field_iterator_view view_iter; - Field_iterator *iterator; - Field *t1_field, *t2_field; - Item *item_t2= 0; - Item_cond_and *cond_and= new Item_cond_and(); - - if (!cond_and) // If not out of memory - goto err_no_arena; - cond_and->top_level_item(); - - if (table->field_translation) - { - iterator= &view_iter; - view_iter.set(tab1); - } - else - { - iterator= &table_iter; - table_iter.set(tab1); - } - - for (; !iterator->end_of_fields(); iterator->next()) - { - const char *t1_field_name= iterator->name(); - uint not_used_field_index= NO_CACHED_FIELD_INDEX; - - if ((t2_field= find_field_in_table(thd, tab2, t1_field_name, - t1_field_name, - strlen(t1_field_name), &item_t2, - 0, 0, 0, - ¬_used_field_index, - FALSE))) - { - if (t2_field != view_ref_found) - { - if (!(item_t2= new Item_field(thd, &select_lex->context, - t2_field))) - goto err; - /* Mark field used for table cache */ - t2_field->query_id= thd->query_id; - t2->used_keys.intersect(t2_field->part_of_key); - } - if ((t1_field= iterator->field())) - { - /* Mark field used for table cache */ - t1_field->query_id= thd->query_id; - t1->used_keys.intersect(t1_field->part_of_key); - } - Item_func_eq *tmp= new Item_func_eq(iterator->create_item(thd), - item_t2); - if (!tmp) - goto err; - cond_and->list.push_back(tmp); - } - } - select_lex->cond_count+= cond_and->list.elements; - - // to prevent natural join processing during PS re-execution - embedding->natural_join= 0; - - if (cond_and->list.elements) - { - COND *on_expr= cond_and; - if (!on_expr->fixed) - on_expr->fix_fields(thd, &on_expr); - if (!embedded->outer_join) // Not left join - { - *conds= and_conds(*conds, cond_and); - // fix_fields() should be made with temporary memory pool - if (arena) - thd->restore_backup_item_arena(arena, &backup); - if (*conds && !(*conds)->fixed) - { - if ((*conds)->fix_fields(thd, conds)) - goto err_no_arena; - } - } - else - { - embedded->on_expr= and_conds(embedded->on_expr, cond_and); - // fix_fields() should be made with temporary memory pool - if (arena) - thd->restore_backup_item_arena(arena, &backup); - if (embedded->on_expr && !embedded->on_expr->fixed) - { - if (embedded->on_expr->fix_fields(thd, &embedded->on_expr)) - goto err_no_arena; - } - } - } - else if (arena) - thd->restore_backup_item_arena(arena, &backup); - } embedding= embedded->embedding; } while (embedding && diff --git a/sql/sql_class.h b/sql/sql_class.h index 5d3361d6c51..cdd655aec4c 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -1225,6 +1225,12 @@ public: ulonglong limit_found_rows; ha_rows cuted_fields, sent_row_count, examined_row_count; + /* + The set of those tables whose fields are referenced in all subqueries + of the query. + TODO: possibly this it is incorrect to have used tables in THD because + with more than one subquery, it is not clear what does the field mean. + */ table_map used_tables; USER_CONN *user_connect; CHARSET_INFO *db_charset; diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index b6183bf5acb..334a013ca6e 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -301,6 +301,7 @@ bool mysql_prepare_delete(THD *thd, TABLE_LIST *table_list, Item **conds) DBUG_ENTER("mysql_prepare_delete"); if (setup_tables(thd, &thd->lex->select_lex.context, + &thd->lex->select_lex.top_join_list, table_list, conds, &select_lex->leaf_tables, FALSE) || setup_conds(thd, table_list, select_lex->leaf_tables, conds) || @@ -359,6 +360,7 @@ bool mysql_multi_delete_prepare(THD *thd) lex->query_tables also point on local list of DELETE SELECT_LEX */ if (setup_tables(thd, &thd->lex->select_lex.context, + &thd->lex->select_lex.top_join_list, lex->query_tables, &lex->select_lex.where, &lex->select_lex.leaf_tables, FALSE)) DBUG_RETURN(TRUE); diff --git a/sql/sql_help.cc b/sql/sql_help.cc index 6780beec258..0a89c3a29d7 100644 --- a/sql/sql_help.cc +++ b/sql/sql_help.cc @@ -75,7 +75,7 @@ enum enum_used_fields RETURN VALUES 0 all ok - 1 one of the fileds didn't finded + 1 one of the fileds was not found */ static bool init_fields(THD *thd, TABLE_LIST *tables, @@ -90,7 +90,7 @@ static bool init_fields(THD *thd, TABLE_LIST *tables, Item_field *field= new Item_field(context, "mysql", find_fields->table_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, NULL, 0, REPORT_ALL_ERRORS, 1, TRUE))) DBUG_RETURN(1); @@ -631,12 +631,15 @@ bool mysqld_help(THD *thd, const char *mask) tables[0].alias= tables[0].table_name= (char*) "help_topic"; tables[0].lock_type= TL_READ; tables[0].next_global= tables[0].next_local= &tables[1]; + tables[0].next_name_resolution_table= tables[0].next_local; tables[1].alias= tables[1].table_name= (char*) "help_category"; tables[1].lock_type= TL_READ; tables[1].next_global= tables[1].next_local= &tables[2]; + tables[1].next_name_resolution_table= tables[1].next_local; tables[2].alias= tables[2].table_name= (char*) "help_relation"; tables[2].lock_type= TL_READ; tables[2].next_global= tables[2].next_local= &tables[3]; + tables[2].next_name_resolution_table= tables[2].next_local; tables[3].alias= tables[3].table_name= (char*) "help_keyword"; tables[3].lock_type= TL_READ; tables[0].db= tables[1].db= tables[2].db= tables[3].db= (char*) "mysql"; @@ -655,6 +658,7 @@ bool mysqld_help(THD *thd, const char *mask) tables do not contain VIEWs => we can pass 0 as conds */ setup_tables(thd, &thd->lex->select_lex.context, + &thd->lex->select_lex.top_join_list, tables, 0, &leaves, FALSE); memcpy((char*) used_fields, (char*) init_used_fields, sizeof(used_fields)); if (init_fields(thd, tables, used_fields, array_elements(used_fields))) diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index f9923283911..62b96d567c3 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -106,12 +106,15 @@ static int check_insert_fields(THD *thd, TABLE_LIST *table_list, } else { // Part field list - Name_resolution_context *context= &thd->lex->select_lex.context; - TABLE_LIST *save_next= table_list->next_local, - *save_context= context->table_list; - bool save_resolve_in_select_list= - thd->lex->select_lex.context.resolve_in_select_list; + SELECT_LEX *select_lex= &thd->lex->select_lex; + Name_resolution_context *context= &select_lex->context; + TABLE_LIST *save_next_local; + TABLE_LIST *save_table_list; + TABLE_LIST *save_first_name_resolution_table; + TABLE_LIST *save_next_name_resolution_table; + bool save_resolve_in_select_list; int res; + if (fields.elements != values.elements) { my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L); @@ -119,17 +122,39 @@ static int check_insert_fields(THD *thd, TABLE_LIST *table_list, } thd->dupp_field=0; - thd->lex->select_lex.no_wrap_view_item= TRUE; - /* fields only from first table */ + select_lex->no_wrap_view_item= TRUE; + + /* Save the state of the current name resolution context. */ + save_table_list= context->table_list; + save_first_name_resolution_table= context->first_name_resolution_table; + save_next_name_resolution_table= (context->first_name_resolution_table) ? + context->first_name_resolution_table-> + next_name_resolution_table : + NULL; + save_resolve_in_select_list= context->resolve_in_select_list; + save_next_local= table_list->next_local; + + /* + Perform name resolution only in the first table - 'table_list', + which is the table that is inserted into. + */ table_list->next_local= 0; context->resolve_in_table_list_only(table_list); res= setup_fields(thd, 0, fields, 1, 0, 0); - table_list->next_local= save_next; + + /* Restore the current context. */ + table_list->next_local= save_next_local; + context->table_list= save_table_list; + context->first_name_resolution_table= save_first_name_resolution_table; + if (context->first_name_resolution_table) + context->first_name_resolution_table-> + next_name_resolution_table= save_next_name_resolution_table; + context->resolve_in_select_list= save_resolve_in_select_list; thd->lex->select_lex.no_wrap_view_item= FALSE; - context->table_list= save_context; - context->resolve_in_select_list= save_resolve_in_select_list; + if (res) return -1; + if (table_list->effective_algorithm == VIEW_ALGORITHM_MERGE) { /* it is join view => we need to find table for update */ @@ -254,9 +279,13 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, ulonglong id; COPY_INFO info; TABLE *table= 0; - TABLE_LIST *next_local; + TABLE_LIST *save_table_list; + TABLE_LIST *save_next_local; + TABLE_LIST *save_first_name_resolution_table; + TABLE_LIST *save_next_name_resolution_table; List_iterator_fast<List_item> its(values_list); List_item *values; + Name_resolution_context *context; #ifndef EMBEDDED_LIBRARY char *query= thd->query; #endif @@ -335,9 +364,23 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, /* mysql_prepare_insert set table_list->table if it was not set */ table= table_list->table; - next_local= table_list->next_local; + context= &thd->lex->select_lex.context; + /* Save the state of the current name resolution context. */ + save_table_list= context->table_list; + save_first_name_resolution_table= context->first_name_resolution_table; + save_next_name_resolution_table= (context->first_name_resolution_table) ? + context->first_name_resolution_table-> + next_name_resolution_table : + NULL; + save_next_local= table_list->next_local; + + /* + Perform name resolution only in the first table - 'table_list', + which is the table that is inserted into. + */ table_list->next_local= 0; - thd->lex->select_lex.context.resolve_in_table_list_only(table_list); + context->resolve_in_table_list_only(table_list); + value_count= values->elements; while ((values= its++)) { @@ -351,7 +394,14 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, goto abort; } its.rewind (); - table_list->next_local= next_local; + + /* Restore the current context. */ + table_list->next_local= save_next_local; + context->first_name_resolution_table= save_first_name_resolution_table; + if (context->first_name_resolution_table) + context->first_name_resolution_table-> + next_name_resolution_table= save_next_name_resolution_table; + /* Fill in the given fields and dump it to the table file */ @@ -707,6 +757,7 @@ static bool mysql_prepare_insert_check_table(THD *thd, TABLE_LIST *table_list, DBUG_ENTER("mysql_prepare_insert_check_table"); if (setup_tables(thd, &thd->lex->select_lex.context, + &thd->lex->select_lex.top_join_list, table_list, where, &thd->lex->select_lex.leaf_tables, select_insert)) DBUG_RETURN(TRUE); @@ -761,10 +812,13 @@ bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list, COND **where, bool select_insert) { SELECT_LEX *select_lex= &thd->lex->select_lex; + Name_resolution_context *context= &select_lex->context; TABLE_LIST *save_table_list; TABLE_LIST *save_next_local; + TABLE_LIST *save_first_name_resolution_table; + TABLE_LIST *save_next_name_resolution_table; + bool save_resolve_in_select_list; bool insert_into_view= (table_list->view != 0); - bool save_resolve_in_select_list; bool res= 0; DBUG_ENTER("mysql_prepare_insert"); DBUG_PRINT("enter", ("table_list 0x%lx, table 0x%lx, view %d", @@ -802,35 +856,57 @@ bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list, select_insert)) DBUG_RETURN(TRUE); - save_table_list= select_lex->context.table_list; - save_resolve_in_select_list= select_lex->context.resolve_in_select_list; - save_next_local= table_list->next_local; + /* Save the state of the current name resolution context. */ + save_table_list= context->table_list; + /* Here first_name_resolution_table points to the first select table. */ + save_first_name_resolution_table= context->first_name_resolution_table; + save_next_name_resolution_table= (context->first_name_resolution_table) ? + context->first_name_resolution_table-> + next_name_resolution_table : + NULL; + save_resolve_in_select_list= context->resolve_in_select_list; + save_next_local= table_list->next_local; + /* + Perform name resolution only in the first table - 'table_list', + which is the table that is inserted into. + */ table_list->next_local= 0; - select_lex->context.resolve_in_table_list_only(table_list); + context->resolve_in_table_list_only(table_list); + + /* Prepare the fields in the statement. */ if (values && - !(res= check_insert_fields(thd, table_list, fields, *values, - !insert_into_view) || - setup_fields(thd, 0, *values, 0, 0, 0)) && + !(res= check_insert_fields(thd, context->table_list, fields, *values, + !insert_into_view) || + setup_fields(thd, 0, *values, 0, 0, 0)) && duplic == DUP_UPDATE) { select_lex->no_wrap_view_item= TRUE; - res= check_update_fields(thd, table_list, update_fields); + res= check_update_fields(thd, context->table_list, update_fields); select_lex->no_wrap_view_item= FALSE; + /* + When we are not using GROUP BY we can refer to other tables in the + ON DUPLICATE KEY part. + */ if (select_lex->group_list.elements == 0) { - /* - When we are not using GROUP BY we can refer to other tables in the - ON DUPLICATE KEY part - */ - table_list->next_local= save_next_local; + context->table_list->next_local= save_next_local; + context->first_name_resolution_table-> + next_name_resolution_table= save_next_local; } if (!res) res= setup_fields(thd, 0, update_values, 1, 0, 0); } + + /* Restore the current context. */ table_list->next_local= save_next_local; - select_lex->context.table_list= save_table_list; - select_lex->context.resolve_in_select_list= save_resolve_in_select_list; + context->table_list= save_table_list; + context->first_name_resolution_table= save_first_name_resolution_table; + if (context->first_name_resolution_table) + context->first_name_resolution_table-> + next_name_resolution_table= save_next_name_resolution_table; + context->resolve_in_select_list= save_resolve_in_select_list; + if (res) DBUG_RETURN(res); @@ -2091,24 +2167,52 @@ select_insert::prepare(List<Item> &values, SELECT_LEX_UNIT *u) res= check_insert_fields(thd, table_list, *fields, values, !insert_into_view) || setup_fields(thd, 0, values, 0, 0, 0); + if (info.handle_duplicates == DUP_UPDATE) { - TABLE_LIST *save_next_local= table_list->next_local; + /* Save the state of the current name resolution context. */ + Name_resolution_context *context= &lex->select_lex.context; + TABLE_LIST *save_table_list; + TABLE_LIST *save_next_local; + TABLE_LIST *save_first_name_resolution_table; + TABLE_LIST *save_next_name_resolution_table; + save_table_list= context->table_list; + save_first_name_resolution_table= context->first_name_resolution_table; + save_next_name_resolution_table= (context->first_name_resolution_table) ? + context->first_name_resolution_table-> + next_name_resolution_table : + NULL; + save_next_local= table_list->next_local; + + /* Perform name resolution only in the first table - 'table_list'. */ table_list->next_local= 0; - lex->select_lex.context.resolve_in_table_list_only(table_list); + context->resolve_in_table_list_only(table_list); + lex->select_lex.no_wrap_view_item= TRUE; - res= res || check_update_fields(thd, table_list, *info.update_fields); + res= res || check_update_fields(thd, context->table_list, + *info.update_fields); lex->select_lex.no_wrap_view_item= FALSE; - /* When we are not using GROUP BY we can refer to other tables in the ON DUPLICATE KEY part */ - if (!lex->select_lex.group_list.elements) - table_list->next_local= save_next_local; + if (lex->select_lex.group_list.elements == 0) + { + context->table_list->next_local= save_next_local; + context->first_name_resolution_table-> + next_name_resolution_table= save_next_local; + } res= res || setup_fields(thd, 0, *info.update_values, 1, 0, 0); + + /* Restore the current context. */ table_list->next_local= save_next_local; + context->first_name_resolution_table= save_first_name_resolution_table; + if (context->first_name_resolution_table) + context->first_name_resolution_table-> + next_name_resolution_table= save_next_name_resolution_table; + } + lex->current_select= lex_current_select_save; if (res) DBUG_RETURN(1); diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 674d9302c72..86e677cc8dc 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -118,8 +118,11 @@ void lex_start(THD *thd, uchar *buf,uint length) lex->buf= lex->ptr= buf; lex->end_of_query= buf+length; + lex->context_stack.empty(); lex->unit.init_query(); lex->unit.init_select(); + /* 'parent_lex' is used in init_query() so it must be before it. */ + lex->select_lex.parent_lex= lex; lex->select_lex.init_query(); lex->value_list.empty(); lex->update_list.empty(); @@ -148,7 +151,6 @@ void lex_start(THD *thd, uchar *buf,uint length) lex->leaf_tables_insert= lex->query_tables= 0; lex->query_tables_last= &lex->query_tables; lex->variables_used= 0; - lex->select_lex.parent_lex= lex; lex->empty_field_list_on_rset= 0; lex->select_lex.select_number= 1; lex->next_state=MY_LEX_START; @@ -1114,6 +1116,11 @@ void st_select_lex::init_query() having_fix_field= 0; context.select_lex= this; context.init(); + /* + Add the name resolution context of the current (sub)query to the + stack of contexts for the whole query. + */ + parent_lex->push_context(&context); cond_count= with_wild= 0; conds_processed_with_permanent_arena= 0; ref_pointer_array= 0; @@ -1130,7 +1137,7 @@ void st_select_lex::init_select() { st_select_lex_node::init_select(); group_list.empty(); - type= db= db1= table1= db2= table2= 0; + type= db= 0; having= 0; use_index_ptr= ignore_index_ptr= 0; table_join_options= 0; @@ -1860,8 +1867,9 @@ TABLE_LIST *st_lex::unlink_first_table(bool *link_to_local) */ if ((*link_to_local= test(select_lex.table_list.first))) { - select_lex.table_list.first= (byte*) (select_lex.context.table_list= - first->next_local); + select_lex.context.table_list= first->next_local; + select_lex.context.first_name_resolution_table= first->next_local; + select_lex.table_list.first= (byte*) (first->next_local); select_lex.table_list.elements--; //safety first->next_local= 0; /* @@ -1966,8 +1974,8 @@ void st_lex::link_first_table_back(TABLE_LIST *first, if (link_to_local) { first->next_local= (TABLE_LIST*) select_lex.table_list.first; - select_lex.table_list.first= - (byte*) (select_lex.context.table_list= first); + select_lex.context.table_list= first; + select_lex.table_list.first= (byte*) first; select_lex.table_list.elements++; //safety } } diff --git a/sql/sql_lex.h b/sql/sql_lex.h index e8c65ba5100..2240f504290 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -472,14 +472,16 @@ class st_select_lex: public st_select_lex_node { public: Name_resolution_context context; - char *db, *db1, *table1, *db2, *table2; /* For outer join using .. */ + char *db; Item *where, *having; /* WHERE & HAVING clauses */ Item *prep_where; /* saved WHERE clause for prepared statement processing */ /* point on lex in which it was created, used in view subquery detection */ st_lex *parent_lex; enum olap_type olap; - SQL_LIST table_list, group_list; /* FROM & GROUP BY clauses */ - List<Item> item_list; /* list of fields & expressions */ + /* FROM clause - points to the beginning of the TABLE_LIST::next_local list. */ + SQL_LIST table_list; + SQL_LIST group_list; /* GROUP BY clause. */ + List<Item> item_list; /* list of fields & expressions */ List<String> interval_list, use_index, *use_index_ptr, ignore_index, *ignore_index_ptr; /* @@ -492,7 +494,12 @@ public: List<TABLE_LIST> top_join_list; /* join list of the top level */ List<TABLE_LIST> *join_list; /* list for the currently parsed join */ TABLE_LIST *embedding; /* table embedding to the above list */ - TABLE_LIST *leaf_tables; /* list of leaves in join table tree */ + /* + Beginning of the list of leaves in a FROM clause, where the leaves + inlcude all base tables including view tables. The tables are connected + by TABLE_LIST::next_leaf, so leaf_tables points to the left-most leaf. + */ + TABLE_LIST *leaf_tables; const char *type; /* type of select for EXPLAIN */ SQL_LIST order_list; /* ORDER clause */ @@ -594,7 +601,6 @@ public: bool init_nested_join(THD *thd); TABLE_LIST *end_nested_join(THD *thd); TABLE_LIST *nest_last_join(THD *thd); - void save_names_for_using_list(TABLE_LIST *tab1, TABLE_LIST *tab2); void add_joined_table(TABLE_LIST *table); TABLE_LIST *convert_right_join(); List<Item>* get_item_list(); @@ -736,6 +742,21 @@ typedef struct st_lex List<set_var_base> var_list; List<Item_param> param_list; List<LEX_STRING> view_list; // view list (list of field names in view) + /* + A stack of name resolution contexts for the query. This stack is used + at parse time to set local name resolution contexts for various parts + of a query. For example, in a JOIN ... ON (some_condition) clause the + Items in 'some_condition' must be resolved only against the operands + of the the join, and not against the whole clause. Similarly, Items in + subqueries should be resolved against the subqueries (and outer queries). + The stack is used in the following way: when the parser detects that + all Items in some clause need a local context, it creates a new context + and pushes it on the stack. All newly created Items always store the + top-most context in the stack. Once the parser leaves the clause that + required a local context, the parser pops the top-most context. + */ + List<Name_resolution_context> context_stack; + SQL_LIST proc_list, auxilliary_table_list, save_list; create_field *last_field; udf_func udf; @@ -927,6 +948,21 @@ typedef struct st_lex return ( query_tables_own_last ? *query_tables_own_last : 0); } void cleanup_after_one_table_open(); + + void push_context(Name_resolution_context *context) + { + context_stack.push_front(context); + } + + void pop_context() + { + context_stack.pop(); + } + + Name_resolution_context *current_context() + { + return context_stack.head(); + } } LEX; struct st_lex_local: public st_lex diff --git a/sql/sql_load.cc b/sql/sql_load.cc index 1ec209aba85..e1684f9bb11 100644 --- a/sql/sql_load.cc +++ b/sql/sql_load.cc @@ -150,6 +150,7 @@ bool mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, if (open_and_lock_tables(thd, table_list)) DBUG_RETURN(TRUE); if (setup_tables(thd, &thd->lex->select_lex.context, + &thd->lex->select_lex.top_join_list, table_list, &unused_conds, &thd->lex->select_lex.leaf_tables, FALSE)) DBUG_RETURN(-1); diff --git a/sql/sql_olap.cc b/sql/sql_olap.cc index 71e8fe4149f..b457ff5a6d6 100644 --- a/sql/sql_olap.cc +++ b/sql/sql_olap.cc @@ -153,7 +153,7 @@ int handle_olaps(LEX *lex, SELECT_LEX *select_lex) List<Item> all_fields(select_lex->item_list); - if (setup_tables(lex->thd, &select_lex->context, + if (setup_tables(lex->thd, &select_lex->context, &select_lex->top_join_list, (TABLE_LIST *)select_lex->table_list.first &select_lex->where, &select_lex->leaf_tables, FALSE) || setup_fields(lex->thd, 0, select_lex->item_list, 1, &all_fields,1) || diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index fefe670432c..4fc11d63370 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -2134,6 +2134,8 @@ int prepare_schema_table(THD *thd, LEX *lex, Table_ident *table_ident, { TABLE_LIST **query_tables_last= lex->query_tables_last; sel= new SELECT_LEX(); + /* 'parent_lex' is used in init_query() so it must be before it. */ + sel->parent_lex= lex; sel->init_query(); if (!sel->add_table_to_list(thd, table_ident, 0, 0, TL_READ, (List<String> *) 0, (List<String> *) 0)) @@ -3237,19 +3239,26 @@ end_with_restore_list: if (!(res= open_and_lock_tables(thd, all_tables))) { /* Skip first table, which is the table we are inserting in */ - select_lex->table_list.first= (byte*)first_table->next_local; - + TABLE_LIST *second_table= first_table->next_local; + select_lex->table_list.first= (byte*) second_table; + select_lex->context.table_list= second_table; + select_lex->context.first_name_resolution_table= second_table; res= mysql_insert_select_prepare(thd); - lex->select_lex.context.table_list= first_table->next_local; if (!res && (result= new select_insert(first_table, first_table->table, &lex->field_list, &lex->update_list, &lex->value_list, lex->duplicates, lex->ignore))) { - /* Skip first table, which is the table we are inserting in */ + /* + Skip first table, which is the table we are inserting in. + Below we set context.table_list again because the call above to + mysql_insert_select_prepare() calls resolve_in_table_list_only(), + which in turn resets context.table_list and + context.first_name_resolution_table. + */ select_lex->context.table_list= first_table->next_local; - + select_lex->context.first_name_resolution_table= first_table->next_local; res= handle_select(thd, lex, result, OPTION_SETUP_TABLES_DONE); delete result; } @@ -5195,9 +5204,9 @@ mysql_new_select(LEX *lex, bool move_down) if (!(select_lex= new (thd->mem_root) SELECT_LEX())) DBUG_RETURN(1); select_lex->select_number= ++thd->select_number; + select_lex->parent_lex= lex; /* Used in init_query. */ select_lex->init_query(); select_lex->init_select(); - select_lex->parent_lex= lex; /* Don't evaluate this subquery during statement prepare even if it's a constant one. The flag is switched off in the end of @@ -5255,6 +5264,7 @@ mysql_new_select(LEX *lex, bool move_down) fake->include_standalone(unit, (SELECT_LEX_NODE**)&unit->fake_select_lex); fake->select_number= INT_MAX; + fake->parent_lex= lex; /* Used in init_query. */ fake->make_empty_select(); fake->linkage= GLOBAL_OPTIONS_TYPE; fake->select_limit= 0; @@ -5263,6 +5273,11 @@ mysql_new_select(LEX *lex, bool move_down) /* allow item list resolving in fake select for ORDER BY */ fake->context.resolve_in_select_list= TRUE; fake->context.select_lex= fake; + /* + Remove the name resolution context of the fake select from the + context stack. + */ + lex->pop_context(); } select_lex->context.outer_context= outer_context; } @@ -5956,6 +5971,7 @@ TABLE_LIST *st_select_lex::add_table_to_list(THD *thd, LEX_STRING *option) { register TABLE_LIST *ptr; + TABLE_LIST *previous_table_ref; /* The table preceding the current one. */ char *alias_str; LEX *lex= thd->lex; DBUG_ENTER("add_table_to_list"); @@ -6051,8 +6067,29 @@ TABLE_LIST *st_select_lex::add_table_to_list(THD *thd, } } } - /* Link table in local list (list for current select) */ + /* Store the table reference preceding the current one. */ + if (table_list.elements > 0) + { + previous_table_ref= (TABLE_LIST*) table_list.next; + DBUG_ASSERT(previous_table_ref); + } + /* + Link the current table reference in a local list (list for current select). + Notice that as a side effect here we set the next_local field of the + previous table reference to 'ptr'. Here we also add one element to the + list 'table_list'. + */ table_list.link_in_list((byte*) ptr, (byte**) &ptr->next_local); + /* + Set next_name_resolution_table of the previous table reference to point to + the current table reference. In effect the list + TABLE_LIST::next_name_resolution_table coincides with + TABLE_LIST::next_local. Later this may be changed in + store_top_level_join_columns() for NATURAL/USING joins. + */ + if (table_list.elements > 1) + previous_table_ref->next_name_resolution_table= ptr; + ptr->next_name_resolution_table= NULL; /* Link table in global list (all used tables) */ lex->add_to_query_tables(ptr); DBUG_RETURN(ptr); @@ -6181,6 +6218,19 @@ TABLE_LIST *st_select_lex::nest_last_join(THD *thd) table->join_list= embedded_list; table->embedding= ptr; embedded_list->push_back(table); + if (table->natural_join) + { + ptr->is_natural_join= TRUE; + /* + If this is a JOIN ... USING, move the list of joined fields to the + table reference that describes the join. + */ + if (table->join_using_fields) + { + ptr->join_using_fields= table->join_using_fields; + table->join_using_fields= NULL; + } + } } join_list->push_front(ptr); nested_join->used_tables= nested_join->not_null_tables= (table_map) 0; @@ -6189,44 +6239,6 @@ TABLE_LIST *st_select_lex::nest_last_join(THD *thd) /* - Save names for a join with using clause - - SYNOPSIS - save_names_for_using_list - tab1 left table in join - tab2 right table in join - - DESCRIPTION - The function saves the full names of the tables in st_select_lex - to be able to build later an on expression to replace the using clause. - - RETURN VALUE - None -*/ - -void st_select_lex::save_names_for_using_list(TABLE_LIST *tab1, - TABLE_LIST *tab2) -{ - while (tab1->nested_join) - { - tab1= tab1->nested_join->join_list.head(); - } - db1= tab1->db; - table1= tab1->alias; - while (tab2->nested_join) - { - TABLE_LIST *next; - List_iterator_fast<TABLE_LIST> it(tab2->nested_join->join_list); - tab2= it++; - while ((next= it++)) - tab2= next; - } - db2= tab2->db; - table2= tab2->alias; -} - - -/* Add a table to the current join list SYNOPSIS @@ -6329,16 +6341,71 @@ void st_select_lex::set_lock_for_tables(thr_lock_type lock_type) } -void add_join_on(TABLE_LIST *b,Item *expr) +/* + Create a new name resolution context for a JOIN ... ON clause. + + SYNOPSIS + make_join_on_context() + thd pointer to current thread + left_op lefto operand of the JOIN + right_op rigth operand of the JOIN + + DESCRIPTION + Create a new name resolution context for a JOIN ... ON clause, + and set the first and last leaves of the list of table references + to be used for name resolution. + + RETURN + A new context if all is OK + NULL - if a memory allocation error occured +*/ + +Name_resolution_context * +make_join_on_context(THD *thd, TABLE_LIST *left_op, TABLE_LIST *right_op) +{ + Name_resolution_context *on_context; + if (!(on_context= (Name_resolution_context*) + thd->calloc(sizeof(Name_resolution_context)))) + return NULL; + on_context->init(); + on_context->first_name_resolution_table= + left_op->first_leaf_for_name_resolution(); + on_context->last_name_resolution_table= + right_op->last_leaf_for_name_resolution(); + return on_context; +} + + +/* + Add an ON condition to the second operand of a JOIN ... ON. + + SYNOPSIS + add_join_on + b the second operand of a JOIN ... ON + expr the condition to be added to the ON clause + + DESCRIPTION + Add an ON condition to the right operand of a JOIN ... ON clause. + + RETURN + FALSE if there was some error + TRUE if all is OK +*/ + +void add_join_on(TABLE_LIST *b, Item *expr) { if (expr) { if (!b->on_expr) - b->on_expr=expr; + b->on_expr= expr; else { - /* This only happens if you have both a right and left join */ - b->on_expr=new Item_cond_and(b->on_expr,expr); + /* + If called from the parser, this happens if you have both a + right and left join. If called later, it happens if we add more + than one condition to the ON clause. + */ + b->on_expr= new Item_cond_and(b->on_expr,expr); } b->on_expr->top_level_item(); } @@ -6346,28 +6413,49 @@ void add_join_on(TABLE_LIST *b,Item *expr) /* - Mark that we have a NATURAL JOIN between two tables + Mark that there is a NATURAL JOIN or JOIN ... USING between two + tables. SYNOPSIS add_join_natural() - a Table to do normal join with - b Do normal join with this table - + a Left join argument + b Right join argument + using_fields Field names from USING clause + IMPLEMENTATION - This function just marks that table b should be joined with a. - The function setup_cond() will create in b->on_expr a list - of equal condition between all fields of the same name. - + This function marks that table b should be joined with a either via + a NATURAL JOIN or via JOIN ... USING. Both join types are special + cases of each other, so we treat them together. The function + setup_conds() creates a list of equal condition between all fields + of the same name for NATURAL JOIN or the fields in 'using_fields' + for JOIN ... USING. The list of equality conditions is stored + either in b->on_expr, or in JOIN::conds, depending on whether there + was an outer join. + + EXAMPLE SELECT * FROM t1 NATURAL LEFT JOIN t2 <=> SELECT * FROM t1 LEFT JOIN t2 ON (t1.i=t2.i and t1.j=t2.j ... ) + + SELECT * FROM t1 NATURAL JOIN t2 WHERE <some_cond> + <=> + SELECT * FROM t1, t2 WHERE (t1.i=t2.i and t1.j=t2.j and <some_cond>) + + SELECT * FROM t1 JOIN t2 USING(j) WHERE <some_cond> + <=> + SELECT * FROM t1, t2 WHERE (t1.j=t2.j and <some_cond>) + + RETURN + None */ -void add_join_natural(TABLE_LIST *a,TABLE_LIST *b) +void add_join_natural(TABLE_LIST *a, TABLE_LIST *b, List<String> *using_fields) { - b->natural_join=a; + b->natural_join= a; + b->join_using_fields= using_fields; } + /* Reload/resets privileges and the different caches. diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 630406dba07..f26656ba609 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -338,7 +338,7 @@ JOIN::prepare(Item ***rref_pointer_array, /* Check that all tables, fields, conds and order are ok */ if ((!(select_options & OPTION_SETUP_TABLES_DONE) && - setup_tables(thd, &select_lex->context, + setup_tables(thd, &select_lex->context, join_list, tables_list, &conds, &select_lex->leaf_tables, FALSE)) || setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) || @@ -11904,13 +11904,14 @@ cp_buffer_from_ref(THD *thd, TABLE_REF *ref) SYNOPSIS find_order_in_list() - thd Pointer to current thread structure - ref_pointer_array All select, group and order by fields - tables List of tables to search in (usually FROM clause) - order Column reference to be resolved - fields List of fields to search in (usually SELECT list) - all_fields All select, group and order by fields - is_group_field True if order is a GROUP field, false if ORDER by field + 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 @@ -11986,7 +11987,7 @@ find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables, order_item_type == Item::REF_ITEM) { from_field= find_field_in_tables(thd, (Item_ident*) order_item, tables, - &view_ref, IGNORE_ERRORS, TRUE, + NULL, &view_ref, IGNORE_ERRORS, TRUE, FALSE); if (!from_field) from_field= (Field*) not_found_field; diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 1c0519475b4..760f5f7c456 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -2072,6 +2072,11 @@ int get_all_tables(THD *thd, TABLE_LIST *tables, COND *cond) else { int res; + /* + Set the parent lex of 'sel' because it is needed by sel.init_query() + which is called inside make_table_list. + */ + sel.parent_lex= lex; if (make_table_list(thd, &sel, base_name, file_name)) goto err; TABLE_LIST *show_table_list= (TABLE_LIST*) sel.table_list.first; diff --git a/sql/sql_update.cc b/sql/sql_update.cc index aa39140f9b1..17efd56372a 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -555,7 +555,7 @@ bool mysql_prepare_update(THD *thd, TABLE_LIST *table_list, tables.table= table; tables.alias= table_list->alias; - if (setup_tables(thd, &select_lex->context, + if (setup_tables(thd, &select_lex->context, &select_lex->top_join_list, table_list, conds, &select_lex->leaf_tables, FALSE) || setup_conds(thd, table_list, select_lex->leaf_tables, conds) || @@ -643,6 +643,7 @@ bool mysql_multi_update_prepare(THD *thd) */ if (setup_tables(thd, &lex->select_lex.context, + &lex->select_lex.top_join_list, table_list, &lex->select_lex.where, &lex->select_lex.leaf_tables, FALSE)) DBUG_RETURN(TRUE); @@ -761,6 +762,7 @@ bool mysql_multi_update_prepare(THD *thd) tbl->cleanup_items(); if (setup_tables(thd, &lex->select_lex.context, + &lex->select_lex.top_join_list, table_list, &lex->select_lex.where, &lex->select_lex.leaf_tables, FALSE) || setup_fields_with_no_wrap(thd, 0, *fields, 1, 0, 0)) diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 849987a267c..3fa015ad623 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -717,7 +717,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); bool_term bool_factor bool_test bool_pri predicate bit_expr bit_term bit_factor value_expr term factor table_wild simple_expr udf_expr - using_list expr_or_default set_expr_or_default interval_expr + expr_or_default set_expr_or_default interval_expr param_marker singlerow_subselect singlerow_subselect_init exists_subselect exists_subselect_init geometry_function signed_literal now_or_signed_literal opt_escape @@ -739,7 +739,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); key_alg opt_btree_or_rtree %type <string_list> - key_usage_list + key_usage_list using_list %type <key_part> key_part @@ -4420,10 +4420,10 @@ simple_expr: my_error(ER_WRONG_COLUMN_NAME, MYF(0), name->str); YYABORT; } - $$= new Item_default_value(&Select->context, $3); + $$= new Item_default_value(Lex->current_context(), $3); } | VALUES '(' simple_ident ')' - { $$= new Item_insert_value(&Select->context, $3); } + { $$= new Item_insert_value(Lex->current_context(), $3); } | FUNC_ARG0 '(' ')' { if (!$1.symbol->create_func) @@ -4714,9 +4714,9 @@ simple_expr: name->init_qname(YYTHD); sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION); if ($5) - $$= new Item_func_sp(&lex->current_select->context, name, *$5); + $$= new Item_func_sp(Lex->current_context(), name, *$5); else - $$= new Item_func_sp(&lex->current_select->context, name); + $$= new Item_func_sp(Lex->current_context(), name); lex->safe_to_cache_query=0; } | IDENT_sys '(' udf_expr_list ')' @@ -4804,9 +4804,9 @@ simple_expr: sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION); if ($3) - $$= new Item_func_sp(&lex->current_select->context, name, *$3); + $$= new Item_func_sp(Lex->current_context(), name, *$3); else - $$= new Item_func_sp(&lex->current_select->context, name); + $$= new Item_func_sp(Lex->current_context(), name); lex->safe_to_cache_query=0; } } @@ -5010,7 +5010,7 @@ sum_expr: { SELECT_LEX *sel= Select; sel->in_sum_expr--; - $$=new Item_func_group_concat(&sel->context, $3, $5, + $$=new Item_func_group_concat(Lex->current_context(), $3, $5, sel->gorder_list, $7); $5->empty(); }; @@ -5146,68 +5146,116 @@ join_table: table_ref normal_join table_ref { YYERROR_UNLESS($1 && ($$=$3)); } | table_ref STRAIGHT_JOIN table_factor { YYERROR_UNLESS($1 && ($$=$3)); $3->straight=1; } - | table_ref normal_join table_ref ON expr - { YYERROR_UNLESS($1 && ($$=$3)); add_join_on($3,$5); } - | table_ref STRAIGHT_JOIN table_factor ON expr - { YYERROR_UNLESS($1 && ($$=$3)); $3->straight=1; add_join_on($3,$5); } + | table_ref normal_join table_ref + ON + { + YYERROR_UNLESS($1 && ($$=$3)); + /* Change the current name resolution context to a local context. */ + Name_resolution_context *on_context; + if (!(on_context= make_join_on_context(YYTHD,$1,$3))) + YYABORT; + Lex->push_context(on_context); + } + expr + { + add_join_on($3,$6); + Lex->pop_context(); + } + | table_ref STRAIGHT_JOIN table_factor + ON + { + YYERROR_UNLESS($1 && ($$=$3)); + /* Change the current name resolution context to a local context. */ + Name_resolution_context *on_context; + if (!(on_context= make_join_on_context(YYTHD,$1,$3))) + YYABORT; + Lex->push_context(on_context); + } + expr + { + $3->straight=1; + add_join_on($3,$6); + Lex->pop_context(); + } | table_ref normal_join table_ref USING { SELECT_LEX *sel= Select; YYERROR_UNLESS($1 && $3); - sel->save_names_for_using_list($1, $3); } '(' using_list ')' - { add_join_on($3,$7); $$=$3; } - - | table_ref LEFT opt_outer JOIN_SYM table_ref ON expr - { YYERROR_UNLESS($1 && $5); add_join_on($5,$7); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; } + { add_join_natural($1,$3,$7); $$=$3; } + | table_ref LEFT opt_outer JOIN_SYM table_ref + ON + { + /* Change the current name resolution context to a local context. */ + Name_resolution_context *on_context; + if (!(on_context= make_join_on_context(YYTHD,$1,$5))) + YYABORT; + Lex->push_context(on_context); + } + expr + { + YYERROR_UNLESS($1 && $5); + add_join_on($5,$8); + Lex->pop_context(); + $5->outer_join|=JOIN_TYPE_LEFT; + $$=$5; + } | table_ref LEFT opt_outer JOIN_SYM table_factor { SELECT_LEX *sel= Select; YYERROR_UNLESS($1 && $5); - sel->save_names_for_using_list($1, $5); } USING '(' using_list ')' - { add_join_on($5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; } + { add_join_natural($1,$5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; } | table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor { YYERROR_UNLESS($1 && $6); - add_join_natural($1,$6); + add_join_natural($1,$6,NULL); $6->outer_join|=JOIN_TYPE_LEFT; $$=$6; } - | table_ref RIGHT opt_outer JOIN_SYM table_ref ON expr + | table_ref RIGHT opt_outer JOIN_SYM table_ref + ON + { + /* Change the current name resolution context to a local context. */ + Name_resolution_context *on_context; + if (!(on_context= make_join_on_context(YYTHD,$1,$5))) + YYABORT; + Lex->push_context(on_context); + } + expr { LEX *lex= Lex; YYERROR_UNLESS($1 && $5); if (!($$= lex->current_select->convert_right_join())) YYABORT; - add_join_on($$, $7); + add_join_on($$, $8); + Lex->pop_context(); } | table_ref RIGHT opt_outer JOIN_SYM table_factor { SELECT_LEX *sel= Select; YYERROR_UNLESS($1 && $5); - sel->save_names_for_using_list($1, $5); } USING '(' using_list ')' { LEX *lex= Lex; if (!($$= lex->current_select->convert_right_join())) YYABORT; - add_join_on($$, $9); + add_join_natural($$,$5,$9); } | table_ref NATURAL RIGHT opt_outer JOIN_SYM table_factor { YYERROR_UNLESS($1 && $6); - add_join_natural($6,$1); + add_join_natural($6,$1,NULL); LEX *lex= Lex; if (!($$= lex->current_select->convert_right_join())) YYABORT; } | table_ref NATURAL JOIN_SYM table_factor - { YYERROR_UNLESS($1 && ($$=$4)); add_join_natural($1,$4); }; + { YYERROR_UNLESS($1 && ($$=$4)); add_join_natural($1,$4,NULL); }; normal_join: @@ -5235,8 +5283,23 @@ table_factor: YYABORT; sel->add_joined_table($$); } - | '{' ident table_ref LEFT OUTER JOIN_SYM table_ref ON expr '}' - { YYERROR_UNLESS($3 && $7); add_join_on($7,$9); $7->outer_join|=JOIN_TYPE_LEFT; $$=$7; } + | '{' ident table_ref LEFT OUTER JOIN_SYM table_ref + ON + { + /* Change the current name resolution context to a local context. */ + Name_resolution_context *on_context; + if (!(on_context= make_join_on_context(YYTHD,$3,$7))) + YYABORT; + Lex->push_context(on_context); + } + expr '}' + { + YYERROR_UNLESS($3 && $7); + add_join_on($7,$10); + Lex->pop_context(); + $7->outer_join|=JOIN_TYPE_LEFT; + $$=$7; + } | select_derived_init get_select_lex select_derived2 { LEX *lex= Lex; @@ -5290,6 +5353,7 @@ table_factor: YYABORT; sel->add_joined_table($$); + lex->pop_context(); } else if ($4 || $6) @@ -5429,32 +5493,18 @@ key_usage_list2: using_list: ident { - SELECT_LEX *sel= Select; - if (!($$= new Item_func_eq(new Item_field(&sel->context, - sel->db1, sel->table1, - $1.str), - new Item_field(&sel->context, - sel->db2, sel->table2, - $1.str)))) + if (!($$= new List<String>)) YYABORT; + $$->push_back(new (YYTHD->mem_root) + String((const char *) $1.str, $1.length, + system_charset_info)); } | using_list ',' ident { - SELECT_LEX *sel= Select; - if (!($$= - new Item_cond_and(new - Item_func_eq(new - Item_field(&sel->context, - sel->db1, - sel->table1, - $3.str), - new - Item_field(&sel->context, - sel->db2, - sel->table2, - $3.str)), - $1))) - YYABORT; + $1->push_back(new (YYTHD->mem_root) + String((const char *) $3.str, $3.length, + system_charset_info)); + $$= $1; }; interval: @@ -6095,7 +6145,7 @@ values: expr_or_default: expr { $$= $1;} - | DEFAULT {$$= new Item_default_value(&Select->context); } + | DEFAULT {$$= new Item_default_value(Lex->current_context()); } ; opt_insert_update: @@ -7056,13 +7106,13 @@ table_wild: ident '.' '*' { SELECT_LEX *sel= Select; - $$ = new Item_field(&sel->context, NullS, $1.str, "*"); + $$ = new Item_field(Lex->current_context(), NullS, $1.str, "*"); sel->with_wild++; } | ident '.' ident '.' '*' { SELECT_LEX *sel= Select; - $$ = new Item_field(&sel->context, (YYTHD->client_capabilities & + $$ = new Item_field(Lex->current_context(), (YYTHD->client_capabilities & CLIENT_NO_SCHEMA ? NullS : $1.str), $3.str,"*"); sel->with_wild++; @@ -7090,8 +7140,8 @@ simple_ident: SELECT_LEX *sel=Select; $$= (sel->parsing_place != IN_HAVING || sel->get_in_sum_expr() > 0) ? - (Item*) new Item_field(&sel->context, NullS, NullS, $1.str) : - (Item*) new Item_ref(&sel->context, NullS, NullS, $1.str); + (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) : + (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str); } } | simple_ident_q { $$= $1; } @@ -7103,8 +7153,8 @@ simple_ident_nospvar: SELECT_LEX *sel=Select; $$= (sel->parsing_place != IN_HAVING || sel->get_in_sum_expr() > 0) ? - (Item*) new Item_field(&sel->context, NullS, NullS, $1.str) : - (Item*) new Item_ref(&sel->context, NullS, NullS, $1.str); + (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) : + (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str); } | simple_ident_q { $$= $1; } ; @@ -7141,7 +7191,7 @@ simple_ident_q: YYABORT; } - if (!(trg_fld= new Item_trigger_field(&lex->current_select->context, + if (!(trg_fld= new Item_trigger_field(Lex->current_context(), new_row ? Item_trigger_field::NEW_ROW: Item_trigger_field::OLD_ROW, @@ -7167,8 +7217,8 @@ simple_ident_q: } $$= (sel->parsing_place != IN_HAVING || sel->get_in_sum_expr() > 0) ? - (Item*) new Item_field(&sel->context, NullS, $1.str, $3.str) : - (Item*) new Item_ref(&sel->context, NullS, $1.str, $3.str); + (Item*) new Item_field(Lex->current_context(), NullS, $1.str, $3.str) : + (Item*) new Item_ref(Lex->current_context(), NullS, $1.str, $3.str); } } | '.' ident '.' ident @@ -7183,8 +7233,8 @@ simple_ident_q: } $$= (sel->parsing_place != IN_HAVING || sel->get_in_sum_expr() > 0) ? - (Item*) new Item_field(&sel->context, NullS, $2.str, $4.str) : - (Item*) new Item_ref(&sel->context, NullS, $2.str, $4.str); + (Item*) new Item_field(Lex->current_context(), NullS, $2.str, $4.str) : + (Item*) new Item_ref(Lex->current_context(), NullS, $2.str, $4.str); } | ident '.' ident '.' ident { @@ -7198,11 +7248,11 @@ simple_ident_q: } $$= (sel->parsing_place != IN_HAVING || sel->get_in_sum_expr() > 0) ? - (Item*) new Item_field(&sel->context, + (Item*) new Item_field(Lex->current_context(), (YYTHD->client_capabilities & CLIENT_NO_SCHEMA ? NullS : $1.str), $3.str, $5.str) : - (Item*) new Item_ref(&sel->context, + (Item*) new Item_ref(Lex->current_context(), (YYTHD->client_capabilities & CLIENT_NO_SCHEMA ? NullS : $1.str), $3.str, $5.str); @@ -7766,8 +7816,7 @@ sys_option_value: it= new Item_null(); } - if (!(trg_fld= new Item_trigger_field(&lex->current_select-> - context, + if (!(trg_fld= new Item_trigger_field(Lex->current_context(), Item_trigger_field::NEW_ROW, $2.base_name.str)) || !(i= new sp_instr_set_trigger_field(lex->sphead-> @@ -8623,7 +8672,12 @@ union_list: lex->current_select->master_unit()->union_distinct= lex->current_select; } - select_init {} + select_init + { + /* Remove from the name resolution context stack the context of the + last select in the union. */ + Lex->pop_context(); + } ; union_opt: @@ -8727,6 +8781,7 @@ subselect_end: ')' { LEX *lex=Lex; + lex->pop_context(); lex->current_select = lex->current_select->return_after_parsing(); }; diff --git a/sql/table.cc b/sql/table.cc index 0324e29abf3..83ec2d64935 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -2147,8 +2147,296 @@ bool st_table_list::set_insert_values(MEM_ROOT *mem_root) } +/* + Test if this is a leaf with respect to name resolution. + + SYNOPSIS + st_table_list::is_leaf_for_name_resolution() + + DESCRIPTION + A table reference is a leaf with respect to name resolution if + it is either a leaf node in a nested join tree (table, view, + schema table, subquery), or an inner node that represents a + NATURAL/USING join, or a nested join with materialized join + columns. + + RETURN + TRUE if a leaf, FALSE otherwise. +*/ +bool st_table_list::is_leaf_for_name_resolution() +{ + return (view || is_natural_join || is_join_columns_complete || + !nested_join); +} + + +/* + Retrieve the first (left-most) leaf in a nested join tree with + respect to name resolution. + + SYNOPSIS + st_table_list::first_leaf_for_name_resolution() + + DESCRIPTION + Given that 'this' is a nested table reference, recursively walk + down the left-most children of 'this' until we reach a leaf + table reference with respect to name resolution. + + IMPLEMENTATION + The left-most child of a nested table reference is the last element + in the list of children because the children are inserted in + reverse order. + + RETURN + - If 'this' is a nested table reference - the left-most child of + the tree rooted in 'this', + - else - 'this' +*/ + +TABLE_LIST *st_table_list::first_leaf_for_name_resolution() +{ + TABLE_LIST *cur_table_ref= this; + TABLE_LIST *next; + NESTED_JOIN *cur_nested_join= nested_join; + + if (this->is_leaf_for_name_resolution()) + return this; + + while (cur_nested_join) + { + List_iterator_fast<TABLE_LIST> it(cur_nested_join->join_list); + cur_table_ref= it++; + /* + If 'this' is a RIGHT JOIN, the operands in 'join_list' are in reverse + order, thus the first operand is already at the front of the list. + */ + if (!(cur_table_ref->outer_join & JOIN_TYPE_RIGHT)) + { + while ((next= it++)) + cur_table_ref= next; + } + if (cur_table_ref->is_leaf_for_name_resolution()) + break; + cur_nested_join= cur_table_ref->nested_join; + } + return cur_table_ref; +} + + +/* + Retrieve the last (right-most) leaf in a nested join tree with + respect to name resolution. + + SYNOPSIS + st_table_list::last_leaf_for_name_resolution() + + DESCRIPTION + Given that 'this' is a nested table reference, recursively walk + down the right-most children of 'this' until we reach a leaf + table reference with respect to name resolution. + + IMPLEMENTATION + The right-most child of a nested table reference is the first + element in the list of children because the children are inserted + in reverse order. + + RETURN + - If 'this' is a nested table reference - the right-most child of + the tree rooted in 'this', + - else - 'this' +*/ + +TABLE_LIST *st_table_list::last_leaf_for_name_resolution() +{ + TABLE_LIST *cur_table_ref= this; + TABLE_LIST *next; + NESTED_JOIN *cur_nested_join= nested_join; + + if (this->is_leaf_for_name_resolution()) + return this; + + while (cur_nested_join) + { + /* + If 'this' is a RIGHT JOIN, the operands in 'join_list' are in reverse + order, thus the last operand is in the end of the list. + */ + if ((cur_table_ref->outer_join & JOIN_TYPE_RIGHT)) + { + List_iterator_fast<TABLE_LIST> it(cur_nested_join->join_list); + cur_table_ref= it++; + while ((next= it++)) + cur_table_ref= next; + } + else + cur_table_ref= cur_nested_join->join_list.head(); + if (cur_table_ref->is_leaf_for_name_resolution()) + break; + cur_nested_join= cur_table_ref->nested_join; + } + return cur_table_ref; +} + + +Natural_join_column::Natural_join_column(Field_translator *field_param, + TABLE_LIST *tab) +{ + DBUG_ASSERT(tab->field_translation); + view_field= field_param; + table_field= NULL; + table_ref= tab; + is_common= FALSE; + is_coalesced= FALSE; +} + + +Natural_join_column::Natural_join_column(Field *field_param, + TABLE_LIST *tab) +{ + table_field= field_param; + view_field= NULL; + table_ref= tab; + is_common= FALSE; + is_coalesced= FALSE; +} + + +const char *Natural_join_column::name() +{ + if (view_field) + { + DBUG_ASSERT(table_field == NULL); + return view_field->name; + } + else + { + DBUG_ASSERT(view_field == NULL); + return table_field->field_name; + } +} + + +Item *Natural_join_column::create_item(THD *thd) +{ + if (view_field) + { + DBUG_ASSERT(table_field == NULL); + return create_view_field(thd, table_ref, &view_field->item, view_field->name); + } + else + { + DBUG_ASSERT(view_field == NULL); + return new Item_field(thd, &thd->lex->current_select->context, table_field); + } +} + + +Field *Natural_join_column::field() +{ + if (view_field) + { + DBUG_ASSERT(table_field == NULL); + return NULL; + } + else + { + DBUG_ASSERT(view_field == NULL); + return table_field; + } +} + + +const char *Natural_join_column::table_name() +{ + return table_ref->alias; +/* + TODO: I think that it is sufficient to return just + table->alias, which is correctly set to either + the view name, the table name, or the alias to + the table reference (view or stored table). + if (view_field) + return table_ref->view_name.str; + else + { + DBUG_ASSERT(!strcmp(table_ref->table_name, + table_ref->table->s->table_name)); + return table_ref->table_name; + } +*/ +} + + +const char *Natural_join_column::db_name() +{ + if (view_field) + return table_ref->view_db.str; + else + { + DBUG_ASSERT(!strcmp(table_ref->db, + table_ref->table->s->db)); + return table_ref->db; + } +} + + +GRANT_INFO *Natural_join_column::grant() +{ + if (view_field) + return &(table_ref->grant); + else + return &(table_ref->table->grant); +} + + + +#ifndef NO_EMBEDDED_ACCESS_CHECKS + +/* + Check the access rights for the current join column. + columns. + + SYNOPSIS + Natural_join_column::check_grants() + + DESCRIPTION + Check the access rights to a column from a natural join in a generic + way that hides the heterogeneity of the column representation - whether + it is a view or a stored table colum. + + RETURN + FALSE - if the column can be accessed + TRUE - if there are no access rights to all equivalent columns +*/ + +bool +Natural_join_column::check_grants(THD *thd, const char *name, uint length) +{ + GRANT_INFO *grant= NULL; /* If NULL do not check access rights. */ + const char *db_name; + const char *table_name; + if (view_field) + { + DBUG_ASSERT(table_field == NULL); + grant= &(table_ref->grant); + db_name= table_ref->view_db.str; + table_name= table_ref->view_name.str; + } + else + { + DBUG_ASSERT(table_field && view_field == NULL); + grant= &(table_ref->table->grant); + db_name= table_ref->table->s->db; + table_name= table_ref->table->s->table_name; + } + + return check_grant_column(thd, grant, db_name, table_name, name, length); +} +#endif + + void Field_iterator_view::set(TABLE_LIST *table) { + DBUG_ASSERT(table->field_translation); view= table; ptr= table->field_translation; array_end= table->field_translation_end; @@ -2218,6 +2506,191 @@ Item *create_view_field(THD *thd, TABLE_LIST *view, Item **field_ref, } +void Field_iterator_natural_join::set(TABLE_LIST *table_ref) +{ + DBUG_ASSERT(table_ref->join_columns); + delete column_ref_it; + + /* + TODO: try not to allocate new iterator every time. If we have to, + then check for out of memory condition. + */ + column_ref_it= new List_iterator_fast<Natural_join_column> + (*(table_ref->join_columns)); + cur_column_ref= (*column_ref_it)++; +} + + +void Field_iterator_table_ref::set_field_iterator() +{ + DBUG_ENTER("Field_iterator_table_ref::set_field_iterator"); + /* + If the table reference we are iterating over is a natural join, or it is + an operand of a natural join, and TABLE_LIST::join_columns contains all + the columns of the join operand, then we pick the columns from + TABLE_LIST::join_columns, instead of the orginial container of the + columns of the join operator. + */ + if (table_ref->is_join_columns_complete) + { + /* Necesary, but insufficient conditions. */ + DBUG_ASSERT(table_ref->is_natural_join || + table_ref->nested_join || + table_ref->join_columns && + /* This is a merge view. */ + ((table_ref->field_translation && + table_ref->join_columns->elements == + (ulong)(table_ref->field_translation_end - + table_ref->field_translation)) || + /* This is stored table or a tmptable view. */ + (!table_ref->field_translation && + table_ref->join_columns->elements == + table_ref->table->s->fields))); + natural_join_it.set(table_ref); + field_it= &natural_join_it; + DBUG_PRINT("info",("field_it for '%s' is Field_iterator_natural_join", + table_ref->table_name)); + } + /* This is a merge view, so use field_translation. */ + else if (table_ref->field_translation) + { + DBUG_ASSERT(table_ref->view && + table_ref->effective_algorithm == VIEW_ALGORITHM_MERGE); + view_field_it.set(table_ref); + field_it= &view_field_it; + DBUG_PRINT("info", ("field_it for '%s' is Field_iterator_view", + table_ref->table_name)); + } + /* This is a base table or stored view. */ + else + { + DBUG_ASSERT(table_ref->table || table_ref->view); + table_field_it.set(table_ref); + field_it= &table_field_it; + DBUG_PRINT("info", ("field_it for '%s' is Field_iterator_table", + table_ref->table_name)); + + } + DBUG_VOID_RETURN; +} + + +void Field_iterator_table_ref::set(TABLE_LIST *table) +{ + DBUG_ASSERT(table); + first_leaf= table->first_leaf_for_name_resolution(); + last_leaf= table->last_leaf_for_name_resolution(); + DBUG_ASSERT(first_leaf && last_leaf); + table_ref= first_leaf; + set_field_iterator(); +} + + +void Field_iterator_table_ref::next() +{ + /* Move to the next field in the current table reference. */ + field_it->next(); + /* + If all fields of the current table reference are exhausted, move to + the next leaf table reference. + */ + if (field_it->end_of_fields() && table_ref != last_leaf) + { + table_ref= table_ref->next_name_resolution_table; + DBUG_ASSERT(table_ref); + set_field_iterator(); + } +} + + +const char *Field_iterator_table_ref::table_name() +{ + if (table_ref->view) + return table_ref->view_name.str; + else if (table_ref->is_natural_join) + return natural_join_it.column_ref()->table_name(); + else + { + DBUG_ASSERT(!strcmp(table_ref->table_name, + table_ref->table->s->table_name)); + return table_ref->table_name; + } +} + + +const char *Field_iterator_table_ref::db_name() +{ + if (table_ref->view) + return table_ref->view_db.str; + else if (table_ref->is_natural_join) + return natural_join_it.column_ref()->db_name(); + else + { + DBUG_ASSERT(!strcmp(table_ref->db, table_ref->table->s->db)); + return table_ref->db; + } +} + + +GRANT_INFO *Field_iterator_table_ref::grant() +{ + if (table_ref->view) + return &(table_ref->grant); + else if (table_ref->is_natural_join) + return natural_join_it.column_ref()->grant(); + else + return &(table_ref->table->grant); +} + + +bool Field_iterator_table_ref::is_coalesced() +{ + if (table_ref->is_natural_join) + return natural_join_it.column_ref()->is_coalesced; + else + return FALSE; +} + +/* + Create new or return existing column reference to a column of a + natural/using join. + + SYNOPSIS + Field_iterator_table_ref::get_or_create_column_ref() + thd [in] pointer to current thread + is_created [out] set to TRUE if the column was created, + FALSE if we return an already created colum + + DESCRIPTION + TODO + + RETURN + Pointer to a column of a natural join (or its operand) + NULL if there was no memory to allocate the column +*/ + +Natural_join_column * +Field_iterator_table_ref::get_or_create_column_ref(THD *thd, bool *is_created) +{ + *is_created= TRUE; + if (field_it == &table_field_it) + return new Natural_join_column(table_field_it.field(), table_ref); + else if (field_it == &view_field_it) + return new Natural_join_column(view_field_it.field_translator(), table_ref); + else + { + /* + This is NATURAL join, we already have created a column reference, + so just return it. + */ + *is_created= FALSE; + Natural_join_column *nj_col= natural_join_it.column_ref(); + DBUG_ASSERT(nj_col); + return nj_col; + } +} + + /***************************************************************************** ** Instansiate templates *****************************************************************************/ diff --git a/sql/table.h b/sql/table.h index 22a35e29e87..bb9de5dc86d 100644 --- a/sql/table.h +++ b/sql/table.h @@ -92,7 +92,10 @@ class Field_timestamp; class Field_blob; class Table_triggers_list; -/* This structure is shared between different table objects */ +/* + This structure is shared between different table objects. There is one + instance of table share per one table in the database. +*/ typedef struct st_table_share { @@ -365,9 +368,86 @@ struct Field_translator }; +/* + Column reference of a NATURAL/USING join. Since column references in + joins can be both from views and stored tables, may point to either a + Field (for tables), or a Field_translator (for views). +*/ + +class Natural_join_column +{ +public: + Field_translator *view_field; /* Column reference of merge view. */ + Field *table_field; /* Column reference of table or temp view. */ + st_table_list *table_ref; /* Original base table/view reference. */ + /* + True if a common join column of two NATURAL/USING join operands. Notice + that when we have a hierarchy of nested NATURAL/USING joins, a column can + be common at some level of nesting but it may not be common at higher + levels of nesting. Thus this flag may change depending on at which level + we are looking at some column. + */ + bool is_common; + /* + A column is coalesced if it was common in some of several nested NATURAL/ + USING joins. We have to know this, because according to ANSI, coalesced + columns cannot be qualified. + */ + bool is_coalesced; +public: + Natural_join_column(Field_translator *field_param, st_table_list *tab); + Natural_join_column(Field *field_param, st_table_list *tab); + const char *name(); + Item *create_item(THD *thd); + Field *field(); + const char *table_name(); + const char *db_name(); + GRANT_INFO *grant(); +#ifndef NO_EMBEDDED_ACCESS_CHECKS + bool check_grants(THD *thd, const char *name, uint length); +#endif +}; + + +/* + Table reference in the FROM clause. + + These table references can be of several types that correspond to + different SQL elements. Below we list all types of TABLE_LISTs with + the necessary conditions to determine when a TABLE_LIST instance + belongs to a certain type. + + 1) table (TABLE_LIST::view == NULL) + - base table + (TABLE_LIST::derived == NULL) + - subquery - TABLE_LIST::table is a temp table + (TABLE_LIST::derived != NULL) + - information schema table + (TABLE_LIST::schema_table != NULL) + NOTICE: for schema tables TABLE_LIST::field_translation may be != NULL + 2) view (TABLE_LIST::view != NULL) + - merge (TABLE_LIST::effective_algorithm == VIEW_ALGORITHM_MERGE) + also (TABLE_LIST::field_translation != NULL) + - tmptable (TABLE_LIST::effective_algorithm == VIEW_ALGORITHM_TMPTABLE) + also (TABLE_LIST::field_translation == NULL) + 3) nested table reference (TABLE_LIST::nested_join != NULL) + - table sequence - e.g. (t1, t2, t3) + TODO: how to distinguish from a JOIN? + - general JOIN + TODO: how to distinguish from a table sequence? + - NATURAL JOIN + (TABLE_LIST::natural_join != NULL) + - JOIN ... USING + (TABLE_LIST::join_using_fields != NULL) +*/ + typedef struct st_table_list { - /* link in a local table list (used by SQL_LIST) */ + /* + List of tables local to a subquery (used by SQL_LIST). Considers + views as leaves (unlike 'next_leaf' below). Created at parse time + in st_select_lex::add_table_to_list() -> table_list.link_in_list(). + */ struct st_table_list *next_local; /* link in a global list of all queries tables */ struct st_table_list *next_global, **prev_global; @@ -375,7 +455,7 @@ typedef struct st_table_list char *option; /* Used by cache index */ Item *on_expr; /* Used with outer join */ /* - The scturcture of ON expression presented in the member above + The structure of ON expression presented in the member above can be changed during certain optimizations. This member contains a snapshot of AND-OR structure of the ON expression made after permanent transformations of the parse tree, and is @@ -384,10 +464,40 @@ typedef struct st_table_list */ Item *prep_on_expr; COND_EQUAL *cond_equal; /* Used with outer join */ - struct st_table_list *natural_join; /* natural join on this table*/ - /* ... join ... USE INDEX ... IGNORE INDEX */ - List<String> *use_index, *ignore_index; - TABLE *table; /* opened table */ + /* + During parsing - left operand of NATURAL/USING join where 'this' is + the right operand. After parsing (this->natural_join == this) iff + 'this' represents a NATURAL or USING join operation. Thus after + parsing 'this' is a NATURAL/USING join iff (natural_join != NULL). + */ + struct st_table_list *natural_join; + /* + True if 'this' represents a nested join that is a NATURAL JOIN. + For one of the operands of 'this', the member 'natural_join' points + to the other operand of 'this'. + */ + bool is_natural_join; + /* Field names in a USING clause for JOIN ... USING. */ + List<String> *join_using_fields; + /* + Explicitly store the result columns of either a NATURAL/USING join or + an operand of such a join. + */ + List<Natural_join_column> *join_columns; + /* TRUE if join_columns contains all columns of this table reference. */ + bool is_join_columns_complete; + + /* + List of nodes in a nested join tree, that should be considered as + leaves with respect to name resolution. The leaves are: views, + top-most nodes representing NATURAL/USING joins, subqueries, and + base tables. All of these TABLE_LIST instances contain a + materialized list of columns. The list is local to a subquery. + */ + struct st_table_list *next_name_resolution_table; + /* Index names in a "... JOIN ... USE/IGNORE INDEX ..." clause. */ + List<String> *use_index, *ignore_index; + TABLE *table; /* opened table */ /* select_result for derived table to pass it from table creation to table filling procedure @@ -404,6 +514,10 @@ typedef struct st_table_list st_select_lex_unit *derived; /* SELECT_LEX_UNIT of derived table */ ST_SCHEMA_TABLE *schema_table; /* Information_schema table */ st_select_lex *schema_select_lex; + /* + True when the view field translation table is used to convert + schema table fields for backwards compatibility with SHOW command. + */ bool schema_table_reformed; TMP_TABLE_PARAM *schema_table_param; /* link to select_lex where this table was used */ @@ -416,7 +530,11 @@ typedef struct st_table_list st_table_list *ancestor; /* most upper view this table belongs to */ st_table_list *belong_to_view; - /* list of join table tree leaves */ + /* + List of all base tables local to a subquery including all view + tables. Unlike 'next_local', this in this list views are *not* + leaves. Created in setup_tables() -> make_leaves_list(). + */ st_table_list *next_leaf; Item *where; /* VIEW WHERE clause condition */ Item *check_option; /* WITH CHECK OPTION condition */ @@ -486,6 +604,9 @@ typedef struct st_table_list bool set_insert_values(MEM_ROOT *mem_root); void hide_view_error(THD *thd); st_table_list *find_underlying_table(TABLE *table); + st_table_list *first_leaf_for_name_resolution(); + st_table_list *last_leaf_for_name_resolution(); + bool is_leaf_for_name_resolution(); inline bool prepare_check_option(THD *thd) { bool res= FALSE; @@ -507,6 +628,10 @@ private: class Item; +/* + Iterator over the fields of a generic table reference. +*/ + class Field_iterator: public Sql_alloc { public: @@ -520,6 +645,11 @@ public: }; +/* + Iterator over the fields of a base table, view with temporary + table, or subquery. +*/ + class Field_iterator_table: public Field_iterator { Field **ptr; @@ -535,6 +665,8 @@ public: }; +/* Iterator over the fields of a merge view. */ + class Field_iterator_view: public Field_iterator { Field_translator *ptr, *array_end; @@ -548,8 +680,71 @@ public: Item *create_item(THD *thd); Item **item_ptr() {return &ptr->item; } Field *field() { return 0; } - inline Item *item() { return ptr->item; } + Field_translator *field_translator() { return ptr; } +}; + + +/* + Field_iterator interface to the list of materialized fields of a + NATURAL/USING join. +*/ + +class Field_iterator_natural_join: public Field_iterator +{ + List_iterator_fast<Natural_join_column> *column_ref_it; + Natural_join_column *cur_column_ref; +public: + Field_iterator_natural_join() :column_ref_it(NULL), cur_column_ref(NULL) {} + ~Field_iterator_natural_join() { delete column_ref_it; } + void set(TABLE_LIST *table); + void next() { cur_column_ref= (*column_ref_it)++; } + bool end_of_fields() { return !cur_column_ref; } + const char *name() { return cur_column_ref->name(); } + Item *create_item(THD *thd) { return cur_column_ref->create_item(thd); } + Field *field() { return cur_column_ref->field(); } + Natural_join_column *column_ref() { return cur_column_ref; } +}; + + +/* + Generic iterator over the fields of an arbitrary table reference. + + DESCRIPTION + This class unifies the various ways of iterating over the columns + of a table reference depending on the type of SQL entity it + represents. If such an entity represents a nested table reference, + this iterator encapsulates the iteration over the columns of the + members of the table reference. + + IMPLEMENTATION + The implementation assumes that all underlying NATURAL/USING table + references already contain their result columns and are linked into + the list TABLE_LIST::next_name_resolution_table. +*/ + +class Field_iterator_table_ref: public Field_iterator +{ + TABLE_LIST *table_ref, *first_leaf, *last_leaf; + Field_iterator_table table_field_it; + Field_iterator_view view_field_it; + Field_iterator_natural_join natural_join_it; + Field_iterator *field_it; + void set_field_iterator(); +public: + Field_iterator_table_ref() :field_it(NULL) {} + void set(TABLE_LIST *table); + void next(); + bool end_of_fields() + { return (table_ref == last_leaf && field_it->end_of_fields()); } + const char *name() { return field_it->name(); } + const char *table_name(); + const char *db_name(); + GRANT_INFO *grant(); + bool is_coalesced(); + Item *create_item(THD *thd) { return field_it->create_item(thd); } + Field *field() { return field_it->field(); } + Natural_join_column *get_or_create_column_ref(THD *thd, bool *is_created); }; diff --git a/tests/mysql_client_test.c b/tests/mysql_client_test.c index 75b41ebe4d1..5558028bb06 100644 --- a/tests/mysql_client_test.c +++ b/tests/mysql_client_test.c @@ -11801,8 +11801,8 @@ static void test_bug6046() stmt= mysql_stmt_init(mysql); - stmt_text= "SELECT t1.a FROM t1 NATURAL JOIN t1 as X1 " - "WHERE t1.b > ? ORDER BY t1.a"; + stmt_text= "SELECT a FROM t1 NATURAL JOIN t1 as X1 " + "WHERE b > ? ORDER BY a"; rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text)); check_execute(stmt, rc); |