mozilla/moz-sql-parser

Select CASE with NULL is formatted to None in the sql

Closed this issue · 6 comments

Hi im having issues formatting a query with a case to sql string. The None that is present in the python dict is formatted as None in the sql. Example

{'select': [{'value': 'a'}, {'value': {'case': [{'when': {'eq': ['some_columns', {'literal': 'Bob'}]}, 'then': {'literal': 'helloworld'}}, None]}, 'name': 'some_columns'}], 'from': 'mytable'}

yields the sql:

SELECT a, CASE WHEN some_columns = 'Bob' THEN 'helloworld' ELSE None END AS some_columns FROM mytable;

Ive written a test which I can create a pull req for. I would love to help fix the issue, but im not really familiar with the grammer and pythonparser.

Note that the above dict is generated when inserting the sql:

SELECT a, CASE WHEN some_columns = 'Bob' THEN 'helloworld' ELSE NULL END AS some_columns FROM mytable;

but im pretty sure that that is expected behaviour.

Thank you! I no longer have access to this repo, so I am working over here: https://github.com/klahnakoski/moz-sql-parser

I will keep you up to date on my progress.

@klahnakoski Ill recreate the issue there next week and add a test in a pull request if I can have access! Btw, im planning to use some of the parsing and formatting for our data pipelines. Next to mysql and postgres a part of queries are run on google BQ, but I think this is not supported?

@abelstam12 please close this issue. I do not have permission.

@abelstam12 The formatter has not been given much time, and I would expect it to fail for many of your statements. If you want to help solve this problem please open another issue: You need only paste the correct SQL: I can make tests from that.

Thank you. Have a good weekend.

@abelstam12

a part of queries are run on google BQ, but I think this is not supported?

That is correct: The parser will not work on most BQ queries. But, if you open an issue and paste problem SQL then I can update the parser to handle it.