summaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
Diffstat (limited to 'sql')
-rw-r--r--sql/field.cc21
-rw-r--r--sql/item.cc12
-rw-r--r--sql/item.h33
-rw-r--r--sql/protocol.cc2
-rw-r--r--sql/sql_type.cc64
-rw-r--r--sql/sql_type.h15
-rw-r--r--sql/sql_type_real.h35
7 files changed, 147 insertions, 35 deletions
diff --git a/sql/field.cc b/sql/field.cc
index de1121bd322..c6bdb013cdf 100644
--- a/sql/field.cc
+++ b/sql/field.cc
@@ -4556,34 +4556,15 @@ String *Field_float::val_str(String *val_buffer,
{
ASSERT_COLUMN_MARKED_FOR_READ;
DBUG_ASSERT(!zerofill || field_length <= MAX_FIELD_CHARLENGTH);
- float nr;
- float4get(nr,ptr);
- uint to_length= 70;
- if (val_buffer->alloc(to_length))
+ if (Float(ptr).to_string(val_buffer, dec))
{
my_error(ER_OUT_OF_RESOURCES, MYF(0));
return val_buffer;
}
- char *to=(char*) val_buffer->ptr();
- size_t len;
-
- if (dec >= FLOATING_POINT_DECIMALS)
- len= my_gcvt(nr, MY_GCVT_ARG_FLOAT, to_length - 1, to, NULL);
- else
- {
- /*
- We are safe here because the buffer length is 70, and
- fabs(float) < 10^39, dec < FLOATING_POINT_DECIMALS. So the resulting string
- will be not longer than 69 chars + terminating '\0'.
- */
- len= my_fcvt(nr, dec, to, NULL);
- }
- val_buffer->length((uint) len);
if (zerofill)
prepend_zeros(val_buffer);
- val_buffer->set_charset(&my_charset_numeric);
return val_buffer;
}
diff --git a/sql/item.cc b/sql/item.cc
index 99335765066..81d474a014c 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -10153,7 +10153,7 @@ longlong Item_cache_real::val_int()
}
-String* Item_cache_real::val_str(String *str)
+String* Item_cache_double::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
if (!has_value())
@@ -10163,6 +10163,16 @@ String* Item_cache_real::val_str(String *str)
}
+String* Item_cache_float::val_str(String *str)
+{
+ DBUG_ASSERT(fixed == 1);
+ if (!has_value())
+ return NULL;
+ Float((float) value).to_string(str, decimals);
+ return str;
+}
+
+
my_decimal *Item_cache_real::val_decimal(my_decimal *decimal_val)
{
DBUG_ASSERT(fixed == 1);
diff --git a/sql/item.h b/sql/item.h
index f6dfbce10b2..2adc111db03 100644
--- a/sql/item.h
+++ b/sql/item.h
@@ -6186,21 +6186,44 @@ public:
class Item_cache_real: public Item_cache
{
+protected:
double value;
public:
- Item_cache_real(THD *thd): Item_cache(thd, &type_handler_double),
- value(0) {}
-
+ Item_cache_real(THD *thd, const Type_handler *h)
+ :Item_cache(thd, h),
+ value(0)
+ {}
double val_real();
longlong val_int();
- String* val_str(String *str);
my_decimal *val_decimal(my_decimal *);
bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
{ return get_date_from_real(ltime, fuzzydate); }
bool cache_value();
Item *convert_to_basic_const_item(THD *thd);
+};
+
+
+class Item_cache_double: public Item_cache_real
+{
+public:
+ Item_cache_double(THD *thd)
+ :Item_cache_real(thd, &type_handler_double)
+ { }
+ String* val_str(String *str);
+ Item *get_copy(THD *thd)
+ { return get_item_copy<Item_cache_double>(thd, this); }
+};
+
+
+class Item_cache_float: public Item_cache_real
+{
+public:
+ Item_cache_float(THD *thd)
+ :Item_cache_real(thd, &type_handler_float)
+ { }
+ String* val_str(String *str);
Item *get_copy(THD *thd)
- { return get_item_copy<Item_cache_real>(thd, this); }
+ { return get_item_copy<Item_cache_float>(thd, this); }
};
diff --git a/sql/protocol.cc b/sql/protocol.cc
index edd084c2132..84ca4585a12 100644
--- a/sql/protocol.cc
+++ b/sql/protocol.cc
@@ -1209,7 +1209,7 @@ bool Protocol_text::store(float from, uint32 decimals, String *buffer)
field_types[field_pos] == MYSQL_TYPE_FLOAT);
field_pos++;
#endif
- buffer->set_real((double) from, decimals, thd->charset());
+ Float(from).to_string(buffer, decimals);
return net_store_data((uchar*) buffer->ptr(), buffer->length());
}
diff --git a/sql/sql_type.cc b/sql/sql_type.cc
index 39d9fc5c2bd..b984dd8eb3b 100644
--- a/sql/sql_type.cc
+++ b/sql/sql_type.cc
@@ -125,6 +125,32 @@ bool Type_handler_data::init()
Type_handler_data *type_handler_data= NULL;
+bool Float::to_string(String *val_buffer, uint dec) const
+{
+ uint to_length= 70;
+ if (val_buffer->alloc(to_length))
+ return true;
+
+ char *to=(char*) val_buffer->ptr();
+ size_t len;
+
+ if (dec >= FLOATING_POINT_DECIMALS)
+ len= my_gcvt(m_value, MY_GCVT_ARG_FLOAT, to_length - 1, to, NULL);
+ else
+ {
+ /*
+ We are safe here because the buffer length is 70, and
+ fabs(float) < 10^39, dec < FLOATING_POINT_DECIMALS. So the resulting string
+ will be not longer than 69 chars + terminating '\0'.
+ */
+ len= my_fcvt(m_value, (int) dec, to, NULL);
+ }
+ val_buffer->length((uint) len);
+ val_buffer->set_charset(&my_charset_numeric);
+ return false;
+}
+
+
void Time::make_from_item(Item *item, const Options opt)
{
if (item->get_date(this, opt.get_date_flags()))
@@ -2708,9 +2734,15 @@ Type_handler_year::Item_get_cache(THD *thd, const Item *item) const
}
Item_cache *
-Type_handler_real_result::Item_get_cache(THD *thd, const Item *item) const
+Type_handler_double::Item_get_cache(THD *thd, const Item *item) const
+{
+ return new (thd->mem_root) Item_cache_double(thd);
+}
+
+Item_cache *
+Type_handler_float::Item_get_cache(THD *thd, const Item *item) const
{
- return new (thd->mem_root) Item_cache_real(thd);
+ return new (thd->mem_root) Item_cache_float(thd);
}
Item_cache *
@@ -3575,7 +3607,7 @@ Type_handler_int_result::Item_func_hybrid_field_type_get_date(
/***************************************************************************/
String *
-Type_handler_real_result::Item_func_hybrid_field_type_val_str(
+Type_handler_double::Item_func_hybrid_field_type_val_str(
Item_func_hybrid_field_type *item,
String *str) const
{
@@ -3583,6 +3615,19 @@ Type_handler_real_result::Item_func_hybrid_field_type_val_str(
}
+String *
+Type_handler_float::Item_func_hybrid_field_type_val_str(
+ Item_func_hybrid_field_type *item,
+ String *str) const
+{
+ Float nr(item->real_op());
+ if (item->null_value)
+ return 0;
+ nr.to_string(str, item->decimals);
+ return str;
+}
+
+
double
Type_handler_real_result::Item_func_hybrid_field_type_val_real(
Item_func_hybrid_field_type *item)
@@ -4042,13 +4087,24 @@ String *Type_handler_decimal_result::
}
-String *Type_handler_real_result::
+String *Type_handler_double::
Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
{
return func->val_string_from_real(str);
}
+String *Type_handler_float::
+ Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
+{
+ Float nr(func->val_real());
+ if (func->null_value)
+ return 0;
+ nr.to_string(str, func->decimals);
+ return str;
+}
+
+
double Type_handler_string_result::
Item_func_min_max_val_real(Item_func_min_max *func) const
{
diff --git a/sql/sql_type.h b/sql/sql_type.h
index 3a5a00231ca..b317832880a 100644
--- a/sql/sql_type.h
+++ b/sql/sql_type.h
@@ -25,6 +25,7 @@
#include "sql_array.h"
#include "sql_const.h"
#include "sql_time.h"
+#include "sql_type_real.h"
class Field;
class Column_definition;
@@ -1779,7 +1780,6 @@ public:
const st_value *value) const;
int Item_save_in_field(Item *item, Field *field, bool no_conversions) const;
Item *make_const_item_for_comparison(THD *, Item *src, const Item *cmp) const;
- Item_cache *Item_get_cache(THD *thd, const Item *item) const;
bool set_comparator_func(Arg_comparator *cmp) const;
bool Item_hybrid_func_fix_attributes(THD *thd,
const char *name,
@@ -1799,8 +1799,6 @@ public:
longlong Item_val_int_signed_typecast(Item *item) const;
longlong Item_val_int_unsigned_typecast(Item *item) const;
String *Item_func_hex_val_str_ascii(Item_func_hex *item, String *str) const;
- String *Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type *,
- String *) const;
double Item_func_hybrid_field_type_val_real(Item_func_hybrid_field_type *)
const;
longlong Item_func_hybrid_field_type_val_int(Item_func_hybrid_field_type *)
@@ -1811,7 +1809,6 @@ public:
bool Item_func_hybrid_field_type_get_date(Item_func_hybrid_field_type *,
MYSQL_TIME *,
ulonglong fuzzydate) const;
- String *Item_func_min_max_val_str(Item_func_min_max *, String *) const;
longlong Item_func_between_val_int(Item_func_between *func) const;
cmp_item *make_cmp_item(THD *thd, CHARSET_INFO *cs) const;
in_vector *make_in_vector(THD *, const Item_func_in *, uint nargs) const;
@@ -2611,6 +2608,11 @@ public:
TABLE *table) const;
void Item_param_set_param_func(Item_param *param,
uchar **pos, ulong len) const;
+
+ Item_cache *Item_get_cache(THD *thd, const Item *item) const;
+ String *Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type *,
+ String *) const;
+ String *Item_func_min_max_val_str(Item_func_min_max *, String *) const;
};
@@ -2643,6 +2645,11 @@ public:
TABLE *table) const;
void Item_param_set_param_func(Item_param *param,
uchar **pos, ulong len) const;
+
+ Item_cache *Item_get_cache(THD *thd, const Item *item) const;
+ String *Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type *,
+ String *) const;
+ String *Item_func_min_max_val_str(Item_func_min_max *, String *) const;
};
diff --git a/sql/sql_type_real.h b/sql/sql_type_real.h
new file mode 100644
index 00000000000..f1024ac453e
--- /dev/null
+++ b/sql/sql_type_real.h
@@ -0,0 +1,35 @@
+/* Copyright (c) 2019 MariaDB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
+
+#ifndef SQL_TYPE_REAL_INCLUDED
+#define SQL_TYPE_REAL_INCLUDED
+
+
+class Float
+{
+ float m_value;
+public:
+ Float(float nr)
+ :m_value(nr)
+ { }
+ Float(const uchar *ptr)
+ {
+ float4get(m_value, ptr);
+ }
+ bool to_string(String *to, uint dec) const;
+};
+
+
+#endif // SQL_TYPE_REAL_INCLUDED