summaryrefslogtreecommitdiff
path: root/sql/sql_derived.cc
diff options
context:
space:
mode:
authorIgor Babaev <igor@askmonty.org>2020-11-18 13:21:19 -0800
committerIgor Babaev <igor@askmonty.org>2020-11-19 07:47:43 -0800
commit1248c654c494df6df3dedf610e322f69a85d3102 (patch)
tree4887e0abe9dcd120f720fbe8e0526a8fb1475c0e /sql/sql_derived.cc
parentbbbab8215f61ac6aa0af5c4f6a5a8509e9707e68 (diff)
downloadmariadb-git-1248c654c494df6df3dedf610e322f69a85d3102.tar.gz
MDEV-19179 Regression: SELECT ... UNION ... with inconsistent column names fails
A bogus error message was issued when a condition was pushed into a materialized derived table or view specified as union of selects with aggregation when the corresponding columns of the selects had different names. This happened because the expression pushed into having clauses of the selects was adjusted for the names of the first select of the union. The easiest solution was to rename the columns of the other selects to be name compatible with the columns of the first select. Approved by Oleksandr Byelkin <sanja@mariadb.com>
Diffstat (limited to 'sql/sql_derived.cc')
-rw-r--r--sql/sql_derived.cc22
1 files changed, 20 insertions, 2 deletions
diff --git a/sql/sql_derived.cc b/sql/sql_derived.cc
index 39499e6895f..5379dd45bfb 100644
--- a/sql/sql_derived.cc
+++ b/sql/sql_derived.cc
@@ -1199,7 +1199,8 @@ bool pushdown_cond_for_derived(THD *thd, Item *cond, TABLE_LIST *derived)
DBUG_RETURN(false);
st_select_lex_unit *unit= derived->get_unit();
- st_select_lex *sl= unit->first_select();
+ st_select_lex *first_sl= unit->first_select();
+ st_select_lex *sl= first_sl;
if (derived->prohibit_cond_pushdown)
DBUG_RETURN(false);
@@ -1311,7 +1312,24 @@ bool pushdown_cond_for_derived(THD *thd, Item *cond, TABLE_LIST *derived)
if (!extracted_cond_copy)
continue;
}
-
+
+ /*
+ 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);
+ Item * item;
+ while((item= it++))
+ {
+ item->share_name_with(nm_it++);
+ }
+ }
+
/*
Transform the references to the 'derived' columns from the condition
pushed into the having clause of sl to make them usable in the new context