github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/tree/function_definition.go (about)

     1  // Copyright 2016 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 tree
    12  
    13  import "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
    14  
    15  // FunctionDefinition implements a reference to the (possibly several)
    16  // overloads for a built-in function.
    17  type FunctionDefinition struct {
    18  	// Name is the short name of the function.
    19  	Name string
    20  
    21  	// Definition is the set of overloads for this function name.
    22  	// We use []overloadImpl here although all the uses of this struct
    23  	// could actually write a []Overload, because we want to share
    24  	// the code with typeCheckOverloadedExprs().
    25  	Definition []overloadImpl
    26  
    27  	// FunctionProperties are the properties common to all overloads.
    28  	FunctionProperties
    29  }
    30  
    31  // FunctionProperties defines the properties of the built-in
    32  // functions that are common across all overloads.
    33  type FunctionProperties struct {
    34  	// UnsupportedWithIssue, if non-zero indicates the built-in is not
    35  	// really supported; the name is a placeholder. Value -1 just says
    36  	// "not supported" without an issue to link; values > 0 provide an
    37  	// issue number to link.
    38  	UnsupportedWithIssue int
    39  
    40  	// Undocumented, when set to true, indicates that the built-in function is
    41  	// hidden from documentation. This is currently used to hide experimental
    42  	// functionality as it is being developed.
    43  	Undocumented bool
    44  
    45  	// Private, when set to true, indicates the built-in function is not
    46  	// available for use by user queries. This is currently used by some
    47  	// aggregates due to issue #10495. Private functions are implicitly
    48  	// considered undocumented.
    49  	Private bool
    50  
    51  	// NullableArgs is set to true when a function's definition can
    52  	// handle NULL arguments. When set, the function will be given the
    53  	// chance to see NULL arguments. When not, the function will
    54  	// evaluate directly to NULL in the presence of any NULL arguments.
    55  	//
    56  	// NOTE: when set, a function should be prepared for any of its arguments to
    57  	// be NULL and should act accordingly.
    58  	NullableArgs bool
    59  
    60  	// Impure is set to true when a function potentially returns a
    61  	// different value when called in the same statement with the same
    62  	// parameters. e.g.: random(), clock_timestamp(). Some functions
    63  	// like now() return the same value in the same statement, but
    64  	// different values in separate statements, and should not be marked
    65  	// as impure.
    66  	Impure bool
    67  
    68  	// DistsqlBlacklist is set to true when a function depends on
    69  	// members of the EvalContext that are not marshaled by DistSQL
    70  	// (e.g. planner). Currently used for DistSQL to determine if
    71  	// expressions can be evaluated on a different node without sending
    72  	// over the EvalContext.
    73  	//
    74  	// TODO(andrei): Get rid of the planner from the EvalContext and then we can
    75  	// get rid of this blacklist.
    76  	DistsqlBlacklist bool
    77  
    78  	// Class is the kind of built-in function (normal/aggregate/window/etc.)
    79  	Class FunctionClass
    80  
    81  	// Category is used to generate documentation strings.
    82  	Category string
    83  
    84  	// ReturnLabels can be used to override the return column name of a
    85  	// function in a FROM clause.
    86  	// This satisfies a Postgres quirk where some json functions have
    87  	// different return labels when used in SELECT or FROM clause.
    88  	ReturnLabels []string
    89  
    90  	// AmbiguousReturnType is true if the builtin's return type can't be
    91  	// determined without extra context. This is used for formatting builtins
    92  	// with the FmtParsable directive.
    93  	AmbiguousReturnType bool
    94  
    95  	// IgnoreVolatilityCheck ignores checking the functions overloads against
    96  	// the Postgres ones at test time.
    97  	// This should be used with caution.
    98  	IgnoreVolatilityCheck bool
    99  }
   100  
   101  // ShouldDocument returns whether the built-in function should be included in
   102  // external-facing documentation.
   103  func (fp *FunctionProperties) ShouldDocument() bool {
   104  	return !(fp.Undocumented || fp.Private)
   105  }
   106  
   107  // FunctionClass specifies the class of the builtin function.
   108  type FunctionClass int
   109  
   110  const (
   111  	// NormalClass is a standard builtin function.
   112  	NormalClass FunctionClass = iota
   113  	// AggregateClass is a builtin aggregate function.
   114  	AggregateClass
   115  	// WindowClass is a builtin window function.
   116  	WindowClass
   117  	// GeneratorClass is a builtin generator function.
   118  	GeneratorClass
   119  	// SQLClass is a builtin function that executes a SQL statement as a side
   120  	// effect of the function call.
   121  	//
   122  	// For example, AddGeometryColumn is a SQLClass function that executes an
   123  	// ALTER TABLE ... ADD COLUMN statement to add a geometry column to an
   124  	// existing table. It returns metadata about the column added.
   125  	//
   126  	// All builtin functions of this class should include a definition for
   127  	// Overload.SQLFn, which returns the SQL statement to be executed. They
   128  	// should also include a definition for Overload.Fn, which is executed
   129  	// like a NormalClass function and returns a Datum.
   130  	SQLClass
   131  )
   132  
   133  // Avoid vet warning about unused enum value.
   134  var _ = NormalClass
   135  
   136  // NewFunctionDefinition allocates a function definition corresponding
   137  // to the given built-in definition.
   138  func NewFunctionDefinition(
   139  	name string, props *FunctionProperties, def []Overload,
   140  ) *FunctionDefinition {
   141  	overloads := make([]overloadImpl, len(def))
   142  
   143  	for i := range def {
   144  		if def[i].PreferredOverload {
   145  			// Builtins with a preferred overload are always ambiguous.
   146  			props.AmbiguousReturnType = true
   147  		}
   148  		// Produce separate telemetry for each overload.
   149  		def[i].counter = sqltelemetry.BuiltinCounter(name, def[i].Signature(false))
   150  
   151  		overloads[i] = &def[i]
   152  	}
   153  	return &FunctionDefinition{
   154  		Name:               name,
   155  		Definition:         overloads,
   156  		FunctionProperties: *props,
   157  	}
   158  }
   159  
   160  // FunDefs holds pre-allocated FunctionDefinition instances
   161  // for every builtin function. Initialized by builtins.init().
   162  var FunDefs map[string]*FunctionDefinition
   163  
   164  // Format implements the NodeFormatter interface.
   165  func (fd *FunctionDefinition) Format(ctx *FmtCtx) {
   166  	ctx.WriteString(fd.Name)
   167  }
   168  func (fd *FunctionDefinition) String() string { return AsString(fd) }