Skip to content

Commit 9fe66c0

Browse files
committed
fix: parse CASE with a signed switch value
isCaseExpressionAhead reads CASE followed by + or - as a column named case, so CASE -1 WHEN -1 THEN 'x' END failed to parse in a statement, and parseExpression returned only the subtraction case - 1 and dropped the rest. The LOOKAHEAD(3) it replaced in the Pratt rewrite took these as CASE expressions. After CASE + or CASE -, look for a WHEN of this CASE, outside of brackets and nested CASE expressions, before anything that ends the expression. A column named case followed by a sign, as in case - 1 or case - CASE WHEN a THEN 1 END, still parses as before.
1 parent c84ad0e commit 9fe66c0

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

‎src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,9 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
13151315
String img = t2.image;
13161316
if (img.length() == 1) {
13171317
char c = img.charAt(0);
1318-
if (c == '=' || c == '>' || c == '<' || c == '+' || c == '-' ||
1318+
// CASE -x WHEN ... has a signed switch value, CASE - x subtracts from a column
1319+
if (c == '+' || c == '-') return isWhenAheadOfCaseOperand(t2);
1320+
if (c == '=' || c == '>' || c == '<' ||
13191321
c == '*' || c == ',' || c == '.' || c == ';') return false;
13201322
}
13211323
}
@@ -1326,6 +1328,30 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
13261328
}
13271329
}
13281330

1331+
/**
1332+
* Whether a WHEN of this CASE follows the given token, outside of brackets and nested CASE
1333+
* expressions, before anything that ends the expression.
1334+
*/
1335+
private boolean isWhenAheadOfCaseOperand(Token t) {
1336+
int depth = 0;
1337+
for (t = nextWithItemToken(t); !interrupted && t.kind != EOF; t = nextWithItemToken(t)) {
1338+
if (t.kind == OPENING_BRACKET || t.kind == K_CASE) {
1339+
depth++;
1340+
} else if (depth > 0) {
1341+
if (t.kind == CLOSING_BRACKET || t.kind == K_END) {
1342+
depth--;
1343+
}
1344+
} else if (t.kind == K_WHEN) {
1345+
return true;
1346+
} else if (t.kind == CLOSING_BRACKET || t.kind == K_END || t.kind == K_THEN
1347+
|| t.kind == K_ELSE || t.kind == K_FROM || t.kind == K_WHERE
1348+
|| ",".equals(t.image) || ";".equals(t.image)) {
1349+
return false;
1350+
}
1351+
}
1352+
return false;
1353+
}
1354+
13291355
/**
13301356
* XML constructor functions take a bare leading keyword before their first argument,
13311357
* e.g. XMLPARSE(CONTENT expr), XMLPARSE(DOCUMENT expr), XMLELEMENT(NAME ident, ...).

‎src/test/java/net/sf/jsqlparser/expression/CaseExpressionTest.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@
1010
package net.sf.jsqlparser.expression;
1111

1212
import net.sf.jsqlparser.JSQLParserException;
13+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
1314
import net.sf.jsqlparser.test.TestUtils;
1415
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.params.ParameterizedTest;
17+
import org.junit.jupiter.params.provider.ValueSource;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
1521

1622
/**
1723
* @author Mathieu Goeminne
@@ -166,4 +172,32 @@ void testFormatClause() throws JSQLParserException {
166172
String sqlStr = "SELECT CAST('18-12-03' AS DATE FORMAT 'YY-MM-DD') AS string_to_date";
167173
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
168174
}
175+
176+
@ParameterizedTest
177+
@ValueSource(strings = {
178+
"CASE -1 WHEN -1 THEN 'negative' ELSE 'other' END",
179+
"CASE -a WHEN 1 THEN 2 END",
180+
"CASE +a WHEN 1 THEN 2 END",
181+
"CASE -a + b WHEN 1 THEN 2 END",
182+
"CASE -t.a WHEN 1 THEN 2 END",
183+
"CASE -abs(a) WHEN 1 THEN 2 END",
184+
"CASE -(a) WHEN 1 THEN 2 END"})
185+
void testSignedSwitchExpression(String expressionStr) throws JSQLParserException {
186+
TestUtils.assertExpressionCanBeParsedAndDeparsed(expressionStr, true);
187+
CaseExpression caseExpression = assertInstanceOf(CaseExpression.class,
188+
CCJSqlParserUtil.parseExpression(expressionStr));
189+
assertEquals(expressionStr.substring(5, expressionStr.indexOf(" WHEN")),
190+
caseExpression.getSwitchExpression().toString());
191+
}
192+
193+
@ParameterizedTest
194+
@ValueSource(strings = {
195+
"case - 1",
196+
"case + 1",
197+
"case - CASE WHEN a THEN 1 END",
198+
"case - (CASE a WHEN 1 THEN 2 END)"})
199+
void testCaseColumnFollowedBySign(String expressionStr) throws JSQLParserException {
200+
TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT " + expressionStr + " FROM t", true);
201+
assertInstanceOf(BinaryExpression.class, CCJSqlParserUtil.parseExpression(expressionStr));
202+
}
169203
}

0 commit comments

Comments
 (0)