You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2024/07/29 15:11:16 UTC
(shardingsphere) branch master updated: Add EncryptProjectionRewriteSupportedChecker (#32323)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 f0cfe064712 Add EncryptProjectionRewriteSupportedChecker (#32323)
f0cfe064712 is described below
commit f0cfe0647127661b001ac85cb0bb5ed25c03f0a5
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon Jul 29 23:11:09 2024 +0800
Add EncryptProjectionRewriteSupportedChecker (#32323)
* Refactor EncryptProjectionTokenGenerator
* Add EncryptProjectionRewriteSupportedChecker
* Add EncryptProjectionRewriteSupportedChecker
---
.../EncryptProjectionTokenGenerator.java | 56 +++----------
.../EncryptProjectionRewriteSupportedChecker.java | 96 ++++++++++++++++++++++
2 files changed, 108 insertions(+), 44 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 b9f30b7cca5..09a15c320fb 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
@@ -18,10 +18,11 @@
package org.apache.shardingsphere.encrypt.rewrite.token.generator.projection;
import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.encrypt.rewrite.token.generator.projection.checker.EncryptProjectionRewriteSupportedChecker;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.encrypt.rule.column.EncryptColumn;
import org.apache.shardingsphere.encrypt.rule.table.EncryptTable;
-import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
+import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.binder.context.segment.select.projection.DerivedColumn;
import org.apache.shardingsphere.infra.binder.context.segment.select.projection.Projection;
import org.apache.shardingsphere.infra.binder.context.segment.select.projection.ProjectionsContext;
@@ -30,17 +31,14 @@ import org.apache.shardingsphere.infra.binder.context.segment.select.projection.
import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
-import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.SQLToken;
import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.generic.SubstitutableColumnNameToken;
import org.apache.shardingsphere.sql.parser.statement.core.enums.SubqueryType;
-import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.combine.CombineSegment;
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.bound.ColumnSegmentBoundInfo;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
import java.util.Collection;
@@ -53,6 +51,7 @@ import java.util.Optional;
/**
* Projection token generator for encrypt.
*/
+@HighFrequencyInvocation
@RequiredArgsConstructor
public final class EncryptProjectionTokenGenerator {
@@ -69,59 +68,28 @@ public final class EncryptProjectionTokenGenerator {
* @return generated SQL tokens
*/
public Collection<SQLToken> generateSQLTokens(final SelectStatementContext selectStatementContext) {
- Collection<SQLToken> result = new LinkedHashSet<>();
- generateSQLTokens(result, selectStatementContext);
- for (SelectStatementContext each : selectStatementContext.getSubqueryContexts().values()) {
- generateSQLTokens(result, each);
- }
+ Collection<SQLToken> result = new LinkedHashSet<>(generateSelectSQLTokens(selectStatementContext));
+ selectStatementContext.getSubqueryContexts().values().stream().map(this::generateSelectSQLTokens).forEach(result::addAll);
return result;
}
- private void generateSQLTokens(final Collection<SQLToken> generatedSQLTokens, final SelectStatementContext selectStatementContext) {
- ShardingSpherePreconditions.checkState(!containsEncryptProjectionInCombineSegment(selectStatementContext),
- () -> new UnsupportedSQLOperationException("Can not support encrypt projection in combine statement"));
+ private Collection<SQLToken> generateSelectSQLTokens(final SelectStatementContext selectStatementContext) {
+ Collection<SQLToken> result = new LinkedList<>();
+ EncryptProjectionRewriteSupportedChecker.checkNotContainEncryptProjectionInCombineSegment(encryptRule, selectStatementContext);
for (ProjectionSegment each : selectStatementContext.getSqlStatement().getProjections().getProjections()) {
- ShardingSpherePreconditions.checkState(!(each instanceof ShorthandProjectionSegment) || !selectStatementContext.containsTableSubquery(),
- () -> new UnsupportedSQLOperationException("Can not support encrypt shorthand expand with subquery statement"));
+ EncryptProjectionRewriteSupportedChecker.checkNotContainEncryptShorthandExpandWithSubqueryStatement(selectStatementContext, each);
if (each instanceof ColumnProjectionSegment) {
- generateSQLToken(selectStatementContext, (ColumnProjectionSegment) each).ifPresent(generatedSQLTokens::add);
+ generateSQLToken(selectStatementContext, (ColumnProjectionSegment) each).ifPresent(result::add);
}
if (each instanceof ShorthandProjectionSegment) {
ShorthandProjectionSegment shorthandSegment = (ShorthandProjectionSegment) each;
Collection<Projection> actualColumns = getShorthandProjection(shorthandSegment, selectStatementContext.getProjectionsContext()).getActualColumns();
if (!actualColumns.isEmpty()) {
- generatedSQLTokens.add(generateSQLToken(shorthandSegment, actualColumns, selectStatementContext, selectStatementContext.getSubqueryType()));
+ result.add(generateSQLToken(shorthandSegment, actualColumns, selectStatementContext, selectStatementContext.getSubqueryType()));
}
}
}
- }
-
- private boolean containsEncryptProjectionInCombineSegment(final SelectStatementContext selectStatementContext) {
- if (!selectStatementContext.getSqlStatement().getCombine().isPresent()) {
- return false;
- }
- CombineSegment combineSegment = selectStatementContext.getSqlStatement().getCombine().get();
- List<Projection> leftProjections = selectStatementContext.getSubqueryContexts().get(combineSegment.getLeft().getStartIndex()).getProjectionsContext().getExpandProjections();
- List<Projection> rightProjections = selectStatementContext.getSubqueryContexts().get(combineSegment.getRight().getStartIndex()).getProjectionsContext().getExpandProjections();
- ShardingSpherePreconditions.checkState(leftProjections.size() == rightProjections.size(), () -> new UnsupportedSQLOperationException("Column projections must be same for combine statement"));
- for (int i = 0; i < leftProjections.size(); i++) {
- Projection leftProjection = leftProjections.get(i);
- Projection rightProjection = rightProjections.get(i);
- ColumnSegmentBoundInfo leftColumnInfo = getColumnSegmentBoundInfo(leftProjection);
- EncryptAlgorithm leftColumnEncryptor = encryptRule.findQueryEncryptor(leftColumnInfo.getOriginalTable().getValue(), leftColumnInfo.getOriginalColumn().getValue()).orElse(null);
- ColumnSegmentBoundInfo rightColumnInfo = getColumnSegmentBoundInfo(rightProjection);
- EncryptAlgorithm rightColumnEncryptor = encryptRule.findQueryEncryptor(rightColumnInfo.getOriginalTable().getValue(), rightColumnInfo.getOriginalColumn().getValue()).orElse(null);
- if (null != leftColumnEncryptor || null != rightColumnEncryptor) {
- return true;
- }
- }
- return false;
- }
-
- private ColumnSegmentBoundInfo getColumnSegmentBoundInfo(final Projection projection) {
- return projection instanceof ColumnProjection
- ? new ColumnSegmentBoundInfo(null, null, ((ColumnProjection) projection).getOriginalTable(), ((ColumnProjection) projection).getOriginalColumn())
- : new ColumnSegmentBoundInfo(new IdentifierValue(projection.getColumnLabel()));
+ return result;
}
private Optional<SubstitutableColumnNameToken> generateSQLToken(final SelectStatementContext selectStatementContext, final ColumnProjectionSegment columnSegment) {
diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/checker/EncryptProjectionRewriteSupportedChecker.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/checker/EncryptProjectionRewriteSupportedChecker.java
new file mode 100644
index 00000000000..2e350753f0f
--- /dev/null
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/checker/EncryptProjectionRewriteSupportedChecker.java
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.encrypt.rewrite.token.generator.projection.checker;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.encrypt.rule.EncryptRule;
+import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
+import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
+import org.apache.shardingsphere.infra.binder.context.segment.select.projection.Projection;
+import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ColumnProjection;
+import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.combine.CombineSegment;
+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.bound.ColumnSegmentBoundInfo;
+import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+
+import java.util.List;
+
+/**
+ * Projection token generator rewrite supported checker.
+ */
+@HighFrequencyInvocation
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class EncryptProjectionRewriteSupportedChecker {
+
+ /**
+ * Check not contain encrypt projection in combine segment.
+ *
+ * @param encryptRule encrypt rule
+ * @param selectStatementContext select statement context
+ */
+ public static void checkNotContainEncryptProjectionInCombineSegment(final EncryptRule encryptRule, final SelectStatementContext selectStatementContext) {
+ ShardingSpherePreconditions.checkState(!containsEncryptProjectionInCombineSegment(encryptRule, selectStatementContext),
+ () -> new UnsupportedSQLOperationException("Can not support encrypt projection in combine statement."));
+ }
+
+ private static boolean containsEncryptProjectionInCombineSegment(final EncryptRule encryptRule, final SelectStatementContext selectStatementContext) {
+ if (!selectStatementContext.getSqlStatement().getCombine().isPresent()) {
+ return false;
+ }
+ CombineSegment combineSegment = selectStatementContext.getSqlStatement().getCombine().get();
+ List<Projection> leftProjections = selectStatementContext.getSubqueryContexts().get(combineSegment.getLeft().getStartIndex()).getProjectionsContext().getExpandProjections();
+ List<Projection> rightProjections = selectStatementContext.getSubqueryContexts().get(combineSegment.getRight().getStartIndex()).getProjectionsContext().getExpandProjections();
+ ShardingSpherePreconditions.checkState(leftProjections.size() == rightProjections.size(), () -> new UnsupportedSQLOperationException("Column projections must be same for combine statement"));
+ for (int i = 0; i < leftProjections.size(); i++) {
+ if (containsEncryptProjectionInCombineSegment(encryptRule, leftProjections.get(i), rightProjections.get(i))) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private static boolean containsEncryptProjectionInCombineSegment(final EncryptRule encryptRule, final Projection leftProjection, final Projection rightProjection) {
+ ColumnSegmentBoundInfo leftColumnInfo = getColumnSegmentBoundInfo(leftProjection);
+ EncryptAlgorithm leftColumnEncryptor = encryptRule.findQueryEncryptor(leftColumnInfo.getOriginalTable().getValue(), leftColumnInfo.getOriginalColumn().getValue()).orElse(null);
+ ColumnSegmentBoundInfo rightColumnInfo = getColumnSegmentBoundInfo(rightProjection);
+ EncryptAlgorithm rightColumnEncryptor = encryptRule.findQueryEncryptor(rightColumnInfo.getOriginalTable().getValue(), rightColumnInfo.getOriginalColumn().getValue()).orElse(null);
+ return null != leftColumnEncryptor || null != rightColumnEncryptor;
+ }
+
+ private static ColumnSegmentBoundInfo getColumnSegmentBoundInfo(final Projection projection) {
+ return projection instanceof ColumnProjection
+ ? new ColumnSegmentBoundInfo(null, null, ((ColumnProjection) projection).getOriginalTable(), ((ColumnProjection) projection).getOriginalColumn())
+ : new ColumnSegmentBoundInfo(new IdentifierValue(projection.getColumnLabel()));
+ }
+
+ /**
+ * Check not contain encrypt shorthand expand with subquery statement.
+ *
+ * @param selectStatementContext select statement context
+ * @param projectionSegment projection segment
+ */
+ public static void checkNotContainEncryptShorthandExpandWithSubqueryStatement(final SelectStatementContext selectStatementContext, final ProjectionSegment projectionSegment) {
+ ShardingSpherePreconditions.checkState(!(projectionSegment instanceof ShorthandProjectionSegment) || !selectStatementContext.containsTableSubquery(),
+ () -> new UnsupportedSQLOperationException("Can not support encrypt shorthand expand with subquery statement."));
+ }
+}