Skip to content

Commit 462d689

Browse files
committed
MDEV-19468 Hybrid type expressions return wrong format for FLOAT
1 parent 4937339 commit 462d689

17 files changed

+255
-69
lines changed

mysql-test/main/derived_cond_pushdown.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7884,7 +7884,7 @@ EXPLAIN
78847884
"access_type": "ALL",
78857885
"rows": 3,
78867886
"filtered": 100,
7887-
"attached_condition": "sq.i = 2.7100000381469727",
7887+
"attached_condition": "sq.i = 2.71",
78887888
"materialized": {
78897889
"query_block": {
78907890
"select_id": 2,

mysql-test/main/select.result

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,26 +2789,26 @@ id select_type table type possible_keys key key_len ref rows Extra
27892789
1 SIMPLE t1 range key1 key1 5 NULL 3 Using where; Using index
27902790
select max(key1) from t1 where key1 <= 0.6158;
27912791
max(key1)
2792-
0.6158000230789185
2792+
0.6158
27932793
select max(key2) from t2 where key2 <= 1.6158;
27942794
max(key2)
2795-
1.6158000230789185
2795+
1.6158
27962796
select min(key1) from t1 where key1 >= 0.3762;
27972797
min(key1)
2798-
0.37619999051094055
2798+
0.3762
27992799
select min(key2) from t2 where key2 >= 1.3762;
28002800
min(key2)
2801-
1.3761999607086182
2801+
1.3762
28022802
select max(key1), min(key2) from t1, t2
28032803
where key1 <= 0.6158 and key2 >= 1.3762;
28042804
max(key1) min(key2)
2805-
0.61580002307891851.3761999607086182
2805+
0.61581.3762
28062806
select max(key1) from t1 where key1 <= 0.6158 and rand() + 0.5 >= 0.5;
28072807
max(key1)
2808-
0.38449999690055847
2808+
0.3845
28092809
select min(key1) from t1 where key1 >= 0.3762 and rand() + 0.5 >= 0.5;
28102810
min(key1)
2811-
0.38449999690055847
2811+
0.3845
28122812
DROP TABLE t1,t2;
28132813
CREATE TABLE t1 (i BIGINT UNSIGNED NOT NULL);
28142814
INSERT INTO t1 VALUES (10);

mysql-test/main/select_jcl6.result

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,26 +2800,26 @@ id select_type table type possible_keys key key_len ref rows Extra
28002800
1 SIMPLE t1 range key1 key1 5 NULL 3 Using where; Using index
28012801
select max(key1) from t1 where key1 <= 0.6158;
28022802
max(key1)
2803-
0.6158000230789185
2803+
0.6158
28042804
select max(key2) from t2 where key2 <= 1.6158;
28052805
max(key2)
2806-
1.6158000230789185
2806+
1.6158
28072807
select min(key1) from t1 where key1 >= 0.3762;
28082808
min(key1)
2809-
0.37619999051094055
2809+
0.3762
28102810
select min(key2) from t2 where key2 >= 1.3762;
28112811
min(key2)
2812-
1.3761999607086182
2812+
1.3762
28132813
select max(key1), min(key2) from t1, t2
28142814
where key1 <= 0.6158 and key2 >= 1.3762;
28152815
max(key1) min(key2)
2816-
0.61580002307891851.3761999607086182
2816+
0.61581.3762
28172817
select max(key1) from t1 where key1 <= 0.6158 and rand() + 0.5 >= 0.5;
28182818
max(key1)
2819-
0.38449999690055847
2819+
0.3845
28202820
select min(key1) from t1 where key1 >= 0.3762 and rand() + 0.5 >= 0.5;
28212821
min(key1)
2822-
0.38449999690055847
2822+
0.3845
28232823
DROP TABLE t1,t2;
28242824
CREATE TABLE t1 (i BIGINT UNSIGNED NOT NULL);
28252825
INSERT INTO t1 VALUES (10);

mysql-test/main/select_pkeycache.result

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,26 +2789,26 @@ id select_type table type possible_keys key key_len ref rows Extra
27892789
1 SIMPLE t1 range key1 key1 5 NULL 3 Using where; Using index
27902790
select max(key1) from t1 where key1 <= 0.6158;
27912791
max(key1)
2792-
0.6158000230789185
2792+
0.6158
27932793
select max(key2) from t2 where key2 <= 1.6158;
27942794
max(key2)
2795-
1.6158000230789185
2795+
1.6158
27962796
select min(key1) from t1 where key1 >= 0.3762;
27972797
min(key1)
2798-
0.37619999051094055
2798+
0.3762
27992799
select min(key2) from t2 where key2 >= 1.3762;
28002800
min(key2)
2801-
1.3761999607086182
2801+
1.3762
28022802
select max(key1), min(key2) from t1, t2
28032803
where key1 <= 0.6158 and key2 >= 1.3762;
28042804
max(key1) min(key2)
2805-
0.61580002307891851.3761999607086182
2805+
0.61581.3762
28062806
select max(key1) from t1 where key1 <= 0.6158 and rand() + 0.5 >= 0.5;
28072807
max(key1)
2808-
0.38449999690055847
2808+
0.3845
28092809
select min(key1) from t1 where key1 >= 0.3762 and rand() + 0.5 >= 0.5;
28102810
min(key1)
2811-
0.38449999690055847
2811+
0.3845
28122812
DROP TABLE t1,t2;
28132813
CREATE TABLE t1 (i BIGINT UNSIGNED NOT NULL);
28142814
INSERT INTO t1 VALUES (10);

mysql-test/main/type_float.result

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,3 +840,40 @@ DROP TABLE t1;
840840
#
841841
# End of 10.2 tests
842842
#
843+
#
844+
# Start of 10.3 tests
845+
#
846+
#
847+
# MDEV-19468 Hybrid type expressions return wrong format for FLOAT
848+
#
849+
CREATE TABLE t1 (a FLOAT);
850+
INSERT INTO t1 VALUES (0.671437);
851+
SELECT a, COALESCE(a), MAX(a), LEAST(a,a), (SELECT a FROM t1) AS c FROM t1;
852+
a COALESCE(a) MAX(a) LEAST(a,a) c
853+
0.671437 0.671437 0.671437 0.671437 0.671437
854+
DROP TABLE t1;
855+
CREATE TABLE t1 (a FLOAT);
856+
INSERT INTO t1 VALUES (0.671437);
857+
SELECT
858+
CONCAT(a),
859+
CONCAT(COALESCE(a)),
860+
CONCAT(LEAST(a,a)),
861+
CONCAT(MAX(a)),
862+
CONCAT((SELECT a FROM t1)) AS c
863+
FROM t1;
864+
CONCAT(a) CONCAT(COALESCE(a)) CONCAT(LEAST(a,a)) CONCAT(MAX(a)) c
865+
0.671437 0.671437 0.671437 0.671437 0.671437
866+
CREATE TABLE t2 AS SELECT
867+
CONCAT(a),
868+
CONCAT(COALESCE(a)),
869+
CONCAT(LEAST(a,a)),
870+
CONCAT(MAX(a)),
871+
CONCAT((SELECT a FROM t1)) AS c
872+
FROM t1;
873+
SELECT * FROM t2;
874+
CONCAT(a) CONCAT(COALESCE(a)) CONCAT(LEAST(a,a)) CONCAT(MAX(a)) c
875+
0.671437 0.671437 0.671437 0.671437 0.671437
876+
DROP TABLE t1, t2;
877+
#
878+
# End of 10.3 tests
879+
#

mysql-test/main/type_float.test

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,40 @@ DROP TABLE t1;
581581
--echo #
582582
--echo # End of 10.2 tests
583583
--echo #
584+
585+
--echo #
586+
--echo # Start of 10.3 tests
587+
--echo #
588+
589+
--echo #
590+
--echo # MDEV-19468 Hybrid type expressions return wrong format for FLOAT
591+
--echo #
592+
593+
CREATE TABLE t1 (a FLOAT);
594+
INSERT INTO t1 VALUES (0.671437);
595+
SELECT a, COALESCE(a), MAX(a), LEAST(a,a), (SELECT a FROM t1) AS c FROM t1;
596+
DROP TABLE t1;
597+
598+
CREATE TABLE t1 (a FLOAT);
599+
INSERT INTO t1 VALUES (0.671437);
600+
SELECT
601+
CONCAT(a),
602+
CONCAT(COALESCE(a)),
603+
CONCAT(LEAST(a,a)),
604+
CONCAT(MAX(a)),
605+
CONCAT((SELECT a FROM t1)) AS c
606+
FROM t1;
607+
CREATE TABLE t2 AS SELECT
608+
CONCAT(a),
609+
CONCAT(COALESCE(a)),
610+
CONCAT(LEAST(a,a)),
611+
CONCAT(MAX(a)),
612+
CONCAT((SELECT a FROM t1)) AS c
613+
FROM t1;
614+
SELECT * FROM t2;
615+
DROP TABLE t1, t2;
616+
617+
618+
--echo #
619+
--echo # End of 10.3 tests
620+
--echo #

sql/field.cc

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4556,34 +4556,15 @@ String *Field_float::val_str(String *val_buffer,
45564556
{
45574557
ASSERT_COLUMN_MARKED_FOR_READ;
45584558
DBUG_ASSERT(!zerofill || field_length <= MAX_FIELD_CHARLENGTH);
4559-
float nr;
4560-
float4get(nr,ptr);
45614559

4562-
uint to_length= 70;
4563-
if (val_buffer->alloc(to_length))
4560+
if (Float(ptr).to_string(val_buffer, dec))
45644561
{
45654562
my_error(ER_OUT_OF_RESOURCES, MYF(0));
45664563
return val_buffer;
45674564
}
45684565

4569-
char *to=(char*) val_buffer->ptr();
4570-
size_t len;
4571-
4572-
if (dec >= FLOATING_POINT_DECIMALS)
4573-
len= my_gcvt(nr, MY_GCVT_ARG_FLOAT, to_length - 1, to, NULL);
4574-
else
4575-
{
4576-
/*
4577-
We are safe here because the buffer length is 70, and
4578-
fabs(float) < 10^39, dec < FLOATING_POINT_DECIMALS. So the resulting string
4579-
will be not longer than 69 chars + terminating '\0'.
4580-
*/
4581-
len= my_fcvt(nr, dec, to, NULL);
4582-
}
4583-
val_buffer->length((uint) len);
45844566
if (zerofill)
45854567
prepend_zeros(val_buffer);
4586-
val_buffer->set_charset(&my_charset_numeric);
45874568
return val_buffer;
45884569
}
45894570

sql/item.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10153,7 +10153,7 @@ longlong Item_cache_real::val_int()
1015310153
}
1015410154

1015510155

10156-
String* Item_cache_real::val_str(String *str)
10156+
String* Item_cache_double::val_str(String *str)
1015710157
{
1015810158
DBUG_ASSERT(fixed == 1);
1015910159
if (!has_value())
@@ -10163,6 +10163,16 @@ String* Item_cache_real::val_str(String *str)
1016310163
}
1016410164

1016510165

10166+
String* Item_cache_float::val_str(String *str)
10167+
{
10168+
DBUG_ASSERT(fixed == 1);
10169+
if (!has_value())
10170+
return NULL;
10171+
Float((float) value).to_string(str, decimals);
10172+
return str;
10173+
}
10174+
10175+
1016610176
my_decimal *Item_cache_real::val_decimal(my_decimal *decimal_val)
1016710177
{
1016810178
DBUG_ASSERT(fixed == 1);

sql/item.h

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6186,21 +6186,44 @@ class Item_cache_date: public Item_cache_temporal
61866186

61876187
class Item_cache_real: public Item_cache
61886188
{
6189+
protected:
61896190
double value;
61906191
public:
6191-
Item_cache_real(THD *thd): Item_cache(thd, &type_handler_double),
6192-
value(0) {}
6193-
6192+
Item_cache_real(THD *thd, const Type_handler *h)
6193+
:Item_cache(thd, h),
6194+
value(0)
6195+
{}
61946196
double val_real();
61956197
longlong val_int();
6196-
String* val_str(String *str);
61976198
my_decimal *val_decimal(my_decimal *);
61986199
bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
61996200
{ return get_date_from_real(ltime, fuzzydate); }
62006201
bool cache_value();
62016202
Item *convert_to_basic_const_item(THD *thd);
6203+
};
6204+
6205+
6206+
class Item_cache_double: public Item_cache_real
6207+
{
6208+
public:
6209+
Item_cache_double(THD *thd)
6210+
:Item_cache_real(thd, &type_handler_double)
6211+
{ }
6212+
String* val_str(String *str);
6213+
Item *get_copy(THD *thd)
6214+
{ return get_item_copy<Item_cache_double>(thd, this); }
6215+
};
6216+
6217+
6218+
class Item_cache_float: public Item_cache_real
6219+
{
6220+
public:
6221+
Item_cache_float(THD *thd)
6222+
:Item_cache_real(thd, &type_handler_float)
6223+
{ }
6224+
String* val_str(String *str);
62026225
Item *get_copy(THD *thd)
6203-
{ return get_item_copy<Item_cache_real>(thd, this); }
6226+
{ return get_item_copy<Item_cache_float>(thd, this); }
62046227
};
62056228

62066229

sql/protocol.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ bool Protocol_text::store(float from, uint32 decimals, String *buffer)
12091209
field_types[field_pos] == MYSQL_TYPE_FLOAT);
12101210
field_pos++;
12111211
#endif
1212-
buffer->set_real((double) from, decimals, thd->charset());
1212+
Float(from).to_string(buffer, decimals);
12131213
return net_store_data((uchar*) buffer->ptr(), buffer->length());
12141214
}
12151215

0 commit comments

Comments
 (0)