1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-29 05:21:33 +03:00

MDEV-34295 CAST(char_col AS DOUBLE) prints redundant spaces in a warning

Field_string::val_int(), Field_string::val_real(), Field_string::val_decimal()
passed the whole buffer of field_length bytes to data type conversion routines.
This made conversion routines to print redundant trailing spaces in case of warnings.

Adding a method Field_string::to_lex_cstring() and using it inside
val_int(), val_real(), val_decimal(), val_str().

After this change conversion routines get the same value with what val_str() returns,
and no redundant trailing spaces are displayed.
This commit is contained in:
Alexander Barkov
2024-06-04 15:06:37 +04:00
parent 581712b989
commit 5e12d49205
10 changed files with 118 additions and 52 deletions

View File

@ -0,0 +1,40 @@
#
# Start of 10.5 tests
#
#
# MDEV-34295 CAST(char_col AS DOUBLE) prints redundant spaces in a warning
#
CREATE TABLE t1 (a CHAR(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci);
INSERT INTO t1 VALUES ('1x'), ('x');
SELECT a, CAST(a AS DOUBLE) FROM t1 ORDER BY a;
a CAST(a AS DOUBLE)
1x 1
x 0
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: '1x'
Warning 1292 Truncated incorrect DOUBLE value: 'x'
SELECT a, CAST(a AS DECIMAL(20,2)) FROM t1 ORDER BY a;
a CAST(a AS DECIMAL(20,2))
1x 1.00
x 0.00
Warnings:
Warning 1292 Truncated incorrect DECIMAL value: '1x'
Warning 1292 Truncated incorrect DECIMAL value: 'x'
SELECT a, CAST(a AS SIGNED) FROM t1 ORDER BY a;
a CAST(a AS SIGNED)
1x 1
x 0
Warnings:
Warning 1292 Truncated incorrect INTEGER value: '1x'
Warning 1292 Truncated incorrect INTEGER value: 'x'
SELECT a, CAST(a AS UNSIGNED) FROM t1 ORDER BY a;
a CAST(a AS UNSIGNED)
1x 1
x 0
Warnings:
Warning 1292 Truncated incorrect INTEGER value: '1x'
Warning 1292 Truncated incorrect INTEGER value: 'x'
DROP TABLE t1;
#
# End of 10.5 tests
#

View File

@ -0,0 +1,19 @@
--echo #
--echo # Start of 10.5 tests
--echo #
--echo #
--echo # MDEV-34295 CAST(char_col AS DOUBLE) prints redundant spaces in a warning
--echo #
CREATE TABLE t1 (a CHAR(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci);
INSERT INTO t1 VALUES ('1x'), ('x');
SELECT a, CAST(a AS DOUBLE) FROM t1 ORDER BY a;
SELECT a, CAST(a AS DECIMAL(20,2)) FROM t1 ORDER BY a;
SELECT a, CAST(a AS SIGNED) FROM t1 ORDER BY a;
SELECT a, CAST(a AS UNSIGNED) FROM t1 ORDER BY a;
DROP TABLE t1;
--echo #
--echo # End of 10.5 tests
--echo #

View File

@ -7485,11 +7485,11 @@ double Field_string::val_real(void)
{ {
DBUG_ASSERT(marked_for_read()); DBUG_ASSERT(marked_for_read());
THD *thd= get_thd(); THD *thd= get_thd();
return Converter_strntod_with_warn(get_thd(), const LEX_CSTRING str= to_lex_cstring();
return Converter_strntod_with_warn(thd,
Warn_filter_string(thd, this), Warn_filter_string(thd, this),
Field_string::charset(), Field_string::charset(),
(const char *) ptr, str.str, str.length).result();
field_length).result();
} }
@ -7497,10 +7497,10 @@ longlong Field_string::val_int(void)
{ {
DBUG_ASSERT(marked_for_read()); DBUG_ASSERT(marked_for_read());
THD *thd= get_thd(); THD *thd= get_thd();
const LEX_CSTRING str= to_lex_cstring();
return Converter_strntoll_with_warn(thd, Warn_filter_string(thd, this), return Converter_strntoll_with_warn(thd, Warn_filter_string(thd, this),
Field_string::charset(), Field_string::charset(),
(const char *) ptr, str.str, str.length).result();
field_length).result();
} }
@ -7516,20 +7516,26 @@ sql_mode_t Field_string::can_handle_sql_mode_dependency_on_store() const
} }
String *Field_string::val_str(String *val_buffer __attribute__((unused)), LEX_CSTRING Field_string::to_lex_cstring() const
String *val_ptr)
{ {
DBUG_ASSERT(marked_for_read()); DBUG_ASSERT(marked_for_read());
/* See the comment for Field_long::store(long long) */ /* See the comment for Field_long::store(long long) */
DBUG_ASSERT(!table || table->in_use == current_thd); DBUG_ASSERT(!table || table->in_use == current_thd);
size_t length; if (get_thd()->variables.sql_mode & MODE_PAD_CHAR_TO_FULL_LENGTH)
if (get_thd()->variables.sql_mode & return Lex_cstring((const char*) ptr,
MODE_PAD_CHAR_TO_FULL_LENGTH) field_charset()->charpos(ptr, ptr + field_length,
length= field_charset()->charpos(ptr, ptr + field_length, Field_string::char_length()));
Field_string::char_length()); return Lex_cstring((const char *) ptr,
else field_charset()->lengthsp((const char*) ptr, field_length));
length= field_charset()->lengthsp((const char*) ptr, field_length); }
val_ptr->set((const char*) ptr, length, field_charset());
String *Field_string::val_str(String *val_buffer __attribute__((unused)),
String *val_ptr)
{
DBUG_ASSERT(marked_for_read());
const LEX_CSTRING str= to_lex_cstring();
val_ptr->set(str.str, str.length, field_charset());
return val_ptr; return val_ptr;
} }
@ -7538,12 +7544,12 @@ my_decimal *Field_string::val_decimal(my_decimal *decimal_value)
{ {
DBUG_ASSERT(marked_for_read()); DBUG_ASSERT(marked_for_read());
THD *thd= get_thd(); THD *thd= get_thd();
const LEX_CSTRING str= to_lex_cstring();
Converter_str2my_decimal_with_warn(thd, Converter_str2my_decimal_with_warn(thd,
Warn_filter_string(thd, this), Warn_filter_string(thd, this),
E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM, E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
Field_string::charset(), Field_string::charset(),
(const char *) ptr, str.str, str.length, decimal_value);
field_length, decimal_value);
return decimal_value; return decimal_value;
} }

View File

@ -4013,6 +4013,7 @@ class Field_string final :public Field_longstr {
field_length >= 4 && field_length >= 4 &&
orig_table->s->frm_version < FRM_VER_TRUE_VARCHAR; orig_table->s->frm_version < FRM_VER_TRUE_VARCHAR;
} }
LEX_CSTRING to_lex_cstring() const;
public: public:
bool can_alter_field_type; bool can_alter_field_type;
Field_string(uchar *ptr_arg, uint32 len_arg,uchar *null_ptr_arg, Field_string(uchar *ptr_arg, uint32 len_arg,uchar *null_ptr_arg,