Skip to content

Separate MySQL Special String Functions colliding with InExpression #1285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public void setName(String string) {
nameparts = Arrays.asList(string);
}

public Function withName(String name) {
this.setName(name);
return this;
}

public void setName(List<String> string) {
nameparts = string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static Expression parseExpression(String expression, boolean allowPartial
consumer.accept(parser);
}
try {
Expression expr = parser.SimpleExpression();
Expression expr = parser.Expression();
if (!allowPartialParse && parser.getNextToken().kind != CCJSqlParserTokenManager.EOF) {
throw new JSQLParserException("could only parse partial expression " + expr.toString());
}
Expand Down
41 changes: 33 additions & 8 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_SQL_NO_CACHE: "SQL_NO_CACHE">
| <K_USING:"USING">
| <K_SIGNED:"SIGNED">
| <K_STRING_FUNCTION_NAME: ("SUBSTR" | "SUBSTRING" | "TRIM" | "POSITION" | "OVERLAY")>
| <K_UNSIGNED:"UNSIGNED">
| <K_VALIDATE : "VALIDATE">
| <K_VALUE:"VALUE">
Expand Down Expand Up @@ -1519,6 +1520,7 @@ String RelObjectNameWithoutValue() :
/*| tk=<K_PLACING> | tk=<K_BOTH> | tk=<K_LEADING> | tk=<K_TRAILING> */
| tk=<K_FORMAT> | tk=<K_DIV> | tk=<K_UNSIGNED> | tk=<K_CASE> | tk=<K_LOCAL>
| tk=<K_ARRAY_LITERAL>
| tk=<K_STRING_FUNCTION_NAME>
| tk=<K_USER>

/* Keywords for ALTER SESSION */
Expand All @@ -1536,7 +1538,7 @@ String RelObjectName() :
{
(result = RelObjectNameWithoutValue()
| tk=<K_GROUP> | tk=<K_INTERVAL> | tk=<K_ON> | tk=<K_ORDER> | tk=<K_START> | tk=<K_TOP> | tk=<K_VALUE>
| tk=<K_VALUES> | tk=<K_CREATE> | tk=<K_TABLES> )
| tk=<K_VALUES> | tk=<K_CREATE> | tk=<K_TABLES> )

{
if (tk!=null) result=tk.image;
Expand Down Expand Up @@ -1566,7 +1568,7 @@ String RelObjectNameExt():
{
( result=RelObjectName() | tk=<K_ALL> | tk=<K_ANY> | tk=<K_SOME> | tk=<K_LEFT> | tk=<K_RIGHT> | tk=<K_SET>
| tk=<K_DOUBLE> | tk=<K_IF> | tk=<K_IIF> | tk=<K_OPTIMIZE> | tk=<K_LIMIT>
| tk=<K_OFFSET> | tk=<K_PROCEDURE> | tk=<K_PUBLIC>
| tk=<K_OFFSET> | tk=<K_PROCEDURE> | tk=<K_PUBLIC>
| tk=<K_CASEWHEN> | tk=<K_IN> )
{
if (tk!=null) result=tk.image;
Expand Down Expand Up @@ -4282,6 +4284,7 @@ Function Function() #Function:
{
(
"{" <K_FN> { retval.setEscaped(true); } InternalFunction(retval) "}"
| LOOKAHEAD(3) retval = SpecialStringFunctionWithNamedParameters()
| InternalFunction(retval)
)
{
Expand All @@ -4290,6 +4293,33 @@ Function Function() #Function:
}
}

Function SpecialStringFunctionWithNamedParameters() :
{
Token funcName;
NamedExpressionList namedExpressionList = null;
ExpressionList expressionList = null;
List<OrderByElement> orderByList;
}
{
funcName = <K_STRING_FUNCTION_NAME>

"("
(
LOOKAHEAD(NamedExpressionList1()) namedExpressionList=NamedExpressionList1()
|
LOOKAHEAD(NamedExpressionListExprFirst(), { getAsBoolean(Feature.allowComplexParsing) }) namedExpressionList = NamedExpressionListExprFirst()
|
LOOKAHEAD(3, { getAsBoolean(Feature.allowComplexParsing) }) expressionList=ComplexExpressionList() {expressionList.setUsingBrackets(false);}
|
LOOKAHEAD(3) expressionList=SimpleExpressionList(false)
)
")"

{
return new Function().withName(funcName.image).withNamedParameters(namedExpressionList).withParameters(expressionList);
}
}

Function InternalFunction(Function retval) :
{
List<String> funcName;
Expand All @@ -4312,10 +4342,6 @@ Function InternalFunction(Function retval) :
( LOOKAHEAD(4)
"*" { retval.setAllColumns(true); }
|
LOOKAHEAD(NamedExpressionList1()) namedExpressionList=NamedExpressionList1()
|
LOOKAHEAD(NamedExpressionListExprFirst(), { getAsBoolean(Feature.allowComplexParsing) }) namedExpressionList = NamedExpressionListExprFirst()
|
LOOKAHEAD(3, { getAsBoolean(Feature.allowComplexParsing) }) (expressionList=ComplexExpressionList() {expressionList.setUsingBrackets(false);} [ orderByList = OrderByElements() { retval.setOrderByElements(orderByList); } ])
|
LOOKAHEAD(3) (expressionList=SimpleExpressionList(false) [ orderByList = OrderByElements() { retval.setOrderByElements(orderByList); } ])
Expand All @@ -4324,7 +4350,7 @@ Function InternalFunction(Function retval) :

)]
[ <K_IGNORE> <K_NULLS> {retval.setIgnoreNulls(true); }]
")"
")"

[ "." (
LOOKAHEAD(2) expr1=Function() { retval.setAttribute(expr1); }
Expand All @@ -4336,7 +4362,6 @@ Function InternalFunction(Function retval) :

{
retval.setParameters(expressionList);
retval.setNamedParameters(namedExpressionList);
retval.setName(funcName);
retval.setKeep(keep);
return retval;
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4664,4 +4664,20 @@ public void testDB2SpecialRegisterDateTimeIssue1249() throws JSQLParserException
public void testKeywordFilterIssue1255() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT col1 AS filter FROM table");
}

@Test
public void testCollisionWithSpecialStringFunctionsIssue1284() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"SELECT test( a in (1) AND 2=2) ", true);

assertSqlCanBeParsedAndDeparsed(
"select\n" +
"sum(if(column1 in('value1', 'value2'), 1, 0)) as tcp_logs,\n" +
"sum(if(column1 in ('value1', 'value2') and column2 = 'value3', 1, 0)) as base_tcp_logs\n" +
"from\n" +
"table1\n" +
"where\n" +
"recv_time >= toDateTime('2021-07-20 00:00:00')\n" +
"and recv_time < toDateTime('2021-07-21 00:00:00')", true);
}
}