Skip to content

Commit

Permalink
Add EncryptProjectionRewriteSupportedChecker
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jul 29, 2024
1 parent 60fffe1 commit 5d8c342
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
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;
Expand All @@ -31,17 +31,14 @@
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;
Expand Down Expand Up @@ -78,11 +75,9 @@ public Collection<SQLToken> generateSQLTokens(final SelectStatementContext selec

private Collection<SQLToken> generateSelectSQLTokens(final SelectStatementContext selectStatementContext) {
Collection<SQLToken> result = new LinkedList<>();
ShardingSpherePreconditions.checkState(!containsEncryptProjectionInCombineSegment(selectStatementContext),
() -> new UnsupportedSQLOperationException("Can not support encrypt projection in combine statement."));
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(result::add);
}
Expand All @@ -97,36 +92,6 @@ private Collection<SQLToken> generateSelectSQLTokens(final SelectStatementContex
return result;
}

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++) {
if (containsEncryptProjectionInCombineSegment(leftProjections.get(i), rightProjections.get(i))) {
return true;
}
}
return false;
}

private boolean containsEncryptProjectionInCombineSegment(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 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()));
}

private Optional<SubstitutableColumnNameToken> generateSQLToken(final SelectStatementContext selectStatementContext, final ColumnProjectionSegment columnSegment) {
ColumnProjection columnProjection = buildColumnProjection(columnSegment);
String columnName = columnProjection.getOriginalColumn().getValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -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."));
}
}

0 comments on commit 5d8c342

Please sign in to comment.