diff --git a/src/client/src/tscParseInsert.c b/src/client/src/tscParseInsert.c index 05b8b031d99c2b0f4e54e9fc3392a20a9e1bcfcc..de974bec46426a892b9c645a95c7b959ac97d9ff 100644 --- a/src/client/src/tscParseInsert.c +++ b/src/client/src/tscParseInsert.c @@ -360,7 +360,7 @@ int32_t tsParseOneColumn(SSchema *pSchema, SStrToken *pToken, char *payload, cha return tscInvalidOperationMsg(msg, "json tag length too long", pToken->z); } if (pToken->type == TK_NULL) { - *(int8_t *)payload = TSDB_DATA_TINYINT_NULL; + *(int8_t *)payload = TSDB_DATA_JSON_PLACEHOLDER; } else if (pToken->type != TK_STRING){ tscInvalidOperationMsg(msg, "invalid json data", pToken->z); } else{ @@ -1063,7 +1063,7 @@ static int32_t tscCheckIfCreateTable(char **sqlstr, SSqlObj *pSql, char** boundC sToken.n -= 2; } - char tagVal[TSDB_MAX_TAGS_LEN]; + char tagVal[TSDB_MAX_TAGS_LEN] = {0}; code = tsParseOneColumn(pSchema, &sToken, tagVal, pInsertParam->msg, &sql, false, tinfo.precision); if (code != TSDB_CODE_SUCCESS) { tdDestroyKVRowBuilder(&kvRowBuilder); diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index b045c566f1a72b8f0ca970d8e30d1b0e6486be68..4d63438c9a5582981b86e41599b6b3a30314c9c9 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -6871,7 +6871,7 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { } SKVRowBuilder kvRowBuilder = {0}; if (pTagsSchema->type == TSDB_DATA_TYPE_JSON) { - if (pItem->pVar.nType != TSDB_DATA_TYPE_BINARY) { + if (pItem->pVar.nType != TSDB_DATA_TYPE_BINARY && pItem->pVar.nType != TSDB_DATA_TYPE_NULL) { tscError("json type error, should be string"); return invalidOperationMsg(pMsg, msg25); } @@ -8714,7 +8714,7 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg5); } tVariantListItem* pItem = taosArrayGet(pValList, 0); - if(pItem->pVar.nType != TSDB_DATA_TYPE_BINARY){ + if(pItem->pVar.nType != TSDB_DATA_TYPE_BINARY && pItem->pVar.nType != TSDB_DATA_TYPE_NULL){ tscError("json type error, should be string"); tdDestroyKVRowBuilder(&kvRowBuilder); return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg7); @@ -8724,6 +8724,7 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { tdDestroyKVRowBuilder(&kvRowBuilder); return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); } + ret = parseJsontoTagData(pItem->pVar.pz, &kvRowBuilder, tscGetErrorMsgPayload(pCmd), pTagSchema[0].colId); if (ret != TSDB_CODE_SUCCESS) { tdDestroyKVRowBuilder(&kvRowBuilder); diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index e96e3c16da84b3ffc25b33e3864c4e38dcc3977f..322413a3cd7e637c477903b09522f60c11056885 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -5459,7 +5459,7 @@ int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, in varDataSetLen(nullTypeVal + CHAR_BYTES, INT_BYTES); *(uint32_t*)(varDataVal(nullTypeKey)) = jsonNULL; tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_NCHAR, nullTypeKey, false); // add json null type - if (strtrim(json) == 0 || strcasecmp(json, "null") == 0){ + if (!json || strtrim(json) == 0 || strcasecmp(json, "null") == 0){ *(uint32_t*)(varDataVal(nullTypeVal + CHAR_BYTES)) = jsonNULL; tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_NCHAR, nullTypeVal, true); // add json null value return TSDB_CODE_SUCCESS; diff --git a/src/common/src/tvariant.c b/src/common/src/tvariant.c index 0d00856f9be76ee917d12ff7435142d6d55ccecf..3c9d62294776bfa639620249416eee738fe24b99 100644 --- a/src/common/src/tvariant.c +++ b/src/common/src/tvariant.c @@ -953,7 +953,7 @@ int32_t tVariantDumpEx(tVariant *pVariant, char *payload, int16_t type, bool inc break; } case TSDB_DATA_TYPE_JSON:{ - if (pVariant->nType == TSDB_DATA_TYPE_BINARY){ + if (pVariant->nType == TSDB_DATA_TYPE_BINARY || pVariant->nType == TSDB_DATA_TYPE_NULL){ *((int8_t *)payload) = TSDB_DATA_JSON_PLACEHOLDER; } else if (pVariant->nType == TSDB_DATA_TYPE_JSON){ // select * from stable, set tag type to json,from setTagValue/tag_project_function memcpy(payload, pVariant->pz, pVariant->nLen); diff --git a/tests/develop-test/0-others/json_tag.py b/tests/develop-test/0-others/json_tag.py index b6a15ca770ea7e4885973a03bfc8e3bd08c3f54d..1271e8ae4a04e28bbe2d2f701524a3d42b1d0e61 100644 --- a/tests/develop-test/0-others/json_tag.py +++ b/tests/develop-test/0-others/json_tag.py @@ -22,9 +22,9 @@ import json class TDTestCase: def caseDescription(self): ''' - Json tag test case, include create table with json tag, - select json tag and query with json tag in where condition, - besides, include json tag in group by/order by/join/subquery. + Json tag test case, include create table with json tag, select json tag and query with json tag in where condition, besides, include json tag in group by/order by/join/subquery. + case1: [TD-12452] fix error if json tag is NULL + case2: [TD-12389] describe child table, tag length error if the tag is json tag ''' return @@ -515,6 +515,23 @@ class TDTestCase: tdSql.query("select jtag->'tag3' from jsons1_16") tdSql.checkData(0, 0, '-2.111000000') + # test TD-12452 + tdSql.execute("ALTER TABLE jsons1_1 SET TAG jtag=NULL") + tdSql.query("select jtag from jsons1_1") + tdSql.checkData(0, 0, None) + tdSql.execute("CREATE TABLE if not exists jsons1_20 using jsons1 tags(NULL)") + tdSql.query("select jtag from jsons1_20") + tdSql.checkData(0, 0, None) + tdSql.execute("insert into jsons1_21 using jsons1 tags(NULL) values(1591061628000, 11, false, '你就会','')") + tdSql.query("select jtag from jsons1_21") + tdSql.checkData(0, 0, None) + + #test TD-12389 + tdSql.query("describe jsons1") + tdSql.checkData(5, 2, 4096) + tdSql.query("describe jsons1_1") + tdSql.checkData(5, 2, 4096) + def stop(self): tdSql.close() tdLog.success("%s successfully executed" % __file__)