diff --git a/sqlglot/dialects/bigquery.py b/sqlglot/dialects/bigquery.py index 7e48c100d4..4a371a0f38 100644 --- a/sqlglot/dialects/bigquery.py +++ b/sqlglot/dialects/bigquery.py @@ -18,7 +18,7 @@ timestrtotime_sql, ts_or_ds_to_date_sql, ) -from sqlglot.helper import seq_get +from sqlglot.helper import seq_get, split_num_words from sqlglot.tokens import TokenType E = t.TypeVar("E", bound=exp.Expression) @@ -231,16 +231,17 @@ def _parse_table_part(self, schema: bool = False) -> t.Optional[exp.Expression]: return this def _parse_table_parts(self, schema: bool = False) -> exp.Expression: - def quote_unsafe_table_part(part: exp.Expression) -> exp.Expression: - if isinstance(part, exp.Identifier): - part.set("quoted", not exp.SAFE_IDENTIFIER_RE.match(part.name)) - return part - table = super()._parse_table_parts(schema=schema) if isinstance(table.this, exp.Identifier) and "." in table.name: - table = exp.to_table(table.name, dialect="bigquery") - for part in table.parts: - part.transform(quote_unsafe_table_part, copy=False) + catalog, db, this, *rest = ( + t.cast(t.Optional[exp.Expression], exp.to_identifier(x)) + for x in split_num_words(table.name, ".", 3) + ) + + if rest and this: + this = exp.Dot.build(t.cast(t.List[exp.Expression], [this, *rest])) + + table = exp.Table(this=this, db=db, catalog=catalog) return table