|
| 1 | +// Copyright 2026 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 analyzer |
| 16 | + |
| 17 | +import ( |
| 18 | + "strings" |
| 19 | + |
| 20 | + "github.com/dolthub/go-mysql-server/sql" |
| 21 | + "github.com/dolthub/go-mysql-server/sql/analyzer" |
| 22 | + "github.com/dolthub/go-mysql-server/sql/plan" |
| 23 | + "github.com/dolthub/go-mysql-server/sql/transform" |
| 24 | + |
| 25 | + "github.com/dolthub/doltgresql/core" |
| 26 | + "github.com/dolthub/doltgresql/core/extensions" |
| 27 | + "github.com/dolthub/doltgresql/core/id" |
| 28 | + "github.com/dolthub/doltgresql/server/functions" |
| 29 | + "github.com/dolthub/doltgresql/server/functions/framework" |
| 30 | + pgnodes "github.com/dolthub/doltgresql/server/node" |
| 31 | + pgtypes "github.com/dolthub/doltgresql/server/types" |
| 32 | +) |
| 33 | + |
| 34 | +// ResolveProcedureDefaults resolves default expressions of routines that are in string format by parsing it into sql.Expression. |
| 35 | +// This function retrieves the procedure overloads and sets CompiledFunction in the Call node. |
| 36 | +func ResolveProcedureDefaults(ctx *sql.Context, a *analyzer.Analyzer, node sql.Node, scope *plan.Scope, selector analyzer.RuleSelector, qFlags *sql.QueryFlags) (sql.Node, transform.TreeIdentity, error) { |
| 37 | + switch n := node.(type) { |
| 38 | + case *pgnodes.Call: |
| 39 | + procCollection, err := core.GetProceduresCollectionFromContext(ctx) |
| 40 | + if err != nil { |
| 41 | + return nil, transform.SameTree, err |
| 42 | + } |
| 43 | + typesCollection, err := core.GetTypesCollectionFromContext(ctx) |
| 44 | + if err != nil { |
| 45 | + return nil, transform.SameTree, err |
| 46 | + } |
| 47 | + schemaName, err := core.GetSchemaName(ctx, nil, n.SchemaName) |
| 48 | + if err != nil { |
| 49 | + return nil, transform.SameTree, err |
| 50 | + } |
| 51 | + procName := id.NewProcedure(schemaName, n.ProcedureName) |
| 52 | + overloads, err := procCollection.GetProcedureOverloads(ctx, procName) |
| 53 | + if err != nil { |
| 54 | + return nil, transform.SameTree, err |
| 55 | + } |
| 56 | + if len(overloads) == 0 { |
| 57 | + if strings.HasPrefix(n.ProcedureName, "dolt_") { |
| 58 | + return nil, transform.SameTree, functions.ErrDoltProcedureSelectOnly |
| 59 | + } |
| 60 | + return nil, transform.SameTree, sql.ErrStoredProcedureDoesNotExist.New(n.ProcedureName) |
| 61 | + } |
| 62 | + |
| 63 | + same := transform.SameTree |
| 64 | + overloadTree := framework.NewOverloads() |
| 65 | + for _, overload := range overloads { |
| 66 | + paramTypes := make([]*pgtypes.DoltgresType, len(overload.ParameterTypes)) |
| 67 | + for i, paramType := range overload.ParameterTypes { |
| 68 | + paramTypes[i], err = typesCollection.GetType(ctx, paramType) |
| 69 | + if err != nil || paramTypes[i] == nil { |
| 70 | + return nil, transform.SameTree, err |
| 71 | + } |
| 72 | + } |
| 73 | + // TODO: we should probably have procedure equivalents instead of converting these to functions |
| 74 | + // probably fine for now since we don't implement/support the differing functionality between the two just yet |
| 75 | + if len(overload.ExtensionName) > 0 { |
| 76 | + if err = overloadTree.Add(framework.CFunction{ |
| 77 | + ID: id.Function(overload.ID), |
| 78 | + ReturnType: pgtypes.Void, |
| 79 | + ParameterTypes: paramTypes, |
| 80 | + Variadic: false, |
| 81 | + IsNonDeterministic: true, |
| 82 | + Strict: false, |
| 83 | + ExtensionName: extensions.LibraryIdentifier(overload.ExtensionName), |
| 84 | + ExtensionSymbol: overload.ExtensionSymbol, |
| 85 | + }); err != nil { |
| 86 | + return nil, transform.SameTree, err |
| 87 | + } |
| 88 | + } else if len(overload.SQLDefinition) > 0 { |
| 89 | + if err = overloadTree.Add(framework.SQLFunction{ |
| 90 | + ID: id.Function(overload.ID), |
| 91 | + ReturnType: pgtypes.Void, |
| 92 | + ParameterNames: overload.ParameterNames, |
| 93 | + ParameterTypes: paramTypes, |
| 94 | + ParameterDefaults: overload.ParameterDefaults, |
| 95 | + Variadic: false, |
| 96 | + IsNonDeterministic: true, |
| 97 | + Strict: false, |
| 98 | + SqlStatement: overload.SQLDefinition, |
| 99 | + SetOf: false, |
| 100 | + }); err != nil { |
| 101 | + return nil, transform.SameTree, err |
| 102 | + } |
| 103 | + } else { |
| 104 | + if err = overloadTree.Add(framework.InterpretedFunction{ |
| 105 | + ID: id.Function(overload.ID), |
| 106 | + ReturnType: pgtypes.Void, |
| 107 | + ParameterNames: overload.ParameterNames, |
| 108 | + ParameterTypes: paramTypes, |
| 109 | + Variadic: false, |
| 110 | + IsNonDeterministic: true, |
| 111 | + Strict: false, |
| 112 | + Statements: overload.Operations, |
| 113 | + }); err != nil { |
| 114 | + return nil, transform.SameTree, err |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + compiledFunction := framework.NewCompiledFunction(n.ProcedureName, n.Exprs, overloadTree, false) |
| 119 | + // fill in default exprs if applicable |
| 120 | + if err := compiledFunction.ResolveDefaultValues(func(defExpr string) (sql.Expression, error) { |
| 121 | + return getDefaultExpr(ctx, a.Catalog, defExpr) |
| 122 | + }); err != nil { |
| 123 | + return nil, transform.SameTree, err |
| 124 | + } |
| 125 | + n.CompiledFunc = compiledFunction |
| 126 | + return node, same, nil |
| 127 | + default: |
| 128 | + return node, transform.SameTree, nil |
| 129 | + } |
| 130 | +} |
0 commit comments