diff --git a/src/util/src/tcompare.c b/src/util/src/tcompare.c index 266d55fc4f85a2cda27bc73cefa6560629a93962..24263f61b5ba442d016356ef9c7eca99bd4211a8 100644 --- a/src/util/src/tcompare.c +++ b/src/util/src/tcompare.c @@ -266,6 +266,7 @@ 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 "*" */ @@ -308,13 +309,30 @@ 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; } } + return TSDB_PATTERN_NOMATCH; } @@ -428,8 +446,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'")