From 57c1c52e1864b0864a002c6c4badfde964cabbd6 Mon Sep 17 00:00:00 2001 From: Cong Hu Date: Wed, 20 Sep 2023 17:27:47 +0800 Subject: [PATCH] Oracle sql parser support approx rank. --- .../main/antlr4/imports/oracle/BaseRule.g4 | 6 +++- .../antlr4/imports/oracle/OracleKeyword.g4 | 4 +++ .../statement/OracleStatementVisitor.java | 12 +++++++- .../resources/case/dml/select-aggregate.xml | 29 +++++++++++++++++++ .../sql/supported/dml/select-aggregate.xml | 3 ++ 5 files changed, 52 insertions(+), 2 deletions(-) diff --git a/parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/BaseRule.g4 b/parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/BaseRule.g4 index 327a850c566ef..385a3b837dec4 100644 --- a/parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/BaseRule.g4 +++ b/parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/BaseRule.g4 @@ -775,7 +775,11 @@ leadLagInfo specialFunction : castFunction | charFunction | extractFunction | formatFunction | firstOrLastValueFunction | trimFunction | featureFunction - | setFunction | translateFunction | cursorFunction | toDateFunction + | setFunction | translateFunction | cursorFunction | toDateFunction | approxRank + ; + +approxRank + : APPROX_RANK LP_ queryPartitionClause? orderByClause RP_ ; toDateFunction diff --git a/parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/OracleKeyword.g4 b/parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/OracleKeyword.g4 index f9d6f4ca486f8..8297e15532967 100644 --- a/parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/OracleKeyword.g4 +++ b/parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/OracleKeyword.g4 @@ -1751,6 +1751,10 @@ APPLY : A P P L Y ; +APPROX_RANK + : A P P R O X UL_ R A N K + ; + MAXIMIZE : M A X I M I Z E ; diff --git a/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/OracleStatementVisitor.java b/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/OracleStatementVisitor.java index c17be79e6cf2d..21715d6181eed 100644 --- a/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/OracleStatementVisitor.java +++ b/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/OracleStatementVisitor.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.sql.parser.autogen.OracleStatementBaseVisitor; import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.AggregationFunctionContext; import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.AnalyticFunctionContext; +import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.ApproxRankContext; import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.BitExprContext; import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.BitValueLiteralsContext; import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.BooleanLiteralsContext; @@ -976,8 +977,17 @@ public final ASTNode visitSpecialFunction(final SpecialFunctionContext ctx) { if (null != ctx.toDateFunction()) { return visit(ctx.toDateFunction()); } + if (null != ctx.approxRank()) { + return visit(ctx.approxRank()); + } throw new IllegalStateException( - "SpecialFunctionContext must have castFunction, charFunction, extractFunction, formatFunction, firstOrLastValueFunction, trimFunction, toDateFunction or featureFunction."); + "SpecialFunctionContext must have castFunction, charFunction, extractFunction, formatFunction, firstOrLastValueFunction, trimFunction, toDateFunction, approxCount" + + " or featureFunction."); + } + + @Override + public ASTNode visitApproxRank(final ApproxRankContext ctx) { + return new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.APPROX_RANK().getText(), getOriginalText(ctx)); } @Override diff --git a/test/it/parser/src/main/resources/case/dml/select-aggregate.xml b/test/it/parser/src/main/resources/case/dml/select-aggregate.xml index 2a861341fd3e9..bd6a047454284 100644 --- a/test/it/parser/src/main/resources/case/dml/select-aggregate.xml +++ b/test/it/parser/src/main/resources/case/dml/select-aggregate.xml @@ -461,4 +461,33 @@ + diff --git a/test/it/parser/src/main/resources/sql/supported/dml/select-aggregate.xml b/test/it/parser/src/main/resources/sql/supported/dml/select-aggregate.xml index 5abe97537d5a0..829b875778654 100644 --- a/test/it/parser/src/main/resources/sql/supported/dml/select-aggregate.xml +++ b/test/it/parser/src/main/resources/sql/supported/dml/select-aggregate.xml @@ -33,4 +33,7 @@ +