You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
parse IF function error when first param(boolean) include logical operation
OK: sum(if(column1 in('value1', 'value2'), 1, 0)) as tcp_logs
ERROR: sum(if(column1 in ('value1', 'value2') and column2 = 'value3', 1, 0))
To Reproduce
Steps to reproduce the behavior:
Example SQL
select
sum(if(column1 in('value1', 'value2'), 1, 0)) as tcp_logs,
sum(if(column1 in ('value1', 'value2') and column2 = 'value3', 1, 0)) as base_tcp_logs
from
table1
where
recv_time >= toDateTime('2021-07-20 00:00:00')
and recv_time < toDateTime('2021-07-21 00:00:00')
Parsing this SQL using JSqlParser with this statements
CCJSqlParserUtil.parse(sql)
Maybe you want to use the extra brackets temporarily until we have fixed that problem thoroughly.
Thank you again for reporting, it is a good and very interesting bug report.
Sorry for being verbose: the failure is about detecting the Sum() as as Column instead as a Function. Left side shows the success, right side show the fails:
assertExpressionCanBeParsedAndDeparsed(
"if( test1 IN ( 'value1', 'value2' ) AND column2 = 'value3', 1, 0 ) ", true);
The IN Expression prevents from detection a Function production. Technically the LOOKAHEAD(Function()) fails and so we move on to the Column() Production.
@wumpz I need some help here, this thing is above my pay grade.
this works exist(1) AND 2=2 and will be parsed into Function with aComplexExpressionList of an AndExpression with a SQLCondition based on the ExistsExpression
this does not work a in (1) AND 2=2 although it is literally the same construct Grammar wise. But somehow the InExpression is not triggered for the SQLCondition and this lets the test for Function fail and we move wrongly to the Column instead
For the testing purpose I have tried already to reduce the InExpression to a minimum functionality like <K_IN> "(" foo=SimpleExpressionList(true) ")" but still no success.
I simply can't understand why SQLCondition is triggered correctly for the ExistsExpression but fails for the InExpression.
assertExpressionCanBeParsedAndDeparsed(
"exist(1) AND 2=2", true);
assertExpressionCanBeParsedAndDeparsed(
"a in (1,2) AND 2=2", true);
Activity
manticore-projects commentedon Jul 23, 2021
Greetings,
thank you for the qualified report.
Unfortunately I was not able to reproduce the issue as it parses just fine for me. You can try online and interactively.
Are you using the expression within a more complex statement maybe, with more nesting/brackets?
manticore-projects commentedon Jul 23, 2021
Nevermind, I can reproduce it, but only with the latest 4.2-Snapshot:
I do suspect, that the
XORexpression introduced in 4.1 contributes to that problem.As a bridge-gap work around you could use the in-official Pre 4.1 snapshot from https://github.com/manticore-projects/jsqlformatter/releases/download/0.1.6/jsqlparser-4.1-SNAPSHOT.jar which is used for the JSQLFormatter.
I will bisect the issue over the weekend and hopefully solve it.
manticore-projects commentedon Jul 23, 2021
Further findings:
INexpression as argument of a boolean expressionINexpression with extra brackets will also work around:@Test public void testIssue1284() throws JSQLParserException { // succeeds assertSqlCanBeParsedAndDeparsed( "SELECT Sum( if( (column1 IN ( 'value1', 'value2' )) AND column2 = 'value3', 1, 0 ) ) ", true); // fails assertSqlCanBeParsedAndDeparsed( "SELECT Sum( if( column1 IN ( 'value1', 'value2' ) AND column2 = 'value3', 1, 0 ) ) ", true); }Maybe you want to use the extra brackets temporarily until we have fixed that problem thoroughly.
Thank you again for reporting, it is a good and very interesting bug report.
manticore-projects commentedon Jul 23, 2021
Sorry for being verbose: the failure is about detecting the

Sum()as asColumninstead as aFunction. Left side shows thesuccess, right side show thefails:manticore-projects commentedon Jul 23, 2021
Further simplification:
The
INExpression prevents from detection aFunctionproduction. Technically theLOOKAHEAD(Function())fails and so we move on to theColumn()Production.manticore-projects commentedon Jul 23, 2021
@wumpz I need some help here, this thing is above my pay grade.
this works
exist(1) AND 2=2and will be parsed intoFunctionwith aComplexExpressionListof anAndExpressionwith aSQLConditionbased on theExistsExpressionthis does not work
a in (1) AND 2=2although it is literally the same construct Grammar wise. But somehow theInExpressionis not triggered for theSQLConditionand this lets the test forFunctionfail and we move wrongly to theColumninsteadFor the testing purpose I have tried already to reduce the InExpression to a minimum functionality like
<K_IN> "(" foo=SimpleExpressionList(true) ")"but still no success.I simply can't understand why SQLCondition is triggered correctly for the
ExistsExpressionbut fails for theInExpression.manticore-projects commentedon Jul 24, 2021
Ok, a good night sleep and 2 mugs of coffee later I actually DO understand what is going on:
there is a Production
NameExpressionListwhich is defined asSimpleExpression() <K_IN> SimpleExpression()NamedExpressionListis tested priorComplexExpressionListand so when it catches we never reach theComplexExpressionListNow, the Problem is, that your particular example MEANS a ComplexExpression but looks to the parser like a
NameExpressionList:Your example triggers only because of the
INexpression.Now, that I understand the root cause we may find a solution eventually.
Separate MySQL Special String Functions accepting Named Argument Sepa…
manticore-projects commentedon Jul 24, 2021
I think I solved it.
Separate MySQL Special String Functions accepting Named Argument Sepa…