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

Handle when column name wildcard is prefixed by table name #296

3 changes: 3 additions & 0 deletions sql_metadata/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,9 @@ def _resolve_nested_query(
# handle case when column name is used but subquery select all by wildcard
if "*" in subparser.columns:
return column_name
for table in subparser.tables:
if f"{table}.*" in subparser.columns:
return column_name
raise exc # pragma: no cover
resolved_column = subparser.columns[column_index]
return [resolved_column]
Expand Down
9 changes: 9 additions & 0 deletions test/test_getting_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ def test_getting_columns():
"test",
]
assert Parser("SELECT /* a comment */ bar FROM test_table").columns == ["bar"]
assert (
Parser(
"""
WITH foo AS (SELECT test_table.* FROM test_table)
SELECT foo.bar FROM foo
"""
).columns
== ["test_table.*", "bar"]
)


def test_columns_with_order_by():
Expand Down
Loading