github.com/matrixorigin/matrixone@v1.2.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 import ( 18 "strings" 19 20 "github.com/matrixorigin/matrixone/pkg/common/moerr" 21 "github.com/matrixorigin/matrixone/pkg/common/reuse" 22 ) 23 24 func init() { 25 reuse.CreatePool[DropFunction]( 26 func() *DropFunction { return &DropFunction{} }, 27 func(d *DropFunction) { d.reset() }, 28 reuse.DefaultOptions[DropFunction](), //. 29 ) //WithEnableChecker() 30 31 reuse.CreatePool[FunctionArgDecl]( 32 func() *FunctionArgDecl { return &FunctionArgDecl{} }, 33 func(f *FunctionArgDecl) { f.reset() }, 34 reuse.DefaultOptions[FunctionArgDecl](), //. 35 ) //WithEnableChecker() 36 37 reuse.CreatePool[ReturnType]( 38 func() *ReturnType { return &ReturnType{} }, 39 func(r *ReturnType) { r.reset() }, 40 reuse.DefaultOptions[ReturnType](), //. 41 ) //WithEnableChecker() 42 43 reuse.CreatePool[FunctionName]( 44 func() *FunctionName { return &FunctionName{} }, 45 func(f *FunctionName) { f.reset() }, 46 reuse.DefaultOptions[FunctionName](), //. 47 ) //WithEnableChecker() 48 49 reuse.CreatePool[CreateFunction]( 50 func() *CreateFunction { return &CreateFunction{} }, 51 func(c *CreateFunction) { c.reset() }, 52 reuse.DefaultOptions[CreateFunction](), //. 53 ) //WithEnableChecker() 54 55 } 56 57 type FunctionArg interface { 58 NodeFormatter 59 Expr 60 GetName(ctx *FmtCtx) string 61 // Deprecated: use plan.GetFunctionArgTypeStrFromAst instead 62 GetType(ctx *FmtCtx) string 63 } 64 65 type FunctionArgImpl struct { 66 FunctionArg 67 } 68 69 // container holding list of arguments in udf 70 type FunctionArgs []FunctionArg 71 72 type FunctionArgDecl struct { 73 FunctionArgImpl 74 Name *UnresolvedName 75 Type ResolvableTypeReference 76 DefaultVal Expr 77 } 78 79 func (node *FunctionArgDecl) Format(ctx *FmtCtx) { 80 if node.Name != nil { 81 node.Name.Format(ctx) 82 ctx.WriteByte(' ') 83 } 84 node.Type.(*T).InternalType.Format(ctx) 85 if node.DefaultVal != nil { 86 ctx.WriteString(" default ") 87 ctx.PrintExpr(node, node.DefaultVal, true) 88 } 89 } 90 91 func (node *FunctionArgDecl) GetName(ctx *FmtCtx) string { 92 if node.Name == nil { 93 return "" 94 } 95 node.Name.Format(ctx) 96 return ctx.String() 97 } 98 99 func (node *FunctionArgDecl) GetType(ctx *FmtCtx) string { 100 node.Type.(*T).InternalType.Format(ctx) 101 return ctx.String() 102 } 103 104 func (node FunctionArgDecl) TypeName() string { return "tree.FunctionArgDecl" } 105 106 func (node *FunctionArgDecl) reset() { 107 // if node.Name != nil { 108 // node.Name.Free() 109 // } 110 *node = FunctionArgDecl{} 111 } 112 113 func (node *FunctionArgDecl) Free() { 114 reuse.Free[FunctionArgDecl](node, nil) 115 } 116 117 type ReturnType struct { 118 Type ResolvableTypeReference 119 } 120 121 func (node *ReturnType) Format(ctx *FmtCtx) { 122 node.Type.(*T).InternalType.Format(ctx) 123 } 124 125 func (node ReturnType) TypeName() string { return "tree.ReturnType" } 126 127 func (node *ReturnType) reset() { 128 *node = ReturnType{} 129 } 130 131 func (node *ReturnType) Free() { 132 reuse.Free[ReturnType](node, nil) 133 } 134 135 type FunctionName struct { 136 Name objName 137 } 138 139 func (node *FunctionName) Format(ctx *FmtCtx) { 140 if node.Name.ExplicitCatalog { 141 ctx.WriteString(string(node.Name.CatalogName)) 142 ctx.WriteByte('.') 143 } 144 if node.Name.ExplicitSchema { 145 ctx.WriteString(string(node.Name.SchemaName)) 146 ctx.WriteByte('.') 147 } 148 ctx.WriteString(string(node.Name.ObjectName)) 149 } 150 151 func (node *FunctionName) HasNoNameQualifier() bool { 152 return !node.Name.ExplicitCatalog && !node.Name.ExplicitSchema 153 } 154 155 func (node FunctionName) TypeName() string { return "tree.FunctionName" } 156 157 func (node *FunctionName) reset() { 158 *node = FunctionName{} 159 } 160 161 func (node *FunctionName) Free() { 162 reuse.Free[FunctionName](node, nil) 163 } 164 165 type FunctionLanguage string 166 167 const ( 168 SQL FunctionLanguage = "sql" 169 PYTHON FunctionLanguage = "python" 170 ) 171 172 type CreateFunction struct { 173 statementImpl 174 Replace bool 175 Name *FunctionName 176 Args FunctionArgs 177 ReturnType *ReturnType 178 Language string 179 Import bool 180 Body string 181 Handler string 182 } 183 184 func NewCreateFunction(replace bool, name *FunctionName, args FunctionArgs, returnType *ReturnType, lang string, import_ bool, body string, handler string) *CreateFunction { 185 createFunction := reuse.Alloc[CreateFunction](nil) 186 createFunction.Replace = replace 187 createFunction.Name = name 188 createFunction.Args = args 189 createFunction.ReturnType = returnType 190 createFunction.Language = lang 191 createFunction.Import = import_ 192 createFunction.Body = body 193 createFunction.Handler = handler 194 return createFunction 195 } 196 197 func (node *CreateFunction) Valid() error { 198 node.Language = strings.ToLower(node.Language) 199 switch node.Language { 200 case string(SQL): 201 if node.Import { 202 return moerr.NewInvalidInputNoCtx("import") 203 } 204 return nil 205 case string(PYTHON): 206 return nil 207 default: 208 return moerr.NewInvalidArgNoCtx("function language", node.Language) 209 } 210 } 211 212 func (node *CreateFunction) Format(ctx *FmtCtx) { 213 ctx.WriteString("create ") 214 if node.Replace { 215 ctx.WriteString("or replace ") 216 } 217 ctx.WriteString("function ") 218 219 // func_name (args) 220 node.Name.Format(ctx) 221 ctx.WriteString(" (") 222 for i, def := range node.Args { 223 if i != 0 { 224 ctx.WriteString(",") 225 ctx.WriteByte(' ') 226 } 227 def.Format(ctx) 228 } 229 ctx.WriteString(")") 230 231 // returns type 232 ctx.WriteString(" returns ") 233 node.ReturnType.Format(ctx) 234 235 // language lang 236 ctx.WriteString(" language ") 237 ctx.WriteString(node.Language) 238 239 // as 'body', or import 'body' 240 if !node.Import { 241 ctx.WriteString(" as '") 242 } else { 243 ctx.WriteString(" import '") 244 } 245 ctx.WriteString(node.Body) 246 ctx.WriteString("'") 247 248 // handler 'handler' 249 if node.Handler != "" { 250 ctx.WriteString(" handler '") 251 ctx.WriteString(node.Handler) 252 ctx.WriteString("'") 253 } 254 } 255 256 func (node *CreateFunction) GetStatementType() string { return "CreateFunction" } 257 func (node *CreateFunction) GetQueryType() string { return QueryTypeDDL } 258 259 func (node CreateFunction) TypeName() string { return "tree.CreateFunction" } 260 261 func (node *CreateFunction) reset() { 262 if node.Name != nil { 263 node.Name.Free() 264 } 265 if node.ReturnType != nil { 266 node.ReturnType.Free() 267 } 268 *node = CreateFunction{} 269 } 270 271 func (node *CreateFunction) Free() { 272 reuse.Free[CreateFunction](node, nil) 273 } 274 275 type DropFunction struct { 276 statementImpl 277 Name *FunctionName 278 Args FunctionArgs 279 } 280 281 func (node DropFunction) TypeName() string { return "tree.DropFunction" } 282 283 func (node *DropFunction) Free() { 284 reuse.Free[DropFunction](node, nil) 285 } 286 287 func (node *DropFunction) reset() { 288 if node.Name != nil { 289 node.Name.Free() 290 } 291 *node = DropFunction{} 292 } 293 294 func (node *DropFunction) Format(ctx *FmtCtx) { 295 ctx.WriteString("drop function ") 296 node.Name.Format(ctx) 297 ctx.WriteString(" (") 298 299 for i, def := range node.Args { 300 if i != 0 { 301 ctx.WriteString(",") 302 ctx.WriteByte(' ') 303 } 304 def.Format(ctx) 305 } 306 307 ctx.WriteString(")") 308 } 309 310 func (node *DropFunction) GetStatementType() string { return "DropFunction" } 311 func (node *DropFunction) GetQueryType() string { return QueryTypeDDL } 312 func NewDropFunction(name *FunctionName, args FunctionArgs) *DropFunction { 313 dropFunction := reuse.Alloc[DropFunction](nil) 314 dropFunction.Name = name 315 dropFunction.Args = args 316 return dropFunction 317 } 318 319 func NewFunctionArgDecl(n *UnresolvedName, t ResolvableTypeReference, d Expr) *FunctionArgDecl { 320 return &FunctionArgDecl{ 321 Name: n, 322 Type: t, 323 DefaultVal: d, 324 } 325 } 326 327 func NewFuncName(name Identifier, prefix ObjectNamePrefix) *FunctionName { 328 return &FunctionName{ 329 Name: objName{ 330 ObjectName: name, 331 ObjectNamePrefix: prefix, 332 }, 333 } 334 } 335 336 func NewReturnType(t ResolvableTypeReference) *ReturnType { 337 return &ReturnType{ 338 Type: t, 339 } 340 }