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): explicitly quote columns in select_star #16822

Merged
merged 5 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 4 additions & 2 deletions superset/db_engine_specs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,9 +952,11 @@ def select_star( # pylint: disable=too-many-arguments,too-many-locals
if (show_cols or latest_partition) and not cols:
cols = database.get_columns(table_name, schema)

if show_cols:
fields = cls._get_fields(cols)
quote = engine.dialect.identifier_preparer.quote
if show_cols:
# Explicitly quote all column names, as BigQuery doesn't quote column
# names that are also identifiers (eg, "limit") by default.
fields = [text(quote(col["name"])) for col in cols]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this going to affect all dbs? Do you think we should create a property on the db engine spec like "force_column_quotes"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it will. It should be safe, and note that we use the same method to quote schemas and tables below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@villebro thoughts on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect the BQ dialect to automatically quote reserved keywords. I tried this quickly on sqlite:

create table bad_colnames("limit" varchar(10), "offset" varchar(10), regular varchar(10));

here's what it rendered:

SELECT "limit",
       "offset",
       regular
FROM main.bad_colnames
LIMIT 100
OFFSET 0

So I'd rather we submit a PR on the BQ connector to make sure reserved keywords are quoted correctly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree this is probably quite safe, I think @eschutho's recommendation to add a property for forcing quotes would be a good solution, so as to avoid adding workarounds that aren't necessary for all engines.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, SQLite does it the right way, BigQuery is the culprit here.

I'll file a ticket upstream and try to make a PR, and incorporate @eschutho's suggestion into this PR so we don't have to wait for a new release upstream.

Thanks, @villebro!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, looks like someone changed the behavior of _get_fields in BigQuery: https://github.com/apache/superset/blob/master/superset/db_engine_specs/bigquery.py#L284-L296

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This goes back 3+ years: #5655

if schema:
full_table_name = quote(schema) + "." + quote(table_name)
else:
Expand Down
6 changes: 3 additions & 3 deletions tests/integration_tests/model_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ def test_select_star(self):
assert (
textwrap.dedent(
"""\
SELECT "source" AS "source",
"target" AS "target",
"value" AS "value"
SELECT "source",
"target",
"value"
FROM "energy_usage"
LIMIT 100"""
)
Expand Down