Repository: tobymao/sqlglot

Issue:
Table incorrectly parsed as column
# Description
Table of format `<schema>.<table>` in a join gets mis-parsed as a column. In the attached example, the table `"usage"."company_names"` gets misparsed as a column. 

### Version details
- Dialect is 'redshift'
- The issue exists on the latest pip version of sqlglot (21.0.2)
- The issue doesn't exist on the latest pip version of sqlglot (20.4.0)

### Correct output with sqlglot==20.4.0
```
Python 3.11.7 (main, Dec  4 2023, 18:10:11) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlglot
from sqlglot import parse_one
sqlglot.__version__>>> from sqlglot import parse_one
>>> sqlglot.__version__
'20.4.0'
>>> dialect = 'redshift'
>>> query = 'SELECT * FROM "usage"."usage" JOIN "usage"."company_names" ON "company_names"."id" = "usage"."customerid"'
>>> parse_one(query, dialect=dialect)
(SELECT expressions: 
  (STAR ), from: 
  (FROM this: 
    (TABLE this: 
      (IDENTIFIER this: usage, quoted: True), db: 
      (IDENTIFIER this: usage, quoted: True))), joins: 
  (JOIN this: 
    (TABLE this: 
      (IDENTIFIER this: company_names, quoted: True), db: 
      (IDENTIFIER this: usage, quoted: True)), on: 
    (EQ this: 
      (COLUMN this: 
        (IDENTIFIER this: id, quoted: True), table: 
        (IDENTIFIER this: company_names, quoted: True)), expression: 
      (COLUMN this: 
        (IDENTIFIER this: customerid, quoted: True), table: 
        (IDENTIFIER this: usage, quoted: True)))))
```        

### Incorrect output with sqlglot==21.0.2
```
Python 3.11.7 (main, Dec  4 2023, 18:10:11) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlglot
from sqlglot import parse_one
sqlglot.__version__>>> from sqlglot import parse_one
>>> sqlglot.__version__
'21.0.2'
>>> dialect = 'redshift'
>>> query = 'SELECT * FROM "usage"."usage" JOIN "usage"."company_names" ON "company_names"."id" = "usage"."customerid"'
>>> parse_one(query, dialect=dialect)
Select(
  expressions=[
    Star()],
  from=From(
    this=Table(
      this=Identifier(this=usage, quoted=True),
      db=Identifier(this=usage, quoted=True))),
  joins=[
    Join(
      this=Column(
        this=Identifier(this=company_names, quoted=True),
        table=Identifier(this=usage, quoted=True)),
      on=EQ(
        this=Column(
          this=Identifier(this=id, quoted=True),
          table=Identifier(this=company_names, quoted=True)),
        expression=Column(
          this=Identifier(this=customerid, quoted=True),
          table=Identifier(this=usage, quoted=True))))])
```
      



Discussion:

Resolving patch:
diff --git a/sqlglot/dialects/redshift.py b/sqlglot/dialects/redshift.py
--- a/sqlglot/dialects/redshift.py
+++ b/sqlglot/dialects/redshift.py
@@ -136,11 +136,11 @@ def _parse_query_modifiers(
                     refs.add(
                         (
                             this.args["from"] if i == 0 else this.args["joins"][i - 1]
-                        ).alias_or_name.lower()
+                        ).this.alias.lower()
                     )
-                    table = join.this
 
-                    if isinstance(table, exp.Table):
+                    table = join.this
+                    if isinstance(table, exp.Table) and not join.args.get("on"):
                         if table.parts[0].name.lower() in refs:
                             table.replace(table.to_column())
             return this


Test patch:
diff --git a/tests/dialects/test_redshift.py b/tests/dialects/test_redshift.py
--- a/tests/dialects/test_redshift.py
+++ b/tests/dialects/test_redshift.py
@@ -1,4 +1,4 @@
-from sqlglot import transpile
+from sqlglot import exp, parse_one, transpile
 from tests.dialects.test_dialect import Validator
 
 
@@ -381,8 +381,6 @@ def test_identity(self):
             "SELECT DATEADD(DAY, 1, DATE('2023-01-01'))",
         )
 
-        self.validate_identity("SELECT * FROM x AS a, a.b AS c, c.d.e AS f, f.g.h.i.j.k AS l")
-
         self.validate_identity(
             """SELECT
   c_name,
@@ -532,3 +530,26 @@ def test_no_schema_binding(self):
                 "redshift": "CREATE OR REPLACE VIEW v1 AS SELECT cola, colb FROM t1 WITH NO SCHEMA BINDING",
             },
         )
+
+    def test_column_unnesting(self):
+        ast = parse_one("SELECT * FROM t.t JOIN t.c1 ON c1.c2 = t.c3", read="redshift")
+        ast.args["from"].this.assert_is(exp.Table)
+        ast.args["joins"][0].this.assert_is(exp.Table)
+        self.assertEqual(ast.sql("redshift"), "SELECT * FROM t.t JOIN t.c1 ON c1.c2 = t.c3")
+
+        ast = parse_one("SELECT * FROM t AS t CROSS JOIN t.c1", read="redshift")
+        ast.args["from"].this.assert_is(exp.Table)
+        ast.args["joins"][0].this.assert_is(exp.Column)
+        self.assertEqual(ast.sql("redshift"), "SELECT * FROM t AS t CROSS JOIN t.c1")
+
+        ast = parse_one(
+            "SELECT * FROM x AS a, a.b AS c, c.d.e AS f, f.g.h.i.j.k AS l", read="redshift"
+        )
+        joins = ast.args["joins"]
+        ast.args["from"].this.assert_is(exp.Table)
+        joins[0].this.this.assert_is(exp.Column)
+        joins[1].this.this.assert_is(exp.Column)
+        joins[2].this.this.assert_is(exp.Dot)
+        self.assertEqual(
+            ast.sql("redshift"), "SELECT * FROM x AS a, a.b AS c, c.d.e AS f, f.g.h.i.j.k AS l"
+        )