Skip to content

Commit

Permalink
plpgsql: handle multiple expressions when one expression is expected
Browse files Browse the repository at this point in the history
Previously, the PLpgSQL parser could panic when the user supplied more
than one expression in a location where only one was expected, for example,
in a return statement. This was because the PLpgSQL parser delegated to
the SQL parser's `ParseExpr` function, which expects exactly one input
expression. This commit returns a syntax error instead of the panic by
switching to use `ParseExprs`, which can handle multiple input expressions.

Informs cockroachdb#109342

Release note: None
  • Loading branch information
DrewKimball committed Oct 11, 2023
1 parent 392c848 commit 066f164
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/sql/plpgsql/parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,5 +463,14 @@ func (l *lexer) GetTypeFromValidSQLSyntax(sqlStr string) (tree.ResolvableTypeRef
}

func (l *lexer) ParseExpr(sqlStr string) (plpgsqltree.Expr, error) {
return parser.ParseExpr(sqlStr)
// Use ParseExprs instead of ParseExpr in order to correctly handle the case
// when multiple expressions are incorrectly passed.
exprs, err := parser.ParseExprs([]string{sqlStr})
if err != nil {
return nil, err
}
if len(exprs) != 1 {
return nil, pgerror.Newf(pgcode.Syntax, "query returned %d columns", len(exprs))
}
return exprs[0], nil
}
15 changes: 15 additions & 0 deletions pkg/sql/plpgsql/parser/testdata/stmt_assign
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ BEGIN
a := NULL;
END

parse
DECLARE
BEGIN
a := 1, 'string';
END
----
at or near ";": syntax error: query returned 2 columns

parse
DECLARE
BEGIN
a := 1, (2, 3, 4, 5);
END
----
at or near ";": syntax error: query returned 2 columns

feature-count
DECLARE
Expand Down

0 comments on commit 066f164

Please sign in to comment.