Repository: tobymao/sqlglot

Issue:
bigquery create table as select with limit

Calling `sqlglot.parse_one(sql, read='bigquery')` fails on this SQL, seemingly because the limit clause gets associated with the create table instead of the select.

```sql
CREATE TABLE IF NOT EXISTS CENSUS.my_table_name AS SELECT `col1`, `col2` FROM CENSUS.my_base_table
  EXCEPT DISTINCT (
    SELECT `col1`, `col2` FROM CENSUS.my_third_table
  )
 LIMIT 0;
```

I'm not sure why you'd write this sort of query with the except distinct clause, but Census seems to generate this SQL.

Discussion:

Patch:
diff --git a/sqlglot/parser.py b/sqlglot/parser.py
index 7cdf4602..6afc11c0 100644
--- a/sqlglot/parser.py
+++ b/sqlglot/parser.py
@@ -4135,8 +4135,8 @@ class Parser(metaclass=_Parser):
         )
 
     def _parse_ddl_select(self) -> t.Optional[exp.Expression]:
-        return self._parse_set_operations(
-            self._parse_select(nested=True, parse_subquery_alias=False)
+        return self._parse_query_modifiers(
+            self._parse_set_operations(self._parse_select(nested=True, parse_subquery_alias=False))
         )
 
     def _parse_transaction(self) -> exp.Expression:


Test patch:
diff --git a/tests/dialects/test_bigquery.py b/tests/dialects/test_bigquery.py
index 45845732..99d8a3ce 100644
--- a/tests/dialects/test_bigquery.py
+++ b/tests/dialects/test_bigquery.py
@@ -31,6 +31,9 @@ class TestBigQuery(Validator):
         self.validate_identity(
             "SELECT * FROM (SELECT * FROM `t`) AS a UNPIVOT((c) FOR c_name IN (v1, v2))"
         )
+        self.validate_identity(
+            "CREATE TABLE IF NOT EXISTS foo AS SELECT * FROM bla EXCEPT DISTINCT (SELECT * FROM bar) LIMIT 0"
+        )
 
         self.validate_all('x <> ""', write={"bigquery": "x <> ''"})
         self.validate_all('x <> """"""', write={"bigquery": "x <> ''"})