Commit f0bd502a authored by unknown's avatar unknown
Browse files

Fixed bug #21646.

Presence of a subquery in the ON expression of a join 
should not block merging the view that contains this join.
Before this patch the such views were converted into 
into temporary table views.


mysql-test/r/view.result:
  Added a test case for bug #21646.
mysql-test/t/view.test:
  Added a test case for bug #21646.
sql/mysql_priv.h:
  Fixed bug #21646.
  Added a new parsing state 'IN_ON', true when
  the parser is in an ON expression of a join.
sql/sql_lex.cc:
  Fixed bug #21646.
  Presence of a subquery in the ON expression of a join 
  should not block merging the view that contains this join.
sql/sql_yacc.yy:
  Fixed bug #21646.
  Added a new parsing state 'IN_ON', true when
  the parser is in an ON expression of a join.
parent 54bb1045
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -2935,4 +2935,18 @@ id select_type table type possible_keys key key_len ref rows Extra
2	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	3	
DROP VIEW v1;
DROP TABLE t1;
CREATE TABLE t1(pk int PRIMARY KEY);
CREATE TABLE t2(pk int PRIMARY KEY, fk int, ver int, org int);
CREATE ALGORITHM=MERGE VIEW v1 AS 
SELECT t1.*
FROM t1 JOIN t2 
ON t2.fk = t1.pk AND 
t2.ver = (SELECT MAX(t.ver) FROM t2 t WHERE t.org = t2.org);
SHOW WARNINGS;
Level	Code	Message
SHOW CREATE VIEW v1;
View	Create View
v1	CREATE ALGORITHM=MERGE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`pk` AS `pk` from (`t1` join `t2` on(((`t2`.`fk` = `t1`.`pk`) and (`t2`.`ver` = (select max(`t`.`ver`) AS `MAX(t.ver)` from `t2` `t` where (`t`.`org` = `t2`.`org`))))))
DROP VIEW v1;
DROP TABLE t1, t2;
End of 5.0 tests.
+18 −0
Original line number Diff line number Diff line
@@ -2850,4 +2850,22 @@ EXPLAIN SELECT * FROM v1 t WHERE t.s1+1 < (SELECT MAX(t1.s1) FROM t1);

DROP VIEW v1;
DROP TABLE t1;

#
# Bug #21646: view qith a subquery in ON expression 
#

CREATE TABLE t1(pk int PRIMARY KEY); 
CREATE TABLE t2(pk int PRIMARY KEY, fk int, ver int, org int);

CREATE ALGORITHM=MERGE VIEW v1 AS 
SELECT t1.*
  FROM t1 JOIN t2 
       ON t2.fk = t1.pk AND 
          t2.ver = (SELECT MAX(t.ver) FROM t2 t WHERE t.org = t2.org);
SHOW WARNINGS;
SHOW CREATE VIEW v1;

DROP VIEW v1;
DROP TABLE t1, t2;
--echo End of 5.0 tests.
+2 −1
Original line number Diff line number Diff line
@@ -446,7 +446,8 @@ enum enum_parsing_place
  NO_MATTER,
  IN_HAVING,
  SELECT_LIST,
  IN_WHERE
  IN_WHERE,
  IN_ON
};

struct st_table;
+2 −1
Original line number Diff line number Diff line
@@ -1710,7 +1710,8 @@ bool st_lex::can_be_merged()
         unit= unit->next_unit())
    {
      if (unit->first_select()->parent_lex == this &&
          (unit->item == 0 || unit->item->place() != IN_WHERE))
          (unit->item == 0 ||
           (unit->item->place() != IN_WHERE && unit->item->place() != IN_ON)))
      {
        selects_allow_merge= 0;
        break;
+8 −0
Original line number Diff line number Diff line
@@ -5212,11 +5212,13 @@ join_table:
            /* Change the current name resolution context to a local context. */
            if (push_new_name_resolution_context(YYTHD, $1, $3))
              YYABORT;
            Select->parsing_place= IN_ON;
          }
          expr
	  {
            add_join_on($3,$6);
            Lex->pop_context();
            Select->parsing_place= NO_MATTER;
          }
        | table_ref STRAIGHT_JOIN table_factor
          ON
@@ -5225,12 +5227,14 @@ join_table:
            /* Change the current name resolution context to a local context. */
            if (push_new_name_resolution_context(YYTHD, $1, $3))
              YYABORT;
            Select->parsing_place= IN_ON;
          }
          expr
          {
            $3->straight=1;
            add_join_on($3,$6);
            Lex->pop_context();
            Select->parsing_place= NO_MATTER;
          }
	| table_ref normal_join table_ref
	  USING
@@ -5254,6 +5258,7 @@ join_table:
            /* Change the current name resolution context to a local context. */
            if (push_new_name_resolution_context(YYTHD, $1, $5))
              YYABORT;
            Select->parsing_place= IN_ON;
          }
          expr
	  {
@@ -5261,6 +5266,7 @@ join_table:
            Lex->pop_context();
            $5->outer_join|=JOIN_TYPE_LEFT;
            $$=$5;
            Select->parsing_place= NO_MATTER;
          }
	| table_ref LEFT opt_outer JOIN_SYM table_factor
	  {
@@ -5285,6 +5291,7 @@ join_table:
            /* Change the current name resolution context to a local context. */
            if (push_new_name_resolution_context(YYTHD, $1, $5))
              YYABORT;
            Select->parsing_place= IN_ON;
          }
          expr
          {
@@ -5293,6 +5300,7 @@ join_table:
              YYABORT;
            add_join_on($$, $8);
            Lex->pop_context();
            Select->parsing_place= NO_MATTER;
          }
	| table_ref RIGHT opt_outer JOIN_SYM table_factor
	  {