Skip to content

Commit

Permalink
executor, session: fix ExecStmt.OriginText to use the actual original…
Browse files Browse the repository at this point in the history
… text (#57393)

close #57396
  • Loading branch information
b6g authored Nov 20, 2024
1 parent 9530fdc commit 5450000
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func (a *ExecStmt) PointGet(ctx context.Context) (*recordSet, error) {

// OriginText returns original statement as a string.
func (a *ExecStmt) OriginText() string {
return a.Text
return a.StmtNode.OriginalText()
}

// IsPrepared returns true if stmt is a prepare statement.
Expand Down
11 changes: 9 additions & 2 deletions pkg/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ import (
"github.com/tikv/client-go/v2/oracle"
tikvutil "github.com/tikv/client-go/v2/util"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func init() {
Expand Down Expand Up @@ -4132,7 +4133,8 @@ func logGeneralQuery(execStmt *executor.ExecStmt, s *session, isPrepared bool) {
if vars.EnableRedactLog != errors.RedactLogEnable {
query += redact.String(vars.EnableRedactLog, vars.PlanCacheParams.String())
}
logutil.GeneralLogger.Info("GENERAL_LOG",

fields := []zapcore.Field{
zap.Uint64("conn", vars.ConnectionID),
zap.String("session_alias", vars.SessionAlias),
zap.String("user", vars.User.LoginString()),
Expand All @@ -4143,7 +4145,12 @@ func logGeneralQuery(execStmt *executor.ExecStmt, s *session, isPrepared bool) {
zap.String("currentDB", vars.CurrentDB),
zap.Bool("isPessimistic", vars.TxnCtx.IsPessimistic),
zap.String("sessionTxnMode", vars.GetReadableTxnMode()),
zap.String("sql", query))
zap.String("sql", query),
}
if ot := execStmt.OriginText(); ot != execStmt.Text {
fields = append(fields, zap.String("originText", strconv.Quote(ot)))
}
logutil.GeneralLogger.Info("GENERAL_LOG", fields...)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/session/test/variable/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ go_test(
"variable_test.go",
],
flaky = True,
shard_count = 11,
shard_count = 12,
deps = [
"//pkg/config",
"//pkg/kv",
Expand Down
39 changes: 39 additions & 0 deletions pkg/session/test/variable/variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package variable
import (
"context"
"fmt"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -441,3 +442,41 @@ func TestGeneralLogNonzeroTxnStartTS(t *testing.T) {
require.True(t, ts > 0)
require.True(t, ok)
}

func TestGeneralLogBinaryText(t *testing.T) {
oldGL := logutil.GeneralLogger
mzc := mockZapCore{Core: zapcore.NewNopCore()}
logutil.GeneralLogger = zap.New(&mzc)
defer func() { logutil.GeneralLogger = oldGL }()

store := testkit.CreateMockStore(t)

b := []byte{0x41, 0xf6, 0xec, 0x9a}
tk := testkit.NewTestKit(t, store)
tk.MustExec("set session tidb_general_log = 1")
tk.MustExec("select * /*+ no_quoted */ from mysql.user")
sqlBinary := fmt.Sprintf("select * /*+ yes_quoted */ from mysql.user where User = _binary '%s'", b)
tk.MustExec(sqlBinary)

getSQLFields := func(s string) (sql zapcore.Field, originText zapcore.Field, ok bool) {
for _, fields := range mzc.fields {
if sql, ok := fields["sql"]; ok && strings.Contains(sql.String, s) {
return sql, fields["originText"], true
}
}
return zapcore.Field{}, zapcore.Field{}, false
}

sql, originText, ok := getSQLFields("no_quoted")
require.True(t, ok)
require.NotEmpty(t, sql.String)
require.Empty(t, originText.String)

sql, originText, ok = getSQLFields("yes_quote")
require.True(t, ok)
require.NotEmpty(t, sql.String)
require.NotEmpty(t, originText.String)
ot, err := strconv.Unquote(originText.String)
require.NoError(t, err)
require.Equal(t, sqlBinary, ot)
}

0 comments on commit 5450000

Please sign in to comment.