Skip to content

Commit 2832b94

Browse files
committed
MDEV-25659 trigger name is empty after upgrade to 10.4
Problem: At some point, we made stored rountines fail at CREATE time instead of execution time in case of this syntax: IF unknown_variable ... END IF As a result, a trigger created before this change and contained an unknown variable worked in a bad way after upgrade: - It was displayed with an empty trigger name by SHOW CREATE TRIGGER - It was displayed with an empty trigger name by INFORMATION_SCHEMA.TRIGGERS - An attempt to DROP this trigger returned errors - nothing happened. - DROP TABLE did not remove the .TRN file corresponding to this broken trigger. Underlying code observations: The old code assumed that the trigger name resides in the current lex: if(thd->lex->spname) m_trigger_name= &thd->lex->spname->m_name; This is not always the case. Some SP statements (e.g. IF) do the following in their beginning: - create a separate local LEX - set thd->lex to this new local LEX - push the new local LEX to the stack in sp_head::m_lex and the following at the end of the statement: - pop the previous LEX from the stack sp_head::m_lex - set thd->lex back to the popped value So when the parse error happens inside e.g. IF statement, thd->lex->spname is a NULL pointer, because thd->lex points to the local LEX (without SP name) rather than the top level LEX (with SP name). Fix: - Adding a new method sp_head::find_spname_recursive() which walks inside the LEX stack sp_head::m_lex from the top (the newest, most local) to the bottom (the oldest), and finds the one which contains a non-zero spname pointer. - Using the new method inside Deprecated_trigger_syntax_handler::handle_condition(): First it still tests thd->lex->spname (like before this change), and uses it in case it is not empty. Otherwise (if thd->lex->spname is empty), it calls sp_head::find_spname_recursive() to find the LEX with a non-empty spname inside the LEX stack of the current sphead.
1 parent 6d8794e commit 2832b94

File tree

4 files changed

+546
-3
lines changed

4 files changed

+546
-3
lines changed

mysql-test/main/trigger-compat.result

Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,298 @@ DROP TRIGGER tr12;
142142
DROP TRIGGER tr11;
143143
DROP TABLE t1;
144144
DROP TABLE t2;
145+
#
146+
# MDEV-25659 trigger name is empty after upgrade to 10.4
147+
#
148+
# START: Total triggers 1, broken triggers 1, DROP TABLE
149+
CREATE TABLE t1 (a INT);
150+
INSERT INTO t1 VALUES (1);
151+
FLUSH TABLES;
152+
DELETE FROM t1 WHERE a=1;
153+
ERROR 42000: Trigger 'tr1' has an error in its body: 'Undeclared variable: unknown_variable'
154+
INSERT INTO t1 VALUES (2);
155+
ERROR 42000: Trigger 'tr1' has an error in its body: 'Undeclared variable: unknown_variable'
156+
SET time_zone='+00:00';
157+
SHOW TRIGGERS LIKE 't1';
158+
Trigger tr1
159+
Event DELETE
160+
Table t1
161+
Statement CREATE DEFINER=`root`@`localhost` TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
162+
BEGIN
163+
IF unknown_variable
164+
THEN
165+
INSERT INTO t2 VALUES (OLD.a);
166+
END IF;
167+
END
168+
Timing AFTER
169+
Created 2022-01-13 08:23:06.47
170+
sql_mode STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
171+
Definer
172+
character_set_client utf8
173+
collation_connection utf8_general_ci
174+
Database Collation latin1_swedish_ci
175+
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE TRIGGER_NAME='tr1';
176+
TRIGGER_CATALOG def
177+
TRIGGER_SCHEMA test
178+
TRIGGER_NAME tr1
179+
EVENT_MANIPULATION DELETE
180+
EVENT_OBJECT_CATALOG def
181+
EVENT_OBJECT_SCHEMA test
182+
EVENT_OBJECT_TABLE t1
183+
ACTION_ORDER 1
184+
ACTION_CONDITION NULL
185+
ACTION_STATEMENT CREATE DEFINER=`root`@`localhost` TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
186+
BEGIN
187+
IF unknown_variable
188+
THEN
189+
INSERT INTO t2 VALUES (OLD.a);
190+
END IF;
191+
END
192+
ACTION_ORIENTATION ROW
193+
ACTION_TIMING AFTER
194+
ACTION_REFERENCE_OLD_TABLE NULL
195+
ACTION_REFERENCE_NEW_TABLE NULL
196+
ACTION_REFERENCE_OLD_ROW OLD
197+
ACTION_REFERENCE_NEW_ROW NEW
198+
CREATED 2022-01-13 08:23:06.47
199+
SQL_MODE STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
200+
DEFINER
201+
CHARACTER_SET_CLIENT utf8
202+
COLLATION_CONNECTION utf8_general_ci
203+
DATABASE_COLLATION latin1_swedish_ci
204+
SET time_zone=DEFAULT;
205+
# Listing trigger files
206+
t1.TRG
207+
tr1.TRN
208+
# Listing trigger files done
209+
DROP TABLE t1;
210+
# Listing trigger files
211+
# Listing trigger files done
212+
# END: Total triggers 1, broken triggers 1, DROP TABLE
213+
# START: Total triggers 1, broken triggers 1, DROP TRIGGER
214+
CREATE TABLE t1 (a INT);
215+
INSERT INTO t1 VALUES (1);
216+
FLUSH TABLES;
217+
DELETE FROM t1 WHERE a=1;
218+
ERROR 42000: Trigger 'tr1' has an error in its body: 'Undeclared variable: unknown_variable'
219+
INSERT INTO t1 VALUES (2);
220+
ERROR 42000: Trigger 'tr1' has an error in its body: 'Undeclared variable: unknown_variable'
221+
SET time_zone='+00:00';
222+
SHOW TRIGGERS LIKE 't1';
223+
Trigger tr1
224+
Event DELETE
225+
Table t1
226+
Statement CREATE DEFINER=`root`@`localhost` TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
227+
BEGIN
228+
IF unknown_variable
229+
THEN
230+
INSERT INTO t2 VALUES (OLD.a);
231+
END IF;
232+
END
233+
Timing AFTER
234+
Created 2022-01-13 08:23:06.47
235+
sql_mode STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
236+
Definer
237+
character_set_client utf8
238+
collation_connection utf8_general_ci
239+
Database Collation latin1_swedish_ci
240+
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE TRIGGER_NAME='tr1';
241+
TRIGGER_CATALOG def
242+
TRIGGER_SCHEMA test
243+
TRIGGER_NAME tr1
244+
EVENT_MANIPULATION DELETE
245+
EVENT_OBJECT_CATALOG def
246+
EVENT_OBJECT_SCHEMA test
247+
EVENT_OBJECT_TABLE t1
248+
ACTION_ORDER 1
249+
ACTION_CONDITION NULL
250+
ACTION_STATEMENT CREATE DEFINER=`root`@`localhost` TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
251+
BEGIN
252+
IF unknown_variable
253+
THEN
254+
INSERT INTO t2 VALUES (OLD.a);
255+
END IF;
256+
END
257+
ACTION_ORIENTATION ROW
258+
ACTION_TIMING AFTER
259+
ACTION_REFERENCE_OLD_TABLE NULL
260+
ACTION_REFERENCE_NEW_TABLE NULL
261+
ACTION_REFERENCE_OLD_ROW OLD
262+
ACTION_REFERENCE_NEW_ROW NEW
263+
CREATED 2022-01-13 08:23:06.47
264+
SQL_MODE STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
265+
DEFINER
266+
CHARACTER_SET_CLIENT utf8
267+
COLLATION_CONNECTION utf8_general_ci
268+
DATABASE_COLLATION latin1_swedish_ci
269+
SET time_zone=DEFAULT;
270+
# Listing trigger files
271+
t1.TRG
272+
tr1.TRN
273+
# Listing trigger files done
274+
DROP TRIGGER tr1;
275+
# Listing trigger files
276+
# Listing trigger files done
277+
DROP TABLE t1;
278+
# END: Total triggers 1, broken triggers 1, DROP TRIGGER
279+
# START: Total triggers 2, broken triggers 1, DROP TABLE
280+
CREATE TABLE t1 (a INT);
281+
INSERT INTO t1 VALUES (1);
282+
FLUSH TABLES;
283+
DELETE FROM t1 WHERE a=1;
284+
ERROR 42000: Trigger 'tr1' has an error in its body: 'Undeclared variable: unknown_variable'
285+
INSERT INTO t1 VALUES (2);
286+
ERROR 42000: Trigger 'tr1' has an error in its body: 'Undeclared variable: unknown_variable'
287+
SET time_zone='+00:00';
288+
SHOW TRIGGERS LIKE 't1';
289+
Trigger tr2
290+
Event INSERT
291+
Table t1
292+
Statement INSERT INTO t2 VALUES (NEW.a+100)
293+
Timing AFTER
294+
Created 2022-01-13 10:01:48.74
295+
sql_mode STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
296+
Definer root@localhost
297+
character_set_client utf8
298+
collation_connection utf8_general_ci
299+
Database Collation latin1_swedish_ci
300+
Trigger tr1
301+
Event DELETE
302+
Table t1
303+
Statement CREATE DEFINER=`root`@`localhost` TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
304+
BEGIN
305+
IF unknown_variable
306+
THEN
307+
INSERT INTO t2 VALUES (OLD.a);
308+
END IF;
309+
END
310+
Timing AFTER
311+
Created 2022-01-13 10:01:48.73
312+
sql_mode STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
313+
Definer
314+
character_set_client utf8
315+
collation_connection utf8_general_ci
316+
Database Collation latin1_swedish_ci
317+
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE TRIGGER_NAME='tr1';
318+
TRIGGER_CATALOG def
319+
TRIGGER_SCHEMA test
320+
TRIGGER_NAME tr1
321+
EVENT_MANIPULATION DELETE
322+
EVENT_OBJECT_CATALOG def
323+
EVENT_OBJECT_SCHEMA test
324+
EVENT_OBJECT_TABLE t1
325+
ACTION_ORDER 1
326+
ACTION_CONDITION NULL
327+
ACTION_STATEMENT CREATE DEFINER=`root`@`localhost` TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
328+
BEGIN
329+
IF unknown_variable
330+
THEN
331+
INSERT INTO t2 VALUES (OLD.a);
332+
END IF;
333+
END
334+
ACTION_ORIENTATION ROW
335+
ACTION_TIMING AFTER
336+
ACTION_REFERENCE_OLD_TABLE NULL
337+
ACTION_REFERENCE_NEW_TABLE NULL
338+
ACTION_REFERENCE_OLD_ROW OLD
339+
ACTION_REFERENCE_NEW_ROW NEW
340+
CREATED 2022-01-13 10:01:48.73
341+
SQL_MODE STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
342+
DEFINER
343+
CHARACTER_SET_CLIENT utf8
344+
COLLATION_CONNECTION utf8_general_ci
345+
DATABASE_COLLATION latin1_swedish_ci
346+
SET time_zone=DEFAULT;
347+
# Listing trigger files
348+
t1.TRG
349+
tr1.TRN
350+
tr2.TRN
351+
# Listing trigger files done
352+
DROP TABLE t1;
353+
# Listing trigger files
354+
# Listing trigger files done
355+
# END: Total triggers 2, broken triggers 1, using DROP TABLE
356+
# START: Total triggers 2, broken triggers 1, DROP TRIGGER
357+
CREATE TABLE t1 (a INT);
358+
INSERT INTO t1 VALUES (1);
359+
FLUSH TABLES;
360+
DELETE FROM t1 WHERE a=1;
361+
ERROR 42000: Trigger 'tr1' has an error in its body: 'Undeclared variable: unknown_variable'
362+
INSERT INTO t1 VALUES (2);
363+
ERROR 42000: Trigger 'tr1' has an error in its body: 'Undeclared variable: unknown_variable'
364+
SET time_zone='+00:00';
365+
SHOW TRIGGERS LIKE 't1';
366+
Trigger tr2
367+
Event INSERT
368+
Table t1
369+
Statement INSERT INTO t2 VALUES (NEW.a+100)
370+
Timing AFTER
371+
Created 2022-01-13 10:01:48.74
372+
sql_mode STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
373+
Definer root@localhost
374+
character_set_client utf8
375+
collation_connection utf8_general_ci
376+
Database Collation latin1_swedish_ci
377+
Trigger tr1
378+
Event DELETE
379+
Table t1
380+
Statement CREATE DEFINER=`root`@`localhost` TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
381+
BEGIN
382+
IF unknown_variable
383+
THEN
384+
INSERT INTO t2 VALUES (OLD.a);
385+
END IF;
386+
END
387+
Timing AFTER
388+
Created 2022-01-13 10:01:48.73
389+
sql_mode STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
390+
Definer
391+
character_set_client utf8
392+
collation_connection utf8_general_ci
393+
Database Collation latin1_swedish_ci
394+
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE TRIGGER_NAME='tr1';
395+
TRIGGER_CATALOG def
396+
TRIGGER_SCHEMA test
397+
TRIGGER_NAME tr1
398+
EVENT_MANIPULATION DELETE
399+
EVENT_OBJECT_CATALOG def
400+
EVENT_OBJECT_SCHEMA test
401+
EVENT_OBJECT_TABLE t1
402+
ACTION_ORDER 1
403+
ACTION_CONDITION NULL
404+
ACTION_STATEMENT CREATE DEFINER=`root`@`localhost` TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
405+
BEGIN
406+
IF unknown_variable
407+
THEN
408+
INSERT INTO t2 VALUES (OLD.a);
409+
END IF;
410+
END
411+
ACTION_ORIENTATION ROW
412+
ACTION_TIMING AFTER
413+
ACTION_REFERENCE_OLD_TABLE NULL
414+
ACTION_REFERENCE_NEW_TABLE NULL
415+
ACTION_REFERENCE_OLD_ROW OLD
416+
ACTION_REFERENCE_NEW_ROW NEW
417+
CREATED 2022-01-13 10:01:48.73
418+
SQL_MODE STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
419+
DEFINER
420+
CHARACTER_SET_CLIENT utf8
421+
COLLATION_CONNECTION utf8_general_ci
422+
DATABASE_COLLATION latin1_swedish_ci
423+
SET time_zone=DEFAULT;
424+
# Listing trigger files
425+
t1.TRG
426+
tr1.TRN
427+
tr2.TRN
428+
# Listing trigger files done
429+
DROP TRIGGER tr1;
430+
# Listing trigger files
431+
t1.TRG
432+
tr2.TRN
433+
# Listing trigger files done
434+
INSERT INTO t1 VALUES (100);
435+
ERROR 42S02: Table 'test.t2' doesn't exist
436+
DROP TABLE t1;
437+
# Listing trigger files
438+
# Listing trigger files done
439+
# END: Total triggers 2, broken triggers 1, using DROP TRIGGER

0 commit comments

Comments
 (0)