JakeWheat / simple-sql-parser

SQL parser in Haskell
http://jakewheat.github.io/simple-sql-parser/latest
Other
82 stars 29 forks source link

Function call cannot be parsed in value expression #8

Closed jtdaugherty closed 5 years ago

jtdaugherty commented 8 years ago

Both of the following queries fail to parse using 0.4.1:

select abs(1);
select * from rel where abs(foo) > 10;

The error I get from the parser in both cases looks like this one:

"<query>" (line 1, column 28):
unexpected "("
expecting value expression
keyword not allowed here: abs
TomMD commented 8 years ago

The problem here is the blacklist was preventing parsing names that are reserved words. However, reserved words, such as most functions, are valid in the select list. One solution is to ditch the black list - I took this approach as that works fine for me (https://github.com/TomMD/simple-sql-parser/commit/2a9bd58322e5183bee33637f0da4c214a691dab8).

Another solution is to allow parsing function names only if they are being applied to arguments. I suspect this would require deeper changes.

JakeWheat commented 8 years ago

Thanks Tom, you are right. I think ditching the black list completely might be a little bit risky, but you can remove all the function names (like abs) from the reserved keywords list, which shouldn't cause any problems.

The second solution you mention is how it should be fixed and isn't too difficult to implement.

jtdaugherty commented 8 years ago

@JakeWheat - is the latter something you can implement and release to Hackage? Or should we look into it ourselves?

TomMD commented 8 years ago

As a matter of update, I said:

One solution is to ditch the black list - I took this approach as that works fine for me (TomMD@2a9bd58).

This did NOT work fine in the end. As you can see on my repo's master branch, the hack solution that does work is to ignore the black list during name parsing in the case of select list idenExprs only - not in the general case of parsing any name. This is still not the second solution, which would be nice.

JakeWheat commented 8 years ago

The best quick solution is to comment out "abs" in the blacklist, and do the same for any other functions you want to use. Not using the blacklist at all has the risk that things like 'from' will be parsed as a column alias and then many queries will fail to parse, (and probably other similar problems to this).

I need this fixed soon as well, so I will be working on this, and the fix should be in master branch in the next few days. I'm not sure when I will release, so please bug me in a week or two if I haven't done a release and you need one.

JakeWheat commented 5 years ago

The fixes for this are now in the released version. The parser uses a list of function names which are also keywords to make it parse. Thanks again for the report.

jtdaugherty commented 5 years ago

Thank you!