From 34ccced6cd5a523b8b30e51298acae32c0a918e7 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Tue, 11 Jan 2022 18:40:46 +0800 Subject: [PATCH] [TD-12252](connector,query,insert,other,tools,taosAdapter):deal with escape character --- src/client/src/tscSQLParser.c | 14 +---- tests/develop-test/2-query/escape.py | 77 ++++++++++++++++++++++------ tests/pytest/query/queryRegex.py | 4 +- 3 files changed, 65 insertions(+), 30 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 5031b5bc84..d881eea516 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -5046,20 +5046,10 @@ static int32_t validateMatchExpr(tSqlExpr* pExpr, STableMeta* pTableMeta, int32_ regex_t regex; char regErrBuf[256] = {0}; - //remove the quote at the begin end of original sql string. - uint32_t lenPattern = pRight->exprToken.n - 2; - char* pattern = malloc(lenPattern + 1); - strncpy(pattern, pRight->exprToken.z+1, lenPattern); - pattern[lenPattern] = '\0'; - - tfree(pRight->value.pz); - pRight->value.pz = pattern; - pRight->value.nLen = lenPattern; - int cflags = REG_EXTENDED; - if ((errCode = regcomp(®ex, pattern, cflags)) != 0) { + if ((errCode = regcomp(®ex, pRight->value.pz, cflags)) != 0) { regerror(errCode, ®ex, regErrBuf, sizeof(regErrBuf)); - tscError("Failed to compile regex pattern %s. reason %s", pattern, regErrBuf); + tscError("Failed to compile regex pattern %s. reason %s", pRight->value.pz, regErrBuf); return invalidOperationMsg(msgBuf, msg3); } regfree(®ex); diff --git a/tests/develop-test/2-query/escape.py b/tests/develop-test/2-query/escape.py index 3737d5c264..ab023a839e 100644 --- a/tests/develop-test/2-query/escape.py +++ b/tests/develop-test/2-query/escape.py @@ -23,7 +23,6 @@ class TDTestCase: case1: [TD-12251] json type containing single quotes cannot be inserted case2: [TD-12334] '\' escape unknown case3: [TD-11071] escape table creation problem - case4: [TD-6232] fix abnormal escaping results about '\' case5: [TD-12815] like wildcards (% _) are not supported nchar type ''' return @@ -61,6 +60,10 @@ class TDTestCase: tdSql.query(r'show tables like "zz\\ "') tdSql.checkRows(1) + tdSql.execute(r"insert into `zz\\ ` values(1591060658000, 1)") + tdSql.query(r'select * from `zz\\ `') + tdSql.checkRows(1) + # [TD-11071] tdSql.execute('create table es (ts timestamp, s int) tags(j int)') tdSql.execute(r'create table `zz\t` using es tags(11)') @@ -74,49 +77,91 @@ class TDTestCase: tdSql.checkData(3, 0, r' ') # [TD-6232] - tdSql.execute('create table tt(ts timestamp, i nchar(128))') + tdSql.execute(r'create table tt(ts timestamp, `i\t` nchar(128))') tdSql.execute(r"insert into tt values(1591060628000, '\t')") tdSql.execute(r"insert into tt values(1591060638000, '\n')") tdSql.execute(r"insert into tt values(1591060648000, '\r')") - tdSql.execute(r"insert into tt values(1591060658000, '\\')") + tdSql.execute(r"insert into tt values(1591060658000, '\\t')") tdSql.execute(r"insert into tt values(1591060668000, '\"')") tdSql.execute(r"insert into tt values(1591060678000, '\'')") tdSql.execute(r"insert into tt values(1591060688000, '\%')") + tdSql.execute(r"insert into tt values(1591060688100, '\\%')") + tdSql.execute(r"insert into tt values(1591060688200, '\\\%')") tdSql.execute(r"insert into tt values(1591060698000, '\_')") tdSql.execute(r"insert into tt values(1591060708000, '\9')") - tdSql.query(r"select * from tt where i='\t'") + tdSql.query(r"select * from tt where `i\t`='\t'") tdSql.checkRows(1) - tdSql.query(r"select * from tt where i='\n'") + tdSql.query(r"select * from tt where `i\t`='\n'") tdSql.checkRows(1) - tdSql.query(r"select * from tt where i='\r'") + tdSql.query(r"select * from tt where `i\t`='\r'") tdSql.checkRows(1) - tdSql.query(r"select * from tt where i='\\'") + tdSql.checkData(0, 1, '\r') + tdSql.query(r"select * from tt where `i\t`='\\t'") tdSql.checkRows(1) - tdSql.query(r"select * from tt where i='\"'") + tdSql.checkData(0, 1, r'\t') + tdSql.query(r"select * from tt where `i\t`='\"'") tdSql.checkRows(1) - tdSql.query(r"select * from tt where i='\''") + tdSql.query(r"select * from tt where `i\t`='\''") tdSql.checkRows(1) - tdSql.query(r"select * from tt where i='\%'") + tdSql.query(r"select * from tt where `i\t`='\%'") + tdSql.checkRows(2) + tdSql.checkData(0, 1, r'\%') + tdSql.query(r"select * from tt where `i\t`='\\%'") + tdSql.checkRows(2) + tdSql.checkData(0, 1, r'\%') + tdSql.query(r"select * from tt where `i\t`='\\\%'") tdSql.checkRows(1) - tdSql.query(r"select * from tt where i='\_'") + tdSql.checkData(0, 1, r'\\%') + tdSql.query(r"select * from tt where `i\t`='\_'") tdSql.checkRows(1) - tdSql.query(r"select * from tt where i='\9'") + tdSql.checkData(0, 1, r'\_') + tdSql.query(r"select * from tt where `i\t`='\9'") tdSql.checkRows(1) - tdSql.query(r"select * from tt where i='9'") + tdSql.query(r"select * from tt where `i\t`='9'") tdSql.checkRows(1) + tdSql.execute(r'create table tb(ts timestamp, `i\t` binary(128))') + tdSql.execute(r"insert into tb values(1591060628000, '\t')") + tdSql.query(r"select * from tb where `i\t`='\t'") + tdSql.checkRows(1) + tdSql.execute(r"insert into tb values(1591060629000, '\\%')") + tdSql.query(r"select * from tb where `i\t`='\%'") + tdSql.checkRows(1) + tdSql.checkData(0, 1, r'\%') + # [TD-12815] like wildcard(%, _) are not supported nchar tdSql.execute(r"insert into tt values(1591070708000, 'h%d')") tdSql.execute(r"insert into tt values(1591080708000, 'h_j')") - tdSql.query(r"select * from tt where i like 'h\%d'") + tdSql.execute(r"insert into tt values(1591090708000, 'h\\j')") + tdSql.query(r"select * from tt where `i\t` like 'h\%d'") + tdSql.checkRows(1) + tdSql.query(r"select * from tt where `i\t` like 'h\_j'") + tdSql.checkRows(1) + tdSql.query(r"select * from tt where `i\t` like 'h\\j'") tdSql.checkRows(1) - tdSql.query(r"select * from tt where i like 'h\_j'") + tdSql.query(r"select * from tt where `i\t` match 'h\\\\j'") tdSql.checkRows(1) + + # normal test + tdSql.error(r"select * from tt where i\t='\t'") + tdSql.error(r"select * from zz\t where s=1") + tdSql.error(r"select i\t from tt where `i\t`='\t'") + + tdSql.execute(r'create table `\n`(ts timestamp, `i\"` nchar(128))') + tdSql.execute(r"insert into `\n` values(1591060708000, 'js')") + tdSql.query(r"select `i\"` from `\n` where `i\"`='js'") + tdSql.checkRows(1) + tdSql.checkData(0, 0, 'js') + + tdSql.query(r'show tables like "\\n"') + tdSql.checkRows(1) + tdSql.checkData(0, 0, r"\n") + def stop(self): tdSql.close() tdLog.success("%s successfully executed" % __file__) tdCases.addWindows(__file__, TDTestCase()) -tdCases.addLinux(__file__, TDTestCase()) \ No newline at end of file +tdCases.addLinux(__file__, TDTestCase()) diff --git a/tests/pytest/query/queryRegex.py b/tests/pytest/query/queryRegex.py index 9edc1db60d..977155bfe4 100644 --- a/tests/pytest/query/queryRegex.py +++ b/tests/pytest/query/queryRegex.py @@ -92,10 +92,10 @@ class TDTestCase: tdSql.error('select * from stb_test where c0 nmatch abc') - tdSql.query("select * from stb_1 where c0 match '\\\\'") + tdSql.query(r"select * from stb_1 where c0 match '\\\\'") tdSql.checkRows(1) - tdSql.query("select * from stb_1 where c0 nmatch '\\\\'") + tdSql.query(r"select * from stb_1 where c0 nmatch '\\\\'") tdSql.checkRows(3) #2021-10-20 for https://jira.taosdata.com:18080/browse/TD-10708 -- GitLab