When pushing a condition into all selects of a unit with more than one select, items are renamed so we can re-use the condition being pushed (without needing to clone and rename elements in the condition).
These names need to be saved and reset for correct name resolution on second execution of prepared statements. Taking advantage of existing mechanisms for resetting item list names (select_lex::save_item_list_names() and mysql_derived_reinit()) we can save and reset the names of only the select_lex's with overwritten names.
Rex Johnston
added a comment - When pushing a condition into all selects of a unit with more than one select, items are renamed so we can re-use the condition being pushed (without needing to clone and rename elements in the condition).
These names need to be saved and reset for correct name resolution on second execution of prepared statements. Taking advantage of existing mechanisms for resetting item list names (select_lex::save_item_list_names() and mysql_derived_reinit()) we can save and reset the names of only the select_lex's with overwritten names.
Hi igor
I discovered this when formulating pushdown tests for 31466. I think it needs to be applied to maintained builds.
Thanks, Rex
Rex Johnston
added a comment - Hi igor
I discovered this when formulating pushdown tests for 31466. I think it needs to be applied to maintained builds.
Thanks, Rex
The following SQL statements illustrate the problem here
createtable t1 (c1 int, c2 int, c3 int);
insertinto t1 values (1,2,3),(4,5,6);
createtable t2 (c4 int, c5 int, c6 int);
insertinto t2 values (7,8,9),(10,11,12);
prepare stmt from
'analyze format=json
select * from
(
select c1, c2, sum(c3) as s from t1 group by c1, c2 having s > 2
union
select c4, c5, sum(c6) as u from t2 group by c4, c5 having u > 3
) dt
where c2 > 0';
execute stmt;
execute stmt;
during the first execution of our statement stmt , JOIN::prepare() handles derived statements, indirectly calling mysql_derived_prepare(). This calls prepare on the unit represented by
select c1, c2, sum(c3) as s from t1 groupby c1, c2 having s > 2
union
select c4, c5, sum(c6) as u from t2 groupby c4, c5 having u > 3
fix_fields() on our having clause(s) resolves u > 3 by matching u to the item for sum(c6) as u
JOIN::optimize() is called before actual statement execution. is_materialized_derived() is true, so pushdown_cond_for_derived() is called for derived TABLE_LIST for the unit show above.
The pushed outer cond is c2 > 0.
This is pushed into select 1 in the unit (select t1.c1 AS c1,t1.c2 AS c2,sum(t1.c3) AS s from t1 group by t1.c1, t1.c2 having s > 2)
then into select 2 (select t2.c4 AS c4,t2.c5 AS c5,sum(t2.c6) AS u from t2 group by t2.c4,t2.c5 having u > 3).
As part of this, it does the following for the reasons in the comment.
/*
Rename the columns of all non-first selects of a union to be compatible
by names with the columns of the first select. It will allow to use copies
of the same expression pushed into having clauses of different selects.
This permanently renames the item list overwriting 'sum(t2.c6) AS u' to 'sum(t2.c6) AS s'
this is not reversed in mysql_derived_reinit().
During the second execution, the unit now looks like this
select c1, c2, sum(c3) as s from t1 groupby c1, c2 having s > 2
union
select c4, c5, sum(c6) as s from t2 groupby c4, c5 having u > 3
During fix_fields(), name resolution on u > 3 will now fail.
Rex Johnston
added a comment - - edited A step by step analysis of the issue.
The following SQL statements illustrate the problem here
create table t1 (c1 int , c2 int , c3 int );
insert into t1 values (1,2,3),(4,5,6);
create table t2 (c4 int , c5 int , c6 int );
insert into t2 values (7,8,9),(10,11,12);
prepare stmt from
'analyze format=json
select * from
(
select c1, c2, sum(c3) as s from t1 group by c1, c2 having s > 2
union
select c4, c5, sum(c6) as u from t2 group by c4, c5 having u > 3
) dt
where c2 > 0' ;
execute stmt;
execute stmt;
during the first execution of our statement stmt , JOIN::prepare() handles derived statements, indirectly calling mysql_derived_prepare() . This calls prepare on the unit represented by
select c1, c2, sum (c3) as s from t1 group by c1, c2 having s > 2
union
select c4, c5, sum (c6) as u from t2 group by c4, c5 having u > 3
fix_fields() on our having clause(s) resolves u > 3 by matching u to the item for sum(c6) as u
JOIN::optimize() is called before actual statement execution.
is_materialized_derived() is true, so pushdown_cond_for_derived() is called for derived TABLE_LIST for the unit show above.
The pushed outer cond is c2 > 0 .
This is pushed into select 1 in the unit (select t1.c1 AS c1,t1.c2 AS c2,sum(t1.c3) AS s from t1 group by t1.c1, t1.c2 having s > 2)
then into select 2 (select t2.c4 AS c4,t2.c5 AS c5,sum(t2.c6) AS u from t2 group by t2.c4,t2.c5 having u > 3).
As part of this, it does the following for the reasons in the comment.
/*
Rename the columns of all non-first selects of a union to be compatible
by names with the columns of the first select. It will allow to use copies
of the same expression pushed into having clauses of different selects.
*/
if (sl != first_sl)
{
DBUG_ASSERT(sl->item_list.elements == first_sl->item_list.elements);
List_iterator_fast<Item> it(sl->item_list);
List_iterator_fast<Item> nm_it(unit->types);
while (Item *item= it++)
item->share_name_with(nm_it++);
}
This permanently renames the item list overwriting 'sum(t2.c6) AS u' to 'sum(t2.c6) AS s'
this is not reversed in mysql_derived_reinit() .
During the second execution, the unit now looks like this
select c1, c2, sum (c3) as s from t1 group by c1, c2 having s > 2
union
select c4, c5, sum (c6) as s from t2 group by c4, c5 having u > 3
During fix_fields() , name resolution on u > 3 will now fail.
The following query over the tables from the above commit also fails on the second execution:
select * from
(
select c1, sum(c3) as s from t1 groupby c1
union
select c4 as c, sum(c6) as u from t2 groupby c
) dt
where c1 > 0
MariaDB [test]> prepare stmt from "
"> select * from
"> (
"> select c1, sum(c3) as s from t1 group by c1
"> union
"> select c4 as c, sum(c6) as u from t2 group by c
"> ) dt
"> where c1 > 0
"> ";
Query OK, 0 rows affected (0.002 sec)
Statement prepared
MariaDB [test]> execute stmt;
+------+------+
| c1 | s |
+------+------+
| 1 | 3 |
| 4 | 6 |
| 7 | 9 |
| 10 | 12 |
+------+------+
4 rows in set (0.004 sec)
MariaDB [test]> execute stmt;
ERROR 1054 (42S22): Unknown column 'c' in 'group statement'
As we can see this query does not contain any having clauses.
Igor Babaev (Inactive)
added a comment - The following query over the tables from the above commit also fails on the second execution:
select * from
(
select c1, sum (c3) as s from t1 group by c1
union
select c4 as c, sum (c6) as u from t2 group by c
) dt
where c1 > 0
MariaDB [test]> prepare stmt from "
"> select * from
"> (
"> select c1, sum(c3) as s from t1 group by c1
"> union
"> select c4 as c, sum(c6) as u from t2 group by c
"> ) dt
"> where c1 > 0
"> ";
Query OK, 0 rows affected (0.002 sec)
Statement prepared
MariaDB [test]> execute stmt;
+------+------+
| c1 | s |
+------+------+
| 1 | 3 |
| 4 | 6 |
| 7 | 9 |
| 10 | 12 |
+------+------+
4 rows in set (0.004 sec)
MariaDB [test]> execute stmt;
ERROR 1054 (42S22): Unknown column 'c' in 'group statement'
As we can see this query does not contain any having clauses.
for release notes: grouping operators referring to column aliases in unions inside derived tables can cause name resolution problems with prepared statements.
Rex Johnston
added a comment - for release notes: grouping operators referring to column aliases in unions inside derived tables can cause name resolution problems with prepared statements.
People
Rex Johnston
Rex Johnston
Votes:
1Vote for this issue
Watchers:
3Start 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":845,"ttfb":228.2999997138977,"pageVisibility":"visible","entityId":129796,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"cdab86e3-a45d-47b6-8225-7c6fa3e921d2","navigationType":0,"readyForUser":941.4000000953674,"redirectCount":0,"resourceLoadedEnd":531,"resourceLoadedStart":236.69999980926514,"resourceTiming":[{"duration":19.700000286102295,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":236.69999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":236.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":256.40000009536743,"responseStart":0,"secureConnectionStart":0},{"duration":19.699999809265137,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/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&whisper-enabled=true","startTime":237,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":237,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":256.69999980926514,"responseStart":0,"secureConnectionStart":0},{"duration":123.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":237.09999990463257,"connectEnd":237.09999990463257,"connectStart":237.09999990463257,"domainLookupEnd":237.09999990463257,"domainLookupStart":237.09999990463257,"fetchStart":237.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":258.19999980926514,"responseEnd":360.69999980926514,"responseStart":278.69999980926514,"secureConnectionStart":237.09999990463257},{"duration":144.19999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/2d8175ec2fa4c816e8023260bd8c1786-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/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&whisper-enabled=true","startTime":237.40000009536743,"connectEnd":237.40000009536743,"connectStart":237.40000009536743,"domainLookupEnd":237.40000009536743,"domainLookupStart":237.40000009536743,"fetchStart":237.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":258.69999980926514,"responseEnd":381.59999990463257,"responseStart":280.2999997138977,"secureConnectionStart":237.40000009536743},{"duration":51.40000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/a9324d6758d385eb45c462685ad88f1d-CDN/lu2cib/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":237.59999990463257,"connectEnd":237.59999990463257,"connectStart":237.59999990463257,"domainLookupEnd":237.59999990463257,"domainLookupStart":237.59999990463257,"fetchStart":237.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":260.09999990463257,"responseEnd":289,"responseStart":288.2999997138977,"secureConnectionStart":237.59999990463257},{"duration":100.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":237.69999980926514,"connectEnd":237.69999980926514,"connectStart":237.69999980926514,"domainLookupEnd":237.69999980926514,"domainLookupStart":237.69999980926514,"fetchStart":237.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":264.40000009536743,"responseEnd":338.59999990463257,"responseStart":337.5,"secureConnectionStart":237.69999980926514},{"duration":99.39999961853027,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":237.90000009536743,"connectEnd":237.90000009536743,"connectStart":237.90000009536743,"domainLookupEnd":237.90000009536743,"domainLookupStart":237.90000009536743,"fetchStart":237.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":265.19999980926514,"responseEnd":337.2999997138977,"responseStart":336.90000009536743,"secureConnectionStart":237.90000009536743},{"duration":23.09999990463257,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2cib/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":238.09999990463257,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":238.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":261.19999980926514,"responseStart":0,"secureConnectionStart":0},{"duration":98.40000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":238.2999997138977,"connectEnd":238.2999997138977,"connectStart":238.2999997138977,"domainLookupEnd":238.2999997138977,"domainLookupStart":238.2999997138977,"fetchStart":238.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":265.40000009536743,"responseEnd":336.69999980926514,"responseStart":336.19999980926514,"secureConnectionStart":238.2999997138977},{"duration":26.699999809265137,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2cib/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":238.40000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":238.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":265.09999990463257,"responseStart":0,"secureConnectionStart":0},{"duration":100.80000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/s/5d5e8fe91fbc506585e83ea3b62ccc4b-CDN/lu2cib/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":238.59999990463257,"connectEnd":238.59999990463257,"connectStart":238.59999990463257,"domainLookupEnd":238.59999990463257,"domainLookupStart":238.59999990463257,"fetchStart":238.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":268.2999997138977,"responseEnd":339.40000009536743,"responseStart":338.7999997138977,"secureConnectionStart":238.59999990463257},{"duration":54.799999713897705,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":250,"connectEnd":290.69999980926514,"connectStart":290.69999980926514,"domainLookupEnd":290.69999980926514,"domainLookupStart":290.69999980926514,"fetchStart":250,"redirectEnd":0,"redirectStart":0,"requestStart":291,"responseEnd":304.7999997138977,"responseStart":303.59999990463257,"secureConnectionStart":290.69999980926514},{"duration":267.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":263.90000009536743,"connectEnd":263.90000009536743,"connectStart":263.90000009536743,"domainLookupEnd":263.90000009536743,"domainLookupStart":263.90000009536743,"fetchStart":263.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":315,"responseEnd":531,"responseStart":528.9000000953674,"secureConnectionStart":263.90000009536743},{"duration":226.7000002861023,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":577.2999997138977,"connectEnd":577.2999997138977,"connectStart":577.2999997138977,"domainLookupEnd":577.2999997138977,"domainLookupStart":577.2999997138977,"fetchStart":577.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":767.0999999046326,"responseEnd":804,"responseStart":803.2999997138977,"secureConnectionStart":577.2999997138977},{"duration":167,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":821.0999999046326,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":821.0999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":988.0999999046326,"responseStart":0,"secureConnectionStart":0},{"duration":207.5,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":845.4000000953674,"connectEnd":845.4000000953674,"connectStart":845.4000000953674,"domainLookupEnd":845.4000000953674,"domainLookupStart":845.4000000953674,"fetchStart":845.4000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":1017.9000000953674,"responseEnd":1052.9000000953674,"responseStart":1052.4000000953674,"secureConnectionStart":845.4000000953674}],"fetchStart":0,"domainLookupStart":9,"domainLookupEnd":30,"connectStart":30,"connectEnd":53,"secureConnectionStart":40,"requestStart":53,"responseStart":229,"responseEnd":264,"domLoading":233,"domInteractive":1037,"domContentLoadedEventStart":1037,"domContentLoadedEventEnd":1088,"domComplete":1471,"loadEventStart":1471,"loadEventEnd":1472,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":991.6999998092651},{"name":"bigPipe.sidebar-id.end","time":993.9000000953674},{"name":"bigPipe.activity-panel-pipe-id.start","time":994.0999999046326},{"name":"bigPipe.activity-panel-pipe-id.end","time":1006},{"name":"activityTabFullyLoaded","time":1115.4000000953674}],"measures":[],"correlationId":"f85e3cbda8e089","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":110,"dbReadsTimeInMs":12,"dbConnsTimeInMs":20,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
When pushing a condition into all selects of a unit with more than one select, items are renamed so we can re-use the condition being pushed (without needing to clone and rename elements in the condition).
These names need to be saved and reset for correct name resolution on second execution of prepared statements. Taking advantage of existing mechanisms for resetting item list names (select_lex::save_item_list_names() and mysql_derived_reinit()) we can save and reset the names of only the select_lex's with overwritten names.