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

Fix show create table wrong result with encrypt when data type contains float #31024

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
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
41 changes: 41 additions & 0 deletions features/encrypt/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<artifactId>shardingsphere-infra-algorithm-message-digest-md5</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-sql-parser-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.shardingsphere</groupId>
Expand All @@ -77,6 +82,42 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-parser-sql-sql92</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-parser-sql-mysql</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-parser-sql-postgresql</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-parser-sql-opengauss</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-parser-sql-oracle</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-parser-sql-sqlserver</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>commons-codec</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.shardingsphere.infra.merge.engine.decorator.ResultDecorator;
import org.apache.shardingsphere.infra.merge.engine.decorator.ResultDecoratorEngine;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.DALStatement;

import java.util.Optional;
Expand All @@ -37,13 +38,13 @@
public final class EncryptResultDecoratorEngine implements ResultDecoratorEngine<EncryptRule> {

@Override
public Optional<ResultDecorator<EncryptRule>> newInstance(final ShardingSphereDatabase database,
public Optional<ResultDecorator<EncryptRule>> newInstance(final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database,
final EncryptRule encryptRule, final ConfigurationProperties props, final SQLStatementContext sqlStatementContext) {
if (sqlStatementContext instanceof SelectStatementContext) {
return Optional.of(new EncryptDQLResultDecorator(database, encryptRule, (SelectStatementContext) sqlStatementContext));
}
if (sqlStatementContext.getSqlStatement() instanceof DALStatement) {
return Optional.of(new EncryptDALResultDecorator());
return Optional.of(new EncryptDALResultDecorator(globalRuleMetaData));
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.encrypt.merge.dal;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.encrypt.merge.dal.show.DecoratedEncryptShowColumnsMergedResult;
import org.apache.shardingsphere.encrypt.merge.dal.show.DecoratedEncryptShowCreateTableMergedResult;
import org.apache.shardingsphere.encrypt.merge.dal.show.MergedEncryptShowColumnsMergedResult;
Expand All @@ -27,6 +28,7 @@
import org.apache.shardingsphere.infra.merge.engine.decorator.ResultDecorator;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.apache.shardingsphere.infra.merge.result.impl.transparent.TransparentMergedResult;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLExplainStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowColumnsStatement;
Expand All @@ -35,16 +37,19 @@
/**
* DAL result decorator for encrypt.
*/
@RequiredArgsConstructor
public final class EncryptDALResultDecorator implements ResultDecorator<EncryptRule> {

private final RuleMetaData globalRuleMetaData;

@Override
public MergedResult decorate(final QueryResult queryResult, final SQLStatementContext sqlStatementContext, final EncryptRule rule) {
SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
if (sqlStatement instanceof MySQLExplainStatement || sqlStatement instanceof MySQLShowColumnsStatement) {
return new MergedEncryptShowColumnsMergedResult(queryResult, sqlStatementContext, rule);
}
if (sqlStatement instanceof MySQLShowCreateTableStatement) {
return new MergedEncryptShowCreateTableMergedResult(queryResult, sqlStatementContext, rule);
return new MergedEncryptShowCreateTableMergedResult(globalRuleMetaData, queryResult, sqlStatementContext, rule);
}
return new TransparentMergedResult(queryResult);
}
Expand All @@ -56,7 +61,7 @@ public MergedResult decorate(final MergedResult mergedResult, final SQLStatement
return new DecoratedEncryptShowColumnsMergedResult(mergedResult, sqlStatementContext, rule);
}
if (sqlStatement instanceof MySQLShowCreateTableStatement) {
return new DecoratedEncryptShowCreateTableMergedResult(mergedResult, sqlStatementContext, rule);
return new DecoratedEncryptShowCreateTableMergedResult(globalRuleMetaData, mergedResult, sqlStatementContext, rule);
}
return mergedResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;

import java.sql.SQLException;

Expand All @@ -30,8 +31,9 @@ public final class DecoratedEncryptShowCreateTableMergedResult extends EncryptSh

private final MergedResult mergedResult;

public DecoratedEncryptShowCreateTableMergedResult(final MergedResult mergedResult, final SQLStatementContext sqlStatementContext, final EncryptRule encryptRule) {
super(sqlStatementContext, encryptRule);
public DecoratedEncryptShowCreateTableMergedResult(final RuleMetaData globalRuleMetaData, final MergedResult mergedResult, final SQLStatementContext sqlStatementContext,
final EncryptRule encryptRule) {
super(globalRuleMetaData, sqlStatementContext, encryptRule);
this.mergedResult = mergedResult;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@

package org.apache.shardingsphere.encrypt.merge.dal.show;

import com.google.common.base.Splitter;
import org.apache.shardingsphere.encrypt.exception.syntax.UnsupportedEncryptSQLException;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.encrypt.rule.EncryptTable;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.type.TableAvailable;
import org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.parser.SQLParserEngine;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.column.ColumnDefinitionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateTableStatement;

import java.io.InputStream;
import java.io.Reader;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Optional;
Expand All @@ -41,22 +45,22 @@
*/
public abstract class EncryptShowCreateTableMergedResult implements MergedResult {

private static final String COMMA = ",";
private static final String COMMA = ", ";

private static final int CREATE_TABLE_DEFINITION_INDEX = 2;

private final DialectDatabaseMetaData dialectDatabaseMetaData;

private final String tableName;

private final EncryptRule encryptRule;

protected EncryptShowCreateTableMergedResult(final SQLStatementContext sqlStatementContext, final EncryptRule encryptRule) {
private final SQLParserEngine sqlParserEngine;

protected EncryptShowCreateTableMergedResult(final RuleMetaData globalRuleMetaData, final SQLStatementContext sqlStatementContext, final EncryptRule encryptRule) {
ShardingSpherePreconditions.checkState(sqlStatementContext instanceof TableAvailable && 1 == ((TableAvailable) sqlStatementContext).getAllTables().size(),
() -> new UnsupportedEncryptSQLException("SHOW CREATE TABLE FOR MULTI TABLE"));
dialectDatabaseMetaData = new DatabaseTypeRegistry(sqlStatementContext.getDatabaseType()).getDialectDatabaseMetaData();
tableName = ((TableAvailable) sqlStatementContext).getAllTables().iterator().next().getTableName().getIdentifier().getValue();
this.encryptRule = encryptRule;
sqlParserEngine = globalRuleMetaData.getSingleRule(SQLParserRule.class).getSQLParserEngine(sqlStatementContext.getDatabaseType());
}

@Override
Expand All @@ -72,26 +76,31 @@ public final Object getValue(final int columnIndex, final Class<?> type) throws
if (!encryptTable.isPresent() || !result.contains("(")) {
return result;
}
StringBuilder builder = new StringBuilder(result.substring(0, result.indexOf('(') + 1));
List<String> columnDefinitions = Splitter.on(COMMA).splitToList(result.substring(result.indexOf('(') + 1, result.lastIndexOf(')')));
for (String each : columnDefinitions) {
findLogicColumnDefinition(each, encryptTable.get()).ifPresent(optional -> builder.append(optional).append(COMMA));
CreateTableStatement createTableStatement = (CreateTableStatement) sqlParserEngine.parse(result, false);
List<ColumnDefinitionSegment> columnDefinitions = new ArrayList<>(createTableStatement.getColumnDefinitions());
StringBuilder builder = new StringBuilder(result.substring(0, columnDefinitions.get(0).getStartIndex()));
for (ColumnDefinitionSegment each : columnDefinitions) {
findLogicColumnDefinition(each, encryptTable.get(), result).ifPresent(optional -> builder.append(optional).append(COMMA));
}
builder.deleteCharAt(builder.length() - 1).append(result.substring(result.lastIndexOf(')')));
// TODO decorate encrypt column index when we support index rewrite
builder.delete(builder.length() - COMMA.length(), builder.length()).append(result.substring(columnDefinitions.get(columnDefinitions.size() - 1).getStopIndex() + 1));
return builder.toString();
}
return getOriginalValue(columnIndex, type);
}

private Optional<String> findLogicColumnDefinition(final String columnDefinition, final EncryptTable encryptTable) {
String columnName = dialectDatabaseMetaData.getQuoteCharacter().unwrap(columnDefinition.trim().split("\\s+")[0]);
private Optional<String> findLogicColumnDefinition(final ColumnDefinitionSegment columnDefinition, final EncryptTable encryptTable, final String sql) {
ColumnSegment columnSegment = columnDefinition.getColumnName();
String columnName = columnSegment.getIdentifier().getValue();
if (encryptTable.isCipherColumn(columnName)) {
return Optional.of(columnDefinition.replace(columnName, encryptTable.getLogicColumnByCipherColumn(columnName)));
String logicColumn = encryptTable.getLogicColumnByCipherColumn(columnName);
return Optional.of(sql.substring(columnDefinition.getStartIndex(), columnSegment.getStartIndex())
+ columnSegment.getIdentifier().getQuoteCharacter().wrap(logicColumn) + sql.substring(columnSegment.getStopIndex() + 1, columnDefinition.getStopIndex() + 1));
}
if (isDerivedColumn(encryptTable, columnName)) {
return Optional.empty();
}
return Optional.of(columnDefinition);
return Optional.of(sql.substring(columnDefinition.getStartIndex(), columnDefinition.getStopIndex() + 1));
}

private boolean isDerivedColumn(final EncryptTable encryptTable, final String columnName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;

import java.sql.SQLException;

Expand All @@ -30,8 +31,9 @@ public final class MergedEncryptShowCreateTableMergedResult extends EncryptShowC

private final QueryResult queryResult;

public MergedEncryptShowCreateTableMergedResult(final QueryResult queryResult, final SQLStatementContext sqlStatementContext, final EncryptRule encryptRule) {
super(sqlStatementContext, encryptRule);
public MergedEncryptShowCreateTableMergedResult(final RuleMetaData globalRuleMetaData, final QueryResult queryResult, final SQLStatementContext sqlStatementContext,
final EncryptRule encryptRule) {
super(globalRuleMetaData, sqlStatementContext, encryptRule);
this.queryResult = queryResult;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.shardingsphere.infra.merge.engine.ResultProcessEngine;
import org.apache.shardingsphere.infra.merge.engine.decorator.ResultDecorator;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLExplainStatement;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -58,7 +59,8 @@ class EncryptResultDecoratorEngineTest {
@Test
void assertNewInstanceWithSelectStatement() {
EncryptResultDecoratorEngine engine = (EncryptResultDecoratorEngine) OrderedSPILoader.getServices(ResultProcessEngine.class, Collections.singleton(rule)).get(rule);
Optional<ResultDecorator<EncryptRule>> actual = engine.newInstance(database, rule, mock(ConfigurationProperties.class), mock(SelectStatementContext.class, RETURNS_DEEP_STUBS));
Optional<ResultDecorator<EncryptRule>> actual =
engine.newInstance(mock(RuleMetaData.class), database, rule, mock(ConfigurationProperties.class), mock(SelectStatementContext.class, RETURNS_DEEP_STUBS));
assertTrue(actual.isPresent());
assertThat(actual.get(), instanceOf(EncryptDQLResultDecorator.class));
}
Expand All @@ -68,14 +70,14 @@ void assertNewInstanceWithDALStatement() {
SQLStatementContext sqlStatementContext = mock(ExplainStatementContext.class);
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(MySQLExplainStatement.class));
EncryptResultDecoratorEngine engine = (EncryptResultDecoratorEngine) OrderedSPILoader.getServices(ResultProcessEngine.class, Collections.singleton(rule)).get(rule);
Optional<ResultDecorator<EncryptRule>> actual = engine.newInstance(database, rule, mock(ConfigurationProperties.class), sqlStatementContext);
Optional<ResultDecorator<EncryptRule>> actual = engine.newInstance(mock(RuleMetaData.class), database, rule, mock(ConfigurationProperties.class), sqlStatementContext);
assertTrue(actual.isPresent());
assertThat(actual.get(), instanceOf(EncryptDALResultDecorator.class));
}

@Test
void assertNewInstanceWithOtherStatement() {
EncryptResultDecoratorEngine engine = (EncryptResultDecoratorEngine) OrderedSPILoader.getServices(ResultProcessEngine.class, Collections.singleton(rule)).get(rule);
assertFalse(engine.newInstance(database, rule, mock(ConfigurationProperties.class), mock(InsertStatementContext.class)).isPresent());
assertFalse(engine.newInstance(mock(RuleMetaData.class), database, rule, mock(ConfigurationProperties.class), mock(InsertStatementContext.class)).isPresent());
}
}
Loading