From a2f18074e78d1077b8905c61af3284096731f2d5 Mon Sep 17 00:00:00 2001 From: tianhao960 Date: Thu, 1 Sep 2022 15:10:31 +0800 Subject: [PATCH] add visitor for json function, add unit test #2 --- .../impl/MySQLStatementSQLVisitor.java | 21 ++++++++++++++++++- .../resources/case/dml/select-expression.xml | 18 ++++++++++++++-- .../sql/supported/dml/select-expression.xml | 1 + 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java index bd0ee26535ecf..6b36118b0eb1f 100644 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java +++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java @@ -71,6 +71,8 @@ import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.IntervalExpressionContext; import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.JoinSpecificationContext; import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.JoinedTableContext; +import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.JsonFunctionContext; +import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.JsonFunctionNameContext; import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.LimitClauseContext; import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.LimitOffsetContext; import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.LimitRowCountContext; @@ -760,7 +762,10 @@ public final ASTNode visitFunctionCall(final FunctionCallContext ctx) { if (null != ctx.regularFunction()) { return visit(ctx.regularFunction()); } - throw new IllegalStateException("FunctionCallContext must have aggregationFunction, regularFunction or specialFunction."); + if (null != ctx.jsonFunction()) { + return visit(ctx.jsonFunction()); + } + throw new IllegalStateException("FunctionCallContext must have aggregationFunction, regularFunction, specialFunction or jsonFunction."); } @Override @@ -771,6 +776,20 @@ public final ASTNode visitAggregationFunction(final AggregationFunctionContext c : new ExpressionProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), getOriginalText(ctx)); } + @Override + public final ASTNode visitJsonFunction(final JsonFunctionContext ctx) { + JsonFunctionNameContext functionNameContext = ctx.jsonFunctionName(); + String functionName; + if (null != functionNameContext) { + functionName = functionNameContext.getText(); + } else if (null != ctx.JSON_SEPARATOR()) { + functionName = ctx.JSON_SEPARATOR().getText(); + } else { + functionName = ctx.JSON_UNQUOTED_SEPARATOR().getText(); + } + return new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), functionName, getOriginalText(ctx)); + } + private ASTNode createAggregationSegment(final AggregationFunctionContext ctx, final String aggregationType) { AggregationType type = AggregationType.valueOf(aggregationType.toUpperCase()); String innerExpression = ctx.start.getInputStream().getText(new Interval(ctx.LP_().getSymbol().getStartIndex(), ctx.stop.getStopIndex())); diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select-expression.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select-expression.xml index 7f746d69cb2e8..fe1bb74733d05 100644 --- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select-expression.xml +++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select-expression.xml @@ -1627,7 +1627,7 @@ - + @@ -1641,7 +1641,21 @@ - + + + + + + diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select-expression.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select-expression.xml index c6b3f2ef68f62..83b8486391f2c 100644 --- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select-expression.xml +++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select-expression.xml @@ -75,6 +75,7 @@ +