This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new d8f5320f9c2 Fix encryption rewrites for MySQL or operators (#28593)
d8f5320f9c2 is described below
commit d8f5320f9c2c7139a02229c608a83a1c1edec83a
Author: niu niu <[email protected]>
AuthorDate: Fri Sep 29 20:27:09 2023 +0800
Fix encryption rewrites for MySQL or operators (#28593)
* Fix encryption rewrites for MySQL or operators
* Change parse condition
---
.../src/main/antlr4/imports/mysql/BaseRule.g4 | 2 +-
.../visitor/statement/MySQLStatementVisitor.java | 4 +--
.../main/resources/case/dml/select-expression.xml | 32 +++++++++++-----------
.../query-with-cipher/dml/select/select-where.xml | 5 ++++
4 files changed, 24 insertions(+), 19 deletions(-)
diff --git a/parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4
b/parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4
index 113ca78dc60..4d3a5739f98 100644
--- a/parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4
+++ b/parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4
@@ -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
diff --git
a/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
b/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
index ae5ce5b7a28..8a43ea4e8ad 100644
---
a/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
+++
b/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
@@ -639,11 +639,11 @@ public abstract class MySQLStatementVisitor extends
MySQLStatementBaseVisitor<AS
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);
}
diff --git a/test/it/parser/src/main/resources/case/dml/select-expression.xml
b/test/it/parser/src/main/resources/case/dml/select-expression.xml
index 9a9166a2123..b44c4ddc474 100644
--- a/test/it/parser/src/main/resources/case/dml/select-expression.xml
+++ b/test/it/parser/src/main/resources/case/dml/select-expression.xml
@@ -522,7 +522,7 @@
<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" />
@@ -530,25 +530,25 @@
</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>
diff --git
a/test/it/rewriter/src/test/resources/scenario/encrypt/case/query-with-cipher/dml/select/select-where.xml
b/test/it/rewriter/src/test/resources/scenario/encrypt/case/query-with-cipher/dml/select/select-where.xml
index 9b044f659d9..1fc4b224689 100644
---
a/test/it/rewriter/src/test/resources/scenario/encrypt/case/query-with-cipher/dml/select/select-where.xml
+++
b/test/it/rewriter/src/test/resources/scenario/encrypt/case/query-with-cipher/dml/select/select-where.xml
@@ -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')" />