Skip to content

Commit b00de36

Browse files
committed
SQL: allow LEFT and RIGHT as function names (#32066)
Due to the way ANTLR works, any declared tokens need to be accounted for manually inside function names (otherwise a different rule gets applied). Fix #32046 (cherry picked from commit 20ea72e)
1 parent fed84c3 commit b00de36

File tree

9 files changed

+922
-765
lines changed

9 files changed

+922
-765
lines changed

x-pack/plugin/sql/src/main/antlr/SqlBase.g4

+6-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,12 @@ functionExpression
243243
;
244244
245245
functionTemplate
246-
: identifier '(' (setQuantifier? expression (',' expression)*)? ')'
246+
: functionName '(' (setQuantifier? expression (',' expression)*)? ')'
247+
;
248+
functionName
249+
: LEFT
250+
| RIGHT
251+
| identifier
247252
;
248253
249254
constant

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/ExpressionBuilder.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,7 @@ public Function visitExtractExpression(ExtractExpressionContext ctx) {
396396
@Override
397397
public Function visitFunctionExpression(FunctionExpressionContext ctx) {
398398
FunctionTemplateContext template = ctx.functionTemplate();
399-
400-
String name = visitIdentifier(template.identifier());
399+
String name = template.functionName().getText();
401400
boolean isDistinct = template.setQuantifier() != null && template.setQuantifier().DISTINCT() != null;
402401
UnresolvedFunction.ResolutionType resolutionType =
403402
isDistinct ? UnresolvedFunction.ResolutionType.DISTINCT : UnresolvedFunction.ResolutionType.STANDARD;

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseBaseListener.java

+12
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,18 @@ class SqlBaseBaseListener implements SqlBaseListener {
803803
* <p>The default implementation does nothing.</p>
804804
*/
805805
@Override public void exitFunctionTemplate(SqlBaseParser.FunctionTemplateContext ctx) { }
806+
/**
807+
* {@inheritDoc}
808+
*
809+
* <p>The default implementation does nothing.</p>
810+
*/
811+
@Override public void enterFunctionName(SqlBaseParser.FunctionNameContext ctx) { }
812+
/**
813+
* {@inheritDoc}
814+
*
815+
* <p>The default implementation does nothing.</p>
816+
*/
817+
@Override public void exitFunctionName(SqlBaseParser.FunctionNameContext ctx) { }
806818
/**
807819
* {@inheritDoc}
808820
*

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseBaseVisitor.java

+7
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,13 @@ class SqlBaseBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements SqlBa
473473
* {@link #visitChildren} on {@code ctx}.</p>
474474
*/
475475
@Override public T visitFunctionTemplate(SqlBaseParser.FunctionTemplateContext ctx) { return visitChildren(ctx); }
476+
/**
477+
* {@inheritDoc}
478+
*
479+
* <p>The default implementation returns the result of calling
480+
* {@link #visitChildren} on {@code ctx}.</p>
481+
*/
482+
@Override public T visitFunctionName(SqlBaseParser.FunctionNameContext ctx) { return visitChildren(ctx); }
476483
/**
477484
* {@inheritDoc}
478485
*

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseListener.java

+10
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,16 @@ interface SqlBaseListener extends ParseTreeListener {
745745
* @param ctx the parse tree
746746
*/
747747
void exitFunctionTemplate(SqlBaseParser.FunctionTemplateContext ctx);
748+
/**
749+
* Enter a parse tree produced by {@link SqlBaseParser#functionName}.
750+
* @param ctx the parse tree
751+
*/
752+
void enterFunctionName(SqlBaseParser.FunctionNameContext ctx);
753+
/**
754+
* Exit a parse tree produced by {@link SqlBaseParser#functionName}.
755+
* @param ctx the parse tree
756+
*/
757+
void exitFunctionName(SqlBaseParser.FunctionNameContext ctx);
748758
/**
749759
* Enter a parse tree produced by the {@code nullLiteral}
750760
* labeled alternative in {@link SqlBaseParser#constant}.

0 commit comments

Comments
 (0)