Skip to content

Commit

Permalink
add visitor for json function, add unit test apache#2
Browse files Browse the repository at this point in the history
  • Loading branch information
tianhao960 committed Sep 1, 2022
1 parent f85d0a8 commit a2f1807
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@
</projections>
<where start-index="22" stop-index="44">
<expr>
<common-expression text="order_id -&gt;&quot;$[1]&quot;" start-index="28" stop-index="44"/>
<function function-name="-&gt;" text="order_id -&gt;&quot;$[1]&quot;" start-index="28" stop-index="44"></function>
</expr>
</where>
</select>
Expand All @@ -1641,7 +1641,21 @@
</projections>
<where start-index="22" stop-index="46">
<expr>
<common-expression text="order_id -&gt;&gt; &quot;$[1]&quot;" start-index="28" stop-index="46"/>
<function function-name="-&gt;&gt;" text="order_id -&gt;&gt; &quot;$[1]&quot;" start-index="28" stop-index="46"></function>
</expr>
</where>
</select>

<select sql-case-id="select_where_with_simple_expr_with_json_contains">
<from start-index="14" stop-index="20">
<simple-table name="t_order" start-index="14" stop-index="20"/>
</from>
<projections distinct-row="false" start-index="7" stop-index="7">
<shorthand-projection start-index="7" stop-index="7"/>
</projections>
<where start-index="22" stop-index="76">
<expr>
<function function-name="JSON_CONTAINS" text="JSON_CONTAINS(order_msg -> '$[*].code', 'x', '$')" start-index="28" stop-index="76"></function>
</expr>
</where>
</select>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<sql-case id="select_where_with_simple_expr_with_odbc_escape_syntax" value="SELECT * FROM t_order WHERE {ts ?}" db-types="MySQL" />
<sql-case id="select_where_with_simple_expr_with_json_extract_sign" value="SELECT * FROM t_order WHERE order_id -&gt;&quot;$[1]&quot;" db-types="MySQL" />
<sql-case id="select_where_with_simple_expr_with_json_unquote_extract_sign" value="SELECT * FROM t_order WHERE order_id -&gt;&gt; &quot;$[1]&quot;" db-types="MySQL" />
<sql-case id="select_where_with_simple_expr_with_json_contains" value="SELECT * FROM t_order WHERE JSON_CONTAINS(order_msg -> '$[*].code', 'x', '$') " db-types="MySQL" />
<sql-case id="select_where_with_simple_expr_with_match" value="SELECT * FROM t_order WHERE MATCH (order_id) AGAINST (? IN NATURAL LANGUAGE MODE)" db-types="MySQL" />
<sql-case id="select_where_with_simple_expr_with_case" value="SELECT * FROM t_order WHERE CASE WHEN order_id &gt; ? THEN ? ELSE ? END" db-types="MySQL" />
<sql-case id="select_where_with_expr_with_not_with_order_by" value="SELECT last_name, job_id, salary, department_id FROM employees WHERE NOT (job_id = 'PU_CLERK' AND department_id = 30) ORDER BY last_name" db-types="Oracle" />
Expand Down

0 comments on commit a2f1807

Please sign in to comment.