github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/optbuilder/misc_statements.go (about)

     1  // Copyright 2019 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  	"fmt"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
    17  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/types"
    19  )
    20  
    21  func (b *Builder) buildControlJobs(n *tree.ControlJobs, inScope *scope) (outScope *scope) {
    22  	if err := b.catalog.RequireAdminRole(b.ctx, n.StatementTag()); err != nil {
    23  		panic(err)
    24  	}
    25  
    26  	// We don't allow the input statement to reference outer columns, so we
    27  	// pass a "blank" scope rather than inScope.
    28  	emptyScope := b.allocScope()
    29  	colTypes := []*types.T{types.Int}
    30  	inputScope := b.buildStmt(n.Jobs, colTypes, emptyScope)
    31  
    32  	checkInputColumns(
    33  		fmt.Sprintf("%s JOBS", tree.JobCommandToStatement[n.Command]),
    34  		inputScope,
    35  		[]string{"job_id"},
    36  		colTypes,
    37  		1, /* minPrefix */
    38  	)
    39  	outScope = inScope.push()
    40  	outScope.expr = b.factory.ConstructControlJobs(
    41  		inputScope.expr.(memo.RelExpr),
    42  		&memo.ControlJobsPrivate{
    43  			Props:   inputScope.makePhysicalProps(),
    44  			Command: n.Command,
    45  		},
    46  	)
    47  	return outScope
    48  }
    49  
    50  func (b *Builder) buildCancelQueries(n *tree.CancelQueries, inScope *scope) (outScope *scope) {
    51  	// We don't allow the input statement to reference outer columns, so we
    52  	// pass a "blank" scope rather than inScope.
    53  	emptyScope := b.allocScope()
    54  	colTypes := []*types.T{types.String}
    55  	inputScope := b.buildStmt(n.Queries, colTypes, emptyScope)
    56  
    57  	checkInputColumns(
    58  		"CANCEL QUERIES",
    59  		inputScope,
    60  		[]string{"query_id"},
    61  		colTypes,
    62  		1, /* minPrefix */
    63  	)
    64  	outScope = inScope.push()
    65  	outScope.expr = b.factory.ConstructCancelQueries(
    66  		inputScope.expr.(memo.RelExpr),
    67  		&memo.CancelPrivate{
    68  			Props:    inputScope.makePhysicalProps(),
    69  			IfExists: n.IfExists,
    70  		},
    71  	)
    72  	return outScope
    73  }
    74  
    75  func (b *Builder) buildCancelSessions(n *tree.CancelSessions, inScope *scope) (outScope *scope) {
    76  	// We don't allow the input statement to reference outer columns, so we
    77  	// pass a "blank" scope rather than inScope.
    78  	emptyScope := b.allocScope()
    79  	colTypes := []*types.T{types.String}
    80  	inputScope := b.buildStmt(n.Sessions, colTypes, emptyScope)
    81  
    82  	checkInputColumns(
    83  		"CANCEL SESSIONS",
    84  		inputScope,
    85  		[]string{"session_id"},
    86  		colTypes,
    87  		1, /* minPrefix */
    88  	)
    89  	outScope = inScope.push()
    90  	outScope.expr = b.factory.ConstructCancelSessions(
    91  		inputScope.expr.(memo.RelExpr),
    92  		&memo.CancelPrivate{
    93  			Props:    inputScope.makePhysicalProps(),
    94  			IfExists: n.IfExists,
    95  		},
    96  	)
    97  	return outScope
    98  }