From 7b90120a61b553ae139831c913c31d053a15ab9c Mon Sep 17 00:00:00 2001 From: yydeng626 Date: Tue, 30 Jan 2024 02:22:42 -0600 Subject: [PATCH] Support parsing SQL Server SELECT DeptId, sql #29164 (#29906) --- .../main/antlr4/imports/sqlserver/BaseRule.g4 | 6 +- .../main/antlr4/imports/sqlserver/Keyword.g4 | 12 ++++ .../statement/SQLServerStatementVisitor.java | 15 +++++ .../src/main/resources/case/dml/insert.xml | 60 +++++++++++++++++++ .../case/dml/select-special-function.xml | 36 +++++++++++ .../resources/sql/supported/dml/insert.xml | 4 ++ .../supported/dml/select-special-function.xml | 1 + 7 files changed, 133 insertions(+), 1 deletion(-) diff --git a/parser/sql/dialect/sqlserver/src/main/antlr4/imports/sqlserver/BaseRule.g4 b/parser/sql/dialect/sqlserver/src/main/antlr4/imports/sqlserver/BaseRule.g4 index c1e4f38083b1e..78046ff879f90 100644 --- a/parser/sql/dialect/sqlserver/src/main/antlr4/imports/sqlserver/BaseRule.g4 +++ b/parser/sql/dialect/sqlserver/src/main/antlr4/imports/sqlserver/BaseRule.g4 @@ -312,7 +312,11 @@ distinct ; specialFunction - : conversionFunction | charFunction | openJsonFunction | jsonFunction | openRowSetFunction | windowFunction + : conversionFunction | charFunction | openJsonFunction | jsonFunction | openRowSetFunction | windowFunction | approxFunction + ; + +approxFunction + : funcName = (APPROX_PERCENTILE_CONT | APPROX_PERCENTILE_DISC) LP_ expr RP_ WITHIN GROUP LP_ ORDER BY expr (ASC | DESC)? RP_ ; conversionFunction diff --git a/parser/sql/dialect/sqlserver/src/main/antlr4/imports/sqlserver/Keyword.g4 b/parser/sql/dialect/sqlserver/src/main/antlr4/imports/sqlserver/Keyword.g4 index a6ac558e18636..bcc4057ded169 100644 --- a/parser/sql/dialect/sqlserver/src/main/antlr4/imports/sqlserver/Keyword.g4 +++ b/parser/sql/dialect/sqlserver/src/main/antlr4/imports/sqlserver/Keyword.g4 @@ -787,3 +787,15 @@ FIRST_VALUE LAST_VALUE :L A S T UL_ V A L U E ; + +APPROX_PERCENTILE_CONT + : A P P R O X UL_ P E R C E N T I L E UL_ C O N T + ; + +APPROX_PERCENTILE_DISC + : A P P R O X UL_ P E R C E N T I L E UL_ D I S C + ; + +WITHIN + : W I T H I N + ; diff --git a/parser/sql/dialect/sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/SQLServerStatementVisitor.java b/parser/sql/dialect/sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/SQLServerStatementVisitor.java index 780f7d5b29b12..0b5192316624e 100644 --- a/parser/sql/dialect/sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/SQLServerStatementVisitor.java +++ b/parser/sql/dialect/sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/SQLServerStatementVisitor.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AggregationClauseContext; import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AggregationFunctionContext; import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AliasContext; +import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.ApproxFunctionContext; import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AssignmentContext; import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AssignmentValueContext; import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AssignmentValuesContext; @@ -678,9 +679,23 @@ public final ASTNode visitSpecialFunction(final SpecialFunctionContext ctx) { if (null != ctx.windowFunction()) { return visit(ctx.windowFunction()); } + if (null != ctx.approxFunction()) { + return visit(ctx.approxFunction()); + } return new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.getChild(0).getChild(0).getText(), getOriginalText(ctx)); } + @Override + public final ASTNode visitApproxFunction(final ApproxFunctionContext ctx) { + FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.funcName.getText(), getOriginalText(ctx)); + if (null != ctx.expr()) { + for (ExprContext each : ctx.expr()) { + result.getParameters().add((ExpressionSegment) visit(each)); + } + } + return result; + } + @Override public final ASTNode visitConversionFunction(final ConversionFunctionContext ctx) { if (null != ctx.castFunction()) { diff --git a/test/it/parser/src/main/resources/case/dml/insert.xml b/test/it/parser/src/main/resources/case/dml/insert.xml index 4b63244210615..eaf6f4113b987 100644 --- a/test/it/parser/src/main/resources/case/dml/insert.xml +++ b/test/it/parser/src/main/resources/case/dml/insert.xml @@ -3457,4 +3457,64 @@ + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + 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 bea78475efeb2..6ee40537f9914 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 @@ -1095,4 +1095,40 @@ + + diff --git a/test/it/parser/src/main/resources/sql/supported/dml/insert.xml b/test/it/parser/src/main/resources/sql/supported/dml/insert.xml index cf6f6407abebc..5b9c5cf544dc9 100644 --- a/test/it/parser/src/main/resources/sql/supported/dml/insert.xml +++ b/test/it/parser/src/main/resources/sql/supported/dml/insert.xml @@ -127,4 +127,8 @@ + + + + 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 a77c62a0f1a69..3892c0cd1428a 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 @@ -56,4 +56,5 @@ +