Skip to content

Commit

Permalink
Merge pull request cockroachdb#47975 from RaduBerinde/backport20.1-47951
Browse files Browse the repository at this point in the history
release-20.1: sql: fix explain output for subqueries/postqueries
  • Loading branch information
RaduBerinde authored May 1, 2020
2 parents 193281a + 09f958f commit d6232da
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 22 deletions.
19 changes: 11 additions & 8 deletions pkg/sql/explain_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func (e *explainPlanNode) Close(ctx context.Context) {
// explainEntry is a representation of the info that makes it into an output row
// of an EXPLAIN statement.
type explainEntry struct {
isNode bool
level int
node, field, fieldVal string
plan planNode
Expand Down Expand Up @@ -336,7 +337,7 @@ func observePlan(

// Explain the subqueries.
for i := range subqueryPlans {
if _, err := observer.enterNode(ctx, "subquery", plan); err != nil && returnError {
if _, err := observer.enterNode(ctx, "subquery", nil /* plan */); err != nil && returnError {
return err
}
observer.attr("subquery", "id", fmt.Sprintf("@S%d", i+1))
Expand All @@ -355,22 +356,22 @@ func observePlan(
} else if subqueryPlans[i].started {
observer.expr(observeAlways, "subquery", "result", -1, subqueryPlans[i].result)
}
if err := observer.leaveNode("subquery", subqueryPlans[i].plan); err != nil && returnError {
if err := observer.leaveNode("subquery", nil /* plan */); err != nil && returnError {
return err
}
}

// Explain the postqueries.
for i := range postqueryPlans {
if _, err := observer.enterNode(ctx, "postquery", plan); err != nil && returnError {
if _, err := observer.enterNode(ctx, "postquery", nil /* plan */); err != nil && returnError {
return err
}
if postqueryPlans[i].plan != nil {
if err := walkPlan(ctx, postqueryPlans[i].plan, observer); err != nil && returnError {
return err
}
}
if err := observer.leaveNode("postquery", postqueryPlans[i].plan); err != nil && returnError {
if err := observer.leaveNode("postquery", nil /* plan */); err != nil && returnError {
return err
}
}
Expand All @@ -394,7 +395,7 @@ func (e *explainer) emitRows(emitRow emitExplainRowFn) error {
n := []treeprinter.Node{tp}

for _, entry := range e.entries {
if entry.plan != nil {
if entry.isNode {
n = append(n[:entry.level+1], n[entry.level].Child(entry.node))
} else {
tp.AddEmptyLine()
Expand Down Expand Up @@ -503,9 +504,10 @@ func (e *explainer) expr(v observeVerbosity, nodeName, fieldName string, n int,
// enterNode implements the planObserver interface.
func (e *explainer) enterNode(_ context.Context, name string, plan planNode) (bool, error) {
e.entries = append(e.entries, explainEntry{
level: e.level,
node: name,
plan: plan,
isNode: true,
level: e.level,
node: name,
plan: plan,
})

e.level++
Expand All @@ -515,6 +517,7 @@ func (e *explainer) enterNode(_ context.Context, name string, plan planNode) (bo
// attr implements the planObserver interface.
func (e *explainer) attr(nodeName, fieldName, attr string) {
e.entries = append(e.entries, explainEntry{
isNode: false,
level: e.level - 1,
field: fieldName,
fieldVal: attr,
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/exec/execbuilder/testdata/insert
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ root · ·
│ │ order +z · ·
│ └── scan buffer node · · (z) ·
│ label buffer 1 · ·
└── subquery · · (z) +z
└── subquery · · · ·
│ id @S1 · ·
│ original sql INSERT INTO xyz SELECT a, b, c FROM abc RETURNING z · ·
│ exec mode all rows · ·
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/opt/exec/execbuilder/testdata/scalar
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ root · ·
├── values · · ("array") ·
│ size 1 column, 1 row · ·
│ row 0, expr 0 ARRAY @S1 · ·
└── subquery · · ("array") ·
└── subquery · · · ·
│ id @S1 · ·
│ original sql (SELECT generate_series(1, 10) ORDER BY 1 DESC) · ·
│ exec mode all rows · ·
Expand All @@ -740,7 +740,7 @@ root · · ("array
├── values · · ("array") ·
│ size 1 column, 1 row · ·
│ row 0, expr 0 ARRAY @S1 · ·
└── subquery · · ("array") ·
└── subquery · · · ·
│ id @S1 · ·
│ original sql (SELECT a FROM t ORDER BY b) · ·
│ exec mode all rows · ·
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/exec/execbuilder/testdata/srfs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ root · ·
│ └── scan · · (z) ·
│ table xz@primary · ·
│ spans FULL SCAN · ·
└── subquery · · (generate_series) ·
└── subquery · · · ·
│ id @S1 · ·
│ original sql (SELECT unnest(ARRAY[x, y]) FROM xy) · ·
│ exec mode one row · ·
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/opt/exec/execbuilder/testdata/subquery
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ root · ·
│ table abc@primary · ·
│ spans FULL SCAN · ·
│ filter a = @S2 · ·
├── subquery · · (a, b, c) ·
├── subquery · · · ·
│ │ id @S1 · ·
│ │ original sql EXISTS (SELECT * FROM abc WHERE c = (a + 3)) · ·
│ │ exec mode exists · ·
Expand All @@ -62,7 +62,7 @@ root · ·
│ table abc@primary · ·
│ spans FULL SCAN · ·
│ filter c = (a + 3) · ·
└── subquery · · (a, b, c) ·
└── subquery · · · ·
│ id @S2 · ·
│ original sql (SELECT max(a) FROM abc WHERE EXISTS (SELECT * FROM abc WHERE c = (a + 3))) · ·
│ exec mode one row · ·
Expand Down Expand Up @@ -148,7 +148,7 @@ root · ·
│ table tab4@primary · ·
│ spans FULL SCAN · ·
│ filter ((col0 <= 0) AND (col4 <= 5.38)) OR ((((col4 = ANY @S1) AND (col3 <= 5)) AND (col3 >= 7)) AND (col3 <= 9)) · ·
└── subquery · · (col0) ·
└── subquery · · · ·
│ id @S1 · ·
│ original sql (SELECT col1 FROM tab4 WHERE col1 > 8.27) · ·
│ exec mode all rows normalized · ·
Expand Down Expand Up @@ -245,7 +245,7 @@ root · · ("array") ·
├── values · · ("array") ·
│ size 1 column, 1 row · ·
│ row 0, expr 0 ARRAY @S1 · ·
└── subquery · · ("array") ·
└── subquery · · · ·
│ id @S1 · ·
│ original sql (SELECT x FROM b) · ·
│ exec mode all rows · ·
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/exec/execbuilder/testdata/update
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ root · ·
│ │ order +a · ·
│ └── scan buffer node · · (a) ·
│ label buffer 1 · ·
└── subquery · · (a) +a
└── subquery · · · ·
│ id @S1 · ·
│ original sql UPDATE abc SET a = c RETURNING a · ·
│ exec mode all rows · ·
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/exec/execbuilder/testdata/upsert
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ root · ·
│ │ order +z · ·
│ └── scan buffer node · · (z) ·
│ label buffer 1 · ·
└── subquery · · (z) +z
└── subquery · · · ·
│ id @S1 · ·
│ original sql UPSERT INTO xyz SELECT a, b, c FROM abc RETURNING z · ·
│ exec mode all rows · ·
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/exec/execbuilder/testdata/window
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ root · ·
│ └── scan · · (v, w) ·
│ table kv@primary · ·
│ spans FULL SCAN · ·
└── subquery · · (avg) ·
└── subquery · · · ·
│ id @S1 · ·
│ original sql (SELECT count(*) FROM kv) · ·
│ exec mode one row · ·
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/opt/exec/execbuilder/testdata/with
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ root · · (a, a) ·
│ │ label buffer 1 (t) · ·
│ └── scan buffer node · · (a) ·
│ label buffer 1 (t) · ·
└── subquery · · (a, a) ·
└── subquery · · · ·
│ id @S1 · ·
│ original sql SELECT a FROM y · ·
│ exec mode all rows · ·
Expand Down Expand Up @@ -50,7 +50,7 @@ EXPLAIN (VERBOSE)
root · · (a) ·
├── scan buffer node · · (a) ·
│ label buffer 1 (t) · ·
└── subquery · · (a) ·
└── subquery · · · ·
│ id @S1 · ·
│ original sql INSERT INTO x VALUES (1) RETURNING a · ·
│ exec mode all rows · ·
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/testdata/explain_tree
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ root
│ └── scan (id int, title string)
│ table movies@primary
│ spans FULL SCAN
└── subquery (movie_id int, title string, name string)
└── subquery
│ id @S1
│ original sql (SELECT name FROM t.actors WHERE name = 'Foo')
│ exec mode one row
Expand Down

0 comments on commit d6232da

Please sign in to comment.