Skip to content

Commit

Permalink
executor: avoid using fmt.Sprint to truncate the string (#44540) (#44557
Browse files Browse the repository at this point in the history
)

close #44542
  • Loading branch information
ti-chi-bot authored Jun 28, 2023
1 parent 464dc82 commit 4f7f3ab
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
6 changes: 5 additions & 1 deletion executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion executor/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
5 changes: 4 additions & 1 deletion util/stmtsummary/statement_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 4f7f3ab

Please sign in to comment.