@@ -8,6 +8,7 @@ package xform
8
8
import (
9
9
"context"
10
10
"math/rand"
11
+ "strings"
11
12
12
13
"github.com/cockroachdb/cockroach/pkg/sql/opt"
13
14
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
@@ -133,6 +134,16 @@ func (o *Optimizer) Init(ctx context.Context, evalCtx *eval.Context, catalog cat
133
134
o .mem = o .f .Memo ()
134
135
o .explorer .init (o )
135
136
137
+ var disabledRules RuleSet
138
+ // If the DisableOptimizerRules session variable is set, then disable
139
+ // the specified rules.
140
+ if ruleNames := evalCtx .SessionData ().DisableOptimizerRules ; len (ruleNames ) > 0 {
141
+ for _ , ruleName := range ruleNames {
142
+ if rule , ok := opt .RuleNameMap [strings .ToLower (ruleName )]; ok {
143
+ disabledRules .Add (int (rule ))
144
+ }
145
+ }
146
+ }
136
147
if seed := evalCtx .SessionData ().TestingOptimizerRandomSeed ; seed != 0 {
137
148
o .rng = rand .New (rand .NewSource (seed ))
138
149
}
@@ -153,7 +164,10 @@ func (o *Optimizer) Init(ctx context.Context, evalCtx *eval.Context, catalog cat
153
164
o .defaultCoster .Init (ctx , evalCtx , o .mem , costPerturbation , o .rng , o )
154
165
o .coster = & o .defaultCoster
155
166
if disableRuleProbability > 0 {
156
- o .disableRulesRandom (disableRuleProbability )
167
+ o .disableRulesRandom (disableRuleProbability , & disabledRules )
168
+ }
169
+ if disabledRules .Len () > 0 {
170
+ o .disableRules (disabledRules )
157
171
}
158
172
}
159
173
@@ -1067,7 +1081,7 @@ func (a *groupStateAlloc) allocate() *groupState {
1067
1081
}
1068
1082
1069
1083
// disableRulesRandom disables rules with the given probability for testing.
1070
- func (o * Optimizer ) disableRulesRandom (probability float64 ) {
1084
+ func (o * Optimizer ) disableRulesRandom (probability float64 , disabledRules * RuleSet ) {
1071
1085
essentialRules := intsets .MakeFast (
1072
1086
// Needed to prevent constraint building from failing.
1073
1087
int (opt .NormalizeInConst ),
@@ -1115,7 +1129,6 @@ func (o *Optimizer) disableRulesRandom(probability float64) {
1115
1129
int (opt .ConvertUncorrelatedExistsToCoalesceSubquery ),
1116
1130
)
1117
1131
1118
- var disabledRules RuleSet
1119
1132
for i := opt .RuleName (1 ); i < opt .NumRuleNames ; i ++ {
1120
1133
var r float64
1121
1134
if o .rng == nil {
@@ -1127,12 +1140,14 @@ func (o *Optimizer) disableRulesRandom(probability float64) {
1127
1140
disabledRules .Add (int (i ))
1128
1141
}
1129
1142
}
1143
+ }
1130
1144
1145
+ func (o * Optimizer ) disableRules (disabledRules RuleSet ) {
1131
1146
o .f .SetDisabledRules (disabledRules )
1132
1147
1133
1148
o .NotifyOnMatchedRule (func (ruleName opt.RuleName ) bool {
1134
1149
if disabledRules .Contains (int (ruleName )) {
1135
- log .Infof (o .ctx , "disabled rule matched: %s" , ruleName .String ())
1150
+ log .VEventf (o .ctx , 2 , "disabled rule matched: %s" , ruleName .String ())
1136
1151
return false
1137
1152
}
1138
1153
return true
0 commit comments