Skip to content

Commit

Permalink
Fix encryption rewrites for MySQL or operators (#28593)
Browse files Browse the repository at this point in the history
* Fix encryption rewrites for MySQL or operators

* Change parse condition
  • Loading branch information
zihaoAK47 authored Sep 29, 2023
1 parent 972abd3 commit d8f5320
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ simpleExpr
| columnRef
| simpleExpr collateClause
| variable
| simpleExpr OR_ simpleExpr
| simpleExpr VERTICAL_BAR_ VERTICAL_BAR_ simpleExpr
| (PLUS_ | MINUS_ | TILDE_ | notOperator | BINARY) simpleExpr
| ROW? LP_ expr (COMMA_ expr)* RP_
| EXISTS? subquery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,11 +639,11 @@ public final ASTNode visitSimpleExpr(final SimpleExprContext ctx) {
if (null != ctx.LP_() && 1 == ctx.expr().size()) {
return visit(ctx.expr(0));
}
if (null != ctx.OR_()) {
if (null != ctx.VERTICAL_BAR_() && 2 == ctx.VERTICAL_BAR_().size()) {
ExpressionSegment left = (ExpressionSegment) visit(ctx.simpleExpr(0));
ExpressionSegment right = (ExpressionSegment) visit(ctx.simpleExpr(1));
String text = ctx.start.getInputStream().getText(new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
return new BinaryOperationExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), left, right, ctx.OR_().getText(), text);
return new BinaryOperationExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), left, right, ctx.VERTICAL_BAR_(0).getText().concat(ctx.VERTICAL_BAR_(1).getText()), text);
}
return visitRemainSimpleExpr(ctx);
}
Expand Down
32 changes: 16 additions & 16 deletions test/it/parser/src/main/resources/case/dml/select-expression.xml
Original file line number Diff line number Diff line change
Expand Up @@ -522,33 +522,33 @@
<expr>
<binary-operation-expression start-index="28" stop-index="71">
<left>
<binary-operation-expression start-index="28" stop-index="52">
<binary-operation-expression start-index="28" stop-index="47">
<left>
<column name="order_id" start-index="28" stop-index="43">
<owner name="t_order" start-index="28" stop-index="34" />
</column>
</left>
<operator>=</operator>
<right>
<binary-operation-expression start-index="47" stop-index="52">
<left>
<parameter-marker-expression parameter-index="0" start-index="47" stop-index="47" />
<literal-expression value="1" start-index="47" stop-index="47" />
</left>
<operator>||</operator>
<right>
<parameter-marker-expression parameter-index="1" start-index="52" stop-index="52" />
<literal-expression value="2" start-index="52" stop-index="52" />
</right>
</binary-operation-expression>
<parameter-marker-expression parameter-index="0" start-index="47" stop-index="47" />
<literal-expression value="1" start-index="47" stop-index="47" />
</right>
</binary-operation-expression>
</left>
<operator>=</operator>
<operator>||</operator>
<right>
<column name="order_id" start-index="56" stop-index="71">
<owner name="t_order" start-index="56" stop-index="62" />
</column>
<binary-operation-expression start-index="52" stop-index="71">
<left>
<parameter-marker-expression parameter-index="1" start-index="52" stop-index="52" />
<literal-expression value="2" start-index="52" stop-index="52" />
</left>
<operator>=</operator>
<right>
<column name="order_id" start-index="56" stop-index="71">
<owner name="t_order" start-index="56" stop-index="62" />
</column>
</right>
</binary-operation-expression>
</right>
</binary-operation-expression>
</expr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
<output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.assisted_query_password in (?, ?) AND a.cipher_amount in (?, ?)" parameters="assisted_query_aaa, assisted_query_aaa, encrypt_1000, encrypt_1000" />
</rewrite-assertion>

<rewrite-assertion id="select_for_parameters_or_operator" db-types="MySQL">
<input sql="SELECT account_id, password FROM t_account WHERE account_id = ? AND password = ? || password = ?" parameters="1, abc, def" />
<output sql="SELECT account_id, cipher_password AS password FROM t_account WHERE account_id = ? AND assisted_query_password = ? || assisted_query_password = ?" parameters="1, assisted_query_abc, assisted_query_def" />
</rewrite-assertion>

<rewrite-assertion id="select_where_with_predicate_with_in_expr_for_literals" db-types="PostgreSQL,openGauss">
<input sql="SELECT a.account_id, a.password, a.amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.amount in ('1000', '2000')" />
<output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.cipher_amount in ('encrypt_1000', 'encrypt_2000')" />
Expand Down

0 comments on commit d8f5320

Please sign in to comment.