Skip to content

Commit

Permalink
Fixing a problem with an OP_CONCAT in WhenExpression (#1837)
Browse files Browse the repository at this point in the history
* fix: Concatenation in inner ELSE statement (Second level of Case Expression)

* fix: broken tests

* fix: Delete lookahead(3)
  • Loading branch information
amigalev authored Aug 20, 2023
1 parent 3cae390 commit f05cb7f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -4796,9 +4796,7 @@ Expression CaseWhenExpression() #CaseWhenExpression:
[
<K_ELSE>
(
LOOKAHEAD(3, {!interrupted}) "(" elseExp=CaseWhenExpression() ")" { elseExp = new Parenthesis( elseExp ); }
| LOOKAHEAD(3, {!interrupted}) elseExp=CaseWhenExpression()
| LOOKAHEAD(3, {getAsBoolean(Feature.allowComplexParsing) && !interrupted}) elseExp=Expression()
LOOKAHEAD({getAsBoolean(Feature.allowComplexParsing) && !interrupted}) elseExp=Expression()
| elseExp=SimpleExpression()
)
]
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/net/sf/jsqlparser/expression/CaseExpressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,33 @@ public void testCaseOrSwitch() throws JSQLParserException {
TestUtils.assertExpressionCanBeParsedAndDeparsed("CASE true OR false WHEN true THEN 1 ELSE 2 END", true);
}

@Test
public void testInnerCaseWithConcatInElsePart() throws JSQLParserException {
String query = "SELECT \n" +
"CASE \n" +
" WHEN 1 = 1 \n" +
" THEN \n" +
" CASE \n" +
" WHEN 2 = 2 \n" +
" THEN '2a' \n" +
" ELSE \n" +
" CASE \n" +
" WHEN 1 = 1 \n" +
" THEN \n" +
" CASE \n" +
" WHEN 2 = 2 \n" +
" THEN '2a' \n" +
" ELSE '' \n" +
" END \n" +
" ELSE 'b' \n" +
" END || 'z'\n" +
" END \n" +
" ELSE 'b' \n" +
"END AS tmp\n" +
"FROM test_table";
TestUtils.assertSqlCanBeParsedAndDeparsed(query, true);
}

@Test
public void testCaseInsideBrackets() throws JSQLParserException {
String sqlStr = "SELECT ( CASE\n"
Expand Down

0 comments on commit f05cb7f

Please sign in to comment.