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

     1  // Copyright 2021 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 tree
    16  
    17  type FunctionArg interface {
    18  	NodeFormatter
    19  	Expr
    20  	GetName(ctx *FmtCtx) string
    21  	GetType(ctx *FmtCtx) string
    22  }
    23  
    24  type FunctionArgImpl struct {
    25  	FunctionArg
    26  }
    27  
    28  // container holding list of arguments in udf
    29  type FunctionArgs []FunctionArg
    30  
    31  type FunctionArgDecl struct {
    32  	FunctionArgImpl
    33  	Name       *UnresolvedName
    34  	Type       ResolvableTypeReference
    35  	DefaultVal Expr
    36  }
    37  
    38  type ReturnType struct {
    39  	Type ResolvableTypeReference
    40  }
    41  
    42  func (node *FunctionArgDecl) Format(ctx *FmtCtx) {
    43  	if node.Name != nil {
    44  		node.Name.Format(ctx)
    45  		ctx.WriteByte(' ')
    46  	}
    47  	node.Type.(*T).InternalType.Format(ctx)
    48  	if node.DefaultVal != nil {
    49  		ctx.WriteString(" default ")
    50  		ctx.PrintExpr(node, node.DefaultVal, true)
    51  	}
    52  }
    53  
    54  func (node *FunctionArgDecl) GetName(ctx *FmtCtx) string {
    55  	node.Name.Format(ctx)
    56  	return ctx.String()
    57  }
    58  
    59  func (node *FunctionArgDecl) GetType(ctx *FmtCtx) string {
    60  	node.Type.(*T).InternalType.Format(ctx)
    61  	return ctx.String()
    62  }
    63  
    64  func (node *ReturnType) Format(ctx *FmtCtx) {
    65  	node.Type.(*T).InternalType.Format(ctx)
    66  }
    67  
    68  type FunctionName struct {
    69  	Name objName
    70  }
    71  
    72  type CreateFunction struct {
    73  	statementImpl
    74  	Name       *FunctionName
    75  	Args       FunctionArgs
    76  	ReturnType *ReturnType
    77  	Body       string
    78  	Language   string
    79  }
    80  
    81  type DropFunction struct {
    82  	statementImpl
    83  	Name *FunctionName
    84  	Args FunctionArgs
    85  }
    86  
    87  func (node *FunctionName) Format(ctx *FmtCtx) {
    88  	if node.Name.ExplicitCatalog {
    89  		ctx.WriteString(string(node.Name.CatalogName))
    90  		ctx.WriteByte('.')
    91  	}
    92  	if node.Name.ExplicitSchema {
    93  		ctx.WriteString(string(node.Name.SchemaName))
    94  		ctx.WriteByte('.')
    95  	}
    96  	ctx.WriteString(string(node.Name.ObjectName))
    97  }
    98  
    99  func (node *FunctionName) HasNoNameQualifier() bool {
   100  	return !node.Name.ExplicitCatalog && !node.Name.ExplicitSchema
   101  }
   102  
   103  func (node *CreateFunction) Format(ctx *FmtCtx) {
   104  	ctx.WriteString("create function ")
   105  
   106  	node.Name.Format(ctx)
   107  
   108  	ctx.WriteString(" (")
   109  
   110  	for i, def := range node.Args {
   111  		if i != 0 {
   112  			ctx.WriteString(",")
   113  			ctx.WriteByte(' ')
   114  		}
   115  		def.Format(ctx)
   116  	}
   117  
   118  	ctx.WriteString(")")
   119  	ctx.WriteString(" returns ")
   120  
   121  	node.ReturnType.Format(ctx)
   122  
   123  	ctx.WriteString(" language ")
   124  	ctx.WriteString(node.Language)
   125  
   126  	ctx.WriteString(" as '")
   127  
   128  	ctx.WriteString(node.Body)
   129  	ctx.WriteString("'")
   130  }
   131  
   132  func (node *DropFunction) Format(ctx *FmtCtx) {
   133  	ctx.WriteString("drop function ")
   134  	node.Name.Format(ctx)
   135  	ctx.WriteString(" (")
   136  
   137  	for i, def := range node.Args {
   138  		if i != 0 {
   139  			ctx.WriteString(",")
   140  			ctx.WriteByte(' ')
   141  		}
   142  		def.Format(ctx)
   143  	}
   144  
   145  	ctx.WriteString(")")
   146  }
   147  
   148  func NewFunctionArgDecl(n *UnresolvedName, t ResolvableTypeReference, d Expr) *FunctionArgDecl {
   149  	return &FunctionArgDecl{
   150  		Name:       n,
   151  		Type:       t,
   152  		DefaultVal: d,
   153  	}
   154  }
   155  
   156  func NewFuncName(name Identifier, prefix ObjectNamePrefix) *FunctionName {
   157  	return &FunctionName{
   158  		Name: objName{
   159  			ObjectName:       name,
   160  			ObjectNamePrefix: prefix,
   161  		},
   162  	}
   163  }
   164  
   165  func NewReturnType(t ResolvableTypeReference) *ReturnType {
   166  	return &ReturnType{
   167  		Type: t,
   168  	}
   169  }
   170  
   171  func (node *CreateFunction) GetStatementType() string { return "CreateFunction" }
   172  func (node *CreateFunction) GetQueryType() string     { return QueryTypeDDL }
   173  
   174  func (node *DropFunction) GetStatementType() string { return "DropFunction" }
   175  func (node *DropFunction) GetQueryType() string     { return QueryTypeDDL }