diff --git a/pkg/sql/opt/bench/BUILD.bazel b/pkg/sql/opt/bench/BUILD.bazel index 09166010d5d7..644326b06518 100644 --- a/pkg/sql/opt/bench/BUILD.bazel +++ b/pkg/sql/opt/bench/BUILD.bazel @@ -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", @@ -44,5 +43,6 @@ go_test( "//pkg/testutils/sqlutils", "//pkg/util/log", "//pkg/util/randutil", + "@com_github_cockroachdb_errors//:errors", ], ) diff --git a/pkg/sql/opt/bench/bench_test.go b/pkg/sql/opt/bench/bench_test.go index 6014e0656781..17e4653b39b7 100644 --- a/pkg/sql/opt/bench/bench_test.go +++ b/pkg/sql/opt/bench/bench_test.go @@ -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" @@ -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". @@ -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() diff --git a/pkg/sql/opt/testutils/opttester/BUILD.bazel b/pkg/sql/opt/testutils/opttester/BUILD.bazel index 438e51d19ec1..0ae32ad6f3b2 100644 --- a/pkg/sql/opt/testutils/opttester/BUILD.bazel +++ b/pkg/sql/opt/testutils/opttester/BUILD.bazel @@ -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", diff --git a/pkg/sql/opt/testutils/opttester/opt_tester.go b/pkg/sql/opt/testutils/opttester/opt_tester.go index 4e4e8c3e6a5f..37679979aca5 100644 --- a/pkg/sql/opt/testutils/opttester/opt_tester.go +++ b/pkg/sql/opt/testutils/opttester/opt_tester.go @@ -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" @@ -271,19 +270,17 @@ 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 @@ -291,37 +288,16 @@ func New(catalog cat.Catalog, sql string) *OptTester { // 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 } @@ -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 } diff --git a/pkg/sql/vars.go b/pkg/sql/vars.go index 0bac31bf15ea..4e7577803d2a 100644 --- a/pkg/sql/vars.go +++ b/pkg/sql/vars.go @@ -3534,11 +3534,11 @@ 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) @@ -3546,7 +3546,6 @@ func SetSessionVariable( return err } sdMutatorBase := sessionDataMutatorBase{ - defaults: make(map[string]string), settings: evalCtx.Settings, } sdMutator := sessionDataMutator{ @@ -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 {