Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove useless isNeedRewrite call in EncryptProjectionTokenGenerator #33927

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -35,26 +35,18 @@
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;
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 @@ private Optional<SubstitutableColumnNameToken> generateSQLToken(final SelectStat
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 @@ private boolean isNeedRewrite(final SelectStatementContext selectStatementContex
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();
Original file line number Diff line number Diff line change
@@ -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 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 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 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_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 `a`) 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 `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 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_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.`cipher_amount` AS `amount`, t_account.`cipher_amount` 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 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.`cipher_amount` AS `amount`, t_account.`cipher_amount` 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_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">
Original file line number Diff line number Diff line change
@@ -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>