JSqlParser icon indicating copy to clipboard operation
JSqlParser copied to clipboard

Error when parsing switch expressions with OR or AND.

Open mgoeminne opened this issue 3 years ago • 3 comments

Describe the bug

First things first: thank you for your work!

A switch expression of a CASE, if containing a OR or a AND, makes the parsing crash.

To Reproduce

SELECT true AND false; 

and

SELECT true OR false;

work fine

SELECT CASE true | false
     WHEN true THEN 1
     ELSE 2
END

and

SELECT CASE true & false
     WHEN true THEN 1
     ELSE 2
END

also have the expected behaviour (with a bitwise expression being returned as the switch expression).

But

SELECT CASE true AND false
         WHEN true THEN 1
         ELSE 2
END;

raises an exception:

Encountered unexpected token: "AND" "AND"

(the issue is similar when AND is replaced by OR).

Expected behavior The last statement should be successfully parsed, with a AndExpression/OrExpression used as the switch expression. At least, https://www.w3schools.com accepts it, and Datagrip sends this kind of statement on Postgres databases.

System

  • Database you are using: my own, mimicking PostreSQL.
  • Java Version: java 12.0.2
  • JSqlParser version: 4.3

mgoeminne avatar Jan 12 '22 15:01 mgoeminne

Confirmed. JSqlParser only allows there a condition. Within the where more complex expressions are allowed:

case when a and b then .. end

wumpz avatar Jan 18 '22 07:01 wumpz

In the when, an Expression is required, while case switches only allow a Condition.

Should I submit a pull request that replaces the Condition with an Expression? By doing so, all the test I proposed in https://github.com/JSQLParser/JSqlParser/pull/1459 do pass.

mgoeminne avatar Jan 18 '22 07:01 mgoeminne

Sorry for the delay. Sure you could provide a PR. But be aware that the performance could degrade.

wumpz avatar Feb 06 '22 20:02 wumpz

Closed, since no further question has been asked.

manticore-projects avatar Nov 12 '22 06:11 manticore-projects