Repository: tobymao/sqlglot

Issue:
[Postgres 14.4] Parser does not support subquery after lateral keyword
Example (valid sql):
```
select art from table1 inner join lateral (select art from table2) table2 on table2.art=table2.art
```
Result:
```
ParseError: Expected table name. Line 1, Col: 35.
  select art from table1 inner join lateral (select art from table2) table2 on table2.art=table2.art;

Required keyword: 'this' missing for <class 'sqlglot.expressions.Table'>. Line 1, Col: 35.
  select art from table1 inner join lateral (select art from table2) table2 on table2.art=table2.art;

Expected VIEW after LATERAL. Line 1, Col: 43.
  select art from table1 inner join lateral (select art from table2) table2 on table2.art=table2.art;

... and 2 more
```

Discussion:

Patch:
diff --git a/sqlglot/generator.py b/sqlglot/generator.py
index b569e123..bb4d6764 100644
--- a/sqlglot/generator.py
+++ b/sqlglot/generator.py
@@ -630,6 +630,8 @@ class Generator:
 
     def lateral_sql(self, expression):
         this = self.sql(expression, "this")
+        if isinstance(expression.this, exp.Subquery):
+            return f"LATERAL{self.sep()}{this}"
         op_sql = self.seg(
             f"LATERAL VIEW{' OUTER' if expression.args.get('outer') else ''}"
         )
diff --git a/sqlglot/parser.py b/sqlglot/parser.py
index 63011266..201b2f31 100644
--- a/sqlglot/parser.py
+++ b/sqlglot/parser.py
@@ -1070,8 +1070,12 @@ class Parser:
         if not self._match(TokenType.LATERAL):
             return None
 
-        if not self._match(TokenType.VIEW):
-            self.raise_error("Expected VIEW after LATERAL")
+        subquery = self._parse_select(table=True)
+
+        if subquery:
+            return self.expression(exp.Lateral, this=subquery)
+        elif not self._match(TokenType.VIEW):
+            self.raise_error("Expected subquery or VIEW after LATERAL")
 
         outer = self._match(TokenType.OUTER)
 
@@ -1105,7 +1109,7 @@ class Parser:
         if not self._match(TokenType.JOIN):
             return None
 
-        kwargs = {"this": self._parse_table()}
+        kwargs = {"this": self._parse_lateral() or self._parse_table()}
 
         if side:
             kwargs["side"] = side.text


Test patch:
diff --git a/tests/dialects/test_dialect.py b/tests/dialects/test_dialect.py
index 658d942b..d81b8b8d 100644
--- a/tests/dialects/test_dialect.py
+++ b/tests/dialects/test_dialect.py
@@ -629,6 +629,14 @@ class TestDialect(Validator):
             },
         )
 
+    def test_lateral_subquery(self):
+        self.validate_identity(
+            "SELECT art FROM tbl1 INNER JOIN LATERAL (SELECT art FROM tbl2) AS tbl2 ON tbl1.art = tbl2.art"
+        )
+        self.validate_identity(
+            "SELECT * FROM tbl AS t LEFT JOIN LATERAL (SELECT * FROM b WHERE b.t_id = t.t_id) AS t ON TRUE"
+        )
+
     def test_set_operators(self):
         self.validate_all(
             "SELECT * FROM a UNION SELECT * FROM b",