forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
explain.go
115 lines (100 loc) · 2.94 KB
/
explain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
package sql
import (
"strings"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/sql/parser"
)
type explainMode int
const (
explainNone explainMode = iota
explainDebug
explainPlan
)
// Explain executes the explain statement, providing debugging and analysis
// info about a DELETE, INSERT, SELECT or UPDATE statement.
//
// Privileges: the same privileges as the statement being explained.
func (p *planner) Explain(n *parser.Explain) (planNode, *roachpb.Error) {
mode := explainNone
if len(n.Options) == 1 && strings.EqualFold(n.Options[0], "DEBUG") {
mode = explainDebug
} else if len(n.Options) == 0 {
mode = explainPlan
}
if mode == explainNone {
return nil, roachpb.NewUErrorf("unsupported EXPLAIN options: %s", n)
}
plan, err := p.makePlan(n.Statement, false)
if err != nil {
return nil, err
}
switch mode {
case explainDebug:
plan, err = markDebug(plan, mode)
if err != nil {
return nil, roachpb.NewUErrorf("%v: %s", err, n)
}
return plan, nil
case explainPlan:
v := &valuesNode{}
v.columns = []resultColumn{
{name: "Level", typ: parser.DummyInt},
{name: "Type", typ: parser.DummyString},
{name: "Description", typ: parser.DummyString},
}
populateExplain(v, plan, 0)
plan = v
default:
return nil, roachpb.NewUErrorf("unsupported EXPLAIN mode: %d", mode)
}
return plan, nil
}
func markDebug(plan planNode, mode explainMode) (planNode, *roachpb.Error) {
switch t := plan.(type) {
case *selectNode:
return markDebug(t.from, mode)
case *scanNode:
// Mark the node as being explained.
t.columns = []resultColumn{
{name: "RowIdx", typ: parser.DummyInt},
{name: "Key", typ: parser.DummyString},
{name: "Value", typ: parser.DummyString},
{name: "Output", typ: parser.DummyBool},
}
t.explain = mode
return t, nil
case *indexJoinNode:
return markDebug(t.index, mode)
case *sortNode:
return markDebug(t.plan, mode)
default:
return nil, roachpb.NewErrorf("TODO(pmattis): unimplemented %T", plan)
}
}
func populateExplain(v *valuesNode, plan planNode, level int) {
name, description, children := plan.ExplainPlan()
row := parser.DTuple{
parser.DInt(level),
parser.DString(name),
parser.DString(description),
}
v.rows = append(v.rows, row)
for _, child := range children {
populateExplain(v, child, level+1)
}
}