Repository: pingcap/tidb
File: pkg/executor/adapter.go (Go)

Code before:
	}
	return
}

// updatePrevStmt is equivalent to `sessVars.PrevStmt = FormatSQL(a.GetTextToLog(false))`
func (a *ExecStmt) updatePrevStmt() {
	sessVars := a.Ctx.GetSessionVars()
	if sessVars.PrevStmt == nil {
		sessVars.PrevStmt = &variable.LazyStmtText{Format: formatSQL}
	}
	rmode := sessVars.EnableRedactLog
	if rmode == errors.RedactLogEnable {
		sql, _ := sessVars.StmtCtx.SQLDigest()
		sessVars.PrevStmt.SetText(sql)
	} else if sensitiveStmt, ok := a.StmtNode.(ast.SensitiveStmtNode); ok {
		sql := sensitiveStmt.SecureText()
		sessVars.PrevStmt.SetText(sql)
	} else {
		sessVars.PrevStmt.Update(rmode, sessVars.StmtCtx.OriginalSQL, sessVars.PlanCacheParams)
	}
}

func (a *ExecStmt) observeStmtBeginForTopProfiling(ctx context.Context) context.Context {
	if !topsqlstate.TopProfilingEnabled() && IsFastPlan(a.Plan) {
		// To reduce the performance impact on fast plan.
		// Drop them does not cause notable accuracy issue in TopSQL.
		return ctx
	}

	vars := a.Ctx.GetSessionVars()
	sc := vars.StmtCtx
	normalizedSQL, sqlDigest := sc.SQLDigest()
	normalizedPlan, planDigest := GetPlanDigest(sc)
	var sqlDigestByte, planDigestByte []byte
	if sqlDigest != nil {
		sqlDigestByte = sqlDigest.Bytes()
	}
	if planDigest != nil {
		planDigestByte = planDigest.Bytes()
	}
	stats := a.Ctx.GetStmtStats()
	if !topsqlstate.TopProfilingEnabled() {
		// Always attach the SQL and plan info uses to catch the running SQL when Top Profiling is enabled in execution.
		if stats != nil {
			stats.OnExecutionBegin(sqlDigestByte, planDigestByte, vars.InPacketBytes.Load())
		}
		return topsql.AttachSQLAndPlanInfo(ctx, sqlDigest, planDigest)
	}

	if stats != nil {
		stats.OnExecutionBegin(sqlDigestByte, planDigestByte, vars.InPacketBytes.Load())

Review comment (refactor):
The comment mentions "TopSQL" but should be updated to "Top Profiling" for consistency with the function name change from `observeStmtBeginForTopSQL` to `observeStmtBeginForTopProfiling` and the overall refactoring goal.
```suggestion
		// Drop them does not cause notable accuracy issue in Top Profiling.
```

Code after:
	}
	return
}

// updatePrevStmt is equivalent to `sessVars.PrevStmt = FormatSQL(a.GetTextToLog(false))`
func (a *ExecStmt) updatePrevStmt() {
	sessVars := a.Ctx.GetSessionVars()
	if sessVars.PrevStmt == nil {
		sessVars.PrevStmt = &variable.LazyStmtText{Format: formatSQL}
	}
	rmode := sessVars.EnableRedactLog
	if rmode == errors.RedactLogEnable {
		sql, _ := sessVars.StmtCtx.SQLDigest()
		sessVars.PrevStmt.SetText(sql)
	} else if sensitiveStmt, ok := a.StmtNode.(ast.SensitiveStmtNode); ok {
		sql := sensitiveStmt.SecureText()
		sessVars.PrevStmt.SetText(sql)
	} else {
		sessVars.PrevStmt.Update(rmode, sessVars.StmtCtx.OriginalSQL, sessVars.PlanCacheParams)
	}
}

func (a *ExecStmt) observeStmtBeginForTopProfiling(ctx context.Context) context.Context {
	if !topsqlstate.TopProfilingEnabled() && IsFastPlan(a.Plan) {
		// To reduce the performance impact on fast plan.
		// Drop them does not cause notable accuracy issue in Top Profiling.
		return ctx
	}

	vars := a.Ctx.GetSessionVars()
	sc := vars.StmtCtx
	normalizedSQL, sqlDigest := sc.SQLDigest()
	normalizedPlan, planDigest := GetPlanDigest(sc)
	var sqlDigestByte, planDigestByte []byte
	if sqlDigest != nil {
		sqlDigestByte = sqlDigest.Bytes()
	}
	if planDigest != nil {
		planDigestByte = planDigest.Bytes()
	}
	stats := a.Ctx.GetStmtStats()
	if !topsqlstate.TopProfilingEnabled() {
		// Always attach the SQL and plan info uses to catch the running SQL when Top Profiling is enabled in execution.
		// Note: Goroutine labels for CPU profiling are only set when TopSQL is enabled.
		if stats != nil {
			stats.OnExecutionBegin(sqlDigestByte, planDigestByte, vars.InPacketBytes.Load())
		}
		return topsql.AttachSQLAndPlanInfo(ctx, sqlDigest, planDigest)
	}

	if stats != nil {