Skip to content

Commit

Permalink
Merge #27126 #27132
Browse files Browse the repository at this point in the history
27126: opt: improve the appliedRule callback r=RaduBerinde a=RaduBerinde

#### opt: allow distinguishing between norm/explore rules

Release note: None

#### opt: improve the appliedRule callback

Improving the appliedRule callback to also pass the ordinal of the
expression that the rule was applied on. Cleaning up the related logic
and state in forcing_opt.

Release note: None


27132: ui: make sure we identify the cluster r=couchand a=couchand

In #24996 I accidentally moved the identify call rather than copying it,
meaning we frequently failed to issue the identify.  This fixes that
by trying to identify on every page if we have not yet.

Release note: None

Co-authored-by: Radu Berinde <[email protected]>
Co-authored-by: Andrew Couch <[email protected]>
  • Loading branch information
3 people committed Jul 3, 2018
3 parents c071d5a + 130c9df + 445b2fc commit dfbd39b
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 64 deletions.
3 changes: 3 additions & 0 deletions pkg/sql/opt/memo/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type ExprID struct {
// expression.
var InvalidExprID = ExprID{}

// Suppress linter complaint.
var _ = InvalidExprID

// MakeNormExprID returns the id of the normalized expression for the given
// group.
func MakeNormExprID(group GroupID) ExprID {
Expand Down
14 changes: 9 additions & 5 deletions pkg/sql/opt/norm/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ type MatchedRuleFunc func(ruleName opt.RuleName) bool

// AppliedRuleFunc defines the callback function for the NotifyOnAppliedRule
// event supported by the optimizer and factory. It is invoked each time an
// optimization rule (Normalize or Explore) has been applied. The function is
// called with the name of the rule and the memo group it affected. If the rule
// was an exploration rule, then the added parameter gives the number of
// expressions added to the group by the rule.
type AppliedRuleFunc func(ruleName opt.RuleName, group memo.GroupID, added int)
// optimization rule (Normalize or Explore) has been applied.
//
// The function is called with the name of the rule and the memo group it
// affected. If the rule was an exploration rule, then the expr parameter
// indicates the expression on which the rule was applied, and the added
// parameter number of expressions added to the group by the rule.
type AppliedRuleFunc func(
ruleName opt.RuleName, group memo.GroupID, expr memo.ExprOrdinal, added int,
)

// Factory constructs a normalized expression tree within the memo. As each
// kind of expression is constructed by the factory, it transitively runs
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/opt/optgen/cmd/optgen/rule_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ func (g *ruleGen) genNormalizeReplace(define *lang.DefineExpr, rule *lang.RuleEx

// Notify listeners that rule was applied.
g.w.nestIndent("if _f.appliedRule != nil {\n")
g.w.writeIndent("_f.appliedRule(opt.%s, _group, 0)\n", rule.Name)
g.w.writeIndent("_f.appliedRule(opt.%s, _group, 0, 0)\n", rule.Name)
g.w.unnest("}\n")

g.w.writeIndent("return _group\n")
Expand Down Expand Up @@ -657,7 +657,7 @@ func (g *ruleGen) genExploreReplace(define *lang.DefineExpr, rule *lang.RuleExpr
// Notify listeners that rule was applied.
g.w.nestIndent("if _e.o.appliedRule != nil {\n")
g.w.writeIndent("_after := _e.mem.ExprCount(_root.Group)\n")
g.w.writeIndent("_e.o.appliedRule(opt.%s, _root.Group, _after-_before)\n", rule.Name)
g.w.writeIndent("_e.o.appliedRule(opt.%s, _root.Group, _root.Expr, _after-_before)\n", rule.Name)
g.w.unnest("}\n")

g.w.unnest("}\n")
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/opt/optgen/cmd/optgen/rule_names_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func (g *ruleNamesGen) generate(compiled *lang.CompiledExpr, w io.Writer) {
fmt.Fprintf(g.w, " startAutoRule RuleName = iota + NumManualRuleNames\n\n")

g.genRuleNameEnumByTag("Normalize")
fmt.Fprintf(g.w, " // startExploreRule tracks the number of normalization rules;\n")
fmt.Fprintf(g.w, " // all rules greater than this value are exploration rules.\n")
fmt.Fprintf(g.w, " startExploreRule\n\n")
g.genRuleNameEnumByTag("Explore")

fmt.Fprintf(g.w, " // NumRuleNames tracks the total count of rule names.\n")
Expand Down
12 changes: 6 additions & 6 deletions pkg/sql/opt/optgen/cmd/optgen/testdata/explorer
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (_e *explorer) exploreInnerJoin(_rootState *exploreState, _root memo.ExprID
_e.mem.MemoizeDenormExpr(_root.Group, memo.Expr(_expr))
if _e.o.appliedRule != nil {
_after := _e.mem.ExprCount(_root.Group)
_e.o.appliedRule(opt.CommuteJoin, _root.Group, _after-_before)
_e.o.appliedRule(opt.CommuteJoin, _root.Group, _root.Expr, _after-_before)
}
}
}
Expand Down Expand Up @@ -231,7 +231,7 @@ func (_e *explorer) exploreInnerJoin(_rootState *exploreState, _root memo.ExprID
_e.mem.MemoizeDenormExpr(_root.Group, memo.Expr(_expr))
if _e.o.appliedRule != nil {
_after := _e.mem.ExprCount(_root.Group)
_e.o.appliedRule(opt.AssociateJoin, _root.Group, _after-_before)
_e.o.appliedRule(opt.AssociateJoin, _root.Group, _root.Expr, _after-_before)
}
}
}
Expand Down Expand Up @@ -298,7 +298,7 @@ func (_e *explorer) exploreSelect(_rootState *exploreState, _root memo.ExprID) (
_e.mem.MemoizeDenormExpr(_root.Group, memo.Expr(_expr))
if _e.o.appliedRule != nil {
_after := _e.mem.ExprCount(_root.Group)
_e.o.appliedRule(opt.PushDownGroupBy, _root.Group, _after-_before)
_e.o.appliedRule(opt.PushDownGroupBy, _root.Group, _root.Expr, _after-_before)
}
}
}
Expand Down Expand Up @@ -328,7 +328,7 @@ func (_e *explorer) exploreScan(_rootState *exploreState, _root memo.ExprID) (_f
}
if _e.o.appliedRule != nil {
_after := _e.mem.ExprCount(_root.Group)
_e.o.appliedRule(opt.GenerateIndexScans, _root.Group, _after-_before)
_e.o.appliedRule(opt.GenerateIndexScans, _root.Group, _root.Expr, _after-_before)
}
}
}
Expand Down Expand Up @@ -405,7 +405,7 @@ func (_e *explorer) exploreList(_rootState *exploreState, _root memo.ExprID) (_f
}
if _e.o.appliedRule != nil {
_after := _e.mem.ExprCount(_root.Group)
_e.o.appliedRule(opt.List, _root.Group, _after-_before)
_e.o.appliedRule(opt.List, _root.Group, _root.Expr, _after-_before)
}
}
}
Expand Down Expand Up @@ -467,7 +467,7 @@ func (_e *explorer) exploreOp(_rootState *exploreState, _root memo.ExprID) (_ful
_e.mem.MemoizeDenormExpr(_root.Group, memo.Expr(_expr))
if _e.o.appliedRule != nil {
_after := _e.mem.ExprCount(_root.Group)
_e.o.appliedRule(opt.ListNot, _root.Group, _after-_before)
_e.o.appliedRule(opt.ListNot, _root.Group, _root.Expr, _after-_before)
}
}
}
Expand Down
38 changes: 19 additions & 19 deletions pkg/sql/opt/optgen/cmd/optgen/testdata/factory
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (_f *Factory) ConstructInnerJoin(
)
_f.mem.AddAltFingerprint(_innerJoinExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.CommuteJoin, _group, 0)
_f.appliedRule(opt.CommuteJoin, _group, 0, 0)
}
return _group
}
Expand Down Expand Up @@ -273,7 +273,7 @@ func (_f *Factory) ConstructEq(
)
_f.mem.AddAltFingerprint(_eqExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.NormalizeVarPlus, _group, 0)
_f.appliedRule(opt.NormalizeVarPlus, _group, 0, 0)
}
return _group
}
Expand Down Expand Up @@ -318,7 +318,7 @@ func (_f *Factory) ConstructLt(
)
_f.mem.AddAltFingerprint(_ltExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.NormalizeVarPlus, _group, 0)
_f.appliedRule(opt.NormalizeVarPlus, _group, 0, 0)
}
return _group
}
Expand Down Expand Up @@ -491,7 +491,7 @@ func (_f *Factory) ConstructFunc(
)
_f.mem.AddAltFingerprint(_funcExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.Concat, _group, 0)
_f.appliedRule(opt.Concat, _group, 0, 0)
}
return _group
}
Expand Down Expand Up @@ -642,7 +642,7 @@ func (_f *Factory) ConstructSelect(
)
_f.mem.AddAltFingerprint(_selectExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.Test, _group, 0)
_f.appliedRule(opt.Test, _group, 0, 0)
}
return _group
}
Expand Down Expand Up @@ -829,7 +829,7 @@ func (_f *Factory) ConstructList(
_group = _f.funcs.Construct(any, first, last, single, empty)
_f.mem.AddAltFingerprint(_listExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.List, _group, 0)
_f.appliedRule(opt.List, _group, 0, 0)
}
return _group
}
Expand Down Expand Up @@ -917,7 +917,7 @@ func (_f *Factory) ConstructJoin(
_group = _f.funcs.Construct(_f.mem.InternList([]memo.GroupID{}), _f.mem.InternList([]memo.GroupID{left}), _f.mem.InternList([]memo.GroupID{left, right}), _f.mem.InternList([]memo.GroupID{_f.mem.InternList([]memo.GroupID{on})}))
_f.mem.AddAltFingerprint(_joinExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.ConstructList, _group, 0)
_f.appliedRule(opt.ConstructList, _group, 0, 0)
}
return _group
}
Expand Down Expand Up @@ -1022,7 +1022,7 @@ func (_f *Factory) ConstructOp(
)
_f.mem.AddAltFingerprint(_opExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.ListNot, _group, 0)
_f.appliedRule(opt.ListNot, _group, 0, 0)
}
return _group
}
Expand Down Expand Up @@ -1144,7 +1144,7 @@ func (_f *Factory) ConstructEq(
_group = m.mem.InternPrivate(tree.NewDString("foo"))
_f.mem.AddAltFingerprint(_eqExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.Constant, _group, 0)
_f.appliedRule(opt.Constant, _group, 0, 0)
}
return _group
}
Expand Down Expand Up @@ -1193,7 +1193,7 @@ func (_f *Factory) ConstructNe(
_group = m.mem.InternPrivate(tree.NewDString("foo"))
_f.mem.AddAltFingerprint(_neExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.Dynamic, _group, 0)
_f.appliedRule(opt.Dynamic, _group, 0, 0)
}
return _group
}
Expand Down Expand Up @@ -1294,7 +1294,7 @@ func (_f *Factory) ConstructPlus(
_group = _f.ConstructNull()
_f.mem.AddAltFingerprint(_plusExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.Fold, _group, 0)
_f.appliedRule(opt.Fold, _group, 0, 0)
}
return _group
}
Expand Down Expand Up @@ -1325,7 +1325,7 @@ func (_f *Factory) ConstructMinus(
_group = _f.ConstructNull()
_f.mem.AddAltFingerprint(_minusExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.Fold, _group, 0)
_f.appliedRule(opt.Fold, _group, 0, 0)
}
return _group
}
Expand Down Expand Up @@ -1490,7 +1490,7 @@ func (_f *Factory) ConstructNot(
_group = _f.funcs.Invert(_f.mem.NormExpr(input).Operator(), left, right)
_f.mem.AddAltFingerprint(_notExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.Invert, _group, 0)
_f.appliedRule(opt.Invert, _group, 0, 0)
}
return _group
}
Expand Down Expand Up @@ -1597,7 +1597,7 @@ func (_f *Factory) ConstructProject(
_group = input
_f.mem.AddAltFingerprint(_projectExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.EliminateProject1, _group, 0)
_f.appliedRule(opt.EliminateProject1, _group, 0, 0)
}
return _group
}
Expand All @@ -1609,7 +1609,7 @@ func (_f *Factory) ConstructProject(
_group = input
_f.mem.AddAltFingerprint(_projectExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.EliminateProject2, _group, 0)
_f.appliedRule(opt.EliminateProject2, _group, 0, 0)
}
return _group
}
Expand All @@ -1621,7 +1621,7 @@ func (_f *Factory) ConstructProject(
_group = input
_f.mem.AddAltFingerprint(_projectExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.EliminateProject3, _group, 0)
_f.appliedRule(opt.EliminateProject3, _group, 0, 0)
}
return _group
}
Expand All @@ -1633,7 +1633,7 @@ func (_f *Factory) ConstructProject(
_group = input
_f.mem.AddAltFingerprint(_projectExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.EliminateProject4, _group, 0)
_f.appliedRule(opt.EliminateProject4, _group, 0, 0)
}
return _group
}
Expand All @@ -1645,7 +1645,7 @@ func (_f *Factory) ConstructProject(
_group = input
_f.mem.AddAltFingerprint(_projectExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.EliminateProject5, _group, 0)
_f.appliedRule(opt.EliminateProject5, _group, 0, 0)
}
return _group
}
Expand All @@ -1657,7 +1657,7 @@ func (_f *Factory) ConstructProject(
_group = input
_f.mem.AddAltFingerprint(_projectExpr.Fingerprint(), _group)
if _f.appliedRule != nil {
_f.appliedRule(opt.EliminateProject6, _group, 0)
_f.appliedRule(opt.EliminateProject6, _group, 0, 0)
}
return _group
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/opt/optgen/cmd/optgen/testdata/rulenames
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const (
EliminateEmptyAnd
EliminateSingletonAnd

// startExploreRule tracks the number of normalization rules;
// all rules greater than this value are exploration rules.
startExploreRule

// ------------------------------------------------------------
// Explore Rule Names
// ------------------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions pkg/sql/opt/rule_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ const (
NumManualRuleNames
)

// IsNormalize returns true if r is a normalization rule.
func (r RuleName) IsNormalize() bool {
return r < startExploreRule
}

// IsExplore returns true if r is an exploration rule.
func (r RuleName) IsExplore() bool {
return r > startExploreRule
}

// Make linter happy.
var _ = InvalidRuleName
var _ = NumManualRuleNames
var _ = RuleName.IsNormalize
var _ = RuleName.IsExplore
4 changes: 2 additions & 2 deletions pkg/sql/opt/rule_name_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dfbd39b

Please sign in to comment.