summaryrefslogtreecommitdiff
path: root/sql/sql_cte.cc
diff options
context:
space:
mode:
authorIgor Babaev <igor@askmonty.org>2019-09-20 15:59:54 -0700
committerIgor Babaev <igor@askmonty.org>2019-09-20 15:59:54 -0700
commitba7725dace48d403187eb2a418a2081703fe5c9d (patch)
tree02def8620d97af2a65bb125dbb21dcbb2899f8e7 /sql/sql_cte.cc
parent90a9c4cae74d2ef1008e3f216026b7fd2697e46b (diff)
downloadmariadb-git-ba7725dace48d403187eb2a418a2081703fe5c9d.tar.gz
MDEV-20229 CTE defined with table value constructor cannot be used in views
A CTE can be defined as a table values constructor. In this case the CTE is always materialized in a temporary table. If the definition of the CTE contains a list of the names of the CTE columns then the query expression that uses this CTE can refer to the CTE columns by these names. Otherwise the names of the columns are taken from the names of the columns in the result set of the query that specifies the CTE. Thus if the column names of a CTE are provided in the definition the columns of result set should be renamed. In a general case renaming of the columns is done in the select lists of the query specifying the CTE. If a CTE is specified by a table value constructor then there are no such select lists and renaming is actually done for the columns of the result of materialization. Now if a view is specified by a query expression that uses a CTE specified by a table value constructor saving the column names of the CTE in the stored view definition becomes critical: without these names the query expression is not able to refer to the columns of the CTE. This patch saves the given column names of CTEs in stored view definitions that use them.
Diffstat (limited to 'sql/sql_cte.cc')
-rw-r--r--sql/sql_cte.cc16
1 files changed, 16 insertions, 0 deletions
diff --git a/sql/sql_cte.cc b/sql/sql_cte.cc
index 247d7e5a3d1..08d5e20f99a 100644
--- a/sql/sql_cte.cc
+++ b/sql/sql_cte.cc
@@ -1430,6 +1430,22 @@ void With_clause::print(String *str, enum_query_type query_type)
void With_element::print(String *str, enum_query_type query_type)
{
str->append(query_name);
+ if (column_list.elements)
+ {
+ List_iterator_fast<LEX_CSTRING> li(column_list);
+ str->append('(');
+ for (LEX_CSTRING *col_name= li++; ; )
+ {
+ str->append(col_name);
+ col_name= li++;
+ if (!col_name)
+ {
+ str->append(')');
+ break;
+ }
+ str->append(',');
+ }
+ }
str->append(STRING_WITH_LEN(" as "));
str->append('(');
spec->print(str, query_type);