diff options
author | igor@rurik.mysql.com <> | 2005-06-15 08:27:33 -0700 |
---|---|---|
committer | igor@rurik.mysql.com <> | 2005-06-15 08:27:33 -0700 |
commit | 316815f44e37812ab694229917e898acca1471a7 (patch) | |
tree | 72b3677130fa85e1a40e4eabe5bd4460ee20308c | |
parent | 4bfe1cc84355e89d7ea9ffc731a02afa816f9ebe (diff) | |
download | mariadb-git-316815f44e37812ab694229917e898acca1471a7.tar.gz |
cast.result, cast.test:
Added test case for bug #11283.
field.h, field.cc:
Fixed bug #11283: wrong conversion from varchar to decimal.
Added methods Field_string::val_decimal,
Field_varstring::val_decimal, Field_blob::val_decimal.
They are not inherited from the base class Field_longstr
anymore.
-rw-r--r-- | mysql-test/r/cast.result | 8 | ||||
-rw-r--r-- | mysql-test/t/cast.test | 13 | ||||
-rw-r--r-- | sql/field.cc | 37 | ||||
-rw-r--r-- | sql/field.h | 4 |
4 files changed, 53 insertions, 9 deletions
diff --git a/mysql-test/r/cast.result b/mysql-test/r/cast.result index 6dc608a9289..fe3a1b0b606 100644 --- a/mysql-test/r/cast.result +++ b/mysql-test/r/cast.result @@ -344,3 +344,11 @@ select cast(s1 as decimal(7,2)) from t1; cast(s1 as decimal(7,2)) 111111.00 drop table t1; +CREATE TABLE t1 (v varchar(10), tt tinytext, t text, +mt mediumtext, lt longtext); +INSERT INTO t1 VALUES ('1.01', '2.02', '3.03', '4.04', '5.05'); +SELECT CAST(v AS DECIMAL), CAST(tt AS DECIMAL), CAST(t AS DECIMAL), +CAST(mt AS DECIMAL), CAST(lt AS DECIMAL) from t1; +CAST(v AS DECIMAL) CAST(tt AS DECIMAL) CAST(t AS DECIMAL) CAST(mt AS DECIMAL) CAST(lt AS DECIMAL) +1.01 2.02 3.03 4.04 5.05 +DROP TABLE t1; diff --git a/mysql-test/t/cast.test b/mysql-test/t/cast.test index cafecd6000d..d8c64a4a38b 100644 --- a/mysql-test/t/cast.test +++ b/mysql-test/t/cast.test @@ -168,3 +168,16 @@ create table t1(s1 time); insert into t1 values ('11:11:11'); select cast(s1 as decimal(7,2)) from t1; drop table t1; + +# +# Test for bug #11283: field conversion from varchar, and text types to decimal +# + +CREATE TABLE t1 (v varchar(10), tt tinytext, t text, + mt mediumtext, lt longtext); +INSERT INTO t1 VALUES ('1.01', '2.02', '3.03', '4.04', '5.05'); + +SELECT CAST(v AS DECIMAL), CAST(tt AS DECIMAL), CAST(t AS DECIMAL), + CAST(mt AS DECIMAL), CAST(lt AS DECIMAL) from t1; + +DROP TABLE t1; diff --git a/sql/field.cc b/sql/field.cc index 89ef25475ff..62de41e9bb2 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -5956,14 +5956,6 @@ longlong Field_string::val_int(void) } -my_decimal *Field_longstr::val_decimal(my_decimal *decimal_value) -{ - str2my_decimal(E_DEC_FATAL_ERROR, ptr, field_length, charset(), - decimal_value); - return decimal_value; -} - - String *Field_string::val_str(String *val_buffer __attribute__((unused)), String *val_ptr) { @@ -5975,6 +5967,14 @@ String *Field_string::val_str(String *val_buffer __attribute__((unused)), } +my_decimal *Field_string::val_decimal(my_decimal *decimal_value) +{ + str2my_decimal(E_DEC_FATAL_ERROR, ptr, field_length, charset(), + decimal_value); + return decimal_value; +} + + int Field_string::cmp(const char *a_ptr, const char *b_ptr) { uint a_len, b_len; @@ -6288,6 +6288,15 @@ String *Field_varstring::val_str(String *val_buffer __attribute__((unused)), } +my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value) +{ + uint length= length_bytes == 1 ? (uint) (uchar) *ptr : uint2korr(ptr); + str2my_decimal(E_DEC_FATAL_ERROR, ptr+length_bytes, length, charset(), + decimal_value); + return decimal_value; +} + + int Field_varstring::cmp(const char *a_ptr, const char *b_ptr) { uint a_length, b_length; @@ -6906,6 +6915,18 @@ String *Field_blob::val_str(String *val_buffer __attribute__((unused)), } +my_decimal *Field_blob::val_decimal(my_decimal *decimal_value) +{ + char *blob; + memcpy_fixed(&blob, ptr+packlength, sizeof(char*)); + if (!blob) + blob= ""; + str2my_decimal(E_DEC_FATAL_ERROR, blob, get_length(ptr), charset(), + decimal_value); + return decimal_value; +} + + int Field_blob::cmp(const char *a,uint32 a_length, const char *b, uint32 b_length) { diff --git a/sql/field.h b/sql/field.h index a522558a8d7..258eab9565f 100644 --- a/sql/field.h +++ b/sql/field.h @@ -381,7 +381,6 @@ public: field_name_arg, table_arg, charset) {} - my_decimal *val_decimal(my_decimal *); int store_decimal(const my_decimal *d); }; @@ -993,6 +992,7 @@ public: double val_real(void); longlong val_int(void); String *val_str(String*,String *); + my_decimal *val_decimal(my_decimal *); int cmp(const char *,const char*); void sort_string(char *buff,uint length); void sql_type(String &str) const; @@ -1051,6 +1051,7 @@ public: double val_real(void); longlong val_int(void); String *val_str(String*,String *); + my_decimal *val_decimal(my_decimal *); int cmp(const char *,const char*); void sort_string(char *buff,uint length); void get_key_image(char *buff,uint length, imagetype type); @@ -1106,6 +1107,7 @@ public: double val_real(void); longlong val_int(void); String *val_str(String*,String *); + my_decimal *val_decimal(my_decimal *); int cmp(const char *,const char*); int cmp(const char *a, uint32 a_length, const char *b, uint32 b_length); int cmp_binary(const char *a,const char *b, uint32 max_length=~0L); |