github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/init.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package function
    16  
    17  import (
    18  	"context"
    19  	"sync"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  )
    23  
    24  // init function fills the functionRegister with
    25  // aggregates,	see initAggregateFunction
    26  // builtins,	see initBuiltIns
    27  // operators,	see initOperators
    28  func init() {
    29  	initRelatedStructure()
    30  
    31  	initOperators()
    32  	initBuiltIns()
    33  	initAggregateFunction()
    34  
    35  	initTypeCheckRelated()
    36  }
    37  
    38  var registerMutex sync.RWMutex
    39  
    40  func initRelatedStructure() {
    41  	functionRegister = make([]Functions, FUNCTION_END_NUMBER)
    42  }
    43  
    44  // appendFunction is a method only used at init-functions to add a new function into supported-function list.
    45  // Ensure that no duplicate functions will be added.
    46  func appendFunction(ctx context.Context, fid int, newFunctions Functions) error {
    47  	functionRegister[fid].TypeCheckFn = newFunctions.TypeCheckFn
    48  	functionRegister[fid].Id = newFunctions.Id
    49  	registerMutex.Lock()
    50  	defer registerMutex.Unlock()
    51  	for _, newFunction := range newFunctions.Overloads {
    52  		newFunction.flag = newFunctions.Flag
    53  		newFunction.layout = newFunctions.Layout
    54  
    55  		requiredIndex := len(functionRegister[fid].Overloads)
    56  		if int(newFunction.Index) != requiredIndex {
    57  			return moerr.NewInternalError(ctx, "function (fid = %d, index = %d)'s index should be %d", fid, newFunction.Index, requiredIndex)
    58  		}
    59  		functionRegister[fid].Overloads = append(functionRegister[fid].Overloads, newFunction)
    60  	}
    61  	return nil
    62  }