From 0304bd03c4471e39802c9cd8ef5f67490144e2ec Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Thu, 13 Jan 2022 17:27:33 +0800 Subject: [PATCH] [TD-12252](query):fix error in escape in like operator --- src/util/src/tcompare.c | 42 +++++++++++++++++++++++++--- tests/develop-test/2-query/escape.py | 6 ++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/util/src/tcompare.c b/src/util/src/tcompare.c index 266d55fc4f..24263f61b5 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 ab023a839e..2ff3c0b7cf 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'") -- GitLab