Skip to content

Commit ce68c53

Browse files
authored
util, executor: fix infoschema like on table names with $ (#67961)
close #67727
1 parent 49c7713 commit ce68c53

3 files changed

Lines changed: 16 additions & 6 deletions

File tree

pkg/executor/test/infoschema/infoschema_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,9 @@ func TestTablesTable(t *testing.T) {
495495
require.NoError(t, err)
496496
require.Equal(t, 2, selectTables)
497497
require.Equal(t, totalTables, remainTables+selectTables)
498+
499+
tk.MustExec("create table test.`$a$a` (a int)")
500+
tk.MustQuery("select count(*) from information_schema.tables where table_name like '$a$%'").Check(testkit.Rows("1"))
498501
}
499502

500503
func TestColumnTable(t *testing.T) {

pkg/util/stringutil/string_util.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package stringutil
1717
import (
1818
"bytes"
1919
"fmt"
20+
"regexp"
2021
"slices"
2122
"strings"
2223
"unicode/utf8"
@@ -259,18 +260,21 @@ func matchRune(a, b rune) bool {
259260
// CompileLike2Regexp convert a like `lhs` to a regular expression
260261
func CompileLike2Regexp(str string) string {
261262
patChars, patTypes := CompilePattern(str, '\\')
262-
var result []rune
263+
var result strings.Builder
264+
result.Grow(len(patChars)*2 + 2)
265+
result.WriteByte('^')
263266
for i := range patChars {
264267
switch patTypes[i] {
265268
case PatMatch:
266-
result = append(result, patChars[i])
269+
result.WriteString(regexp.QuoteMeta(string(patChars[i])))
267270
case PatOne:
268-
result = append(result, '.')
271+
result.WriteByte('.')
269272
case PatAny:
270-
result = append(result, '.', '*')
273+
result.WriteString(".*")
271274
}
272275
}
273-
return "^" + string(result) + "$"
276+
result.WriteByte('$')
277+
return result.String()
274278
}
275279

276280
// DoMatchBinary is an adapter for `DoMatchInner`, `str` is binary strings or ascii string.

pkg/util/stringutil/string_util_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ func TestCompileLike2Regexp(t *testing.T) {
116116
{``, `^$`},
117117
{`a`, `^a$`},
118118
{`aA`, `^aA$`},
119+
{`$a$%`, `^\$a\$.*$`},
120+
{`a.b%`, `^a\.b.*$`},
121+
{`a+b`, `^a\+b$`},
119122
{`_`, `^.$`},
120123
{`__`, `^..$`},
121124
{`%`, `^.*$`},
@@ -124,7 +127,7 @@ func TestCompileLike2Regexp(t *testing.T) {
124127
{`a%`, `^a.*$`},
125128
{`\%a`, `^%a$`},
126129
{`\_a`, `^_a$`},
127-
{`\\_a`, `^\.a$`},
130+
{`\\_a`, `^\\.a$`},
128131
{`\a\b`, `^ab$`},
129132
{`%%_`, `^..*$`},
130133
{`%_%_aA`, "^...*aA$"},

0 commit comments

Comments
 (0)