From 4f7f3ab4cbef5a4ad45ec5257d50522362529271 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Wed, 28 Jun 2023 08:38:35 +0800 Subject: [PATCH] executor: avoid using fmt.Sprint to truncate the string (#44540) (#44557) close pingcap/tidb#44542 --- executor/adapter.go | 6 +++++- executor/adapter_test.go | 2 +- util/stmtsummary/statement_summary.go | 5 ++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/executor/adapter.go b/executor/adapter.go index 59fe7d30cd38b..33a30af2a2112 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -1199,7 +1199,11 @@ func FormatSQL(sql string) stringutil.StringerFunc { return QueryReplacer.Replace(sql) // no limit } if int32(length) > maxQueryLen { - sql = fmt.Sprintf("%.*q(len:%d)", maxQueryLen, sql, length) + var result strings.Builder + result.Grow(int(maxQueryLen)) + result.WriteString(sql[:maxQueryLen]) + fmt.Fprintf(&result, "(len:%d)", length) + return QueryReplacer.Replace(result.String()) } return QueryReplacer.Replace(sql) } diff --git a/executor/adapter_test.go b/executor/adapter_test.go index 1e28472a72bfb..012075c8e4a0c 100644 --- a/executor/adapter_test.go +++ b/executor/adapter_test.go @@ -50,5 +50,5 @@ func TestFormatSQL(t *testing.T) { require.Equal(t, "aaaaaaaaaaaaaaaaaaaa", val.String()) variable.QueryLogMaxLen.Store(5) val = executor.FormatSQL("aaaaaaaaaaaaaaaaaaaa") - require.Equal(t, "\"aaaaa\"(len:20)", val.String()) + require.Equal(t, "aaaaa(len:20)", val.String()) } diff --git a/util/stmtsummary/statement_summary.go b/util/stmtsummary/statement_summary.go index 6740656c9ca9e..41238e088d68e 100644 --- a/util/stmtsummary/statement_summary.go +++ b/util/stmtsummary/statement_summary.go @@ -908,7 +908,10 @@ func formatSQL(sql string) string { maxSQLLength := StmtSummaryByDigestMap.maxSQLLength() length := len(sql) if length > maxSQLLength { - sql = fmt.Sprintf("%.*s(len:%d)", maxSQLLength, sql, length) + var result strings.Builder + result.WriteString(sql[:maxSQLLength]) + fmt.Fprintf(&result, "(len:%d)", length) + return result.String() } return sql }