github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/builtins/builtinsregistry/builtins_registry.go (about)

     1  // Copyright 2022 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 builtinsregistry stores the definitions of builtin functions.
    12  //
    13  // The builtins package imports this package and registers its functions.
    14  // This package exists to avoid import cycles so that catalog packages
    15  // can interact with builtin definitions without needing to import the
    16  // builtins package.
    17  package builtinsregistry
    18  
    19  import "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/tree"
    20  
    21  var registry = map[string]definition{}
    22  
    23  // Subscription is a hook to be called once on all registered builtins.
    24  type Subscription func(name string, props *tree.FunctionProperties, overloads []tree.Overload)
    25  
    26  var subscriptions []Subscription
    27  
    28  // Register registers a builtin. Intending to be called at init time, it panics
    29  // if a function of the same name has already been registered.
    30  func Register(name string, props *tree.FunctionProperties, overloads []tree.Overload) {
    31  	if _, exists := registry[name]; exists {
    32  		panic("duplicate builtin: " + name)
    33  	}
    34  	for i := range overloads {
    35  		var hook func()
    36  		overloads[i].OnTypeCheck = &hook
    37  	}
    38  	registry[name] = definition{
    39  		props:     props,
    40  		overloads: overloads,
    41  	}
    42  	for _, s := range subscriptions {
    43  		s(name, props, overloads)
    44  	}
    45  }
    46  
    47  // AddSubscription defines a hook to be called once on all registered builtins.
    48  // Subscriptions should be added at init() time, but are load-order independent:
    49  // if you add a subscription after a function is registered, it will immediately
    50  // be called on that function, while functions that are registered afterwards will
    51  // also trigger the hook.
    52  func AddSubscription(s Subscription) {
    53  	for name, def := range registry {
    54  		s(name, def.props, def.overloads)
    55  	}
    56  	subscriptions = append(subscriptions, s)
    57  }
    58  
    59  // GetBuiltinProperties provides low-level access to a built-in function's properties.
    60  // For a better, semantic-rich interface consider using tree.FunctionDefinition
    61  // instead, and resolve function names via ResolvableFunctionReference.Resolve().
    62  func GetBuiltinProperties(name string) (*tree.FunctionProperties, []tree.Overload) {
    63  	def, ok := registry[name]
    64  	if !ok {
    65  		return nil, nil
    66  	}
    67  	return def.props, def.overloads
    68  }
    69  
    70  type definition struct {
    71  	props     *tree.FunctionProperties
    72  	overloads []tree.Overload
    73  }