SELECT SQL_NO_CACHE a.project_number FROM projects a WHERE ( SELECT z.country FROM projects_history z WHERE z.project_number = a.project_number AND z.history_date <= '2014-09-01'ORDERBY z.id DESC LIMIT 1 ) IN ( SELECT r.country FROM region r WHERE r.region = 'eame' );
The query was executed in MariaDB (10.0.3), MySQL(5.6.17) and MySQL(5.0.96). The MySQL(5.0.96) returns the expected result.
See attached files for details (db dump, explain, actual result and expected result)
#0 Item_field::str_result (this=0x7fff94008ae8, str=0x7fff9400db50) at /home/psergey/dev2/5.5/sql/item.cc:2602
#1 0x00000000007f6808 in Item_cache_str::cache_value (this=0x7fff9400da50) at /home/psergey/dev2/5.5/sql/item.cc:9154
#2 0x00000000007fc1d8 in Item_cache::has_value (this=0x7fff9400da50) at /home/psergey/dev2/5.5/sql/item.h:4124
#3 0x00000000007f6bb9 in Item_cache_str::save_in_field (this=0x7fff9400da50, field=0x7fff94029988, no_conversions=true) at /home/psergey/dev2/5.5/sql/item.cc:9225
#4 0x000000000068241c in store_key_item::copy_inner (this=0x7fff94029950) at /home/psergey/dev2/5.5/sql/sql_select.h:1664
#5 0x000000000068200b in store_key::copy (this=0x7fff94029950) at /home/psergey/dev2/5.5/sql/sql_select.h:1554
#6 0x000000000067421c in cp_buffer_from_ref (thd=0x2f5bcb0, table=0x7fff9402e268, ref=0x7fff940284f8) at /home/psergey/dev2/5.5/sql/sql_select.cc:20357
#7 0x0000000000674155 in cmp_buffer_with_ref (thd=0x2f5bcb0, table=0x7fff9402e268, tab_ref=0x7fff940284f8) at /home/psergey/dev2/5.5/sql/sql_select.cc:20339
#8 0x000000000066d22d in join_read_key2 (thd=0x2f5bcb0, tab=0x7fff940282e8, table=0x7fff9402e268, table_ref=0x7fff940284f8) at /home/psergey/dev2/5.5/sql/sql_select.cc:17522
#9 0x000000000066d18e in join_read_key (tab=0x7fff940282e8) at /home/psergey/dev2/5.5/sql/sql_select.cc:17500
#10 0x000000000066bbae in sub_select (join=0x7fff9400c610, join_tab=0x7fff940282e8, end_of_records=false) at /home/psergey/dev2/5.5/sql/sql_select.cc:16874
#11 0x000000000066c2d7 in evaluate_join_record (join=0x7fff9400c610, join_tab=0x7fff94027fc8, error=0) at /home/psergey/dev2/5.5/sql/sql_select.cc:17096
#12 0x000000000066bc10 in sub_select (join=0x7fff9400c610, join_tab=0x7fff94027fc8, end_of_records=false) at /home/psergey/dev2/5.5/sql/sql_select.cc:16877
#13 0x000000000066b477 in do_select (join=0x7fff9400c610, fields=0x2f5f7c0, table=0x0, procedure=0x0) at /home/psergey/dev2/5.5/sql/sql_select.cc:16539
#14 0x0000000000649dff in JOIN::exec (this=0x7fff9400c610) at /home/psergey/dev2/5.5/sql/sql_select.cc:2873
the problem is, we never executed the subquery. A breakpoint in JOIN::exec() was only hit for select_number==1
Sergei Petrunia
added a comment - - edited This is where we get the lookup value from:
(gdb) wher
#0 Item_field::str_result (this=0x7fff94008ae8, str=0x7fff9400db50) at /home/psergey/dev2/5.5/sql/item.cc:2602
#1 0x00000000007f6808 in Item_cache_str::cache_value (this=0x7fff9400da50) at /home/psergey/dev2/5.5/sql/item.cc:9154
#2 0x00000000007fc1d8 in Item_cache::has_value (this=0x7fff9400da50) at /home/psergey/dev2/5.5/sql/item.h:4124
#3 0x00000000007f6bb9 in Item_cache_str::save_in_field (this=0x7fff9400da50, field=0x7fff94029988, no_conversions=true) at /home/psergey/dev2/5.5/sql/item.cc:9225
#4 0x000000000068241c in store_key_item::copy_inner (this=0x7fff94029950) at /home/psergey/dev2/5.5/sql/sql_select.h:1664
#5 0x000000000068200b in store_key::copy (this=0x7fff94029950) at /home/psergey/dev2/5.5/sql/sql_select.h:1554
#6 0x000000000067421c in cp_buffer_from_ref (thd=0x2f5bcb0, table=0x7fff9402e268, ref=0x7fff940284f8) at /home/psergey/dev2/5.5/sql/sql_select.cc:20357
#7 0x0000000000674155 in cmp_buffer_with_ref (thd=0x2f5bcb0, table=0x7fff9402e268, tab_ref=0x7fff940284f8) at /home/psergey/dev2/5.5/sql/sql_select.cc:20339
#8 0x000000000066d22d in join_read_key2 (thd=0x2f5bcb0, tab=0x7fff940282e8, table=0x7fff9402e268, table_ref=0x7fff940284f8) at /home/psergey/dev2/5.5/sql/sql_select.cc:17522
#9 0x000000000066d18e in join_read_key (tab=0x7fff940282e8) at /home/psergey/dev2/5.5/sql/sql_select.cc:17500
#10 0x000000000066bbae in sub_select (join=0x7fff9400c610, join_tab=0x7fff940282e8, end_of_records=false) at /home/psergey/dev2/5.5/sql/sql_select.cc:16874
#11 0x000000000066c2d7 in evaluate_join_record (join=0x7fff9400c610, join_tab=0x7fff94027fc8, error=0) at /home/psergey/dev2/5.5/sql/sql_select.cc:17096
#12 0x000000000066bc10 in sub_select (join=0x7fff9400c610, join_tab=0x7fff94027fc8, end_of_records=false) at /home/psergey/dev2/5.5/sql/sql_select.cc:16877
#13 0x000000000066b477 in do_select (join=0x7fff9400c610, fields=0x2f5f7c0, table=0x0, procedure=0x0) at /home/psergey/dev2/5.5/sql/sql_select.cc:16539
#14 0x0000000000649dff in JOIN::exec (this=0x7fff9400c610) at /home/psergey/dev2/5.5/sql/sql_select.cc:2873
#15 0x000000000064a60f in mysql_select (thd=0x2f5bcb0, rref_pointer_array=0x2f5f918, tables=0x7fff94007ab0, wild_num=0, fields=..., conds=0x7fff9400c368, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff9400c5f0, unit=0x2f5efd0, select_lex=0x2f5f6a8) at /home/psergey/dev2/5.5/sql/sql_select.cc:3094
We copy the data from "z.country" field:
(gdb) p this->field_name
$125 = 0x7fff94016d20 "country"
(gdb) p this
$126 = (Item_field *) 0x7fff94008ae8
(gdb) p this->field->table->alias.Ptr
$127 = 0x7fff94012440 "z"
the problem is, we never executed the subquery. A breakpoint in JOIN::exec() was only hit for select_number==1
int null_count= test(cur_key_part->field->real_maybe_null());
*ref_key= new store_key_item(thd, cur_key_part->field,
(gdb) p tab_ref->items[0]
$142 = (Item_cache_str *) 0x7fff9400db00
Sergei Petrunia
added a comment - The store_key_item object was created in setup_sj_materialization_part2:
for (i= 0; i < tmp_key_parts; i++, cur_key_part++, ref_key++)
{
tab_ref->items[i]= emb_sj_nest->sj_subq_pred->left_expr->element_index(i);
int null_count= test(cur_key_part->field->real_maybe_null());
*ref_key= new store_key_item(thd, cur_key_part->field,
(gdb) p tab_ref->items[0]
$142 = (Item_cache_str *) 0x7fff9400db00
Can't really understand the code in bool Item_cache_str::cache_value()
Trying this query
MariaDB [test]> SELECT SQL_NO_CACHE a.project_number FROM projects a WHERE a.project_number > (SELECT z.country FROM projects_history z WHERE z.project_number = a.project_number AND z.history_date <= '2014-09-01' ORDER BY z.id DESC LIMIT 1);
Sergei Petrunia
added a comment - Can't really understand the code in bool Item_cache_str::cache_value()
Trying this query
MariaDB [test]> SELECT SQL_NO_CACHE a.project_number FROM projects a WHERE a.project_number > (SELECT z.country FROM projects_history z WHERE z.project_number = a.project_number AND z.history_date <= '2014-09-01' ORDER BY z.id DESC LIMIT 1);
and seeing:
(gdb) print example
$158 = (Item_singlerow_subselect *) 0x7fff98008838
while in the bug's testcase it is:
(gdb) print example
$162 = (Item_field *) 0x7fff94008ae8
(gdb) p emb_sj_nest->sj_subq_pred->left_expr->element_index(0)
$333 = (Item_cache_str *) 0x7fff94045750
(gdb) p emb_sj_nest->sj_subq_pred->left_expr->element_index(0)->example
$334 = (Item_field *) 0x7fff94008ad8
That is, left_expr is an Item_singlerow_subselect. When one calls left_expr->val_XXX() (provided the subquery returns a single value, not tuple) , the subquery gets re-executed. However, left_expr->element_index() is an Item_cache(Item_field). So, if you just call left_expr->element_index(0)->val_int(), subquery is not executed, and you don't get the new value.
According to Sanja, this is a common problem for "row-like" items, and the convention is to call row_item->bring_value() before calling elements of the row_item.
We didn't have this problem with ref access before, because row items are not used for ref access. (Except for sj-materialization)
Sergei Petrunia
added a comment - Discussed with sanja . The interesting part is this code in setup_sj_materialization_part2:
emb_sj_nest->sj_subq_pred->left_expr->element_index(i);
Here :
(gdb) p emb_sj_nest->sj_subq_pred->left_expr
$332 = (Item_singlerow_subselect *) 0x7fff94042810
(gdb) p emb_sj_nest->sj_subq_pred->left_expr->element_index(0)
$333 = (Item_cache_str *) 0x7fff94045750
(gdb) p emb_sj_nest->sj_subq_pred->left_expr->element_index(0)->example
$334 = (Item_field *) 0x7fff94008ad8
That is, left_expr is an Item_singlerow_subselect. When one calls left_expr->val_XXX() (provided the subquery returns a single value, not tuple) , the subquery gets re-executed. However, left_expr->element_index() is an Item_cache(Item_field). So, if you just call left_expr->element_index(0)->val_int(), subquery is not executed, and you don't get the new value.
According to Sanja, this is a common problem for "row-like" items, and the convention is to call row_item->bring_value() before calling elements of the row_item.
We didn't have this problem with ref access before, because row items are not used for ref access. (Except for sj-materialization)
Sergei Petrunia
added a comment - This patch fixes the problem and passes the testsuite. It's not very elegant, though: subquery code in the middle of eq_ref access code:
=== modified file 'sql/sql_select.cc'
--- sql/sql_select.cc 2014-10-14 11:11:06 +0000
+++ sql/sql_select.cc 2014-10-14 22:20:05 +0000
@@ -17517,8 +17517,18 @@ int join_read_key2(THD *thd, JOIN_TAB *t
return 1;
}
}
-
+
+ /*
+ The following is needed when one makes ref (or eq_ref) access from row
+ comparisons: one must call row->bring_value() to get the new values.
+ */
+ if (tab && tab->bush_children)
+ {
+ TABLE_LIST *emb_sj_nest= tab->bush_children->start->emb_sj_nest;
+ emb_sj_nest->sj_subq_pred->left_expr->bring_value();
+ }
/* TODO: Why don't we do "Late NULLs Filtering" here? */
+
if (cmp_buffer_with_ref(thd, table, table_ref) ||
(table->status & (STATUS_GARBAGE | STATUS_NO_PARENT | STATUS_NULL_ROW)))
{
People
Sergei Petrunia
Kenneth Cruz
Votes:
0Vote for this issue
Watchers:
5Start watching this issue
Dates
Created:
Updated:
Resolved:
Git Integration
Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.
{"report":{"fcp":980.6000003814697,"ttfb":249.9000005722046,"pageVisibility":"visible","entityId":45926,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"f53c19be-0128-4637-b74e-ba9d3ab1ab8f","navigationType":0,"readyForUser":1060.4000005722046,"redirectCount":0,"resourceLoadedEnd":723.1999998092651,"resourceLoadedStart":255,"resourceTiming":[{"duration":62.5,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":255,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":255,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":317.5,"responseStart":0,"secureConnectionStart":0},{"duration":62.39999961853027,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2bu7/820016/12ta74/8679b4946efa1a0bb029a3a22206fb5d/_/download/contextbatch/css/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true","startTime":255.30000019073486,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":255.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":317.69999980926514,"responseStart":0,"secureConnectionStart":0},{"duration":256.6000003814697,"initiatorType":"script","name":"https://jira.mariadb.org/s/fbf975c0cce4b1abf04784eeae9ba1f4-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":255.5,"connectEnd":255.5,"connectStart":255.5,"domainLookupEnd":255.5,"domainLookupStart":255.5,"fetchStart":255.5,"redirectEnd":0,"redirectStart":0,"requestStart":321.4000005722046,"responseEnd":512.1000003814697,"responseStart":346.30000019073486,"secureConnectionStart":255.5},{"duration":456.0999994277954,"initiatorType":"script","name":"https://jira.mariadb.org/s/099b33461394b8015fc36c0a4b96e19f-CDN/lu2bu7/820016/12ta74/8679b4946efa1a0bb029a3a22206fb5d/_/download/contextbatch/js/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true","startTime":255.60000038146973,"connectEnd":255.60000038146973,"connectStart":255.60000038146973,"domainLookupEnd":255.60000038146973,"domainLookupStart":255.60000038146973,"fetchStart":255.60000038146973,"redirectEnd":0,"redirectStart":0,"requestStart":321.6000003814697,"responseEnd":711.6999998092651,"responseStart":365.19999980926514,"secureConnectionStart":255.60000038146973},{"duration":97.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/94c15bff32baef80f4096a08aceae8bc-CDN/lu2bu7/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":255.69999980926514,"connectEnd":255.69999980926514,"connectStart":255.69999980926514,"domainLookupEnd":255.69999980926514,"domainLookupStart":255.69999980926514,"fetchStart":255.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":321.80000019073486,"responseEnd":353.19999980926514,"responseStart":349.30000019073486,"secureConnectionStart":255.69999980926514},{"duration":97.60000038146973,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":255.80000019073486,"connectEnd":255.80000019073486,"connectStart":255.80000019073486,"domainLookupEnd":255.80000019073486,"domainLookupStart":255.80000019073486,"fetchStart":255.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":322,"responseEnd":353.4000005722046,"responseStart":350.1000003814697,"secureConnectionStart":255.80000019073486},{"duration":97.89999961853027,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":255.9000005722046,"connectEnd":255.9000005722046,"connectStart":255.9000005722046,"domainLookupEnd":255.9000005722046,"domainLookupStart":255.9000005722046,"fetchStart":255.9000005722046,"redirectEnd":0,"redirectStart":0,"requestStart":322,"responseEnd":353.80000019073486,"responseStart":351.30000019073486,"secureConnectionStart":255.9000005722046},{"duration":63.19999980926514,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2bu7/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":256.1000003814697,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":256.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":319.30000019073486,"responseStart":0,"secureConnectionStart":0},{"duration":146.5,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":256.1000003814697,"connectEnd":256.1000003814697,"connectStart":256.1000003814697,"domainLookupEnd":256.1000003814697,"domainLookupStart":256.1000003814697,"fetchStart":256.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":323,"responseEnd":402.6000003814697,"responseStart":401,"secureConnectionStart":256.1000003814697},{"duration":64.80000019073486,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2bu7/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/css/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.css?jira.create.linked.issue=true","startTime":256.30000019073486,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":256.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":321.1000003814697,"responseStart":0,"secureConnectionStart":0},{"duration":99.89999961853027,"initiatorType":"script","name":"https://jira.mariadb.org/s/3339d87fa2538a859872f2df449bf8d0-CDN/lu2bu7/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/js/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.js?jira.create.linked.issue=true&locale=en","startTime":256.30000019073486,"connectEnd":256.30000019073486,"connectStart":256.30000019073486,"domainLookupEnd":256.30000019073486,"domainLookupStart":256.30000019073486,"fetchStart":256.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":325.5,"responseEnd":356.19999980926514,"responseStart":354.69999980926514,"secureConnectionStart":256.30000019073486},{"duration":460.79999923706055,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":262.4000005722046,"connectEnd":262.4000005722046,"connectStart":262.4000005722046,"domainLookupEnd":262.4000005722046,"domainLookupStart":262.4000005722046,"fetchStart":262.4000005722046,"redirectEnd":0,"redirectStart":0,"requestStart":360.30000019073486,"responseEnd":723.1999998092651,"responseStart":719.4000005722046,"secureConnectionStart":262.4000005722046},{"duration":458.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":263.1000003814697,"connectEnd":263.1000003814697,"connectStart":263.1000003814697,"domainLookupEnd":263.1000003814697,"domainLookupStart":263.1000003814697,"fetchStart":263.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":377.69999980926514,"responseEnd":721.6000003814697,"responseStart":716,"secureConnectionStart":263.1000003814697},{"duration":110.09999942779541,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":647.9000005722046,"connectEnd":647.9000005722046,"connectStart":647.9000005722046,"domainLookupEnd":647.9000005722046,"domainLookupStart":647.9000005722046,"fetchStart":647.9000005722046,"redirectEnd":0,"redirectStart":0,"requestStart":726.6999998092651,"responseEnd":758,"responseStart":757,"secureConnectionStart":647.9000005722046}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":101,"responseStart":250,"responseEnd":260,"domLoading":254,"domInteractive":1138,"domContentLoadedEventStart":1139,"domContentLoadedEventEnd":1201,"domComplete":1663,"loadEventStart":1663,"loadEventEnd":1664,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1098},{"name":"bigPipe.sidebar-id.end","time":1098.8000001907349},{"name":"bigPipe.activity-panel-pipe-id.start","time":1099},{"name":"bigPipe.activity-panel-pipe-id.end","time":1102.8000001907349},{"name":"activityTabFullyLoaded","time":1218.1000003814697}],"measures":[],"correlationId":"a2b85592fe0b2","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":77,"dbReadsTimeInMs":9,"dbConnsTimeInMs":15,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
This is where we get the lookup value from:
(gdb) wher
#0 Item_field::str_result (this=0x7fff94008ae8, str=0x7fff9400db50) at /home/psergey/dev2/5.5/sql/item.cc:2602
#1 0x00000000007f6808 in Item_cache_str::cache_value (this=0x7fff9400da50) at /home/psergey/dev2/5.5/sql/item.cc:9154
#2 0x00000000007fc1d8 in Item_cache::has_value (this=0x7fff9400da50) at /home/psergey/dev2/5.5/sql/item.h:4124
#3 0x00000000007f6bb9 in Item_cache_str::save_in_field (this=0x7fff9400da50, field=0x7fff94029988, no_conversions=true) at /home/psergey/dev2/5.5/sql/item.cc:9225
#4 0x000000000068241c in store_key_item::copy_inner (this=0x7fff94029950) at /home/psergey/dev2/5.5/sql/sql_select.h:1664
#5 0x000000000068200b in store_key::copy (this=0x7fff94029950) at /home/psergey/dev2/5.5/sql/sql_select.h:1554
#6 0x000000000067421c in cp_buffer_from_ref (thd=0x2f5bcb0, table=0x7fff9402e268, ref=0x7fff940284f8) at /home/psergey/dev2/5.5/sql/sql_select.cc:20357
#7 0x0000000000674155 in cmp_buffer_with_ref (thd=0x2f5bcb0, table=0x7fff9402e268, tab_ref=0x7fff940284f8) at /home/psergey/dev2/5.5/sql/sql_select.cc:20339
#8 0x000000000066d22d in join_read_key2 (thd=0x2f5bcb0, tab=0x7fff940282e8, table=0x7fff9402e268, table_ref=0x7fff940284f8) at /home/psergey/dev2/5.5/sql/sql_select.cc:17522
#9 0x000000000066d18e in join_read_key (tab=0x7fff940282e8) at /home/psergey/dev2/5.5/sql/sql_select.cc:17500
#10 0x000000000066bbae in sub_select (join=0x7fff9400c610, join_tab=0x7fff940282e8, end_of_records=false) at /home/psergey/dev2/5.5/sql/sql_select.cc:16874
#11 0x000000000066c2d7 in evaluate_join_record (join=0x7fff9400c610, join_tab=0x7fff94027fc8, error=0) at /home/psergey/dev2/5.5/sql/sql_select.cc:17096
#12 0x000000000066bc10 in sub_select (join=0x7fff9400c610, join_tab=0x7fff94027fc8, end_of_records=false) at /home/psergey/dev2/5.5/sql/sql_select.cc:16877
#13 0x000000000066b477 in do_select (join=0x7fff9400c610, fields=0x2f5f7c0, table=0x0, procedure=0x0) at /home/psergey/dev2/5.5/sql/sql_select.cc:16539
#14 0x0000000000649dff in JOIN::exec (this=0x7fff9400c610) at /home/psergey/dev2/5.5/sql/sql_select.cc:2873
#15 0x000000000064a60f in mysql_select (thd=0x2f5bcb0, rref_pointer_array=0x2f5f918, tables=0x7fff94007ab0, wild_num=0, fields=..., conds=0x7fff9400c368, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff9400c5f0, unit=0x2f5efd0, select_lex=0x2f5f6a8) at /home/psergey/dev2/5.5/sql/sql_select.cc:3094
We copy the data from "z.country" field:
(gdb) p this->field_name
$125 = 0x7fff94016d20 "country"
(gdb) p this
$126 = (Item_field *) 0x7fff94008ae8
(gdb) p this->field->table->alias.Ptr
$127 = 0x7fff94012440 "z"
the problem is, we never executed the subquery. A breakpoint in JOIN::exec() was only hit for select_number==1