summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mysql-test/r/union.result37
-rw-r--r--mysql-test/t/union.test37
-rw-r--r--sql/sql_select.cc32
-rw-r--r--sql/sql_select.h2
4 files changed, 99 insertions, 9 deletions
diff --git a/mysql-test/r/union.result b/mysql-test/r/union.result
index fe2339db471..83d889b7b73 100644
--- a/mysql-test/r/union.result
+++ b/mysql-test/r/union.result
@@ -1995,4 +1995,41 @@ avg(f) sub
31.5000 0
1.5000 1
drop table t1,t2,t3;
+#
+# MDEV-14715 Assertion `!table || (!table->read_set ||
+# bitmap_is_set(table->read_set, field_index))'
+# failed in Field_num::val_decimal
+#
+CREATE TABLE t1 (a INT, b INT) ENGINE=MyISAM;
+CREATE VIEW v1 AS SELECT * FROM t1;
+INSERT INTO t1 VALUES (1, NULL),(3, 4);
+(SELECT a, sum(a) AS f FROM v1 group by a ORDER BY b + sum(a))
+UNION
+(SELECT 2, 2);
+ERROR HY000: Invalid use of group function
+(SELECT a, sum(a) AS f FROM v1 group by a ORDER BY b + 1)
+UNION
+(SELECT 2, 2);
+a f
+1 1
+3 3
+2 2
+SELECT a, b FROM t1
+UNION
+(SELECT a, VAR_POP(a) AS f FROM v1 GROUP BY a ORDER BY b/a );
+a b
+1 NULL
+3 4
+1 0
+3 0
+DROP TABLE t1;
+(SELECT a, sum(a) AS f FROM v1 group by a ORDER BY b + 1)
+UNION
+(SELECT 2, 2);
+ERROR HY000: View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
+DROP VIEW v1;
+(SELECT a, sum(a) AS f FROM v1 group by a ORDER BY b + 1)
+UNION
+(SELECT 2, 2);
+ERROR 42S02: Table 'test.v1' doesn't exist
End of 5.5 tests
diff --git a/mysql-test/t/union.test b/mysql-test/t/union.test
index 4a3c19b49ab..55d09a7d5ac 100644
--- a/mysql-test/t/union.test
+++ b/mysql-test/t/union.test
@@ -1384,4 +1384,41 @@ select e,f, (e , f) in (select e,b from t1 union select c,d from t2) as sub from
select avg(f), (e , f) in (select e,b from t1 union select c,d from t2) as sub from t3 group by sub;
drop table t1,t2,t3;
+--echo #
+--echo # MDEV-14715 Assertion `!table || (!table->read_set ||
+--echo # bitmap_is_set(table->read_set, field_index))'
+--echo # failed in Field_num::val_decimal
+--echo #
+
+CREATE TABLE t1 (a INT, b INT) ENGINE=MyISAM;
+CREATE VIEW v1 AS SELECT * FROM t1;
+INSERT INTO t1 VALUES (1, NULL),(3, 4);
+
+--error ER_INVALID_GROUP_FUNC_USE
+(SELECT a, sum(a) AS f FROM v1 group by a ORDER BY b + sum(a))
+UNION
+(SELECT 2, 2);
+
+(SELECT a, sum(a) AS f FROM v1 group by a ORDER BY b + 1)
+UNION
+(SELECT 2, 2);
+
+SELECT a, b FROM t1
+UNION
+(SELECT a, VAR_POP(a) AS f FROM v1 GROUP BY a ORDER BY b/a );
+
+DROP TABLE t1;
+
+--error ER_VIEW_INVALID
+(SELECT a, sum(a) AS f FROM v1 group by a ORDER BY b + 1)
+UNION
+(SELECT 2, 2);
+
+DROP VIEW v1;
+
+--error ER_NO_SUCH_TABLE
+(SELECT a, sum(a) AS f FROM v1 group by a ORDER BY b + 1)
+UNION
+(SELECT 2, 2);
+
--echo End of 5.5 tests
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 02a3f0590ac..5db503cd266 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -279,6 +279,10 @@ enum enum_exec_or_opt {WALK_OPTIMIZATION_TABS , WALK_EXECUTION_TABS};
JOIN_TAB *first_breadth_first_tab(JOIN *join, enum enum_exec_or_opt tabs_kind);
JOIN_TAB *next_breadth_first_tab(JOIN *join, enum enum_exec_or_opt tabs_kind,
JOIN_TAB *tab);
+static bool
+find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
+ ORDER *order, List<Item> &fields, List<Item> &all_fields,
+ bool is_group_field, bool add_to_all_fields);
/**
This handles SELECT with and without UNION.
@@ -735,9 +739,15 @@ JOIN::prepare(Item ***rref_pointer_array,
/* Resolve the ORDER BY that was skipped, then remove it. */
if (skip_order_by && select_lex != select_lex->master_unit()->global_parameters)
{
- if (setup_order(thd, (*rref_pointer_array), tables_list, fields_list,
- all_fields, select_lex->order_list.first))
- DBUG_RETURN(-1);
+ thd->where= "order clause";
+ for (ORDER *order= select_lex->order_list.first; order; order= order->next)
+ {
+ /* Don't add the order items to all fields. Just resolve them to ensure
+ the query is valid, we'll drop them immediately after. */
+ if (find_order_in_list(thd, *rref_pointer_array, tables_list, order,
+ fields_list, all_fields, false, false))
+ DBUG_RETURN(-1);
+ }
select_lex->order_list.empty();
}
@@ -20625,7 +20635,10 @@ cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref)
SELECT list)
@param[in,out] all_fields All select, group and order by fields
@param[in] is_group_field True if order is a GROUP field, false if
- ORDER by field
+ ORDER by field
+ @param[in] add_to_all_fields If the item is to be added to all_fields and
+ ref_pointer_array, this flag can be set to
+ false to stop the automatic insertion.
@retval
FALSE if OK
@@ -20636,7 +20649,7 @@ cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref)
static bool
find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
ORDER *order, List<Item> &fields, List<Item> &all_fields,
- bool is_group_field)
+ bool is_group_field, bool add_to_all_fields)
{
Item *order_item= *order->item; /* The item from the GROUP/ORDER caluse. */
Item::Type order_item_type;
@@ -20755,6 +20768,9 @@ find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
thd->is_error()))
return TRUE; /* Wrong field. */
+ if (!add_to_all_fields)
+ return FALSE;
+
uint el= all_fields.elements;
DBUG_ASSERT(all_fields.elements <=
thd->lex->current_select->ref_pointer_array_size);
@@ -20784,13 +20800,13 @@ find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
*/
int setup_order(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
- List<Item> &fields, List<Item> &all_fields, ORDER *order)
+ List<Item> &fields, List<Item> &all_fields, ORDER *order)
{
thd->where="order clause";
for (; order; order=order->next)
{
if (find_order_in_list(thd, ref_pointer_array, tables, order, fields,
- all_fields, FALSE))
+ all_fields, FALSE, true))
return 1;
}
return 0;
@@ -20842,7 +20858,7 @@ setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
for (ord= order; ord; ord= ord->next)
{
if (find_order_in_list(thd, ref_pointer_array, tables, ord, fields,
- all_fields, TRUE))
+ all_fields, TRUE, true))
return 1;
(*ord->item)->marker= UNDEF_POS; /* Mark found */
if ((*ord->item)->with_sum_func)
diff --git a/sql/sql_select.h b/sql/sql_select.h
index 1bc1e4c2b7a..e208377e275 100644
--- a/sql/sql_select.h
+++ b/sql/sql_select.h
@@ -1757,7 +1757,7 @@ int get_quick_record(SQL_SELECT *select);
SORT_FIELD * make_unireg_sortorder(ORDER *order, uint *length,
SORT_FIELD *sortorder);
int setup_order(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
- List<Item> &fields, List <Item> &all_fields, ORDER *order);
+ List<Item> &fields, List <Item> &all_fields, ORDER *order);
int setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
List<Item> &fields, List<Item> &all_fields, ORDER *order,
bool *hidden_group_fields);