From e84ba7c9a579e0e455db34c1fa925377a355fa8e Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Fri, 17 Sep 2021 11:30:43 +0800 Subject: [PATCH] TD-6129 fix json encode error --- src/client/src/tscParseInsert.c | 2 +- src/client/src/tscSQLParser.c | 6 +++--- src/client/src/tscUtil.c | 6 +++--- src/common/inc/tdataformat.h | 6 ++++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/client/src/tscParseInsert.c b/src/client/src/tscParseInsert.c index 91db5277bf..89647324b9 100644 --- a/src/client/src/tscParseInsert.c +++ b/src/client/src/tscParseInsert.c @@ -1077,7 +1077,7 @@ static int32_t tscCheckIfCreateTable(char **sqlstr, SSqlObj *pSql, char** boundC return code; } - tdAddColToKVRow(&kvRowBuilder, pSchema->colId, pSchema->type, tagVal); + tdAddColToKVRow(&kvRowBuilder, pSchema->colId, pSchema->type, tagVal, false); } // encode json tag string diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 208fa10abc..180e3d7443 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -7618,7 +7618,7 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg4); } - tdAddColToKVRow(&kvRowBuilder, pSchema->colId, pSchema->type, tagVal); + tdAddColToKVRow(&kvRowBuilder, pSchema->colId, pSchema->type, tagVal, false); findColumnIndex = true; break; @@ -7640,7 +7640,7 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { SSchema* pSchema = &pTagSchema[i]; tVariantListItem* pItem = taosArrayGet(pValList, i); - char tagVal[TSDB_MAX_TAGS_LEN]; + char tagVal[TSDB_MAX_TAGS_LEN] = {0}; if (pSchema->type == TSDB_DATA_TYPE_BINARY || pSchema->type == TSDB_DATA_TYPE_NCHAR) { if (pItem->pVar.nLen > pSchema->bytes) { tdDestroyKVRowBuilder(&kvRowBuilder); @@ -7678,7 +7678,7 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg4); } - tdAddColToKVRow(&kvRowBuilder, pSchema->colId, pSchema->type, tagVal); + tdAddColToKVRow(&kvRowBuilder, pSchema->colId, pSchema->type, tagVal, false); } } diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index be61434dac..615183f9f3 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -5178,7 +5178,7 @@ int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, in } varDataSetLen(tagVal, output); - tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_NCHAR, tagVal); // add json key + tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_NCHAR, tagVal, false); // add json key memset(tagVal, 0, TSDB_MAX_TAGS_LEN); if(item->type == cJSON_String){ // add json value format: type|data @@ -5192,12 +5192,12 @@ int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, in } varDataSetLen(tagData, output); - tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_NCHAR, tagVal); + tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_NCHAR, tagVal, true); }else if(item->type == cJSON_Number){ *tagVal = item->type; // type char* tagData = tagVal + CHAR_BYTES; *((double *)tagData) = item->valuedouble; - tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_BIGINT, tagVal); + tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_BIGINT, tagVal, true); }else{ retCode = tscSQLSyntaxErrMsg(errMsg, "invalidate json value", NULL); goto end; diff --git a/src/common/inc/tdataformat.h b/src/common/inc/tdataformat.h index a01c377539..54e9521469 100644 --- a/src/common/inc/tdataformat.h +++ b/src/common/inc/tdataformat.h @@ -544,7 +544,7 @@ void tdDestroyKVRowBuilder(SKVRowBuilder *pBuilder); void tdResetKVRowBuilder(SKVRowBuilder *pBuilder); SKVRow tdGetKVRowFromBuilder(SKVRowBuilder *pBuilder); -static FORCE_INLINE int tdAddColToKVRow(SKVRowBuilder *pBuilder, int16_t colId, int8_t type, void *value) { +static FORCE_INLINE int tdAddColToKVRow(SKVRowBuilder *pBuilder, int16_t colId, int8_t type, void *value, bool isJumpJsonVType) { if (pBuilder->nCols >= pBuilder->tCols) { pBuilder->tCols *= 2; SColIdx* pColIdx = (SColIdx *)realloc((void *)(pBuilder->pColIdx), sizeof(SColIdx) * pBuilder->tCols); @@ -557,7 +557,9 @@ static FORCE_INLINE int tdAddColToKVRow(SKVRowBuilder *pBuilder, int16_t colId, pBuilder->nCols++; - int tlen = IS_VAR_DATA_TYPE(type) ? varDataTLen(value) : TYPE_BYTES[type]; + void *jumpType = value; + if(isJumpJsonVType) jumpType = value + CHAR_BYTES; + int tlen = IS_VAR_DATA_TYPE(type) ? varDataTLen(jumpType) : TYPE_BYTES[type]; if (tlen > pBuilder->alloc - pBuilder->size) { while (tlen > pBuilder->alloc - pBuilder->size) { pBuilder->alloc *= 2; -- GitLab