bug #16813 (WITH CHECK OPTION fails with UPDATE)

We use the condition from CHECK OPTION twice handling UPDATE command.
First we construnct 'update_cond' AND 'option_cond'
condition to select records to be updated, then we check the
'option_cond' for the updated row.
The problem is that first 'AND' condition is optimized during the 'select'
which can break 'option_cond' structure, so it will be unusable for
the sectond use - to check the updated row.
Possible soultion is either use copy of the condition in the first
use or to make optimization less traumatic for the operands.
I picked the first one. 
parent cf9d1c91
......@@ -2935,4 +2935,14 @@ 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(id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, val INT UNSIGNED NOT NULL);
CREATE VIEW v1 AS SELECT id, val FROM t1 WHERE val >= 1 AND val <= 5 WITH CHECK OPTION;
INSERT INTO v1 (val) VALUES (2);
INSERT INTO v1 (val) VALUES (4);
INSERT INTO v1 (val) VALUES (6);
ERROR HY000: CHECK OPTION failed 'test.v1'
UPDATE v1 SET val=6 WHERE id=2;
ERROR HY000: CHECK OPTION failed 'test.v1'
DROP VIEW v1;
DROP TABLE t1;
End of 5.0 tests.
......@@ -2850,4 +2850,19 @@ EXPLAIN SELECT * FROM v1 t WHERE t.s1+1 < (SELECT MAX(t1.s1) FROM t1);
DROP VIEW v1;
DROP TABLE t1;
#
# Bug #16813 (WITH CHECK OPTION doesn't work with UPDATE)
#
CREATE TABLE t1(id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, val INT UNSIGNED NOT NULL);
CREATE VIEW v1 AS SELECT id, val FROM t1 WHERE val >= 1 AND val <= 5 WITH CHECK OPTION;
INSERT INTO v1 (val) VALUES (2);
INSERT INTO v1 (val) VALUES (4);
-- error 1369
INSERT INTO v1 (val) VALUES (6);
-- error 1369
UPDATE v1 SET val=6 WHERE id=2;
DROP VIEW v1;
DROP TABLE t1;
--echo End of 5.0 tests.
......@@ -1965,12 +1965,13 @@ bool st_table_list::prep_where(THD *thd, Item **conds,
this expression will not be moved to WHERE condition (i.e. will
be clean correctly for PS/SP)
*/
tbl->on_expr= and_conds(tbl->on_expr, where);
tbl->on_expr= and_conds(tbl->on_expr,
where->copy_andor_structure(thd));
break;
}
}
if (tbl == 0)
*conds= and_conds(*conds, where);
*conds= and_conds(*conds, where->copy_andor_structure(thd));
if (arena)
thd->restore_active_arena(arena, &backup);
where_processed= TRUE;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment