sql-metadata icon indicating copy to clipboard operation
sql-metadata copied to clipboard

Issue with column alias' that are the same as a table alias

Open christopherpickering opened this issue 3 years ago • 1 comments

Hey, thanks again for the tool!

I found an issue w/ SQL SERVER sql:

SELECT
  two.col two
FROM
  dbo.table_one one
  Left JOIN dbo.table_two two ON two.id = one.id

Causes a failure

/sql_metadata/parser.py", line 724, in _add_to_columns_aliases_subsection
    section = COLUMNS_SECTIONS[keyword]
KeyError: 'LEFTJOIN'

If I change the alias on the column to something else, then we pass. But both are valid sql.. is it possible for the parser to accept this as valid?

christopherpickering avatar Nov 16 '21 13:11 christopherpickering

Hi, I also want to thank you for this tool. It's awesome!

Let me add some comments to this issue.

I noticed that I get the following error sometimes: AttributeError: 'NoneType' object has no attribute 'normalized'

When I was trying to test it, I found this: My goal is to receive the list of the used table names, so I use the Parser(query_alt).tables method/function. Here is an example code filled with dummy values. The mentioned method works correctly.

query_alt = '''
with main as
(
		select	 a.*,
				 b."attribute_b" as "asd"

		from	"table_a" a

		left join	"table_b" b
		on		 b."id" = a."b_id"
)
----------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------

select	 main.*,
		 other_q."attribute_e"

from	 main

left join
(
        select	 m."o_id"
        from	"main" m

        left join	"table_c" c
        on		 c."id" = m."id_c"

        where	 lower(c."attribute_d") != \'unspecified\'
        group by	 1, 2
) other_q
on		 other_q."o_id" = main."m_id"
'''

BUT if I add an altered name of a column it crashes:

query_alt = '''
with main as
(
		select	 a.*,
				 b."attribute_b" as "asd"

		from	"table_a" a

		left join	"table_b" b
		on		 b."id" = a."b_id"
)
----------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------

select	 main.*,
		 other_q."attribute_e"

from	 main

left join
(
        select	 m."o_id" as "asd2"
        from	"main" m

        left join	"table_c" c
        on		 c."id" = m."id_c"

        where	 lower(c."attribute_d") != \'unspecified\'
        group by	 1, 2
) other_q
on		 other_q."o_id" = main."m_id"
'''
Parser(query_alt).tables

If I remove the visual separator (------) between the WITH and the main select, it works again:

query_alt = '''
with main as
(
		select	 a.*,
				 b."attribute_b" as "asd"

		from	"table_a" a

		left join	"table_b" b
		on		 b."id" = a."b_id"
)

select	 main.*,
		 other_q."attribute_e"

from	 main

left join
(
        select	 m."o_id" as "asd2"
        from	"main" m

        left join	"table_c" c
        on		 c."id" = m."id_c"

        where	 lower(c."attribute_d") != \'unspecified\'
        group by	 1, 2
) other_q
on		 other_q."o_id" = main."m_id"
'''
Parser(query_alt).tables

Could you please take a look at it?

Thank you!

domecrane avatar Dec 22 '21 13:12 domecrane