Skip to content

Commit d5856a6

Browse files
authored
parser: also reduce literal list with charset (pingcap#61484) (pingcap#62185)
close pingcap#58447
1 parent d12a12e commit d5856a6

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

pkg/parser/digester.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,16 @@ func (d *sqlDigester) reduceLit(currTok *token, redact string, forBinding bool,
370370
return
371371
}
372372

373-
// Aggressive reduce lists.
373+
// "_charset ?, _charset ?," => "..."
374374
last4 := d.tokens.back(4)
375+
if toPop := d.isGenericListWithCharset(last4); toPop != 0 {
376+
d.tokens.popBack(toPop)
377+
currTok.tok = genericSymbolList
378+
currTok.lit = "..."
379+
return
380+
}
381+
382+
// Aggressive reduce lists.
375383
if d.isGenericLists(last4) {
376384
d.tokens.popBack(4)
377385
currTok.tok = genericSymbolList
@@ -549,6 +557,27 @@ func (d *sqlDigester) isGenericList(last2 []token) (generic bool) {
549557
return
550558
}
551559

560+
func (d *sqlDigester) isGenericListWithCharset(last []token) int {
561+
if len(last) < 3 {
562+
return 0
563+
}
564+
toPop := 0
565+
if len(last) >= 4 {
566+
// elminate the first _charset
567+
if last[0].tok == underscoreCS {
568+
toPop = 1
569+
}
570+
last = last[1:]
571+
}
572+
if last[2].tok != underscoreCS {
573+
return 0
574+
}
575+
if !d.isGenericList(last[:2]) {
576+
return 0
577+
}
578+
return toPop + 3
579+
}
580+
552581
func (d *sqlDigester) isOrderOrGroupBy() (orderOrGroupBy bool) {
553582
var (
554583
last []token

pkg/parser/digester_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ func TestNormalize(t *testing.T) {
3030
}{
3131
// Generic normalization rules
3232
{"select _utf8mb4'123'", "select (_charset) ?"},
33+
{"select * from b where id in (_utf8mb4'123')", "select * from `b` where `id` in ( (_charset) ? )"},
34+
{"select * from b where id in (_utf8mb4'123', _binary'34')", "select * from `b` where `id` in ( ... )"},
35+
{"select * from b where id in (_utf8mb4'123', _binary'34', _binary'56')", "select * from `b` where `id` in ( ... )"},
3336
{"SELECT 1", "select ?"},
3437
{"select null", "select ?"},
3538
{"select \\N", "select ?"},

0 commit comments

Comments
 (0)