[MDEV-6054] ER_BAD_FIELD_ERROR (Unknown column in where clause) on the 2nd execution of PS Created: 2014-04-09  Updated: 2023-11-28

Status: Stalled
Project: MariaDB Server
Component/s: Prepared Statements
Affects Version/s: 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 10.10, 10.11
Fix Version/s: 10.4, 10.5, 10.6

Type: Bug Priority: Major
Reporter: Elena Stepanova Assignee: Dmitry Shulga
Resolution: Unresolved Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-5531 double call procedure in one session ... Closed

 Description   

The problem was introduced in 5.5 tree with the following revision:

revno: 4030
revision-id: monty@askmonty.org-20140124125018-qwln430o4bu3pued
parent: monty@askmonty.org-20140124123019-2ts6q2ip6076gmqg
committer: Michael Widenius <monty@askmonty.org>
branch nick: maria-5.5
timestamp: Fri 2014-01-24 14:50:18 +0200
message:
  Fix for MDEV-5531: double call procedure in one session - hard shutdown the server
  Main fix was to not cache derivied tables as they may be temporary tables that are deleted before the next query.
  This was a bit tricky as Item_field::fix_fields depended on cached_tables to be set to resolve some columns.

CREATE TABLE t1 (a INT, b INT) ENGINE=MyISAM;
CREATE TABLE t2 (c INT) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1,1),(2,2);
INSERT INTO t2 VALUES (3),(4);
 
PREPARE stmt FROM 'SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq WHERE a IN ( SELECT c FROM t2 WHERE c <= sq.b )';
EXECUTE stmt;
EXECUTE stmt;

query 'EXECUTE stmt' failed: 1054: Unknown column 'sq.b' in 'where clause'



 Comments   
Comment by Michael Widenius [ 2014-09-09 ]

simplified prepare (easier to debug):

PREPARE stmt FROM 'SELECT 1 FROM ( SELECT DISTINCT * FROM t1 ) AS sq WHERE a IN ( SELECT 1 FROM t2 WHERE sq.b <= c)';

The problem is that during the first execute, we call, as part of sub query optimization:

void Item_field::fix_after_pullout(st_select_lex *new_parent, Item **ref)

This sets ctx->outer_contex = NULL for the new context.

This causes setup_without_group-> setup_conds for the second execution to not find the outer ref field sq.b.

One can't use the old outer_context for the new context as in fix_outer_field we do:

Item_subselect *prev_subselect_item=
last_checked_context->select_lex->master_unit()->item;

Which is 0 for the new context that was created.

Comment by Elena Stepanova [ 2022-12-20 ]

Still reproducible on all existing versions.

Generated at Thu Feb 08 07:09:01 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.