Skip to content

Commit

Permalink
sql: prevent panic when using UDF as EXECUTE argument
Browse files Browse the repository at this point in the history
Fixes #99008

Release note (bug fix): A bug has been fixed that caused nodes to crash
when attempting to `EXECUTE` a prepared statement with an argument that
referenced a user-defined function. This bug was present since
user-defined functions were introduced in version 22.2.
  • Loading branch information
mgartner committed Aug 7, 2023
1 parent cdf6d15 commit 20c43ab
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pkg/ccl/logictestccl/tests/3node-tenant/generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pkg/sql/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func (p *planner) fillInPlaceholders(
}

qArgs := make(tree.QueryArguments, len(params))
var semaCtx tree.SemaContext
for i, e := range params {
idx := tree.PlaceholderIdx(i)

Expand All @@ -54,7 +53,7 @@ func (p *planner) fillInPlaceholders(
}
}
typedExpr, err := schemaexpr.SanitizeVarFreeExpr(
ctx, e, typ, "EXECUTE parameter" /* context */, &semaCtx, volatility.Volatile, true, /*allowAssignmentCast*/
ctx, e, typ, "EXECUTE parameter" /* context */, p.SemaCtx(), volatility.Volatile, true, /*allowAssignmentCast*/
)
if err != nil {
return nil, pgerror.WithCandidateCode(err, pgcode.WrongObjectType)
Expand Down
8 changes: 8 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/udf_prepare
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
statement ok
CREATE FUNCTION f() RETURNS INT LANGUAGE SQL AS $$ SELECT 1 $$

statement ok
PREPARE p AS SELECT $1::INT

statement error pgcode 0A000 cannot evaluate function in this context
EXECUTE p(f())
7 changes: 7 additions & 0 deletions pkg/sql/logictest/tests/fakedist-disk/generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/sql/logictest/tests/fakedist-vec-off/generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/sql/logictest/tests/fakedist/generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/sql/logictest/tests/local-vec-off/generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/sql/logictest/tests/local/generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/sql/sem/eval/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ func (e *evaluator) EvalFuncExpr(ctx context.Context, expr *tree.FuncExpr) (tree
return tree.DNull, err
}

if fn.Body != "" {
return nil, pgerror.Newf(pgcode.FeatureNotSupported, "cannot evaluate function in this context")
}

res, err := fn.Fn.(FnOverload)(ctx, e.ctx(), args)
if err != nil {
return nil, expr.MaybeWrapError(err)
Expand Down

0 comments on commit 20c43ab

Please sign in to comment.