Repository: tobymao/sqlglot

Issue:
Ambiguous column error in qualify_columns when using multiple joins
See the following example.
The optimizer rule `qualify_columns` gives the error: `sqlglot.errors.OptimizeError: Ambiguous column: name`

This correct. But the error is gone when I remove 1 join. 

```
        query = """
            SELECT
                name,
            FROM
                `table_1`
            JOIN `table_2` using (id)
            JOIN `table_3` using (id)
            JOIN `table_4` using (id)
            JOIN `table_5` using (id)
            JOIN `table_6` using (id)
            JOIN `table_7` using (id)
            JOIN `table_8` using (id)
        """
        expression = parse_one(query, read="bigquery")

        optimized_expression = optimize(
            expression,
            schema={
                "table_1": {
                    "id": "INT",
                    "name": "STRING",
                },
                "table_2": {
                    "id": "INT",
                    "name": "STRING",
                },
                "table_3": {
                    "id": "INT",
                    "name": "STRING",
                },
                "table_4": {
                    "id": "INT",
                    "name": "STRING",
                },
                "table_5": {
                    "id": "INT",
                    "name": "STRING",
                },
                "table_6": {
                    "id": "INT",
                    "name": "STRING",
                },
                "table_7": {
                    "id": "INT",
                    "name": "STRING",
                },
                "table_8": {
                    "id": "INT",
                    "name": "STRING",
                },
            },
        )

```


Discussion:

Resolving patch:
diff --git a/sqlglot/optimizer/qualify_columns.py b/sqlglot/optimizer/qualify_columns.py
--- a/sqlglot/optimizer/qualify_columns.py
+++ b/sqlglot/optimizer/qualify_columns.py
@@ -202,12 +202,14 @@ def _get_unambiguous_columns(source_columns):
     unambiguous_columns = {
         col: first_table for col in _find_unique_columns(first_columns)
     }
+    all_columns = set(unambiguous_columns)
 
     for table, columns in source_columns[1:]:
         unique = _find_unique_columns(columns)
-        ambiguous = set(unambiguous_columns).intersection(unique)
+        ambiguous = set(all_columns).intersection(unique)
+        all_columns.update(columns)
         for column in ambiguous:
-            unambiguous_columns.pop(column)
+            unambiguous_columns.pop(column, None)
         for column in unique.difference(ambiguous):
             unambiguous_columns[column] = table
 


Test patch:
diff --git a/tests/fixtures/optimizer/qualify_columns__invalid.sql b/tests/fixtures/optimizer/qualify_columns__invalid.sql
--- a/tests/fixtures/optimizer/qualify_columns__invalid.sql
+++ b/tests/fixtures/optimizer/qualify_columns__invalid.sql
@@ -10,3 +10,4 @@ WITH z AS (SELECT * FROM x) SELECT * FROM x AS z;
 SELECT a FROM x JOIN (SELECT b FROM y WHERE y.b = x.c);
 SELECT a FROM x AS y JOIN (SELECT a FROM y) AS q ON y.a = q.a;
 SELECT q.a FROM (SELECT x.b FROM x) AS z JOIN (SELECT a FROM z) AS q ON z.b = q.a;
+SELECT b FROM x AS a CROSS JOIN y AS b CROSS JOIN y AS c;