decimal insert tests

This commit is contained in:
wangjiaming0909 2025-02-20 11:28:35 +08:00
parent cd99b2c6d3
commit efe432cff7
5 changed files with 668 additions and 212 deletions

View File

@ -4783,7 +4783,6 @@ void valueSetDatum(SValue *pVal, int8_t type, void *pDatum, uint32_t len) {
void valueCloneDatum(SValue *pDst, const SValue *pSrc, int8_t type) {
if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
// we assume that pDst->pData not NULL
memcpy(pDst->pData, pSrc->pData, pSrc->nData);
pDst->nData = pSrc->nData;
} else {

View File

@ -170,6 +170,8 @@ int32_t calcActualWeight(int32_t prec, int32_t scale, int32_t exp, int32_t weigh
static int32_t decimalVarFromStr(const char* str, int32_t len, DecimalVar* result) {
int32_t code = 0, pos = 0;
int32_t expectPrecision = result->precision;
int32_t expectScale = result->scale;
result->precision = 0;
result->scale = 0;
result->exponent = 0;
@ -231,9 +233,14 @@ static int32_t decimalVarFromStr(const char* str, int32_t len, DecimalVar* resul
Decimal64 delta = {0};
if (curPrec > maxPrecision(result->type)) {
if (!afterPoint) return TSDB_CODE_DECIMAL_OVERFLOW;
if (rounded || curPrec - 1 != maxPrecision(result->type) || str[pos] < '5') break;
int32_t curScale = result->scale - result->exponent + places;
if (rounded || curScale > expectScale + 1 /*scale already overflowed, no need do rounding*/ ||
curPrec - 1 != maxPrecision(result->type) /* not the maxPrecision + 1 digit, no need do rounding*/ ||
str[pos] < '5')
break;
// do rounding
// Do rounding for the maxPrecision + 1 digit.
// Here we cannot directly add this digit into the results, because it may cause overflow.
DECIMAL64_SET_VALUE(&delta, 1);
scaleUp = places - 1;
rounded = true;
@ -1585,7 +1592,7 @@ static void decimal64RoundWithPositiveScale(Decimal64* pDec, uint8_t prec, int8_
int32_t decimal64FromStr(const char* str, int32_t len, uint8_t expectPrecision, uint8_t expectScale, Decimal64* pRes) {
int32_t code = 0;
DecimalVar var = {.type = DECIMAL_64, .pDec = pRes->words};
DecimalVar var = {.type = DECIMAL_64, .pDec = pRes->words, .precision = expectPrecision, .scale = expectScale};
DECIMAL64_SET_VALUE(pRes, 0);
code = decimalVarFromStr(str, len, &var);
if (TSDB_CODE_SUCCESS != code) return code;
@ -1626,7 +1633,7 @@ static void decimal128ScaleTo(Decimal128* pDec, uint8_t oldScale, uint8_t newSca
int32_t decimal128FromStr(const char* str, int32_t len, uint8_t expectPrecision, uint8_t expectScale,
Decimal128* pRes) {
int32_t code = 0;
DecimalVar var = {.type = DECIMAL_128, .pDec = pRes->words};
DecimalVar var = {.type = DECIMAL_128, .pDec = pRes->words, .precision = expectPrecision, .scale = expectScale};
DECIMAL128_SET_HIGH_WORD(pRes, 0);
DECIMAL128_SET_LOW_WORD(pRes, 0);
code = decimalVarFromStr(str, len, &var);

View File

@ -980,39 +980,41 @@ TEST(decimal, decimalFromStr_all) {
testDecimalFromStr(units);
std::vector<DecimalFromStrTestUnit<128>> dec128Units = {
{38, 10, "0e-10", "0", false},
{38, 10, "0e10", "0", false},
{38, 10, "e-100000", "0", false},
{38, 10, "e-10", "0", false},
{38, 10, "e10", "0", false},
{38, 10, "-1.23456789012300000000000000000099000009e20", "-123456789012300000000.0000000001", false},
{38, 10, "-1.234567890123e20", "-123456789012300000000.0000000000", false},
{20, 15, "1234567890.9999999999e-6", "1234.567891000000000", false},
{20, 15, "1234567890.9999999999999999999e-5", "12345.678910000000000", false},
{20, 15, "1234567890.99999999999e-5", "12345.678910000000000", false},
{20, 10, "12345667788.12312e-10", "1.2345667788", false},
{20, 20, "1.234567123123123e-20", "0.00000000000000000001", false},
{38, 38, "1.23456789121312312312312312355e-10", "0.00000000012345678912131231231231231236", false},
{38, 38, "1.23456e-10", "0.00000000012345600000000000000000000000", false},
{38, 10, "123456789012345678901234567.89012345679", "123456789012345678901234567.8901234568", false},
{38, 10, "123456789012345678901234567.89012345670", "123456789012345678901234567.8901234567", false},
{38, 10, "-123456789012345678901234567.89012345671", "-123456789012345678901234567.8901234567", false},
{38, 10, "-123456789012345678901234567.89012345679", "-123456789012345678901234567.8901234568", false},
{38, 10, "-9999999999999999999999999999.99999999995", "", true},
{38, 10, "-9999999999999999999999999999.99999999994", "-9999999999999999999999999999.9999999999", false},
{38, 10, "9999999999999999999999999999.99999999996", "", true},
{38, 10, "9999999999999999999999999999.99999999994", "9999999999999999999999999999.9999999999", false},
{36, 35, "9.99999999999999999999999999999999999", "9.99999999999999999999999999999999999", false},
{36, 35, "9.999999999999999999999999999999999999111231231", "", true},
{38, 38, "0.000000000000000000000000000000000000001", "0", false},
{38, 38, "0.000000000000000000000000000000000000006", "0.00000000000000000000000000000000000001", false},
{38, 35, "123.000000000000000000000000000000001", "123.00000000000000000000000000000000100", false},
{38, 5, "123.", "123.00000", false},
{20, 4, "-.12345", "-0.1235", false},
{20, 4, "-.", "0", false},
{30, 10, "1.2345e+20", "", true},
{38, 38, "1.23456e0", "", true},
{38, 38, "1.23456e-1", "0.12345600000000000000000000000000000000", false},
{38, 38, "1.23456789121312312312312312355e-10", "0.00000000012345678912131231231231231236", false},
{38, 10, "610854818164978322886511028.733672028246706062283745797933332437780013",
"610854818164978322886511028.7336720282", false},
{38, 10, "0e-10", "0", false},
{38, 10, "0e10", "0", false},
{38, 10, "e-100000", "0", false},
{38, 10, "e-10", "0", false},
{38, 10, "e10", "0", false},
{38, 10, "-1.23456789012300000000000000000099000009e20", "-123456789012300000000.0000000001", false},
{38, 10, "-1.234567890123e20", "-123456789012300000000.0000000000", false},
{20, 15, "1234567890.9999999999e-6", "1234.567891000000000", false},
{20, 15, "1234567890.9999999999999999999e-5", "12345.678910000000000", false},
{20, 15, "1234567890.99999999999e-5", "12345.678910000000000", false},
{20, 10, "12345667788.12312e-10", "1.2345667788", false},
{20, 20, "1.234567123123123e-20", "0.00000000000000000001", false},
{38, 38, "1.23456e-10", "0.00000000012345600000000000000000000000", false},
{38, 10, "123456789012345678901234567.89012345679", "123456789012345678901234567.8901234568", false},
{38, 10, "123456789012345678901234567.89012345670", "123456789012345678901234567.8901234567", false},
{38, 10, "-123456789012345678901234567.89012345671", "-123456789012345678901234567.8901234567", false},
{38, 10, "-123456789012345678901234567.89012345679", "-123456789012345678901234567.8901234568", false},
{38, 10, "-9999999999999999999999999999.99999999995", "", true},
{38, 10, "-9999999999999999999999999999.99999999994", "-9999999999999999999999999999.9999999999", false},
{38, 10, "9999999999999999999999999999.99999999996", "", true},
{38, 10, "9999999999999999999999999999.99999999994", "9999999999999999999999999999.9999999999", false},
{36, 35, "9.99999999999999999999999999999999999", "9.99999999999999999999999999999999999", false},
{36, 35, "9.999999999999999999999999999999999999111231231", "", true},
{38, 38, "0.000000000000000000000000000000000000001", "0", false},
{38, 38, "0.000000000000000000000000000000000000006", "0.00000000000000000000000000000000000001", false},
{38, 35, "123.000000000000000000000000000000001", "123.00000000000000000000000000000000100", false},
{38, 5, "123.", "123.00000", false},
{20, 4, "-.12345", "-0.1235", false},
{20, 4, "-.", "0", false},
{30, 10, "1.2345e+20", "", true},
{38, 38, "1.23456e0", "", true},
{38, 38, "1.23456e-1", "0.12345600000000000000000000000000000000", false},
};
testDecimalFromStr(dec128Units);
}

View File

@ -1862,7 +1862,7 @@ static void clearColValArray(SArray* pCols) {
int32_t num = taosArrayGetSize(pCols);
for (int32_t i = 0; i < num; ++i) {
SColVal* pCol = taosArrayGet(pCols, i);
if (IS_VAR_DATA_TYPE(pCol->value.type)) {
if (IS_VAR_DATA_TYPE(pCol->value.type) || pCol->value.type == TSDB_DATA_TYPE_DECIMAL) {
taosMemoryFreeClear(pCol->value.pData);
}
}

File diff suppressed because it is too large Load Diff