Skip to content

Commit

Permalink
Handle when column name wildcard is prefixed by table name #296 (#330)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
macbre and sumartoyo authored Sep 11, 2024
1 parent c2849ea commit 8bdc496
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sql_metadata/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,8 @@ def _resolve_sub_queries(self, column: str) -> List[str]:
return column if isinstance(column, list) else [column]

@staticmethod
def _resolve_nested_query(
# pylint:disable=too-many-return-statements
def _resolve_nested_query( # noqa: C901
subquery_alias: str,
nested_queries_names: List[str],
nested_queries: Dict,
Expand Down Expand Up @@ -845,6 +846,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

0 comments on commit 8bdc496

Please sign in to comment.