github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/optbuilder/explain.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package optbuilder 12 13 import ( 14 "github.com/cockroachdb/cockroach/pkg/server/telemetry" 15 "github.com/cockroachdb/cockroach/pkg/sql/opt/memo" 16 "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" 17 "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" 18 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 19 "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" 20 "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" 21 ) 22 23 func (b *Builder) buildExplain(explain *tree.Explain, inScope *scope) (outScope *scope) { 24 b.pushWithFrame() 25 26 // We don't allow the statement under Explain to reference outer columns, so we 27 // pass a "blank" scope rather than inScope. 28 stmtScope := b.buildStmtAtRoot(explain.Statement, nil /* desiredTypes */, b.allocScope()) 29 30 b.popWithFrame(stmtScope) 31 outScope = inScope.push() 32 33 var cols sqlbase.ResultColumns 34 switch explain.Mode { 35 case tree.ExplainPlan: 36 telemetry.Inc(sqltelemetry.ExplainPlanUseCounter) 37 if explain.Flags[tree.ExplainFlagVerbose] || explain.Flags[tree.ExplainFlagTypes] { 38 cols = sqlbase.ExplainPlanVerboseColumns 39 } else { 40 cols = sqlbase.ExplainPlanColumns 41 } 42 43 case tree.ExplainDistSQL: 44 analyze := explain.Flags[tree.ExplainFlagAnalyze] 45 if analyze { 46 telemetry.Inc(sqltelemetry.ExplainAnalyzeUseCounter) 47 } else { 48 telemetry.Inc(sqltelemetry.ExplainDistSQLUseCounter) 49 } 50 if analyze && tree.IsStmtParallelized(explain.Statement) { 51 panic(pgerror.Newf(pgcode.FeatureNotSupported, 52 "EXPLAIN ANALYZE does not support RETURNING NOTHING statements")) 53 } 54 cols = sqlbase.ExplainDistSQLColumns 55 56 case tree.ExplainOpt: 57 if explain.Flags[tree.ExplainFlagVerbose] { 58 telemetry.Inc(sqltelemetry.ExplainOptVerboseUseCounter) 59 } else { 60 telemetry.Inc(sqltelemetry.ExplainOptUseCounter) 61 } 62 cols = sqlbase.ExplainOptColumns 63 64 case tree.ExplainVec: 65 telemetry.Inc(sqltelemetry.ExplainVecUseCounter) 66 cols = sqlbase.ExplainVecColumns 67 68 default: 69 panic(pgerror.Newf(pgcode.FeatureNotSupported, 70 "EXPLAIN ANALYZE does not support RETURNING NOTHING statements")) 71 } 72 b.synthesizeResultColumns(outScope, cols) 73 74 input := stmtScope.expr.(memo.RelExpr) 75 private := memo.ExplainPrivate{ 76 Options: explain.ExplainOptions, 77 ColList: colsToColList(outScope.cols), 78 Props: stmtScope.makePhysicalProps(), 79 StmtType: explain.Statement.StatementType(), 80 } 81 outScope.expr = b.factory.ConstructExplain(input, &private) 82 return outScope 83 }