|
| 1 | +// Copyright 2025 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 framework |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + "strconv" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/dolthub/go-mysql-server/sql" |
| 24 | + "github.com/lib/pq" |
| 25 | + |
| 26 | + "github.com/dolthub/doltgresql/core/id" |
| 27 | + pgtypes "github.com/dolthub/doltgresql/server/types" |
| 28 | +) |
| 29 | + |
| 30 | +// InterpretedFunction is the implementation of functions created using PL/pgSQL. The created functions are converted to |
| 31 | +// a collection of operations, and an interpreter iterates over those operations to handle the logic. |
| 32 | +type InterpretedFunction struct { |
| 33 | + ID id.Function |
| 34 | + ReturnType *pgtypes.DoltgresType |
| 35 | + ParameterNames []string |
| 36 | + ParameterTypes []*pgtypes.DoltgresType |
| 37 | + Variadic bool |
| 38 | + IsNonDeterministic bool |
| 39 | + Strict bool |
| 40 | + Labels map[string]int |
| 41 | + Statements []InterpreterOperation |
| 42 | +} |
| 43 | + |
| 44 | +var _ FunctionInterface = InterpretedFunction{} |
| 45 | + |
| 46 | +// GetExpectedParameterCount implements the interface FunctionInterface. |
| 47 | +func (iFunc InterpretedFunction) GetExpectedParameterCount() int { |
| 48 | + return len(iFunc.ParameterTypes) |
| 49 | +} |
| 50 | + |
| 51 | +// GetName implements the interface FunctionInterface. |
| 52 | +func (iFunc InterpretedFunction) GetName() string { |
| 53 | + return iFunc.ID.FunctionName() |
| 54 | +} |
| 55 | + |
| 56 | +// GetParameters implements the interface FunctionInterface. |
| 57 | +func (iFunc InterpretedFunction) GetParameters() []*pgtypes.DoltgresType { |
| 58 | + return iFunc.ParameterTypes |
| 59 | +} |
| 60 | + |
| 61 | +// GetReturn implements the interface FunctionInterface. |
| 62 | +func (iFunc InterpretedFunction) GetReturn() *pgtypes.DoltgresType { |
| 63 | + return iFunc.ReturnType |
| 64 | +} |
| 65 | + |
| 66 | +// InternalID implements the interface FunctionInterface. |
| 67 | +func (iFunc InterpretedFunction) InternalID() id.Id { |
| 68 | + return iFunc.ID.AsId() |
| 69 | +} |
| 70 | + |
| 71 | +// IsStrict implements the interface FunctionInterface. |
| 72 | +func (iFunc InterpretedFunction) IsStrict() bool { |
| 73 | + return iFunc.Strict |
| 74 | +} |
| 75 | + |
| 76 | +// Return implements the interface plan.Interpreter. |
| 77 | +func (iFunc InterpretedFunction) Return(ctx *sql.Context) sql.Type { |
| 78 | + return iFunc.ReturnType |
| 79 | +} |
| 80 | + |
| 81 | +// NonDeterministic implements the interface FunctionInterface. |
| 82 | +func (iFunc InterpretedFunction) NonDeterministic() bool { |
| 83 | + return iFunc.IsNonDeterministic |
| 84 | +} |
| 85 | + |
| 86 | +// VariadicIndex implements the interface FunctionInterface. |
| 87 | +func (iFunc InterpretedFunction) VariadicIndex() int { |
| 88 | + // TODO: implement variadic |
| 89 | + return -1 |
| 90 | +} |
| 91 | + |
| 92 | +// querySingleReturn handles queries that are supposed to return a single value. |
| 93 | +func (iFunc InterpretedFunction) querySingleReturn(ctx *sql.Context, stack InterpreterStack, stmt string, targetType *pgtypes.DoltgresType, bindings []string) (val any, err error) { |
| 94 | + if len(bindings) > 0 { |
| 95 | + for i, bindingName := range bindings { |
| 96 | + variable := stack.GetVariable(bindingName) |
| 97 | + if variable == nil { |
| 98 | + return nil, fmt.Errorf("variable `%s` could not be found", bindingName) |
| 99 | + } |
| 100 | + formattedVar, err := variable.Type.FormatValue(variable.Value) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + switch variable.Type.TypCategory { |
| 105 | + case pgtypes.TypeCategory_ArrayTypes, pgtypes.TypeCategory_StringTypes: |
| 106 | + formattedVar = pq.QuoteLiteral(formattedVar) |
| 107 | + } |
| 108 | + stmt = strings.Replace(stmt, "$"+strconv.Itoa(i+1), formattedVar, 1) |
| 109 | + } |
| 110 | + } |
| 111 | + sch, rowIter, _, err := stack.runner.QueryWithBindings(ctx, stmt, nil, nil, nil) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + rows, err := sql.RowIterToRows(ctx, rowIter) |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + if len(sch) != 1 { |
| 120 | + return nil, errors.New("expression does not result in a single value") |
| 121 | + } |
| 122 | + if len(rows) != 1 { |
| 123 | + return nil, errors.New("expression returned multiple result sets") |
| 124 | + } |
| 125 | + if len(rows[0]) != 1 { |
| 126 | + return nil, errors.New("expression returned multiple results") |
| 127 | + } |
| 128 | + if targetType == nil { |
| 129 | + return rows[0][0], nil |
| 130 | + } |
| 131 | + fromType, ok := sch[0].Type.(*pgtypes.DoltgresType) |
| 132 | + if !ok { |
| 133 | + fromType, err = pgtypes.FromGmsTypeToDoltgresType(sch[0].Type) |
| 134 | + if err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + } |
| 138 | + castFunc := GetAssignmentCast(fromType, targetType) |
| 139 | + if castFunc == nil { |
| 140 | + // TODO: try I/O casting |
| 141 | + return nil, errors.New("no valid cast for return value") |
| 142 | + } |
| 143 | + return castFunc(ctx, rows[0][0], targetType) |
| 144 | +} |
| 145 | + |
| 146 | +// queryMultiReturn handles queries that may return multiple values over multiple rows. |
| 147 | +func (iFunc InterpretedFunction) queryMultiReturn(ctx *sql.Context, stack InterpreterStack, stmt string, bindings []string) (rowIter sql.RowIter, err error) { |
| 148 | + if len(bindings) > 0 { |
| 149 | + for i, bindingName := range bindings { |
| 150 | + variable := stack.GetVariable(bindingName) |
| 151 | + if variable == nil { |
| 152 | + return nil, fmt.Errorf("variable `%s` could not be found", bindingName) |
| 153 | + } |
| 154 | + formattedVar, err := variable.Type.FormatValue(variable.Value) |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + switch variable.Type.TypCategory { |
| 159 | + case pgtypes.TypeCategory_ArrayTypes, pgtypes.TypeCategory_StringTypes: |
| 160 | + formattedVar = pq.QuoteLiteral(formattedVar) |
| 161 | + } |
| 162 | + stmt = strings.Replace(stmt, "$"+strconv.Itoa(i+1), formattedVar, 1) |
| 163 | + } |
| 164 | + } |
| 165 | + _, rowIter, _, err = stack.runner.QueryWithBindings(ctx, stmt, nil, nil, nil) |
| 166 | + return rowIter, err |
| 167 | +} |
| 168 | + |
| 169 | +// enforceInterfaceInheritance implements the interface FunctionInterface. |
| 170 | +func (iFunc InterpretedFunction) enforceInterfaceInheritance(error) {} |
0 commit comments