Skip to content

Commit

Permalink
plpgsql/parser: fix parsing for escaped strings
Browse files Browse the repository at this point in the history
This commit adds support for scanning an escaped string constant in a
PL/pgSQL routine. This is necessary because escaped strings have imbalanced
quotes (like `e'foobar''`), which previously caused the scanner to return
an "unterminated string" error.

Informs #106368

Release note (bug fix): Fixed a bug that could result in a syntax error
if a PL/pgSQL routine was created with an escaped string constant in
the routine body. This bug has existed since v23.2.
  • Loading branch information
DrewKimball committed Feb 28, 2024
1 parent 83d7ea8 commit 2a423d8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
53 changes: 32 additions & 21 deletions pkg/sql/plpgsql/parser/testdata/const
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
parse
DECLARE
x STRING := e'1GW\'';
BEGIN
INSERT INTO tab SELECT e'1GW\'';
END
----
DECLARE
x STRING := e'1GW\'';
BEGIN
INSERT INTO tab SELECT e'1GW\'';
END;
-- normalized!
DECLARE
x STRING := (e'1GW\'');
BEGIN
INSERT INTO tab SELECT (e'1GW\'');
END;
-- fully parenthesized
DECLARE
x STRING := '_';
BEGIN
INSERT INTO tab SELECT '_';
END;
-- literals removed
DECLARE
_ STRING := e'1GW\'';
BEGIN
INSERT INTO _ SELECT e'1GW\'';
END;
-- identifiers removed

parse
DECLARE
x STRING := b'1GW\'';
Expand Down Expand Up @@ -94,27 +126,6 @@ INSERT INTO _ SELECT B'010101';
END;
-- identifiers removed

# TODO(drewk): This is a valid constant.
error
DECLARE
x STRING := e'1GW\'';
BEGIN
INSERT INTO tab SELECT e'1GW\'';
END
----
at or near ";": at or near "EOF": syntax error
DETAIL: source SQL:
SET ROW (e'1GW\''
^
--
source SQL:
DECLARE
x STRING := e'1GW\'';
BEGIN
INSERT INTO tab SELECT e'1GW\'';
^
HINT: try \h SET SESSION

error
DECLARE
x STRING := 'foo;
Expand Down
14 changes: 14 additions & 0 deletions pkg/sql/scanner/plpgsql_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ func (s *PLpgSQLScanner) Scan(lval ScanSymType) {
s.scanIdent(lval)
return

case 'e', 'E':
// Escaped string?
if s.peek() == singleQuote {
// [eE]'[^']'
s.lastAttemptedID = int32(lexbase.SCONST)
s.pos++
if s.scanString(lval, singleQuote, true /* allowEscapes */, true /* requireUTF8 */) {
lval.SetID(lexbase.SCONST)
}
return
}
s.scanIdent(lval)
return

case '.':
switch t := s.peek(); {
case t == '.': // ..
Expand Down

0 comments on commit 2a423d8

Please sign in to comment.