-
Notifications
You must be signed in to change notification settings - Fork 14.4k
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# pylint: disable=unused-argument, import-outside-toplevel, protected-access | ||
|
||
from flask.ctx import AppContext | ||
from pytest_mock import MockFixture | ||
from sqlalchemy import select | ||
from sqlalchemy.dialects.sqlite.base import SQLiteDialect | ||
from sqlalchemy.sql import sqltypes | ||
|
||
|
||
def test_get_fields(app_context: AppContext) -> None: | ||
""" | ||
Test the custom ``_get_fields`` method. | ||
|
||
The method adds custom labels (aliases) to the columns to prevent | ||
collision when referencing record fields. Eg, if we had these two | ||
columns: | ||
|
||
name STRING | ||
project STRUCT<name STRING> | ||
|
||
One could write this query: | ||
|
||
SELECT | ||
`name`, | ||
`project`.`name` | ||
FROM | ||
the_table | ||
|
||
But then both columns would get aliased as "name". | ||
|
||
The custom method will replace the fields so that the final query | ||
looks like this: | ||
|
||
SELECT | ||
`name` AS `name`, | ||
`project`.`name` AS project__name | ||
FROM | ||
the_table | ||
|
||
""" | ||
from superset.db_engine_specs.bigquery import BigQueryEngineSpec | ||
|
||
columns = [{"name": "limit"}, {"name": "name"}, {"name": "project.name"}] | ||
fields = BigQueryEngineSpec._get_fields(columns) | ||
|
||
# generic SQL | ||
query = select(fields) | ||
assert ( | ||
str(query) | ||
== 'SELECT "limit" AS "limit", name AS name, "project.name" AS project__name' | ||
) | ||
|
||
# BigQuery-specific SQL | ||
try: | ||
from pybigquery.sqlalchemy_bigquery import BigQueryDialect | ||
except ModuleNotFoundError: | ||
return | ||
|
||
assert str(query.compile(dialect=BigQueryDialect())) == ( | ||
"SELECT `limit` AS `limit`, `name` AS `name`, " | ||
"`project`.`name` AS `project__name`" | ||
) | ||
|
||
|
||
def test_select_star(mocker: MockFixture, app_context: AppContext) -> None: | ||
""" | ||
Test the ``select_star`` method. | ||
|
||
The method removes pseudo-columns from structures inside arrays. While these | ||
pseudo-columns show up as "columns" for metadata reasons, we can't select them | ||
in the query, as opposed to fields from non-array structures. | ||
""" | ||
from superset.db_engine_specs.bigquery import BigQueryEngineSpec | ||
|
||
cols = [ | ||
{ | ||
"name": "trailer", | ||
"type": sqltypes.ARRAY(sqltypes.JSON()), | ||
"nullable": True, | ||
"comment": None, | ||
"default": None, | ||
"precision": None, | ||
"scale": None, | ||
"max_length": None, | ||
}, | ||
{ | ||
"name": "trailer.key", | ||
"type": sqltypes.String(), | ||
"nullable": True, | ||
"comment": None, | ||
"default": None, | ||
"precision": None, | ||
"scale": None, | ||
"max_length": None, | ||
}, | ||
{ | ||
"name": "trailer.value", | ||
"type": sqltypes.String(), | ||
"nullable": True, | ||
"comment": None, | ||
"default": None, | ||
"precision": None, | ||
"scale": None, | ||
"max_length": None, | ||
}, | ||
{ | ||
"name": "trailer.email", | ||
"type": sqltypes.String(), | ||
"nullable": True, | ||
"comment": None, | ||
"default": None, | ||
"precision": None, | ||
"scale": None, | ||
"max_length": None, | ||
}, | ||
] | ||
|
||
# mock the database so we can compile the query | ||
database = mocker.MagicMock() | ||
database.compile_sqla_query = lambda query: str( | ||
query.compile(dialect=SQLiteDialect()) | ||
) | ||
|
||
# use SQLite dialect so we don't need the BQ dependency | ||
engine = mocker.MagicMock() | ||
engine.dialect = SQLiteDialect() | ||
|
||
sql = BigQueryEngineSpec.select_star( | ||
database=database, | ||
table_name="my_table", | ||
engine=engine, | ||
schema=None, | ||
limit=100, | ||
show_cols=True, | ||
indent=True, | ||
latest_partition=False, | ||
cols=cols, | ||
) | ||
assert ( | ||
sql | ||
== """SELECT trailer AS trailer | ||
FROM my_table | ||
LIMIT ? | ||
OFFSET ?""" | ||
) | ||
|
||
# BigQuery-specific SQL | ||
try: | ||
from pybigquery.sqlalchemy_bigquery import BigQueryDialect | ||
except ModuleNotFoundError: | ||
return | ||
|
||
database.compile_sqla_query = lambda query: str( | ||
query.compile(dialect=BigQueryDialect()) | ||
) | ||
engine.dialect = BigQueryDialect() | ||
|
||
sql = BigQueryEngineSpec.select_star( | ||
database=database, | ||
table_name="my_table", | ||
engine=engine, | ||
schema=None, | ||
limit=100, | ||
show_cols=True, | ||
indent=True, | ||
latest_partition=False, | ||
cols=cols, | ||
) | ||
assert ( | ||
sql | ||
== """SELECT `trailer` AS `trailer` | ||
FROM `my_table` | ||
LIMIT :param_1""" | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was implemented in 2018 (#5655) and is no longer needed. I tested loading the preview for a table with nested records and it works fine when the
_get_fields
method is removed, each part gets quoted separately:Note that the data preview query quotes the parts correctly, even though it fails (for an unrelated reason).