diff --git a/include/libs/qcom/query.h b/include/libs/qcom/query.h index 4f9334abb5bc56dcfd3d3a5a6513786fd8f5e9f5..1fb8a1db73c51b466bf4bcf1b68128d3e561642a 100644 --- a/include/libs/qcom/query.h +++ b/include/libs/qcom/query.h @@ -208,6 +208,7 @@ char* jobTaskStatusStr(int32_t status); SSchema createSchema(int8_t type, int32_t bytes, col_id_t colId, const char* name); void destroyQueryExecRes(SQueryExecRes* pRes); int32_t dataConverToStr(char *str, int type, void *buf, int32_t bufSize, int32_t *len); +char* parseTagDatatoJson(void* p); extern int32_t (*queryBuildMsg[TDMT_MAX])(void *input, char **msg, int32_t msgSize, int32_t *msgLen, void*(*mallocFp)(int32_t)); extern int32_t (*queryProcessMsgRsp[TDMT_MAX])(void* output, char* msg, int32_t msgSize); diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index 8920922006d517df10e536ad407e8f83909350c4..4b24228839ed6f6dfee2ee02223fa9cbe2b63397 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -1444,80 +1444,6 @@ static int32_t doPrepareResPtr(SReqResultInfo* pResInfo) { return TSDB_CODE_SUCCESS; } -static char* parseTagDatatoJson(void* p) { - char* string = NULL; - cJSON* json = cJSON_CreateObject(); - if (json == NULL) { - goto end; - } - - SArray* pTagVals = NULL; - if (tTagToValArray((const STag*)p, &pTagVals) != 0) { - goto end; - } - - int16_t nCols = taosArrayGetSize(pTagVals); - char tagJsonKey[256] = {0}; - for (int j = 0; j < nCols; ++j) { - STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j); - // json key encode by binary - memset(tagJsonKey, 0, sizeof(tagJsonKey)); - memcpy(tagJsonKey, pTagVal->pKey, strlen(pTagVal->pKey)); - // json value - char type = pTagVal->type; - if (type == TSDB_DATA_TYPE_NULL) { - cJSON* value = cJSON_CreateNull(); - if (value == NULL) { - goto end; - } - cJSON_AddItemToObject(json, tagJsonKey, value); - } else if (type == TSDB_DATA_TYPE_NCHAR) { - cJSON* value = NULL; - if (pTagVal->nData > 0) { - char* tagJsonValue = taosMemoryCalloc(pTagVal->nData, 1); - int32_t length = taosUcs4ToMbs((TdUcs4*)pTagVal->pData, pTagVal->nData, tagJsonValue); - if (length < 0) { - tscError("charset:%s to %s. val:%s convert json value failed.", DEFAULT_UNICODE_ENCODEC, tsCharset, - pTagVal->pData); - taosMemoryFree(tagJsonValue); - goto end; - } - value = cJSON_CreateString(tagJsonValue); - taosMemoryFree(tagJsonValue); - if (value == NULL) { - goto end; - } - } else if (pTagVal->nData == 0) { - value = cJSON_CreateString(""); - } else { - ASSERT(0); - } - - cJSON_AddItemToObject(json, tagJsonKey, value); - } else if (type == TSDB_DATA_TYPE_DOUBLE) { - double jsonVd = *(double*)(&pTagVal->i64); - cJSON* value = cJSON_CreateNumber(jsonVd); - if (value == NULL) { - goto end; - } - cJSON_AddItemToObject(json, tagJsonKey, value); - } else if (type == TSDB_DATA_TYPE_BOOL) { - char jsonVd = *(char*)(&pTagVal->i64); - cJSON* value = cJSON_CreateBool(jsonVd); - if (value == NULL) { - goto end; - } - cJSON_AddItemToObject(json, tagJsonKey, value); - } else { - ASSERT(0); - } - } - string = cJSON_PrintUnformatted(json); -end: - cJSON_Delete(json); - return string; -} - static int32_t doConvertUCS4(SReqResultInfo* pResultInfo, int32_t numOfRows, int32_t numOfCols, int32_t* colLength) { for (int32_t i = 0; i < numOfCols; ++i) { int32_t type = pResultInfo->fields[i].type; diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index 51a5d6909048f798790b2e47f43c2214fed8cfa4..fcf5f173c4792fcd53d69d79b656d1f99c59c5c2 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -1918,7 +1918,7 @@ int32_t tDeserializeSTableCfgRsp(void *buf, int32_t bufLen, STableCfgRsp *pRsp) } if (tDecodeI32(&decoder, &pRsp->tagsLen) < 0) return -1; - if (tDecodeBinaryAlloc(&decoder, &pRsp->pTags, NULL) < 0) return -1; + if (tDecodeBinaryAlloc(&decoder, (void**)&pRsp->pTags, NULL) < 0) return -1; tEndDecode(&decoder); diff --git a/source/libs/command/src/command.c b/source/libs/command/src/command.c index 1d095d33c85476c229680160d1cd1aff57ced029..3947dcdcceac9dad676157105d00987c52e2f9dc 100644 --- a/source/libs/command/src/command.c +++ b/source/libs/command/src/command.c @@ -332,6 +332,15 @@ void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) { } } + +void appendTagNameFields(char* buf, int32_t* len, STableCfg* pCfg) { + for (int32_t i = 0; i < pCfg->numOfTags; ++i) { + SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i; + *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "%s`%s`", ((i > 0) ? ", " : ""), pSchema->name); + } +} + + int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg) { SArray *pTagVals = NULL; STag *pTag = (STag*)pCfg->pTags; @@ -340,6 +349,7 @@ int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg) { char *pJson = parseTagDatatoJson(pTag); if (pJson) { *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "%s", pJson); + taosMemoryFree(pJson); } return TSDB_CODE_SUCCESS; @@ -350,15 +360,40 @@ int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg) { return code; } - int16_t nCols = taosArrayGetSize(pTagVals); + int16_t valueNum = taosArrayGetSize(pTagVals); int32_t num = 0; - for (int i = 0; i < nCols; i++) { - STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i); - char type = pTagVal->type; - int32_t tlen = 0; - - dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, type, pTagVal->pData, pTagVal->nData, &tlen); - *len += tlen; + int32_t j = 0; + for (int32_t i = 0; i < pCfg->numOfTags; ++i) { + SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i; + if (i > 0) { + *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, ", "); + } + + if (j >= valueNum) { + *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "NULL"); + continue; + } + + STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, j); + if (pSchema->colId > pTagVal->cid) { + qError("tag value and column mismatch, schemaId:%d, valId:%d", pSchema->colId, pTagVal->cid); + taosArrayDestroy(pTagVals); + return TSDB_CODE_APP_ERROR; + } else if (pSchema->colId == pTagVal->cid) { + char type = pTagVal->type; + int32_t tlen = 0; + + if (IS_VAR_DATA_TYPE(type)) { + dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, type, pTagVal->pData, pTagVal->nData, &tlen); + } else { + dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, type, &pTagVal->i64, tDataTypes[type].bytes, &tlen); + } + *len += tlen; + j++; + } else { + *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "NULL"); + } + /* if (type == TSDB_DATA_TYPE_BINARY) { @@ -389,6 +424,9 @@ int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg) { */ } + taosArrayDestroy(pTagVals); + + return TSDB_CODE_SUCCESS; } void appendTableOptions(char* buf, int32_t* len, STableCfg* pCfg) { @@ -444,7 +482,9 @@ static int32_t setCreateTBResultIntoDataBlock(SSDataBlock* pBlock, char *tbName, appendTagFields(buf2, &len, pCfg); len += sprintf(buf2 + VARSTR_HEADER_SIZE + len, ")"); } else if (TSDB_CHILD_TABLE == pCfg->tableType) { - len += sprintf(buf2 + VARSTR_HEADER_SIZE, "CREATE TABLE `%s` USING `%s` TAGS (", tbName, pCfg->stbName); + len += sprintf(buf2 + VARSTR_HEADER_SIZE, "CREATE TABLE `%s` USING `%s` (", tbName, pCfg->stbName); + appendTagNameFields(buf2, &len, pCfg); + len += sprintf(buf2 + VARSTR_HEADER_SIZE + len, ") TAGS ("); code = appendTagValues(buf2, &len, pCfg); if (code) { return code; diff --git a/source/libs/qcom/src/queryUtil.c b/source/libs/qcom/src/queryUtil.c index f469c25558b397cc7a53a47edc01ce68b6c40c92..d102c9275a032e44bb3c43ac9da5815c97e85362 100644 --- a/source/libs/qcom/src/queryUtil.c +++ b/source/libs/qcom/src/queryUtil.c @@ -19,6 +19,7 @@ #include "tmsg.h" #include "trpc.h" #include "tsched.h" +#include "cJSON.h" #define VALIDNUMOFCOLS(x) ((x) >= TSDB_MIN_COLUMNS && (x) <= TSDB_MAX_COLUMNS) #define VALIDNUMOFTAGS(x) ((x) >= 0 && (x) <= TSDB_MAX_TAGS) @@ -297,4 +298,78 @@ int32_t dataConverToStr(char *str, int type, void *buf, int32_t bufSize, int32_t return TSDB_CODE_SUCCESS; } +char* parseTagDatatoJson(void* p) { + char* string = NULL; + cJSON* json = cJSON_CreateObject(); + if (json == NULL) { + goto end; + } + + SArray* pTagVals = NULL; + if (tTagToValArray((const STag*)p, &pTagVals) != 0) { + goto end; + } + + int16_t nCols = taosArrayGetSize(pTagVals); + char tagJsonKey[256] = {0}; + for (int j = 0; j < nCols; ++j) { + STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j); + // json key encode by binary + memset(tagJsonKey, 0, sizeof(tagJsonKey)); + memcpy(tagJsonKey, pTagVal->pKey, strlen(pTagVal->pKey)); + // json value + char type = pTagVal->type; + if (type == TSDB_DATA_TYPE_NULL) { + cJSON* value = cJSON_CreateNull(); + if (value == NULL) { + goto end; + } + cJSON_AddItemToObject(json, tagJsonKey, value); + } else if (type == TSDB_DATA_TYPE_NCHAR) { + cJSON* value = NULL; + if (pTagVal->nData > 0) { + char* tagJsonValue = taosMemoryCalloc(pTagVal->nData, 1); + int32_t length = taosUcs4ToMbs((TdUcs4*)pTagVal->pData, pTagVal->nData, tagJsonValue); + if (length < 0) { + qError("charset:%s to %s. val:%s convert json value failed.", DEFAULT_UNICODE_ENCODEC, tsCharset, + pTagVal->pData); + taosMemoryFree(tagJsonValue); + goto end; + } + value = cJSON_CreateString(tagJsonValue); + taosMemoryFree(tagJsonValue); + if (value == NULL) { + goto end; + } + } else if (pTagVal->nData == 0) { + value = cJSON_CreateString(""); + } else { + ASSERT(0); + } + + cJSON_AddItemToObject(json, tagJsonKey, value); + } else if (type == TSDB_DATA_TYPE_DOUBLE) { + double jsonVd = *(double*)(&pTagVal->i64); + cJSON* value = cJSON_CreateNumber(jsonVd); + if (value == NULL) { + goto end; + } + cJSON_AddItemToObject(json, tagJsonKey, value); + } else if (type == TSDB_DATA_TYPE_BOOL) { + char jsonVd = *(char*)(&pTagVal->i64); + cJSON* value = cJSON_CreateBool(jsonVd); + if (value == NULL) { + goto end; + } + cJSON_AddItemToObject(json, tagJsonKey, value); + } else { + ASSERT(0); + } + } + string = cJSON_PrintUnformatted(json); +end: + cJSON_Delete(json); + return string; +} +