Commit ab271ee7 authored by Varun Gupta's avatar Varun Gupta

MDEV-23826: ORDER BY in view definition leads to wrong result with GROUP BY on query using view

Introduced val_time_packed and val_datetime_packed  functions for Item_direct_ref
to make sure to get the value from the item it is referring to.

The issue for incorrect result was that the item was getting its value
from the temporary table rather than from the view.
parent 3b94309a
......@@ -2875,3 +2875,47 @@ WHERE c.table_schema=(SELECT COUNT(*) FROM INFORMATION_SCHEMA.columns GROUP BY c
GROUP BY t.table_name;
ERROR HY001: Out of sort memory, consider increasing server sort buffer size
SET max_sort_length= @save_max_sort_length;
#
# MDEV-23826: ORDER BY in view definition leads to wrong result with GROUP BY on query using view
#
CREATE TABLE t1
(
id INT PRIMARY KEY AUTO_INCREMENT,
dt datetime,
INDEX(dt),
foo int
);
INSERT INTO t1 VALUES (1,'2020-09-26 12:00:00',1);
INSERT INTO t1 VALUES (2,'2020-09-26 13:00:00',1);
INSERT INTO t1 VALUES (3,'2020-09-27 13:00:00',1);
INSERT INTO t1 VALUES (4,'2020-09-27 12:00:00',1);
INSERT INTO t1 VALUES (5,'2020-09-28 12:00:00',1);
INSERT INTO t1 VALUES (6,'2020-09-28 13:00:00',1);
INSERT INTO t1 VALUES (7,'2020-09-25 12:00:00',1);
INSERT INTO t1 VALUES (8,'2020-09-25 13:00:00',1);
INSERT INTO t1 VALUES (9,'2020-09-26 13:00:00',1);
CREATE VIEW v1 AS SELECT * FROM t1;
CREATE VIEW v2 AS SELECT * FROM t1 ORDER BY dt;
SELECT dt, sum(foo) AS foo FROM v1 WHERE dt>DATE_SUB('2020-09-27 00:00:00', INTERVAL 3 DAY) GROUP BY dt;
dt foo
2020-09-25 12:00:00 1
2020-09-25 13:00:00 1
2020-09-26 12:00:00 1
2020-09-26 13:00:00 2
2020-09-27 12:00:00 1
2020-09-27 13:00:00 1
2020-09-28 12:00:00 1
2020-09-28 13:00:00 1
SELECT dt, sum(foo) AS foo FROM v2 WHERE dt>DATE_SUB('2020-09-27 00:00:00', INTERVAL 3 DAY) GROUP BY dt;
dt foo
2020-09-25 12:00:00 1
2020-09-25 13:00:00 1
2020-09-26 12:00:00 1
2020-09-26 13:00:00 2
2020-09-27 12:00:00 1
2020-09-27 13:00:00 1
2020-09-28 12:00:00 1
2020-09-28 13:00:00 1
DROP TABLE t1;
DROP VIEW v1,v2;
# End of 10.2 tests
......@@ -1996,3 +1996,36 @@ ON t.table_schema=c.table_schema
WHERE c.table_schema=(SELECT COUNT(*) FROM INFORMATION_SCHEMA.columns GROUP BY column_type)
GROUP BY t.table_name;
SET max_sort_length= @save_max_sort_length;
--echo #
--echo # MDEV-23826: ORDER BY in view definition leads to wrong result with GROUP BY on query using view
--echo #
CREATE TABLE t1
(
id INT PRIMARY KEY AUTO_INCREMENT,
dt datetime,
INDEX(dt),
foo int
);
INSERT INTO t1 VALUES (1,'2020-09-26 12:00:00',1);
INSERT INTO t1 VALUES (2,'2020-09-26 13:00:00',1);
INSERT INTO t1 VALUES (3,'2020-09-27 13:00:00',1);
INSERT INTO t1 VALUES (4,'2020-09-27 12:00:00',1);
INSERT INTO t1 VALUES (5,'2020-09-28 12:00:00',1);
INSERT INTO t1 VALUES (6,'2020-09-28 13:00:00',1);
INSERT INTO t1 VALUES (7,'2020-09-25 12:00:00',1);
INSERT INTO t1 VALUES (8,'2020-09-25 13:00:00',1);
INSERT INTO t1 VALUES (9,'2020-09-26 13:00:00',1);
CREATE VIEW v1 AS SELECT * FROM t1;
CREATE VIEW v2 AS SELECT * FROM t1 ORDER BY dt;
SELECT dt, sum(foo) AS foo FROM v1 WHERE dt>DATE_SUB('2020-09-27 00:00:00', INTERVAL 3 DAY) GROUP BY dt;
SELECT dt, sum(foo) AS foo FROM v2 WHERE dt>DATE_SUB('2020-09-27 00:00:00', INTERVAL 3 DAY) GROUP BY dt;
DROP TABLE t1;
DROP VIEW v1,v2;
--echo # End of 10.2 tests
......@@ -8160,6 +8160,22 @@ bool Item_direct_ref::get_date(MYSQL_TIME *ltime,ulonglong fuzzydate)
}
longlong Item_direct_ref::val_time_packed()
{
longlong tmp = (*ref)->val_time_packed();
null_value= (*ref)->null_value;
return tmp;
}
longlong Item_direct_ref::val_datetime_packed()
{
longlong tmp = (*ref)->val_datetime_packed();
null_value= (*ref)->null_value;
return tmp;
}
Item_cache_wrapper::~Item_cache_wrapper()
{
DBUG_ASSERT(expr_cache == 0);
......
......@@ -4671,13 +4671,16 @@ class Item_direct_ref :public Item_ref
return Item_ref::fix_fields(thd, it);
}
void save_val(Field *to);
/* Below we should have all val() methods as in Item_ref */
double val_real();
longlong val_int();
String *val_str(String* tmp);
my_decimal *val_decimal(my_decimal *);
bool val_bool();
String *val_str(String* tmp);
bool is_null();
bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
longlong val_datetime_packed();
longlong val_time_packed();
virtual Ref_Type ref_type() { return DIRECT_REF; }
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_direct_ref>(thd, mem_root, this); }
......@@ -4992,6 +4995,20 @@ class Item_direct_view_ref :public Item_direct_ref
}
return Item_direct_ref::get_date(ltime, fuzzydate);
}
longlong val_time_packed()
{
if (check_null_ref())
return 0;
else
return Item_direct_ref::val_time_packed();
}
longlong val_datetime_packed()
{
if (check_null_ref())
return 0;
else
return Item_direct_ref::val_datetime_packed();
}
bool send(Protocol *protocol, String *buffer);
void save_org_in_field(Field *field,
fast_field_copier data __attribute__ ((__unused__)))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment