github.com/dolthub/go-mysql-server@v0.18.0/sql/functions.go (about) 1 // Copyright 2020-2021 Dolthub, Inc. 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 sql 16 17 // Function is a function defined by the user that can be applied in a SQL query. 18 type Function interface { 19 // NewInstance returns a new instance of the function to evaluate against rows 20 NewInstance([]Expression) (Expression, error) 21 // FunctionName returns the name of this function 22 FunctionName() string 23 // isFunction is a private method to restrict implementations of Function 24 isFunction() 25 } 26 27 // FunctionProvider is an interface that allows custom functions to be provided. It's usually (but not always) 28 // implemented by a DatabaseProvider. 29 type FunctionProvider interface { 30 // Function returns the function with the name provided, case-insensitive 31 Function(ctx *Context, name string) (Function, error) 32 } 33 34 type CreateFunc0Args func() Expression 35 type CreateFunc1Args func(e1 Expression) Expression 36 type CreateFunc2Args func(e1, e2 Expression) Expression 37 type CreateFunc3Args func(e1, e2, e3 Expression) Expression 38 type CreateFunc4Args func(e1, e2, e3, e4 Expression) Expression 39 type CreateFunc5Args func(e1, e2, e3, e4, e5 Expression) Expression 40 type CreateFunc6Args func(e1, e2, e3, e4, e5, e6 Expression) Expression 41 type CreateFunc7Args func(e1, e2, e3, e4, e5, e6, e7 Expression) Expression 42 type CreateFuncNArgs func(args ...Expression) (Expression, error) 43 44 type ( 45 // Function0 is a function with 0 arguments. 46 Function0 struct { 47 Name string 48 Fn CreateFunc0Args 49 } 50 // Function1 is a function with 1 argument. 51 Function1 struct { 52 Name string 53 Fn CreateFunc1Args 54 } 55 // Function2 is a function with 2 arguments. 56 Function2 struct { 57 Name string 58 Fn CreateFunc2Args 59 } 60 // Function3 is a function with 3 arguments. 61 Function3 struct { 62 Name string 63 Fn CreateFunc3Args 64 } 65 // Function4 is a function with 4 arguments. 66 Function4 struct { 67 Name string 68 Fn CreateFunc4Args 69 } 70 // Function5 is a function with 5 arguments. 71 Function5 struct { 72 Name string 73 Fn CreateFunc5Args 74 } 75 // Function6 is a function with 6 arguments. 76 Function6 struct { 77 Name string 78 Fn CreateFunc6Args 79 } 80 // Function7 is a function with 7 arguments. 81 Function7 struct { 82 Name string 83 Fn CreateFunc7Args 84 } 85 // FunctionN is a function with variable number of arguments. This function 86 // is expected to return ErrInvalidArgumentNumber if the arity does not 87 // match, since the check has to be done in the implementation. 88 FunctionN struct { 89 Name string 90 Fn CreateFuncNArgs 91 } 92 ) 93 94 var _ Function = Function0{} 95 var _ Function = Function1{} 96 var _ Function = Function2{} 97 var _ Function = Function3{} 98 var _ Function = Function4{} 99 var _ Function = Function5{} 100 var _ Function = Function6{} 101 var _ Function = Function7{} 102 var _ Function = FunctionN{} 103 104 func NewFunction0(name string, fn func() Expression) Function0 { 105 return Function0{ 106 Name: name, 107 Fn: fn, 108 } 109 } 110 111 func (fn Function0) NewInstance(args []Expression) (Expression, error) { 112 if len(args) != 0 { 113 return nil, ErrInvalidArgumentNumber.New(fn.Name, 0, len(args)) 114 } 115 116 return fn.Fn(), nil 117 } 118 119 func (fn Function1) NewInstance(args []Expression) (Expression, error) { 120 if len(args) != 1 { 121 return nil, ErrInvalidArgumentNumber.New(fn.Name, 1, len(args)) 122 } 123 124 return fn.Fn(args[0]), nil 125 } 126 127 func (fn Function2) NewInstance(args []Expression) (Expression, error) { 128 if len(args) != 2 { 129 return nil, ErrInvalidArgumentNumber.New(fn.Name, 2, len(args)) 130 } 131 132 return fn.Fn(args[0], args[1]), nil 133 } 134 135 func (fn Function3) NewInstance(args []Expression) (Expression, error) { 136 if len(args) != 3 { 137 return nil, ErrInvalidArgumentNumber.New(fn.Name, 3, len(args)) 138 } 139 140 return fn.Fn(args[0], args[1], args[2]), nil 141 } 142 143 func (fn Function4) NewInstance(args []Expression) (Expression, error) { 144 if len(args) != 4 { 145 return nil, ErrInvalidArgumentNumber.New(fn.Name, 4, len(args)) 146 } 147 148 return fn.Fn(args[0], args[1], args[2], args[3]), nil 149 } 150 151 func (fn Function5) NewInstance(args []Expression) (Expression, error) { 152 if len(args) != 5 { 153 return nil, ErrInvalidArgumentNumber.New(fn.Name, 5, len(args)) 154 } 155 156 return fn.Fn(args[0], args[1], args[2], args[3], args[4]), nil 157 } 158 159 func (fn Function6) NewInstance(args []Expression) (Expression, error) { 160 if len(args) != 6 { 161 return nil, ErrInvalidArgumentNumber.New(fn.Name, 6, len(args)) 162 } 163 164 return fn.Fn(args[0], args[1], args[2], args[3], args[4], args[5]), nil 165 } 166 167 func (fn Function7) NewInstance(args []Expression) (Expression, error) { 168 if len(args) != 7 { 169 return nil, ErrInvalidArgumentNumber.New(fn.Name, 7, len(args)) 170 } 171 172 return fn.Fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6]), nil 173 } 174 175 func (fn FunctionN) NewInstance(args []Expression) (Expression, error) { 176 return fn.Fn(args...) 177 } 178 179 func (fn Function0) FunctionName() string { return fn.Name } 180 func (fn Function1) FunctionName() string { return fn.Name } 181 func (fn Function2) FunctionName() string { return fn.Name } 182 func (fn Function3) FunctionName() string { return fn.Name } 183 func (fn Function4) FunctionName() string { return fn.Name } 184 func (fn Function5) FunctionName() string { return fn.Name } 185 func (fn Function6) FunctionName() string { return fn.Name } 186 func (fn Function7) FunctionName() string { return fn.Name } 187 func (fn FunctionN) FunctionName() string { return fn.Name } 188 189 func (Function0) isFunction() {} 190 func (Function1) isFunction() {} 191 func (Function2) isFunction() {} 192 func (Function3) isFunction() {} 193 func (Function4) isFunction() {} 194 func (Function5) isFunction() {} 195 func (Function6) isFunction() {} 196 func (Function7) isFunction() {} 197 func (FunctionN) isFunction() {} 198 199 // UnsupportedFunctionStub is a marker interface for function stubs that are unsupported 200 type UnsupportedFunctionStub interface { 201 IsUnsupported() bool 202 } 203 204 // FunctionExpression is an Expression that represents a function. 205 type FunctionExpression interface { 206 Expression 207 FunctionName() string 208 Description() string 209 }