Skip to content

Commit

Permalink
sql: support redaction of EXPLAIN (PLAN)
Browse files Browse the repository at this point in the history
Support redaction of `EXPLAIN (PLAN)` and add plan.txt back to redacted
statement diagnostics bundles.

Part of: cockroachdb#68570

Epic: CRDB-19756

Release note (sql change): Add support for the `REDACT` flag to the
following variants of `EXPLAIN`:
- `EXPLAIN`
- `EXPLAIN (PLAN)`
- `EXPLAIN (VEC)`
- `EXPLAIN ANALYZE`
- `EXPLAIN ANALYZE (PLAN)`
These explain statements (along with `EXPLAIN ANALYZE (DEBUG)`, which
already supported `REDACT`) will have constants, literal values,
parameter values, and any other user data redacted in output.
  • Loading branch information
michae2 committed Feb 1, 2023
1 parent d6a32ed commit 5d5092b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
4 changes: 0 additions & 4 deletions pkg/sql/explain_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,6 @@ func (b *stmtBundleBuilder) addOptPlans(ctx context.Context) {

// addExecPlan adds the EXPLAIN (VERBOSE) plan as file plan.txt.
func (b *stmtBundleBuilder) addExecPlan(plan string) {
if b.flags.RedactValues {
return
}

if plan == "" {
plan = "no plan"
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/explain_bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ CREATE TABLE users(id UUID DEFAULT gen_random_uuid() PRIMARY KEY, promo_id INT R
}
}
return nil
}, "env.sql schema.sql statement.sql vec.txt vec-v.txt",
}, "env.sql plan.txt schema.sql statement.sql vec-v.txt vec.txt",
)
})
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/exec/explain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ go_library(
"//pkg/util/timeutil",
"//pkg/util/treeprinter",
"@com_github_cockroachdb_errors//:errors",
"@com_github_cockroachdb_redact//:redact",
"@com_github_dustin_go_humanize//:go-humanize",
],
)
Expand Down
16 changes: 12 additions & 4 deletions pkg/sql/opt/exec/explain/emit.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ func Emit(plan *Plan, ob *OutputBuilder, spanFormatFn SpanFormatFn) error {
// This field contains the original subquery (which could have been modified
// by optimizer transformations).
if s.ExprNode != nil {
ob.Attr("original sql", tree.AsStringWithFlags(s.ExprNode, tree.FmtSimple))
flags := tree.FmtSimple
if e.ob.flags.HideValues {
flags |= tree.FmtHideConstants
}
if e.ob.flags.RedactValues {
flags |= tree.FmtMarkRedactionNode | tree.FmtOmitNameRedaction
}
ob.Attr("original sql", tree.AsStringWithFlags(s.ExprNode, flags))
}
var mode string
switch s.Mode {
Expand Down Expand Up @@ -1006,7 +1013,8 @@ func (e *emitter) spansStr(table cat.Table, index cat.Index, scanParams exec.Sca
}

// In verbose mode show the physical spans, unless the table is virtual.
if e.ob.flags.Verbose && !table.IsVirtualTable() {
if e.ob.flags.Verbose && !e.ob.flags.HideValues && !e.ob.flags.RedactValues &&
!table.IsVirtualTable() {
return e.spanFormatFn(table, index, scanParams)
}

Expand All @@ -1017,8 +1025,8 @@ func (e *emitter) spansStr(table cat.Table, index cat.Index, scanParams exec.Sca
return fmt.Sprintf("%d span%s", n, util.Pluralize(int64(n)))
}

// If we must hide values, only show the count.
if e.ob.flags.HideValues {
// If we must hide or redact values, only show the count.
if e.ob.flags.HideValues || e.ob.flags.RedactValues {
n := scanParams.IndexConstraint.Spans.Count()
return fmt.Sprintf("%d span%s", n, util.Pluralize(int64(n)))
}
Expand Down
10 changes: 9 additions & 1 deletion pkg/sql/opt/exec/explain/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/treeprinter"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
)

// OutputBuilder is used to build the output of an explain tree.
Expand Down Expand Up @@ -148,6 +149,9 @@ func (ob *OutputBuilder) Expr(key string, expr tree.TypedExpr, varColumns colinf
if ob.flags.HideValues {
flags |= tree.FmtHideConstants
}
if ob.flags.RedactValues {
flags |= tree.FmtMarkRedactionNode | tree.FmtOmitNameRedaction
}
f := tree.NewFmtCtx(
flags,
tree.FmtIndexedVarFormat(func(ctx *tree.FmtCtx, idx int) {
Expand Down Expand Up @@ -210,7 +214,11 @@ func (ob *OutputBuilder) BuildStringRows() []string {
}
// Add any fields for the node.
for entry = popField(); entry != nil; entry = popField() {
child.AddLine(entry.fieldStr())
field := entry.fieldStr()
if ob.flags.RedactValues {
field = string(redact.RedactableString(field).Redact())
}
child.AddLine(field)
}
}
result = append(result, tp.FormattedRows()...)
Expand Down
12 changes: 8 additions & 4 deletions pkg/sql/sem/tree/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,14 @@ func MakeExplain(options []string, stmt Statement) (Statement, error) {
}

if opts.Flags[ExplainFlagRedact] {
// TODO(michae2): Support redaction of other EXPLAIN variants.
if !analyze || opts.Mode != ExplainDebug {
return nil, unimplemented.New(
"EXPLAIN (REDACT)", "the REDACT flag can only be used with EXPLAIN ANALYZE (DEBUG)",
// TODO(michae2): Support redaction of other EXPLAIN modes.
switch opts.Mode {
case ExplainPlan:
case ExplainVec:
case ExplainDebug:
default:
return nil, unimplemented.Newf(
"EXPLAIN (REDACT)", "the REDACT flag cannot be used with %s", opts.Mode,
)
}
}
Expand Down

0 comments on commit 5d5092b

Please sign in to comment.