Skip to content

Commit 0c6455a

Browse files
committed
MDEV-17249 MAKETIME(-1e50,0,0) returns a wrong result
1 parent e43bc02 commit 0c6455a

File tree

13 files changed

+219
-32
lines changed

13 files changed

+219
-32
lines changed

mysql-test/r/func_time.result

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,5 +2661,50 @@ SELECT PERIOD_DIFF(2018, AES_ENCRYPT('Rae Bareli', 'Rae Bareli'));
26612661
PERIOD_DIFF(2018, AES_ENCRYPT('Rae Bareli', 'Rae Bareli'))
26622662
24257
26632663
#
2664+
# MDEV-17249 MAKETIME(-1e50,0,0) returns a wrong result
2665+
#
2666+
SELECT
2667+
MAKETIME(1e10,0,0),
2668+
MAKETIME(-1e10,0,0),
2669+
MAKETIME(1e50,0,0),
2670+
MAKETIME(-1e50,0,0),
2671+
MAKETIME(COALESCE(1e50),0,0),
2672+
MAKETIME(COALESCE(-1e50),0,0);
2673+
MAKETIME(1e10,0,0) 838:59:59
2674+
MAKETIME(-1e10,0,0) -838:59:59
2675+
MAKETIME(1e50,0,0) 838:59:59
2676+
MAKETIME(-1e50,0,0) -838:59:59
2677+
MAKETIME(COALESCE(1e50),0,0) 838:59:59
2678+
MAKETIME(COALESCE(-1e50),0,0) -838:59:59
2679+
Warnings:
2680+
Level Warning
2681+
Code 1292
2682+
Message Truncated incorrect time value: '10000000000:00:00'
2683+
Level Warning
2684+
Code 1292
2685+
Message Truncated incorrect time value: '-10000000000:00:00'
2686+
Level Warning
2687+
Code 1292
2688+
Message Truncated incorrect time value: '9223372036854775807:00:00'
2689+
Level Warning
2690+
Code 1292
2691+
Message Truncated incorrect time value: '-9223372036854775808:00:00'
2692+
Level Warning
2693+
Code 1292
2694+
Message Truncated incorrect time value: '9223372036854775807:00:00'
2695+
Level Warning
2696+
Code 1292
2697+
Message Truncated incorrect time value: '-9223372036854775808:00:00'
2698+
CREATE TABLE t1 (a FLOAT);
2699+
INSERT INTO t1 VALUES (1e30),(-1e30);
2700+
SELECT MAKETIME(a,0,0) FROM t1;
2701+
MAKETIME(a,0,0)
2702+
838:59:59
2703+
-838:59:59
2704+
Warnings:
2705+
Warning 1292 Truncated incorrect time value: '9223372036854775807:00:00'
2706+
Warning 1292 Truncated incorrect time value: '-9223372036854775808:00:00'
2707+
DROP TABLE t1;
2708+
#
26642709
# End of 5.5 tests
26652710
#

mysql-test/r/type_float.result

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,39 @@ End of 5.0 tests
447447
select format(truncate('1.7976931348623157E+308',-12),1,'fr_BE') as foo;
448448
foo
449449
0
450+
#
451+
# MDEV-17249 MAKETIME(-1e50,0,0) returns a wrong result
452+
#
453+
SELECT LEFT('a',EXP(50));
454+
LEFT('a',EXP(50))
455+
a
456+
SELECT LEFT('a', COALESCE(1e30));
457+
LEFT('a', COALESCE(1e30))
458+
a
459+
CREATE TABLE t1 (a FLOAT);
460+
INSERT INTO t1 VALUES (1e30);
461+
SELECT LEFT('a',a), LEFT('a',1e30) FROM t1;
462+
LEFT('a',a) LEFT('a',1e30)
463+
a a
464+
DROP TABLE t1;
465+
PREPARE stmt FROM 'SELECT LEFT(111,?)';
466+
SET @a=1e30;
467+
EXECUTE stmt USING @a;
468+
LEFT(111,?)
469+
111
470+
DEALLOCATE PREPARE stmt;
471+
CREATE TABLE t1 (a INT);
472+
INSERT INTO t1 VALUES (1),(2),(3);
473+
SELECT LEFT('a',(SELECT 1e30 FROM t1 LIMIT 1));
474+
LEFT('a',(SELECT 1e30 FROM t1 LIMIT 1))
475+
a
476+
DROP TABLE t1;
477+
CREATE TABLE t1 (a DOUBLE);
478+
INSERT INTO t1 VALUES (1e30),(0);
479+
SELECT LEFT('a', SUM(a)) FROM t1;
480+
LEFT('a', SUM(a))
481+
a
482+
SELECT LEFT('a', AVG(a)) FROM t1;
483+
LEFT('a', AVG(a))
484+
a
485+
DROP TABLE t1;

mysql-test/t/func_time.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,27 @@ SELECT SEC_TO_TIME(MAKEDATE(0,RAND(~0)));
16341634
#
16351635
SELECT PERIOD_DIFF(2018, AES_ENCRYPT('Rae Bareli', 'Rae Bareli'));
16361636

1637+
1638+
--echo #
1639+
--echo # MDEV-17249 MAKETIME(-1e50,0,0) returns a wrong result
1640+
--echo #
1641+
1642+
--vertical_results
1643+
SELECT
1644+
MAKETIME(1e10,0,0),
1645+
MAKETIME(-1e10,0,0),
1646+
MAKETIME(1e50,0,0),
1647+
MAKETIME(-1e50,0,0),
1648+
MAKETIME(COALESCE(1e50),0,0),
1649+
MAKETIME(COALESCE(-1e50),0,0);
1650+
--horizontal_results
1651+
1652+
CREATE TABLE t1 (a FLOAT);
1653+
INSERT INTO t1 VALUES (1e30),(-1e30);
1654+
SELECT MAKETIME(a,0,0) FROM t1;
1655+
DROP TABLE t1;
1656+
1657+
16371658
--echo #
16381659
--echo # End of 5.5 tests
16391660
--echo #

mysql-test/t/type_float.test

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,32 @@ eval select concat((truncate((-1.7976931348623157E+307),(0x1e))),
331331
--echo #
332332

333333
select format(truncate('1.7976931348623157E+308',-12),1,'fr_BE') as foo;
334+
335+
336+
--echo #
337+
--echo # MDEV-17249 MAKETIME(-1e50,0,0) returns a wrong result
338+
--echo #
339+
340+
SELECT LEFT('a',EXP(50));
341+
SELECT LEFT('a', COALESCE(1e30));
342+
343+
CREATE TABLE t1 (a FLOAT);
344+
INSERT INTO t1 VALUES (1e30);
345+
SELECT LEFT('a',a), LEFT('a',1e30) FROM t1;
346+
DROP TABLE t1;
347+
348+
PREPARE stmt FROM 'SELECT LEFT(111,?)';
349+
SET @a=1e30;
350+
EXECUTE stmt USING @a;
351+
DEALLOCATE PREPARE stmt;
352+
353+
CREATE TABLE t1 (a INT);
354+
INSERT INTO t1 VALUES (1),(2),(3);
355+
SELECT LEFT('a',(SELECT 1e30 FROM t1 LIMIT 1));
356+
DROP TABLE t1;
357+
358+
CREATE TABLE t1 (a DOUBLE);
359+
INSERT INTO t1 VALUES (1e30),(0);
360+
SELECT LEFT('a', SUM(a)) FROM t1;
361+
SELECT LEFT('a', AVG(a)) FROM t1;
362+
DROP TABLE t1;

sql/field.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3948,7 +3948,8 @@ longlong Field_float::val_int(void)
39483948
{
39493949
float j;
39503950
float4get(j,ptr);
3951-
return (longlong) rint(j);
3951+
bool error;
3952+
return double_to_longlong(j, false, &error);
39523953
}
39533954

39543955

sql/item.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3692,7 +3692,10 @@ longlong Item_param::val_int()
36923692
{
36933693
switch (state) {
36943694
case REAL_VALUE:
3695-
return (longlong) rint(value.real);
3695+
{
3696+
bool error;
3697+
return double_to_longlong(value.real, unsigned_flag, &error);
3698+
}
36963699
case INT_VALUE:
36973700
return value.integer;
36983701
case DECIMAL_VALUE:
@@ -9231,7 +9234,8 @@ longlong Item_cache_real::val_int()
92319234
DBUG_ASSERT(fixed == 1);
92329235
if (!has_value())
92339236
return 0;
9234-
return (longlong) rint(value);
9237+
bool error;
9238+
return double_to_longlong(value, unsigned_flag, &error);
92359239
}
92369240

92379241

sql/item_func.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,10 @@ longlong Item_func_hybrid_result_type::val_int()
999999
case INT_RESULT:
10001000
return int_op();
10011001
case REAL_RESULT:
1002-
return (longlong) rint(real_op());
1002+
{
1003+
bool error;
1004+
return double_to_longlong(real_op(), unsigned_flag, &error);
1005+
}
10031006
case STRING_RESULT:
10041007
{
10051008
if (is_temporal_type(field_type()))

sql/item_func.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,11 @@ class Item_real_func :public Item_func
411411
String *val_str(String*str);
412412
my_decimal *val_decimal(my_decimal *decimal_value);
413413
longlong val_int()
414-
{ DBUG_ASSERT(fixed == 1); return (longlong) rint(val_real()); }
414+
{
415+
DBUG_ASSERT(fixed == 1);
416+
bool error;
417+
return double_to_longlong(val_real(), unsigned_flag, &error);
418+
}
415419
enum Item_result result_type () const { return REAL_RESULT; }
416420
void fix_length_and_dec()
417421
{ decimals= NOT_FIXED_DEC; max_length= float_length(decimals); }
@@ -1465,7 +1469,9 @@ class Item_func_udf_float :public Item_udf_func
14651469
longlong val_int()
14661470
{
14671471
DBUG_ASSERT(fixed == 1);
1468-
return (longlong) rint(Item_func_udf_float::val_real());
1472+
bool error;
1473+
return double_to_longlong(Item_func_udf_float::val_real(),
1474+
unsigned_flag, &error);
14691475
}
14701476
my_decimal *val_decimal(my_decimal *dec_buf)
14711477
{

sql/item_sum.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,8 @@ longlong Item_sum_sum::val_int()
14341434
&result);
14351435
return result;
14361436
}
1437-
return (longlong) rint(val_real());
1437+
bool error;
1438+
return double_to_longlong(val_real(), unsigned_flag, &error);
14381439
}
14391440

14401441

@@ -2648,7 +2649,8 @@ double Item_avg_field::val_real()
26482649

26492650
longlong Item_avg_field::val_int()
26502651
{
2651-
return (longlong) rint(val_real());
2652+
bool error;
2653+
return double_to_longlong(val_real(), unsigned_flag, &error);
26522654
}
26532655

26542656

sql/item_sum.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,9 @@ class Item_sum_num :public Item_sum
699699
longlong val_int()
700700
{
701701
DBUG_ASSERT(fixed == 1);
702-
return (longlong) rint(val_real()); /* Real as default */
702+
// Real as default
703+
bool error;
704+
return double_to_longlong(val_real(), unsigned_flag, &error);
703705
}
704706
String *val_str(String*str);
705707
my_decimal *val_decimal(my_decimal *);
@@ -867,7 +869,11 @@ class Item_sum_avg :public Item_sum_sum
867869
bool add();
868870
double val_real();
869871
// In SPs we might force the "wrong" type with select into a declare variable
870-
longlong val_int() { return (longlong) rint(val_real()); }
872+
longlong val_int()
873+
{
874+
bool error;
875+
return double_to_longlong(val_real(), unsigned_flag, &error);
876+
}
871877
my_decimal *val_decimal(my_decimal *);
872878
String *val_str(String *str);
873879
void reset_field();
@@ -904,7 +910,10 @@ class Item_variance_field :public Item_result_field
904910
enum Type type() const {return FIELD_VARIANCE_ITEM; }
905911
double val_real();
906912
longlong val_int()
907-
{ /* can't be fix_fields()ed */ return (longlong) rint(val_real()); }
913+
{ /* can't be fix_fields()ed */
914+
bool error;
915+
return double_to_longlong(val_real(), unsigned_flag, &error);
916+
}
908917
String *val_str(String *str)
909918
{ return val_string_from_real(str); }
910919
my_decimal *val_decimal(my_decimal *dec_buf)
@@ -1215,7 +1224,9 @@ class Item_sum_udf_float :public Item_udf_sum
12151224
longlong val_int()
12161225
{
12171226
DBUG_ASSERT(fixed == 1);
1218-
return (longlong) rint(Item_sum_udf_float::val_real());
1227+
bool error;
1228+
return double_to_longlong(Item_sum_udf_float::val_real(),
1229+
unsigned_flag, &error);
12191230
}
12201231
double val_real();
12211232
String *val_str(String*str);

0 commit comments

Comments
 (0)