Skip to content

Commit

Permalink
opt: set all session settings to their global default
Browse files Browse the repository at this point in the history
All session settings in optimizer tests and benchmarks are now set to
their global defaults. Prior to this commit each new session setting had
to be manually set in both tests and benchmarks. This step could be
forgotten, such as with `optimizer_merge_joins_enabled` which was
unknowingly disabled in benchmarks when it was added. Now, all session
settings with global defaults are automatically set.

Additionally, `sql.SetSessionVariable` has been renamed to
`sql.TestingSetSessionVariable` to discourage its use outside of tests.

NOTE: Now that `optimizer_merge_joins_enabled` is enabled, some
benchmarks will appear to regress.

Release note: None
  • Loading branch information
mgartner committed Jul 26, 2024
1 parent 536c510 commit 67cd6f1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 60 deletions.
4 changes: 2 additions & 2 deletions pkg/sql/opt/bench/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ go_test(
"//pkg/security/securitytest",
"//pkg/server",
"//pkg/settings/cluster",
"//pkg/sql",
"//pkg/sql/catalog/schemaexpr",
"//pkg/sql/catalog/tabledesc",
"//pkg/sql/opt",
"//pkg/sql/opt/exec",
"//pkg/sql/opt/exec/execbuilder",
"//pkg/sql/opt/exec/explain",
Expand All @@ -44,5 +43,6 @@ go_test(
"//pkg/testutils/sqlutils",
"//pkg/util/log",
"//pkg/util/randutil",
"@com_github_cockroachdb_errors//:errors",
],
)
27 changes: 6 additions & 21 deletions pkg/sql/opt/bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ import (
"github.com/cockroachdb/cockroach/pkg/security/securitytest"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemaexpr"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/exec"
"github.com/cockroachdb/cockroach/pkg/sql/opt/exec/execbuilder"
"github.com/cockroachdb/cockroach/pkg/sql/opt/exec/explain"
Expand All @@ -43,6 +42,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
"github.com/cockroachdb/errors"
)

// A query can be issued using the "simple protocol" or the "prepare protocol".
Expand Down Expand Up @@ -754,25 +754,10 @@ func newHarness(tb testing.TB, query benchQuery, schemas []string) *harness {
evalCtx: eval.MakeTestingEvalContext(cluster.MakeTestingClusterSettings()),
}

// Setup the default session settings.
h.evalCtx.SessionData().OptimizerUseMultiColStats = true
h.evalCtx.SessionData().ZigzagJoinEnabled = true
h.evalCtx.SessionData().OptimizerUseForecasts = true
h.evalCtx.SessionData().OptimizerUseHistograms = true
h.evalCtx.SessionData().LocalityOptimizedSearch = true
h.evalCtx.SessionData().ReorderJoinsLimit = opt.DefaultJoinOrderLimit
h.evalCtx.SessionData().InsertFastPath = true
h.evalCtx.SessionData().OptSplitScanLimit = tabledesc.MaxBucketAllowed
h.evalCtx.SessionData().VariableInequalityLookupJoinEnabled = true
h.evalCtx.SessionData().OptimizerUseVirtualComputedColumnStats = true
h.evalCtx.SessionData().OptimizerUseTrigramSimilarityOptimization = true
h.evalCtx.SessionData().OptimizerUseImprovedDistinctOnLimitHintCosting = true
h.evalCtx.SessionData().OptimizerUseImprovedTrigramSimilaritySelectivity = true
h.evalCtx.SessionData().TrigramSimilarityThreshold = 0.3
h.evalCtx.SessionData().OptimizerUseImprovedZigzagJoinCosting = true
h.evalCtx.SessionData().OptimizerUseImprovedMultiColumnSelectivityEstimate = true
h.evalCtx.SessionData().OptimizerProveImplicationWithVirtualComputedColumns = true
h.evalCtx.SessionData().OptimizerPushOffsetIntoIndexJoin = true
// Set session settings to their global defaults.
if err := sql.TestingResetSessionVariables(h.ctx, h.evalCtx); err != nil {
panic(errors.Wrap(err, "could not reset session variables"))
}

// Set up the test catalog.
h.testCat = testcat.New()
Expand Down
1 change: 0 additions & 1 deletion pkg/sql/opt/testutils/opttester/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ go_library(
"//pkg/sql",
"//pkg/sql/catalog/descpb",
"//pkg/sql/catalog/schemaexpr",
"//pkg/sql/catalog/tabledesc",
"//pkg/sql/opt",
"//pkg/sql/opt/cat",
"//pkg/sql/opt/exec",
Expand Down
42 changes: 9 additions & 33 deletions pkg/sql/opt/testutils/opttester/opt_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemaexpr"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
"github.com/cockroachdb/cockroach/pkg/sql/opt/exec"
Expand Down Expand Up @@ -271,57 +270,34 @@ type Flags struct {

// New constructs a new instance of the OptTester for the given SQL statement.
// Metadata used by the SQL query is accessed via the catalog.
func New(catalog cat.Catalog, sql string) *OptTester {
func New(catalog cat.Catalog, sqlStr string) *OptTester {
ctx := context.Background()
ot := &OptTester{
catalog: catalog,
sql: sql,
sql: sqlStr,
ctx: ctx,
semaCtx: tree.MakeSemaContext(catalog),
evalCtx: eval.MakeTestingEvalContext(cluster.MakeTestingClusterSettings()),
}
ot.f = &norm.Factory{}
ot.f.Init(ot.ctx, &ot.evalCtx, ot.catalog)
ot.evalCtx.SessionData().ReorderJoinsLimit = opt.DefaultJoinOrderLimit
ot.evalCtx.SessionData().OptimizerUseMultiColStats = true
ot.Flags.ctx = ot.ctx
ot.Flags.evalCtx = ot.evalCtx
ot.semaCtx.SearchPath = tree.EmptySearchPath
// To allow opttester tests to use now(), we hardcode a preset transaction
// time. May 10, 2017 is a historic day: the release date of CockroachDB 1.0.
ot.evalCtx.TxnTimestamp = time.Date(2017, 05, 10, 13, 0, 0, 0, time.UTC)

// Set any OptTester-wide session flags here.
// Set session settings to their global defaults.
if err := sql.TestingResetSessionVariables(ctx, ot.evalCtx); err != nil {
panic(errors.Wrap(err, "could not reset session variables"))
}

// Set non-default session settings.
ot.evalCtx.SessionData().UserProto = username.MakeSQLUsernameFromPreNormalizedString("opttester").EncodeProto()
ot.evalCtx.SessionData().Database = "defaultdb"
ot.evalCtx.SessionData().ZigzagJoinEnabled = true
ot.evalCtx.SessionData().OptimizerUseForecasts = true
ot.evalCtx.SessionData().OptimizerUseHistograms = true
ot.evalCtx.SessionData().LocalityOptimizedSearch = true
ot.evalCtx.SessionData().ReorderJoinsLimit = opt.DefaultJoinOrderLimit
ot.evalCtx.SessionData().InsertFastPath = true
ot.evalCtx.SessionData().OptSplitScanLimit = tabledesc.MaxBucketAllowed
ot.evalCtx.SessionData().VariableInequalityLookupJoinEnabled = true
ot.evalCtx.SessionData().OptimizerUseImprovedDisjunctionStats = true
ot.evalCtx.SessionData().OptimizerUseLimitOrderingForStreamingGroupBy = true
ot.evalCtx.SessionData().OptimizerUseImprovedSplitDisjunctionForJoins = true
ot.evalCtx.SessionData().OptimizerAlwaysUseHistograms = true
ot.evalCtx.SessionData().OptimizerHoistUncorrelatedEqualitySubqueries = true
ot.evalCtx.SessionData().OptimizerUseImprovedComputedColumnFiltersDerivation = true
ot.evalCtx.SessionData().OptimizerUseImprovedJoinElimination = true
ot.evalCtx.SessionData().OptimizerUseProvidedOrderingFix = true
ot.evalCtx.SessionData().OptimizerMergeJoinsEnabled = true
ot.evalCtx.SessionData().OptimizerUseVirtualComputedColumnStats = true
ot.evalCtx.SessionData().OptimizerUseTrigramSimilarityOptimization = true
ot.evalCtx.SessionData().OptimizerUseImprovedDistinctOnLimitHintCosting = true
ot.evalCtx.SessionData().OptimizerUseImprovedTrigramSimilaritySelectivity = true
ot.evalCtx.SessionData().TrigramSimilarityThreshold = 0.3
ot.evalCtx.SessionData().OptimizerUseImprovedZigzagJoinCosting = true
ot.evalCtx.SessionData().OptimizerUseImprovedMultiColumnSelectivityEstimate = true
ot.evalCtx.SessionData().OptimizerProveImplicationWithVirtualComputedColumns = true
ot.evalCtx.SessionData().OptimizerPushOffsetIntoIndexJoin = true
ot.evalCtx.SessionData().OptimizerUsePolymorphicParameterFix = true
ot.evalCtx.SessionData().ImplicitSelectForUpdate = false

return ot
}
Expand Down Expand Up @@ -979,7 +955,7 @@ func (f *Flags) Set(arg datadriven.CmdArg) error {
if len(s) != 2 {
return errors.Errorf("Expected both session variable name and value for set flag")
}
err := sql.SetSessionVariable(f.ctx, f.evalCtx, s[0], s[1])
err := sql.TestingSetSessionVariable(f.ctx, f.evalCtx, s[0], s[1])
if err != nil {
return err
}
Expand Down
27 changes: 24 additions & 3 deletions pkg/sql/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -3534,19 +3534,18 @@ func init() {
}()
}

// SetSessionVariable sets a new value for session setting `varName` in the
// TestingSetSessionVariable sets a new value for session setting `varName` in the
// session settings owned by `evalCtx`, returning an error if not successful.
// This function should only be used for testing. For general-purpose code,
// please use SessionAccessor.SetSessionVar instead.
func SetSessionVariable(
func TestingSetSessionVariable(
ctx context.Context, evalCtx eval.Context, varName, varValue string,
) (err error) {
err = CheckSessionVariableValueValid(ctx, evalCtx.Settings, varName, varValue)
if err != nil {
return err
}
sdMutatorBase := sessionDataMutatorBase{
defaults: make(map[string]string),
settings: evalCtx.Settings,
}
sdMutator := sessionDataMutator{
Expand All @@ -3562,6 +3561,28 @@ func SetSessionVariable(
return sVar.Set(ctx, sdMutator, varValue)
}

// TestingResetSessionVariables resets all session settings in evalCtx to their
// global default, if they have a global default.
func TestingResetSessionVariables(ctx context.Context, evalCtx eval.Context) (err error) {
sdMutatorBase := sessionDataMutatorBase{
settings: evalCtx.Settings,
}
sdMutator := sessionDataMutator{
data: evalCtx.SessionData(),
sessionDataMutatorBase: sdMutatorBase,
sessionDataMutatorCallbacks: sessionDataMutatorCallbacks{},
}
for _, v := range varGen {
if v.Set == nil || v.GlobalDefault == nil {
continue
}
if err := v.Set(ctx, sdMutator, v.GlobalDefault(&evalCtx.Settings.SV)); err != nil {
return err
}
}
return nil
}

// makePostgresBoolGetStringValFn returns a function that evaluates and returns
// a string representation of the first argument value.
func makePostgresBoolGetStringValFn(varName string) getStringValFn {
Expand Down

0 comments on commit 67cd6f1

Please sign in to comment.