From e27cccd1771b60e70b349c5612105b62adb9e253 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Sat, 18 Sep 2021 14:04:08 +0800 Subject: [PATCH] TD-6129 fix json encode error --- src/client/src/tscUtil.c | 4 ++-- src/tsdb/inc/tsdbMeta.h | 3 ++- src/tsdb/src/tsdbMeta.c | 18 ++++++++++------- src/tsdb/src/tsdbRead.c | 43 +++++++++++++++++++++++++++++++--------- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index b2f9f25d80..c21b326476 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -5272,7 +5272,7 @@ int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, in if(item->type == cJSON_String){ // add json value format: type|data output = 0; *tagVal = item->type; // type - char* tagData = tagVal + CHAR_BYTES; + char* tagData = POINTER_SHIFT(tagVal,CHAR_BYTES); if (!taosMbsToUcs4(item->valuestring, strlen(item->valuestring), varDataVal(tagData), TSDB_MAX_TAGS_LEN - VARSTR_HEADER_SIZE, &output)) { tscError("json string error:%s|%s", strerror(errno), item->string); retCode = tscSQLSyntaxErrMsg(errMsg, "serizelize json error", NULL); @@ -5283,7 +5283,7 @@ int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, in tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_NCHAR, tagVal, true); }else if(item->type == cJSON_Number){ *tagVal = item->type; // type - char* tagData = tagVal + CHAR_BYTES; + char* tagData = POINTER_SHIFT(tagVal,CHAR_BYTES); *((double *)tagData) = item->valuedouble; tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_BIGINT, tagVal, true); }else{ diff --git a/src/tsdb/inc/tsdbMeta.h b/src/tsdb/inc/tsdbMeta.h index d70fb9de12..49ccb7ff68 100644 --- a/src/tsdb/inc/tsdbMeta.h +++ b/src/tsdb/inc/tsdbMeta.h @@ -20,7 +20,7 @@ #pragma pack (push,1) typedef struct jsonMapValue{ - uint64_t uid; // the unique table ID + void* table; // STable * int16_t colId; // the json col ID. }JsonMapValue; @@ -97,6 +97,7 @@ int16_t tsdbGetLastColumnsIndexByColId(STable* pTable, int16_t colId); int tsdbUpdateLastColSchema(STable *pTable, STSchema *pNewSchema); STSchema* tsdbGetTableLatestSchema(STable *pTable); void tsdbFreeLastColumns(STable* pTable); +int tscCompareJsonMapValue(const void* a, const void* b); static FORCE_INLINE int tsdbCompareSchemaVersion(const void *key1, const void *key2) { if (*(int16_t *)key1 < schemaVersion(*(STSchema **)key2)) { diff --git a/src/tsdb/src/tsdbMeta.c b/src/tsdb/src/tsdbMeta.c index e1bc5f05d9..ade3b7a8e8 100644 --- a/src/tsdb/src/tsdbMeta.c +++ b/src/tsdb/src/tsdbMeta.c @@ -1083,11 +1083,11 @@ static void tsdbRemoveTableFromMeta(STsdbRepo *pRepo, STable *pTable, bool rmFro tsdbUnRefTable(pTable); } -static int tscCompareJsonMapValue(const void* a, const void* b) { +int tscCompareJsonMapValue(const void* a, const void* b) { const JsonMapValue* x = (const JsonMapValue*)a; const JsonMapValue* y = (const JsonMapValue*)b; - if (x->uid > y->uid) return 1; - if (x->uid < y->uid) return -1; + if (x->table > y->table) return 1; + if (x->table < y->table) return -1; return 0; } @@ -1127,9 +1127,13 @@ static int tsdbAddTableIntoIndex(STsdbMeta *pMeta, STable *pTable, bool refSuper } tablist = tablistNew; } - JsonMapValue jmvalue = {TABLE_UID(pTable), pColIdx->colId}; - taosArrayPush(tablist, &jmvalue); - taosArraySort(tablist, tscCompareJsonMapValue); + JsonMapValue jmvalue = {pTable, pColIdx->colId}; + void* p = taosArraySearch(tablist, &jmvalue, tscCompareJsonMapValue, TD_EQ); + if (p == NULL) { + taosArrayPush(tablist, &jmvalue); + }else{ + taosArrayInsert(tablist, TARRAY_ELEM_IDX((SArray*)tablist, p), &jmvalue); + } } }else{ tSkipListPut(pSTable->pIndex, (void *)pTable); @@ -1165,7 +1169,7 @@ static int tsdbRemoveTableFromIndex(STsdbMeta *pMeta, STable *pTable) { continue; } - JsonMapValue jmvalue = {TABLE_UID(pTable), pColIdx->colId}; + JsonMapValue jmvalue = {pTable, pColIdx->colId}; void* p = taosArraySearch(tablist, &jmvalue, tscCompareJsonMapValue, TD_EQ); if (p == NULL) { tsdbError("json tag no tableid error,%d", j); diff --git a/src/tsdb/src/tsdbRead.c b/src/tsdb/src/tsdbRead.c index 4aab9dff7d..19f7f413a4 100644 --- a/src/tsdb/src/tsdbRead.c +++ b/src/tsdb/src/tsdbRead.c @@ -2676,17 +2676,40 @@ static int tsdbReadRowsFromCache(STableCheckInfo* pCheckInfo, TSKEY maxKey, int } static int32_t getAllTableList(STable* pSuperTable, SArray* list) { - SSkipListIterator* iter = tSkipListCreateIter(pSuperTable->pIndex); - while (tSkipListIterNext(iter)) { - SSkipListNode* pNode = tSkipListIterGet(iter); + STSchema* pTagSchema = tsdbGetTableTagSchema(pSuperTable); + if(pTagSchema->numOfCols == 1 && pTagSchema->columns[0].type == TSDB_DATA_TYPE_JSON){ + SArray* pRecord = taosHashIterate(pSuperTable->jsonKeyMap, NULL); + SArray* tablist = taosArrayInit(32, sizeof(JsonMapValue)); + + while(pRecord){ + for (int i = 0; i < taosArrayGetSize(pRecord); ++i) { + void* p = taosArrayGet(pRecord, i); + void* pFind = taosArraySearch(tablist, p, tscCompareJsonMapValue, TD_EQ); + if(pFind == NULL){ + taosArrayPush(tablist, p); + } + } + pRecord = taosHashIterate(pSuperTable->jsonKeyMap, pRecord); + } + for (int i = 0; i < taosArrayGetSize(tablist); ++i) { + JsonMapValue* p = taosArrayGet(pRecord, i); + STableKeyInfo info = {.pTable = p->table, .lastKey = TSKEY_INITIAL_VAL}; + taosArrayPush(list, &info); + } + taosArrayDestroy(tablist); + }else{ + SSkipListIterator* iter = tSkipListCreateIter(pSuperTable->pIndex); + while (tSkipListIterNext(iter)) { + SSkipListNode* pNode = tSkipListIterGet(iter); - STable* pTable = (STable*) SL_GET_NODE_DATA((SSkipListNode*) pNode); + STable* pTable = (STable*) SL_GET_NODE_DATA((SSkipListNode*) pNode); - STableKeyInfo info = {.pTable = pTable, .lastKey = TSKEY_INITIAL_VAL}; - taosArrayPush(list, &info); - } + STableKeyInfo info = {.pTable = pTable, .lastKey = TSKEY_INITIAL_VAL}; + taosArrayPush(list, &info); + } - tSkipListDestroyIter(iter); + tSkipListDestroyIter(iter); + } return TSDB_CODE_SUCCESS; } @@ -3626,6 +3649,7 @@ SArray* createTableGroup(SArray* pTableList, STSchema* pTagSchema, SColIndex* pC int32_t tsdbQuerySTableByTagCond(STsdbRepo* tsdb, uint64_t uid, TSKEY skey, const char* pTagCond, size_t len, STableGroupInfo* pGroupInfo, SColIndex* pColIndex, int32_t numOfCols) { + SArray* res = NULL; if (tsdbRLockRepoMeta(tsdb) < 0) goto _error; STable* pTable = tsdbGetTableByUid(tsdbGetMeta(tsdb), uid); @@ -3647,7 +3671,7 @@ int32_t tsdbQuerySTableByTagCond(STsdbRepo* tsdb, uint64_t uid, TSKEY skey, cons } //NOTE: not add ref count for super table - SArray* res = taosArrayInit(8, sizeof(STableKeyInfo)); + res = taosArrayInit(8, sizeof(STableKeyInfo)); STSchema* pTagSchema = tsdbGetTableTagSchema(pTable); // no tags and tbname condition, all child tables of this stable are involved @@ -3711,6 +3735,7 @@ int32_t tsdbQuerySTableByTagCond(STsdbRepo* tsdb, uint64_t uid, TSKEY skey, cons return ret; _error: + taosArrayDestroy(res); return terrno; } -- GitLab