From fb36f5e24e4857b3d97d14b1a888e78ae4b9ab00 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Fri, 1 Apr 2022 19:10:53 +0800 Subject: [PATCH] [td-14428] return correct length for both nchar and varchar type. --- source/client/inc/clientInt.h | 1 + source/client/src/clientEnv.c | 1 + source/client/src/clientImpl.c | 16 ++++++++++++++-- source/client/src/clientMain.c | 2 +- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/source/client/inc/clientInt.h b/source/client/inc/clientInt.h index 49cb12cccd..e930dc2675 100644 --- a/source/client/inc/clientInt.h +++ b/source/client/inc/clientInt.h @@ -153,6 +153,7 @@ typedef struct SReqResultInfo { const char* pRspMsg; const char* pData; TAOS_FIELD* fields; + TAOS_FIELD* userFields; // the fields info that return to user uint32_t numOfCols; int32_t* length; char** convertBuf; diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 53cc77af1a..769870ada8 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -169,6 +169,7 @@ static void doFreeReqResultInfo(SReqResultInfo *pResInfo) { taosMemoryFreeClear(pResInfo->row); taosMemoryFreeClear(pResInfo->pCol); taosMemoryFreeClear(pResInfo->fields); + taosMemoryFreeClear(pResInfo->userFields); if (pResInfo->convertBuf != NULL) { for (int32_t i = 0; i < pResInfo->numOfCols; ++i) { diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index 8cd53f18b0..4b41785d08 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -228,12 +228,24 @@ void setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t assert(pSchema != NULL && numOfCols > 0); pResInfo->numOfCols = numOfCols; - pResInfo->fields = taosMemoryCalloc(numOfCols, sizeof(pSchema[0])); + pResInfo->fields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD)); + pResInfo->userFields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD)); for (int32_t i = 0; i < pResInfo->numOfCols; ++i) { pResInfo->fields[i].bytes = pSchema[i].bytes; - pResInfo->fields[i].type = pSchema[i].type; + pResInfo->fields[i].type = pSchema[i].type; + + pResInfo->userFields[i].bytes = pSchema[i].bytes; + pResInfo->userFields[i].type = pSchema[i].type; + + if (pSchema[i].type == TSDB_DATA_TYPE_VARCHAR) { + pResInfo->userFields[i].bytes -= VARSTR_HEADER_SIZE; + } else if (pSchema[i].type == TSDB_DATA_TYPE_NCHAR) { + pResInfo->userFields[i].bytes = (pResInfo->userFields[i].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE; + } + tstrncpy(pResInfo->fields[i].name, pSchema[i].name, tListLen(pResInfo->fields[i].name)); + tstrncpy(pResInfo->userFields[i].name, pSchema[i].name, tListLen(pResInfo->userFields[i].name)); } } diff --git a/source/client/src/clientMain.c b/source/client/src/clientMain.c index 42d4b5b2e5..2dd77b7f22 100644 --- a/source/client/src/clientMain.c +++ b/source/client/src/clientMain.c @@ -146,7 +146,7 @@ TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) { } SReqResultInfo *pResInfo = &(((SRequestObj *)res)->body.resInfo); - return pResInfo->fields; + return pResInfo->userFields; } TAOS_RES *taos_query(TAOS *taos, const char *sql) {