diff --git a/sql_metadata/parser.py b/sql_metadata/parser.py index d003c547..d546fb3d 100644 --- a/sql_metadata/parser.py +++ b/sql_metadata/parser.py @@ -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] diff --git a/test/test_getting_columns.py b/test/test_getting_columns.py index 8f6c20b5..74531760 100644 --- a/test/test_getting_columns.py +++ b/test/test_getting_columns.py @@ -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():