Skip to content

Commit 4fdac6c

Browse files
committed
MDEV-9701: CREATE VIEW with GROUP BY or ORDER BY and constant produces invalid definition
Fixed printing integer constant in the ORDER clause (MySQL solution) Removed workaround for double resolving counter in the ORDER.
1 parent b25373b commit 4fdac6c

File tree

5 files changed

+64
-21
lines changed

5 files changed

+64
-21
lines changed

mysql-test/r/view.result

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,9 @@ drop table t2;
826826
create table t1 (a int);
827827
insert into t1 values (1), (2);
828828
create view v1 as select 5 from t1 order by 1;
829+
show create view v1;
830+
View Create View character_set_client collation_connection
831+
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select 5 AS `5` from `t1` order by 1 latin1 latin1_swedish_ci
829832
select * from v1;
830833
5
831834
5
@@ -5884,5 +5887,21 @@ a
58845887
DROP VIEW v1;
58855888
DROP TABLE t1;
58865889
#
5890+
# MDEV-9701: CREATE VIEW with GROUP BY or ORDER BY and constant
5891+
# produces invalid definition
5892+
#
5893+
CREATE TABLE t1 ( i INT );
5894+
INSERT INTO t1 VALUES (1),(2);
5895+
CREATE VIEW v1 AS
5896+
SELECT 3 AS three, COUNT(*) FROM t1 GROUP BY three;
5897+
show create view v1;
5898+
View Create View character_set_client collation_connection
5899+
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select 3 AS `three`,count(0) AS `COUNT(*)` from `t1` group by '' latin1 latin1_swedish_ci
5900+
SELECT * FROM v1;
5901+
three COUNT(*)
5902+
3 2
5903+
drop view v1;
5904+
drop table t1;
5905+
#
58875906
# End of 10.1 tests
58885907
#

mysql-test/t/view.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ drop table t2;
731731
create table t1 (a int);
732732
insert into t1 values (1), (2);
733733
create view v1 as select 5 from t1 order by 1;
734+
show create view v1;
734735
select * from v1;
735736
drop view v1;
736737
drop table t1;
@@ -5747,6 +5748,22 @@ SELECT * FROM v1 WHERE a=_latin1'a' COLLATE latin1_bin;
57475748
DROP VIEW v1;
57485749
DROP TABLE t1;
57495750

5751+
--echo #
5752+
--echo # MDEV-9701: CREATE VIEW with GROUP BY or ORDER BY and constant
5753+
--echo # produces invalid definition
5754+
--echo #
5755+
CREATE TABLE t1 ( i INT );
5756+
INSERT INTO t1 VALUES (1),(2);
5757+
5758+
CREATE VIEW v1 AS
5759+
SELECT 3 AS three, COUNT(*) FROM t1 GROUP BY three;
5760+
5761+
show create view v1;
5762+
5763+
SELECT * FROM v1;
5764+
5765+
drop view v1;
5766+
drop table t1;
57505767

57515768
--echo #
57525769
--echo # End of 10.1 tests

sql/sql_lex.cc

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,30 +2689,22 @@ void st_select_lex::print_order(String *str,
26892689
{
26902690
if (order->counter_used)
26912691
{
2692-
if (!(query_type & QT_VIEW_INTERNAL))
2692+
char buffer[20];
2693+
size_t length= my_snprintf(buffer, 20, "%d", order->counter);
2694+
str->append(buffer, (uint) length);
2695+
}
2696+
else
2697+
{
2698+
/* replace numeric reference with equivalent for ORDER constant */
2699+
if (order->item[0]->type() == Item::INT_ITEM &&
2700+
order->item[0]->basic_const_item())
26932701
{
2694-
char buffer[20];
2695-
size_t length= my_snprintf(buffer, 20, "%d", order->counter);
2696-
str->append(buffer, (uint) length);
2702+
/* make it expression instead of integer constant */
2703+
str->append(STRING_WITH_LEN("''"));
26972704
}
26982705
else
2699-
{
2700-
/* replace numeric reference with expression */
2701-
if (order->item[0]->type() == Item::INT_ITEM &&
2702-
order->item[0]->basic_const_item())
2703-
{
2704-
char buffer[20];
2705-
size_t length= my_snprintf(buffer, 20, "%d", order->counter);
2706-
str->append(buffer, (uint) length);
2707-
/* make it expression instead of integer constant */
2708-
str->append(STRING_WITH_LEN("+0"));
2709-
}
2710-
else
2711-
(*order->item)->print(str, query_type);
2712-
}
2706+
(*order->item)->print(str, query_type);
27132707
}
2714-
else
2715-
(*order->item)->print(str, query_type);
27162708
if (!order->asc)
27172709
str->append(STRING_WITH_LEN(" desc"));
27182710
if (order->next)

sql/sql_select.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21874,7 +21874,11 @@ find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
2187421874
*/
2187521875
if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
2187621876
{ /* Order by position */
21877-
uint count= (uint) order_item->val_int();
21877+
uint count;
21878+
if (order->counter_used)
21879+
count= order->counter; // counter was once resolved
21880+
else
21881+
count= (uint) order_item->val_int();
2187821882
if (!count || count > fields.elements)
2187921883
{
2188021884
my_error(ER_BAD_FIELD_ERROR, MYF(0),

sql/sql_union.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,11 +1161,22 @@ List<Item> *st_select_lex_unit::get_unit_column_types()
11611161
return &sl->item_list;
11621162
}
11631163

1164+
1165+
static void cleanup_order(ORDER *order)
1166+
{
1167+
for (; order; order= order->next)
1168+
order->counter_used= 0;
1169+
}
1170+
1171+
11641172
bool st_select_lex::cleanup()
11651173
{
11661174
bool error= FALSE;
11671175
DBUG_ENTER("st_select_lex::cleanup()");
11681176

1177+
cleanup_order(order_list.first);
1178+
cleanup_order(group_list.first);
1179+
11691180
if (join)
11701181
{
11711182
DBUG_ASSERT((st_select_lex*)join->select_lex == this);

0 commit comments

Comments
 (0)