Skip to content

Commit 115461e

Browse files
authored
*: support tidb_redact_log for explain (#54553) (#55307)
close #54565
1 parent a64e23d commit 115461e

File tree

87 files changed

+1247
-467
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+1247
-467
lines changed

pkg/executor/aggfuncs/builder.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ func BuildWindowFunctions(ctx sessionctx.Context, windowFuncDesc *aggregation.Ag
9292
case ast.WindowFuncCumeDist:
9393
return buildCumeDist(ordinal, orderByCols)
9494
case ast.WindowFuncNthValue:
95-
return buildNthValue(windowFuncDesc, ordinal)
95+
return buildNthValue(ctx, windowFuncDesc, ordinal)
9696
case ast.WindowFuncNtile:
97-
return buildNtile(windowFuncDesc, ordinal)
97+
return buildNtile(ctx, windowFuncDesc, ordinal)
9898
case ast.WindowFuncPercentRank:
9999
return buildPercentRank(ordinal, orderByCols)
100100
case ast.WindowFuncLead:
@@ -680,22 +680,22 @@ func buildCumeDist(ordinal int, orderByCols []*expression.Column) AggFunc {
680680
return r
681681
}
682682

683-
func buildNthValue(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
683+
func buildNthValue(sctx sessionctx.Context, aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
684684
base := baseAggFunc{
685685
args: aggFuncDesc.Args,
686686
ordinal: ordinal,
687687
}
688688
// Already checked when building the function description.
689-
nth, _, _ := expression.GetUint64FromConstant(aggFuncDesc.Args[1])
689+
nth, _, _ := expression.GetUint64FromConstant(sctx, aggFuncDesc.Args[1])
690690
return &nthValue{baseAggFunc: base, tp: aggFuncDesc.RetTp, nth: nth}
691691
}
692692

693-
func buildNtile(aggFuncDes *aggregation.AggFuncDesc, ordinal int) AggFunc {
693+
func buildNtile(sctx sessionctx.Context, aggFuncDes *aggregation.AggFuncDesc, ordinal int) AggFunc {
694694
base := baseAggFunc{
695695
args: aggFuncDes.Args,
696696
ordinal: ordinal,
697697
}
698-
n, _, _ := expression.GetUint64FromConstant(aggFuncDes.Args[0])
698+
n, _, _ := expression.GetUint64FromConstant(sctx, aggFuncDes.Args[0])
699699
return &ntile{baseAggFunc: base, n: n}
700700
}
701701

@@ -709,7 +709,7 @@ func buildPercentRank(ordinal int, orderByCols []*expression.Column) AggFunc {
709709
func buildLeadLag(ctx sessionctx.Context, aggFuncDesc *aggregation.AggFuncDesc, ordinal int) baseLeadLag {
710710
offset := uint64(1)
711711
if len(aggFuncDesc.Args) >= 2 {
712-
offset, _, _ = expression.GetUint64FromConstant(aggFuncDesc.Args[1])
712+
offset, _, _ = expression.GetUint64FromConstant(ctx, aggFuncDesc.Args[1])
713713
}
714714
var defaultExpr expression.Expression
715715
defaultExpr = expression.NewNull()

pkg/executor/aggfuncs/func_group_concat.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ func (e *baseGroupConcat4String) AppendFinalResult2Chunk(_ sessionctx.Context, p
7373
func (e *baseGroupConcat4String) handleTruncateError(sctx sessionctx.Context) (err error) {
7474
if atomic.CompareAndSwapInt32(e.truncated, 0, 1) {
7575
if !sctx.GetSessionVars().StmtCtx.TruncateAsWarning {
76-
return expression.ErrCutValueGroupConcat.GenWithStackByArgs(e.args[0].String())
76+
return expression.ErrCutValueGroupConcat.GenWithStackByArgs(e.args[0].StringWithCtx(false))
7777
}
78-
sctx.GetSessionVars().StmtCtx.AppendWarning(expression.ErrCutValueGroupConcat.GenWithStackByArgs(e.args[0].String()))
78+
sctx.GetSessionVars().StmtCtx.AppendWarning(expression.ErrCutValueGroupConcat.FastGenByArgs(e.args[0].StringWithCtx(false)))
7979
}
8080
return nil
8181
}

pkg/executor/distsql.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func closeAll(objs ...Closeable) error {
152152
func rebuildIndexRanges(ctx sessionctx.Context, is *plannercore.PhysicalIndexScan, idxCols []*expression.Column, colLens []int) (ranges []*ranger.Range, err error) {
153153
access := make([]expression.Expression, 0, len(is.AccessCondition))
154154
for _, cond := range is.AccessCondition {
155-
newCond, err1 := expression.SubstituteCorCol2Constant(cond)
155+
newCond, err1 := expression.SubstituteCorCol2Constant(ctx, cond)
156156
if err1 != nil {
157157
return nil, err1
158158
}
@@ -341,7 +341,7 @@ func (e *IndexReaderExecutor) open(ctx context.Context, kvRanges []kv.KeyRange)
341341
if err != nil {
342342
return err
343343
}
344-
pbConditions, err := expression.ExpressionsToPBList(e.Ctx().GetSessionVars().StmtCtx, []expression.Expression{inCondition}, e.Ctx().GetClient())
344+
pbConditions, err := expression.ExpressionsToPBList(e.Ctx(), []expression.Expression{inCondition}, e.Ctx().GetClient())
345345
if err != nil {
346346
return err
347347
}

pkg/executor/importer/import.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ func (p *Plan) initParameters(plan *plannercore.ImportInto) error {
756756
optionMap := make(map[string]interface{}, len(plan.Options))
757757
for _, opt := range plan.Options {
758758
if opt.Value != nil {
759-
val := opt.Value.String()
759+
val := opt.Value.StringWithCtx(false)
760760
if opt.Name == cloudStorageURIOption {
761761
val = ast.RedactURL(val)
762762
}

pkg/expression/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ go_library(
105105
"//pkg/util/password-validation",
106106
"//pkg/util/plancodec",
107107
"//pkg/util/printer",
108+
"//pkg/util/redact",
108109
"//pkg/util/sem",
109110
"//pkg/util/set",
110111
"//pkg/util/size",
@@ -117,7 +118,6 @@ go_library(
117118
"@com_github_pingcap_errors//:errors",
118119
"@com_github_pingcap_failpoint//:failpoint",
119120
"@com_github_pingcap_tipb//go-tipb",
120-
"@com_github_pkg_errors//:errors",
121121
"@com_github_tikv_client_go_v2//oracle",
122122
"@org_uber_go_atomic//:atomic",
123123
"@org_uber_go_zap//:zap",

pkg/expression/aggregation/agg_to_pb.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (desc *baseFuncDesc) GetTiPBExpr(tryWindowDesc bool) (tp tipb.ExprType) {
101101

102102
// AggFuncToPBExpr converts aggregate function to pb.
103103
func AggFuncToPBExpr(sctx sessionctx.Context, client kv.Client, aggFunc *AggFuncDesc, storeType kv.StoreType) (*tipb.Expr, error) {
104-
pc := expression.NewPBConverter(client, sctx.GetSessionVars().StmtCtx)
104+
pc := expression.NewPBConverter(client, sctx)
105105
tp := aggFunc.GetTiPBExpr(false)
106106
if !client.IsRequestTypeSupported(kv.ReqTypeSelect, int64(tp)) {
107107
return nil, errors.New("select request is not supported by client")
@@ -111,7 +111,7 @@ func AggFuncToPBExpr(sctx sessionctx.Context, client kv.Client, aggFunc *AggFunc
111111
for _, arg := range aggFunc.Args {
112112
pbArg := pc.ExprToPB(arg)
113113
if pbArg == nil {
114-
return nil, errors.New(aggFunc.String() + " can't be converted to PB.")
114+
return nil, errors.New(aggFunc.StringWithCtx(false) + " can't be converted to PB.")
115115
}
116116
children = append(children, pbArg)
117117
}
@@ -122,11 +122,10 @@ func AggFuncToPBExpr(sctx sessionctx.Context, client kv.Client, aggFunc *AggFunc
122122

123123
if tp == tipb.ExprType_GroupConcat {
124124
orderBy := make([]*tipb.ByItem, 0, len(aggFunc.OrderByItems))
125-
sc := sctx.GetSessionVars().StmtCtx
126125
for _, arg := range aggFunc.OrderByItems {
127-
pbArg := expression.SortByItemToPB(sc, client, arg.Expr, arg.Desc)
126+
pbArg := expression.SortByItemToPB(sctx, client, arg.Expr, arg.Desc)
128127
if pbArg == nil {
129-
return nil, errors.New(aggFunc.String() + " can't be converted to PB.")
128+
return nil, errors.New(aggFunc.StringWithCtx(false) + " can't be converted to PB.")
130129
}
131130
orderBy = append(orderBy, pbArg)
132131
}

pkg/expression/aggregation/base_func.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,15 @@ func (a *baseFuncDesc) clone() *baseFuncDesc {
7373

7474
// String implements the fmt.Stringer interface.
7575
func (a *baseFuncDesc) String() string {
76+
return a.StringWithCtx(false)
77+
}
78+
79+
// StringWithCtx returns the string within given context.
80+
func (a *baseFuncDesc) StringWithCtx(redact bool) string {
7681
buffer := bytes.NewBufferString(a.Name)
7782
buffer.WriteString("(")
7883
for i, arg := range a.Args {
79-
buffer.WriteString(arg.String())
84+
buffer.WriteString(arg.StringWithCtx(redact))
8085
if i+1 != len(a.Args) {
8186
buffer.WriteString(", ")
8287
}
@@ -150,7 +155,7 @@ func (a *baseFuncDesc) typeInfer4ApproxPercentile(ctx sessionctx.Context) error
150155
}
151156
percent, isNull, err := a.Args[1].EvalInt(ctx, chunk.Row{})
152157
if err != nil {
153-
return fmt.Errorf("APPROX_PERCENTILE: Invalid argument %s", a.Args[1].String())
158+
return fmt.Errorf("APPROX_PERCENTILE: Invalid argument %s", a.Args[1].StringWithCtx(false))
154159
}
155160
if percent <= 0 || percent > 100 || isNull {
156161
if isNull {

pkg/expression/aggregation/concat.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (cf *concatFunction) Update(evalCtx *AggEvaluateContext, sc *stmtctx.Statem
104104
}
105105
evalCtx.Buffer.Truncate(i)
106106
if !cf.truncated {
107-
sc.AppendWarning(expression.ErrCutValueGroupConcat.GenWithStackByArgs(cf.Args[0].String()))
107+
sc.AppendWarning(expression.ErrCutValueGroupConcat.FastGenByArgs(cf.Args[0].StringWithCtx(false)))
108108
}
109109
cf.truncated = true
110110
}

pkg/expression/aggregation/descriptor.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,18 @@ func NewAggFuncDescForWindowFunc(ctx sessionctx.Context, desc *WindowFuncDesc, h
6666

6767
// String implements the fmt.Stringer interface.
6868
func (a *AggFuncDesc) String() string {
69+
return a.StringWithCtx(false)
70+
}
71+
72+
// StringWithCtx returns the string representation within given ctx.
73+
func (a *AggFuncDesc) StringWithCtx(redact bool) string {
6974
buffer := bytes.NewBufferString(a.Name)
7075
buffer.WriteString("(")
7176
if a.HasDistinct {
7277
buffer.WriteString("distinct ")
7378
}
7479
for i, arg := range a.Args {
75-
buffer.WriteString(arg.String())
80+
buffer.WriteString(arg.StringWithCtx(redact))
7681
if i+1 != len(a.Args) {
7782
buffer.WriteString(", ")
7883
}
@@ -81,7 +86,7 @@ func (a *AggFuncDesc) String() string {
8186
buffer.WriteString(" order by ")
8287
}
8388
for i, arg := range a.OrderByItems {
84-
buffer.WriteString(arg.String())
89+
buffer.WriteString(arg.StringWithCtx(redact))
8590
if i+1 != len(a.OrderByItems) {
8691
buffer.WriteString(", ")
8792
}

pkg/expression/aggregation/explain.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ import (
2020

2121
"github.com/pingcap/failpoint"
2222
"github.com/pingcap/tidb/pkg/parser/ast"
23+
"github.com/pingcap/tidb/pkg/sessionctx"
2324
)
2425

2526
// ExplainAggFunc generates explain information for a aggregation function.
26-
func ExplainAggFunc(agg *AggFuncDesc, normalized bool) string {
27+
func ExplainAggFunc(ctx sessionctx.Context, agg *AggFuncDesc, normalized bool) string {
2728
var buffer bytes.Buffer
2829
showMode := false
2930
failpoint.Inject("show-agg-mode", func(v failpoint.Value) {
@@ -49,13 +50,13 @@ func ExplainAggFunc(agg *AggFuncDesc, normalized bool) string {
4950
if normalized {
5051
fmt.Fprintf(&buffer, "%s desc", item.Expr.ExplainNormalizedInfo())
5152
} else {
52-
fmt.Fprintf(&buffer, "%s desc", item.Expr.ExplainInfo())
53+
fmt.Fprintf(&buffer, "%s desc", item.Expr.ExplainInfo(ctx))
5354
}
5455
} else {
5556
if normalized {
5657
fmt.Fprintf(&buffer, "%s", item.Expr.ExplainNormalizedInfo())
5758
} else {
58-
fmt.Fprintf(&buffer, "%s", item.Expr.ExplainInfo())
59+
fmt.Fprintf(&buffer, "%s", item.Expr.ExplainInfo(ctx))
5960
}
6061
}
6162

@@ -71,7 +72,7 @@ func ExplainAggFunc(agg *AggFuncDesc, normalized bool) string {
7172
if normalized {
7273
buffer.WriteString(arg.ExplainNormalizedInfo())
7374
} else {
74-
buffer.WriteString(arg.ExplainInfo())
75+
buffer.WriteString(arg.ExplainInfo(ctx))
7576
}
7677
}
7778
buffer.WriteString(")")

0 commit comments

Comments
 (0)