Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/format_repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ paths=`find . -maxdepth 1 -mindepth 1 ! -name ".idea" ! -name ".git" ! -name ".g

goimports -w -local github.com/dolthub/doltgresql $paths

bad_files=$(find $paths -name '*.go' | while read f; do
bad_files=$(find $paths -name '*.go' ! -path ".idea/*" | while read f; do
if [[ $(awk '/import \(/{flag=1;next}/\)/{flag=0}flag' < $f | egrep -c '$^') -gt 2 ]]; then
echo $f
fi
Expand Down
27 changes: 27 additions & 0 deletions server/expression/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2025 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package expression

import (
"github.com/dolthub/doltgresql/server/functions/framework"
pgtypes "github.com/dolthub/doltgresql/server/types"
)

// Init handles all setup needed for this package.
func Init() {
framework.NewUnsafeLiteral = func(val any, t *pgtypes.DoltgresType) framework.LiteralInterface {
return NewUnsafeLiteral(val, t)
}
}
3 changes: 3 additions & 0 deletions server/functions/framework/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func RegisterFunction(f FunctionInterface) {
case Function4:
name := strings.ToLower(f.Name)
Catalog[name] = append(Catalog[name], f)
case InterpretedFunction:
name := strings.ToLower(f.ID.FunctionName())
Catalog[name] = append(Catalog[name], f)
default:
panic("unhandled function type")
}
Expand Down
25 changes: 22 additions & 3 deletions server/functions/framework/compiled_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

cerrors "github.com/cockroachdb/errors"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/analyzer"
"github.com/dolthub/go-mysql-server/sql/expression"
"gopkg.in/src-d/go-errors.v1"

Expand All @@ -46,15 +47,17 @@ type CompiledFunction struct {
overload overloadMatch
originalTypes []*pgtypes.DoltgresType
callResolved []*pgtypes.DoltgresType
runner analyzer.StatementRunner
stashedErr error
}

var _ sql.FunctionExpression = (*CompiledFunction)(nil)
var _ sql.NonDeterministicExpression = (*CompiledFunction)(nil)
var _ analyzer.Interpreter = (*CompiledFunction)(nil)

// NewCompiledFunction returns a newly compiled function.
func NewCompiledFunction(name string, args []sql.Expression, functions *Overloads, isOperator bool) *CompiledFunction {
return newCompiledFunctionInternal(name, args, functions, functions.overloadsForParams(len(args)), isOperator)
return newCompiledFunctionInternal(name, args, functions, functions.overloadsForParams(len(args)), isOperator, nil)
}

// newCompiledFunctionInternal is called internally, which skips steps that may have already been processed.
Expand All @@ -64,8 +67,16 @@ func newCompiledFunctionInternal(
overloads *Overloads,
fnOverloads []Overload,
isOperator bool,
runner analyzer.StatementRunner,
) *CompiledFunction {
c := &CompiledFunction{Name: name, Arguments: args, IsOperator: isOperator, overloads: overloads, fnOverloads: fnOverloads}
c := &CompiledFunction{
Name: name,
Arguments: args,
IsOperator: isOperator,
overloads: overloads,
fnOverloads: fnOverloads,
runner: runner,
}
// First we'll analyze all the parameters.
originalTypes, err := c.analyzeParameters()
if err != nil {
Expand Down Expand Up @@ -292,6 +303,8 @@ func (c *CompiledFunction) Eval(ctx *sql.Context, row sql.Row) (interface{}, err
return f.Callable(ctx, ([4]*pgtypes.DoltgresType)(c.callResolved), args[0], args[1], args[2])
case Function4:
return f.Callable(ctx, ([5]*pgtypes.DoltgresType)(c.callResolved), args[0], args[1], args[2], args[3])
case InterpretedFunction:
return f.Call(ctx, c.runner, c.callResolved, args)
default:
return nil, cerrors.Errorf("unknown function type in CompiledFunction::Eval")
}
Expand All @@ -309,7 +322,13 @@ func (c *CompiledFunction) WithChildren(children ...sql.Expression) (sql.Express
}

// We have to re-resolve here, since the change in children may require it (e.g. we have more type info than we did)
return newCompiledFunctionInternal(c.Name, children, c.overloads, c.fnOverloads, c.IsOperator), nil
return newCompiledFunctionInternal(c.Name, children, c.overloads, c.fnOverloads, c.IsOperator, c.runner), nil
}

// SetStatementRunner implements the interface analyzer.Interpreter.
func (c *CompiledFunction) SetStatementRunner(ctx *sql.Context, runner analyzer.StatementRunner) sql.Expression {
c.runner = runner
return c
}

// GetQuickFunction returns the QuickFunction form of this function, if it exists. If one does not exist, then this
Expand Down
2 changes: 1 addition & 1 deletion server/functions/framework/intermediate_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ func (f IntermediateFunction) Compile(name string, parameters ...sql.Expression)
if f.Functions == nil {
return nil
}
return newCompiledFunctionInternal(name, parameters, f.Functions, f.AllOverloads, f.IsOperator)
return newCompiledFunctionInternal(name, parameters, f.Functions, f.AllOverloads, f.IsOperator, nil)
}
170 changes: 170 additions & 0 deletions server/functions/framework/interpreter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// Copyright 2025 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package framework

import (
"errors"
"fmt"
"strconv"
"strings"

"github.com/dolthub/go-mysql-server/sql"
"github.com/lib/pq"

"github.com/dolthub/doltgresql/core/id"
pgtypes "github.com/dolthub/doltgresql/server/types"
)

// InterpretedFunction is the implementation of functions created using PL/pgSQL. The created functions are converted to
// a collection of operations, and an interpreter iterates over those operations to handle the logic.
type InterpretedFunction struct {
ID id.Function
ReturnType *pgtypes.DoltgresType
ParameterNames []string
ParameterTypes []*pgtypes.DoltgresType
Variadic bool
IsNonDeterministic bool
Strict bool
Labels map[string]int
Statements []InterpreterOperation
}

var _ FunctionInterface = InterpretedFunction{}

// GetExpectedParameterCount implements the interface FunctionInterface.
func (iFunc InterpretedFunction) GetExpectedParameterCount() int {
return len(iFunc.ParameterTypes)
}

// GetName implements the interface FunctionInterface.
func (iFunc InterpretedFunction) GetName() string {
return iFunc.ID.FunctionName()
}

// GetParameters implements the interface FunctionInterface.
func (iFunc InterpretedFunction) GetParameters() []*pgtypes.DoltgresType {
return iFunc.ParameterTypes
}

// GetReturn implements the interface FunctionInterface.
func (iFunc InterpretedFunction) GetReturn() *pgtypes.DoltgresType {
return iFunc.ReturnType
}

// InternalID implements the interface FunctionInterface.
func (iFunc InterpretedFunction) InternalID() id.Id {
return iFunc.ID.AsId()
}

// IsStrict implements the interface FunctionInterface.
func (iFunc InterpretedFunction) IsStrict() bool {
return iFunc.Strict
}

// Return implements the interface plan.Interpreter.
func (iFunc InterpretedFunction) Return(ctx *sql.Context) sql.Type {
return iFunc.ReturnType
}

// NonDeterministic implements the interface FunctionInterface.
func (iFunc InterpretedFunction) NonDeterministic() bool {
return iFunc.IsNonDeterministic
}

// VariadicIndex implements the interface FunctionInterface.
func (iFunc InterpretedFunction) VariadicIndex() int {
// TODO: implement variadic
return -1
}

// querySingleReturn handles queries that are supposed to return a single value.
func (iFunc InterpretedFunction) querySingleReturn(ctx *sql.Context, stack InterpreterStack, stmt string, targetType *pgtypes.DoltgresType, bindings []string) (val any, err error) {
if len(bindings) > 0 {
for i, bindingName := range bindings {
variable := stack.GetVariable(bindingName)
if variable == nil {
return nil, fmt.Errorf("variable `%s` could not be found", bindingName)
}
formattedVar, err := variable.Type.FormatValue(variable.Value)
if err != nil {
return nil, err
}
switch variable.Type.TypCategory {
case pgtypes.TypeCategory_ArrayTypes, pgtypes.TypeCategory_StringTypes:
formattedVar = pq.QuoteLiteral(formattedVar)
}
stmt = strings.Replace(stmt, "$"+strconv.Itoa(i+1), formattedVar, 1)
}
}
sch, rowIter, _, err := stack.runner.QueryWithBindings(ctx, stmt, nil, nil, nil)
if err != nil {
return nil, err
}
rows, err := sql.RowIterToRows(ctx, rowIter)
if err != nil {
return nil, err
}
if len(sch) != 1 {
return nil, errors.New("expression does not result in a single value")
}
if len(rows) != 1 {
return nil, errors.New("expression returned multiple result sets")
}
if len(rows[0]) != 1 {
return nil, errors.New("expression returned multiple results")
}
if targetType == nil {
return rows[0][0], nil
}
fromType, ok := sch[0].Type.(*pgtypes.DoltgresType)
if !ok {
fromType, err = pgtypes.FromGmsTypeToDoltgresType(sch[0].Type)
if err != nil {
return nil, err
}
}
castFunc := GetAssignmentCast(fromType, targetType)
if castFunc == nil {
// TODO: try I/O casting
return nil, errors.New("no valid cast for return value")
}
return castFunc(ctx, rows[0][0], targetType)
}

// queryMultiReturn handles queries that may return multiple values over multiple rows.
func (iFunc InterpretedFunction) queryMultiReturn(ctx *sql.Context, stack InterpreterStack, stmt string, bindings []string) (rowIter sql.RowIter, err error) {
if len(bindings) > 0 {
for i, bindingName := range bindings {
variable := stack.GetVariable(bindingName)
if variable == nil {
return nil, fmt.Errorf("variable `%s` could not be found", bindingName)
}
formattedVar, err := variable.Type.FormatValue(variable.Value)
if err != nil {
return nil, err
}
switch variable.Type.TypCategory {
case pgtypes.TypeCategory_ArrayTypes, pgtypes.TypeCategory_StringTypes:
formattedVar = pq.QuoteLiteral(formattedVar)
}
stmt = strings.Replace(stmt, "$"+strconv.Itoa(i+1), formattedVar, 1)
}
}
_, rowIter, _, err = stack.runner.QueryWithBindings(ctx, stmt, nil, nil, nil)
return rowIter, err
}

// enforceInterfaceInheritance implements the interface FunctionInterface.
func (iFunc InterpretedFunction) enforceInterfaceInheritance(error) {}
Loading