uber / queryparser

Parsing and analysis of Vertica, Hive, and Presto SQL.
MIT License
1.08k stars 147 forks source link

getColumns errors for some Presto queries that use union #52

Closed bachung-stripe closed 2 years ago

bachung-stripe commented 5 years ago

I'm getting some errors when trying to getColumns for certain Presto queries that use union.

For example: getColumns $ fst $ parseAndResolve "select aAlias from (select a as aAlias, b from foo union select a as aAlias, b from foo)" or getColumns $ fst $ parseAndResolve "select a from (select a, b from foo union select a, b from foo)" will fail with

*** Exception: column alias missing from aliasMap: ColumnAliasId (-1)
CallStack (from HasCallStack):
  error, called at /src/Database/Sql/Util/Columns.hs:118:28 in main:Database.Sql.Util.Columns

But not getColumns $ fst $ parseAndResolve "select c from (select a, b from foo union select a, b from foo)" or getColumns $ fst $ parseAndResolve "select a, b from foo union select a, b from foo"

(where parseAndResolve is using the Presto query parser)

This also happens if you use intersect instead of union

h4v0kh3l1 commented 5 years ago

@bachung-stripe Thanks for reporting this, I'm able to reproduce this.

Have you seen this in any other dialect parser?

bachung-stripe commented 5 years ago

I can't get the queries to parse in Vertica, but in Hive, I get the same error in getColumns.

In Hive: getColumns $ fst $ parseAndResolve "SELECT fooAlias.a from (select a, b from foo union select a, b from foo) as fooAlias"

I'm not super familiar with the differences between each dialect though.

h4v0kh3l1 commented 5 years ago

Got it, I'll see if I can get a bugfix in by next week

h4v0kh3l1 commented 5 years ago

I haven't had the time to solve this, for now I'll put my debugging branch up (https://github.com/uber/queryparser/tree/heli/fix_getColumns). I have a test target that the bugfix needs to pass, as well as some QOL improvements in the testing output.

Hopefully I can make progress on this, until then I'll update if I can crack the bug. Anyone else is also welcome to fix and open a pull request.