Skip to content

Commit 3f952e0

Browse files
committed
Bug#29929664: REMOVE STRING PARAMETER FROM PROTOCOL'S STORE_FLOAT AND STORE_DOUBLE
Protocol::store_float() and Protocol::store_double() have a String parameter, which is used by Protocol_text for converting the floating-point number to text, and which is not used by the other protocols. This patch makes Protocol_text use a sufficiently large char buffer on the stack instead, and removes the String parameter. Change-Id: Ie76e4dfd5ce461e6c354fa263cd97a4ff390535d
1 parent 0a6362b commit 3f952e0

12 files changed

+78
-99
lines changed

include/sql_string.h

-11
Original file line numberDiff line numberDiff line change
@@ -335,17 +335,6 @@ class String {
335335
*/
336336
bool set_real(double num, uint decimals, const CHARSET_INFO *cs);
337337

338-
/**
339-
Sets the contents of this string to the string representation of the given
340-
float value.
341-
342-
@param num the float value
343-
@param decimals the number of decimals
344-
@param cs the character set of the string
345-
@return false on success, true on error
346-
*/
347-
bool set_float(float num, uint decimals, const CHARSET_INFO *cs);
348-
349338
/*
350339
PMG 2004.11.12
351340
This is a method that works the same as perl's "chop". It simply

sql-common/sql_string.cc

+5-28
Original file line numberDiff line numberDiff line change
@@ -173,40 +173,17 @@ bool String::set_int(longlong num, bool unsigned_flag, const CHARSET_INFO *cs) {
173173
return false;
174174
}
175175

176-
/**
177-
Sets the contents of this string to be the string representation of the given
178-
floating-point number.
179-
180-
@param num the floating-point number
181-
@param decimals the number of decimals
182-
@param cs the character set of the string
183-
@param gcvt_arg_type MY_GCVT_ARG_FLOAT if num is to be interpreted as a float,
184-
MY_GCVT_ARG_DOUBLE if it is to be interpreted as a double
185-
@param destination the string to modify
186-
@return false on success, true on error
187-
*/
188-
static bool set_floating_point(double num, uint decimals,
189-
const CHARSET_INFO *cs,
190-
my_gcvt_arg_type gcvt_arg_type,
191-
String *destination) {
176+
bool String::set_real(double num, uint decimals, const CHARSET_INFO *cs) {
192177
char buff[FLOATING_POINT_BUFFER];
193178
uint dummy_errors;
194179

195180
if (decimals >= DECIMAL_NOT_SPECIFIED) {
196-
size_t len = my_gcvt(num, gcvt_arg_type, static_cast<int>(sizeof(buff)) - 1,
197-
buff, nullptr);
198-
return destination->copy(buff, len, &my_charset_latin1, cs, &dummy_errors);
181+
size_t len = my_gcvt(num, MY_GCVT_ARG_DOUBLE,
182+
static_cast<int>(sizeof(buff)) - 1, buff, nullptr);
183+
return copy(buff, len, &my_charset_latin1, cs, &dummy_errors);
199184
}
200185
size_t len = my_fcvt(num, decimals, buff, nullptr);
201-
return destination->copy(buff, len, &my_charset_latin1, cs, &dummy_errors);
202-
}
203-
204-
bool String::set_real(double num, uint decimals, const CHARSET_INFO *cs) {
205-
return set_floating_point(num, decimals, cs, MY_GCVT_ARG_DOUBLE, this);
206-
}
207-
208-
bool String::set_float(float num, uint decimals, const CHARSET_INFO *cs) {
209-
return set_floating_point(num, decimals, cs, MY_GCVT_ARG_FLOAT, this);
186+
return copy(buff, len, &my_charset_latin1, cs, &dummy_errors);
210187
}
211188

212189
bool String::copy() {

sql/field.cc

+2-4
Original file line numberDiff line numberDiff line change
@@ -4277,9 +4277,8 @@ size_t Field_float::make_sort_key(uchar *to, size_t length) const {
42774277
bool Field_float::send_to_protocol(Protocol *protocol) const {
42784278
ASSERT_COLUMN_MARKED_FOR_READ;
42794279
if (is_null()) return protocol->store_null();
4280-
StringBuffer<FLOATING_POINT_BUFFER> buffer;
42814280
return protocol->store_float(static_cast<float>(Field_float::val_real()), dec,
4282-
zerofill ? field_length : 0, &buffer);
4281+
zerofill ? field_length : 0);
42834282
}
42844283

42854284
/**
@@ -4500,9 +4499,8 @@ String *Field_double::val_str(String *val_buffer,
45004499

45014500
bool Field_double::send_to_protocol(Protocol *protocol) const {
45024501
if (is_null()) return protocol->store_null();
4503-
StringBuffer<FLOATING_POINT_BUFFER> buffer;
45044502
return protocol->store_double(Field_double::val_real(), dec,
4505-
zerofill ? field_length : 0, &buffer);
4503+
zerofill ? field_length : 0);
45064504
}
45074505

45084506
int Field_double::cmp(const uchar *a_ptr, const uchar *b_ptr) const {

sql/item.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -6559,12 +6559,12 @@ bool Item::send(Protocol *protocol, String *buffer) {
65596559
}
65606560
case MYSQL_TYPE_FLOAT: {
65616561
float nr = static_cast<float>(val_real());
6562-
if (!null_value) return protocol->store_float(nr, decimals, 0, buffer);
6562+
if (!null_value) return protocol->store_float(nr, decimals, 0);
65636563
break;
65646564
}
65656565
case MYSQL_TYPE_DOUBLE: {
65666566
double nr = val_real();
6567-
if (!null_value) return protocol->store_double(nr, decimals, 0, buffer);
6567+
if (!null_value) return protocol->store_double(nr, decimals, 0);
65686568
break;
65696569
}
65706570
case MYSQL_TYPE_DATE: {

sql/protocol.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,8 @@ class Protocol {
123123
virtual bool store_decimal(const my_decimal *, uint, uint) = 0;
124124
virtual bool store_string(const char *from, size_t length,
125125
const CHARSET_INFO *fromcs) = 0;
126-
virtual bool store_float(float from, uint32 decimals, uint32 zerofill,
127-
String *buffer) = 0;
128-
virtual bool store_double(double from, uint32 decimals, uint32 zerofill,
129-
String *buffer) = 0;
126+
virtual bool store_float(float from, uint32 decimals, uint32 zerofill) = 0;
127+
virtual bool store_double(double from, uint32 decimals, uint32 zerofill) = 0;
130128
virtual bool store_datetime(const MYSQL_TIME &time, uint precision) = 0;
131129
virtual bool store_date(const MYSQL_TIME &time) = 0;
132130
virtual bool store_time(const MYSQL_TIME &time, uint precision) = 0;

sql/protocol_callback.cc

+2-4
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,13 @@ bool Protocol_callback::store_string(const char *from, size_t length,
132132
return false;
133133
}
134134

135-
bool Protocol_callback::store_float(float from, uint32 decimals, uint32,
136-
String *) {
135+
bool Protocol_callback::store_float(float from, uint32 decimals, uint32) {
137136
if (callbacks.get_double)
138137
return callbacks.get_double(callbacks_ctx, from, decimals);
139138
return false;
140139
}
141140

142-
bool Protocol_callback::store_double(double from, uint32 decimals, uint32,
143-
String *) {
141+
bool Protocol_callback::store_double(double from, uint32 decimals, uint32) {
144142
if (callbacks.get_double)
145143
return callbacks.get_double(callbacks_ctx, from, decimals);
146144
return false;

sql/protocol_callback.h

+2-6
Original file line numberDiff line numberDiff line change
@@ -174,28 +174,24 @@ class Protocol_callback final : public Protocol {
174174
175175
@param from value
176176
@param decimals
177-
@param buffer auxiliary buffer
178177
179178
@return
180179
false success
181180
true failure
182181
*/
183-
bool store_float(float from, uint32 decimals, uint32,
184-
String *buffer) override;
182+
bool store_float(float from, uint32 decimals, uint32) override;
185183

186184
/**
187185
Sends DOUBLE value
188186
189187
@param from value
190188
@param decimals
191-
@param buffer auxiliary buffer
192189
193190
@return
194191
false success
195192
true failure
196193
*/
197-
bool store_double(double from, uint32 decimals, uint32,
198-
String *buffer) override;
194+
bool store_double(double from, uint32 decimals, uint32) override;
199195

200196
/**
201197
Sends DATETIME value

sql/protocol_classic.cc

+52-21
Original file line numberDiff line numberDiff line change
@@ -3324,32 +3324,63 @@ bool Protocol_text::store_decimal(const my_decimal *d, uint prec, uint dec) {
33243324
packet);
33253325
}
33263326

3327-
bool Protocol_text::store_float(float from, uint32 decimals, uint32 zerofill,
3328-
String *buffer) {
3327+
/**
3328+
Converts a floating-point value to text for the text protocol.
3329+
3330+
@param value the floating point value
3331+
@param decimals the precision of the value
3332+
@param gcvt_arg_type the type of the floating-point value
3333+
@param buffer a buffer large enough to hold FLOATING_POINT_BUFFER
3334+
characters plus a terminating zero character
3335+
@return the length of the text representation of the value
3336+
*/
3337+
static size_t floating_point_to_text(double value, uint32 decimals,
3338+
my_gcvt_arg_type gcvt_arg_type,
3339+
char *buffer) {
3340+
if (decimals < DECIMAL_NOT_SPECIFIED)
3341+
return my_fcvt(value, decimals, buffer, nullptr);
3342+
return my_gcvt(value, gcvt_arg_type, FLOATING_POINT_BUFFER, buffer, nullptr);
3343+
}
3344+
3345+
/**
3346+
Stores a floating-point value in the text protocol.
3347+
3348+
@param value the floating point value
3349+
@param decimals the precision of the value
3350+
@param zerofill the length up to which the value should be zero-padded,
3351+
or 0 if no zero-padding should be used
3352+
@param gcvt_arg_type the type of the floating-point value
3353+
@param packet the destination buffer
3354+
@return false on success, true on error
3355+
*/
3356+
static bool store_floating_point(double value, uint32 decimals, uint32 zerofill,
3357+
my_gcvt_arg_type gcvt_arg_type,
3358+
String *packet) {
3359+
char buffer[FLOATING_POINT_BUFFER + 1];
3360+
size_t length =
3361+
floating_point_to_text(value, decimals, gcvt_arg_type, buffer);
3362+
if (zerofill != 0)
3363+
return net_store_zero_padded_data(buffer, length, zerofill, packet);
3364+
return net_store_data(pointer_cast<const uchar *>(buffer), length, packet);
3365+
}
3366+
3367+
bool Protocol_text::store_float(float from, uint32 decimals, uint32 zerofill) {
33293368
// field_types check is needed because of the embedded protocol
33303369
DBUG_ASSERT(send_metadata || field_types == 0 ||
33313370
field_types[field_pos] == MYSQL_TYPE_FLOAT);
33323371
field_pos++;
3333-
buffer->set_float(from, decimals, m_thd->charset());
3334-
if (zerofill != 0)
3335-
return net_store_zero_padded_data(buffer->ptr(), buffer->length(), zerofill,
3336-
packet);
3337-
return net_store_data(pointer_cast<const uchar *>(buffer->ptr()),
3338-
buffer->length(), packet);
3372+
return store_floating_point(from, decimals, zerofill, MY_GCVT_ARG_FLOAT,
3373+
packet);
33393374
}
33403375

3341-
bool Protocol_text::store_double(double from, uint32 decimals, uint32 zerofill,
3342-
String *buffer) {
3376+
bool Protocol_text::store_double(double from, uint32 decimals,
3377+
uint32 zerofill) {
33433378
// field_types check is needed because of the embedded protocol
33443379
DBUG_ASSERT(send_metadata || field_types == 0 ||
33453380
field_types[field_pos] == MYSQL_TYPE_DOUBLE);
33463381
field_pos++;
3347-
buffer->set_real(from, decimals, m_thd->charset());
3348-
if (zerofill != 0)
3349-
return net_store_zero_padded_data(buffer->ptr(), buffer->length(), zerofill,
3350-
packet);
3351-
return net_store_data(pointer_cast<const uchar *>(buffer->ptr()),
3352-
buffer->length(), packet);
3382+
return store_floating_point(from, decimals, zerofill, MY_GCVT_ARG_DOUBLE,
3383+
packet);
33533384
}
33543385

33553386
/**
@@ -3589,10 +3620,10 @@ bool Protocol_binary::store_longlong(longlong from, bool unsigned_flag,
35893620
return 0;
35903621
}
35913622

3592-
bool Protocol_binary::store_float(float from, uint32 decimals, uint32 zerofill,
3593-
String *buffer) {
3623+
bool Protocol_binary::store_float(float from, uint32 decimals,
3624+
uint32 zerofill) {
35943625
if (send_metadata)
3595-
return Protocol_text::store_float(from, decimals, zerofill, buffer);
3626+
return Protocol_text::store_float(from, decimals, zerofill);
35963627
// field_types check is needed because of the embedded protocol
35973628
DBUG_ASSERT(field_types == nullptr ||
35983629
field_types[field_pos] == MYSQL_TYPE_FLOAT);
@@ -3604,9 +3635,9 @@ bool Protocol_binary::store_float(float from, uint32 decimals, uint32 zerofill,
36043635
}
36053636

36063637
bool Protocol_binary::store_double(double from, uint32 decimals,
3607-
uint32 zerofill, String *buffer) {
3638+
uint32 zerofill) {
36083639
if (send_metadata)
3609-
return Protocol_text::store_double(from, decimals, zerofill, buffer);
3640+
return Protocol_text::store_double(from, decimals, zerofill);
36103641
// field_types check is needed because of the embedded protocol
36113642
DBUG_ASSERT(field_types == nullptr ||
36123643
field_types[field_pos] == MYSQL_TYPE_DOUBLE);

sql/protocol_classic.h

+4-8
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,8 @@ class Protocol_text : public Protocol_classic {
225225
bool store_longlong(longlong from, bool unsigned_flag,
226226
uint32 zerofill) override;
227227
bool store_decimal(const my_decimal *, uint, uint) final;
228-
bool store_float(float nr, uint32 decimals, uint32 zerofill,
229-
String *buffer) override;
230-
bool store_double(double from, uint32 decimals, uint32 zerofill,
231-
String *buffer) override;
228+
bool store_float(float nr, uint32 decimals, uint32 zerofill) override;
229+
bool store_double(double from, uint32 decimals, uint32 zerofill) override;
232230
bool store_datetime(const MYSQL_TIME &time, uint precision) override;
233231
bool store_date(const MYSQL_TIME &time) override;
234232
bool store_time(const MYSQL_TIME &time, uint precision) override;
@@ -257,10 +255,8 @@ class Protocol_binary final : public Protocol_text {
257255
bool store_datetime(const MYSQL_TIME &time, uint precision) override;
258256
bool store_date(const MYSQL_TIME &time) override;
259257
bool store_time(const MYSQL_TIME &time, uint precision) override;
260-
bool store_float(float nr, uint32 decimals, uint32 zerofill,
261-
String *buffer) override;
262-
bool store_double(double from, uint32 decimals, uint32 zerofill,
263-
String *buffer) override;
258+
bool store_float(float nr, uint32 decimals, uint32 zerofill) override;
259+
bool store_double(double from, uint32 decimals, uint32 zerofill) override;
264260

265261
bool start_result_metadata(uint num_cols, uint flags,
266262
const CHARSET_INFO *resultcs) override;

sql/sql_prepare.cc

+4-6
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,8 @@ class Protocol_local final : public Protocol {
279279
bool store_datetime(const MYSQL_TIME &time, uint precision) override;
280280
bool store_date(const MYSQL_TIME &time) override;
281281
bool store_time(const MYSQL_TIME &time, uint precision) override;
282-
bool store_float(float value, uint32 decimals, uint32 zerofill,
283-
String *buffer) override;
284-
bool store_double(double value, uint32 decimals, uint32 zerofill,
285-
String *buffer) override;
282+
bool store_float(float value, uint32 decimals, uint32 zerofill) override;
283+
bool store_double(double value, uint32 decimals, uint32 zerofill) override;
286284
bool store_field(const Field *field) override;
287285

288286
enum enum_protocol_type type() const override { return PROTOCOL_LOCAL; }
@@ -3541,13 +3539,13 @@ bool Protocol_local::store_time(const MYSQL_TIME &time, uint) {
35413539

35423540
/* Store a floating point number, as is. */
35433541

3544-
bool Protocol_local::store_float(float value, uint32, uint32, String *) {
3542+
bool Protocol_local::store_float(float value, uint32, uint32) {
35453543
return store_column(&value, sizeof(float));
35463544
}
35473545

35483546
/* Store a double precision number, as is. */
35493547

3550-
bool Protocol_local::store_double(double value, uint32, uint32, String *) {
3548+
bool Protocol_local::store_double(double value, uint32, uint32) {
35513549
return store_column(&value, sizeof(double));
35523550
}
35533551

sql/sql_profile.cc

+1-3
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,6 @@ bool PROFILING::show_profiles() {
452452
iterator = history.iterator_next(iterator)) {
453453
prof = history.iterator_value(iterator);
454454

455-
String elapsed;
456-
457455
double query_time_usecs = prof->m_end_time_usecs - prof->m_start_time_usecs;
458456

459457
if (++idx <= unit->offset_limit_cnt) continue;
@@ -462,7 +460,7 @@ bool PROFILING::show_profiles() {
462460
protocol->start_row();
463461
protocol->store((uint32)(prof->profiling_query_id));
464462
protocol->store_double(query_time_usecs / (1000.0 * 1000),
465-
TIME_FLOAT_DIGITS - 1, 0, &elapsed);
463+
TIME_FLOAT_DIGITS - 1, 0);
466464
if (prof->m_query_source.str != NULL)
467465
protocol->store_string(prof->m_query_source.str,
468466
prof->m_query_source.length, system_charset_info);

unittest/gunit/field-t.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ class Mock_protocol : public Protocol {
139139
bool store_string(const char *, size_t, const CHARSET_INFO *) override {
140140
return false;
141141
}
142-
bool store_float(float, uint32, uint32, String *) override { return false; }
143-
bool store_double(double, uint32, uint32, String *) override { return false; }
142+
bool store_float(float, uint32, uint32) override { return false; }
143+
bool store_double(double, uint32, uint32) override { return false; }
144144
bool store_datetime(const MYSQL_TIME &, uint) override { return false; }
145145
bool store_date(const MYSQL_TIME &) override { return false; }
146146
bool store_field(const Field *) override { return false; }

0 commit comments

Comments
 (0)