Skip to content

Commit 1633431

Browse files
committed
Better optional logging
1 parent b4ce0d9 commit 1633431

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

sql/analyzer/indexed_joins.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,7 @@ func replanJoin(ctx *sql.Context, n *plan.JoinNode, a *Analyzer, scope *plan.Sco
229229
return nil, err
230230
}
231231

232-
if m.Tracer.TraceEnabled {
233-
m.Tracer.Log("Completed cost-based optimization:\n%s", m.CostDebugString())
234-
}
232+
m.LogCostDebugString()
235233

236234
if a.Verbose && a.Debug {
237235
a.Log("%s", m.String())
@@ -240,9 +238,7 @@ func replanJoin(ctx *sql.Context, n *plan.JoinNode, a *Analyzer, scope *plan.Sco
240238
scope.JoinTrees = append(scope.JoinTrees, m.String())
241239
}
242240

243-
if m.Tracer.TraceEnabled {
244-
m.Tracer.Log("Best root plan:\n%s", m.BestPlanDebugString())
245-
}
241+
m.LogBestPlanDebugString()
246242

247243
return m.BestRootPlan(ctx)
248244
}

sql/memo/memo.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -645,10 +645,15 @@ func (m *Memo) String() string {
645645
return b.String()
646646
}
647647

648-
// CostDebugString returns a string representation of the memo with cost
648+
// LogCostDebugString logs a string representation of the memo with cost
649649
// information for each expression, ordered by best to worst for each group,
650650
// displayed in a tree structure.
651-
func (m *Memo) CostDebugString() interface{} {
651+
// Only logs if tracing is enabled.
652+
func (m *Memo) LogCostDebugString() {
653+
if m.root == nil || !m.Tracer.TraceEnabled {
654+
return
655+
}
656+
652657
exprs := make([]string, m.cnt)
653658
groups := make([]*ExprGroup, 0)
654659

@@ -685,18 +690,20 @@ func (m *Memo) CostDebugString() interface{} {
685690
}
686691
b.WriteString(fmt.Sprintf("%s G%d: %s\n", beg, i+1, g))
687692
}
688-
return b.String()
693+
694+
m.Tracer.Log("Completed cost-based optimization:\n%s", b.String())
689695
}
690696

691-
// BestPlanDebugString returns a physical tree representation of the best plan for each group in the tree that is
697+
// LogBestPlanDebugString logs a physical tree representation of the best plan for each group in the tree that is
692698
// referenced by the best plan in the root. This differs from other debug strings in that it represents the groups
693699
// as children of their parents, rather than as a flat list, and only includes groups that are part of the best plan.
694-
func (m *Memo) BestPlanDebugString() interface{} {
695-
if m.root == nil {
696-
return ""
700+
// Only logs if tracing is enabled.
701+
func (m *Memo) LogBestPlanDebugString() {
702+
if m.root == nil || !m.Tracer.TraceEnabled {
703+
return
697704
}
698705

699-
return m.root.BestPlanDebugString()
706+
m.Tracer.Log("Best root plan:\n%s", m.root.BestPlanDebugString())
700707
}
701708

702709
type tableProps struct {

0 commit comments

Comments
 (0)