Skip to content

Commit 1f1e3ce

Browse files
committed
MDEV-21319 COUNT(*) returns 1, actual SELECT returns no result in 10.3.21, but 1 result in 10.1.41
Item_ref::val_(datetime|time)_packed() erroneously called (*ref)->val_(datetime|time)_packed(). - Fixing to call (*ref)->val_(datetime|time)_packed_result(). - Backporting Item::val_(datetime|time)_packed_result() from 10.3. - Fixing Item_field::get_date_result() to handle null_value in the same way how Item_field::get_date() does.
1 parent a5a433e commit 1f1e3ce

File tree

6 files changed

+250
-5
lines changed

6 files changed

+250
-5
lines changed

mysql-test/r/type_datetime.result

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,5 +1198,86 @@ Warning 1292 Incorrect datetime value: '2' for column 'pk' at row 2
11981198
DROP VIEW v1;
11991199
DROP TABLE t1;
12001200
#
1201+
# MDEV-21319 COUNT(*) returns 1, actual SELECT returns no result in 10.3.21, but 1 result in 10.1.41
1202+
#
1203+
CREATE TABLE t1
1204+
(
1205+
id INT NOT NULL PRIMARY KEY,
1206+
id2 INT,
1207+
k TINYINT,
1208+
j INT,
1209+
t DATETIME,
1210+
KEY k1 (id2,k,j,t)
1211+
);
1212+
INSERT INTO t1 VALUES
1213+
(53,54,1,0,'2019-12-13 10:09:59'),
1214+
(54,54,1,0,'2019-12-13 16:28:41'),
1215+
(55,54,1,0,'2019-12-13 16:29:10'),
1216+
(56,54,1,0,'2019-12-13 16:29:43'),
1217+
(57,54,1,0,'2019-12-13 16:30:16'),
1218+
(58,54,1,0,'2019-12-13 16:30:49'),
1219+
(59,54,1,0,'2019-12-13 16:31:23'),
1220+
(60,54,1,0,'2019-12-13 16:31:55'),
1221+
(61,54,1,0,'2019-12-13 16:32:28'),
1222+
(62,54,1,0,'2019-12-13 16:33:01'),
1223+
(63,54,1,0,'2019-12-13 16:33:34'),
1224+
(64,54,1,0,'2019-12-13 16:34:07'),
1225+
(65,54,1,0,'2019-12-13 16:34:40'),
1226+
(66,54,1,0,'2019-12-13 16:35:13'),
1227+
(67,54,1,0,'2019-12-13 16:35:46'),
1228+
(68,54,1,0,'2019-12-13 16:36:19');
1229+
SELECT t FROM t1 GROUP BY t HAVING t=max(t);
1230+
t
1231+
2019-12-13 10:09:59
1232+
2019-12-13 16:28:41
1233+
2019-12-13 16:29:10
1234+
2019-12-13 16:29:43
1235+
2019-12-13 16:30:16
1236+
2019-12-13 16:30:49
1237+
2019-12-13 16:31:23
1238+
2019-12-13 16:31:55
1239+
2019-12-13 16:32:28
1240+
2019-12-13 16:33:01
1241+
2019-12-13 16:33:34
1242+
2019-12-13 16:34:07
1243+
2019-12-13 16:34:40
1244+
2019-12-13 16:35:13
1245+
2019-12-13 16:35:46
1246+
2019-12-13 16:36:19
1247+
SELECT t FROM t1 WHERE id2=54 and j=0 and k=1 GROUP BY t HAVING t=max(t);
1248+
t
1249+
2019-12-13 10:09:59
1250+
2019-12-13 16:28:41
1251+
2019-12-13 16:29:10
1252+
2019-12-13 16:29:43
1253+
2019-12-13 16:30:16
1254+
2019-12-13 16:30:49
1255+
2019-12-13 16:31:23
1256+
2019-12-13 16:31:55
1257+
2019-12-13 16:32:28
1258+
2019-12-13 16:33:01
1259+
2019-12-13 16:33:34
1260+
2019-12-13 16:34:07
1261+
2019-12-13 16:34:40
1262+
2019-12-13 16:35:13
1263+
2019-12-13 16:35:46
1264+
2019-12-13 16:36:19
1265+
DROP TABLE t1;
1266+
CREATE TABLE t1 (pk INT);
1267+
CREATE VIEW v1 AS SELECT * FROM t1;
1268+
INSERT INTO t1 VALUES (1);
1269+
SELECT pk<DATE'2001-01-01' FROM t1 GROUP BY pk;
1270+
pk<DATE'2001-01-01'
1271+
1
1272+
Warnings:
1273+
Warning 1292 Incorrect datetime value: '1' for column 'pk' at row 1
1274+
SELECT pk<DATE'2001-01-01' FROM v1 GROUP BY pk;
1275+
pk<DATE'2001-01-01'
1276+
1
1277+
Warnings:
1278+
Warning 1292 Incorrect datetime value: '1' for column 'pk' at row 1
1279+
DROP VIEW v1;
1280+
DROP TABLE t1;
1281+
#
12011282
# End of 10.1 tests
12021283
#

mysql-test/r/type_time.result

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,5 +1246,71 @@ drop table t1;
12461246
SET @@optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
12471247
set @@old_mode= @save_old_mode;
12481248
#
1249+
# MDEV-21319 COUNT(*) returns 1, actual SELECT returns no result in 10.3.21, but 1 result in 10.1.41
1250+
#
1251+
CREATE OR REPLACE TABLE t1
1252+
(
1253+
id INT NOT NULL PRIMARY KEY,
1254+
id2 INT,
1255+
k TINYINT,
1256+
j INT,
1257+
t TIME,
1258+
KEY k1 (id2,k,j,t)
1259+
);
1260+
INSERT INTO t1 VALUES
1261+
(53,54,1,0,'10:09:59'),
1262+
(54,54,1,0,'16:28:41'),
1263+
(55,54,1,0,'16:29:10'),
1264+
(56,54,1,0,'16:29:43'),
1265+
(57,54,1,0,'16:30:16'),
1266+
(58,54,1,0,'16:30:49'),
1267+
(59,54,1,0,'16:31:23'),
1268+
(60,54,1,0,'16:31:55'),
1269+
(61,54,1,0,'16:32:28'),
1270+
(62,54,1,0,'16:33:01'),
1271+
(63,54,1,0,'16:33:34'),
1272+
(64,54,1,0,'16:34:07'),
1273+
(65,54,1,0,'16:34:40'),
1274+
(66,54,1,0,'16:35:13'),
1275+
(67,54,1,0,'16:35:46'),
1276+
(68,54,1,0,'16:36:19');
1277+
SELECT t FROM t1 GROUP BY t HAVING t=MAX(t);
1278+
t
1279+
10:09:59
1280+
16:28:41
1281+
16:29:10
1282+
16:29:43
1283+
16:30:16
1284+
16:30:49
1285+
16:31:23
1286+
16:31:55
1287+
16:32:28
1288+
16:33:01
1289+
16:33:34
1290+
16:34:07
1291+
16:34:40
1292+
16:35:13
1293+
16:35:46
1294+
16:36:19
1295+
SELECT t FROM t1 WHERE id2=54 AND j=0 AND k=1 GROUP BY t HAVING t=MAX(t);
1296+
t
1297+
10:09:59
1298+
16:28:41
1299+
16:29:10
1300+
16:29:43
1301+
16:30:16
1302+
16:30:49
1303+
16:31:23
1304+
16:31:55
1305+
16:32:28
1306+
16:33:01
1307+
16:33:34
1308+
16:34:07
1309+
16:34:40
1310+
16:35:13
1311+
16:35:46
1312+
16:36:19
1313+
DROP TABLE t1;
1314+
#
12491315
# End of 10.1 tests
12501316
#

mysql-test/t/type_datetime.test

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,49 @@ DROP VIEW v1;
747747
DROP TABLE t1;
748748

749749

750+
--echo #
751+
--echo # MDEV-21319 COUNT(*) returns 1, actual SELECT returns no result in 10.3.21, but 1 result in 10.1.41
752+
--echo #
753+
754+
CREATE TABLE t1
755+
(
756+
id INT NOT NULL PRIMARY KEY,
757+
id2 INT,
758+
k TINYINT,
759+
j INT,
760+
t DATETIME,
761+
KEY k1 (id2,k,j,t)
762+
);
763+
INSERT INTO t1 VALUES
764+
(53,54,1,0,'2019-12-13 10:09:59'),
765+
(54,54,1,0,'2019-12-13 16:28:41'),
766+
(55,54,1,0,'2019-12-13 16:29:10'),
767+
(56,54,1,0,'2019-12-13 16:29:43'),
768+
(57,54,1,0,'2019-12-13 16:30:16'),
769+
(58,54,1,0,'2019-12-13 16:30:49'),
770+
(59,54,1,0,'2019-12-13 16:31:23'),
771+
(60,54,1,0,'2019-12-13 16:31:55'),
772+
(61,54,1,0,'2019-12-13 16:32:28'),
773+
(62,54,1,0,'2019-12-13 16:33:01'),
774+
(63,54,1,0,'2019-12-13 16:33:34'),
775+
(64,54,1,0,'2019-12-13 16:34:07'),
776+
(65,54,1,0,'2019-12-13 16:34:40'),
777+
(66,54,1,0,'2019-12-13 16:35:13'),
778+
(67,54,1,0,'2019-12-13 16:35:46'),
779+
(68,54,1,0,'2019-12-13 16:36:19');
780+
SELECT t FROM t1 GROUP BY t HAVING t=max(t);
781+
SELECT t FROM t1 WHERE id2=54 and j=0 and k=1 GROUP BY t HAVING t=max(t);
782+
DROP TABLE t1;
783+
784+
CREATE TABLE t1 (pk INT);
785+
CREATE VIEW v1 AS SELECT * FROM t1;
786+
INSERT INTO t1 VALUES (1);
787+
SELECT pk<DATE'2001-01-01' FROM t1 GROUP BY pk;
788+
SELECT pk<DATE'2001-01-01' FROM v1 GROUP BY pk;
789+
DROP VIEW v1;
790+
DROP TABLE t1;
791+
792+
750793
--echo #
751794
--echo # End of 10.1 tests
752795
--echo #

mysql-test/t/type_time.test

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,40 @@ drop table t1;
746746
SET @@optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
747747
set @@old_mode= @save_old_mode;
748748

749+
--echo #
750+
--echo # MDEV-21319 COUNT(*) returns 1, actual SELECT returns no result in 10.3.21, but 1 result in 10.1.41
751+
--echo #
752+
753+
CREATE OR REPLACE TABLE t1
754+
(
755+
id INT NOT NULL PRIMARY KEY,
756+
id2 INT,
757+
k TINYINT,
758+
j INT,
759+
t TIME,
760+
KEY k1 (id2,k,j,t)
761+
);
762+
INSERT INTO t1 VALUES
763+
(53,54,1,0,'10:09:59'),
764+
(54,54,1,0,'16:28:41'),
765+
(55,54,1,0,'16:29:10'),
766+
(56,54,1,0,'16:29:43'),
767+
(57,54,1,0,'16:30:16'),
768+
(58,54,1,0,'16:30:49'),
769+
(59,54,1,0,'16:31:23'),
770+
(60,54,1,0,'16:31:55'),
771+
(61,54,1,0,'16:32:28'),
772+
(62,54,1,0,'16:33:01'),
773+
(63,54,1,0,'16:33:34'),
774+
(64,54,1,0,'16:34:07'),
775+
(65,54,1,0,'16:34:40'),
776+
(66,54,1,0,'16:35:13'),
777+
(67,54,1,0,'16:35:46'),
778+
(68,54,1,0,'16:36:19');
779+
SELECT t FROM t1 GROUP BY t HAVING t=MAX(t);
780+
SELECT t FROM t1 WHERE id2=54 AND j=0 AND k=1 GROUP BY t HAVING t=MAX(t);
781+
DROP TABLE t1;
782+
749783
--echo #
750784
--echo # End of 10.1 tests
751785
--echo #

sql/item.cc

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ bool Item::get_date_with_conversion(MYSQL_TIME *ltime, ulonglong fuzzydate)
145145
}
146146

147147

148+
longlong Item::val_datetime_packed_result()
149+
{
150+
MYSQL_TIME ltime, tmp;
151+
if (get_date_result(&ltime, TIME_FUZZY_DATES | TIME_INVALID_DATES))
152+
return 0;
153+
if (ltime.time_type != MYSQL_TIMESTAMP_TIME)
154+
return pack_time(&ltime);
155+
if ((null_value= time_to_datetime_with_warn(current_thd, &ltime, &tmp, 0)))
156+
return 0;
157+
return pack_time(&tmp);
158+
}
159+
160+
148161
/**
149162
Get date/time/datetime.
150163
If DATETIME or DATE result is returned, it's converted to TIME.
@@ -2594,12 +2607,13 @@ bool Item_field::get_date(MYSQL_TIME *ltime,ulonglong fuzzydate)
25942607

25952608
bool Item_field::get_date_result(MYSQL_TIME *ltime, ulonglong fuzzydate)
25962609
{
2597-
if (result_field->is_null() || result_field->get_date(ltime,fuzzydate))
2610+
if ((null_value= result_field->is_null()) ||
2611+
result_field->get_date(ltime, fuzzydate))
25982612
{
25992613
bzero((char*) ltime,sizeof(*ltime));
2600-
return (null_value= 1);
2614+
return true;
26012615
}
2602-
return (null_value= 0);
2616+
return false;
26032617
}
26042618

26052619

@@ -7313,7 +7327,7 @@ bool Item_ref::get_date(MYSQL_TIME *ltime,ulonglong fuzzydate)
73137327
longlong Item_ref::val_datetime_packed()
73147328
{
73157329
DBUG_ASSERT(fixed);
7316-
longlong tmp= (*ref)->val_datetime_packed();
7330+
longlong tmp= (*ref)->val_datetime_packed_result();
73177331
null_value= (*ref)->null_value;
73187332
return tmp;
73197333
}
@@ -7322,7 +7336,7 @@ longlong Item_ref::val_datetime_packed()
73227336
longlong Item_ref::val_time_packed()
73237337
{
73247338
DBUG_ASSERT(fixed);
7325-
longlong tmp= (*ref)->val_time_packed();
7339+
longlong tmp= (*ref)->val_time_packed_result();
73267340
null_value= (*ref)->null_value;
73277341
return tmp;
73287342
}

sql/item.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,13 @@ class Item: public Value_source,
13021302
uint fuzzydate= TIME_FUZZY_DATES | TIME_INVALID_DATES | TIME_TIME_ONLY;
13031303
return get_date(&ltime, fuzzydate) ? 0 : pack_time(&ltime);
13041304
}
1305+
longlong val_datetime_packed_result();
1306+
longlong val_time_packed_result()
1307+
{
1308+
MYSQL_TIME ltime;
1309+
uint fuzzydate= TIME_TIME_ONLY | TIME_INVALID_DATES | TIME_FUZZY_DATES;
1310+
return get_date_result(&ltime, fuzzydate) ? 0 : pack_time(&ltime);
1311+
}
13051312
// Get a temporal value in packed DATE/DATETIME or TIME format
13061313
longlong val_temporal_packed(enum_field_types f_type)
13071314
{

0 commit comments

Comments
 (0)