代码拉取完成,页面将自动刷新
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package parser
import (
"bytes"
"strings"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
)
// FunctionDefinition implements a reference to the (possibly several)
// overloads for a built-in function.
type FunctionDefinition struct {
// Name is the short name of the function.
Name string
// HasOverloadsNeedingRepeatedEvaluation is true if one or more of
// the overload definitions has needsRepeatedEvaluation set.
// Set e.g. for aggregate functions.
HasOverloadsNeedingRepeatedEvaluation bool
// Definition is the set of overloads for this function name.
Definition []overloadImpl
}
func newFunctionDefinition(name string, def []Builtin) *FunctionDefinition {
hasRowDependentOverloads := false
overloads := make([]overloadImpl, len(def))
for i, d := range def {
overloads[i] = d
if d.needsRepeatedEvaluation {
hasRowDependentOverloads = true
}
}
return &FunctionDefinition{
Name: name,
HasOverloadsNeedingRepeatedEvaluation: hasRowDependentOverloads,
Definition: overloads,
}
}
// funDefs holds pre-allocated FunctionDefinition instances
// for every builtin function. Initialized by setupBuiltins().
var funDefs map[string]*FunctionDefinition
// Format implements the NodeFormatter interface.
func (fd *FunctionDefinition) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString(fd.Name)
}
func (fd *FunctionDefinition) String() string { return AsString(fd) }
// SearchPath represents a list of namespaces to search builtins in.
// The names must be normalized (as per Name.Normalize) already.
type SearchPath []string
// ResolveFunction transforms an UnresolvedName to a FunctionDefinition.
func (n UnresolvedName) ResolveFunction(searchPath SearchPath) (*FunctionDefinition, error) {
fn, err := n.normalizeFunctionName()
if err != nil {
return nil, err
}
if len(fn.selector) > 0 {
// We do not support selectors at this point.
return nil, pgerror.NewErrorf(pgerror.CodeSyntaxError, "invalid function name: %s", n)
}
if d, ok := funDefs[fn.function()]; ok && fn.prefix() == "" {
// Fast path: return early.
return d, nil
}
// Although the conversion from Name to string should go via
// Name.Normalize(), functions are special in that they are
// guaranteed to not contain special Unicode characters. So we can
// use ToLower directly.
// TODO(knz): this will need to be revisited once we allow
// function names to exist in custom namespaces, whose names
// may contain special characters.
prefix := strings.ToLower(fn.prefix())
smallName := strings.ToLower(fn.function())
fullName := smallName
if prefix == "pg_catalog" {
// If the user specified e.g. `pg_catalog.max()` we want to find
// it in the global namespace.
prefix = ""
}
if prefix != "" {
fullName = prefix + "." + smallName
}
def, ok := funDefs[fullName]
if !ok {
found := false
if prefix == "" {
// The function wasn't qualified, so we must search for it via
// the search path first.
for _, alt := range searchPath {
fullName = alt + "." + smallName
if def, ok = funDefs[fullName]; ok {
found = true
break
}
}
}
if !found {
return nil, pgerror.NewErrorf(
pgerror.CodeUndefinedFunctionError, "unknown function: %s()", n)
}
}
return def, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。