diff --git a/src/util/src/tcompare.c b/src/util/src/tcompare.c index b15b1b0632f5a86ed4afa346f77af747bae8ec05..fe4ded7742c4080b1a79a5b80b5ab1a702567ad1 100644 --- a/src/util/src/tcompare.c +++ b/src/util/src/tcompare.c @@ -266,9 +266,10 @@ int patternMatch(const char *patterStr, const char *str, size_t size, const SPat int32_t j = 0; int32_t o = 0; int32_t m = 0; + char escape = '\\'; // "\" while ((c = patterStr[i++]) != 0) { - if (c == pInfo->matchAll) { /* Match "*" */ + if (c == pInfo->matchAll) { while ((c = patterStr[i++]) == pInfo->matchAll || c == pInfo->matchOne) { if (c == pInfo->matchOne) { @@ -308,8 +309,24 @@ int patternMatch(const char *patterStr, const char *str, size_t size, const SPat ++o; if (j <= size) { - if (c == '\\' && patterStr[i] == '_' && c1 == '_') { i++; continue; } - if (c == '\\' && patterStr[i] == '%' && c1 == '%') { i++; continue; } + if (c == escape && patterStr[i] == pInfo->matchOne){ + if(c1 == pInfo->matchOne){ + i++; + continue; + } + else{ + return TSDB_PATTERN_NOMATCH; + } + } + if (c == escape && patterStr[i] == pInfo->matchAll){ + if(c1 == pInfo->matchAll){ + i++; + continue; + } + else{ + return TSDB_PATTERN_NOMATCH; + } + } if (c == c1 || tolower(c) == tolower(c1) || (c == pInfo->matchOne && c1 != 0)) { continue; } @@ -428,8 +445,24 @@ int WCSPatternMatch(const uint32_t *patterStr, const uint32_t *str, size_t size, c1 = str[j++]; if (j <= size) { - if (c == escape && patterStr[i] == matchOne && c1 == matchOne) { i++; continue; } - if (c == escape && patterStr[i] == matchAll && c1 == matchAll) { i++; continue; } + if (c == escape && patterStr[i] == matchOne){ + if(c1 == matchOne){ + i++; + continue; + } + else{ + return TSDB_PATTERN_NOMATCH; + } + } + if (c == escape && patterStr[i] == matchAll){ + if(c1 == matchAll){ + i++; + continue; + } + else{ + return TSDB_PATTERN_NOMATCH; + } + } if (c == c1 || towlower(c) == towlower(c1) || (c == matchOne && c1 != 0)) { continue; } diff --git a/tests/develop-test/2-query/escape.py b/tests/develop-test/2-query/escape.py index ab023a839eaee8217e29c2a488ec7803fb23636f..2ff3c0b7cfcd45d1cf5cf6abe5f4c09cc0956994 100644 --- a/tests/develop-test/2-query/escape.py +++ b/tests/develop-test/2-query/escape.py @@ -131,11 +131,17 @@ class TDTestCase: tdSql.checkData(0, 1, r'\%') # [TD-12815] like wildcard(%, _) are not supported nchar + tdSql.execute(r"insert into tt values(1591050708000, 'h\%d')") tdSql.execute(r"insert into tt values(1591070708000, 'h%d')") + tdSql.execute(r"insert into tt values(1591080808000, 'h\_j')") tdSql.execute(r"insert into tt values(1591080708000, 'h_j')") 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\%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\t` like 'h\\j'")