Skip to content

Commit

Permalink
[Enhancement] adjust translate function syntax structure to prevent p…
Browse files Browse the repository at this point in the history
…arser performance rollback

Signed-off-by: stephen <[email protected]>
  • Loading branch information
stephen-shelby committed Jan 8, 2025
1 parent 8d07f00 commit 1f639cc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6857,7 +6857,6 @@ private static void addArgumentUseTypeInt(Expr value, List<Expr> exprs) {

@Override
public ParseNode visitSimpleFunctionCall(StarRocksParser.SimpleFunctionCallContext context) {

String fullFunctionName = getQualifiedName(context.qualifiedName()).toString();
NodePosition pos = createPos(context);

Expand Down Expand Up @@ -7057,6 +7056,17 @@ public ParseNode visitSimpleFunctionCall(StarRocksParser.SimpleFunctionCallConte
return SyntaxSugars.parse(functionCallExpr);
}

@Override
public ParseNode visitTranslateFunctionCall(StarRocksParser.TranslateFunctionCallContext context) {
String fullFunctionName = "translate";
NodePosition pos = createPos(context);

FunctionName fnName = FunctionName.createFnName(fullFunctionName);
FunctionCallExpr functionCallExpr = new FunctionCallExpr(fnName,
new FunctionParams(false, visit(context.expression(), Expr.class)), pos);
return SyntaxSugars.parse(functionCallExpr);
}

@Override
public ParseNode visitAggregationFunctionCall(StarRocksParser.AggregationFunctionCallContext context) {
NodePosition pos = createPos(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2478,6 +2478,7 @@ functionCall
| specialFunctionExpression #specialFunction
| aggregationFunction over? #aggregationFunctionCall
| windowFunction over #windowFunctionCall
| TRANSLATE '(' (expression (',' expression)*)? ')' #translateFunctionCall
| qualifiedName '(' (expression (',' expression)*)? ')' over? #simpleFunctionCall
;

Expand Down Expand Up @@ -2974,7 +2975,7 @@ nonReserved
| SAMPLE | SCHEDULE | SCHEDULER | SECOND | SECURITY | SEPARATOR | SERIALIZABLE |SEMI | SESSION | SETS | SIGNED | SNAPSHOT | SNAPSHOTS | SQLBLACKLIST | START
| STREAM | SUM | STATUS | STOP | SKIP_HEADER | SWAP
| STORAGE| STRING | STRUCT | STATS | SUBMIT | SUSPEND | SYNC | SYSTEM_TIME
| TABLES | TABLET | TABLETS | TAG | TASK | TEMPORARY | TIMESTAMP | TIMESTAMPADD | TIMESTAMPDIFF | THAN | TIME | TIMES | TRANSACTION | TRACE
| TABLES | TABLET | TABLETS | TAG | TASK | TEMPORARY | TIMESTAMP | TIMESTAMPADD | TIMESTAMPDIFF | THAN | TIME | TIMES | TRANSACTION | TRACE | TRANSLATE
| TRIM_SPACE
| TRIGGERS | TRUNCATE | TYPE | TYPES
| UNBOUNDED | UNCOMMITTED | UNSET | UNINSTALL | USAGE | USER | USERS | UNLOCK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ TIMESTAMPADD: 'TIMESTAMPADD';
TIMESTAMPDIFF: 'TIMESTAMPDIFF';
TINYINT: 'TINYINT';
TRANSACTION: 'TRANSACTION';
TRANSLATE: {getCharPositionInLine() == 0}? 'TRANSLATE';
TRANSLATE: 'TRANSLATE';
TO: 'TO';
TRACE: 'TRACE';
TRIGGERS: 'TRIGGERS';
Expand Down
12 changes: 12 additions & 0 deletions fe/fe-core/src/test/java/com/starrocks/sql/parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.atn.PredictionMode;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -578,6 +579,17 @@ private static Stream<Arguments> unexpectedTokenSqls() {
return arguments.stream();
}

@Test
public void testTranslateFunction() {
String sql = "select translate('abcabc', 'ab', '12') as test;";
SessionVariable sessionVariable = new SessionVariable();
try {
SqlParser.parse(sql, sessionVariable);
} catch (Exception e) {
Assertions.fail("sql should success. errMsg: " + e.getMessage());
}
}

}


0 comments on commit 1f639cc

Please sign in to comment.