This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 6752fde8b21 Fix mysql TimeStampDiff function parse (#29636)
6752fde8b21 is described below
commit 6752fde8b21e71b887e8a05338c51d38f96dd371
Author: ZhangCheng <[email protected]>
AuthorDate: Wed Jan 3 10:00:13 2024 +0800
Fix mysql TimeStampDiff function parse (#29636)
---
.../src/main/antlr4/imports/mysql/BaseRule.g4 | 5 +++
.../src/main/antlr4/imports/mysql/MySQLKeyword.g4 | 3 ++
.../visitor/statement/MySQLStatementVisitor.java | 37 ++++++++++++----------
3 files changed, 28 insertions(+), 17 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 27dd6acc9c6..de0adee78bf 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
@@ -1017,6 +1017,7 @@ specialFunction
| weightStringFunction
| windowFunction
| groupingFunction
+ | timeStampDiffFunction
;
currentUserFunction
@@ -1027,6 +1028,10 @@ groupingFunction
: GROUPING LP_ expr (COMMA_ expr)* RP_
;
+timeStampDiffFunction
+ : TIMESTAMPDIFF LP_ intervalUnit COMMA_ expr COMMA_ expr RP_
+ ;
+
groupConcatFunction
: GROUP_CONCAT LP_ distinct? (expr (COMMA_ expr)* | ASTERISK_)?
(orderByClause)? (SEPARATOR expr)? RP_
;
diff --git
a/parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/MySQLKeyword.g4
b/parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/MySQLKeyword.g4
index 323eb018bac..18118be32e8 100644
--- a/parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/MySQLKeyword.g4
+++ b/parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/MySQLKeyword.g4
@@ -3146,3 +3146,6 @@ ZONE
: Z O N E
;
+TIMESTAMPDIFF
+ : T I M E S T A M P D I F F
+ ;
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 066195f3a66..216179598bd 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
@@ -133,6 +133,7 @@ import
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableRe
import
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableStatementContext;
import
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableValueConstructorContext;
import
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TemporalLiteralsContext;
+import
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TimeStampDiffFunctionContext;
import
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TrimFunctionContext;
import
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TypeDatetimePrecisionContext;
import
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.UdfFunctionContext;
@@ -147,7 +148,6 @@ import
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WhereCl
import
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WindowClauseContext;
import
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WindowFunctionContext;
import
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WindowItemContext;
-import
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WindowSpecificationContext;
import
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WithClauseContext;
import org.apache.shardingsphere.sql.parser.sql.common.enums.AggregationType;
import org.apache.shardingsphere.sql.parser.sql.common.enums.CombineType;
@@ -239,6 +239,7 @@ import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQ
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLUpdateStatement;
import java.math.BigDecimal;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
@@ -860,7 +861,7 @@ public abstract class MySQLStatementVisitor extends
MySQLStatementBaseVisitor<AS
public ASTNode visitWindowClause(final WindowClauseContext ctx) {
if (null != ctx.windowItem()) {
return new WindowSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), getWindowItem(ctx.windowItem(0)),
-
getWindowSpecification(ctx.windowItem(0).windowSpecification()));
+
getExpressions(ctx.windowItem(0).windowSpecification().expr()));
}
return new WindowSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex());
}
@@ -869,14 +870,6 @@ public abstract class MySQLStatementVisitor extends
MySQLStatementBaseVisitor<AS
return new IdentifierValue(ctx.identifier().getText());
}
- private Collection<ExpressionSegment> getWindowSpecification(final
WindowSpecificationContext ctx) {
- Collection<ExpressionSegment> result = new LinkedList<>();
- for (ExprContext each : ctx.expr()) {
- result.add((ExpressionSegment) visit(each));
- }
- return result;
- }
-
@Override
public ASTNode visitHavingClause(final HavingClauseContext ctx) {
ExpressionSegment expr = (ExpressionSegment) visit(ctx.expr());
@@ -957,20 +950,20 @@ public abstract class MySQLStatementVisitor extends
MySQLStatementBaseVisitor<AS
if (null != ctx.distinct()) {
AggregationDistinctProjectionSegment result =
new
AggregationDistinctProjectionSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), type, getOriginalText(ctx),
getDistinctExpression(ctx));
- result.getParameters().addAll(getExpressions(ctx));
+ result.getParameters().addAll(getExpressions(ctx.expr()));
return result;
}
AggregationProjectionSegment result = new
AggregationProjectionSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), type, getOriginalText(ctx));
- result.getParameters().addAll(getExpressions(ctx));
+ result.getParameters().addAll(getExpressions(ctx.expr()));
return result;
}
- private Collection<ExpressionSegment> getExpressions(final
AggregationFunctionContext ctx) {
- if (null == ctx.expr()) {
+ private Collection<ExpressionSegment> getExpressions(final
List<ExprContext> exprList) {
+ if (null == exprList) {
return Collections.emptyList();
}
- Collection<ExpressionSegment> result = new LinkedList<>();
- for (ExprContext each : ctx.expr()) {
+ Collection<ExpressionSegment> result = new
ArrayList<>(exprList.size());
+ for (ExprContext each : exprList) {
result.add((ExpressionSegment) visit(each));
}
return result;
@@ -1022,6 +1015,9 @@ public abstract class MySQLStatementVisitor extends
MySQLStatementBaseVisitor<AS
if (null != ctx.currentUserFunction()) {
return visit(ctx.currentUserFunction());
}
+ if (null != ctx.timeStampDiffFunction()) {
+ return visit(ctx.timeStampDiffFunction());
+ }
return new FunctionSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), getOriginalText(ctx), getOriginalText(ctx));
}
@@ -1040,7 +1036,7 @@ public abstract class MySQLStatementVisitor extends
MySQLStatementBaseVisitor<AS
super.visitWindowFunction(ctx);
FunctionSegment result = new
FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(),
ctx.windowingClause().OVER().getText(), getOriginalText(ctx));
result.getParameters().add(new
FunctionSegment(ctx.funcName.getStartIndex(), ctx.funcName.getStopIndex(),
ctx.funcName.getText(), ctx.funcName.getText() + "()"));
-
result.getParameters().addAll(getWindowSpecification(ctx.windowingClause().windowSpecification()));
+
result.getParameters().addAll(getExpressions(ctx.windowingClause().windowSpecification().expr()));
return result;
}
@@ -1179,6 +1175,13 @@ public abstract class MySQLStatementVisitor extends
MySQLStatementBaseVisitor<AS
return new FunctionSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), ctx.CURRENT_USER().getText(),
getOriginalText(ctx));
}
+ @Override
+ public ASTNode visitTimeStampDiffFunction(final
TimeStampDiffFunctionContext ctx) {
+ FunctionSegment result = new
FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(),
ctx.TIMESTAMPDIFF().getText(), getOriginalText(ctx));
+ result.getParameters().addAll(getExpressions(ctx.expr()));
+ return result;
+ }
+
@Override
public final ASTNode visitRegularFunction(final RegularFunctionContext
ctx) {
return null == ctx.completeRegularFunction() ?
visit(ctx.shorthandRegularFunction()) : visit(ctx.completeRegularFunction());