From bba8f765ad0aafb368727bbc72a057aae5c2ce89 Mon Sep 17 00:00:00 2001 From: Faizan Qazi Date: Wed, 30 Oct 2024 16:16:25 +0000 Subject: [PATCH] sql/catalog: fix panic inside sequence expression parsing 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. --- pkg/sql/catalog/seqexpr/sequence.go | 8 ++++---- pkg/sql/logictest/testdata/logic_test/create_table | 13 +++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/pkg/sql/catalog/seqexpr/sequence.go b/pkg/sql/catalog/seqexpr/sequence.go index f9937ef8f187..bf3a5e60e5c5 100644 --- a/pkg/sql/catalog/seqexpr/sequence.go +++ b/pkg/sql/catalog/seqexpr/sequence.go @@ -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++ { @@ -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 diff --git a/pkg/sql/logictest/testdata/logic_test/create_table b/pkg/sql/logictest/testdata/logic_test/create_table index e46d9b95e9d0..e24f90eb88bb 100644 --- a/pkg/sql/logictest/testdata/logic_test/create_table +++ b/pkg/sql/logictest/testdata/logic_test/create_table @@ -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