Skip to content

Commit

Permalink
Fix(postgres): allow FOR clause without FROM in SUBSTRING closes #3377
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas committed Apr 30, 2024
1 parent 60b5c3b commit 3e8de71
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 4 additions & 2 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5315,8 +5315,10 @@ def _parse_substring(self) -> exp.Substring:

if self._match(TokenType.FROM):
args.append(self._parse_bitwise())
if self._match(TokenType.FOR):
args.append(self._parse_bitwise())
if self._match(TokenType.FOR):
if len(args) == 1:
args.append(exp.Literal.number(1))
args.append(self._parse_bitwise())

return self.validate_expression(exp.Substring.from_arg_list(args), args)

Expand Down
9 changes: 8 additions & 1 deletion tests/dialects/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,14 @@ def test_postgres(self):
"MERGE INTO x USING (SELECT id) AS y ON a = b WHEN MATCHED THEN UPDATE SET x.a = y.b WHEN NOT MATCHED THEN INSERT (a, b) VALUES (y.a, y.b)",
"MERGE INTO x USING (SELECT id) AS y ON a = b WHEN MATCHED THEN UPDATE SET a = y.b WHEN NOT MATCHED THEN INSERT (a, b) VALUES (y.a, y.b)",
)
self.validate_identity("SELECT * FROM t1*", "SELECT * FROM t1")
self.validate_identity(
"SELECT * FROM t1*",
"SELECT * FROM t1",
)
self.validate_identity(
"SELECT SUBSTRING('afafa' for 1)",
"SELECT SUBSTRING('afafa' FROM 1 FOR 1)",
)

self.validate_all(
"CREATE TABLE t (c INT)",
Expand Down

0 comments on commit 3e8de71

Please sign in to comment.