sqlfluff icon indicating copy to clipboard operation
sqlfluff copied to clipboard

[TSQL] Error parsing statements with a case expression in where clause

Open martinswan opened this issue 11 months ago • 1 comments

Search before asking

  • [X] I searched the issues and found no similar issues.

What Happened

SQL code that we have in or system contains a case expression in the where clause. The code runs fine in the system, but SQLFluff is not able to parse this code.

Expected Behaviour

I would expect the following code to run without error:

import sqlfluff

query = f"""SELECT
  *
FROM MyDatabase.MySchema.MyTable
WHERE CASE WHEN 1 = 1 THEN 'Y' ELSE 'N' END <> 'Y'
"""

sqlfluff.parse(query, dialect='tsql')

The code does run without error if you update it to include parentheses around the case expression likes so:

import sqlfluff

query = f"""SELECT
  *
FROM MyDatabase.MySchema.MyTable
WHERE (CASE WHEN 1 = 1 THEN 'Y' ELSE 'N' END) <> 'Y'
"""

sqlfluff.parse(query, dialect='tsql')

Observed Behaviour

Instead of the code running without error, I get the following error:

sqlfluff.api.simple.APIParsingError: Found 1 issues while parsing string.
Line 4, Position 45: Found unparsable section: "<> 'Y'"

How to reproduce

Run the following code to get this error:

import sqlfluff

query = f"""SELECT
  *
FROM MyDatabase.MySchema.MyTable
WHERE CASE WHEN 1 = 1 THEN 'Y' ELSE 'N' END <> 'Y'
"""

sqlfluff.parse(query, dialect='tsql')

Dialect

tsql

Version

sqlfluff 2.3.5 python 3.12.1

Configuration

(no configuration)

Are you willing to work on and submit a PR to address the issue?

  • [ ] Yes I am willing to submit a PR!

Code of Conduct

martinswan avatar Apr 02 '24 21:04 martinswan

Thanks for the report! This appears to have been fixed by #5728.

keraion avatar Apr 03 '24 01:04 keraion