-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
52730: sql: new EXPLAIN infrastructure r=RaduBerinde a=RaduBerinde #### opt: store columns in OpaqueMetadata Store the result columns of opaque operators in their metadata instead of returning it separately. Release note: None #### opt: separate CreateTable/CreateTableAs exec operators Separate the two variants in the exec factory so that we don't have to pass around a `nil` node. Release note: None #### sql: new EXPLAIN infrastructure This change reimplements the default `(PLAN)` variant of EXPLAIN. Instead of visiting a plan node tree to get EXPLAIN information, we record the exact `exec.Factory` calls that were produced. We do this using an `exec.ExplainFactory` which is an extension of `exec.Factory`. This factory wraps a regular `exec.Factory` and forwards all factory calls to it (while also recording all arguments). The result is that for each `exec.Node` there is a corresponding `*explain.Node`. The factory also provides a way for the execbuilder to annotate nodes with more information, which will be initially used for row count and cost (not exposed in this PR). The implementation relies on optgen for the factory methods and per-operation identifiers and structs. I tried to avoid major changes to the EXPLAIN output; I left a lot of TODOs for improvements that I plan to make in follow-up changes. Some egregious issues were fixed in separate PRs. Still, there are various differences; the notable ones are: - for simple projections, we now show a `project` node without any rendering expressions. - in some cases we see a new `project` that just rearranges columns. I will address this in a follow-up change. - window function information is no longer duplicated. - expressions for inverted filters and joins now show the column names instead of unnamed references like `@2`. - we no longer see a `count` node on top of mutations. Release note (sql change): various improvements to EXPLAIN. #### sql: EXPLAIN test updates Separate commit for explain test changes. Release note: None Co-authored-by: Radu Berinde <radu@cockroachlabs.com>
- Loading branch information
Showing
66 changed files
with
4,980 additions
and
3,898 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package sql | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" | ||
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat" | ||
"github.com/cockroachdb/cockroach/pkg/sql/opt/exec" | ||
"github.com/cockroachdb/cockroach/pkg/sql/opt/exec/explain" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase" | ||
) | ||
|
||
// explainNewPlanNode implements EXPLAIN (PLAN); it produces the output of | ||
// EXPLAIN given an explain.Plan. | ||
// | ||
// TODO(radu): move and rename this once explainPlanNode is removed. | ||
type explainNewPlanNode struct { | ||
flags explain.Flags | ||
plan *explain.Plan | ||
run explainNewPlanNodeRun | ||
|
||
columns sqlbase.ResultColumns | ||
} | ||
|
||
type explainNewPlanNodeRun struct { | ||
results *valuesNode | ||
} | ||
|
||
func (e *explainNewPlanNode) startExec(params runParams) error { | ||
ob := explain.NewOutputBuilder(e.flags) | ||
|
||
realPlan := e.plan.WrappedPlan.(*planTop) | ||
distribution, willVectorize := explainGetDistributedAndVectorized(params, &realPlan.planComponents) | ||
ob.AddField("distribution", distribution.String()) | ||
ob.AddField("vectorized", fmt.Sprintf("%t", willVectorize)) | ||
|
||
spanFormatFn := func(table cat.Table, index cat.Index, scanParams exec.ScanParams) string { | ||
var tabDesc *sqlbase.ImmutableTableDescriptor | ||
var idxDesc *descpb.IndexDescriptor | ||
if table.IsVirtualTable() { | ||
tabDesc = table.(*optVirtualTable).desc | ||
idxDesc = index.(*optVirtualIndex).desc | ||
} else { | ||
tabDesc = table.(*optTable).desc | ||
idxDesc = index.(*optIndex).desc | ||
} | ||
spans, err := generateScanSpans(params.p, tabDesc, idxDesc, scanParams) | ||
if err != nil { | ||
return err.Error() | ||
} | ||
// skip is how many fields to skip when pretty-printing spans. | ||
// Usually 2, but can be 4 when running EXPLAIN from a tenant since there | ||
// will be an extra tenant prefix and ID. For example: | ||
// - /51/1/1 is a key read as a system tenant where the first two values | ||
// are the table ID and the index ID. | ||
// - /Tenant/10/51/1/1 is a key read as a non-system tenant where the first | ||
// four values are the special tenant prefix byte and tenant ID, followed | ||
// by the table ID and the index ID. | ||
skip := 2 | ||
if !params.p.ExecCfg().Codec.ForSystemTenant() { | ||
skip = 4 | ||
} | ||
return sqlbase.PrettySpans(idxDesc, spans, skip) | ||
} | ||
|
||
if err := explain.Emit(e.plan, ob, spanFormatFn); err != nil { | ||
return err | ||
} | ||
|
||
v := params.p.newContainerValuesNode(e.columns, 0) | ||
for _, row := range ob.BuildExplainRows() { | ||
if _, err := v.rows.AddRow(params.ctx, row); err != nil { | ||
return err | ||
} | ||
} | ||
e.run.results = v | ||
|
||
return nil | ||
} | ||
|
||
func (e *explainNewPlanNode) Next(params runParams) (bool, error) { return e.run.results.Next(params) } | ||
func (e *explainNewPlanNode) Values() tree.Datums { return e.run.results.Values() } | ||
|
||
func (e *explainNewPlanNode) Close(ctx context.Context) { | ||
e.plan.Root.WrappedNode().(planNode).Close(ctx) | ||
for i := range e.plan.Subqueries { | ||
e.plan.Subqueries[i].Root.(*explain.Node).WrappedNode().(planNode).Close(ctx) | ||
} | ||
for i := range e.plan.Checks { | ||
e.plan.Checks[i].WrappedNode().(planNode).Close(ctx) | ||
} | ||
if e.run.results != nil { | ||
e.run.results.Close(ctx) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.