diff --git a/include/decimal.h b/include/decimal.h index cab18f99348..02725fe0567 100644 --- a/include/decimal.h +++ b/include/decimal.h @@ -43,7 +43,8 @@ typedef struct st_decimal_t { int internal_str2dec(const char *from, decimal_t *to, char **end, my_bool fixed); int decimal2string(const decimal_t *from, char *to, int *to_len, - int fixed_precision, int fixed_decimals, + decimal_digits_t fixed_precision, + decimal_digits_t fixed_decimals, char filler); int decimal2ulonglong(const decimal_t *from, ulonglong *to); int ulonglong2decimal(ulonglong from, decimal_t *to); @@ -51,16 +52,18 @@ int decimal2longlong(const decimal_t *from, longlong *to); int longlong2decimal(longlong from, decimal_t *to); int decimal2double(const decimal_t *from, double *to); int double2decimal(double from, decimal_t *to); -int decimal_actual_fraction(const decimal_t *from); -int decimal2bin(const decimal_t *from, uchar *to, int precision, int scale); -int bin2decimal(const uchar *from, decimal_t *to, int precision, int scale); +decimal_digits_t decimal_actual_fraction(const decimal_t *from); +int decimal2bin(const decimal_t *from, uchar *to, decimal_digits_t precision, + decimal_digits_t scale); +int bin2decimal(const uchar *from, decimal_t *to, decimal_digits_t precision, + decimal_digits_t scale); -int decimal_size(int precision, int scale); -int decimal_bin_size(int precision, int scale); -int decimal_result_size(decimal_t *from1, decimal_t *from2, char op, - int param); +uint decimal_size(decimal_digits_t precision, decimal_digits_t scale); +uint decimal_bin_size(decimal_digits_t precision, decimal_digits_t scale); +uint decimal_result_size(decimal_t *from1, decimal_t *from2, char op, + int param); -int decimal_intg(const decimal_t *from); +decimal_digits_t decimal_intg(const decimal_t *from); int decimal_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to); int decimal_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to); int decimal_cmp(const decimal_t *from1, const decimal_t *from2); @@ -71,7 +74,8 @@ int decimal_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to); int decimal_round(const decimal_t *from, decimal_t *to, int new_scale, decimal_round_mode mode); int decimal_is_zero(const decimal_t *from); -void max_decimal(int precision, int frac, decimal_t *to); +void max_decimal(decimal_digits_t precision, decimal_digits_t frac, + decimal_t *to); #define string2decimal(A,B,C) internal_str2dec((A), (B), (C), 0) #define string2decimal_fixed(A,B,C) internal_str2dec((A), (B), (C), 1) diff --git a/include/m_ctype.h b/include/m_ctype.h index f15f7e3b16e..9dea95bfd9d 100644 --- a/include/m_ctype.h +++ b/include/m_ctype.h @@ -79,6 +79,7 @@ typedef const struct my_collation_handler_st MY_COLLATION_HANDLER; typedef const struct unicase_info_st MY_UNICASE_INFO; typedef const struct uni_ctype_st MY_UNI_CTYPE; typedef const struct my_uni_idx_st MY_UNI_IDX; +typedef uint16 decimal_digits_t; typedef struct unicase_info_char_st { diff --git a/mysys/ma_dyncol.c b/mysys/ma_dyncol.c index 8e8ac8a98a4..16373602805 100644 --- a/mysys/ma_dyncol.c +++ b/mysys/ma_dyncol.c @@ -1208,7 +1208,7 @@ dynamic_column_decimal_read(DYNAMIC_COLUMN_VALUE *store_it_here, (length - intg_len - frac_len) > (size_t) (DECIMAL_BUFF_LENGTH*sizeof(decimal_digit_t)) || decimal_bin_size(intg + frac, frac) != - (int) (length - intg_len - frac_len)) + (uint) (length - intg_len - frac_len)) return ER_DYNCOL_FORMAT; if (bin2decimal(data, &store_it_here->x.decimal.value, precision, scale) != diff --git a/plugin/type_inet/sql_type_inet.h b/plugin/type_inet/sql_type_inet.h index 4de1124f4c1..ee89c03619e 100644 --- a/plugin/type_inet/sql_type_inet.h +++ b/plugin/type_inet/sql_type_inet.h @@ -402,19 +402,19 @@ public: bool can_return_time() const override { return false; } bool convert_to_binary_using_val_native() const override { return true; } - uint Item_time_precision(THD *thd, Item *item) const override + decimal_digits_t Item_time_precision(THD *thd, Item *item) const override { return 0; } - uint Item_datetime_precision(THD *thd, Item *item) const override + decimal_digits_t Item_datetime_precision(THD *thd, Item *item) const override { return 0; } - uint Item_decimal_scale(const Item *item) const override + decimal_digits_t Item_decimal_scale(const Item *item) const override { return 0; } - uint Item_decimal_precision(const Item *item) const override + decimal_digits_t Item_decimal_precision(const Item *item) const override { /* This will be needed if we ever allow cast from INET6 to DECIMAL. @@ -429,7 +429,7 @@ public: Returns how many digits a divisor adds into a division result. See Item::divisor_precision_increment() in item.h for more comments. */ - uint Item_divisor_precision_increment(const Item *) const override + decimal_digits_t Item_divisor_precision_increment(const Item *) const override { return 0; } diff --git a/sql/field.cc b/sql/field.cc index fcae0f5bf11..a46517241e3 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -1500,7 +1500,7 @@ bool Field::make_empty_rec_store_default_value(THD *thd, Item *item) Field_num::Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg, uchar null_bit_arg, utype unireg_check_arg, const LEX_CSTRING *field_name_arg, - uint8 dec_arg, bool zero_arg, bool unsigned_arg) + decimal_digits_t dec_arg, bool zero_arg, bool unsigned_arg) :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg), dec(dec_arg),zerofill(zero_arg),unsigned_flag(unsigned_arg) @@ -3293,10 +3293,11 @@ Field *Field_decimal::make_new_field(MEM_ROOT *root, TABLE *new_table, ** Field_new_decimal ****************************************************************************/ -static uint get_decimal_precision(uint len, uint8 dec, bool unsigned_val) +static decimal_digits_t get_decimal_precision(uint len, decimal_digits_t dec, + bool unsigned_val) { uint precision= my_decimal_length_to_precision(len, dec, unsigned_val); - return MY_MIN(precision, DECIMAL_MAX_PRECISION); + return (decimal_digits_t) MY_MIN(precision, DECIMAL_MAX_PRECISION); } Field_new_decimal::Field_new_decimal(uchar *ptr_arg, @@ -3304,7 +3305,7 @@ Field_new_decimal::Field_new_decimal(uchar *ptr_arg, uchar null_bit_arg, enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg, - uint8 dec_arg,bool zero_arg, + decimal_digits_t dec_arg,bool zero_arg, bool unsigned_arg) :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg) @@ -4833,7 +4834,7 @@ int Field_double::store(longlong nr, bool unsigned_val) 1 Value was truncated */ -int truncate_double(double *nr, uint field_length, uint dec, +int truncate_double(double *nr, uint field_length, decimal_digits_t dec, bool unsigned_flag, double max_value) { int error= 0; @@ -10402,7 +10403,8 @@ void Column_definition::create_length_to_internal_length_bit() void Column_definition::create_length_to_internal_length_newdecimal() { DBUG_ASSERT(length < UINT_MAX32); - uint prec= get_decimal_precision((uint)length, decimals, flags & UNSIGNED_FLAG); + decimal_digit_t prec= get_decimal_precision((uint)length, decimals, + flags & UNSIGNED_FLAG); pack_length= my_decimal_get_binary_size(prec, decimals); } diff --git a/sql/field.h b/sql/field.h index ba50e0da1d7..b14440d9fc6 100644 --- a/sql/field.h +++ b/sql/field.h @@ -472,7 +472,7 @@ struct ha_field_option_struct; struct st_cache_field; int field_conv(Field *to,Field *from); -int truncate_double(double *nr, uint field_length, uint dec, +int truncate_double(double *nr, uint field_length, decimal_digits_t dec, bool unsigned_flag, double max_value); inline uint get_enum_pack_length(int elements) @@ -1327,7 +1327,7 @@ public: { memcpy(ptr, val, len); } - virtual uint decimals() const { return 0; } + virtual decimal_digits_t decimals() const { return 0; } virtual Information_schema_numeric_attributes information_schema_numeric_attributes() const { @@ -2015,12 +2015,12 @@ protected: protocol_send_type_t send_type); public: - const uint8 dec; + const decimal_digits_t dec; bool zerofill,unsigned_flag; // Purify cannot handle bit fields Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg, uchar null_bit_arg, utype unireg_check_arg, const LEX_CSTRING *field_name_arg, - uint8 dec_arg, bool zero_arg, bool unsigned_arg); + decimal_digits_t dec_arg, bool zero_arg, bool unsigned_arg); CHARSET_INFO *charset() const override { return DTCollation_numeric::singleton().collation; @@ -2040,7 +2040,7 @@ public: void add_zerofill_and_unsigned(String &res) const; friend class Create_field; void make_send_field(Send_field *) override; - uint decimals() const override { return (uint) dec; } + decimal_digits_t decimals() const override { return dec; } uint size_of() const override { return sizeof(*this); } bool eq_def(const Field *field) const override; Copy_func *get_copy_func(const Field *from) const override @@ -2090,7 +2090,8 @@ public: uchar null_bit_arg, utype unireg_check_arg, const LEX_CSTRING *field_name_arg, const DTCollation &collation); - uint decimals() const override { return is_created_from_null_item ? 0 : NOT_FIXED_DEC; } + decimal_digits_t decimals() const override + { return is_created_from_null_item ? 0 : NOT_FIXED_DEC; } int save_in_field(Field *to) override { return save_in_field_str(to); } bool memcpy_field_possible(const Field *from) const override { @@ -2238,7 +2239,7 @@ public: Field_real(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg, uchar null_bit_arg, utype unireg_check_arg, const LEX_CSTRING *field_name_arg, - uint8 dec_arg, bool zero_arg, bool unsigned_arg) + decimal_digits_t dec_arg, bool zero_arg, bool unsigned_arg) :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg), not_fixed(dec_arg >= FLOATING_POINT_DECIMALS) @@ -2290,7 +2291,7 @@ public: Field_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg, uchar null_bit_arg, enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg, - uint8 dec_arg,bool zero_arg,bool unsigned_arg) + decimal_digits_t dec_arg, bool zero_arg,bool unsigned_arg) :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg) @@ -2334,7 +2335,7 @@ public: class Field_new_decimal final :public Field_num { public: /* The maximum number of decimal digits can be stored */ - uint precision; + decimal_digits_t precision; uint bin_size; /* Constructors take max_length of the field as a parameter - not the @@ -2346,7 +2347,7 @@ public: uchar null_bit_arg, enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg, - uint8 dec_arg, bool zero_arg, bool unsigned_arg); + decimal_digits_t dec_arg, bool zero_arg, bool unsigned_arg); const Type_handler *type_handler() const override { return &type_handler_newdecimal; } enum ha_base_keytype key_type() const override { return HA_KEYTYPE_BINARY; } @@ -2828,7 +2829,7 @@ public: Field_float(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg, uchar null_bit_arg, enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg, - uint8 dec_arg,bool zero_arg,bool unsigned_arg) + decimal_digits_t dec_arg,bool zero_arg,bool unsigned_arg) :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg) @@ -2837,7 +2838,7 @@ public: dec_arg= NOT_FIXED_DEC; } Field_float(uint32 len_arg, bool maybe_null_arg, - const LEX_CSTRING *field_name_arg, uint8 dec_arg) + const LEX_CSTRING *field_name_arg, decimal_digits_t dec_arg) :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, (uint) 0, NONE, field_name_arg, dec_arg, 0, 0) { @@ -2876,7 +2877,7 @@ public: Field_double(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg, uchar null_bit_arg, enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg, - uint8 dec_arg,bool zero_arg,bool unsigned_arg) + decimal_digits_t dec_arg,bool zero_arg,bool unsigned_arg) :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg) @@ -2885,7 +2886,7 @@ public: dec_arg= NOT_FIXED_DEC; } Field_double(uint32 len_arg, bool maybe_null_arg, - const LEX_CSTRING *field_name_arg, uint8 dec_arg) + const LEX_CSTRING *field_name_arg, decimal_digits_t dec_arg) :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0, NONE, field_name_arg, dec_arg, 0, 0) { @@ -2894,7 +2895,7 @@ public: } Field_double(uint32 len_arg, bool maybe_null_arg, const LEX_CSTRING *field_name_arg, - uint8 dec_arg, bool not_fixed_arg) + decimal_digits_t dec_arg, bool not_fixed_arg) :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0, NONE, field_name_arg, dec_arg, 0, 0) { @@ -3249,21 +3250,22 @@ public: */ class Field_timestamp_with_dec :public Field_timestamp { protected: - uint dec; + decimal_digits_t dec; public: Field_timestamp_with_dec(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg, enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg, - TABLE_SHARE *share, uint dec_arg) : + TABLE_SHARE *share, decimal_digits_t dec_arg) : Field_timestamp(ptr_arg, - MAX_DATETIME_WIDTH + dec_arg + MY_TEST(dec_arg), null_ptr_arg, + MAX_DATETIME_WIDTH + dec_arg + MY_TEST(dec_arg), + null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg, share), dec(dec_arg) { DBUG_ASSERT(dec <= TIME_SECOND_PART_DIGITS); } - uint decimals() const override { return dec; } + decimal_digits_t decimals() const override { return dec; } enum ha_base_keytype key_type() const override { return HA_KEYTYPE_BINARY; } uchar *pack(uchar *to, const uchar *from, uint max_length) override { return Field::pack(to, from, max_length); } @@ -3294,7 +3296,7 @@ public: uchar *null_ptr_arg, uchar null_bit_arg, enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg, - TABLE_SHARE *share, uint dec_arg) : + TABLE_SHARE *share, decimal_digits_t dec_arg) : Field_timestamp_with_dec(ptr_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg, share, dec_arg) { @@ -3323,7 +3325,7 @@ public: uchar *null_ptr_arg, uchar null_bit_arg, enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg, - TABLE_SHARE *share, uint dec_arg) : + TABLE_SHARE *share, decimal_digits_t dec_arg) : Field_timestamp_with_dec(ptr_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg, share, dec_arg) {} @@ -3619,19 +3621,19 @@ public: */ class Field_time_with_dec :public Field_time { protected: - uint dec; + decimal_digits_t dec; public: Field_time_with_dec(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg, enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg, - uint dec_arg) + decimal_digits_t dec_arg) :Field_time(ptr_arg, MIN_TIME_WIDTH + dec_arg + MY_TEST(dec_arg), null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg), dec(dec_arg) { DBUG_ASSERT(dec <= TIME_SECOND_PART_DIGITS); } - uint decimals() const override { return dec; } + decimal_digits_t decimals() const override { return dec; } enum ha_base_keytype key_type() const override { return HA_KEYTYPE_BINARY; } longlong val_int() override; double val_real() override; @@ -3647,8 +3649,9 @@ class Field_time_hires final :public Field_time_with_dec { void store_TIME(const MYSQL_TIME *) override; public: Field_time_hires(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg, - enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg, - uint dec_arg) + enum utype unireg_check_arg, + const LEX_CSTRING *field_name_arg, + decimal_digits_t dec_arg) :Field_time_with_dec(ptr_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg, dec_arg) @@ -3679,8 +3682,8 @@ class Field_timef final :public Field_time_with_dec { void store_TIME(const MYSQL_TIME *ltime) override; public: Field_timef(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg, - enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg, - uint dec_arg) + enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg, + decimal_digits_t dec_arg) :Field_time_with_dec(ptr_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg, dec_arg) @@ -3818,18 +3821,18 @@ public: */ class Field_datetime_with_dec :public Field_datetime { protected: - uint dec; + decimal_digits_t dec; public: Field_datetime_with_dec(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg, enum utype unireg_check_arg, - const LEX_CSTRING *field_name_arg, uint dec_arg) + const LEX_CSTRING *field_name_arg, decimal_digits_t dec_arg) :Field_datetime(ptr_arg, MAX_DATETIME_WIDTH + dec_arg + MY_TEST(dec_arg), null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg), dec(dec_arg) { DBUG_ASSERT(dec <= TIME_SECOND_PART_DIGITS); } - uint decimals() const override final { return dec; } + decimal_digits_t decimals() const override final { return dec; } enum ha_base_keytype key_type() const override final { return HA_KEYTYPE_BINARY; } void make_send_field(Send_field *field) override final; bool send(Protocol *protocol) override final; @@ -3859,7 +3862,7 @@ class Field_datetime_hires final :public Field_datetime_with_dec { public: Field_datetime_hires(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg, enum utype unireg_check_arg, - const LEX_CSTRING *field_name_arg, uint dec_arg) + const LEX_CSTRING *field_name_arg, decimal_digits_t dec_arg) :Field_datetime_with_dec(ptr_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg, dec_arg) { @@ -3890,7 +3893,7 @@ class Field_datetimef final :public Field_datetime_with_dec { public: Field_datetimef(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg, enum utype unireg_check_arg, - const LEX_CSTRING *field_name_arg, uint dec_arg) + const LEX_CSTRING *field_name_arg, decimal_digits_t dec_arg) :Field_datetime_with_dec(ptr_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg, dec_arg) {} @@ -3934,7 +3937,7 @@ static inline Field_timestamp * new_Field_timestamp(MEM_ROOT *root,uchar *ptr, uchar *null_ptr, uchar null_bit, enum Field::utype unireg_check, const LEX_CSTRING *field_name, - TABLE_SHARE *share, uint dec) + TABLE_SHARE *share, decimal_digits_t dec) { if (dec==0) return new (root) @@ -3950,7 +3953,7 @@ new_Field_timestamp(MEM_ROOT *root,uchar *ptr, uchar *null_ptr, uchar null_bit, static inline Field_time * new_Field_time(MEM_ROOT *root, uchar *ptr, uchar *null_ptr, uchar null_bit, enum Field::utype unireg_check, const LEX_CSTRING *field_name, - uint dec) + decimal_digits_t dec) { if (dec == 0) return new (root) @@ -3965,7 +3968,7 @@ new_Field_time(MEM_ROOT *root, uchar *ptr, uchar *null_ptr, uchar null_bit, static inline Field_datetime * new_Field_datetime(MEM_ROOT *root, uchar *ptr, uchar *null_ptr, uchar null_bit, enum Field::utype unireg_check, - const LEX_CSTRING *field_name, uint dec) + const LEX_CSTRING *field_name, decimal_digits_t dec) { if (dec == 0) return new (root) @@ -4768,7 +4771,7 @@ public: bool has_charset() const override { return true; } /* enum and set are sorted as integers */ CHARSET_INFO *sort_charset() const override { return &my_charset_bin; } - uint decimals() const override { return 0; } + decimal_digits_t decimals() const override { return 0; } const TYPELIB *get_typelib() const override { return typelib; } uchar *pack(uchar *to, const uchar *from, uint max_length) override; @@ -5104,7 +5107,7 @@ public: max number of characters. */ ulonglong length; - uint decimals; + decimal_digits_t decimals; Field::utype unireg_check; const TYPELIB *interval; // Which interval to use CHARSET_INFO *charset; @@ -5661,7 +5664,8 @@ public: LEX_CSTRING table_name, org_table_name; LEX_CSTRING col_name, org_col_name; ulong length; - uint flags, decimals; + uint flags; + decimal_digits_t decimals; Send_field(Field *field) { field->make_send_field(this); diff --git a/sql/item.cc b/sql/item.cc index 1833fa4f357..e14c8029573 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -7029,7 +7029,7 @@ void Item_hex_hybrid::print(String *str, enum_query_type query_type) } -uint Item_hex_hybrid::decimal_precision() const +decimal_digits_t Item_hex_hybrid::decimal_precision() const { switch (max_length) {// HEX DEC case 0: // ---- --- diff --git a/sql/item.h b/sql/item.h index 061cc9f2e90..22b6aa6656f 100644 --- a/sql/item.h +++ b/sql/item.h @@ -1655,19 +1655,19 @@ public: inline uint float_length(uint decimals_par) const { return decimals < FLOATING_POINT_DECIMALS ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;} /* Returns total number of decimal digits */ - virtual uint decimal_precision() const + decimal_digits_t decimal_precision() const override { return type_handler()->Item_decimal_precision(this); } /* Returns the number of integer part digits only */ - inline int decimal_int_part() const - { return my_decimal_int_part(decimal_precision(), decimals); } + inline decimal_digits_t decimal_int_part() const + { return (decimal_digits_t) my_decimal_int_part(decimal_precision(), decimals); } /* Returns the number of fractional digits only. NOT_FIXED_DEC is replaced to the maximum possible number of fractional digits, taking into account the data type. */ - uint decimal_scale() const + decimal_digits_t decimal_scale() const { return type_handler()->Item_decimal_scale(this); } @@ -4267,8 +4267,8 @@ public: Item *clone_item(THD *thd) override; void print(String *str, enum_query_type query_type) override; Item *neg(THD *thd) override; - uint decimal_precision() const override - { return (uint) (max_length - MY_TEST(value < 0)); } + decimal_digits_t decimal_precision() const override + { return (decimal_digits_t) (max_length - MY_TEST(value < 0)); } Item *get_copy(THD *thd) override { return get_item_copy(thd, this); } }; @@ -4324,7 +4324,8 @@ public: double val_real() { return ulonglong2double((ulonglong)value); } Item *clone_item(THD *thd); Item *neg(THD *thd); - uint decimal_precision() const { return max_length; } + decimal_digits_t decimal_precision() const override + { return decimal_digits_t(max_length); } Item *get_copy(THD *thd) { return get_item_copy(thd, this); } }; @@ -4380,7 +4381,8 @@ public: str->append(str_value); } Item *neg(THD *thd) override; - uint decimal_precision() const override { return decimal_value.precision(); } + decimal_digits_t decimal_precision() const override + { return decimal_value.precision(); } void set_decimal_value(my_decimal *value_par); Item *get_copy(THD *thd) override { return get_item_copy(thd, this); } @@ -4779,7 +4781,7 @@ public: Item_hex_constant(thd, str, str_length) {} const Type_handler *type_handler() const override { return &type_handler_hex_hybrid; } - uint decimal_precision() const override; + decimal_digits_t decimal_precision() const override; double val_real() override { return (double) (ulonglong) Item_hex_hybrid::val_int(); @@ -4909,7 +4911,7 @@ public: collation= DTCollation_numeric(); decimals= 0; } - Item_temporal_literal(THD *thd, uint dec_arg): + Item_temporal_literal(THD *thd, decimal_digits_t dec_arg): Item_literal(thd) { collation= DTCollation_numeric(); @@ -4997,7 +4999,7 @@ class Item_time_literal: public Item_temporal_literal protected: Time cached_time; public: - Item_time_literal(THD *thd, const Time *ltime, uint dec_arg): + Item_time_literal(THD *thd, const Time *ltime, decimal_digits_t dec_arg): Item_temporal_literal(thd, dec_arg), cached_time(*ltime) { @@ -5045,7 +5047,8 @@ protected: (null_value= cached_time.check_date_with_warn(current_thd)); } public: - Item_datetime_literal(THD *thd, const Datetime *ltime, uint dec_arg): + Item_datetime_literal(THD *thd, const Datetime *ltime, + decimal_digits_t dec_arg): Item_temporal_literal(thd, dec_arg), cached_time(*ltime) { @@ -5138,7 +5141,8 @@ class Item_datetime_literal_for_invalid_dates: public Item_datetime_literal { public: Item_datetime_literal_for_invalid_dates(THD *thd, - const Datetime *ltime, uint dec_arg) + const Datetime *ltime, + decimal_digits_t dec_arg) :Item_datetime_literal(thd, ltime, dec_arg) { maybe_null= false; diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h index aa7269ab95a..d8f884145e9 100644 --- a/sql/item_cmpfunc.h +++ b/sql/item_cmpfunc.h @@ -225,7 +225,7 @@ public: const Type_handler *fixed_type_handler() const { return &type_handler_bool; } CHARSET_INFO *compare_collation() const { return NULL; } bool fix_length_and_dec() { decimals=0; max_length=1; return FALSE; } - uint decimal_precision() const { return 1; } + decimal_digits_t decimal_precision() const override { return 1; } bool need_parentheses_in_default() { return true; } }; @@ -968,7 +968,7 @@ public: Item_func_strcmp(THD *thd, Item *a, Item *b): Item_long_func(thd, a, b) {} longlong val_int(); - uint decimal_precision() const { return 1; } + decimal_digits_t decimal_precision() const override { return 1; } const char *func_name() const { return "strcmp"; } bool fix_length_and_dec() { @@ -1006,7 +1006,7 @@ public: longlong val_int(); bool fix_length_and_dec(); const char *func_name() const { return "interval"; } - uint decimal_precision() const { return 2; } + decimal_digits_t decimal_precision() const override { return 2; } void print(String *str, enum_query_type query_type) { str->append(func_name()); diff --git a/sql/item_func.cc b/sql/item_func.cc index f308435eea7..0a6070822f9 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -2739,7 +2739,7 @@ my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value) dec= INT_MIN; if (!(null_value= (value.is_null() || args[1]->null_value || - value.round_to(decimal_value, (uint) dec, + value.round_to(decimal_value, (int) dec, truncate ? TRUNCATE : HALF_UP) > 1))) return decimal_value; return 0; diff --git a/sql/item_func.h b/sql/item_func.h index e774d9c53bd..1e4788b1ff2 100644 --- a/sql/item_func.h +++ b/sql/item_func.h @@ -1341,7 +1341,8 @@ public: return args[0]->type_handler()->Item_func_signed_fix_length_and_dec(this); } virtual void print(String *str, enum_query_type query_type); - uint decimal_precision() const { return args[0]->decimal_precision(); } + decimal_digits_t decimal_precision() const override + { return args[0]->decimal_precision(); } bool need_parentheses_in_default() { return true; } Item *get_copy(THD *thd) { return get_item_copy(thd, this); } @@ -1372,7 +1373,7 @@ public: { return args[0]->type_handler()->Item_func_unsigned_fix_length_and_dec(this); } - uint decimal_precision() const { return max_length; } + decimal_digits_t decimal_precision() const override { return max_length; } virtual void print(String *str, enum_query_type query_type); Item *get_copy(THD *thd) { return get_item_copy(thd, this); } @@ -1383,10 +1384,10 @@ class Item_decimal_typecast :public Item_func { my_decimal decimal_value; public: - Item_decimal_typecast(THD *thd, Item *a, uint len, uint dec) + Item_decimal_typecast(THD *thd, Item *a, uint len, decimal_digits_t dec) :Item_func(thd, a) { - decimals= (uint8) dec; + decimals= dec; collation= DTCollation_numeric(); fix_char_length(my_decimal_precision_to_length_no_truncation(len, dec, unsigned_flag)); @@ -1656,7 +1657,8 @@ public: void fix_length_and_dec_double(); void fix_length_and_dec_decimal(); bool fix_length_and_dec(); - uint decimal_precision() const { return args[0]->decimal_precision(); } + decimal_digits_t decimal_precision() const override + { return args[0]->decimal_precision(); } bool need_parentheses_in_default() { return true; } Item *get_copy(THD *thd) { return get_item_copy(thd, this); } @@ -1990,7 +1992,7 @@ class Item_func_sign :public Item_long_func public: Item_func_sign(THD *thd, Item *a): Item_long_func(thd, a) {} const char *func_name() const { return "sign"; } - uint decimal_precision() const { return 1; } + decimal_digits_t decimal_precision() const override { return 1; } bool fix_length_and_dec() { fix_char_length(2); return FALSE; } longlong val_int(); Item *get_copy(THD *thd) diff --git a/sql/item_geofunc.h b/sql/item_geofunc.h index 0ccb5edc9bb..f50bb6316d7 100644 --- a/sql/item_geofunc.h +++ b/sql/item_geofunc.h @@ -874,7 +874,7 @@ public: longlong val_int(); const char *func_name() const { return "st_issimple"; } bool fix_length_and_dec() { decimals=0; max_length=2; return FALSE; } - uint decimal_precision() const { return 1; } + decimal_digits_t decimal_precision() const override { return 1; } Item *get_copy(THD *thd) { return get_item_copy(thd, this); } }; @@ -887,7 +887,7 @@ public: longlong val_int(); const char *func_name() const { return "st_isclosed"; } bool fix_length_and_dec() { decimals=0; max_length=2; return FALSE; } - uint decimal_precision() const { return 1; } + decimal_digits_t decimal_precision() const override { return 1; } Item *get_copy(THD *thd) { return get_item_copy(thd, this); } }; diff --git a/sql/my_decimal.cc b/sql/my_decimal.cc index edf3e82de40..ac86ff71b64 100644 --- a/sql/my_decimal.cc +++ b/sql/my_decimal.cc @@ -198,7 +198,8 @@ str_set_decimal(uint mask, const my_decimal *val, E_DEC_OVERFLOW */ -int my_decimal::to_binary(uchar *bin, int prec, int scale, uint mask) const +int my_decimal::to_binary(uchar *bin, int prec, decimal_digits_t scale, + uint mask) const { int err1= E_DEC_OK, err2; my_decimal rounded; @@ -329,7 +330,7 @@ my_decimal *date2my_decimal(const MYSQL_TIME *ltime, my_decimal *dec) } -void my_decimal_trim(ulonglong *precision, uint *scale) +void my_decimal_trim(ulonglong *precision, decimal_digits_t *scale) { if (!(*precision) && !(*scale)) { diff --git a/sql/my_decimal.h b/sql/my_decimal.h index 4c1f41463d5..a0e3be2fbd9 100644 --- a/sql/my_decimal.h +++ b/sql/my_decimal.h @@ -50,7 +50,8 @@ typedef struct st_mysql_time MYSQL_TIME; #define DECIMAL_MAX_FIELD_SIZE DECIMAL_MAX_PRECISION -inline uint my_decimal_size(uint precision, uint scale) +inline uint my_decimal_size(decimal_digits_t precision, + decimal_digits_t scale) { /* Always allocate more space to allow library to put decimal point @@ -60,9 +61,12 @@ inline uint my_decimal_size(uint precision, uint scale) } -inline int my_decimal_int_part(uint precision, uint decimals) +inline decimal_digits_t my_decimal_int_part(decimal_digits_t precision, + decimal_digits_t decimals) { - return precision - ((decimals == DECIMAL_NOT_SPECIFIED) ? 0 : decimals); + return (decimal_digits_t) (precision - + ((decimals == DECIMAL_NOT_SPECIFIED) ? 0 : + decimals)); } @@ -147,7 +151,7 @@ public: { init(); } - my_decimal(const uchar *bin, int prec, int scale) + my_decimal(const uchar *bin, decimal_digits_t prec, decimal_digits_t scale) { init(); check_result(E_DEC_FATAL_ERROR, bin2decimal(bin, this, prec, scale)); @@ -168,7 +172,7 @@ public: bool sign() const { return decimal_t::sign; } void sign(bool s) { decimal_t::sign= s; } - uint precision() const { return intg + frac; } + decimal_digits_t precision() const { return (decimal_digits_t) (intg + frac); } void set_zero() { /* @@ -217,17 +221,19 @@ public: { return to_string(to, 0, 0, 0); } - String *to_string_round(String *to, int scale, my_decimal *round_buff) const + String *to_string_round(String *to, decimal_digits_t scale, + my_decimal *round_buff) const { (void) round_to(round_buff, scale, HALF_UP); // QQ: check result? return round_buff->to_string(to); } + /* Scale can be negative here when called from truncate() */ int round_to(my_decimal *to, int scale, decimal_round_mode mode, int mask= E_DEC_FATAL_ERROR) const { return check_result(mask, decimal_round(this, to, scale, mode)); } - int to_binary(uchar *bin, int prec, int scale, + int to_binary(uchar *bin, int prec, decimal_digits_t scale, uint mask= E_DEC_FATAL_ERROR) const; #endif /** Swap two my_decimal values */ @@ -253,7 +259,8 @@ bool str_set_decimal(uint mask, const my_decimal *val, uint fixed_prec, extern my_decimal decimal_zero; inline -void max_my_decimal(my_decimal *to, int precision, int frac) +void max_my_decimal(my_decimal *to, decimal_digits_t precision, + decimal_digits_t frac) { DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION)&& (frac <= DECIMAL_MAX_SCALE)); @@ -277,30 +284,34 @@ inline int check_result_and_overflow(uint mask, int result, my_decimal *val) return result; } -inline uint my_decimal_length_to_precision(uint length, uint scale, - bool unsigned_flag) +inline decimal_digits_t my_decimal_length_to_precision(decimal_digits_t length, + decimal_digits_t scale, + bool unsigned_flag) { /* Precision can't be negative thus ignore unsigned_flag when length is 0. */ DBUG_ASSERT(length || !scale); - return (uint) (length - (scale>0 ? 1:0) - - (unsigned_flag || !length ? 0:1)); + return (decimal_digits_t) (length - (scale>0 ? 1:0) - + (unsigned_flag || !length ? 0:1)); } -inline uint32 my_decimal_precision_to_length_no_truncation(uint precision, - uint8 scale, - bool unsigned_flag) +inline decimal_digits_t +my_decimal_precision_to_length_no_truncation(decimal_digits_t precision, + decimal_digits_t scale, + bool unsigned_flag) { /* When precision is 0 it means that original length was also 0. Thus unsigned_flag is ignored in this case. */ DBUG_ASSERT(precision || !scale); - return (uint32)(precision + (scale > 0 ? 1 : 0) + - (unsigned_flag || !precision ? 0 : 1)); + return (decimal_digits_t)(precision + (scale > 0 ? 1 : 0) + + (unsigned_flag || !precision ? 0 : 1)); } -inline uint32 my_decimal_precision_to_length(uint precision, uint8 scale, - bool unsigned_flag) +inline decimal_digits_t +my_decimal_precision_to_length(decimal_digits_t precision, + decimal_digits_t scale, + bool unsigned_flag) { /* When precision is 0 it means that original length was also 0. Thus @@ -313,7 +324,7 @@ inline uint32 my_decimal_precision_to_length(uint precision, uint8 scale, } inline -int my_decimal_string_length(const my_decimal *d) +uint my_decimal_string_length(const my_decimal *d) { /* length of string representation including terminating '\0' */ return decimal_string_size(d); @@ -321,7 +332,7 @@ int my_decimal_string_length(const my_decimal *d) inline -int my_decimal_max_length(const my_decimal *d) +uint my_decimal_max_length(const my_decimal *d) { /* -1 because we do not count \0 */ return decimal_string_size(d) - 1; @@ -329,9 +340,10 @@ int my_decimal_max_length(const my_decimal *d) inline -int my_decimal_get_binary_size(uint precision, uint scale) +uint my_decimal_get_binary_size(decimal_digits_t precision, + decimal_digits_t scale) { - return decimal_bin_size((int)precision, (int)scale); + return decimal_bin_size(precision, scale); } @@ -343,8 +355,8 @@ void my_decimal2decimal(const my_decimal *from, my_decimal *to) inline -int binary2my_decimal(uint mask, const uchar *bin, my_decimal *d, int prec, - int scale) +int binary2my_decimal(uint mask, const uchar *bin, my_decimal *d, + decimal_digits_t prec, decimal_digits_t scale) { return check_result(mask, bin2decimal(bin, d, prec, scale)); } @@ -531,7 +543,7 @@ int my_decimal_intg(const my_decimal *a) } -void my_decimal_trim(ulonglong *precision, uint *scale); +void my_decimal_trim(ulonglong *precision, decimal_digits_t *scale); #endif /*my_decimal_h*/ diff --git a/sql/sql_class.h b/sql/sql_class.h index fc65bdcab3e..175ba36ae9f 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -7581,7 +7581,7 @@ public: void set_maybe_null(bool maybe_null_arg) { m_maybe_null= maybe_null_arg; } bool get_maybe_null() const { return m_maybe_null; } - uint decimal_precision() const + decimal_digits_t decimal_precision() const { /* Type_holder is not used directly to create fields, so diff --git a/sql/sql_i_s.h b/sql/sql_i_s.h index b9a768f1452..60a91205bbf 100644 --- a/sql/sql_i_s.h +++ b/sql/sql_i_s.h @@ -76,8 +76,10 @@ public: { } const Type_handler *type_handler() const { return m_type_handler; } uint char_length() const { return m_char_length; } - uint decimal_precision() const { return (m_char_length / 100) % 100; } - uint decimal_scale() const { return m_char_length % 10; } + decimal_digits_t decimal_precision() const + { return (decimal_digits_t) ((m_char_length / 100) % 100); } + decimal_digits_t decimal_scale() const + { return (decimal_digits_t) (m_char_length % 10); } uint fsp() const { DBUG_ASSERT(m_char_length <= TIME_SECOND_PART_DIGITS); diff --git a/sql/sql_type.cc b/sql/sql_type.cc index 6ee51b763e0..6a1ccbbbd22 100644 --- a/sql/sql_type.cc +++ b/sql/sql_type.cc @@ -1179,9 +1179,9 @@ Datetime_truncation_not_needed::Datetime_truncation_not_needed(THD *thd, Item *i /********************************************************************/ -uint Type_numeric_attributes::find_max_decimals(Item **item, uint nitems) +decimal_digits_t Type_numeric_attributes::find_max_decimals(Item **item, uint nitems) { - uint res= 0; + decimal_digits_t res= 0; for (uint i= 0; i < nitems; i++) set_if_bigger(res, item[i]->decimals); return res; @@ -1218,9 +1218,10 @@ uint32 Type_numeric_attributes::find_max_octet_length(Item **item, uint nitems) } -int Type_numeric_attributes::find_max_decimal_int_part(Item **item, uint nitems) +decimal_digits_t Type_numeric_attributes:: +find_max_decimal_int_part(Item **item, uint nitems) { - int max_int_part= 0; + decimal_digits_t max_int_part= 0; for (uint i=0 ; i < nitems ; i++) set_if_bigger(max_int_part, item[i]->decimal_int_part()); return max_int_part; @@ -1237,11 +1238,12 @@ Type_numeric_attributes::aggregate_numeric_attributes_decimal(Item **item, uint nitems, bool unsigned_arg) { - int max_int_part= find_max_decimal_int_part(item, nitems); + decimal_digits_t max_int_part= find_max_decimal_int_part(item, nitems); decimals= find_max_decimals(item, nitems); - int precision= MY_MIN(max_int_part + decimals, DECIMAL_MAX_PRECISION); + decimal_digits_t precision= (decimal_digits_t) + MY_MIN(max_int_part + decimals, DECIMAL_MAX_PRECISION); max_length= my_decimal_precision_to_length_no_truncation(precision, - (uint8) decimals, + decimals, unsigned_flag); } @@ -7030,20 +7032,20 @@ const Vers_type_handler* Type_handler_blob_common::vers() const /***************************************************************************/ -uint Type_handler::Item_time_precision(THD *thd, Item *item) const +decimal_digits_t Type_handler::Item_time_precision(THD *thd, Item *item) const { return MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS); } -uint Type_handler::Item_datetime_precision(THD *thd, Item *item) const +decimal_digits_t Type_handler::Item_datetime_precision(THD *thd, Item *item) const { return MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS); } -uint Type_handler_string_result::Item_temporal_precision(THD *thd, Item *item, - bool is_time) const +decimal_digits_t Type_handler_string_result:: +Item_temporal_precision(THD *thd, Item *item, bool is_time) const { StringBuffer<64> buf; String *tmp; @@ -7059,34 +7061,34 @@ uint Type_handler_string_result::Item_temporal_precision(THD *thd, Item *item, Datetime(thd, &status, tmp->ptr(), tmp->length(), tmp->charset(), Datetime::Options(TIME_FUZZY_DATES, TIME_FRAC_TRUNCATE)). is_valid_datetime())) - return MY_MIN(status.precision, TIME_SECOND_PART_DIGITS); - return MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS); + return (decimal_digits_t) MY_MIN(status.precision, TIME_SECOND_PART_DIGITS); + return (decimal_digits_t) MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS); } /***************************************************************************/ -uint Type_handler::Item_decimal_scale(const Item *item) const +decimal_digits_t Type_handler::Item_decimal_scale(const Item *item) const { - return item->decimals < NOT_FIXED_DEC ? - item->decimals : - MY_MIN(item->max_length, DECIMAL_MAX_SCALE); + return (item->decimals < NOT_FIXED_DEC ? + item->decimals : + (decimal_digits_t) MY_MIN(item->max_length, DECIMAL_MAX_SCALE)); } -uint Type_handler_temporal_result:: - Item_decimal_scale_with_seconds(const Item *item) const +decimal_digits_t Type_handler_temporal_result:: +Item_decimal_scale_with_seconds(const Item *item) const { - return item->decimals < NOT_FIXED_DEC ? - item->decimals : - TIME_SECOND_PART_DIGITS; + return (item->decimals < NOT_FIXED_DEC ? + item->decimals : + TIME_SECOND_PART_DIGITS); } -uint Type_handler::Item_divisor_precision_increment(const Item *item) const +decimal_digits_t Type_handler::Item_divisor_precision_increment(const Item *item) const { return item->decimals; } -uint Type_handler_temporal_result:: - Item_divisor_precision_increment_with_seconds(const Item *item) const +decimal_digits_t Type_handler_temporal_result:: +Item_divisor_precision_increment_with_seconds(const Item *item) const { return item->decimals < NOT_FIXED_DEC ? item->decimals : @@ -7095,7 +7097,7 @@ uint Type_handler_temporal_result:: /***************************************************************************/ -uint Type_handler_string_result::Item_decimal_precision(const Item *item) const +decimal_digits_t Type_handler_string_result::Item_decimal_precision(const Item *item) const { uint res= item->max_char_length(); /* @@ -7104,49 +7106,51 @@ uint Type_handler_string_result::Item_decimal_precision(const Item *item) const INT(0) or DECIMAL(0,0) when converting NULL or empty strings to INT/DECIMAL: CREATE TABLE t1 AS SELECT CONVERT(NULL,SIGNED) AS a; */ - return res ? MY_MIN(res, DECIMAL_MAX_PRECISION) : 1; + return res ? (decimal_digits_t) MY_MIN(res, DECIMAL_MAX_PRECISION) : (decimal_digits_t) 1; } -uint Type_handler_real_result::Item_decimal_precision(const Item *item) const +decimal_digits_t Type_handler_real_result::Item_decimal_precision(const Item *item) const { uint res= item->max_char_length(); - return res ? MY_MIN(res, DECIMAL_MAX_PRECISION) : 1; + return res ? (decimal_digits_t) MY_MIN(res, DECIMAL_MAX_PRECISION) : (decimal_digits_t) 1; } -uint Type_handler_decimal_result::Item_decimal_precision(const Item *item) const +decimal_digits_t Type_handler_decimal_result::Item_decimal_precision(const Item *item) const { uint prec= my_decimal_length_to_precision(item->max_char_length(), item->decimals, item->unsigned_flag); - return MY_MIN(prec, DECIMAL_MAX_PRECISION); + return (decimal_digits_t) MY_MIN(prec, DECIMAL_MAX_PRECISION); } -uint Type_handler_int_result::Item_decimal_precision(const Item *item) const +decimal_digits_t Type_handler_int_result::Item_decimal_precision(const Item *item) const { uint prec= my_decimal_length_to_precision(item->max_char_length(), item->decimals, item->unsigned_flag); - return MY_MIN(prec, DECIMAL_MAX_PRECISION); + return (decimal_digits_t) MY_MIN(prec, DECIMAL_MAX_PRECISION); } -uint Type_handler_time_common::Item_decimal_precision(const Item *item) const +decimal_digits_t Type_handler_time_common::Item_decimal_precision(const Item *item) const { - return 7 + MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS); + return (decimal_digits_t) (7 + MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS)); } -uint Type_handler_date_common::Item_decimal_precision(const Item *item) const +decimal_digits_t Type_handler_date_common::Item_decimal_precision(const Item *item) const { return 8; } -uint Type_handler_datetime_common::Item_decimal_precision(const Item *item) const +decimal_digits_t Type_handler_datetime_common:: +Item_decimal_precision(const Item *item) const { - return 14 + MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS); + return (decimal_digits_t) (14 + MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS)); } -uint Type_handler_timestamp_common::Item_decimal_precision(const Item *item) const +decimal_digits_t Type_handler_timestamp_common:: +Item_decimal_precision(const Item *item) const { - return 14 + MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS); + return (decimal_digits_t) (14 + MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS)); } /***************************************************************************/ @@ -7681,7 +7685,7 @@ static void wrong_precision_error(uint errcode, Item *a, */ bool get_length_and_scale(ulonglong length, ulonglong decimals, - uint *out_length, uint *out_decimals, + uint *out_length, decimal_digits_t *out_decimals, uint max_precision, uint max_scale, Item *a) { @@ -7696,7 +7700,7 @@ bool get_length_and_scale(ulonglong length, ulonglong decimals, return 1; } - *out_decimals= (uint) decimals; + *out_decimals= (decimal_digits_t) decimals; my_decimal_trim(&length, out_decimals); *out_length= (uint) length; @@ -7764,7 +7768,8 @@ Item *Type_handler_decimal_result:: create_typecast_item(THD *thd, Item *item, const Type_cast_attributes &attr) const { - uint len, dec; + uint len; + decimal_digits_t dec; if (get_length_and_scale(attr.length(), attr.decimals(), &len, &dec, DECIMAL_MAX_PRECISION, DECIMAL_MAX_SCALE, item)) return NULL; @@ -7776,7 +7781,8 @@ Item *Type_handler_double:: create_typecast_item(THD *thd, Item *item, const Type_cast_attributes &attr) const { - uint len, dec; + uint len; + decimal_digits_t dec; if (!attr.length_specified()) return new (thd->mem_root) Item_double_typecast(thd, item, DBL_DIG + 7, diff --git a/sql/sql_type.h b/sql/sql_type.h index f801c243171..ab48c1f7f32 100644 --- a/sql/sql_type.h +++ b/sql/sql_type.h @@ -397,7 +397,7 @@ public: { return m_ptr ? m_ptr->to_string(to, prec, dec, filler) : NULL; } - int to_binary(uchar *bin, int prec, int scale) const + int to_binary(uchar *bin, int prec, decimal_digits_t scale) const { return (m_ptr ? m_ptr : &decimal_zero)->to_binary(bin, prec, scale); } @@ -420,12 +420,13 @@ class Dec_ptr_and_buffer: public Dec_ptr protected: my_decimal m_buffer; public: + /* scale is int as it can be negative here */ int round_to(my_decimal *to, int scale, decimal_round_mode mode) { DBUG_ASSERT(m_ptr); return m_ptr->round_to(to, scale, mode); } - int round_self(uint scale, decimal_round_mode mode) + int round_self(decimal_digits_t scale, decimal_round_mode mode) { return round_to(&m_buffer, scale, mode); } @@ -437,7 +438,7 @@ public: m_ptr= &m_buffer; return res; } - String *to_string_round(String *to, uint dec) + String *to_string_round(String *to, decimal_digits_t dec) { /* decimal_round() allows from==to @@ -3048,28 +3049,27 @@ char_to_byte_length_safe(size_t char_length_arg, uint32 mbmaxlen_arg) return tmp > UINT_MAX32 ? (uint32) UINT_MAX32 : static_cast(tmp); } - class Type_numeric_attributes { public: static uint count_unsigned(Item **item, uint nitems); static uint32 find_max_char_length(Item **item, uint nitems); static uint32 find_max_octet_length(Item **item, uint nitems); - static int find_max_decimal_int_part(Item **item, uint nitems); - static uint find_max_decimals(Item **item, uint nitems); + static decimal_digits_t find_max_decimal_int_part(Item **item, uint nitems); + static decimal_digits_t find_max_decimals(Item **item, uint nitems); public: /* The maximum value length in characters multiplied by collation->mbmaxlen. Almost always it's the maximum value length in bytes. */ uint32 max_length; - uint decimals; + decimal_digits_t decimals; bool unsigned_flag; public: Type_numeric_attributes() :max_length(0), decimals(0), unsigned_flag(false) { } - Type_numeric_attributes(uint32 max_length_arg, uint decimals_arg, + Type_numeric_attributes(uint32 max_length_arg, decimal_digits_t decimals_arg, bool unsigned_flag_arg) :max_length(max_length_arg), decimals(decimals_arg), @@ -3086,9 +3086,10 @@ protected: class Type_temporal_attributes: public Type_numeric_attributes { public: - Type_temporal_attributes(uint int_part_length, uint dec, bool unsigned_arg) + Type_temporal_attributes(uint32 int_part_length, decimal_digits_t dec, bool unsigned_arg) :Type_numeric_attributes(int_part_length + (dec ? 1 : 0), - MY_MIN(dec, TIME_SECOND_PART_DIGITS), + MY_MIN(dec, + (decimal_digits_t) TIME_SECOND_PART_DIGITS), unsigned_arg) { max_length+= decimals; @@ -3099,7 +3100,7 @@ public: class Type_temporal_attributes_not_fixed_dec: public Type_numeric_attributes { public: - Type_temporal_attributes_not_fixed_dec(uint32 int_part_length, uint dec, + Type_temporal_attributes_not_fixed_dec(uint32 int_part_length, decimal_digits_t dec, bool unsigned_flag) :Type_numeric_attributes(int_part_length, dec, unsigned_flag) { @@ -3154,7 +3155,7 @@ public: max_length= char_to_byte_length_safe(max_char_length_arg, collation.collation->mbmaxlen); } - void fix_attributes_temporal(uint32 int_part_length, uint dec) + void fix_attributes_temporal(uint32 int_part_length, decimal_digits_t dec) { *this= Type_std_attributes( Type_temporal_attributes(int_part_length, dec, false), @@ -3164,11 +3165,11 @@ public: { fix_attributes_temporal(MAX_DATE_WIDTH, 0); } - void fix_attributes_time(uint dec) + void fix_attributes_time(decimal_digits_t dec) { fix_attributes_temporal(MIN_TIME_WIDTH, dec); } - void fix_attributes_datetime(uint dec) + void fix_attributes_datetime(decimal_digits_t dec) { fix_attributes_temporal(MAX_DATETIME_WIDTH, dec); } @@ -3307,7 +3308,7 @@ public: virtual ~Type_all_attributes() {} virtual void set_maybe_null(bool maybe_null_arg)= 0; // Returns total number of decimal digits - virtual uint decimal_precision() const= 0; + virtual decimal_digits_t decimal_precision() const= 0; virtual const TYPELIB *get_typelib() const= 0; virtual void set_typelib(const TYPELIB *typelib)= 0; }; @@ -3458,7 +3459,7 @@ class Information_schema_numeric_attributes ATTR_PRECISION_AND_SCALE= (ATTR_PRECISION|ATTR_SCALE) }; uint m_precision; - uint m_scale; + decimal_digits_t m_scale; enum_attr m_available_attributes; public: Information_schema_numeric_attributes() @@ -3469,7 +3470,7 @@ public: :m_precision(precision), m_scale(0), m_available_attributes(ATTR_PRECISION) { } - Information_schema_numeric_attributes(uint precision, uint scale) + Information_schema_numeric_attributes(uint precision, decimal_digits_t scale) :m_precision(precision), m_scale(scale), m_available_attributes(ATTR_PRECISION_AND_SCALE) { } @@ -3480,10 +3481,10 @@ public: DBUG_ASSERT(has_precision()); return (uint) m_precision; } - uint scale() const + decimal_digits_t scale() const { DBUG_ASSERT(has_scale()); - return (uint) m_scale; + return m_scale; } }; @@ -3830,15 +3831,15 @@ public: virtual bool can_return_extract_source(interval_type type) const; virtual bool is_bool_type() const { return false; } virtual bool is_general_purpose_string_type() const { return false; } - virtual uint Item_time_precision(THD *thd, Item *item) const; - virtual uint Item_datetime_precision(THD *thd, Item *item) const; - virtual uint Item_decimal_scale(const Item *item) const; - virtual uint Item_decimal_precision(const Item *item) const= 0; + virtual decimal_digits_t Item_time_precision(THD *thd, Item *item) const; + virtual decimal_digits_t Item_datetime_precision(THD *thd, Item *item) const; + virtual decimal_digits_t Item_decimal_scale(const Item *item) const; + virtual decimal_digits_t Item_decimal_precision(const Item *item) const= 0; /* Returns how many digits a divisor adds into a division result. See Item::divisor_precision_increment() in item.h for more comments. */ - virtual uint Item_divisor_precision_increment(const Item *) const; + virtual decimal_digits_t Item_divisor_precision_increment(const Item *) const; /** Makes a temporary table Field to handle numeric aggregate functions, e.g. SUM(DISTINCT expr), AVG(DISTINCT expr), etc. @@ -4480,7 +4481,7 @@ public: } bool Item_eq_value(THD *thd, const Type_cmp_attributes *attr, Item *a, Item *b) const override; - uint Item_decimal_precision(const Item *item) const override + decimal_digits_t Item_decimal_precision(const Item *item) const override { DBUG_ASSERT(0); return DECIMAL_MAX_PRECISION; @@ -4806,7 +4807,7 @@ public: bool binary_cmp) const override; bool Item_eq_value(THD *thd, const Type_cmp_attributes *attr, Item *a, Item *b) const override; - uint Item_decimal_precision(const Item *item) const override; + decimal_digits_t Item_decimal_precision(const Item *item) const override; bool Item_save_in_value(THD *thd, Item *item, st_value *value) const override; bool Item_param_set_from_value(THD *thd, Item_param *param, @@ -4935,7 +4936,7 @@ public: VDec va(a), vb(b); return va.ptr() && vb.ptr() && !va.cmp(vb); } - uint Item_decimal_precision(const Item *item) const override; + decimal_digits_t Item_decimal_precision(const Item *item) const override; bool Item_save_in_value(THD *thd, Item *item, st_value *value) const override; void Item_param_set_param_func(Item_param *param, uchar **pos, ulong len) const override; @@ -5177,7 +5178,7 @@ public: bool binary_cmp) const override; bool Item_eq_value(THD *thd, const Type_cmp_attributes *attr, Item *a, Item *b) const override; - uint Item_decimal_precision(const Item *item) const override; + decimal_digits_t Item_decimal_precision(const Item *item) const override; bool Item_save_in_value(THD *thd, Item *item, st_value *value) const override; bool Item_param_set_from_value(THD *thd, Item_param *param, @@ -5263,8 +5264,8 @@ public: class Type_handler_temporal_result: public Type_handler { protected: - uint Item_decimal_scale_with_seconds(const Item *item) const; - uint Item_divisor_precision_increment_with_seconds(const Item *) const; + decimal_digits_t Item_decimal_scale_with_seconds(const Item *item) const; + decimal_digits_t Item_divisor_precision_increment_with_seconds(const Item *) const; public: Item_result result_type() const override { return STRING_RESULT; } Item_result cmp_type() const override { return TIME_RESULT; } @@ -5351,7 +5352,7 @@ public: class Type_handler_string_result: public Type_handler { - uint Item_temporal_precision(THD *thd, Item *item, bool is_time) const; + decimal_digits_t Item_temporal_precision(THD *thd, Item *item, bool is_time) const; public: const Name &default_value() const override; protocol_send_type_t protocol_send_type() const override @@ -5411,15 +5412,15 @@ public: bool binary_cmp) const override; bool Item_eq_value(THD *thd, const Type_cmp_attributes *attr, Item *a, Item *b) const override; - uint Item_time_precision(THD *thd, Item *item) const override + decimal_digits_t Item_time_precision(THD *thd, Item *item) const override { return Item_temporal_precision(thd, item, true); } - uint Item_datetime_precision(THD *thd, Item *item) const override + decimal_digits_t Item_datetime_precision(THD *thd, Item *item) const override { return Item_temporal_precision(thd, item, false); } - uint Item_decimal_precision(const Item *item) const override; + decimal_digits_t Item_decimal_precision(const Item *item) const override; void Item_update_null_value(Item *item) const override; bool Item_save_in_value(THD *thd, Item *item, st_value *value) const override; void Item_param_setup_conversion(THD *thd, Item_param *) const override; @@ -6102,12 +6103,12 @@ public: const override; bool Item_eq_value(THD *thd, const Type_cmp_attributes *attr, Item *a, Item *b) const override; - uint Item_decimal_scale(const Item *item) const override + decimal_digits_t Item_decimal_scale(const Item *item) const override { return Item_decimal_scale_with_seconds(item); } - uint Item_decimal_precision(const Item *item) const override; - uint Item_divisor_precision_increment(const Item *item) const override + decimal_digits_t Item_decimal_precision(const Item *item) const override; + decimal_digits_t Item_divisor_precision_increment(const Item *item) const override { return Item_divisor_precision_increment_with_seconds(item); } @@ -6317,7 +6318,7 @@ public: void Column_definition_attributes_frm_pack(const Column_definition_attributes *at, uchar *buff) const override; - uint Item_decimal_precision(const Item *item) const override; + decimal_digits_t Item_decimal_precision(const Item *item) const override; String *print_item_value(THD *thd, Item *item, String *str) const override; Item_cache *Item_get_cache(THD *thd, const Item *item) const override; String *Item_func_min_max_val_str(Item_func_min_max *, String *) const override; @@ -6446,12 +6447,12 @@ public: const uchar *buffer, LEX_CUSTRING *gis_options) const override; - uint Item_decimal_scale(const Item *item) const override + decimal_digits_t Item_decimal_scale(const Item *item) const override { return Item_decimal_scale_with_seconds(item); } - uint Item_decimal_precision(const Item *item) const override; - uint Item_divisor_precision_increment(const Item *item) const override + decimal_digits_t Item_decimal_precision(const Item *item) const override; + decimal_digits_t Item_divisor_precision_increment(const Item *item) const override { return Item_divisor_precision_increment_with_seconds(item); } @@ -6608,12 +6609,12 @@ public: const Type_std_attributes *item, SORT_FIELD_ATTR *attr) const override; bool Column_definition_fix_attributes(Column_definition *c) const override; - uint Item_decimal_scale(const Item *item) const override + decimal_digits_t Item_decimal_scale(const Item *item) const override { return Item_decimal_scale_with_seconds(item); } - uint Item_decimal_precision(const Item *item) const override; - uint Item_divisor_precision_increment(const Item *item) const override + decimal_digits_t Item_decimal_precision(const Item *item) const override; + decimal_digits_t Item_divisor_precision_increment(const Item *item) const override { return Item_divisor_precision_increment_with_seconds(item); } diff --git a/strings/decimal.c b/strings/decimal.c index 1f9a28c1ad5..b5ac9f4f069 100644 --- a/strings/decimal.c +++ b/strings/decimal.c @@ -225,9 +225,10 @@ static inline int ROUND_UP(int x) to->buf and to->len must be set. */ -void max_decimal(int precision, int frac, decimal_t *to) +void max_decimal(decimal_digits_t precision, decimal_digits_t frac, + decimal_t *to) { - int intpart; + decimal_digits_t intpart; dec1 *buf= to->buf; DBUG_ASSERT(precision && precision >= frac); @@ -252,9 +253,10 @@ void max_decimal(int precision, int frac, decimal_t *to) } -static dec1 *remove_leading_zeroes(const decimal_t *from, int *intg_result) +static dec1 *remove_leading_zeroes(const decimal_t *from, + decimal_digits_t *intg_result) { - int intg= from->intg, i; + decimal_digits_t intg= from->intg, i; dec1 *buf0= from->buf; i= ((intg - 1) % DIG_PER_DEC1) + 1; while (intg > 0 && *buf0 == 0) @@ -283,9 +285,9 @@ static dec1 *remove_leading_zeroes(const decimal_t *from, int *intg_result) from number for processing */ -int decimal_actual_fraction(const decimal_t *from) +decimal_digits_t decimal_actual_fraction(const decimal_t *from) { - int frac= from->frac, i; + decimal_digits_t frac= from->frac, i; dec1 *buf0= from->buf + ROUND_UP(from->intg) + ROUND_UP(frac) - 1; if (frac == 0) @@ -331,11 +333,13 @@ int decimal_actual_fraction(const decimal_t *from) */ int decimal2string(const decimal_t *from, char *to, int *to_len, - int fixed_precision, int fixed_decimals, + decimal_digits_t fixed_precision, + decimal_digits_t fixed_decimals, char filler) { /* {intg_len, frac_len} output widths; {intg, frac} places in input */ - int len, intg, frac= from->frac, i, intg_len, frac_len, fill; + int len, frac= from->frac, i, intg_len, frac_len, fill, intg; + decimal_digits_t intg_tmp; /* number digits before decimal point */ int fixed_intg= (fixed_precision ? (fixed_precision - fixed_decimals) : 0); @@ -346,7 +350,8 @@ int decimal2string(const decimal_t *from, char *to, int *to_len, DBUG_ASSERT(*to_len >= 2+ (int) from->sign); /* removing leading zeroes */ - buf0= remove_leading_zeroes(from, &intg); + buf0= remove_leading_zeroes(from, &intg_tmp); + intg= (int) intg_tmp; /* intg can be negative later */ if (unlikely(intg+frac==0)) { intg=1; @@ -415,7 +420,10 @@ int decimal2string(const decimal_t *from, char *to, int *to_len, fill= intg_len - intg; if (intg == 0) + { + DBUG_ASSERT(fill > 0); fill--; /* symbol 0 before digital point */ + } for(; fill; fill--) *s++=filler; if (intg) @@ -1262,11 +1270,12 @@ int decimal2longlong(const decimal_t *from, longlong *to) 7E F2 04 C7 2D FB 2D */ -int decimal2bin(const decimal_t *from, uchar *to, int precision, int frac) +int decimal2bin(const decimal_t *from, uchar *to, decimal_digits_t precision, + decimal_digits_t frac) { dec1 mask=from->sign ? -1 : 0, *buf1=from->buf, *stop1; int error=E_DEC_OK, intg=precision-frac, - isize1, intg1, intg1x, from_intg, + isize1, intg1, intg1x, intg0=intg/DIG_PER_DEC1, frac0=frac/DIG_PER_DEC1, intg0x=intg-intg0*DIG_PER_DEC1, @@ -1276,6 +1285,7 @@ int decimal2bin(const decimal_t *from, uchar *to, int precision, int frac) isize0=intg0*sizeof(dec1)+dig2bytes[intg0x], fsize0=frac0*sizeof(dec1)+dig2bytes[frac0x], fsize1=frac1*sizeof(dec1)+dig2bytes[frac1x]; + decimal_digits_t from_intg; const int orig_isize0= isize0; const int orig_fsize0= fsize0; uchar *orig_to= to; @@ -1397,7 +1407,8 @@ int decimal2bin(const decimal_t *from, uchar *to, int precision, int frac) E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW */ -int bin2decimal(const uchar *from, decimal_t *to, int precision, int scale) +int bin2decimal(const uchar *from, decimal_t *to, decimal_digits_t precision, + decimal_digits_t scale) { int error=E_DEC_OK, intg=precision-scale, intg0=intg/DIG_PER_DEC1, frac0=scale/DIG_PER_DEC1, @@ -1516,9 +1527,9 @@ err: (multiply by sizeof(dec1) to get the size if bytes) */ -int decimal_size(int precision, int scale) +uint decimal_size(decimal_digits_t precision, decimal_digits_t scale) { - DBUG_ASSERT(scale >= 0 && precision > 0 && scale <= precision); + DBUG_ASSERT(precision > 0 && scale <= precision); return ROUND_UP(precision-scale)+ROUND_UP(scale); } @@ -1529,13 +1540,12 @@ int decimal_size(int precision, int scale) size in bytes */ -int decimal_bin_size(int precision, int scale) +uint decimal_bin_size(decimal_digits_t precision, decimal_digits_t scale) { int intg=precision-scale, intg0=intg/DIG_PER_DEC1, frac0=scale/DIG_PER_DEC1, intg0x=intg-intg0*DIG_PER_DEC1, frac0x=scale-frac0*DIG_PER_DEC1; - DBUG_ASSERT(scale >= 0); DBUG_ASSERT(precision > 0); DBUG_ASSERT(scale <= precision); return intg0*sizeof(dec1)+dig2bytes[intg0x]+ @@ -1782,7 +1792,7 @@ done: multiply by sizeof(dec1) */ -int decimal_result_size(decimal_t *from1, decimal_t *from2, char op, int param) +uint decimal_result_size(decimal_t *from1, decimal_t *from2, char op, int param) { switch (op) { case '-': @@ -1798,7 +1808,7 @@ int decimal_result_size(decimal_t *from1, decimal_t *from2, char op, int param) return ROUND_UP(from1->intg+from2->intg+1+from1->frac+from2->frac+param); default: DBUG_ASSERT(0); } - return -1; /* shut up the warning */ + return 0; /* shut up the warning */ } static int do_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to) @@ -2019,9 +2029,9 @@ static int do_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to) return error; } -int decimal_intg(const decimal_t *from) +decimal_digits_t decimal_intg(const decimal_t *from) { - int res; + decimal_digits_t res; remove_leading_zeroes(from, &res); return res; }