Skip to content

Commit

Permalink
Oracle sql parser support approx rank.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamhucong committed Sep 20, 2023
1 parent 41d9f0c commit 57c1c52
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions test/it/parser/src/main/resources/case/dml/select-aggregate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,33 @@
<aggregation-projection type="BIT_XOR" expression="BIT_XOR(user_id)" start-index="7" stop-index="22" />
</projections>
</select>
<select sql-case-id="select_approx_count">
<projections start-index="7" stop-index="92">
<column-projection name="owner" start-index="7" stop-index="11"/>
<expression-projection text="approx_count(*)" start-index="14" stop-index="28"/>
<expression-projection text="approx_rank(partition by owner order by approx_count(*) desc)" start-index="32" stop-index="92"/>
</projections>
<from>
<simple-table name="t" start-index="99" stop-index="99"/>
</from>
<group-by>
<column-item name="owner" start-index="110" stop-index="114"/>
</group-by>
<having start-index="116" stop-index="188">
<expr>
<binary-operation-expression start-index="123" stop-index="188">
<left>
<function function-name="approx_rank" start-index="123" stop-index="183" text="approx_rank(partition by owner order by approx_count(*) desc)"/>
</left>
<operator>&lt;=</operator>
<right>
<literal-expression start-index="188" stop-index="188" value="1"/>
</right>
</binary-operation-expression>
</expr>
</having>
<order-by>
<index-item index="1" start-index="199" stop-index="199"/>
</order-by>
</select>
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@
<sql-case id="select_count_with_in_clause" value="SELECT COUNT(*) FROM t_order WHERE last_value IN (?, ?)" db-types="MySQL" />
<sql-case id="select_count_with_not_in_clause" value="SELECT COUNT(*) FROM t_order WHERE category IN (?, ?) AND last_value NOT IN (?, ?)" db-types="MySQL" />
<sql-case id="select_bit_xor" value="SELECT BIT_XOR(user_id) FROM t_order" db-types="MySQL" />
<sql-case id="select_approx_count"
value="select owner, approx_count(*) , approx_rank(partition by owner order by approx_count(*) desc) from t group by owner having approx_rank(partition by owner order by approx_count(*) desc) &lt;= 1 order by 1"
db-types="Oracle"/>
</sql-cases>

0 comments on commit 57c1c52

Please sign in to comment.