diff --git a/session/session.go b/session/session.go index fc9e1a49b99a3..a35363bfdae77 100644 --- a/session/session.go +++ b/session/session.go @@ -1104,7 +1104,12 @@ func (s *session) ParseSQL(ctx context.Context, sql, charset, collation string) defer span1.Finish() } defer trace.StartRegion(ctx, "ParseSQL").End() - s.parser.SetSQLMode(s.sessionVars.SQLMode) + + sqlMode := s.sessionVars.SQLMode + if s.isInternal() { + sqlMode = sqlMode & (^mysql.ModeNoBackslashEscapes) + } + s.parser.SetSQLMode(sqlMode) s.parser.SetParserConfig(s.sessionVars.BuildParserConfig()) return s.parser.Parse(sql, charset, collation) } diff --git a/session/session_test.go b/session/session_test.go index 7fdd46a4a12b9..10b18d718945b 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -4306,3 +4306,35 @@ func (s *testSessionSuite) TestInTxnPSProtoPointGet(c *C) { c.Assert(txn.Valid(), IsTrue) tk.MustExec("commit") } + +func (s *testSessionSuite) TestRandomBinary(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + + ctx := context.Background() + allBytes := [][]byte{ + {4, 0, 0, 0, 0, 0, 0, 4, '2'}, + {4, 0, 0, 0, 0, 0, 0, 4, '.'}, + {4, 0, 0, 0, 0, 0, 0, 4, '*'}, + {4, 0, 0, 0, 0, 0, 0, 4, '('}, + {4, 0, 0, 0, 0, 0, 0, 4, '\''}, + {4, 0, 0, 0, 0, 0, 0, 4, '!'}, + {4, 0, 0, 0, 0, 0, 0, 4, 29}, + {4, 0, 0, 0, 0, 0, 0, 4, 28}, + {4, 0, 0, 0, 0, 0, 0, 4, 23}, + {4, 0, 0, 0, 0, 0, 0, 4, 16}, + } + sql := "insert into mysql.stats_top_n (table_id, is_index, hist_id, value, count) values " + var val string + for i, bytes := range allBytes { + if i == 0 { + val += sqlexec.MustEscapeSQL("(874, 0, 1, %?, 3)", bytes) + } else { + val += sqlexec.MustEscapeSQL(",(874, 0, 1, %?, 3)", bytes) + } + } + sql += val + tk.MustExec("set sql_mode = 'NO_BACKSLASH_ESCAPES';") + _, err := tk.Se.ExecuteInternal(ctx, sql) + c.Assert(err, Equals, nil) +}