Skip to content

Commit 3bc8dd3

Browse files
committed
improved :: cast to accept more types of expressions
1 parent 45fad04 commit 3bc8dd3

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,9 +1487,11 @@ Expression BitwiseXor():
14871487
Expression PrimaryExpression():
14881488
{
14891489
Expression retval = null;
1490+
CastExpression castExpr = null;
14901491
Token token = null;
14911492
boolean isInverse = false;
14921493
String tmp = "";
1494+
ColDataType type = null;
14931495
}
14941496
{
14951497

@@ -1530,6 +1532,14 @@ Expression PrimaryExpression():
15301532

15311533
)
15321534

1535+
[ "::" type=ColDataType() {
1536+
castExpr = new CastExpression();
1537+
castExpr.setUseCastKeyword(false);
1538+
castExpr.setLeftExpression(retval);
1539+
castExpr.setType(type);
1540+
retval=castExpr;
1541+
} ]
1542+
15331543
{
15341544
if (isInverse) {
15351545
retval = new InverseExpression(retval);
@@ -1588,11 +1598,7 @@ CastExpression CastExpression():
15881598
boolean useCastKeyword;
15891599
}
15901600
{
1591-
(
15921601
<K_CAST> "(" expression=SimpleExpression() <K_AS> type=ColDataType() ")" { retval.setUseCastKeyword(true); }
1593-
|
1594-
expression=Column() "::" type=ColDataType() { retval.setUseCastKeyword(false); }
1595-
)
15961602

15971603
{
15981604
retval.setLeftExpression(expression);

src/test/java/net/sf/jsqlparser/test/select/SelectTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,16 @@ public void testCastTypeProblem3() throws JSQLParserException {
604604
String stmt = "SELECT col1::varchar (256) FROM tabelle1";
605605
assertSqlCanBeParsedAndDeparsed(stmt);
606606
}
607+
608+
public void testCastTypeProblem4() throws JSQLParserException {
609+
String stmt = "SELECT 5::varchar (256) FROM tabelle1";
610+
assertSqlCanBeParsedAndDeparsed(stmt);
611+
}
612+
613+
public void testCastTypeProblem5() throws JSQLParserException {
614+
String stmt = "SELECT 5.67::varchar (256) FROM tabelle1";
615+
assertSqlCanBeParsedAndDeparsed(stmt);
616+
}
607617

608618
public void testCaseElseAddition() throws JSQLParserException {
609619
String stmt = "SELECT CASE WHEN 1 + 3 > 20 THEN 0 ELSE 1000 + 1 END AS d FROM dual";

0 commit comments

Comments
 (0)