diff options
author | Alexander Barkov <bar@mysql.com> | 2010-02-11 08:17:25 +0400 |
---|---|---|
committer | Alexander Barkov <bar@mysql.com> | 2010-02-11 08:17:25 +0400 |
commit | 1094ffd572ebd533aa9b61d42ceaccc957f8e6c2 (patch) | |
tree | d6306efc9e4d7e6bc6bf4ae5a58bd6aec40420e9 /sql | |
parent | 68ac1c9c28c7cc7b06d508cdd110f0cde082ffea (diff) | |
download | mariadb-git-1094ffd572ebd533aa9b61d42ceaccc957f8e6c2.tar.gz |
WL#2649 Number-to-string conversions
added:
include/ctype_numconv.inc
mysql-test/include/ctype_numconv.inc
mysql-test/r/ctype_binary.result
mysql-test/t/ctype_binary.test
Adding tests
modified:
mysql-test/r/bigint.result
mysql-test/r/case.result
mysql-test/r/create.result
mysql-test/r/ctype_cp1251.result
mysql-test/r/ctype_latin1.result
mysql-test/r/ctype_ucs.result
mysql-test/r/func_gconcat.result
mysql-test/r/func_str.result
mysql-test/r/metadata.result
mysql-test/r/ps_1general.result
mysql-test/r/ps_2myisam.result
mysql-test/r/ps_3innodb.result
mysql-test/r/ps_4heap.result
mysql-test/r/ps_5merge.result
mysql-test/r/show_check.result
mysql-test/r/type_datetime.result
mysql-test/r/type_ranges.result
mysql-test/r/union.result
mysql-test/suite/ndb/r/ps_7ndb.result
mysql-test/t/ctype_cp1251.test
mysql-test/t/ctype_latin1.test
mysql-test/t/ctype_ucs.test
mysql-test/t/func_str.test
Fixing tests
@ sql/field.cc
- Return str result using my_charset_numeric.
- Using real multi-byte aware str_to_XXX functions
to handle tricky charset values propely (e.g. UCS2)
@ sql/field.h
- Changing derivation of non-string field types to DERIVATION_NUMERIC.
- Changing binary() for numeric/datetime fields to always
return TRUE even if charset is not my_charset_bin. We need
this to keep ha_base_keytype() return HA_KEYTYPE_BINARY.
- Adding BINARY_FLAG into some fields, because it's not
being set automatically anymore with
"my_charset_bin to my_charset_numeric" change.
- Changing derivation for numeric/datetime datatypes to a weaker
value, to make "SELECT concat('string', field)" use character
set of the string literal for the result of the function.
@ sql/item.cc
- Implementing generic val_str_ascii().
- Using max_char_length() instead of direct read of max_length
to make "tricky" charsets like UCS2 work.
NOTE: in the future we'll possibly remove all direct reads of max_length
- Fixing Item_num::safe_charset_converter().
Previously it alligned binary string to
character string (for example by adding leading 0x00
when doing binary->UCS2 conversion). Now it just
converts from my_charset_numbner to "tocs".
- Using val_str_ascii() in Item::get_time() to make UCS2 arguments work.
- Other misc changes
@ sql/item.h
- Changing MY_COLL_CMP_CONV and MY_COLL_ALLOW_CONV to
bit operations instead of hard-coded bit masks.
- Addding new method DTCollation.set_numeric().
- Adding new methods to Item.
- Adding helper functions to make code look nicer:
agg_item_charsets_for_string_result()
agg_item_charsets_for_comparison()
- Changing charset for Item_num-derived items
from my_charset_bin to my_charset_numeric
(which is an alias for latin1).
@ sql/item_cmpfunc.cc
- Using new helper functions
- Other misc changes
@ sql/item_cmpfunc.h
- Fixing strcmp() to return max_length=2.
Previously it returned 1, which was wrong,
because it did not fit '-1'.
@ sql/item_func.cc
- Using new helper functions
- Other minor changes
@ sql/item_func.h
- Removing unused functions
- Adding helper functions
agg_arg_charsets_for_string_result()
agg_arg_charsets_for_comparison()
- Adding set_numeric() into constructors of numeric items.
- Using fix_length_and_charset() and fix_char_length()
instead of direct write to max_length.
@ sql/item_geofunc.cc
- Changing class for Item_func_geometry_type and
Item_func_as_wkt from Item_str_func to
Item_str_ascii_func, to make them return UCS2 result
properly (when character_set_connection=ucs2).
@ sql/item_geofunc.h
- Changing class for Item_func_geometry_type and
Item_func_as_wkt from Item_str_func to
Item_str_ascii_func, to make them return UCS2 result
properly (when @@character_set_connection=ucs2).
@ sql/item_strfunc.cc
- Implementing Item_str_func::val_str().
- Renaming val_str to val_str_ascii for some items,
to make them work with UCS2 properly.
- Using new helper functions
- All single-argument functions that expect string
result now call this method:
agg_arg_charsets_for_string_result(collation, args, 1);
This enables character set conversion to @@character_set_connection
in case of pure numeric input.
@ sql/item_strfunc.h
- Introducing Item_str_ascii_func - for functions
which return pure ASCII data, for performance purposes,
as well as for the cases when the old implementation
of val_str() was heavily 8-bit oriented and implementing
a UCS2-aware version is tricky.
@ sql/item_sum.cc
- Using new helper functions.
@ sql/item_timefunc.cc
- Using my_charset_numeric instead of my_charset_bin.
- Using fix_char_length(), fix_length_and_charset()
and fix_length_and_charset_datetime()
instead of direct write to max_length.
- Using tricky-charset aware function str_to_time_with_warn()
@ sql/item_timefunc.h
- Using new helper functions for charset and length initialization.
- Changing base class for Item_func_get_format() to make
it return UCS2 properly (when character_set_connection=ucs2).
@ sql/item_xmlfunc.cc
- Using new helper function
@ sql/my_decimal.cc
- Adding a new DECIMAL to CHAR converter
with real multibyte support (e.g. UCS2)
@ sql/mysql_priv.h
- Introducing a new derivation level for numeric/datetime data types.
- Adding macros for my_charset_numeric and MY_REPERTOIRE_NUMERIC.
- Adding prototypes for str_set_decimal()
- Adding prototypes for character-set aware str_to_xxx() functions.
@ sql/protocol.cc
- Changing charsetnr to "binary" client-side metadata for
numeric/datetime data types.
@ sql/time.cc
- Adding to_ascii() helper function, to convert a string
in any character set to ascii representation. In the
future can be extended to understand digits written
in various non-Latin word scripts.
- Adding real multy-byte character set aware versions for str_to_XXXX,
to make these these type of queries work correct:
INSERT INTO t1 SET datetime_column=ucs2_expression;
@ strings/ctype-ucs2.c
- endptr was not calculated correctly. INSERTing of UCS2
values into numeric columns returned warnings about
truncated wrong data.
Diffstat (limited to 'sql')
-rw-r--r-- | sql/field.cc | 64 | ||||
-rw-r--r-- | sql/field.h | 42 | ||||
-rw-r--r-- | sql/item.cc | 97 | ||||
-rw-r--r-- | sql/item.h | 131 | ||||
-rw-r--r-- | sql/item_cmpfunc.cc | 34 | ||||
-rw-r--r-- | sql/item_cmpfunc.h | 5 | ||||
-rw-r--r-- | sql/item_func.cc | 101 | ||||
-rw-r--r-- | sql/item_func.h | 65 | ||||
-rw-r--r-- | sql/item_geofunc.cc | 7 | ||||
-rw-r--r-- | sql/item_geofunc.h | 17 | ||||
-rw-r--r-- | sql/item_strfunc.cc | 112 | ||||
-rw-r--r-- | sql/item_strfunc.h | 74 | ||||
-rw-r--r-- | sql/item_sum.cc | 8 | ||||
-rw-r--r-- | sql/item_timefunc.cc | 49 | ||||
-rw-r--r-- | sql/item_timefunc.h | 73 | ||||
-rw-r--r-- | sql/item_xmlfunc.cc | 2 | ||||
-rw-r--r-- | sql/my_decimal.cc | 53 | ||||
-rw-r--r-- | sql/mysql_priv.h | 28 | ||||
-rw-r--r-- | sql/protocol.cc | 4 | ||||
-rw-r--r-- | sql/time.cc | 77 |
20 files changed, 744 insertions, 299 deletions
diff --git a/sql/field.cc b/sql/field.cc index 51bb527fc85..8b48831dc57 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -1808,7 +1808,7 @@ bool Field::get_date(MYSQL_TIME *ltime,uint fuzzydate) char buff[40]; String tmp(buff,sizeof(buff),&my_charset_bin),*res; if (!(res=val_str(&tmp)) || - str_to_datetime_with_warn(res->ptr(), res->length(), + str_to_datetime_with_warn(res->charset(), res->ptr(), res->length(), ltime, fuzzydate) <= MYSQL_TIMESTAMP_ERROR) return 1; return 0; @@ -1819,7 +1819,7 @@ bool Field::get_time(MYSQL_TIME *ltime) char buff[40]; String tmp(buff,sizeof(buff),&my_charset_bin),*res; if (!(res=val_str(&tmp)) || - str_to_time_with_warn(res->ptr(), res->length(), ltime)) + str_to_time_with_warn(res->charset(), res->ptr(), res->length(), ltime)) return 1; return 0; } @@ -1836,7 +1836,9 @@ int Field::store_time(MYSQL_TIME *ltime, timestamp_type type_arg) ASSERT_COLUMN_MARKED_FOR_WRITE; char buff[MAX_DATE_STRING_REP_LENGTH]; uint length= (uint) my_TIME_to_str(ltime, buff); - return store(buff, length, &my_charset_bin); + /* Avoid conversion when field character set is ASCII compatible */ + return store(buff, length, (charset()->state & MY_CS_NONASCII) ? + &my_charset_latin1 : charset()); } @@ -2428,7 +2430,7 @@ String *Field_decimal::val_str(String *val_buffer __attribute__((unused)), size_t tmp_length; for (str=ptr ; *str == ' ' ; str++) ; - val_ptr->set_charset(&my_charset_bin); + val_ptr->set_charset(&my_charset_numeric); tmp_length= (size_t) (str-ptr); if (field_length < tmp_length) // Error in data val_ptr->length(0); @@ -2558,7 +2560,7 @@ Field *Field_new_decimal::create_from_item (Item *item) { uint8 dec= item->decimals; uint8 intg= item->decimal_precision() - dec; - uint32 len= item->max_length; + uint32 len= item->max_char_length(); DBUG_ASSERT (item->result_type() == DECIMAL_RESULT); @@ -2850,6 +2852,7 @@ String *Field_new_decimal::val_str(String *val_buffer, uint fixed_precision= zerofill ? precision : 0; my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), fixed_precision, dec, '0', val_buffer); + val_buffer->set_charset(&my_charset_numeric); return val_buffer; } @@ -3121,7 +3124,7 @@ String *Field_tiny::val_str(String *val_buffer, String *val_ptr __attribute__((unused))) { ASSERT_COLUMN_MARKED_FOR_READ; - CHARSET_INFO *cs= &my_charset_bin; + CHARSET_INFO *cs= &my_charset_numeric; uint length; uint mlength=max(field_length+1,5*cs->mbmaxlen); val_buffer->alloc(mlength); @@ -3137,6 +3140,7 @@ String *Field_tiny::val_str(String *val_buffer, val_buffer->length(length); if (zerofill) prepend_zeros(val_buffer); + val_buffer->set_charset(cs); return val_buffer; } @@ -3333,7 +3337,7 @@ String *Field_short::val_str(String *val_buffer, String *val_ptr __attribute__((unused))) { ASSERT_COLUMN_MARKED_FOR_READ; - CHARSET_INFO *cs= &my_charset_bin; + CHARSET_INFO *cs= &my_charset_numeric; uint length; uint mlength=max(field_length+1,7*cs->mbmaxlen); val_buffer->alloc(mlength); @@ -3354,6 +3358,7 @@ String *Field_short::val_str(String *val_buffer, val_buffer->length(length); if (zerofill) prepend_zeros(val_buffer); + val_buffer->set_charset(cs); return val_buffer; } @@ -3550,7 +3555,7 @@ String *Field_medium::val_str(String *val_buffer, String *val_ptr __attribute__((unused))) { ASSERT_COLUMN_MARKED_FOR_READ; - CHARSET_INFO *cs= &my_charset_bin; + CHARSET_INFO *cs= &my_charset_numeric; uint length; uint mlength=max(field_length+1,10*cs->mbmaxlen); val_buffer->alloc(mlength); @@ -3561,6 +3566,7 @@ String *Field_medium::val_str(String *val_buffer, val_buffer->length(length); if (zerofill) prepend_zeros(val_buffer); /* purecov: inspected */ + val_buffer->set_charset(cs); return val_buffer; } @@ -3769,7 +3775,7 @@ String *Field_long::val_str(String *val_buffer, String *val_ptr __attribute__((unused))) { ASSERT_COLUMN_MARKED_FOR_READ; - CHARSET_INFO *cs= &my_charset_bin; + CHARSET_INFO *cs= &my_charset_numeric; uint length; uint mlength=max(field_length+1,12*cs->mbmaxlen); val_buffer->alloc(mlength); @@ -3789,6 +3795,7 @@ String *Field_long::val_str(String *val_buffer, val_buffer->length(length); if (zerofill) prepend_zeros(val_buffer); + val_buffer->set_charset(cs); return val_buffer; } @@ -4010,7 +4017,7 @@ longlong Field_longlong::val_int(void) String *Field_longlong::val_str(String *val_buffer, String *val_ptr __attribute__((unused))) { - CHARSET_INFO *cs= &my_charset_bin; + CHARSET_INFO *cs= &my_charset_numeric; uint length; uint mlength=max(field_length+1,22*cs->mbmaxlen); val_buffer->alloc(mlength); @@ -4028,6 +4035,7 @@ String *Field_longlong::val_str(String *val_buffer, val_buffer->length(length); if (zerofill) prepend_zeros(val_buffer); + val_buffer->set_charset(cs); return val_buffer; } @@ -4254,6 +4262,7 @@ String *Field_float::val_str(String *val_buffer, val_buffer->length((uint) len); if (zerofill) prepend_zeros(val_buffer); + val_buffer->set_charset(&my_charset_numeric); return val_buffer; } @@ -4564,6 +4573,7 @@ String *Field_double::val_str(String *val_buffer, val_buffer->length((uint) len); if (zerofill) prepend_zeros(val_buffer); + val_buffer->set_charset(&my_charset_numeric); return val_buffer; } @@ -4698,7 +4708,7 @@ Field_timestamp::Field_timestamp(uchar *ptr_arg, uint32 len_arg, unireg_check_arg, field_name_arg, cs) { /* For 4.0 MYD and 4.0 InnoDB compatibility */ - flags|= ZEROFILL_FLAG | UNSIGNED_FLAG; + flags|= ZEROFILL_FLAG | UNSIGNED_FLAG | BINARY_FLAG; if (!share->timestamp_field && unireg_check != NONE) { /* This timestamp has auto-update */ @@ -4718,7 +4728,7 @@ Field_timestamp::Field_timestamp(bool maybe_null_arg, NONE, field_name_arg, cs) { /* For 4.0 MYD and 4.0 InnoDB compatibility */ - flags|= ZEROFILL_FLAG | UNSIGNED_FLAG; + flags|= ZEROFILL_FLAG | UNSIGNED_FLAG | BINARY_FLAG; if (unireg_check != TIMESTAMP_DN_FIELD) flags|= ON_UPDATE_NOW_FLAG; } @@ -4770,7 +4780,7 @@ int Field_timestamp::store(const char *from,uint len,CHARSET_INFO *cs) THD *thd= table ? table->in_use : current_thd; /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */ - have_smth_to_conv= (str_to_datetime(from, len, &l_time, + have_smth_to_conv= (str_to_datetime(cs, from, len, &l_time, (thd->variables.sql_mode & MODE_NO_ZERO_DATE) | MODE_NO_ZERO_IN_DATE, &error) > @@ -4919,10 +4929,10 @@ String *Field_timestamp::val_str(String *val_buffer, String *val_ptr) if (temp == 0L) { /* Zero time is "000000" */ - val_ptr->set(STRING_WITH_LEN("0000-00-00 00:00:00"), &my_charset_bin); + val_ptr->set(STRING_WITH_LEN("0000-00-00 00:00:00"), &my_charset_numeric); return val_ptr; } - val_buffer->set_charset(&my_charset_bin); // Safety + val_buffer->set_charset(&my_charset_numeric); // Safety thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp,(my_time_t)temp); @@ -4966,6 +4976,7 @@ String *Field_timestamp::val_str(String *val_buffer, String *val_ptr) *to++= (char) ('0'+(char) (temp2)); *to++= (char) ('0'+(char) (temp)); *to= 0; + val_buffer->set_charset(&my_charset_numeric); return val_buffer; } @@ -5076,7 +5087,7 @@ int Field_time::store(const char *from,uint len,CHARSET_INFO *cs) int error= 0; int warning; - if (str_to_time(from, len, <ime, &warning)) + if (str_to_time(cs, from, len, <ime, &warning)) { tmp=0L; error= 2; @@ -5235,6 +5246,7 @@ String *Field_time::val_str(String *val_buffer, ltime.minute= (uint) (tmp/100 % 100); ltime.second= (uint) (tmp % 100); make_time((DATE_TIME_FORMAT*) 0, <ime, val_buffer); + val_buffer->set_charset(&my_charset_numeric); return val_buffer; } @@ -5432,6 +5444,7 @@ String *Field_year::val_str(String *val_buffer, val_buffer->length(field_length); char *to=(char*) val_buffer->ptr(); sprintf(to,field_length == 2 ? "%02d" : "%04d",(int) Field_year::val_int()); + val_buffer->set_charset(&my_charset_numeric); return val_buffer; } @@ -5459,7 +5472,7 @@ int Field_date::store(const char *from, uint len,CHARSET_INFO *cs) int error; THD *thd= table ? table->in_use : current_thd; - if (str_to_datetime(from, len, &l_time, TIME_FUZZY_DATE | + if (str_to_datetime(cs, from, len, &l_time, TIME_FUZZY_DATE | (thd->variables.sql_mode & (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE | MODE_INVALID_DATES)), @@ -5606,6 +5619,7 @@ String *Field_date::val_str(String *val_buffer, ltime.month= (int) ((uint32) tmp/100 % 100); ltime.day= (int) ((uint32) tmp % 100); make_date((DATE_TIME_FORMAT *) 0, <ime, val_buffer); + val_buffer->set_charset(&my_charset_numeric); return val_buffer; } @@ -5695,7 +5709,7 @@ int Field_newdate::store(const char *from,uint len,CHARSET_INFO *cs) int error; THD *thd= table ? table->in_use : current_thd; enum enum_mysql_timestamp_type ret; - if ((ret= str_to_datetime(from, len, &l_time, + if ((ret= str_to_datetime(cs, from, len, &l_time, (TIME_FUZZY_DATE | (thd->variables.sql_mode & (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE | @@ -5867,6 +5881,7 @@ String *Field_newdate::val_str(String *val_buffer, *pos--= (char) ('0'+part%10); part/=10; *pos--= (char) ('0'+part%10); part/=10; *pos= (char) ('0'+part); + val_buffer->set_charset(&my_charset_numeric); return val_buffer; } @@ -5929,7 +5944,7 @@ int Field_datetime::store(const char *from,uint len,CHARSET_INFO *cs) enum enum_mysql_timestamp_type func_res; THD *thd= table ? table->in_use : current_thd; - func_res= str_to_datetime(from, len, &time_tmp, + func_res= str_to_datetime(cs, from, len, &time_tmp, (TIME_FUZZY_DATE | (thd->variables.sql_mode & (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE | @@ -6129,6 +6144,7 @@ String *Field_datetime::val_str(String *val_buffer, *pos--= (char) ('0'+(char) (part3%10)); part3/=10; *pos--= (char) ('0'+(char) (part3%10)); part3/=10; *pos=(char) ('0'+(char) part3); + val_buffer->set_charset(&my_charset_numeric); return val_buffer; } @@ -6370,7 +6386,7 @@ int Field_str::store(double nr) else set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1); } - return store(buff, length, charset()); + return store(buff, length, &my_charset_numeric); } @@ -6405,7 +6421,7 @@ int Field_string::store(longlong nr, bool unsigned_val) int Field_longstr::store_decimal(const my_decimal *d) { char buff[DECIMAL_MAX_STR_LENGTH+1]; - String str(buff, sizeof(buff), &my_charset_bin); + String str(buff, sizeof(buff), &my_charset_numeric); my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str); return store(str.ptr(), str.length(), str.charset()); } @@ -9565,8 +9581,8 @@ bool Create_field::init(THD *thd, char *fld_name, enum_field_types fld_type, case MYSQL_TYPE_TIME: case MYSQL_TYPE_DATETIME: case MYSQL_TYPE_TIMESTAMP: - charset= &my_charset_bin; - flags|= BINCMP_FLAG; + charset= &my_charset_numeric; + flags|= BINARY_FLAG; default: break; } @@ -9682,7 +9698,7 @@ Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length, case MYSQL_TYPE_TIME: case MYSQL_TYPE_DATETIME: case MYSQL_TYPE_TIMESTAMP: - field_charset= &my_charset_bin; + field_charset= &my_charset_numeric; default: break; } diff --git a/sql/field.h b/sql/field.h index c30bbfae879..08ffdbf5987 100644 --- a/sql/field.h +++ b/sql/field.h @@ -414,11 +414,14 @@ public: virtual bool get_date(MYSQL_TIME *ltime,uint fuzzydate); virtual bool get_time(MYSQL_TIME *ltime); virtual CHARSET_INFO *charset(void) const { return &my_charset_bin; } + virtual CHARSET_INFO *charset_for_protocol(void) const + { return binary() ? &my_charset_bin : charset(); } virtual CHARSET_INFO *sort_charset(void) const { return charset(); } virtual bool has_charset(void) const { return FALSE; } virtual void set_charset(CHARSET_INFO *charset_arg) { } virtual enum Derivation derivation(void) const { return DERIVATION_IMPLICIT; } + virtual uint repertoire(void) const { return MY_REPERTOIRE_UNICODE30; } virtual void set_derivation(enum Derivation derivation_arg) { } bool set_warning(MYSQL_ERROR::enum_warning_level, unsigned int code, int cuted_increment); @@ -600,6 +603,9 @@ public: const char *field_name_arg, uint8 dec_arg, bool zero_arg, bool unsigned_arg); Item_result result_type () const { return REAL_RESULT; } + enum Derivation derivation(void) const { return DERIVATION_NUMERIC; } + uint repertoire(void) const { return MY_REPERTOIRE_NUMERIC; } + CHARSET_INFO *charset(void) const { return &my_charset_numeric; } void prepend_zeros(String *value); void add_zerofill_and_unsigned(String &res) const; friend class Create_field; @@ -1170,6 +1176,10 @@ public: enum_field_types type() const { return MYSQL_TYPE_TIMESTAMP;} enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; } enum Item_result cmp_type () const { return INT_RESULT; } + enum Derivation derivation(void) const { return DERIVATION_NUMERIC; } + uint repertoire(void) const { return MY_REPERTOIRE_NUMERIC; } + CHARSET_INFO *charset(void) const { return &my_charset_numeric; } + bool binary() const { return 1; } int store(const char *to,uint length,CHARSET_INFO *charset); int store(double nr); int store(longlong nr, bool unsigned_val); @@ -1262,14 +1272,18 @@ public: CHARSET_INFO *cs) :Field_str(ptr_arg, MAX_DATE_WIDTH, null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg, cs) - {} + { flags|= BINARY_FLAG; } Field_date(bool maybe_null_arg, const char *field_name_arg, CHARSET_INFO *cs) :Field_str((uchar*) 0, MAX_DATE_WIDTH, maybe_null_arg ? (uchar*) "": 0,0, - NONE, field_name_arg, cs) {} + NONE, field_name_arg, cs) { flags|= BINARY_FLAG; } enum_field_types type() const { return MYSQL_TYPE_DATE;} enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; } enum Item_result cmp_type () const { return INT_RESULT; } + enum Derivation derivation(void) const { return DERIVATION_NUMERIC; } + uint repertoire(void) const { return MY_REPERTOIRE_NUMERIC; } + CHARSET_INFO *charset(void) const { return &my_charset_numeric; } + bool binary() const { return 1; } int store(const char *to,uint length,CHARSET_INFO *charset); int store(double nr); int store(longlong nr, bool unsigned_val); @@ -1306,15 +1320,19 @@ public: CHARSET_INFO *cs) :Field_str(ptr_arg, 10, null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg, cs) - {} + { flags|= BINARY_FLAG; } Field_newdate(bool maybe_null_arg, const char *field_name_arg, CHARSET_INFO *cs) :Field_str((uchar*) 0,10, maybe_null_arg ? (uchar*) "": 0,0, - NONE, field_name_arg, cs) {} + NONE, field_name_arg, cs) { flags|= BINARY_FLAG; } enum_field_types type() const { return MYSQL_TYPE_DATE;} enum_field_types real_type() const { return MYSQL_TYPE_NEWDATE; } enum ha_base_keytype key_type() const { return HA_KEYTYPE_UINT24; } enum Item_result cmp_type () const { return INT_RESULT; } + enum Derivation derivation(void) const { return DERIVATION_NUMERIC; } + uint repertoire(void) const { return MY_REPERTOIRE_NUMERIC; } + CHARSET_INFO *charset(void) const { return &my_charset_numeric; } + bool binary() const { return 1; } int store(const char *to,uint length,CHARSET_INFO *charset); int store(double nr); int store(longlong nr, bool unsigned_val); @@ -1342,14 +1360,18 @@ public: CHARSET_INFO *cs) :Field_str(ptr_arg, 8, null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg, cs) - {} + { flags|= BINARY_FLAG; } Field_time(bool maybe_null_arg, const char *field_name_arg, CHARSET_INFO *cs) :Field_str((uchar*) 0,8, maybe_null_arg ? (uchar*) "": 0,0, - NONE, field_name_arg, cs) {} + NONE, field_name_arg, cs) { flags|= BINARY_FLAG; } enum_field_types type() const { return MYSQL_TYPE_TIME;} enum ha_base_keytype key_type() const { return HA_KEYTYPE_INT24; } enum Item_result cmp_type () const { return INT_RESULT; } + enum Derivation derivation(void) const { return DERIVATION_NUMERIC; } + uint repertoire(void) const { return MY_REPERTOIRE_NUMERIC; } + CHARSET_INFO *charset(void) const { return &my_charset_numeric; } + bool binary() const { return 1; } int store_time(MYSQL_TIME *ltime, timestamp_type type); int store(const char *to,uint length,CHARSET_INFO *charset); int store(double nr); @@ -1377,16 +1399,20 @@ public: CHARSET_INFO *cs) :Field_str(ptr_arg, MAX_DATETIME_WIDTH, null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg, cs) - {} + { flags|= BINARY_FLAG; } Field_datetime(bool maybe_null_arg, const char *field_name_arg, CHARSET_INFO *cs) :Field_str((uchar*) 0, MAX_DATETIME_WIDTH, maybe_null_arg ? (uchar*) "": 0,0, - NONE, field_name_arg, cs) {} + NONE, field_name_arg, cs) { flags|= BINARY_FLAG; } enum_field_types type() const { return MYSQL_TYPE_DATETIME;} #ifdef HAVE_LONG_LONG enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONGLONG; } #endif enum Item_result cmp_type () const { return INT_RESULT; } + enum Derivation derivation(void) const { return DERIVATION_NUMERIC; } + uint repertoire(void) const { return MY_REPERTOIRE_NUMERIC; } + CHARSET_INFO *charset(void) const { return &my_charset_numeric; } + bool binary() const { return 1; } uint decimals() const { return DATETIME_DEC; } int store(const char *to,uint length,CHARSET_INFO *charset); int store(double nr); diff --git a/sql/item.cc b/sql/item.cc index e785f0addde..f7643db57cd 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -201,6 +201,37 @@ bool Item::val_bool() } +/* + For the items which don't have its own fast val_str_ascii() + implementation we provide a generic slower version, + which converts from the Item character set to ASCII. + For better performance conversion happens only in + case of a "tricky" Item character set (e.g. UCS2). + Normally conversion does not happen. +*/ +String *Item::val_str_ascii(String *str) +{ + DBUG_ASSERT(fixed == 1); + + if (!(collation.collation->state & MY_CS_NONASCII)) + return val_str(str); + + DBUG_ASSERT(str != &str_value); + + uint errors; + String *res= val_str(&str_value); + if (!res) + return 0; + + if ((null_value= str->copy(res->ptr(), res->length(), + collation.collation, &my_charset_latin1, + &errors))) + return 0; + + return str; +} + + String *Item::val_string_from_real(String *str) { double nr= val_real(); @@ -443,10 +474,11 @@ uint Item::decimal_precision() const if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT)) { uint prec= - my_decimal_length_to_precision(max_length, decimals, unsigned_flag); + my_decimal_length_to_precision(max_char_length(), decimals, + unsigned_flag); return min(prec, DECIMAL_MAX_PRECISION); } - return min(max_length, DECIMAL_MAX_PRECISION); + return min(max_char_length(), DECIMAL_MAX_PRECISION); } @@ -783,15 +815,40 @@ Item *Item::safe_charset_converter(CHARSET_INFO *tocs) */ Item *Item_num::safe_charset_converter(CHARSET_INFO *tocs) { + /* + Item_num returns pure ASCII result, + so conversion is needed only in case of "tricky" character + sets like UCS2. If tocs is not "tricky", return the item itself. + */ + if (!(tocs->state & MY_CS_NONASCII)) + return this; + Item_string *conv; - char buf[64]; - String *s, tmp(buf, sizeof(buf), &my_charset_bin); - s= val_str(&tmp); - if ((conv= new Item_string(s->ptr(), s->length(), s->charset()))) + uint conv_errors; + char buf[64], buf2[64]; + String tmp(buf, sizeof(buf), &my_charset_bin); + String cstr(buf2, sizeof(buf2), &my_charset_bin); + String *ostr= val_str(&tmp); + char *ptr; + cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors); + if (conv_errors || !(conv= new Item_string(cstr.ptr(), cstr.length(), + cstr.charset(), + collation.derivation))) { - conv->str_value.copy(); - conv->str_value.mark_as_const(); + /* + Safe conversion is not possible (or EOM). + We could not convert a string into the requested character set + without data loss. The target charset does not cover all the + characters from the string. Operation cannot be done correctly. + */ + return NULL; } + if (!(ptr= current_thd->strmake(cstr.ptr(), cstr.length()))) + return NULL; + conv->str_value.set(ptr, cstr.length(), cstr.charset()); + /* Ensure that no one is going to change the result string */ + conv->str_value.mark_as_const(); + conv->fix_char_length(max_char_length()); return conv; } @@ -910,7 +967,7 @@ bool Item::get_date(MYSQL_TIME *ltime,uint fuzzydate) char buff[40]; String tmp(buff,sizeof(buff), &my_charset_bin),*res; if (!(res=val_str(&tmp)) || - str_to_datetime_with_warn(res->ptr(), res->length(), + str_to_datetime_with_warn(res->charset(), res->ptr(), res->length(), ltime, fuzzydate) <= MYSQL_TIMESTAMP_ERROR) goto err; } @@ -945,8 +1002,8 @@ bool Item::get_time(MYSQL_TIME *ltime) { char buff[40]; String tmp(buff,sizeof(buff),&my_charset_bin),*res; - if (!(res=val_str(&tmp)) || - str_to_time_with_warn(res->ptr(), res->length(), ltime)) + if (!(res=val_str_ascii(&tmp)) || + str_to_time_with_warn(res->charset(), res->ptr(), res->length(), ltime)) { bzero((char*) ltime,sizeof(*ltime)); return 1; @@ -1650,6 +1707,11 @@ bool agg_item_collations(DTCollation &c, const char *fname, my_coll_agg_error(av, count, fname, item_sep); return TRUE; } + + /* If all arguments where numbers, reset to @@collation_connection */ + if (c.derivation == DERIVATION_NUMERIC) + c.set(Item::default_charset(), DERIVATION_COERCIBLE, MY_REPERTOIRE_NUMERIC); + return FALSE; } @@ -1895,13 +1957,14 @@ void Item_field::set_field(Field *field_par) field=result_field=field_par; // for easy coding with fields maybe_null=field->maybe_null(); decimals= field->decimals(); - max_length= field_par->max_display_length(); table_name= *field_par->table_name; field_name= field_par->field_name; db_name= field_par->table->s->db.str; alias_name_used= field_par->table->alias_name_used; unsigned_flag=test(field_par->flags & UNSIGNED_FLAG); - collation.set(field_par->charset(), field_par->derivation()); + collation.set(field_par->charset(), field_par->derivation(), + field_par->repertoire()); + fix_char_length(field_par->char_length()); fixed= 1; if (field->table->s->tmp_table == SYSTEM_TMP_TABLE) any_privileges= 0; @@ -2210,7 +2273,7 @@ String *Item_int::val_str(String *str) { // following assert is redundant, because fixed=1 assigned in constructor DBUG_ASSERT(fixed == 1); - str->set(value, &my_charset_bin); + str->set(value, collation.collation); return str; } @@ -2240,7 +2303,7 @@ String *Item_uint::val_str(String *str) { // following assert is redundant, because fixed=1 assigned in constructor DBUG_ASSERT(fixed == 1); - str->set((ulonglong) value, &my_charset_bin); + str->set((ulonglong) value, collation.collation); return str; } @@ -2340,7 +2403,7 @@ double Item_decimal::val_real() String *Item_decimal::val_str(String *result) { - result->set_charset(&my_charset_bin); + result->set_charset(&my_charset_numeric); my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, result); return result; } @@ -4866,7 +4929,7 @@ void Item::init_make_field(Send_field *tmp_field, tmp_field->col_name= name; tmp_field->charsetnr= collation.collation->number; tmp_field->flags= (maybe_null ? 0 : NOT_NULL_FLAG) | - (my_binary_compare(collation.collation) ? + (my_binary_compare(charset_for_protocol()) ? BINARY_FLAG : 0); tmp_field->type= field_type_arg; tmp_field->length=max_length; diff --git a/sql/item.h b/sql/item.h index b7e6cc6c204..0bfb2673121 100644 --- a/sql/item.h +++ b/sql/item.h @@ -44,9 +44,10 @@ class Item_field; #define MY_COLL_ALLOW_SUPERSET_CONV 1 #define MY_COLL_ALLOW_COERCIBLE_CONV 2 -#define MY_COLL_ALLOW_CONV 3 #define MY_COLL_DISALLOW_NONE 4 -#define MY_COLL_CMP_CONV 7 + +#define MY_COLL_ALLOW_CONV (MY_COLL_ALLOW_SUPERSET_CONV | MY_COLL_ALLOW_COERCIBLE_CONV) +#define MY_COLL_CMP_CONV (MY_COLL_ALLOW_CONV | MY_COLL_DISALLOW_NONE) class DTCollation { public: @@ -91,6 +92,12 @@ public: derivation= derivation_arg; repertoire= repertoire_arg; } + void set_numeric() + { + collation= &my_charset_numeric; + derivation= DERIVATION_NUMERIC; + repertoire= MY_REPERTOIRE_NUMERIC; + } void set(CHARSET_INFO *collation_arg) { collation= collation_arg; @@ -105,6 +112,7 @@ public: { switch(derivation) { + case DERIVATION_NUMERIC: return "NUMERIC"; case DERIVATION_IGNORABLE: return "IGNORABLE"; case DERIVATION_COERCIBLE: return "COERCIBLE"; case DERIVATION_IMPLICIT: return "IMPLICIT"; @@ -690,6 +698,77 @@ public: If value is not null null_value flag will be reset to FALSE. */ virtual String *val_str(String *str)=0; + + /* + Returns string representation of this item in ASCII format. + + SYNOPSIS + val_str_ascii() + str - similar to val_str(); + + NOTE + This method is introduced for performance optimization purposes. + + 1. val_str() result of some Items in string context + depends on @@character_set_results. + @@character_set_results can be set to a "real multibyte" character + set like UCS2, UTF16, UTF32. (We'll use only UTF32 in the examples + below for convenience.) + + So the default string result of such functions + in these circumstances is real multi-byte character set, like UTF32. + + For example, all numbers in string context + return result in @@character_set_results: + + SELECT CONCAT(20010101); -> UTF32 + + We do sprintf() first (to get ASCII representation) + and then convert to UTF32; + + So these kind "data sources" can use ASCII representation + internally, but return multi-byte data only because + @@character_set_results wants so. + Therefore, conversion from ASCII to UTF32 is applied internally. + + + 2. Some other functions need in fact ASCII input. + + For example, + inet_aton(), GeometryFromText(), Convert_TZ(), GET_FORMAT(). + + Similar, fields of certain type, like DATE, TIME, + when you insert string data into them, expect in fact ASCII input. + If they get non-ASCII input, for example UTF32, they + convert input from UTF32 to ASCII, and then use ASCII + representation to do further processing. + + + 3. Now imagine we pass result of a data source of the first type + to a data destination of the second type. + + What happens: + a. data source converts data from ASCII to UTF32, because + @@character_set_results wants so and passes the result to + data destination. + b. data destination gets UTF32 string. + c. data destination converts UTF32 string to ASCII, + because it needs ASCII representation to be able to handle data + correctly. + + As a result we get two steps of unnecessary conversion: + From ASCII to UTF32, then from UTF32 to ASCII. + + A better way to handle these situations is to pass ASCII + representation directly from the source to the destination. + + This is why val_str_ascii() introduced. + + RETURN + Similar to val_str() + */ + virtual String *val_str_ascii(String *str); + /* Return decimal representation of item with fixed point. @@ -864,6 +943,16 @@ public: static CHARSET_INFO *default_charset(); virtual CHARSET_INFO *compare_collation() { return NULL; } + /* + For backward compatibility, to make numeric + data types return "binary" charset in client-side metadata. + */ + virtual CHARSET_INFO *charset_for_protocol(void) const + { + return result_type() == STRING_RESULT ? collation.collation : + &my_charset_bin; + }; + virtual bool walk(Item_processor processor, bool walk_subquery, uchar *arg) { return (this->*processor)(arg); @@ -1069,6 +1158,20 @@ public: { return Field::GEOM_GEOMETRY; }; String *check_well_formed_result(String *str, bool send_error= 0); bool eq_by_collation(Item *item, bool binary_cmp, CHARSET_INFO *cs); + uint32 max_char_length() const + { return max_length / collation.collation->mbmaxlen; } + void fix_length_and_charset(uint32 max_char_length_arg, CHARSET_INFO *cs) + { + max_length= max_char_length_arg * cs->mbmaxlen; + collation.collation= cs; + } + void fix_char_length(uint32 max_char_length_arg) + { max_length= max_char_length_arg * collation.collation->mbmaxlen; } + void fix_length_and_charset_datetime(uint32 max_char_length_arg) + { + collation.set(&my_charset_numeric, DERIVATION_NUMERIC, MY_REPERTOIRE_ASCII); + fix_char_length(max_char_length_arg); + } }; @@ -1371,12 +1474,30 @@ bool agg_item_set_converter(DTCollation &coll, const char *fname, Item **args, uint nargs, uint flags, int item_sep); bool agg_item_charsets(DTCollation &c, const char *name, Item **items, uint nitems, uint flags, int item_sep); - +inline bool +agg_item_charsets_for_string_result(DTCollation &c, const char *name, + Item **items, uint nitems, + int item_sep= 1) +{ + uint flags= MY_COLL_ALLOW_SUPERSET_CONV | + MY_COLL_ALLOW_COERCIBLE_CONV; + return agg_item_charsets(c, name, items, nitems, flags, item_sep); +} +inline bool +agg_item_charsets_for_comparison(DTCollation &c, const char *name, + Item **items, uint nitems, + int item_sep= 1) +{ + uint flags= MY_COLL_ALLOW_SUPERSET_CONV | + MY_COLL_ALLOW_COERCIBLE_CONV | + MY_COLL_DISALLOW_NONE; + return agg_item_charsets(c, name, items, nitems, flags, item_sep); +} class Item_num: public Item_basic_constant { public: - Item_num() {} /* Remove gcc warning */ + Item_num() { collation.set_numeric(); } /* Remove gcc warning */ virtual Item_num *neg()= 0; Item *safe_charset_converter(CHARSET_INFO *tocs); bool check_partition_func_processor(uchar *int_arg) { return FALSE;} @@ -1561,6 +1682,8 @@ public: DBUG_ASSERT(field_type() == MYSQL_TYPE_GEOMETRY); return field->get_geometry_type(); } + CHARSET_INFO *charset_for_protocol(void) const + { return field->charset_for_protocol(); } friend class Item_default_value; friend class Item_insert_value; friend class st_select_lex_unit; diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index 1da383ce3e9..db4a55bbcd0 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -483,7 +483,7 @@ void Item_bool_func2::fix_length_and_dec() DTCollation coll; if (args[0]->result_type() == STRING_RESULT && args[1]->result_type() == STRING_RESULT && - agg_arg_charsets(coll, args, 2, MY_COLL_CMP_CONV, 1)) + agg_arg_charsets_for_comparison(coll, args, 2)) return; args[0]->cmp_context= args[1]->cmp_context= @@ -934,6 +934,7 @@ int Arg_comparator::set_cmp_func(Item_result_field *owner_arg, func= &Arg_comparator::compare_datetime; get_value_a_func= &get_datetime_value; get_value_b_func= &get_datetime_value; + cmp_collation.set(&my_charset_numeric); return 0; } else if (type == STRING_RESULT && (*a)->field_type() == MYSQL_TYPE_TIME && @@ -2173,7 +2174,7 @@ void Item_func_between::fix_length_and_dec() if ( agg_cmp_type(&cmp_type, args, 3)) return; if (cmp_type == STRING_RESULT && - agg_arg_charsets(cmp_collation, args, 3, MY_COLL_CMP_CONV, 1)) + agg_arg_charsets_for_comparison(cmp_collation, args, 3)) return; /* @@ -2374,7 +2375,7 @@ Item_func_ifnull::fix_length_and_dec() switch (hybrid_type) { case STRING_RESULT: - agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1); + agg_arg_charsets_for_comparison(collation, args, arg_count); break; case DECIMAL_RESULT: case REAL_RESULT: @@ -2549,12 +2550,12 @@ Item_func_if::fix_length_and_dec() agg_result_type(&cached_result_type, args+1, 2); if (cached_result_type == STRING_RESULT) { - if (agg_arg_charsets(collation, args+1, 2, MY_COLL_ALLOW_CONV, 1)) + if (agg_arg_charsets_for_string_result(collation, args + 1, 2)) return; } else { - collation.set(&my_charset_bin); // Number + collation.set_numeric(); // Number } cached_field_type= agg_field_type(args + 1, 2); } @@ -2640,7 +2641,7 @@ Item_func_nullif::fix_length_and_dec() unsigned_flag= args[0]->unsigned_flag; cached_result_type= args[0]->result_type(); if (cached_result_type == STRING_RESULT && - agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1)) + agg_arg_charsets_for_comparison(collation, args, arg_count)) return; } } @@ -2917,9 +2918,13 @@ void Item_func_case::fix_length_and_dec() agg[nagg++]= args[else_expr_num]; agg_result_type(&cached_result_type, agg, nagg); - if ((cached_result_type == STRING_RESULT) && - agg_arg_charsets(collation, agg, nagg, MY_COLL_ALLOW_CONV, 1)) - return; + if (cached_result_type == STRING_RESULT) + { + if (agg_arg_charsets_for_string_result(collation, agg, nagg)) + return; + } + else + collation.set_numeric(); cached_field_type= agg_field_type(agg, nagg); /* @@ -2944,7 +2949,7 @@ void Item_func_case::fix_length_and_dec() { DBUG_ASSERT((Item_result)i != ROW_RESULT); if ((Item_result)i == STRING_RESULT && - agg_arg_charsets(cmp_collation, agg, nagg, MY_COLL_CMP_CONV, 1)) + agg_arg_charsets_for_comparison(cmp_collation, agg, nagg)) return; if (!(cmp_items[i]= cmp_item::get_comparator((Item_result)i, @@ -3107,7 +3112,7 @@ void Item_func_coalesce::fix_length_and_dec() case STRING_RESULT: count_only_length(); decimals= NOT_FIXED_DEC; - agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1); + agg_arg_charsets_for_string_result(collation, args, arg_count); break; case DECIMAL_RESULT: count_decimal_length(); @@ -3752,7 +3757,7 @@ void Item_func_in::fix_length_and_dec() if (type_cnt == 1) { if (cmp_type == STRING_RESULT && - agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1)) + agg_arg_charsets_for_comparison(cmp_collation, args, arg_count)) return; arg_types_compatible= TRUE; } @@ -3930,8 +3935,7 @@ void Item_func_in::fix_length_and_dec() if (found_types & (1 << i) && !cmp_items[i]) { if ((Item_result)i == STRING_RESULT && - agg_arg_charsets(cmp_collation, args, arg_count, - MY_COLL_CMP_CONV, 1)) + agg_arg_charsets_for_comparison(cmp_collation, args, arg_count)) return; if (!cmp_items[i] && !(cmp_items[i]= cmp_item::get_comparator((Item_result)i, @@ -4762,7 +4766,7 @@ Item_func_regex::fix_fields(THD *thd, Item **ref) max_length= 1; decimals= 0; - if (agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1)) + if (agg_arg_charsets_for_comparison(cmp_collation, args, 2)) return TRUE; regex_lib_flags= (cmp_collation.collation->state & diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h index ef4eef3276c..b9a3860acb6 100644 --- a/sql/item_cmpfunc.h +++ b/sql/item_cmpfunc.h @@ -637,6 +637,11 @@ public: { Item_func::print(str, query_type); } + void fix_length_and_dec() + { + Item_bool_func2::fix_length_and_dec(); + fix_char_length(2); // returns "1" or "0" or "-1" + } }; diff --git a/sql/item_func.cc b/sql/item_func.cc index 75f8b2045b5..0a572a8f3e3 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -440,13 +440,15 @@ Field *Item_func::tmp_table_field(TABLE *table) switch (result_type()) { case INT_RESULT: - if (max_length > MY_INT32_NUM_DECIMAL_DIGITS) - field= new Field_longlong(max_length, maybe_null, name, unsigned_flag); + if (max_char_length() > MY_INT32_NUM_DECIMAL_DIGITS) + field= new Field_longlong(max_char_length(), maybe_null, name, + unsigned_flag); else - field= new Field_long(max_length, maybe_null, name, unsigned_flag); + field= new Field_long(max_char_length(), maybe_null, name, + unsigned_flag); break; case REAL_RESULT: - field= new Field_double(max_length, maybe_null, name, decimals); + field= new Field_double(max_char_length(), maybe_null, name, decimals); break; case STRING_RESULT: return make_string_field(table); @@ -487,7 +489,7 @@ String *Item_real_func::val_str(String *str) double nr= val_real(); if (null_value) return 0; /* purecov: inspected */ - str->set_real(nr,decimals, &my_charset_bin); + str->set_real(nr, decimals, collation.collation); return str; } @@ -626,7 +628,7 @@ String *Item_int_func::val_str(String *str) longlong nr=val_int(); if (null_value) return 0; - str->set_int(nr, unsigned_flag, &my_charset_bin); + str->set_int(nr, unsigned_flag, collation.collation); return str; } @@ -746,6 +748,7 @@ String *Item_func_numhybrid::val_str(String *str) if (!(val= decimal_op(&decimal_value))) return 0; // null is set my_decimal_round(E_DEC_FATAL_ERROR, val, decimals, FALSE, val); + str->set_charset(collation.collation); my_decimal2string(E_DEC_FATAL_ERROR, val, 0, 0, 0, str); break; } @@ -754,7 +757,7 @@ String *Item_func_numhybrid::val_str(String *str) longlong nr= int_op(); if (null_value) return 0; /* purecov: inspected */ - str->set_int(nr, unsigned_flag, &my_charset_bin); + str->set_int(nr, unsigned_flag, collation.collation); break; } case REAL_RESULT: @@ -762,7 +765,7 @@ String *Item_func_numhybrid::val_str(String *str) double nr= real_op(); if (null_value) return 0; /* purecov: inspected */ - str->set_real(nr,decimals,&my_charset_bin); + str->set_real(nr, decimals, collation.collation); break; } case STRING_RESULT: @@ -897,6 +900,7 @@ longlong Item_func_signed::val_int_from_str(int *error) uint32 length; String tmp(buff,sizeof(buff), &my_charset_bin), *res; longlong value; + CHARSET_INFO *cs; /* For a string result, we must first get the string and then convert it @@ -912,9 +916,10 @@ longlong Item_func_signed::val_int_from_str(int *error) null_value= 0; start= (char *)res->ptr(); length= res->length(); + cs= res->charset(); end= start + length; - value= my_strtoll10(start, &end, error); + value= cs->cset->strtoll10(cs, start, &end, error); if (*error > 0 || end != start+ length) { char err_buff[128]; @@ -2265,7 +2270,7 @@ void Item_func_min_max::fix_length_and_dec() } if (cmp_type == STRING_RESULT) { - agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1); + agg_arg_charsets_for_comparison(collation, args, arg_count); if (datetime_found) { thd= current_thd; @@ -2273,9 +2278,13 @@ void Item_func_min_max::fix_length_and_dec() } } else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT)) - max_length= my_decimal_precision_to_length_no_truncation(max_int_part + - decimals, decimals, - unsigned_flag); + { + collation.set_numeric(); + fix_char_length(my_decimal_precision_to_length_no_truncation(max_int_part + + decimals, + decimals, + unsigned_flag)); + } cached_field_type= agg_field_type(args, arg_count); } @@ -2345,7 +2354,7 @@ String *Item_func_min_max::val_str(String *str) longlong nr=val_int(); if (null_value) return 0; - str->set_int(nr, unsigned_flag, &my_charset_bin); + str->set_int(nr, unsigned_flag, collation.collation); return str; } case DECIMAL_RESULT: @@ -2361,7 +2370,7 @@ String *Item_func_min_max::val_str(String *str) double nr= val_real(); if (null_value) return 0; /* purecov: inspected */ - str->set_real(nr,decimals,&my_charset_bin); + str->set_real(nr, decimals, collation.collation); return str; } case STRING_RESULT: @@ -2532,7 +2541,7 @@ longlong Item_func_coercibility::val_int() void Item_func_locate::fix_length_and_dec() { max_length= MY_INT32_NUM_DECIMAL_DIGITS; - agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1); + agg_arg_charsets_for_comparison(cmp_collation, args, 2); } @@ -2656,7 +2665,7 @@ void Item_func_field::fix_length_and_dec() for (uint i=1; i < arg_count ; i++) cmp_type= item_cmp_type(cmp_type, args[i]->result_type()); if (cmp_type == STRING_RESULT) - agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1); + agg_arg_charsets_for_comparison(cmp_collation, args, arg_count); } @@ -2723,7 +2732,7 @@ void Item_func_find_in_set::fix_length_and_dec() } } } - agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1); + agg_arg_charsets_for_comparison(cmp_collation, args, 2); } static const char separator=','; @@ -3964,7 +3973,9 @@ bool Item_func_set_user_var::fix_fields(THD *thd, Item **ref) */ null_item= (args[0]->type() == NULL_ITEM); if (!entry->collation.collation || !null_item) - entry->collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT); + entry->collation.set(args[0]->collation.derivation == DERIVATION_NUMERIC ? + default_charset() : args[0]->collation.collation, + DERIVATION_IMPLICIT); collation.set(entry->collation.collation, DERIVATION_IMPLICIT); cached_result_type= args[0]->result_type(); return FALSE; @@ -3975,9 +3986,15 @@ void Item_func_set_user_var::fix_length_and_dec() { maybe_null=args[0]->maybe_null; - max_length=args[0]->max_length; decimals=args[0]->decimals; - collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT); + collation.set(DERIVATION_IMPLICIT); + if (args[0]->collation.derivation == DERIVATION_NUMERIC) + fix_length_and_charset(args[0]->max_char_length(), default_charset()); + else + { + fix_length_and_charset(args[0]->max_char_length(), + args[0]->collation.collation); + } } @@ -4172,16 +4189,16 @@ String *user_var_entry::val_str(my_bool *null_value, String *str, switch (type) { case REAL_RESULT: - str->set_real(*(double*) value, decimals, &my_charset_bin); + str->set_real(*(double*) value, decimals, collation.collation); break; case INT_RESULT: if (!unsigned_flag) - str->set(*(longlong*) value, &my_charset_bin); + str->set(*(longlong*) value, collation.collation); else - str->set(*(ulonglong*) value, &my_charset_bin); + str->set(*(ulonglong*) value, collation.collation); break; case DECIMAL_RESULT: - my_decimal2string(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, 0, 0, str); + str_set_decimal((my_decimal *) value, str, collation.collation); break; case STRING_RESULT: if (str->copy(value, length, collation.collation)) @@ -4339,13 +4356,13 @@ Item_func_set_user_var::update() case REAL_RESULT: { res= update_hash((void*) &save_result.vreal,sizeof(save_result.vreal), - REAL_RESULT, &my_charset_bin, DERIVATION_IMPLICIT, 0); + REAL_RESULT, default_charset(), DERIVATION_IMPLICIT, 0); break; } case INT_RESULT: { res= update_hash((void*) &save_result.vint, sizeof(save_result.vint), - INT_RESULT, &my_charset_bin, DERIVATION_IMPLICIT, + INT_RESULT, default_charset(), DERIVATION_IMPLICIT, unsigned_flag); break; } @@ -4369,7 +4386,7 @@ Item_func_set_user_var::update() else res= update_hash((void*) save_result.vdec, sizeof(my_decimal), DECIMAL_RESULT, - &my_charset_bin, DERIVATION_IMPLICIT, 0); + default_charset(), DERIVATION_IMPLICIT, 0); break; } case ROW_RESULT: @@ -4802,17 +4819,17 @@ void Item_func_get_user_var::fix_length_and_dec() collation.set(var_entry->collation); switch(m_cached_result_type) { case REAL_RESULT: - max_length= DBL_DIG + 8; + fix_char_length(DBL_DIG + 8); break; case INT_RESULT: - max_length= MAX_BIGINT_WIDTH; + fix_char_length(MAX_BIGINT_WIDTH); decimals=0; break; case STRING_RESULT: max_length= MAX_BLOB_WIDTH; break; case DECIMAL_RESULT: - max_length= DECIMAL_MAX_STR_LENGTH; + fix_char_length(DECIMAL_MAX_STR_LENGTH); decimals= DECIMAL_MAX_SCALE; break; case ROW_RESULT: // Keep compiler happy @@ -4998,12 +5015,14 @@ void Item_func_get_system_var::fix_length_and_dec() case SHOW_INT: case SHOW_HA_ROWS: unsigned_flag= TRUE; - max_length= MY_INT64_NUM_DECIMAL_DIGITS; + collation.set_numeric(); + fix_char_length(MY_INT64_NUM_DECIMAL_DIGITS); decimals=0; break; case SHOW_LONGLONG: unsigned_flag= TRUE; - max_length= MY_INT64_NUM_DECIMAL_DIGITS; + collation.set_numeric(); + fix_char_length(MY_INT64_NUM_DECIMAL_DIGITS); decimals=0; break; case SHOW_CHAR: @@ -5037,13 +5056,15 @@ void Item_func_get_system_var::fix_length_and_dec() case SHOW_BOOL: case SHOW_MY_BOOL: unsigned_flag= FALSE; - max_length= 1; + collation.set_numeric(); + fix_char_length(1); decimals=0; break; case SHOW_DOUBLE: unsigned_flag= FALSE; decimals= 6; - max_length= DBL_DIG + 6; + collation.set_numeric(); + fix_char_length(DBL_DIG + 6); break; default: my_error(ER_VAR_CANT_BE_READ, MYF(0), var->name.str); @@ -5405,8 +5426,8 @@ longlong Item_func_inet_aton::val_int() char buff[36]; int dot_count= 0; - String *s,tmp(buff,sizeof(buff),&my_charset_bin); - if (!(s = args[0]->val_str(&tmp))) // If null value + String *s, tmp(buff, sizeof(buff), &my_charset_latin1); + if (!(s = args[0]->val_str_ascii(&tmp))) // If null value goto err; null_value=0; @@ -5414,7 +5435,7 @@ longlong Item_func_inet_aton::val_int() while (p < end) { c = *p++; - int digit = (int) (c - '0'); // Assume ascii + int digit = (int) (c - '0'); if (digit >= 0 && digit <= 9) { if ((byte_result = byte_result * 10 + digit) > 255) @@ -5564,8 +5585,8 @@ bool Item_func_match::fix_fields(THD *thd, Item **ref) return 1; } table->fulltext_searched=1; - return agg_arg_collations_for_comparison(cmp_collation, - args+1, arg_count-1, 0); + return agg_item_collations_for_comparison(cmp_collation, func_name(), + args+1, arg_count-1, 0); } bool Item_func_match::fix_index() diff --git a/sql/item_func.h b/sql/item_func.h index 6bfdae8d56d..fc7f8708a45 100644 --- a/sql/item_func.h +++ b/sql/item_func.h @@ -160,22 +160,24 @@ public: my_decimal *val_decimal(my_decimal *); - bool agg_arg_collations(DTCollation &c, Item **items, uint nitems, - uint flags) + bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems, + uint flags, int item_sep) { - return agg_item_collations(c, func_name(), items, nitems, flags, 1); + return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep); } - bool agg_arg_collations_for_comparison(DTCollation &c, - Item **items, uint nitems, - uint flags) + bool agg_arg_charsets_for_string_result(DTCollation &c, + Item **items, uint nitems, + int item_sep= 1) { - return agg_item_collations_for_comparison(c, func_name(), - items, nitems, flags); + return agg_item_charsets_for_string_result(c, func_name(), + items, nitems, item_sep); } - bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems, - uint flags, int item_sep) + bool agg_arg_charsets_for_comparison(DTCollation &c, + Item **items, uint nitems, + int item_sep= 1) { - return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep); + return agg_item_charsets_for_comparison(c, func_name(), + items, nitems, item_sep); } bool walk(Item_processor processor, bool walk_subquery, uchar *arg); Item *transform(Item_transformer transformer, uchar *arg); @@ -226,10 +228,10 @@ public: class Item_real_func :public Item_func { public: - Item_real_func() :Item_func() {} - Item_real_func(Item *a) :Item_func(a) {} - Item_real_func(Item *a,Item *b) :Item_func(a,b) {} - Item_real_func(List<Item> &list) :Item_func(list) {} + Item_real_func() :Item_func() { collation.set_numeric(); } + Item_real_func(Item *a) :Item_func(a) { collation.set_numeric(); } + Item_real_func(Item *a,Item *b) :Item_func(a,b) { collation.set_numeric(); } + Item_real_func(List<Item> &list) :Item_func(list) { collation.set_numeric(); } String *val_str(String*str); my_decimal *val_decimal(my_decimal *decimal_value); longlong val_int() @@ -246,13 +248,13 @@ protected: Item_result hybrid_type; public: Item_func_numhybrid(Item *a) :Item_func(a), hybrid_type(REAL_RESULT) - {} + { collation.set_numeric(); } Item_func_numhybrid(Item *a,Item *b) :Item_func(a,b), hybrid_type(REAL_RESULT) - {} + { collation.set_numeric(); } Item_func_numhybrid(List<Item> &list) :Item_func(list), hybrid_type(REAL_RESULT) - {} + { collation.set_numeric(); } enum Item_result result_type () const { return hybrid_type; } void fix_length_and_dec(); @@ -335,13 +337,18 @@ class Item_num_op :public Item_func_numhybrid class Item_int_func :public Item_func { public: - Item_int_func() :Item_func() { max_length= 21; } - Item_int_func(Item *a) :Item_func(a) { max_length= 21; } - Item_int_func(Item *a,Item *b) :Item_func(a,b) { max_length= 21; } + Item_int_func() :Item_func() + { collation.set_numeric(); fix_char_length(21); } + Item_int_func(Item *a) :Item_func(a) + { collation.set_numeric(); fix_char_length(21); } + Item_int_func(Item *a,Item *b) :Item_func(a,b) + { collation.set_numeric(); fix_char_length(21); } Item_int_func(Item *a,Item *b,Item *c) :Item_func(a,b,c) - { max_length= 21; } - Item_int_func(List<Item> &list) :Item_func(list) { max_length= 21; } - Item_int_func(THD *thd, Item_int_func *item) :Item_func(thd, item) {} + { collation.set_numeric(); fix_char_length(21); } + Item_int_func(List<Item> &list) :Item_func(list) + { collation.set_numeric(); fix_char_length(21); } + Item_int_func(THD *thd, Item_int_func *item) :Item_func(thd, item) + { collation.set_numeric(); } double val_real(); String *val_str(String*str); enum Item_result result_type () const { return INT_RESULT; } @@ -370,7 +377,7 @@ public: longlong val_int(); longlong val_int_from_str(int *error); void fix_length_and_dec() - { max_length=args[0]->max_length; unsigned_flag=0; } + { fix_char_length(args[0]->max_char_length()); unsigned_flag=0; } virtual void print(String *str, enum_query_type query_type); uint decimal_precision() const { return args[0]->decimal_precision(); } }; @@ -383,7 +390,8 @@ public: const char *func_name() const { return "cast_as_unsigned"; } void fix_length_and_dec() { - max_length= min(args[0]->max_length, DECIMAL_MAX_PRECISION + 2); + fix_char_length(min(args[0]->max_char_length(), + DECIMAL_MAX_PRECISION + 2)); unsigned_flag=1; } longlong val_int(); @@ -398,8 +406,9 @@ public: Item_decimal_typecast(Item *a, int len, int dec) :Item_func(a) { decimals= dec; - max_length= my_decimal_precision_to_length_no_truncation(len, dec, - unsigned_flag); + collation.set_numeric(); + fix_char_length(my_decimal_precision_to_length_no_truncation(len, dec, + unsigned_flag)); } String *val_str(String *str); double val_real(); diff --git a/sql/item_geofunc.cc b/sql/item_geofunc.cc index 8c38cb2a859..c5554265fbe 100644 --- a/sql/item_geofunc.cc +++ b/sql/item_geofunc.cc @@ -52,7 +52,7 @@ String *Item_func_geometry_from_text::val_str(String *str) DBUG_ASSERT(fixed == 1); Geometry_buffer buffer; String arg_val; - String *wkt= args[0]->val_str(&arg_val); + String *wkt= args[0]->val_str_ascii(&arg_val); if ((null_value= args[0]->null_value)) return 0; @@ -110,7 +110,7 @@ String *Item_func_geometry_from_wkb::val_str(String *str) } -String *Item_func_as_wkt::val_str(String *str) +String *Item_func_as_wkt::val_str_ascii(String *str) { DBUG_ASSERT(fixed == 1); String arg_val; @@ -134,6 +134,7 @@ String *Item_func_as_wkt::val_str(String *str) void Item_func_as_wkt::fix_length_and_dec() { + collation.set(default_charset(), DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII); max_length=MAX_BLOB_WIDTH; maybe_null= 1; } @@ -157,7 +158,7 @@ String *Item_func_as_wkb::val_str(String *str) } -String *Item_func_geometry_type::val_str(String *str) +String *Item_func_geometry_type::val_str_ascii(String *str) { DBUG_ASSERT(fixed == 1); String *swkb= args[0]->val_str(str); diff --git a/sql/item_geofunc.h b/sql/item_geofunc.h index 9a55ea7d5b1..25755de1e2c 100644 --- a/sql/item_geofunc.h +++ b/sql/item_geofunc.h @@ -57,12 +57,12 @@ public: String *val_str(String *); }; -class Item_func_as_wkt: public Item_str_func +class Item_func_as_wkt: public Item_str_ascii_func { public: - Item_func_as_wkt(Item *a): Item_str_func(a) {} + Item_func_as_wkt(Item *a): Item_str_ascii_func(a) {} const char *func_name() const { return "astext"; } - String *val_str(String *); + String *val_str_ascii(String *); void fix_length_and_dec(); }; @@ -75,16 +75,17 @@ public: enum_field_types field_type() const { return MYSQL_TYPE_BLOB; } }; -class Item_func_geometry_type: public Item_str_func +class Item_func_geometry_type: public Item_str_ascii_func { public: - Item_func_geometry_type(Item *a): Item_str_func(a) {} - String *val_str(String *); + Item_func_geometry_type(Item *a): Item_str_ascii_func(a) {} + String *val_str_ascii(String *); const char *func_name() const { return "geometrytype"; } void fix_length_and_dec() { - max_length=20; // "GeometryCollection" is the most long - maybe_null= 1; + // "GeometryCollection" is the longest + fix_length_and_charset(20, default_charset()); + maybe_null= 1; }; }; diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index c33e0f4c6fb..7bb2d358abf 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -41,6 +41,38 @@ C_MODE_END String my_empty_string("",default_charset_info); +/* + For the Items which have only val_str_ascii() method + and don't have their own "native" val_str(), + we provide a "wrapper" method to convert from ASCII + to Item character set when it's necessary. + Conversion happens only in case of "tricky" Item character set (e.g. UCS2). + Normally conversion does not happen, and val_str_ascii() is immediately + returned instead. +*/ +String *Item_str_ascii_func::val_str(String *str) +{ + DBUG_ASSERT(fixed == 1); + + if (!(collation.collation->state & MY_CS_NONASCII)) + return val_str_ascii(str); + + DBUG_ASSERT(str != &ascii_buf); + + uint errors; + String *res= val_str_ascii(&ascii_buf); + if (!res) + return 0; + + if ((null_value= str->copy(res->ptr(), res->length(), + &my_charset_latin1, collation.collation, + &errors))) + return 0; + + return str; +} + + /* Convert an array of bytes to a hexadecimal representation. @@ -112,7 +144,7 @@ longlong Item_str_func::val_int() } -String *Item_func_md5::val_str(String *str) +String *Item_func_md5::val_str_ascii(String *str) { DBUG_ASSERT(fixed == 1); String * sptr= args[0]->val_str(str); @@ -139,7 +171,6 @@ String *Item_func_md5::val_str(String *str) void Item_func_md5::fix_length_and_dec() { - max_length=32; /* The MD5() function treats its parameter as being a case sensitive. Thus we set binary collation on it so different instances of MD5() will be @@ -148,10 +179,11 @@ void Item_func_md5::fix_length_and_dec() args[0]->collation.set( get_charset_by_csname(args[0]->collation.collation->csname, MY_CS_BINSORT,MYF(0)), DERIVATION_COERCIBLE); + fix_length_and_charset(32, default_charset()); } -String *Item_func_sha::val_str(String *str) +String *Item_func_sha::val_str_ascii(String *str) { DBUG_ASSERT(fixed == 1); String * sptr= args[0]->val_str(str); @@ -181,7 +213,6 @@ String *Item_func_sha::val_str(String *str) void Item_func_sha::fix_length_and_dec() { - max_length=SHA1_HASH_SIZE*2; // size of hex representation of hash /* The SHA() function treats its parameter as being a case sensitive. Thus we set binary collation on it so different instances of MD5() will be @@ -190,6 +221,8 @@ void Item_func_sha::fix_length_and_dec() args[0]->collation.set( get_charset_by_csname(args[0]->collation.collation->csname, MY_CS_BINSORT,MYF(0)), DERIVATION_COERCIBLE); + // size of hex representation of hash + fix_length_and_charset(SHA1_HASH_SIZE * 2, default_charset()); } @@ -414,7 +447,7 @@ void Item_func_concat::fix_length_and_dec() { ulonglong max_result_length= 0; - if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1)) + if (agg_arg_charsets_for_string_result(collation, args, arg_count)) return; for (uint i=0 ; i < arg_count ; i++) @@ -772,7 +805,7 @@ void Item_func_concat_ws::fix_length_and_dec() { ulonglong max_result_length; - if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1)) + if (agg_arg_charsets_for_string_result(collation, args, arg_count)) return; /* @@ -843,8 +876,8 @@ String *Item_func_reverse::val_str(String *str) void Item_func_reverse::fix_length_and_dec() { - collation.set(args[0]->collation); - max_length = args[0]->max_length; + agg_arg_charsets_for_string_result(collation, args, 1); + fix_char_length(args[0]->max_char_length()); } /** @@ -987,7 +1020,7 @@ void Item_func_replace::fix_length_and_dec() } max_length= (ulong) max_result_length; - if (agg_arg_charsets(collation, args, 3, MY_COLL_CMP_CONV, 1)) + if (agg_arg_charsets_for_comparison(collation, args, 3)) return; } @@ -1046,7 +1079,7 @@ void Item_func_insert::fix_length_and_dec() ulonglong max_result_length; // Handle character set for args[0] and args[3]. - if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 3)) + if (agg_arg_charsets_for_string_result(collation, args, 2, 3)) return; max_result_length= ((ulonglong) args[0]->max_length+ (ulonglong) args[3]->max_length); @@ -1094,7 +1127,7 @@ String *Item_str_conv::val_str(String *str) void Item_func_lcase::fix_length_and_dec() { - collation.set(args[0]->collation); + agg_arg_charsets_for_string_result(collation, args, 1); multiply= collation.collation->casedn_multiply; converter= collation.collation->cset->casedn; max_length= args[0]->max_length * multiply; @@ -1102,7 +1135,7 @@ void Item_func_lcase::fix_length_and_dec() void Item_func_ucase::fix_length_and_dec() { - collation.set(args[0]->collation); + agg_arg_charsets_for_string_result(collation, args, 1); multiply= collation.collation->caseup_multiply; converter= collation.collation->cset->caseup; max_length= args[0]->max_length * multiply; @@ -1150,7 +1183,7 @@ void Item_str_func::left_right_max_length() void Item_func_left::fix_length_and_dec() { - collation.set(args[0]->collation); + agg_arg_charsets_for_string_result(collation, args, 1); left_right_max_length(); } @@ -1183,7 +1216,7 @@ String *Item_func_right::val_str(String *str) void Item_func_right::fix_length_and_dec() { - collation.set(args[0]->collation); + agg_arg_charsets_for_string_result(collation, args, 1); left_right_max_length(); } @@ -1239,7 +1272,7 @@ void Item_func_substr::fix_length_and_dec() { max_length=args[0]->max_length; - collation.set(args[0]->collation); + agg_arg_charsets_for_string_result(collation, args, 1); if (args[1]->const_item()) { int32 start= (int32) args[1]->val_int(); @@ -1264,7 +1297,7 @@ void Item_func_substr_index::fix_length_and_dec() { max_length= args[0]->max_length; - if (agg_arg_charsets(collation, args, 2, MY_COLL_CMP_CONV, 1)) + if (agg_arg_charsets_for_comparison(collation, args, 2)) return; } @@ -1594,7 +1627,7 @@ void Item_func_trim::fix_length_and_dec() max_length= args[0]->max_length; if (arg_count == 1) { - collation.set(args[0]->collation); + agg_arg_charsets_for_string_result(collation, args, 1); remove.set_charset(collation.collation); remove.set_ascii(" ",1); } @@ -1602,7 +1635,7 @@ void Item_func_trim::fix_length_and_dec() { // Handle character set for args[1] and args[0]. // Note that we pass args[1] as the first item, and args[0] as the second. - if (agg_arg_charsets(collation, &args[1], 2, MY_COLL_CMP_CONV, -1)) + if (agg_arg_charsets_for_comparison(collation, &args[1], 2, -1)) return; } } @@ -1627,7 +1660,7 @@ void Item_func_trim::print(String *str, enum_query_type query_type) /* Item_func_password */ -String *Item_func_password::val_str(String *str) +String *Item_func_password::val_str_ascii(String *str) { DBUG_ASSERT(fixed == 1); String *res= args[0]->val_str(str); @@ -1636,7 +1669,7 @@ String *Item_func_password::val_str(String *str) if (res->length() == 0) return &my_empty_string; my_make_scrambled_password(tmp_value, res->ptr(), res->length()); - str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH, res->charset()); + str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH, &my_charset_latin1); return str; } @@ -1651,7 +1684,7 @@ char *Item_func_password::alloc(THD *thd, const char *password, /* Item_func_old_password */ -String *Item_func_old_password::val_str(String *str) +String *Item_func_old_password::val_str_ascii(String *str) { DBUG_ASSERT(fixed == 1); String *res= args[0]->val_str(str); @@ -1660,7 +1693,7 @@ String *Item_func_old_password::val_str(String *str) if (res->length() == 0) return &my_empty_string; my_make_scrambled_password_323(tmp_value, res->ptr(), res->length()); - str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH_323, res->charset()); + str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH_323, &my_charset_latin1); return str; } @@ -1880,7 +1913,7 @@ bool Item_func_current_user::fix_fields(THD *thd, Item **ref) void Item_func_soundex::fix_length_and_dec() { - collation.set(args[0]->collation); + agg_arg_charsets_for_string_result(collation, args, 1); max_length=args[0]->max_length; set_if_bigger(max_length, 4 * collation.collation->mbminlen); tmp_value.set_charset(collation.collation); @@ -2186,7 +2219,7 @@ void Item_func_elt::fix_length_and_dec() max_length=0; decimals=0; - if (agg_arg_charsets(collation, args+1, arg_count-1, MY_COLL_ALLOW_CONV, 1)) + if (agg_arg_charsets_for_string_result(collation, args + 1, arg_count - 1)) return; for (uint i= 1 ; i < arg_count ; i++) @@ -2253,7 +2286,7 @@ void Item_func_make_set::fix_length_and_dec() { max_length=arg_count-1; - if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1)) + if (agg_arg_charsets_for_string_result(collation, args, arg_count)) return; for (uint i=0 ; i < arg_count ; i++) @@ -2413,7 +2446,7 @@ inline String* alloc_buffer(String *res,String *str,String *tmp_value, void Item_func_repeat::fix_length_and_dec() { - collation.set(args[0]->collation); + agg_arg_charsets_for_string_result(collation, args, 1); if (args[1]->const_item()) { /* must be longlong to avoid truncation */ @@ -2497,7 +2530,7 @@ err: void Item_func_rpad::fix_length_and_dec() { // Handle character set for args[0] and args[2]. - if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 2)) + if (agg_arg_charsets_for_string_result(collation, &args[0], 2, 2)) return; if (args[1]->const_item()) { @@ -2599,7 +2632,7 @@ String *Item_func_rpad::val_str(String *str) void Item_func_lpad::fix_length_and_dec() { // Handle character set for args[0] and args[2]. - if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 2)) + if (agg_arg_charsets_for_string_result(collation, &args[0], 2, 2)) return; if (args[1]->const_item()) @@ -2769,7 +2802,7 @@ String *Item_func_conv_charset::val_str(String *str) void Item_func_conv_charset::fix_length_and_dec() { collation.set(conv_charset, DERIVATION_IMPLICIT); - max_length = args[0]->max_length*conv_charset->mbmaxlen; + fix_char_length(args[0]->max_char_length()); } void Item_func_conv_charset::print(String *str, enum_query_type query_type) @@ -2859,7 +2892,7 @@ String *Item_func_charset::val_str(String *str) DBUG_ASSERT(fixed == 1); uint dummy_errors; - CHARSET_INFO *cs= args[0]->collation.collation; + CHARSET_INFO *cs= args[0]->charset_for_protocol(); null_value= 0; str->copy(cs->csname, (uint) strlen(cs->csname), &my_charset_latin1, collation.collation, &dummy_errors); @@ -2870,7 +2903,7 @@ String *Item_func_collation::val_str(String *str) { DBUG_ASSERT(fixed == 1); uint dummy_errors; - CHARSET_INFO *cs= args[0]->collation.collation; + CHARSET_INFO *cs= args[0]->charset_for_protocol(); null_value= 0; str->copy(cs->name, (uint) strlen(cs->name), @@ -2904,7 +2937,7 @@ String *Item_func_hex::val_str(String *str) if ((null_value= args[0]->null_value)) return 0; ptr= longlong2str(dec,ans,16); - if (str->copy(ans,(uint32) (ptr-ans),default_charset())) + if (str->copy(ans,(uint32) (ptr-ans), &my_charset_numeric)) return &my_empty_string; // End of memory return str; } @@ -3109,11 +3142,11 @@ void Item_func_export_set::fix_length_and_dec() { uint length=max(args[1]->max_length,args[2]->max_length); uint sep_length=(arg_count > 3 ? args[3]->max_length : 1); - max_length=length*64+sep_length*63; - if (agg_arg_charsets(collation, args+1, min(4,arg_count)-1, - MY_COLL_ALLOW_CONV, 1)) + if (agg_arg_charsets_for_string_result(collation, + args + 1, min(4, arg_count) - 1)) return; + fix_char_length(length * 64 + sep_length * 63); } String* Item_func_inet_ntoa::val_str(String* str) @@ -3132,6 +3165,7 @@ String* Item_func_inet_ntoa::val_str(String* str) if ((null_value= (args[0]->null_value || n > (ulonglong) LL(4294967295)))) return 0; // Null value + str->set_charset(collation.collation); str->length(0); int4store(buf,n); @@ -3149,11 +3183,11 @@ String* Item_func_inet_ntoa::val_str(String* str) num[0]=(char) n1+'0'; num[1]=(char) n2+'0'; num[2]=(char) c+'0'; - uint length=(n1 ? 4 : n2 ? 3 : 2); // Remove pre-zero - - (void) str->append(num+4-length,length); + uint length= (n1 ? 4 : n2 ? 3 : 2); // Remove pre-zero + uint dot_length= (p <= buf) ? 1 : 0; + (void) str->append(num + 4 - length, length - dot_length, + &my_charset_latin1); } - str->length(str->length()-1); // Remove last '.'; return str; } diff --git a/sql/item_strfunc.h b/sql/item_strfunc.h index 09a7da021c0..c9d62ac1052 100644 --- a/sql/item_strfunc.h +++ b/sql/item_strfunc.h @@ -41,28 +41,41 @@ public: bool fix_fields(THD *thd, Item **ref); }; -class Item_func_md5 :public Item_str_func + + +/* + Functions that return values with ASCII repertoire +*/ +class Item_str_ascii_func :public Item_str_func +{ + String ascii_buf; +public: + Item_str_ascii_func() :Item_str_func() {} + Item_str_ascii_func(Item *a) :Item_str_func(a) {} + Item_str_ascii_func(Item *a,Item *b) :Item_str_func(a,b) {} + Item_str_ascii_func(Item *a,Item *b,Item *c) :Item_str_func(a,b,c) {} + String *val_str_convert_from_ascii(String *str, String *ascii_buf); + String *val_str(String *str); + virtual String *val_str_ascii(String *)= 0; +}; + + +class Item_func_md5 :public Item_str_ascii_func { String tmp_value; public: - Item_func_md5(Item *a) :Item_str_func(a) - { - collation.set(&my_charset_bin); - } - String *val_str(String *); + Item_func_md5(Item *a) :Item_str_ascii_func(a) {} + String *val_str_ascii(String *); void fix_length_and_dec(); const char *func_name() const { return "md5"; } }; -class Item_func_sha :public Item_str_func +class Item_func_sha :public Item_str_ascii_func { public: - Item_func_sha(Item *a) :Item_str_func(a) - { - collation.set(&my_charset_bin); - } - String *val_str(String *); + Item_func_sha(Item *a) :Item_str_ascii_func(a) {} + String *val_str_ascii(String *); void fix_length_and_dec(); const char *func_name() const { return "sha"; } }; @@ -263,13 +276,16 @@ public: authentication procedure works, see comments in password.c. */ -class Item_func_password :public Item_str_func +class Item_func_password :public Item_str_ascii_func { char tmp_value[SCRAMBLED_PASSWORD_CHAR_LENGTH+1]; public: - Item_func_password(Item *a) :Item_str_func(a) {} - String *val_str(String *str); - void fix_length_and_dec() { max_length= SCRAMBLED_PASSWORD_CHAR_LENGTH; } + Item_func_password(Item *a) :Item_str_ascii_func(a) {} + String *val_str_ascii(String *str); + void fix_length_and_dec() + { + fix_length_and_charset(SCRAMBLED_PASSWORD_CHAR_LENGTH, default_charset()); + } const char *func_name() const { return "password"; } static char *alloc(THD *thd, const char *password, size_t pass_len); }; @@ -282,13 +298,16 @@ public: function. */ -class Item_func_old_password :public Item_str_func +class Item_func_old_password :public Item_str_ascii_func { char tmp_value[SCRAMBLED_PASSWORD_CHAR_LENGTH_323+1]; public: - Item_func_old_password(Item *a) :Item_str_func(a) {} - String *val_str(String *str); - void fix_length_and_dec() { max_length= SCRAMBLED_PASSWORD_CHAR_LENGTH_323; } + Item_func_old_password(Item *a) :Item_str_ascii_func(a) {} + String *val_str_ascii(String *str); + void fix_length_and_dec() + { + fix_length_and_charset(SCRAMBLED_PASSWORD_CHAR_LENGTH_323, default_charset()); + } const char *func_name() const { return "old_password"; } static char *alloc(THD *thd, const char *password, size_t pass_len); }; @@ -688,7 +707,7 @@ public: void fix_length_and_dec() { decimals= 0; - max_length= 3 * 8 + 7; + fix_length_and_charset(3 * 8 + 7, default_charset()); maybe_null= 1; } }; @@ -848,14 +867,11 @@ class Item_func_uuid: public Item_str_func { public: Item_func_uuid(): Item_str_func() {} - void fix_length_and_dec() { - collation.set(system_charset_info); - /* - NOTE! uuid() should be changed to use 'ascii' - charset when hex(), format(), md5(), etc, and implicit - number-to-string conversion will use 'ascii' - */ - max_length= UUID_LENGTH * system_charset_info->mbmaxlen; + void fix_length_and_dec() + { + collation.set(system_charset_info, + DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII); + fix_char_length(UUID_LENGTH); } const char *func_name() const{ return "uuid"; } String *val_str(String *); diff --git a/sql/item_sum.cc b/sql/item_sum.cc index a61c5d59d67..3d83c7ee513 100644 --- a/sql/item_sum.cc +++ b/sql/item_sum.cc @@ -3202,11 +3202,9 @@ Item_func_group_concat::fix_fields(THD *thd, Item **ref) return TRUE; } - if (agg_item_charsets(collation, func_name(), - args, - /* skip charset aggregation for order columns */ - arg_count - arg_count_order, - MY_COLL_ALLOW_CONV, 1)) + /* skip charset aggregation for order columns */ + if (agg_item_charsets_for_string_result(collation, func_name(), + args, arg_count - arg_count_order)) return 1; result.set_charset(collation.collation); diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc index 392bc936f17..49d31bf1252 100644 --- a/sql/item_timefunc.cc +++ b/sql/item_timefunc.cc @@ -57,7 +57,7 @@ static bool make_datetime(date_time_format_types format, MYSQL_TIME *ltime, String *str) { char *buff; - CHARSET_INFO *cs= &my_charset_bin; + CHARSET_INFO *cs= &my_charset_numeric; uint length= MAX_DATE_STRING_REP_LENGTH; if (str->alloc(length)) @@ -1586,9 +1586,7 @@ bool Item_func_from_days::get_date(MYSQL_TIME *ltime, uint fuzzy_date) void Item_func_curdate::fix_length_and_dec() { - collation.set(&my_charset_bin); - decimals=0; - max_length=MAX_DATE_WIDTH*MY_CHARSET_BIN_MB_MAXLEN; + Item_date::fix_length_and_dec(); store_now_in_TIME(<ime); @@ -1649,7 +1647,7 @@ bool Item_func_curdate::get_date(MYSQL_TIME *res, String *Item_func_curtime::val_str(String *str) { DBUG_ASSERT(fixed == 1); - str_value.set(buff, buff_length, &my_charset_bin); + str_value.set(buff, buff_length, &my_charset_latin1); return &str_value; } @@ -1659,11 +1657,10 @@ void Item_func_curtime::fix_length_and_dec() MYSQL_TIME ltime; decimals= DATETIME_DEC; - collation.set(&my_charset_bin); store_now_in_TIME(<ime); value= TIME_to_ulonglong_time(<ime); buff_length= (uint) my_time_to_str(<ime, buff); - max_length= buff_length; + fix_length_and_charset_datetime(buff_length); } @@ -1698,7 +1695,7 @@ void Item_func_curtime_utc::store_now_in_TIME(MYSQL_TIME *now_time) String *Item_func_now::val_str(String *str) { DBUG_ASSERT(fixed == 1); - str_value.set(buff,buff_length, &my_charset_bin); + str_value.set(buff, buff_length, &my_charset_numeric); return &str_value; } @@ -1706,13 +1703,12 @@ String *Item_func_now::val_str(String *str) void Item_func_now::fix_length_and_dec() { decimals= DATETIME_DEC; - collation.set(&my_charset_bin); store_now_in_TIME(<ime); value= (longlong) TIME_to_ulonglong_datetime(<ime); buff_length= (uint) my_datetime_to_str(<ime, buff); - max_length= buff_length; + fix_length_and_charset_datetime(buff_length); } @@ -1776,7 +1772,7 @@ String *Item_func_sysdate_local::val_str(String *str) DBUG_ASSERT(fixed == 1); store_now_in_TIME(<ime); buff_length= (uint) my_datetime_to_str(<ime, buff); - str_value.set(buff, buff_length, &my_charset_bin); + str_value.set(buff, buff_length, &my_charset_numeric); return &str_value; } @@ -1800,8 +1796,7 @@ double Item_func_sysdate_local::val_real() void Item_func_sysdate_local::fix_length_and_dec() { decimals= 0; - collation.set(&my_charset_bin); - max_length= MAX_DATETIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN; + fix_length_and_charset_datetime(MAX_DATETIME_WIDTH); } @@ -2005,7 +2000,8 @@ String *Item_func_date_format::val_str(String *str) { String *res; if (!(res=args[0]->val_str(str)) || - (str_to_time_with_warn(res->ptr(), res->length(), &l_time))) + (str_to_time_with_warn(res->charset(), res->ptr(), res->length(), + &l_time))) goto null_date; l_time.year=l_time.month=l_time.day=0; @@ -2049,9 +2045,8 @@ null_date: void Item_func_from_unixtime::fix_length_and_dec() { thd= current_thd; - collation.set(&my_charset_bin); decimals= DATETIME_DEC; - max_length=MAX_DATETIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN; + fix_length_and_charset_datetime(MAX_DATETIME_WIDTH); maybe_null= 1; thd->time_zone_used= 1; } @@ -2109,9 +2104,8 @@ bool Item_func_from_unixtime::get_date(MYSQL_TIME *ltime, void Item_func_convert_tz::fix_length_and_dec() { - collation.set(&my_charset_bin); decimals= 0; - max_length= MAX_DATETIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN; + fix_length_and_charset_datetime(MAX_DATETIME_WIDTH); maybe_null= 1; } @@ -2155,13 +2149,13 @@ bool Item_func_convert_tz::get_date(MYSQL_TIME *ltime, if (!from_tz_cached) { - from_tz= my_tz_find(thd, args[1]->val_str(&str)); + from_tz= my_tz_find(thd, args[1]->val_str_ascii(&str)); from_tz_cached= args[1]->const_item(); } if (!to_tz_cached) { - to_tz= my_tz_find(thd, args[2]->val_str(&str)); + to_tz= my_tz_find(thd, args[2]->val_str_ascii(&str)); to_tz_cached= args[2]->const_item(); } @@ -2195,9 +2189,8 @@ void Item_date_add_interval::fix_length_and_dec() { enum_field_types arg0_field_type; - collation.set(&my_charset_bin); maybe_null=1; - max_length=MAX_DATETIME_FULL_WIDTH*MY_CHARSET_BIN_MB_MAXLEN; + fix_length_and_charset_datetime(MAX_DATETIME_FULL_WIDTH); value.alloc(max_length); /* @@ -2375,7 +2368,9 @@ longlong Item_extract::val_int() else { String *res= args[0]->val_str(&value); - if (!res || str_to_time_with_warn(res->ptr(), res->length(), <ime)) + if (!res || + str_to_time_with_warn(res->charset(), res->ptr(), res->length(), + <ime)) { null_value=1; return 0; @@ -2814,7 +2809,7 @@ void Item_func_add_time::fix_length_and_dec() { enum_field_types arg0_field_type; decimals=0; - max_length=MAX_DATETIME_FULL_WIDTH*MY_CHARSET_BIN_MB_MAXLEN; + fix_length_and_charset_datetime(MAX_DATETIME_FULL_WIDTH); maybe_null= 1; /* @@ -3225,12 +3220,12 @@ void Item_func_timestamp_diff::print(String *str, enum_query_type query_type) } -String *Item_func_get_format::val_str(String *str) +String *Item_func_get_format::val_str_ascii(String *str) { DBUG_ASSERT(fixed == 1); const char *format_name; KNOWN_DATE_TIME_FORMAT *format; - String *val= args[0]->val_str(str); + String *val= args[0]->val_str_ascii(str); ulong val_len; if ((null_value= args[0]->null_value)) @@ -3249,7 +3244,7 @@ String *Item_func_get_format::val_str(String *str) (const uchar *) format_name, val_len)) { const char *format_str= get_date_time_format_str(format, type); - str->set(format_str, (uint) strlen(format_str), &my_charset_bin); + str->set(format_str, (uint) strlen(format_str), &my_charset_numeric); return str; } } diff --git a/sql/item_timefunc.h b/sql/item_timefunc.h index 860bd983184..20e4d6488d6 100644 --- a/sql/item_timefunc.h +++ b/sql/item_timefunc.h @@ -122,23 +122,22 @@ public: class Item_func_month :public Item_func { public: - Item_func_month(Item *a) :Item_func(a) {} + Item_func_month(Item *a) :Item_func(a) { collation.set_numeric(); } longlong val_int(); double val_real() { DBUG_ASSERT(fixed == 1); return (double) Item_func_month::val_int(); } String *val_str(String *str) { - str->set(val_int(), &my_charset_bin); + str->set(val_int(), collation.collation); return null_value ? 0 : str; } const char *func_name() const { return "month"; } enum Item_result result_type () const { return INT_RESULT; } void fix_length_and_dec() { - collation.set(&my_charset_bin); - decimals=0; - max_length=2*MY_CHARSET_BIN_MB_MAXLEN; - maybe_null=1; + decimals= 0; + fix_char_length(2); + maybe_null= 1; } bool check_partition_func_processor(uchar *int_arg) {return FALSE;} }; @@ -165,9 +164,9 @@ public: const char *func_name() const { return "dayofyear"; } void fix_length_and_dec() { - decimals=0; - max_length=3*MY_CHARSET_BIN_MB_MAXLEN; - maybe_null=1; + decimals= 0; + fix_char_length(3); + maybe_null= 1; } bool check_partition_func_processor(uchar *int_arg) {return FALSE;} }; @@ -290,7 +289,7 @@ class Item_func_weekday :public Item_func bool odbc_type; public: Item_func_weekday(Item *a,bool type_arg) - :Item_func(a), odbc_type(type_arg) {} + :Item_func(a), odbc_type(type_arg) { collation.set_numeric(); } longlong val_int(); double val_real() { DBUG_ASSERT(fixed == 1); return (double) val_int(); } String *val_str(String *str) @@ -306,10 +305,9 @@ public: enum Item_result result_type () const { return INT_RESULT; } void fix_length_and_dec() { - collation.set(&my_charset_bin); - decimals=0; - max_length=1*MY_CHARSET_BIN_MB_MAXLEN; - maybe_null=1; + decimals= 0; + fix_char_length(1); + maybe_null= 1; } bool check_partition_func_processor(uchar *int_arg) {return FALSE;} }; @@ -379,15 +377,15 @@ public: Item_date(Item *a) :Item_func(a) {} enum Item_result result_type () const { return STRING_RESULT; } enum_field_types field_type() const { return MYSQL_TYPE_DATE; } + CHARSET_INFO *charset_for_protocol(void) const { return &my_charset_bin; } String *val_str(String *str); longlong val_int(); double val_real() { return val_real_from_decimal(); } const char *func_name() const { return "date"; } void fix_length_and_dec() { - collation.set(&my_charset_bin); - decimals=0; - max_length=MAX_DATE_WIDTH*MY_CHARSET_BIN_MB_MAXLEN; + decimals= 0; + fix_length_and_charset_datetime(MAX_DATE_WIDTH); } Field *tmp_table_field(TABLE *table) { @@ -414,6 +412,7 @@ public: Item_date_func(Item *a,Item *b) :Item_str_func(a,b) {} Item_date_func(Item *a,Item *b, Item *c) :Item_str_func(a,b,c) {} enum_field_types field_type() const { return MYSQL_TYPE_DATETIME; } + CHARSET_INFO *charset_for_protocol(void) const { return &my_charset_bin; } Field *tmp_table_field(TABLE *table) { return tmp_table_field_from_field_type(table, 0); @@ -440,10 +439,11 @@ public: Item_str_timefunc(Item *a,Item *b) :Item_str_func(a,b) {} Item_str_timefunc(Item *a, Item *b, Item *c) :Item_str_func(a, b ,c) {} enum_field_types field_type() const { return MYSQL_TYPE_TIME; } + CHARSET_INFO *charset_for_protocol(void) const { return &my_charset_bin; } void fix_length_and_dec() { decimals= DATETIME_DEC; - max_length=MAX_TIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN; + fix_length_and_charset_datetime(MAX_TIME_WIDTH); } Field *tmp_table_field(TABLE *table) { @@ -701,7 +701,6 @@ public: void fix_length_and_dec() { Item_str_timefunc::fix_length_and_dec(); - collation.set(&my_charset_bin); maybe_null=1; } const char *func_name() const { return "sec_to_time"; } @@ -774,13 +773,7 @@ public: class Item_typecast_maybe_null :public Item_typecast { public: - Item_typecast_maybe_null(Item *a) :Item_typecast(a) {} - void fix_length_and_dec() - { - collation.set(&my_charset_bin); - max_length=args[0]->max_length; - maybe_null= 1; - } + Item_typecast_maybe_null(Item *a) :Item_typecast(a) { maybe_null= 1; } }; @@ -813,16 +806,12 @@ public: bool get_time(MYSQL_TIME *ltime); const char *cast_type() const { return "date"; } enum_field_types field_type() const { return MYSQL_TYPE_DATE; } + CHARSET_INFO *charset_for_protocol(void) const { return &my_charset_bin; } Field *tmp_table_field(TABLE *table) { return tmp_table_field_from_field_type(table, 0); - } - void fix_length_and_dec() - { - collation.set(&my_charset_bin); - max_length= 10; - maybe_null= 1; } + void fix_length_and_dec() { fix_length_and_charset_datetime(10); } bool result_as_longlong() { return TRUE; } longlong val_int(); double val_real() { return (double) val_int(); } @@ -847,6 +836,7 @@ public: bool get_time(MYSQL_TIME *ltime); const char *cast_type() const { return "time"; } enum_field_types field_type() const { return MYSQL_TYPE_TIME; } + CHARSET_INFO *charset_for_protocol(void) const { return &my_charset_bin; } Field *tmp_table_field(TABLE *table) { return tmp_table_field_from_field_type(table, 0); @@ -863,6 +853,8 @@ public: { return save_time_in_field(field); } + void fix_length_and_dec() + { fix_length_and_charset_datetime(args[0]->max_char_length()); } }; @@ -874,15 +866,14 @@ public: String *val_str(String *str); const char *cast_type() const { return "datetime"; } enum_field_types field_type() const { return MYSQL_TYPE_DATETIME; } + CHARSET_INFO *charset_for_protocol(void) const { return &my_charset_bin; } Field *tmp_table_field(TABLE *table) { return tmp_table_field_from_field_type(table, 0); } void fix_length_and_dec() { - collation.set(&my_charset_bin); - maybe_null= 1; - max_length= MAX_DATETIME_FULL_WIDTH * MY_CHARSET_BIN_MB_MAXLEN; + fix_length_and_charset_datetime(MAX_DATETIME_FULL_WIDTH); decimals= DATETIME_DEC; } bool result_as_longlong() { return TRUE; } @@ -907,10 +898,11 @@ public: String *val_str(String *str); const char *func_name() const { return "makedate"; } enum_field_types field_type() const { return MYSQL_TYPE_DATE; } + CHARSET_INFO *charset_for_protocol(void) const { return &my_charset_bin; } void fix_length_and_dec() { decimals=0; - max_length=MAX_DATE_WIDTH*MY_CHARSET_BIN_MB_MAXLEN; + fix_length_and_charset_datetime(MAX_DATE_WIDTH); maybe_null= 1; } longlong val_int(); @@ -929,6 +921,7 @@ public: String *val_str(String *str); enum_field_types field_type() const { return cached_field_type; } void fix_length_and_dec(); + CHARSET_INFO *charset_for_protocol(void) const { return &my_charset_bin; } Field *tmp_table_field(TABLE *table) { @@ -1019,20 +1012,20 @@ enum date_time_format USA_FORMAT, JIS_FORMAT, ISO_FORMAT, EUR_FORMAT, INTERNAL_FORMAT }; -class Item_func_get_format :public Item_str_func +class Item_func_get_format :public Item_str_ascii_func { public: const timestamp_type type; // keep it public Item_func_get_format(timestamp_type type_arg, Item *a) - :Item_str_func(a), type(type_arg) + :Item_str_ascii_func(a), type(type_arg) {} - String *val_str(String *str); + String *val_str_ascii(String *str); const char *func_name() const { return "get_format"; } void fix_length_and_dec() { maybe_null= 1; decimals=0; - max_length=17*MY_CHARSET_BIN_MB_MAXLEN; + fix_length_and_charset(17, default_charset()); } virtual void print(String *str, enum_query_type query_type); }; diff --git a/sql/item_xmlfunc.cc b/sql/item_xmlfunc.cc index 3e20b90e68e..3621733f456 100644 --- a/sql/item_xmlfunc.cc +++ b/sql/item_xmlfunc.cc @@ -2569,7 +2569,7 @@ void Item_xml_str_func::fix_length_and_dec() nodeset_func= 0; - if (agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1)) + if (agg_arg_charsets_for_comparison(collation, args, arg_count)) return; if (collation.collation->mbminlen > 1) diff --git a/sql/my_decimal.cc b/sql/my_decimal.cc index 16d07526a0f..234a0a98782 100644 --- a/sql/my_decimal.cc +++ b/sql/my_decimal.cc @@ -110,10 +110,63 @@ int my_decimal2string(uint mask, const my_decimal *d, &length, (int)fixed_prec, fixed_dec, filler); str->length(length); + str->set_charset(&my_charset_numeric); return check_result(mask, result); } +/** + @brief Converting decimal to string with character set conversion + + @details Convert given my_decimal to String; allocate buffer as needed. + + @param[in] mask what problems to warn on (mask of E_DEC_* values) + @param[in] val the decimal to print + @param[in] fixed_prec overall number of digits if ZEROFILL, 0 otherwise + @param[in] fixed_dec number of decimal places (if fixed_prec != 0) + @param[in] filler what char to pad with (ZEROFILL et al.) + @param[out] *str where to store the resulting string + @param[in] cs character set + + @return error coce + @retval E_DEC_OK + @retval E_DEC_TRUNCATED + @retval E_DEC_OVERFLOW + @retval E_DEC_OOM + + Would be great to make it a method of the String class, + but this would need to include + my_decimal.h from sql_string.h and sql_string.cc, which is not desirable. +*/ +bool +str_set_decimal(uint mask, const my_decimal *val, + uint fixed_prec, uint fixed_dec, char filler, + String *str, CHARSET_INFO *cs) +{ + if (!(cs->state & MY_CS_NONASCII)) + { + /* For ASCII-compatible character sets we can use my_decimal2string */ + my_decimal2string(mask, val, fixed_prec, fixed_dec, filler, str); + str->set_charset(cs); + return FALSE; + } + else + { + /* + For ASCII-incompatible character sets (like UCS2) we + call my_decimal2string() on a temporary buffer first, + and then convert the result to the target character + with help of str->copy(). + */ + uint errors; + char buf[DECIMAL_MAX_STR_LENGTH]; + String tmp(buf, sizeof(buf), &my_charset_latin1); + my_decimal2string(mask, val, fixed_prec, fixed_dec, filler, &tmp); + return str->copy(tmp.ptr(), tmp.length(), &my_charset_latin1, cs, &errors); + } +} + + /* Convert from decimal to binary representation diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 944bc9d4cdd..b4b871e68ba 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -218,7 +218,8 @@ extern CHARSET_INFO *error_message_charset_info; enum Derivation { - DERIVATION_IGNORABLE= 5, + DERIVATION_IGNORABLE= 6, + DERIVATION_NUMERIC= 5, DERIVATION_COERCIBLE= 4, DERIVATION_SYSCONST= 3, DERIVATION_IMPLICIT= 2, @@ -226,6 +227,8 @@ enum Derivation DERIVATION_EXPLICIT= 0 }; +#define my_charset_numeric my_charset_latin1 +#define MY_REPERTOIRE_NUMERIC MY_REPERTOIRE_ASCII typedef struct my_locale_errmsgs { @@ -869,6 +872,16 @@ typedef Comp_creator* (*chooser_compare_func_creator)(bool invert); #include "item.h" extern my_decimal decimal_zero; +/* my_decimal.cc */ +bool str_set_decimal(uint mask, const my_decimal *val, uint fixed_prec, + uint fixed_dec, char filler, String *str, + CHARSET_INFO *cs); +inline bool str_set_decimal(const my_decimal *val, String *str, + CHARSET_INFO *cs) +{ + return str_set_decimal(E_DEC_FATAL_ERROR, val, 0, 0, 0, str, cs); +} + /* sql_parse.cc */ void free_items(Item *item); void cleanup_items(Item *item); @@ -2233,8 +2246,17 @@ ulong convert_month_to_period(ulong month); void get_date_from_daynr(long daynr,uint *year, uint *month, uint *day); my_time_t TIME_to_timestamp(THD *thd, const MYSQL_TIME *t, my_bool *not_exist); -bool str_to_time_with_warn(const char *str,uint length,MYSQL_TIME *l_time); -timestamp_type str_to_datetime_with_warn(const char *str, uint length, +/* Character set-aware version of str_to_time() */ +bool str_to_time(CHARSET_INFO *cs, const char *str,uint length, + MYSQL_TIME *l_time, int *warning); +/* Character set-aware version of str_to_datetime() */ +timestamp_type str_to_datetime(CHARSET_INFO *cs, + const char *str, uint length, + MYSQL_TIME *l_time, uint flags, int *was_cut); +bool str_to_time_with_warn(CHARSET_INFO *cs, const char *str,uint length, + MYSQL_TIME *l_time); +timestamp_type str_to_datetime_with_warn(CHARSET_INFO *cs, + const char *str, uint length, MYSQL_TIME *l_time, uint flags); void localtime_to_TIME(MYSQL_TIME *to, struct tm *from); void calc_time_from_sec(MYSQL_TIME *to, long seconds, long microseconds); diff --git a/sql/protocol.cc b/sql/protocol.cc index 855a6842f1f..fd943c98bd6 100644 --- a/sql/protocol.cc +++ b/sql/protocol.cc @@ -736,10 +736,10 @@ bool Protocol::send_result_set_metadata(List<Item> *list, uint flags) /* Store fixed length fields */ pos= (char*) local_packet->ptr()+local_packet->length(); *pos++= 12; // Length of packed fields - if (item->collation.collation == &my_charset_bin || thd_charset == NULL) + if (item->charset_for_protocol() == &my_charset_bin || thd_charset == NULL) { /* No conversion */ - int2store(pos, field.charsetnr); + int2store(pos, item->charset_for_protocol()->number); int4store(pos+2, field.length); } else diff --git a/sql/time.cc b/sql/time.cc index 5f804072eb0..2db7812b9ef 100644 --- a/sql/time.cc +++ b/sql/time.cc @@ -214,6 +214,69 @@ ulong convert_month_to_period(ulong month) /* + Convert a string to 8-bit representation, + for use in str_to_time/str_to_date/str_to_date. + + In the future to_ascii() can be extended to convert + non-ASCII digits to ASCII digits + (for example, ARABIC-INDIC, DEVANAGARI, BENGALI, and so on) + so DATE/TIME/DATETIME values understand digits in the + respected scripts. +*/ +static uint +to_ascii(CHARSET_INFO *cs, + const char *src, uint src_length, + char *dst, uint dst_length) + +{ + int cnvres; + my_wc_t wc; + const char *srcend= src + src_length; + char *dst0= dst, *dstend= dst + dst_length - 1; + while (dst < dstend && + (cnvres= (cs->cset->mb_wc)(cs, &wc, + (const uchar*) src, + (const uchar*) srcend)) > 0 && + wc < 128) + { + src+= cnvres; + *dst++= wc; + } + *dst= '\0'; + return dst - dst0; +} + + +/* Character set-aware version of str_to_time() */ +bool str_to_time(CHARSET_INFO *cs, const char *str,uint length, + MYSQL_TIME *l_time, int *warning) +{ + char cnv[32]; + if ((cs->state & MY_CS_NONASCII) != 0) + { + length= to_ascii(cs, str, length, cnv, sizeof(cnv)); + str= cnv; + } + return str_to_time(str, length, l_time, warning); +} + + +/* Character set-aware version of str_to_datetime() */ +timestamp_type str_to_datetime(CHARSET_INFO *cs, + const char *str, uint length, + MYSQL_TIME *l_time, uint flags, int *was_cut) +{ + char cnv[32]; + if ((cs->state & MY_CS_NONASCII) != 0) + { + length= to_ascii(cs, str, length, cnv, sizeof(cnv)); + str= cnv; + } + return str_to_datetime(str, length, l_time, flags, was_cut); +} + + +/* Convert a timestamp string to a MYSQL_TIME value and produce a warning if string was truncated during conversion. @@ -222,14 +285,15 @@ ulong convert_month_to_period(ulong month) */ timestamp_type -str_to_datetime_with_warn(const char *str, uint length, MYSQL_TIME *l_time, +str_to_datetime_with_warn(CHARSET_INFO *cs, + const char *str, uint length, MYSQL_TIME *l_time, uint flags) { int was_cut; THD *thd= current_thd; timestamp_type ts_type; - ts_type= str_to_datetime(str, length, l_time, + ts_type= str_to_datetime(cs, str, length, l_time, (flags | (thd->variables.sql_mode & (MODE_INVALID_DATES | MODE_NO_ZERO_DATE))), @@ -284,7 +348,8 @@ my_time_t TIME_to_timestamp(THD *thd, const MYSQL_TIME *t, my_bool *in_dst_time_ See str_to_time() for more info. */ bool -str_to_time_with_warn(const char *str, uint length, MYSQL_TIME *l_time) +str_to_time_with_warn(CHARSET_INFO *cs, + const char *str, uint length, MYSQL_TIME *l_time) { int warning; bool ret_val= str_to_time(str, length, l_time, &warning); @@ -697,7 +762,7 @@ void make_time(const DATE_TIME_FORMAT *format __attribute__((unused)), { uint length= (uint) my_time_to_str(l_time, (char*) str->ptr()); str->length(length); - str->set_charset(&my_charset_bin); + str->set_charset(&my_charset_numeric); } @@ -706,7 +771,7 @@ void make_date(const DATE_TIME_FORMAT *format __attribute__((unused)), { uint length= (uint) my_date_to_str(l_time, (char*) str->ptr()); str->length(length); - str->set_charset(&my_charset_bin); + str->set_charset(&my_charset_numeric); } @@ -715,7 +780,7 @@ void make_datetime(const DATE_TIME_FORMAT *format __attribute__((unused)), { uint length= (uint) my_datetime_to_str(l_time, (char*) str->ptr()); str->length(length); - str->set_charset(&my_charset_bin); + str->set_charset(&my_charset_numeric); } |