Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bigquery): quote all parts of table names #9141

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion ibis/backends/bigquery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,27 @@
return node


def _force_quote_table(table: sge.Table) -> sge.Table:
"""Force quote all the parts of a bigquery path.

The BigQuery identifier quoting semantics are bonkers
https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#identifiers

my-table is OK, but not mydataset.my-table
cpcloud marked this conversation as resolved.
Show resolved Hide resolved

mytable-287 is OK, but not mytable-287a

Just quote everything.
"""
for key in ("this", "db", "catalog"):
if (val := table.args[key]) is not None:
if isinstance(val, sg.exp.Identifier) and not val.quoted:
val.args["quoted"] = True
else:
table.args[key] = sg.to_identifier(val, quoted=True)
return table


class Backend(SQLBackend, CanCreateDatabase, CanCreateSchema):
name = "bigquery"
compiler = BigQueryCompiler()
Expand Down Expand Up @@ -1025,14 +1046,21 @@
try:
table = sg.parse_one(name, into=sge.Table, read="bigquery")
except sg.ParseError:
table = sg.table(name, db=dataset, catalog=project_id)
table = sg.table(

Check warning on line 1049 in ibis/backends/bigquery/__init__.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/bigquery/__init__.py#L1049

Added line #L1049 was not covered by tests
name,
db=dataset,
catalog=project_id,
quoted=self.compiler.quoted,
)
else:
if table.args["db"] is None:
table.args["db"] = dataset

if table.args["catalog"] is None:
table.args["catalog"] = project_id

table = _force_quote_table(table)

Check warning on line 1062 in ibis/backends/bigquery/__init__.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/bigquery/__init__.py#L1062

Added line #L1062 was not covered by tests

column_defs = [
sge.ColumnDef(
this=sg.to_identifier(name, quoted=self.compiler.quoted),
Expand Down
14 changes: 14 additions & 0 deletions ibis/backends/bigquery/tests/system/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,17 @@ def test_client_with_regional_endpoints(project_id, credentials, dataset_id):
df = alltypes.execute()
assert df.empty
assert not len(alltypes.to_pyarrow())


def test_create_table_from_memtable_needs_quotes(project_id, credentials):
con = ibis.bigquery.connect(
project_id=project_id,
dataset_id=f"{project_id}.testing",
credentials=credentials,
)

con.create_table(
"region-table",
schema=ibis.schema(dict(its_always="str", quoting="int")),
)
con.drop_table("region-table")
27 changes: 26 additions & 1 deletion ibis/backends/bigquery/tests/unit/test_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

import pytest
import sqlglot as sg

from ibis.backends.bigquery import client
from ibis.backends.bigquery import _force_quote_table, client


@pytest.mark.parametrize(
Expand Down Expand Up @@ -30,3 +31,27 @@ def test_parse_project_and_dataset_raises_error():
expected_message = "data-project.my_dataset.table is not a BigQuery dataset"
with pytest.raises(ValueError, match=expected_message):
client.parse_project_and_dataset("my-project", "data-project.my_dataset.table")


@pytest.mark.parametrize(
"bq_path_str, expected",
[
("ibis-gbq.ibis_gbq_testing.argle", "`ibis-gbq`.`ibis_gbq_testing`.`argle`"),
(
"ibis-gbq.ibis_gbq_testing.28argle",
"`ibis-gbq`.`ibis_gbq_testing`.`28argle`",
),
("mytable-287a", "`mytable-287a`"),
("myproject.mydataset.my-table", "`myproject`.`mydataset`.`my-table`"),
("my-dataset.mytable", "`my-dataset`.`mytable`"),
(
"a-7b0a.dev_test_dataset.test_ibis5",
"`a-7b0a`.`dev_test_dataset`.`test_ibis5`",
),
],
)
def test_force_quoting(bq_path_str, expected):
table = sg.parse_one(bq_path_str, into=sg.exp.Table, read="bigquery")
table = _force_quote_table(table)

assert table.sql("bigquery") == expected
Loading