Skip to content

Commit 8bdc496

Browse files
macbresumartoyo
andauthored
Handle when column name wildcard is prefixed by table name #296 (#330)
* Handle when column name wildcard is prefixed by table name * Add test for getting wildcard column with table prefix * Formatting tests * linter: _resolve_nested_query is complex, ignore for now Co-authored-by: Dimas <dimasjkl@gmail.com>
1 parent c2849ea commit 8bdc496

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

sql_metadata/parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,8 @@ def _resolve_sub_queries(self, column: str) -> List[str]:
809809
return column if isinstance(column, list) else [column]
810810

811811
@staticmethod
812-
def _resolve_nested_query(
812+
# pylint:disable=too-many-return-statements
813+
def _resolve_nested_query( # noqa: C901
813814
subquery_alias: str,
814815
nested_queries_names: List[str],
815816
nested_queries: Dict,
@@ -845,6 +846,9 @@ def _resolve_nested_query(
845846
# handle case when column name is used but subquery select all by wildcard
846847
if "*" in subparser.columns:
847848
return column_name
849+
for table in subparser.tables:
850+
if f"{table}.*" in subparser.columns:
851+
return column_name
848852
raise exc # pragma: no cover
849853
resolved_column = subparser.columns[column_index]
850854
return [resolved_column]

test/test_getting_columns.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ def test_getting_columns():
9292
"test",
9393
]
9494
assert Parser("SELECT /* a comment */ bar FROM test_table").columns == ["bar"]
95+
assert (
96+
Parser(
97+
"""
98+
WITH foo AS (SELECT test_table.* FROM test_table)
99+
SELECT foo.bar FROM foo
100+
"""
101+
).columns
102+
== ["test_table.*", "bar"]
103+
)
95104

96105

97106
def test_columns_with_order_by():

0 commit comments

Comments
 (0)