Merge pull request #27246 from taosdata/fix/3.0/TD-31470

fix:[TD-31470] Fix replace function wrong length.
This commit is contained in:
dapan1121 2024-08-15 18:58:58 +08:00 committed by GitHub
commit de3fe2e1df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 4 deletions

View File

@ -2327,13 +2327,24 @@ static int32_t translateReplace(SFunctionNode* pFunc, char* pErrBuf, int32_t len
} }
} }
uint8_t type = getSDataTypeFromNode(nodesListGetNode(pFunc->pParameterList, 0))->type; uint8_t orgType = getSDataTypeFromNode(nodesListGetNode(pFunc->pParameterList, 0))->type;
uint8_t fromType = getSDataTypeFromNode(nodesListGetNode(pFunc->pParameterList, 1))->type;
uint8_t toType = getSDataTypeFromNode(nodesListGetNode(pFunc->pParameterList, 2))->type;
int32_t orgLen = getSDataTypeFromNode(nodesListGetNode(pFunc->pParameterList, 0))->bytes; int32_t orgLen = getSDataTypeFromNode(nodesListGetNode(pFunc->pParameterList, 0))->bytes;
int32_t fromLen = getSDataTypeFromNode(nodesListGetNode(pFunc->pParameterList, 1))->bytes; int32_t fromLen = getSDataTypeFromNode(nodesListGetNode(pFunc->pParameterList, 1))->bytes;
int32_t toLen = getSDataTypeFromNode(nodesListGetNode(pFunc->pParameterList, 2))->bytes; int32_t toLen = getSDataTypeFromNode(nodesListGetNode(pFunc->pParameterList, 2))->bytes;
int32_t resLen = orgLen + orgLen / fromLen * (toLen - fromLen); int32_t resLen;
pFunc->node.resType = (SDataType){.bytes = resLen, .type = type}; // Since we don't know the accurate length of result, estimate the maximum length here.
// To make the resLen bigger, we should make fromLen smaller and toLen bigger.
if (orgType == TSDB_DATA_TYPE_VARBINARY && fromType != orgType) {
fromLen = fromLen / TSDB_NCHAR_SIZE;
}
if (orgType == TSDB_DATA_TYPE_NCHAR && toType != orgType) {
toLen = toLen * TSDB_NCHAR_SIZE;
}
resLen = TMAX(orgLen, orgLen + orgLen / fromLen * (toLen - fromLen));
pFunc->node.resType = (SDataType){.bytes = resLen, .type = orgType};
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }

View File

@ -1458,7 +1458,21 @@ int32_t replaceFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pO
numOfRows = TMAX(numOfRows, pInput[i].numOfRows); numOfRows = TMAX(numOfRows, pInput[i].numOfRows);
} }
outputLen = pInputData[0]->info.bytes + pInputData[0]->info.bytes / pInputData[1]->info.bytes * (pInputData[2]->info.bytes - pInputData[1]->info.bytes); int8_t orgType = pInputData[0]->info.type;
int8_t fromType = pInputData[1]->info.type;
int8_t toType = pInputData[2]->info.type;
int32_t orgLength = pInputData[0]->info.bytes;
int32_t fromLength = pInputData[1]->info.bytes;
int32_t toLength = pInputData[2]->info.bytes;
if (orgType == TSDB_DATA_TYPE_VARBINARY && fromType != orgType) {
fromLength = fromLength / TSDB_NCHAR_SIZE;
}
if (orgType == TSDB_DATA_TYPE_NCHAR && toType != orgType) {
toLength = toLength * TSDB_NCHAR_SIZE;
}
outputLen = TMAX(orgLength, orgLength + orgLength / fromLength * (toLength - fromLength));
if (GET_PARAM_TYPE(&pInput[0]) == TSDB_DATA_TYPE_NULL || if (GET_PARAM_TYPE(&pInput[0]) == TSDB_DATA_TYPE_NULL ||
GET_PARAM_TYPE(&pInput[1]) == TSDB_DATA_TYPE_NULL || GET_PARAM_TYPE(&pInput[1]) == TSDB_DATA_TYPE_NULL ||
GET_PARAM_TYPE(&pInput[2]) == TSDB_DATA_TYPE_NULL || GET_PARAM_TYPE(&pInput[2]) == TSDB_DATA_TYPE_NULL ||