Skip to content

Commit

Permalink
Refactor bigquery _parse_table_parts
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas committed May 24, 2023
1 parent a6d5067 commit bae0dce
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions sqlglot/dialects/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit bae0dce

Please sign in to comment.