Merge pull request #16179 from taosdata/fix/TD-17499
fix(query): set proper ltrim/rtrim result schema bytes
This commit is contained in:
commit
ca56b8ee03
|
@ -192,6 +192,24 @@ static bool validateTimezoneFormat(const SValueNode* pVal) {
|
|||
return true;
|
||||
}
|
||||
|
||||
static int32_t countTrailingSpaces(const SValueNode* pVal, bool isLtrim) {
|
||||
int32_t numOfSpaces = 0;
|
||||
int32_t len = varDataLen(pVal->datum.p);
|
||||
char* str = varDataVal(pVal->datum.p);
|
||||
|
||||
int32_t startPos = isLtrim ? 0 : len - 1;
|
||||
int32_t step = isLtrim ? 1 : -1;
|
||||
for (int32_t i = startPos; i < len || i >= 0; i += step) {
|
||||
if (!isspace(str[i])) {
|
||||
break;
|
||||
}
|
||||
numOfSpaces++;
|
||||
}
|
||||
|
||||
return numOfSpaces;
|
||||
|
||||
}
|
||||
|
||||
void static addTimezoneParam(SNodeList* pList) {
|
||||
char buf[6] = {0};
|
||||
time_t t = taosTime(NULL);
|
||||
|
@ -293,6 +311,40 @@ static int32_t translateInOutStr(SFunctionNode* pFunc, char* pErrBuf, int32_t le
|
|||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
static int32_t translateTrimStr(SFunctionNode* pFunc, char* pErrBuf, int32_t len, bool isLtrim) {
|
||||
if (1 != LIST_LENGTH(pFunc->pParameterList)) {
|
||||
return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
|
||||
}
|
||||
|
||||
SExprNode* pPara1 = (SExprNode*)nodesListGetNode(pFunc->pParameterList, 0);
|
||||
if (!IS_VAR_DATA_TYPE(pPara1->resType.type)) {
|
||||
return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
|
||||
}
|
||||
|
||||
int32_t numOfSpaces = 0;
|
||||
SNode* pParamNode1 = nodesListGetNode(pFunc->pParameterList, 0);
|
||||
// for select trim functions with constant value from table,
|
||||
// need to set the proper result result schema bytes to avoid
|
||||
// trailing garbage characters
|
||||
if (nodeType(pParamNode1) == QUERY_NODE_VALUE) {
|
||||
SValueNode* pValue = (SValueNode*)pParamNode1;
|
||||
numOfSpaces = countTrailingSpaces(pValue, isLtrim);
|
||||
}
|
||||
|
||||
|
||||
int32_t resBytes = pPara1->resType.bytes - numOfSpaces;
|
||||
pFunc->node.resType = (SDataType){.bytes = resBytes, .type = pPara1->resType.type};
|
||||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
static int32_t translateLtrim(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
|
||||
return translateTrimStr(pFunc, pErrBuf, len, true);
|
||||
}
|
||||
|
||||
static int32_t translateRtrim(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
|
||||
return translateTrimStr(pFunc, pErrBuf, len, false);
|
||||
}
|
||||
|
||||
static int32_t translateLogarithm(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
|
||||
int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
|
||||
if (1 != numOfParams && 2 != numOfParams) {
|
||||
|
@ -2827,7 +2879,7 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
|
|||
.name = "ltrim",
|
||||
.type = FUNCTION_TYPE_LTRIM,
|
||||
.classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_STRING_FUNC,
|
||||
.translateFunc = translateInOutStr,
|
||||
.translateFunc = translateLtrim,
|
||||
.getEnvFunc = NULL,
|
||||
.initFunc = NULL,
|
||||
.sprocessFunc = ltrimFunction,
|
||||
|
@ -2837,7 +2889,7 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
|
|||
.name = "rtrim",
|
||||
.type = FUNCTION_TYPE_RTRIM,
|
||||
.classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_STRING_FUNC,
|
||||
.translateFunc = translateInOutStr,
|
||||
.translateFunc = translateRtrim,
|
||||
.getEnvFunc = NULL,
|
||||
.initFunc = NULL,
|
||||
.sprocessFunc = rtrimFunction,
|
||||
|
|
|
@ -758,7 +758,9 @@ EDealRes sclRewriteFunction(SNode** pNode, SScalarCtx *ctx) {
|
|||
res->datum.p = taosMemoryCalloc(len, 1);
|
||||
memcpy(res->datum.p, output.columnData->pData, len);
|
||||
} else if (IS_VAR_DATA_TYPE(type)) {
|
||||
res->datum.p = taosMemoryCalloc(res->node.resType.bytes + VARSTR_HEADER_SIZE + 1, 1);
|
||||
//res->datum.p = taosMemoryCalloc(res->node.resType.bytes + VARSTR_HEADER_SIZE + 1, 1);
|
||||
res->datum.p = taosMemoryCalloc(varDataTLen(output.columnData->pData), 1);
|
||||
res->node.resType.bytes = varDataTLen(output.columnData->pData);
|
||||
memcpy(res->datum.p, output.columnData->pData, varDataTLen(output.columnData->pData));
|
||||
} else {
|
||||
nodesSetValueNodeValue(res, output.columnData->pData);
|
||||
|
|
Loading…
Reference in New Issue