github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/optbuilder/export.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  	"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
    15  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    16  	"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
    17  	"github.com/cockroachdb/cockroach/pkg/sql/types"
    18  )
    19  
    20  // buildExport builds an EXPORT statement.
    21  func (b *Builder) buildExport(export *tree.Export, inScope *scope) (outScope *scope) {
    22  	if err := b.catalog.RequireAdminRole(b.ctx, "EXPORT"); err != nil {
    23  		panic(err)
    24  	}
    25  	// We don't allow the input statement to reference outer columns, so we
    26  	// pass a "blank" scope rather than inScope.
    27  	emptyScope := b.allocScope()
    28  	inputScope := b.buildStmt(export.Query, nil /* desiredTypes */, emptyScope)
    29  
    30  	texpr := emptyScope.resolveType(export.File, types.String)
    31  	fileName := b.buildScalar(
    32  		texpr, emptyScope, nil /* outScope */, nil /* outCol */, nil, /* colRefs */
    33  	)
    34  	options := b.buildKVOptions(export.Options, emptyScope)
    35  
    36  	outScope = inScope.push()
    37  	b.synthesizeResultColumns(outScope, sqlbase.ExportColumns)
    38  	outScope.expr = b.factory.ConstructExport(
    39  		inputScope.expr.(memo.RelExpr),
    40  		fileName,
    41  		options,
    42  		&memo.ExportPrivate{
    43  			FileFormat: export.FileFormat,
    44  			Columns:    colsToColList(outScope.cols),
    45  			Props:      inputScope.makePhysicalProps(),
    46  		},
    47  	)
    48  	return outScope
    49  }
    50  
    51  func (b *Builder) buildKVOptions(opts tree.KVOptions, inScope *scope) memo.KVOptionsExpr {
    52  	res := make(memo.KVOptionsExpr, len(opts))
    53  	for i := range opts {
    54  		res[i].Key = string(opts[i].Key)
    55  		if opts[i].Value != nil {
    56  			texpr := inScope.resolveType(opts[i].Value, types.String)
    57  			res[i].Value = b.buildScalar(
    58  				texpr, inScope, nil /* outScope */, nil /* outCol */, nil, /* colRefs */
    59  			)
    60  		} else {
    61  			res[i].Value = b.factory.ConstructNull(types.String)
    62  		}
    63  	}
    64  	return res
    65  }