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
  • Loading branch information
b6g committed Nov 15, 2024
1 parent ef8cac2 commit 5c00e43
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -2067,7 +2067,7 @@ func (a *ExecStmt) GetTextToLog(keepHint bool) string {
} else if sensitiveStmt, ok := a.StmtNode.(ast.SensitiveStmtNode); ok {
sql = sensitiveStmt.SecureText()
} else {
sql = redact.String(rmode, sessVars.StmtCtx.OriginalSQL+sessVars.PlanCacheParams.String())
sql = redact.String(rmode, a.OriginText()+sessVars.PlanCacheParams.String())
}
return sql
}
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 @@ -4134,7 +4135,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 @@ -4145,7 +4147,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 execStmt.OriginText() != execStmt.Text {
fields = append(fields, zap.String("sqlQuoted", strconv.Quote(query)))
}
logutil.GeneralLogger.Info("GENERAL_LOG", fields...)
}
}

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")
tk.MustExec(fmt.Sprintf("select * /*+ yes_quoted */ from mysql.user where User = _binary '%s'", b))

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

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

sql, sqlQuoted, ok = getSQLFields("yes_quote")
require.True(t, ok)
require.NotEmpty(t, sql.String)
require.True(t, strings.Contains(sql.String, string(b)))
require.NotEmpty(t, sqlQuoted.String)
s, err := strconv.Unquote(sqlQuoted.String)
require.NoError(t, err)
require.Equal(t, sql.String, s)
}

0 comments on commit 5c00e43

Please sign in to comment.