Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: fix statement bundle creation when memo isn't detached #92952

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/sql/explain_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"github.com/cockroachdb/errors"
)

const noPlan = "no plan"

// setExplainBundleResult sets the result of an EXPLAIN ANALYZE (DEBUG)
// statement. warnings will be printed out as is in the CLI.
//
Expand Down Expand Up @@ -242,9 +244,9 @@ func (b *stmtBundleBuilder) addStatement() {
func (b *stmtBundleBuilder) addOptPlans(ctx context.Context) {
if b.plan.mem == nil || b.plan.mem.RootExpr() == nil {
// No optimizer plans; an error must have occurred during planning.
b.z.AddFile("opt.txt", "no plan")
b.z.AddFile("opt-v.txt", "no plan")
b.z.AddFile("opt-vv.txt", "no plan")
b.z.AddFile("opt.txt", noPlan)
b.z.AddFile("opt-v.txt", noPlan)
b.z.AddFile("opt-vv.txt", noPlan)
return
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/sql/explain_bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ CREATE TABLE s.a (a INT PRIMARY KEY);`)
checkBundle(t, fmt.Sprintf("%+v", pqErr.Detail), "", nil, base, plans, "distsql.html")
})

// #92920 Make sure schema and opt files are created.
t.Run("memo-reset", func(t *testing.T) {
rows := r.QueryStr(t, "EXPLAIN ANALYZE (DEBUG) CREATE TABLE t (i int)")
checkBundle(t, fmt.Sprint(rows), "", func(name, contents string) error {
if name == "opt.txt" {
if contents == noPlan {
return errors.Errorf("opt.txt empty")
}
}
return nil
}, base, plans, "distsql.html vec.txt vec-v.txt")
})

// Verify that we can issue the statement with prepare (which can happen
// depending on the client).
t.Run("prepare", func(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/sql/opt/norm/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ func (f *Factory) DetachMemo() *memo.Memo {
return m
}

// ReleaseMemo is just like DetachMemo but it doesn't call Detach on the memo
// preserving any statistics information for explain purposes (distinct, null
// count etc).
func (f *Factory) ReleaseMemo() *memo.Memo {
m := f.mem
f.mem = nil
return m
}

// DisableOptimizations disables all transformation rules. The unaltered input
// expression tree becomes the output expression tree (because no transforms
// are applied).
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/plan_opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ func (opc *optPlanningCtx) buildExecMemo(ctx context.Context) (_ *memo.Memo, _ e
return memo, nil
}

return f.Memo(), nil
return f.ReleaseMemo(), nil
}

// runExecBuilder execbuilds a plan using the given factory and stores the
Expand Down