@@ -675,6 +675,34 @@ def test_nested_resolver_unresolvable_reference():
675675 assert "nonexistent.col" in p .columns
676676
677677
678+ def test_cte_with_subquery_and_star_alias ():
679+ # Solved: https://github.com/macbre/sql-metadata/issues/392
680+ p = Parser ("""with x as (select d.nbr, d.af_pk
681+ from test_db.test_table3 d)
682+ select q.hx_id, q.text
683+ from (select prod_code, s.*
684+ from testdb.test_table s
685+ inner join testdb.test_table2 p on s.s1_fk = p.p1_sk
686+ ) q
687+ inner join x on q.s2_fk = x.af_pk""" )
688+ assert p .tables == [
689+ "test_db.test_table3" , "testdb.test_table" , "testdb.test_table2"
690+ ]
691+ assert p .with_names == ["x" ]
692+ assert "testdb.test_table.*" in p .columns
693+
694+
695+ def test_bracketed_select_with_cte_and_column_alias ():
696+ # Solved: https://github.com/macbre/sql-metadata/issues/326
697+ p = Parser ("""with a as (select id, a from tbl1),
698+ with b as (select id, b from tbl2)
699+ (select a.id, a.a + b.b as t
700+ from a left join b on a.id = b.id)""" )
701+ assert p .tables == ["tbl1" , "tbl2" ]
702+ assert p .with_names == ["a" , "b" ]
703+ assert p .columns == ["id" , "a" , "b" ]
704+
705+
678706def test_with_queries_empty_when_no_cte ():
679707 """A query with no CTEs returns empty with_queries."""
680708 p = Parser ("SELECT * FROM t" )
0 commit comments