Skip to content

Commit

Permalink
opt: add max-stack opttest option
Browse files Browse the repository at this point in the history
This commit adds the `max-stack` option for opttests, allowing tests to
alter the maximum stack size for the goroutine that optimizes the query.

Release note: None
  • Loading branch information
mgartner committed Oct 17, 2024
1 parent c49e4e0 commit 56d6b22
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/sql/opt/testutils/opttester/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ go_library(
"@com_github_cockroachdb_datadriven//:datadriven",
"@com_github_cockroachdb_errors//:errors",
"@com_github_cockroachdb_errors//oserror",
"@com_github_dustin_go_humanize//:go-humanize",
"@com_github_pmezard_go_difflib//difflib",
],
)
Expand Down
24 changes: 24 additions & 0 deletions pkg/sql/opt/testutils/opttester/opt_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"os"
"path/filepath"
"runtime"
"runtime/debug"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -69,6 +70,7 @@ import (
"github.com/cockroachdb/datadriven"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/errors/oserror"
"github.com/dustin/go-humanize"
"github.com/pmezard/go-difflib/difflib"
)

Expand Down Expand Up @@ -265,6 +267,10 @@ type Flags struct {

// TxnIsoLevel is the isolation level to plan for.
TxnIsoLevel isolation.Level

// MaxStackBytes specifies the number of bytes to limit the stack size to.
// If it is zero, the stack size has the default Go limit.
MaxStackBytes int
}

// New constructs a new instance of the OptTester for the given SQL statement.
Expand Down Expand Up @@ -555,6 +561,9 @@ func New(catalog cat.Catalog, sqlStr string) *OptTester {
// full path or a relative path to testdata.
//
// - isolation: sets the isolation level to plan for.
//
// - max-stack: sets the maximum stack size for the goroutine that optimizes
// the query. See debug.SetMaxStack.
func (ot *OptTester) RunCommand(tb testing.TB, d *datadriven.TestData) string {
// Allow testcases to override the flags.
for _, a := range d.CmdArgs {
Expand All @@ -578,6 +587,11 @@ func (ot *OptTester) RunCommand(tb testing.TB, d *datadriven.TestData) string {
ot.evalCtx.Placeholders = nil
ot.evalCtx.TxnIsoLevel = ot.Flags.TxnIsoLevel

if ot.Flags.MaxStackBytes > 0 {
originalMaxStack := debug.SetMaxStack(ot.Flags.MaxStackBytes)
defer debug.SetMaxStack(originalMaxStack)
}

switch d.Cmd {
case "exec-ddl":
testCatalog, ok := ot.catalog.(*testcat.Catalog)
Expand Down Expand Up @@ -1164,6 +1178,16 @@ func (f *Flags) Set(arg datadriven.CmdArg) error {
}
f.TxnIsoLevel = isolation.Level(level)

case "max-stack":
if len(arg.Vals) != 1 {
return fmt.Errorf("max-stack requires one argument")
}
bytes, err := humanize.ParseBytes(arg.Vals[0])
if err != nil {
return err
}
f.MaxStackBytes = int(bytes)

default:
return fmt.Errorf("unknown argument: %s", arg.Key)
}
Expand Down

0 comments on commit 56d6b22

Please sign in to comment.