From 3de234a23e185a333ce5e654de8a7174535917b7 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Tue, 21 Sep 2021 16:49:51 +0800 Subject: [PATCH] TD-6129 add json type check --- src/client/src/tscParseInsert.c | 5 +++++ src/client/src/tscSQLParser.c | 11 +++++++++++ src/client/src/tscUtil.c | 3 +-- tests/pytest/stable/json_tag.py | 10 ++++++++-- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/client/src/tscParseInsert.c b/src/client/src/tscParseInsert.c index 89647324b9..3b4a06fe4c 100644 --- a/src/client/src/tscParseInsert.c +++ b/src/client/src/tscParseInsert.c @@ -1084,6 +1084,11 @@ static int32_t tscCheckIfCreateTable(char **sqlstr, SSqlObj *pSql, char** boundC if(spd.numOfBound == 1 && pTagSchema[spd.boundedColumns[0]].type == TSDB_DATA_TYPE_JSON){ char tmp = sToken.z[sToken.n]; sToken.z[sToken.n] = 0; + if(sToken.type != TK_STRING) { + tdDestroyKVRowBuilder(&kvRowBuilder); + tscDestroyBoundColumnInfo(&spd); + return tscSQLSyntaxErrMsg(pInsertParam->msg, "json type error, should be string", NULL); + } code = parseJsontoTagData(sToken.z, &kvRowBuilder, pInsertParam->msg, pTagSchema[spd.boundedColumns[0]].colId); if (code != TSDB_CODE_SUCCESS) { tdDestroyKVRowBuilder(&kvRowBuilder); diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 180e3d7443..60fc6c2ef4 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -5867,6 +5867,8 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { const char* msg23 = "only column length coulbe be modified"; const char* msg24 = "invalid binary/nchar column length"; + const char* msg25 = "json type error, should be string"; + int32_t code = TSDB_CODE_SUCCESS; SSqlCmd* pCmd = &pSql->cmd; @@ -6070,6 +6072,10 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { if (tdInitKVRowBuilder(&kvRowBuilder) < 0) { return TSDB_CODE_TSC_OUT_OF_MEMORY; } + if(pItem->pVar.nType != TSDB_DATA_TYPE_BINARY){ + tscError("json type error, should be string"); + return invalidOperationMsg(pMsg, "json type error, should be string"); + } code = parseJsontoTagData(pItem->pVar.pz, &kvRowBuilder, pMsg, pTagsSchema->colId); if (code != TSDB_CODE_SUCCESS) { tdDestroyKVRowBuilder(&kvRowBuilder); @@ -7492,6 +7498,7 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { const char* msg3 = "tag value too long"; const char* msg4 = "illegal value or data overflow"; const char* msg5 = "tags number not matched"; + const char* msg6 = "json type error, should be string"; SSqlCmd* pCmd = &pSql->cmd; @@ -7689,6 +7696,10 @@ 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){ + tscError("json type error, should be string"); + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "json type error, should be string"); + } 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 c21b326476..0f9e33991d 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -5234,11 +5234,10 @@ end: } int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, int16_t startColId){ - cJSON *root = cJSON_Parse(json); + cJSON *root = cJSON_Parse(json->pz); if (root == NULL){ tscError("json parse error"); return tscSQLSyntaxErrMsg(errMsg, "json parse error", NULL); - } int retCode = 0; diff --git a/tests/pytest/stable/json_tag.py b/tests/pytest/stable/json_tag.py index 2d67b486a5..edd6649f29 100644 --- a/tests/pytest/stable/json_tag.py +++ b/tests/pytest/stable/json_tag.py @@ -49,10 +49,16 @@ class TDTestCase: print("==============step3") tdLog.info("alter stable add tag") tdSql.execute( - "ALTER STABLE db_json_tag_test.jsons1 add COLUMN tag2 nchar(20)") + "ALTER STABLE db_json_tag_test.jsons1 add tag tag2 nchar(20)") tdSql.execute( - "ALTER STABLE db_json_tag_test.jsons1 drop COLUMN jtag") + "ALTER STABLE db_json_tag_test.jsons1 drop tag jtag") + + tdSql.execute( + "ALTER TABLE db_json_tag_test.jsons1_1 SET TAG jtag=4") + + tdSql.execute( + "ALTER TABLE db_json_tag_test.jsons1_1 SET TAG jtag='{\"sex\":\"femail\",\"age\":35}'") def stop(self): tdSql.close() -- GitLab