summaryrefslogtreecommitdiff
path: root/sql/my_decimal.h
diff options
context:
space:
mode:
authorMonty <monty@mariadb.org>2020-08-27 12:24:32 +0300
committerSergei Golubchik <serg@mariadb.org>2021-05-19 22:27:27 +0200
commitfa7d4abf164b37d0f98012ff7e906ebe7de8f1ed (patch)
treee351694a4b538ddf42f988d1d1df8f3ff1e3762d /sql/my_decimal.h
parentaee84453ab6befef296afdf8e66a8e295b624c72 (diff)
downloadmariadb-git-fa7d4abf164b37d0f98012ff7e906ebe7de8f1ed.tar.gz
Added typedef decimal_digits_t (uint16) for number of digits in most
aspects of decimals and integers For fields and Item's uint8 should be good enough. After discussions with Alexander Barkov we choose uint16 (for now) as some format functions may accept +256 digits. The reason for this patch was to make the usage and storage of decimal digits simlar. Before this patch decimals was stored/used as uint8, int and uint. The lengths for numbers where also using a lot of different types. Changed most decimal variables and functions to use the new typedef. squash! af7f09106b6c1dc20ae8c480bff6fd22d266b184 Use decimal_digits_t for all aspects of digits (total, precision and scale), both for decimals and integers.
Diffstat (limited to 'sql/my_decimal.h')
-rw-r--r--sql/my_decimal.h64
1 files changed, 38 insertions, 26 deletions
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*/