You've already forked mariadb-columnstore-engine
mirror of
https://github.com/mariadb-corporation/mariadb-columnstore-engine.git
synced 2025-07-29 08:21:15 +03:00
MCOL-4387 Convert dataconvert::decimalToString() into VDecimal and TSInt128 methods
This commit is contained in:
@ -1204,175 +1204,6 @@ bool stringToTimestampStruct(const string& data, TimeStamp& timeStamp, const str
|
||||
|
||||
}
|
||||
|
||||
size_t DataConvert::writeIntPart(int128_t* dec,
|
||||
char* p,
|
||||
const unsigned int buflen,
|
||||
const uint8_t scale)
|
||||
{
|
||||
int128_t intPart = *dec;
|
||||
int128_t high = 0, mid = 0, low = 0;
|
||||
uint64_t maxUint64divisor = 10000000000000000000ULL;
|
||||
|
||||
if (scale)
|
||||
{
|
||||
// Assuming scale = [0, 56]
|
||||
switch (scale / datatypes::maxPowOf10)
|
||||
{
|
||||
case 2: // scale = [38, 56]
|
||||
intPart /= datatypes::mcs_pow_10[datatypes::maxPowOf10];
|
||||
intPart /= datatypes::mcs_pow_10[datatypes::maxPowOf10];
|
||||
low = intPart;
|
||||
break;
|
||||
case 1: // scale = [19, 37]
|
||||
intPart /= datatypes::mcs_pow_10[datatypes::maxPowOf10];
|
||||
intPart /= datatypes::mcs_pow_10[scale % datatypes::maxPowOf10];
|
||||
low = intPart % maxUint64divisor;
|
||||
mid = intPart / maxUint64divisor;
|
||||
break;
|
||||
case 0: // scale = [0, 18]
|
||||
intPart /= datatypes::mcs_pow_10[scale % datatypes::maxPowOf10];
|
||||
low = intPart % maxUint64divisor;
|
||||
intPart /= maxUint64divisor;
|
||||
mid = intPart % maxUint64divisor;
|
||||
high = intPart / maxUint64divisor;
|
||||
break;
|
||||
default:
|
||||
throw QueryDataExcept("writeIntPart() bad scale", formatErr);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
low = intPart % maxUint64divisor;
|
||||
intPart /= maxUint64divisor;
|
||||
mid = intPart % maxUint64divisor;
|
||||
high = intPart / maxUint64divisor;
|
||||
}
|
||||
|
||||
// pod[0] is low 8 bytes, pod[1] is high 8 bytes
|
||||
uint64_t* high_pod = reinterpret_cast<uint64_t*>(&high);
|
||||
uint64_t* mid_pod = reinterpret_cast<uint64_t*>(&mid);
|
||||
uint64_t* low_pod = reinterpret_cast<uint64_t*>(&low);
|
||||
char* original_p = p;
|
||||
|
||||
// WIP replace sprintf with streams
|
||||
if (high_pod[0] != 0)
|
||||
{
|
||||
p += sprintf(p, "%lu", high_pod[0]);
|
||||
p += sprintf(p, "%019lu", mid_pod[0]);
|
||||
p += sprintf(p, "%019lu", low_pod[0]);
|
||||
}
|
||||
else if (mid_pod[0] != 0)
|
||||
{
|
||||
p += sprintf(p, "%lu", mid_pod[0]);
|
||||
p += sprintf(p, "%019lu", low_pod[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
p += sprintf(p, "%lu", low_pod[0]);
|
||||
}
|
||||
|
||||
size_t written = p - original_p;
|
||||
|
||||
if (buflen <= written)
|
||||
{
|
||||
throw QueryDataExcept("writeIntPart() char buffer overflow.", formatErr);
|
||||
}
|
||||
|
||||
return written;
|
||||
}
|
||||
|
||||
size_t DataConvert::writeFractionalPart(int128_t* dec,
|
||||
char* p,
|
||||
const unsigned int buflen,
|
||||
const uint8_t scale)
|
||||
{
|
||||
int128_t scaleDivisor = 1;
|
||||
|
||||
switch (scale / datatypes::maxPowOf10)
|
||||
{
|
||||
case 2:
|
||||
scaleDivisor *= datatypes::mcs_pow_10[datatypes::maxPowOf10];
|
||||
scaleDivisor *= datatypes::mcs_pow_10[datatypes::maxPowOf10];
|
||||
break;
|
||||
case 1:
|
||||
scaleDivisor *= datatypes::mcs_pow_10[datatypes::maxPowOf10];
|
||||
case 0:
|
||||
scaleDivisor *= datatypes::mcs_pow_10[scale % datatypes::maxPowOf10];
|
||||
}
|
||||
|
||||
int128_t fractionalPart = *dec % scaleDivisor;
|
||||
|
||||
// divide by the base until we have non-zero quotient
|
||||
size_t written = 0;
|
||||
scaleDivisor /= 10;
|
||||
|
||||
char* original_p = p;
|
||||
|
||||
while (scaleDivisor > 1 && fractionalPart / scaleDivisor == 0)
|
||||
{
|
||||
*p++ = '0';
|
||||
written++;
|
||||
scaleDivisor /= 10;
|
||||
}
|
||||
|
||||
p += writeIntPart(&fractionalPart, p, buflen - written, 0);
|
||||
|
||||
written = p - original_p;
|
||||
|
||||
// this should never be true
|
||||
if (written < scale)
|
||||
{
|
||||
for (size_t left = written; left < scale; left++)
|
||||
{
|
||||
*p++ = '0';
|
||||
}
|
||||
}
|
||||
|
||||
return written;
|
||||
}
|
||||
|
||||
void DataConvert::decimalToString(int128_t* dec,
|
||||
const uint8_t scale,
|
||||
char *p,
|
||||
const unsigned int buflen,
|
||||
cscDataType colDataType) // colDataType is redundant
|
||||
{
|
||||
char* original_p = p;
|
||||
size_t written = 0;
|
||||
// Raise exception on NULL and EMPTY value
|
||||
if (datatypes::Decimal::isWideDecimalNullValue(*dec) ||
|
||||
datatypes::Decimal::isWideDecimalEmptyValue(*dec))
|
||||
{
|
||||
throw QueryDataExcept("toString() char buffer overflow.", formatErr);
|
||||
}
|
||||
|
||||
if (*dec < static_cast<int128_t>(0))
|
||||
{
|
||||
*p++ = '-';
|
||||
*dec *= -1;
|
||||
}
|
||||
|
||||
written = writeIntPart(dec, p, buflen, scale);
|
||||
p += written;
|
||||
|
||||
if (scale)
|
||||
{
|
||||
*p++ = '.';
|
||||
written = p - original_p;
|
||||
p += writeFractionalPart(dec, p, buflen - written, scale);
|
||||
}
|
||||
|
||||
*p = '\0';
|
||||
|
||||
written = p - original_p;
|
||||
|
||||
if (buflen <= written)
|
||||
{
|
||||
throw QueryDataExcept("toString() char buffer overflow.", formatErr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
boost::any
|
||||
DataConvert::StringToBit(const datatypes::SystemCatalog::TypeAttributesStd& colType,
|
||||
const datatypes::ConvertFromStringParam &prm,
|
||||
|
Reference in New Issue
Block a user