[MDEV-31995] Bogus error executing PS for query using CTE with renaming of columns Created: 2023-08-23 Updated: 2024-01-09 Resolved: 2023-11-07 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Optimizer - CTE, Server |
| Affects Version/s: | 10.4.31, 11.2.1 |
| Fix Version/s: | 10.4.33, 10.5.24, 10.6.17, 10.11.7, 11.0.5, 11.1.4, 11.2.3, 11.3.2 |
| Type: | Bug | Priority: | Major |
| Reporter: | Rex Johnston | Assignee: | Rex Johnston |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Issue Links: |
|
||||||||||||
| Description |
|
so far so good.
ERROR 1054 (42S22): Unknown column 'col1' in 'group statement' swapping the original column name back in...
is OK order by clause is similar
is OK.
ERROR 1054 (42S22): Unknown column 'col1' in 'order clause' interestingly (by design) the where clause is treated differently
ERROR 1054 (42S22): Unknown column 'col1' in 'where clause' |
| Comments |
| Comment by Rex Johnston [ 2023-09-01 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
The attached commit addresses column naming issues with CTEs in the use of prepared statements and procedures. There are three related but different issues addressed here. 1) First execution issue. Consider the following
During parsing, the Item representing the first column in the derived table (cte) created, then has it's name set to c1. When run as a prepared statement, context analysis is run again before statement execution, so name resolution on elements in order by will no longer find an Item with the name of c1 and will produce an error Columns of the first select in the unit representing any potential union determine the names of the resultant set, so only Items in this select are renamed to names specified in the with clause. 2) Second execution issue. Consider this example
and what happens to Items in the second select in the union. Parsing sets the name of the first Item to col3, which is then used in resolving the group by clause. The optimizer will then potentially push the clause "c1=1" into BOTH selects in the union and in doing so will rename all Items in all selects in the union in that position to c1 to make the clause valid. During the next execution, the group by clause will no longer be resolvable. Examining the JSON output from an explain command on the above query produces
but with set @@optimizer_switch="condition_pushdown_for_derived=off";
we can see this condition (t1.a=1) pushdown in action. 3) Memory Leak. During parsing, Item names are allocated in statement memory. During context analysis before each execution, Item renaming allocated more memory on statement memory. For statements that have a high number of executions, this would be a noticable memory leak. A recently introduced compile time flag, activated with the cmake option -DWITH_PROTECT_STATEMENT_MEMROOT:BOOL=ON triggers a failing assert on second execution
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Rex Johnston [ 2023-10-30 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thanks Igor. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Igor Babaev [ 2023-10-30 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
ok to push into 10.4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Marko Mäkelä [ 2023-11-17 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
The 10.5 version of this was pushed as a normal commit, not as a merge of the 10.4 commit. I just merged this to 10.6. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2024-01-08 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Suggested line for the changelog: |