mysql表达式中decimal的设置,和length的设置

以add表达式为例:
void Item_func_additive_op::result_precision()
{
decimals= max(args[0]->decimals, args[1]->decimals);
int arg1_int= args[0]->decimal_precision() - args[0]->decimals;
int arg2_int= args[1]->decimal_precision() - args[1]->decimals;
int precision= max(arg1_int, arg2_int) + 1 + decimals;

/* Integer operations keep unsigned_flag if one of arguments is unsigned */
if (result_type() == INT_RESULT)
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
else
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
max_length= my_decimal_precision_to_length_no_truncation(precision, decimals,
unsigned_flag);
}

inline uint32 my_decimal_precision_to_length_no_truncation(uint precision,
uint8 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));
}

上述这段代码中将decimals设置为两个参数的最大的decimals,通过precision和decimal和unsigned_flag 设置max_length
void Item_func::fix_num_length_and_dec()
{
uint fl_length= 0;
decimals=0;
for (uint i=0 ; i < arg_count ; i++)
{
set_if_bigger(decimals,args[i]->decimals);
set_if_bigger(fl_length, args[i]->max_length);
}
max_length=float_length(decimals);
if (fl_length > max_length)
{
decimals= NOT_FIXED_DEC;
max_length= float_length(NOT_FIXED_DEC);
}
}
inline uint float_length(uint decimals_par) const
{ return decimals != NOT_FIXED_DEC ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;}
NOT_FIXED_DEC 31
上述这段代码中通过比较各个参数中max_length的最大值和通过decimal计算出来的最大值,重新设定decimals和max_length。

你可能感兴趣的:(mysql)