From d95ec693c00f06215cc4625195a6f4fb72243901 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Tue, 31 May 2022 21:05:43 +0800 Subject: [PATCH] feat:add new logic for new tag format --- include/common/tdataformat.h | 4 ++-- source/common/src/tdataformat.c | 3 +++ source/libs/parser/inc/parUtil.h | 2 +- source/libs/parser/src/parInsert.c | 16 +++++++--------- source/libs/parser/src/parTranslater.c | 11 +++++------ source/libs/parser/src/parUtil.c | 16 +++++++++++----- source/libs/scalar/test/scalar/scalarTests.cpp | 6 ++---- 7 files changed, 31 insertions(+), 27 deletions(-) diff --git a/include/common/tdataformat.h b/include/common/tdataformat.h index 38bbee1f1d..18932119ff 100644 --- a/include/common/tdataformat.h +++ b/include/common/tdataformat.h @@ -136,8 +136,8 @@ struct STagVal { }; #pragma pack(push, 1) -#define TD_TAG_JSON ((int8_t)0x80) // distinguish JSON string and JSON value with the highest bit -#define TD_TAG_LARGE ((int8_t)0x40) +#define TD_TAG_JSON ((int8_t)0x40) // distinguish JSON string and JSON value with the highest bit +#define TD_TAG_LARGE ((int8_t)0x20) struct STag { int8_t flags; int16_t len; diff --git a/source/common/src/tdataformat.c b/source/common/src/tdataformat.c index 722fa57bad..71842655ec 100644 --- a/source/common/src/tdataformat.c +++ b/source/common/src/tdataformat.c @@ -567,6 +567,9 @@ static void debugPrintTagVal(int8_t type, const void *val, int32_t vlen, const c case TSDB_DATA_TYPE_UBIGINT: printf("%s:%d type:%d vlen:%d, val:%" PRIu64 "\n", tag, ln, (int32_t)type, vlen, *(uint64_t *)val); break; + case TSDB_DATA_TYPE_NULL: + printf("%s:%d type:%d vlen:%d, val:%" PRIi8 "\n", tag, ln, (int32_t)type, vlen, *(int8_t *)val); + break; default: ASSERT(0); break; diff --git a/source/libs/parser/inc/parUtil.h b/source/libs/parser/inc/parUtil.h index 35cab7db36..00a8767e0c 100644 --- a/source/libs/parser/inc/parUtil.h +++ b/source/libs/parser/inc/parUtil.h @@ -55,7 +55,7 @@ int32_t getNumOfColumns(const STableMeta* pTableMeta); int32_t getNumOfTags(const STableMeta* pTableMeta); STableComInfo getTableInfo(const STableMeta* pTableMeta); STableMeta* tableMetaDup(const STableMeta* pTableMeta); -int32_t parseJsontoTagData(const char* json, SArray* pTagVals, SMsgBuf* errMsg); +int32_t parseJsontoTagData(const char* json, SArray* pTagVals, STag **ppTag, SMsgBuf* pMsgBuf); int32_t trimString(const char* src, int32_t len, char* dst, int32_t dlen); diff --git a/source/libs/parser/src/parInsert.c b/source/libs/parser/src/parInsert.c index 05e5d46c7e..df1e597b86 100644 --- a/source/libs/parser/src/parInsert.c +++ b/source/libs/parser/src/parInsert.c @@ -959,6 +959,7 @@ static int32_t parseTagsClause(SInsertParseContext* pCxt, SSchema* pSchema, uint SToken sToken; bool isParseBindParam = false; bool isJson = false; + STag* pTag = NULL; for (int i = 0; i < pCxt->tags.numOfBound; ++i) { NEXT_TOKEN_WITH_PREV(pCxt->pSql, sToken); @@ -990,7 +991,7 @@ static int32_t parseTagsClause(SInsertParseContext* pCxt, SSchema* pSchema, uint taosMemoryFree(tmpTokenBuf); goto end; } - code = parseJsontoTagData(sToken.z, pTagVals, &pCxt->msg); + code = parseJsontoTagData(sToken.z, pTagVals, &pTag, &pCxt->msg); taosMemoryFree(tmpTokenBuf); if(code != TSDB_CODE_SUCCESS){ goto end; @@ -1015,9 +1016,7 @@ static int32_t parseTagsClause(SInsertParseContext* pCxt, SSchema* pSchema, uint goto end; } - STag* pTag = NULL; - code = tTagNew(pTagVals, 1, isJson, &pTag); - if (code != TSDB_CODE_SUCCESS) { + if(!isJson && (code = tTagNew(pTagVals, 1, false, &pTag)) != TSDB_CODE_SUCCESS) { goto end; } @@ -1528,6 +1527,8 @@ int32_t qBindStmtTagsValue(void* pBlock, void* boundTags, int64_t suid, char* tN SSchema* pSchema = pDataBlock->pTableMeta->schema; bool isJson = false; + STag* pTag = NULL; + for (int c = 0; c < tags->numOfBound; ++c) { if (bind[c].is_null && bind[c].is_null[0]) { continue; @@ -1548,7 +1549,7 @@ int32_t qBindStmtTagsValue(void* pBlock, void* boundTags, int64_t suid, char* tN isJson = true; char *tmp = taosMemoryCalloc(1, colLen + 1); memcpy(tmp, bind[c].buffer, colLen); - code = parseJsontoTagData(tmp, pTagArray, &pBuf); + code = parseJsontoTagData(tmp, pTagArray, &pTag, &pBuf); taosMemoryFree(tmp); if(code != TSDB_CODE_SUCCESS){ goto end; @@ -1586,10 +1587,7 @@ int32_t qBindStmtTagsValue(void* pBlock, void* boundTags, int64_t suid, char* tN } } - STag* pTag = NULL; - - if (0 != tTagNew(pTagArray, 1, isJson, &pTag)) { - code = buildInvalidOperationMsg(&pBuf, "out of memory"); + if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) { goto end; } diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index bd31c16f54..16c35fce84 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -4218,7 +4218,7 @@ static int32_t buildKVRowForBindTags(STranslateContext* pCxt, SCreateSubTableCla } isJson = true; - code = parseJsontoTagData(pVal->literal, pTagArray, &pCxt->msgBuf); + code = parseJsontoTagData(pVal->literal, pTagArray, ppTag, &pCxt->msgBuf); if(code != TSDB_CODE_SUCCESS){ goto end; } @@ -4235,7 +4235,7 @@ static int32_t buildKVRowForBindTags(STranslateContext* pCxt, SCreateSubTableCla } } - code = tTagNew(pTagArray, 1, isJson, ppTag); + if(!isJson) code = tTagNew(pTagArray, 1, false, ppTag); end: if(isJson){ @@ -4285,7 +4285,7 @@ static int32_t buildKVRowForAllTags(STranslateContext* pCxt, SCreateSubTableClau } isJson = true; - code = parseJsontoTagData(pVal->literal, pTagArray, &pCxt->msgBuf); + code = parseJsontoTagData(pVal->literal, pTagArray, ppTag, &pCxt->msgBuf); if(code != TSDB_CODE_SUCCESS){ goto end; } @@ -4302,7 +4302,7 @@ static int32_t buildKVRowForAllTags(STranslateContext* pCxt, SCreateSubTableClau } ++index; } - code = tTagNew(pTagArray, 1, isJson, ppTag); + if(!isJson) code = tTagNew(pTagArray, 1, false, ppTag); end: if(isJson){ @@ -4580,11 +4580,10 @@ static int32_t buildUpdateTagValReq(STranslateContext* pCxt, SAlterTableStmt* pS int32_t code = TSDB_CODE_SUCCESS; STag* pTag = NULL; do{ - code = parseJsontoTagData(pStmt->pVal->literal, pTagVals, &pCxt->msgBuf); + code = parseJsontoTagData(pStmt->pVal->literal, pTagVals, &pTag, &pCxt->msgBuf); if (TSDB_CODE_SUCCESS != code) { break; } - code = tTagNew(pTagVals, 1, true, &pTag); }while(0); for (int i = 0; i < taosArrayGetSize(pTagVals); ++i) { STagVal *p = (STagVal *)taosArrayGet(pTagVals, i); diff --git a/source/libs/parser/src/parUtil.c b/source/libs/parser/src/parUtil.c index 726028355f..18fb5322b1 100644 --- a/source/libs/parser/src/parUtil.c +++ b/source/libs/parser/src/parUtil.c @@ -328,24 +328,27 @@ static bool isValidateTag(char* input) { return true; } -int32_t parseJsontoTagData(const char* json, SArray* pTagVals, SMsgBuf* pMsgBuf) { +int32_t parseJsontoTagData(const char* json, SArray* pTagVals, STag **ppTag, SMsgBuf* pMsgBuf) { + int32_t retCode = TSDB_CODE_SUCCESS; // set json NULL data if (!json || strtrim((char*)json) == 0 || strcasecmp(json, TSDB_DATA_NULL_STR_L) == 0) { - return TSDB_CODE_SUCCESS; + retCode = TSDB_CODE_SUCCESS; + goto end; } // set json real data cJSON* root = cJSON_Parse(json); if (root == NULL) { - return buildSyntaxErrMsg(pMsgBuf, "json parse error", json); + retCode = buildSyntaxErrMsg(pMsgBuf, "json parse error", json); + goto end; } int32_t size = cJSON_GetArraySize(root); if (!cJSON_IsObject(root)) { - return buildSyntaxErrMsg(pMsgBuf, "json error invalide value", json); + retCode = buildSyntaxErrMsg(pMsgBuf, "json error invalide value", json); + goto end; } - int32_t retCode = TSDB_CODE_SUCCESS; SHashObj* keyHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, false); for (int32_t i = 0; i < size; i++) { cJSON* item = cJSON_GetArrayItem(root, i); @@ -413,6 +416,9 @@ int32_t parseJsontoTagData(const char* json, SArray* pTagVals, SMsgBuf* pMsgBuf) end: taosHashCleanup(keyHash); + if(retCode == TSDB_CODE_SUCCESS){ + tTagNew(pTagVals, 1, true, ppTag); + } cJSON_Delete(root); return retCode; } diff --git a/source/libs/scalar/test/scalar/scalarTests.cpp b/source/libs/scalar/test/scalar/scalarTests.cpp index 2081c6a3f0..6ddf4b12b9 100644 --- a/source/libs/scalar/test/scalar/scalarTests.cpp +++ b/source/libs/scalar/test/scalar/scalarTests.cpp @@ -1112,9 +1112,8 @@ TEST(columnTest, json_column_arith_op) { char rightv[256] = {0}; memcpy(rightv, rightvTmp, strlen(rightvTmp)); SArray *tags = taosArrayInit(1, sizeof(STagVal)); - parseJsontoTagData(rightv, tags, NULL); STag* row = NULL; - tTagNew(tags, 1, true, &row); + parseJsontoTagData(rightv, tags, &row, NULL); const int32_t len = 8; EOperatorType op[len] = {OP_TYPE_ADD, OP_TYPE_SUB, OP_TYPE_MULTI, OP_TYPE_DIV, @@ -1189,9 +1188,8 @@ TEST(columnTest, json_column_logic_op) { char rightv[256] = {0}; memcpy(rightv, rightvTmp, strlen(rightvTmp)); SArray *tags = taosArrayInit(1, sizeof(STagVal)); - parseJsontoTagData(rightv, tags, NULL); STag* row = NULL; - tTagNew(tags, 1, true, &row); + parseJsontoTagData(rightv, tags, &row, NULL); const int32_t len = 9; const int32_t len1 = 4; -- GitLab