Skip to content

Commit 6a63d49

Browse files
committed
parser: also reduce literal list with charset
Signed-off-by: xhe <[email protected]>
1 parent 4c9e2ea commit 6a63d49

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
@@ -364,8 +364,16 @@ func (d *sqlDigester) reduceLit(currTok *token, redact string, forBinding bool,
364364
return
365365
}
366366

367-
// Aggressive reduce lists.
367+
// "_charset ?, _charset ?," => "..."
368368
last4 := d.tokens.back(4)
369+
if toPop := d.isGenericListWithCharset(last4); toPop != 0 {
370+
d.tokens.popBack(toPop)
371+
currTok.tok = genericSymbolList
372+
currTok.lit = "..."
373+
return
374+
}
375+
376+
// Aggressive reduce lists.
369377
if d.isGenericLists(last4) {
370378
d.tokens.popBack(4)
371379
currTok.tok = genericSymbolList
@@ -543,6 +551,27 @@ func (d *sqlDigester) isGenericList(last2 []token) (generic bool) {
543551
return
544552
}
545553

554+
func (d *sqlDigester) isGenericListWithCharset(last []token) int {
555+
if len(last) < 3 {
556+
return 0
557+
}
558+
toPop := 0
559+
if len(last) >= 4 {
560+
// elminate the first _charset
561+
if last[0].tok == underscoreCS {
562+
toPop = 1
563+
}
564+
last = last[1:]
565+
}
566+
if last[2].tok != underscoreCS {
567+
return 0
568+
}
569+
if !d.isGenericList(last[:2]) {
570+
return 0
571+
}
572+
return toPop + 3
573+
}
574+
546575
func (d *sqlDigester) isOrderOrGroupBy() (orderOrGroupBy bool) {
547576
var (
548577
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)