From eb13ade6901330bab10fae84771a5abdf60f02f4 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Fri, 1 Oct 2021 09:46:54 +0800 Subject: [PATCH] TD-6129 add tag?'key' in where logic --- src/client/src/tscParseInsert.c | 2 +- src/client/src/tscSQLParser.c | 3 ++- src/inc/taoserror.h | 1 + src/query/src/qFilter.c | 3 +++ src/util/src/terror.c | 2 ++ tests/pytest/stable/json_tag.py | 8 ++++++++ 6 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/client/src/tscParseInsert.c b/src/client/src/tscParseInsert.c index 9bc011c895..00bc928a3d 100644 --- a/src/client/src/tscParseInsert.c +++ b/src/client/src/tscParseInsert.c @@ -1117,7 +1117,7 @@ static int32_t tscCheckIfCreateTable(char **sqlstr, SSqlObj *pSql, char** boundC char tmp[128]= {0}; sprintf(tmp, "tag value is too small, can not contain encoded json tag:%d|%d", kvRowLen(row), pTagSchema[spd.boundedColumns[0]].bytes); tscDestroyBoundColumnInfo(&spd); - return tscSQLSyntaxErrMsg(pInsertParam->msg, tmp, NULL); + return tscInvalidOperationMsg(pInsertParam->msg, tmp, NULL); } } tscDestroyBoundColumnInfo(&spd); diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index a1b9563b17..e29546ed61 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -4453,6 +4453,7 @@ static int32_t validateMatchExpr(tSqlExpr* pExpr, STableMeta* pTableMeta, int32_ const char* msg1 = "regular expression string should be less than %d characters"; const char* msg2 = "illegal column type for match/nmatch"; const char* msg3 = "invalid regular expression"; + const char* msg4 = "json column type must encode by binary for match/nmatch"; tSqlExpr* pLeft = pExpr->pLeft; tSqlExpr* pRight = pExpr->pRight; @@ -4471,7 +4472,7 @@ static int32_t validateMatchExpr(tSqlExpr* pExpr, STableMeta* pTableMeta, int32_ } if(pLeft->tokenId == TK_ARROW && pSchema[index].type == TSDB_DATA_TYPE_JSON && !JSON_TYPE_BINARY){ - return invalidOperationMsg(msgBuf, msg2); + return invalidOperationMsg(msgBuf, msg4); } if (!(pRight->type == SQL_NODE_VALUE && pRight->value.nType == TSDB_DATA_TYPE_BINARY)) { diff --git a/src/inc/taoserror.h b/src/inc/taoserror.h index 536e63b741..e4eee7b6d8 100644 --- a/src/inc/taoserror.h +++ b/src/inc/taoserror.h @@ -290,6 +290,7 @@ int32_t* taosGetErrno(); #define TSDB_CODE_QRY_SYS_ERROR TAOS_DEF_ERROR_CODE(0, 0x070E) //"System error") #define TSDB_CODE_QRY_JSON_KEY_NOT_EXIST TAOS_DEF_ERROR_CODE(0, 0x070F) //"json tag key not exist") #define TSDB_CODE_QRY_JSON_KEY_TYPE_ERROR TAOS_DEF_ERROR_CODE(0, 0x0710) //"json tag key type not match") +#define TSDB_CODE_QRY_JSON_KEY_NOT_STR_ERROR TAOS_DEF_ERROR_CODE(0, 0x0711) //"json tag key must be string in match/nmatch") // grant #define TSDB_CODE_GRANT_EXPIRED TAOS_DEF_ERROR_CODE(0, 0x0800) //"License expired") diff --git a/src/query/src/qFilter.c b/src/query/src/qFilter.c index 90e063387a..a57637e953 100644 --- a/src/query/src/qFilter.c +++ b/src/query/src/qFilter.c @@ -3213,6 +3213,9 @@ int filterJsonTypeConvert(SFilterInfo* info) { SFilterField *colLeft = FILTER_UNIT_LEFT_FIELD(info, &info->units[i]); info->units[i].compare.type = FILTER_GET_COL_FIELD_TYPE(colLeft); + if((info->units[i].compare.optr == TSDB_RELATION_MATCH || info->units[i].compare.optr == TSDB_RELATION_NMATCH) + && info->units[i].compare.type != TSDB_DATA_TYPE_BINARY) + return TSDB_CODE_QRY_JSON_KEY_NOT_STR_ERROR; // SFilterField *colRight = FILTER_UNIT_RIGHT_FIELD(info, &info->units[i]); // tVariant* var = colRight->desc; // if(!tVariantTypeMatch(var, info->units[i].compare.type)) diff --git a/src/util/src/terror.c b/src/util/src/terror.c index 20da087961..f5a7ab5f15 100644 --- a/src/util/src/terror.c +++ b/src/util/src/terror.c @@ -296,6 +296,8 @@ TAOS_DEFINE_ERROR(TSDB_CODE_QRY_INVALID_TIME_CONDITION, "One valid time range TAOS_DEFINE_ERROR(TSDB_CODE_QRY_SYS_ERROR, "System error") TAOS_DEFINE_ERROR(TSDB_CODE_QRY_JSON_KEY_NOT_EXIST, "json tag key not exist") TAOS_DEFINE_ERROR(TSDB_CODE_QRY_JSON_KEY_TYPE_ERROR, "json tag key type not match") +TAOS_DEFINE_ERROR(TSDB_CODE_QRY_JSON_KEY_NOT_STR_ERROR, "json tag key must be string in match/nmatch") + // grant TAOS_DEFINE_ERROR(TSDB_CODE_GRANT_EXPIRED, "License expired") TAOS_DEFINE_ERROR(TSDB_CODE_GRANT_DNODE_LIMITED, "DNode creation limited by licence") diff --git a/tests/pytest/stable/json_tag.py b/tests/pytest/stable/json_tag.py index 587a70d978..bac92e0f1d 100644 --- a/tests/pytest/stable/json_tag.py +++ b/tests/pytest/stable/json_tag.py @@ -154,7 +154,15 @@ class TDTestCase: tdSql.query("select * from db_json_tag_test.jsons1 where jtag->'location' in ('shanghai') and jtag->'class'=55") tdSql.checkRows(1) + # test where condition match + tdSql.query("select * from db_json_tag_test.jsons1 where jtag->'location' match 'jin$'") + tdSql.checkRows(0) + + tdSql.query("select * from db_json_tag_test.jsons1 where jtag->'location' match 'jin'") + tdSql.checkRows(2) + tdSql.error("select * from db_json_tag_test.jsons1 where jtag->'num' match '5'") + def stop(self): tdSql.close() tdLog.success("%s successfully executed" % __file__) -- GitLab