Skip to content

Commit

Permalink
sql/catalog: fix panic inside sequence expression parsing
Browse files Browse the repository at this point in the history
Previously, if the correct overloads were not found for sequence
builtins it was possible for the server to panic. This could happen when
rewriting a CREATE TABLE expression with an invalid sequence builtin
call. To address this, this patch updates the sequence logic to return
the error instead of panicking on it.

Fixes: #133399

Release note (bug fix): Address a panic inside CREATE TABLE AS if
sequence builtin expressions had invalid function overloads.
  • Loading branch information
fqazi committed Oct 30, 2024
1 parent 14e8f7a commit bba8f76
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pkg/sql/catalog/seqexpr/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ func GetSequenceFromFunc(funcExpr *tree.FuncExpr) (*SeqIdentifier, error) {
if len(funcExpr.Exprs) == overload.Types.Length() {
paramTypes, ok := overload.Types.(tree.ParamTypes)
if !ok {
panic(pgerror.Newf(
return nil, pgerror.Newf(
pgcode.InvalidFunctionDefinition,
"%s has invalid argument types", funcExpr.Func.String(),
))
)
}
found = true
for i := 0; i < len(paramTypes); i++ {
Expand All @@ -98,10 +98,10 @@ func GetSequenceFromFunc(funcExpr *tree.FuncExpr) (*SeqIdentifier, error) {
}
}
if !found {
panic(pgerror.New(
return nil, pgerror.New(
pgcode.DatatypeMismatch,
"could not find matching function overload for given arguments",
))
)
}
}
return nil, nil
Expand Down
13 changes: 13 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/create_table
Original file line number Diff line number Diff line change
Expand Up @@ -1097,3 +1097,16 @@ statement ok
SET inject_retry_errors_enabled=false

subtest end


# Addresses a bug where parsing nextval expressions with extra values could end
# end up with a panic when rewriting sequence expressions.
subtest 133399

statement ok
CREATE TABLE v_133399 (c01 INT);

statement error pgcode 42804 could not find matching function overload for given arguments
CREATE TABLE t_133399 AS (SELECT * FROM v_133399 WINDOW window_name AS (ROWS c01 BETWEEN nextval ('abc', 'abc', 'abc') AND c01 PRECEDING));

subtest end

0 comments on commit bba8f76

Please sign in to comment.