This is an automated email from the ASF dual-hosted git repository.
chengzhang 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 063500ca9d0 Remove useless isNeedRewrite call in
EncryptProjectionTokenGenerator (#33927)
063500ca9d0 is described below
commit 063500ca9d0f6f466bb06441a053bbeb7d7adb8a
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Fri Dec 6 10:09:13 2024 +0800
Remove useless isNeedRewrite call in EncryptProjectionTokenGenerator
(#33927)
---
.../EncryptProjectionTokenGenerator.java | 55 +---------------------
.../dml/select/select-subquery.xml | 14 +++---
.../dml/select/select-subquery.xml | 4 +-
3 files changed, 10 insertions(+), 63 deletions(-)
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptProjectionTokenGenerator.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptProjectionTokenGenerator.java
index a05860fa1df..31e2b9826da 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptProjectionTokenGenerator.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptProjectionTokenGenerator.java
@@ -35,17 +35,11 @@ import
org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperation
import org.apache.shardingsphere.infra.rewrite.sql.token.common.pojo.SQLToken;
import
org.apache.shardingsphere.infra.rewrite.sql.token.common.pojo.generic.SubstitutableColumnNameToken;
import org.apache.shardingsphere.sql.parser.statement.core.enums.SubqueryType;
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ColumnProjectionSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ShorthandProjectionSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.OwnerSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.ParenthesesSegment;
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.WithSegment;
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.JoinTableSegment;
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SubqueryTableSegment;
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableSegment;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
import java.util.Collection;
@@ -53,8 +47,6 @@ import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
-import java.util.Set;
-import java.util.stream.Collectors;
/**
* Projection token generator for encrypt.
@@ -105,7 +97,7 @@ public final class EncryptProjectionTokenGenerator {
String columnName = columnProjection.getOriginalColumn().getValue();
boolean newAddedColumn =
existColumnNames.add(columnProjection.getOriginalTable().getValue() + "." +
columnName);
Optional<EncryptTable> encryptTable =
rule.findEncryptTable(columnProjection.getOriginalTable().getValue());
- if (encryptTable.isPresent() &&
encryptTable.get().isEncryptColumn(columnName) &&
isNeedRewrite(selectStatementContext, columnSegment)) {
+ if (encryptTable.isPresent() &&
encryptTable.get().isEncryptColumn(columnName)) {
EncryptColumn encryptColumn =
encryptTable.get().getEncryptColumn(columnName);
Collection<Projection> projections =
generateProjections(encryptColumn, columnProjection,
selectStatementContext.getSubqueryType(), newAddedColumn);
int startIndex = getStartIndex(columnSegment);
@@ -145,51 +137,6 @@ public final class EncryptProjectionTokenGenerator {
return !selectStatementContext.containsTableSubquery();
}
- private boolean isNeedRewrite(final SelectStatementContext
selectStatementContext, final ColumnProjectionSegment columnSegment) {
- SelectStatement sqlStatement =
selectStatementContext.getSqlStatement();
- if (sqlStatement.getWithSegment().isPresent() &&
!(sqlStatement.getFrom().isPresent() && sqlStatement.getFrom().get() instanceof
SubqueryTableSegment)
- && columnSegment.getColumn().getOwner().isPresent()) {
- WithSegment withSegment = sqlStatement.getWithSegment().get();
- if (columnSegment.getStopIndex() < withSegment.getStartIndex() ||
columnSegment.getStartIndex() > withSegment.getStopIndex()) {
- Set<String> withTableAlias =
withSegment.getCommonTableExpressions().stream().map(each ->
each.getAliasSegment().getIdentifier().getValue()).collect(Collectors.toSet());
- return
!withTableAlias.contains(columnSegment.getColumn().getOwner().get().getIdentifier().getValue());
- }
- }
- if (sqlStatement.getFrom().isPresent() &&
isContainsInJoinSubquery(sqlStatement.getFrom().get(), columnSegment)) {
- return false;
- }
- return !selectStatementContext.containsTableSubquery();
- }
-
- private boolean isContainsInJoinSubquery(final TableSegment tableSegment,
final ColumnProjectionSegment columnSegment) {
- if (tableSegment instanceof JoinTableSegment &&
isContainsInJoinSubquery(((JoinTableSegment) tableSegment).getLeft(),
columnSegment)) {
- return true;
- }
- if (tableSegment instanceof JoinTableSegment &&
isContainsInJoinSubquery(((JoinTableSegment) tableSegment).getRight(),
columnSegment)) {
- return true;
- }
- if (tableSegment instanceof SubqueryTableSegment) {
- SubqueryTableSegment subqueryTable = (SubqueryTableSegment)
tableSegment;
- ColumnSegment column = columnSegment.getColumn();
- if (subqueryTable.getAliasName().isPresent() &&
column.getOwner().isPresent()) {
- return
subqueryTable.getAliasName().get().equalsIgnoreCase(column.getOwner().get().getIdentifier().getValue());
- } else {
- return isContainsInSubqueryProjections(columnSegment,
subqueryTable);
- }
- }
- return false;
- }
-
- private boolean isContainsInSubqueryProjections(final
ColumnProjectionSegment columnSegment, final SubqueryTableSegment
subqueryTable) {
- for (ProjectionSegment each :
subqueryTable.getSubquery().getSelect().getProjections().getProjections()) {
- if (each instanceof ColumnProjectionSegment
- && ((ColumnProjectionSegment)
each).getColumn().getIdentifier().getValue().equalsIgnoreCase(columnSegment.getColumn().getIdentifier().getValue()))
{
- return true;
- }
- }
- return false;
- }
-
private int getStartIndex(final ColumnProjectionSegment columnSegment) {
if (columnSegment.getColumn().getLeftParentheses().isPresent()) {
return
columnSegment.getColumn().getLeftParentheses().get().getStartIndex();
diff --git
a/test/it/rewriter/src/test/resources/scenario/encrypt/case/query-with-cipher/dml/select/select-subquery.xml
b/test/it/rewriter/src/test/resources/scenario/encrypt/case/query-with-cipher/dml/select/select-subquery.xml
index 5fd447c1a66..e9dfaf9d443 100644
---
a/test/it/rewriter/src/test/resources/scenario/encrypt/case/query-with-cipher/dml/select/select-subquery.xml
+++
b/test/it/rewriter/src/test/resources/scenario/encrypt/case/query-with-cipher/dml/select/select-subquery.xml
@@ -24,32 +24,32 @@
<rewrite-assertion id="select_not_nested_subquery_in_table_segment"
db-types="MySQL">
<input sql="SELECT u.amount, u.password, o.certificate_number FROM
(SELECT certificate_number FROM t_account) o, t_account u WHERE
o.certificate_number=u.certificate_number AND u.password=?" parameters="1" />
- <output sql="SELECT u.cipher_amount AS amount, u.cipher_password AS
password, o.certificate_number FROM (SELECT cipher_certificate_number AS
certificate_number, cipher_certificate_number,
assisted_query_certificate_number, like_query_certificate_number FROM
t_account) o, t_account u WHERE
o.assisted_query_certificate_number=u.assisted_query_certificate_number AND
u.assisted_query_password=?" parameters="assisted_query_1" />
+ <output sql="SELECT u.cipher_amount AS amount, u.cipher_password AS
password, o.cipher_certificate_number AS certificate_number FROM (SELECT
cipher_certificate_number AS certificate_number, cipher_certificate_number,
assisted_query_certificate_number, like_query_certificate_number FROM
t_account) o, t_account u WHERE
o.assisted_query_certificate_number=u.assisted_query_certificate_number AND
u.assisted_query_password=?" parameters="assisted_query_1" />
</rewrite-assertion>
<rewrite-assertion id="select_not_nested_subquery_in_table_segment_refed"
db-types="MySQL">
<input sql="SELECT u.amount, u.password, o.certificate_number FROM
(SELECT certificate_number FROM t_account_bak) o, t_account u WHERE
o.certificate_number=u.certificate_number AND u.password=?" parameters="1" />
- <output sql="SELECT u.cipher_amount AS amount, u.cipher_password AS
password, o.certificate_number FROM (SELECT cipher_certificate_number AS
certificate_number, cipher_certificate_number,
assisted_query_certificate_number, like_query_certificate_number FROM
t_account_bak) o, t_account u WHERE
o.assisted_query_certificate_number=u.assisted_query_certificate_number AND
u.assisted_query_password=?" parameters="assisted_query_1" />
+ <output sql="SELECT u.cipher_amount AS amount, u.cipher_password AS
password, o.cipher_certificate_number AS certificate_number FROM (SELECT
cipher_certificate_number AS certificate_number, cipher_certificate_number,
assisted_query_certificate_number, like_query_certificate_number FROM
t_account_bak) o, t_account u WHERE
o.assisted_query_certificate_number=u.assisted_query_certificate_number AND
u.assisted_query_password=?" parameters="assisted_query_1" />
</rewrite-assertion>
<rewrite-assertion id="select_not_nested_subquery_in_table_segment_alias"
db-types="MySQL">
<input sql="SELECT o.certificate_number FROM (SELECT
a.certificate_number FROM t_account a) o" />
- <output sql="SELECT o.certificate_number FROM (SELECT
a.cipher_certificate_number AS certificate_number, a.cipher_certificate_number,
a.assisted_query_certificate_number, a.like_query_certificate_number FROM
t_account a) o" />
+ <output sql="SELECT o.cipher_certificate_number AS certificate_number
FROM (SELECT a.cipher_certificate_number AS certificate_number,
a.cipher_certificate_number, a.assisted_query_certificate_number,
a.like_query_certificate_number FROM t_account a) o" />
</rewrite-assertion>
<rewrite-assertion
id="select_not_nested_subquery_in_table_segment_with_shorthand_project_alias"
db-types="MySQL">
<input sql="SELECT u.amount, u.password, o.certificate_number FROM
(SELECT a.* FROM t_account a) o, t_account u WHERE
o.certificate_number=u.certificate_number AND u.password=?" parameters="1" />
- <output sql="SELECT u.cipher_amount AS amount, u.cipher_password AS
password, o.certificate_number FROM (SELECT a.`account_id`,
a.`cipher_certificate_number` AS `certificate_number`,
a.`cipher_certificate_number`, a.`assisted_query_certificate_number`,
a.`like_query_certificate_number`, a.`cipher_password` AS `password`,
a.`cipher_password`, a.`assisted_query_password`, a.`like_query_password`,
a.`cipher_amount` AS `amount`, a.`cipher_amount` FROM t_account a) o, t_account
u WHER [...]
+ <output sql="SELECT u.cipher_amount AS amount, u.cipher_password AS
password, o.cipher_certificate_number AS certificate_number FROM (SELECT
a.`account_id`, a.`cipher_certificate_number` AS `certificate_number`,
a.`cipher_certificate_number`, a.`assisted_query_certificate_number`,
a.`like_query_certificate_number`, a.`cipher_password` AS `password`,
a.`cipher_password`, a.`assisted_query_password`, a.`like_query_password`,
a.`cipher_amount` AS `amount`, a.`cipher_amount` FROM t_a [...]
</rewrite-assertion>
<rewrite-assertion
id="select_not_nested_subquery_in_table_segment_with_shorthand_project_alias_quote"
db-types="MySQL">
<input sql="SELECT u.amount, u.password, o.certificate_number FROM
(SELECT a.* FROM t_account `a`) o, t_account u WHERE
o.certificate_number=u.certificate_number AND u.password=?" parameters="1" />
- <output sql="SELECT u.cipher_amount AS amount, u.cipher_password AS
password, o.certificate_number FROM (SELECT `a`.`account_id`,
`a`.`cipher_certificate_number` AS `certificate_number`,
`a`.`cipher_certificate_number`, `a`.`assisted_query_certificate_number`,
`a`.`like_query_certificate_number`, `a`.`cipher_password` AS `password`,
`a`.`cipher_password`, `a`.`assisted_query_password`,
`a`.`like_query_password`, `a`.`cipher_amount` AS `amount`, `a`.`cipher_amount`
FROM t_account [...]
+ <output sql="SELECT u.cipher_amount AS amount, u.cipher_password AS
password, o.cipher_certificate_number AS certificate_number FROM (SELECT
`a`.`account_id`, `a`.`cipher_certificate_number` AS `certificate_number`,
`a`.`cipher_certificate_number`, `a`.`assisted_query_certificate_number`,
`a`.`like_query_certificate_number`, `a`.`cipher_password` AS `password`,
`a`.`cipher_password`, `a`.`assisted_query_password`,
`a`.`like_query_password`, `a`.`cipher_amount` AS `amount`, `a`.`c [...]
</rewrite-assertion>
<rewrite-assertion
id="select_not_nested_subquery_in_table_segment_with_shorthand_project"
db-types="MySQL">
<input sql="SELECT u.amount, u.password, o.certificate_number FROM
(SELECT * FROM t_account) o, t_account u WHERE
o.certificate_number=u.certificate_number AND u.password=?" parameters="1" />
- <output sql="SELECT u.cipher_amount AS amount, u.cipher_password AS
password, o.certificate_number FROM (SELECT t_account.`account_id`,
t_account.`cipher_certificate_number` AS `certificate_number`,
t_account.`cipher_certificate_number`,
t_account.`assisted_query_certificate_number`,
t_account.`like_query_certificate_number`, t_account.`cipher_password` AS
`password`, t_account.`cipher_password`, t_account.`assisted_query_password`,
t_account.`like_query_password`, t_account.`cip [...]
+ <output sql="SELECT u.cipher_amount AS amount, u.cipher_password AS
password, o.cipher_certificate_number AS certificate_number FROM (SELECT
t_account.`account_id`, t_account.`cipher_certificate_number` AS
`certificate_number`, t_account.`cipher_certificate_number`,
t_account.`assisted_query_certificate_number`,
t_account.`like_query_certificate_number`, t_account.`cipher_password` AS
`password`, t_account.`cipher_password`, t_account.`assisted_query_password`,
t_account.`like_qu [...]
</rewrite-assertion>
<rewrite-assertion
id="select_not_nested_subquery_in_predicate_right_equal_condition"
db-types="MySQL">
@@ -89,7 +89,7 @@
<rewrite-assertion
id="select_not_nested_subquery_in_tablesegment_from_alias" db-types="MySQL">
<input sql="SELECT b.certificate_number, b.amount FROM (SELECT
a.certificate_number as certificate_number, a.amount FROM t_account a WHERE
a.amount = 1373) b" />
- <output sql="SELECT b.certificate_number, b.amount FROM (SELECT
a.cipher_certificate_number AS certificate_number, a.cipher_certificate_number,
a.assisted_query_certificate_number, a.like_query_certificate_number,
a.cipher_amount AS amount, a.cipher_amount FROM t_account a WHERE
a.cipher_amount = 'encrypt_1373') b" />
+ <output sql="SELECT b.cipher_certificate_number AS certificate_number,
b.cipher_amount AS amount FROM (SELECT a.cipher_certificate_number AS
certificate_number, a.cipher_certificate_number,
a.assisted_query_certificate_number, a.like_query_certificate_number,
a.cipher_amount AS amount, a.cipher_amount FROM t_account a WHERE
a.cipher_amount = 'encrypt_1373') b" />
</rewrite-assertion>
<rewrite-assertion id="select_with_exists_sub_query" db-types="MySQL">
diff --git
a/test/it/rewriter/src/test/resources/scenario/mix/case/query-with-cipher/dml/select/select-subquery.xml
b/test/it/rewriter/src/test/resources/scenario/mix/case/query-with-cipher/dml/select/select-subquery.xml
index aa6eb095034..ce0a73e983d 100644
---
a/test/it/rewriter/src/test/resources/scenario/mix/case/query-with-cipher/dml/select/select-subquery.xml
+++
b/test/it/rewriter/src/test/resources/scenario/mix/case/query-with-cipher/dml/select/select-subquery.xml
@@ -37,7 +37,7 @@
<rewrite-assertion id="select_not_nested_subquery_in_table_segment_alias"
db-types="MySQL">
<input sql="SELECT o.password FROM (SELECT a.password FROM t_account
a) o" />
- <output sql="SELECT o.password FROM (SELECT a.cipher_password AS
password, a.cipher_password, a.assisted_query_password FROM t_account_0 a) o" />
- <output sql="SELECT o.password FROM (SELECT a.cipher_password AS
password, a.cipher_password, a.assisted_query_password FROM t_account_1 a) o" />
+ <output sql="SELECT o.cipher_password AS password FROM (SELECT
a.cipher_password AS password, a.cipher_password, a.assisted_query_password
FROM t_account_0 a) o" />
+ <output sql="SELECT o.cipher_password AS password FROM (SELECT
a.cipher_password AS password, a.cipher_password, a.assisted_query_password
FROM t_account_1 a) o" />
</rewrite-assertion>
</rewrite-assertions>