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

Inconsistent and sometimes incomplete list of columns

Open bigluck opened this issue 3 years ago • 2 comments

Ciao, I’m playing with the library to get the list of all fields accessed by a given query, but I’m getting inconsistent results based on the complexity of the SQL query.

Let’s start with a simple example like this one:



SELECT p.ID AS test, p.*
FROM MYSCHEMA.MYTABLE.PERSON AS p
WHERE 1

Result: ['MYSCHEMA.MYTABLE.PERSON.ID', 'MYSCHEMA.MYTABLE.PERSON.*']

Everything works as expected, but if I run a more straightforward SQL query like:

SELECT *
FROM MYSCHEMA.MYTABLE.PERSON
WHERE 1

I get a completely different response: ['*']; I was expecting [‘MYSCHEMA.MYTABLE.PERSON.*’] instead.

By testing a more complex query like this one, I get a completely unusable result:

SELECT
    CASE
        WHEN (
            SELECT min("ID")
            FROM MYSCHEMA.MYTABLE1
        ) > (
            SELECT max(ID)
            FROM MYSCHEMA.MYTABLE2
        ) THEN TRUE
        WHEN (
            SELECT max(ID)
            FROM MYSCHEMA.MYTABLE1
        ) < (
            SELECT min(ID)
            from MYSCHEMA.MYTABLE2
        ) THEN TRUE
        ELSE FALSE
    END AS NO_COMMON_DATA

The result is now a generic [‘ID’] event if it should be: [‘MYSCHEMA.MYTABLE1.ID’, ‘MYSCHEMA.MYTABLE2.ID’]

Finally, if I test this last query:

SELECT COUNT(*) FROM HUBSPOT.DEAL_PIPELINE

I get an empty array [] instead of [‘HUBSPOT.DEAL_PIPELINE.*’]

Who can help me? Thanks again

bigluck avatar May 26 '22 17:05 bigluck

Currently, we don't infer the table if you do not provide it (or alias of it) in a query as this would work only for the selects from a single table.

collerek avatar Jun 15 '22 11:06 collerek

@collerek - yay, you're back! 🙂

macbre avatar Jun 17 '22 08:06 macbre