From e55969073952e82c9192f1876d48bec3dba511c6 Mon Sep 17 00:00:00 2001 From: lushaorong Date: Mon, 26 Aug 2024 10:36:33 +0800 Subject: [PATCH] Improve the parsing of methods in MySQL (#32599) * Improve the parsing of methods in MySQL * Improve the parsing of methods in MySQL --- .../src/main/antlr4/imports/mysql/BaseRule.g4 | 5 +- .../src/main/antlr4/imports/mysql/Keyword.g4 | 4 + .../statement/MySQLStatementVisitor.java | 3 +- .../case/dml/select-special-function.xml | 203 ++++++++++++++++++ .../supported/dml/select-special-function.xml | 11 + 5 files changed, 223 insertions(+), 3 deletions(-) diff --git a/parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4 b/parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4 index cc7682cb19748..555ddf2aa2dee 100644 --- a/parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4 +++ b/parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4 @@ -33,6 +33,7 @@ customKeyword | POSITION | SUBSTRING | SUBSTR + | MID | EXTRACT | TRIM | LAST_DAY @@ -1113,8 +1114,8 @@ positionFunction ; substringFunction - : (SUBSTRING | SUBSTR) LP_ expr FROM NUMBER_ (FOR NUMBER_)? RP_ - | (SUBSTRING | SUBSTR) LP_ expr COMMA_ NUMBER_ (COMMA_ NUMBER_)? RP_ + : (SUBSTRING | SUBSTR | MID) LP_ expr FROM NUMBER_ (FOR NUMBER_)? RP_ + | (SUBSTRING | SUBSTR | MID) LP_ expr COMMA_ NUMBER_ (COMMA_ NUMBER_)? RP_ ; extractFunction diff --git a/parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/Keyword.g4 b/parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/Keyword.g4 index 882e72c3fe0a1..081f2c5e5730e 100644 --- a/parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/Keyword.g4 +++ b/parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/Keyword.g4 @@ -60,6 +60,10 @@ SUBSTR : S U B S T R ; +MID + : M I D + ; + EXTRACT : E X T R A C T ; diff --git a/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java b/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java index 648a744b71e84..5a230c4d7463a 100644 --- a/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java +++ b/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java @@ -1099,7 +1099,8 @@ public final ASTNode visitPositionFunction(final PositionFunctionContext ctx) { @Override public final ASTNode visitSubstringFunction(final SubstringFunctionContext ctx) { FunctionSegment result = new FunctionSegment( - ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), null == ctx.SUBSTR() ? ctx.SUBSTRING().getText() : ctx.SUBSTR().getText(), getOriginalText(ctx)); + ctx.getStart().getStartIndex(), + ctx.getStop().getStopIndex(), null == ctx.SUBSTR() ? null == ctx.SUBSTRING() ? ctx.MID().getText() : ctx.SUBSTRING().getText() : ctx.SUBSTR().getText(), getOriginalText(ctx)); result.getParameters().add((ExpressionSegment) visit(ctx.expr())); for (TerminalNode each : ctx.NUMBER_()) { result.getParameters().add(new LiteralExpressionSegment(each.getSymbol().getStartIndex(), each.getSymbol().getStopIndex(), new NumberLiteralValue(each.getText()).getValue())); diff --git a/test/it/parser/src/main/resources/case/dml/select-special-function.xml b/test/it/parser/src/main/resources/case/dml/select-special-function.xml index 8c8c7235754f4..d61745a251638 100644 --- a/test/it/parser/src/main/resources/case/dml/select-special-function.xml +++ b/test/it/parser/src/main/resources/case/dml/select-special-function.xml @@ -3020,4 +3020,207 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml b/test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml index fe08fe5f763b9..f6c87df785968 100644 --- a/test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml +++ b/test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml @@ -180,4 +180,15 @@ + + + + + + + + + + +