Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exec: fix explain(vec) for queries with subqueries #40511

Merged
merged 1 commit into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/sql/distsqlrun/column_exec_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,12 +774,13 @@ func planProjectionOperators(
case *tree.BinaryExpr:
return planProjectionExpr(ctx, t.Operator, t.ResolvedType(), t.TypedLeft(), t.TypedRight(), columnTypes, input)
case *tree.CastExpr:
op, resultIdx, ct, memUsed, err = planProjectionOperators(ctx, t.Expr.(tree.TypedExpr), columnTypes, input)
expr := t.Expr.(tree.TypedExpr)
op, resultIdx, ct, memUsed, err = planProjectionOperators(ctx, expr, columnTypes, input)
if err != nil {
return nil, 0, nil, 0, err
}
outputIdx := len(ct)
op, err = exec.GetCastOperator(op, resultIdx, outputIdx, t.Expr.(tree.TypedExpr).ResolvedType(), t.Type)
op, err = exec.GetCastOperator(op, resultIdx, outputIdx, expr.ResolvedType(), t.Type)
ct = append(ct, *t.Type)
if sMem, ok := op.(exec.StaticMemoryOperator); ok {
memUsed += sMem.EstimateStaticMemoryUsage()
Expand Down
60 changes: 60 additions & 0 deletions pkg/sql/exec/cast_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package exec

import (
"context"
"fmt"
"math"

"github.com/cockroachdb/apd"
Expand Down Expand Up @@ -73,6 +74,14 @@ var _ interface{} = execgen.GET
func GetCastOperator(
input Operator, colIdx int, resultIdx int, fromType *semtypes.T, toType *semtypes.T,
) (Operator, error) {
if fromType.Family() == semtypes.UnknownFamily {
return &castOpNullAny{
OneInputNode: NewOneInputNode(input),
colIdx: colIdx,
outputIdx: resultIdx,
toType: typeconv.FromColumnType(toType),
}, nil
}
switch from := typeconv.FromColumnType(fromType); from {
// {{ range $typ, $overloads := . }}
case coltypes._ALLTYPES:
Expand All @@ -98,6 +107,57 @@ func GetCastOperator(
}
}

type castOpNullAny struct {
OneInputNode
colIdx int
outputIdx int
toType coltypes.T
}

var _ StaticMemoryOperator = &castOpNullAny{}

func (c *castOpNullAny) EstimateStaticMemoryUsage() int {
return EstimateBatchSizeBytes([]coltypes.T{c.toType}, coldata.BatchSize)
}

func (c *castOpNullAny) Init() {
c.input.Init()
}

func (c *castOpNullAny) Next(ctx context.Context) coldata.Batch {
batch := c.input.Next(ctx)
n := batch.Length()
if n == 0 {
return batch
}
if c.outputIdx == batch.Width() {
batch.AppendCol(c.toType)
}
vec := batch.ColVec(c.colIdx)
projVec := batch.ColVec(c.outputIdx)
vecNulls := vec.Nulls()
projNulls := projVec.Nulls()
if sel := batch.Selection(); sel != nil {
sel = sel[:n]
for _, i := range sel {
if vecNulls.NullAt(i) {
projNulls.SetNull(i)
} else {
execerror.VectorizedInternalPanic(errors.Errorf("unexpected non-null at index %d", i))
}
}
} else {
for i := uint16(0); i < n; i++ {
if vecNulls.NullAt(uint16(i)) {
projNulls.SetNull(uint16(i))
} else {
execerror.VectorizedInternalPanic(fmt.Errorf("unexpected non-null at index %d", i))
}
}
}
return batch
}

// {{ range $typ, $overloads := . }}
// {{ range $overloads }}
// {{ if isCastFuncSet . }}
Expand Down
25 changes: 23 additions & 2 deletions pkg/sql/explain_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/distsqlrun"
"github.com/cockroachdb/cockroach/pkg/sql/exec"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/util/mon"
"github.com/cockroachdb/cockroach/pkg/util/treeprinter"
)

Expand All @@ -37,6 +39,7 @@ type explainVecNode struct {
// The current row returned by the node.
values tree.Datums
}
subqueryPlans []subquery
}

type flowWithNode struct {
Expand All @@ -62,7 +65,17 @@ func (n *explainVecNode) startExec(params runParams) error {
planCtx.ignoreClose = true
planCtx.planner = params.p
planCtx.stmtType = n.stmtType
planCtx.noEvalSubqueries = true
outerSubqueries := planCtx.planner.curPlan.subqueryPlans
defer func() {
planCtx.planner.curPlan.subqueryPlans = outerSubqueries
}()
planCtx.planner.curPlan.subqueryPlans = n.subqueryPlans
for i := range planCtx.planner.curPlan.subqueryPlans {
p := &planCtx.planner.curPlan.subqueryPlans[i]
// Fake subquery results - they're not important for our explain output.
p.started = true
p.result = tree.DNull
}

var plan PhysicalPlan
var err error
Expand All @@ -88,8 +101,16 @@ func (n *explainVecNode) startExec(params runParams) error {
flowCtx := &distsqlrun.FlowCtx{
NodeID: planCtx.EvalContext().NodeID,
EvalCtx: planCtx.EvalContext(),
Cfg: &distsqlrun.ServerConfig{},
Cfg: &distsqlrun.ServerConfig{
Settings: params.p.execCfg.Settings,
DiskMonitor: &mon.BytesMonitor{},
},
}
// Temporarily set vectorize to on so that we can get the whole plan back even
// if we wouldn't support it due to lack of streaming.
origMode := flowCtx.EvalCtx.SessionData.VectorizeMode
flowCtx.EvalCtx.SessionData.VectorizeMode = sessiondata.VectorizeExperimentalOn
defer func() { flowCtx.EvalCtx.SessionData.VectorizeMode = origMode }()

sortedFlows := make([]flowWithNode, 0, len(flows))
for nodeID, flow := range flows {
Expand Down
Loading