Skip to content

Commit

Permalink
Fix empty sql in SQL E2E (#28877)
Browse files Browse the repository at this point in the history
* Fix empty sql in SQL E2E

* Optimize empty filter when use splitter

* Fix code style

* Fix code style
  • Loading branch information
RaigorJiang authored Oct 27, 2023
1 parent 937a557 commit 1f28db4
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.util.stream.Collectors;

/**
* Result set for show sharding table nodes.
* Show sharding table nodes executor.
*/
public final class ShowShardingTableNodesExecutor implements RQLExecutor<ShowShardingTableNodesStatement> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public interface RQLExecutor<T extends RQLStatement> extends TypedSPI {
/**
* Get query result rows.
*
* @param shardingSphereDatabase ShardingSphere database
* @param database database
* @param sqlStatement SQL statement
* @return query result rows
*/
Collection<LocalDataQueryResultRow> getRows(ShardingSphereDatabase shardingSphereDatabase, T sqlStatement);
Collection<LocalDataQueryResultRow> getRows(ShardingSphereDatabase database, T sqlStatement);

@Override
Class<T> getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.google.common.base.Splitter;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.shaded.com.google.common.base.Strings;
import org.apache.shardingsphere.data.pipeline.common.config.CreateTableConfiguration.CreateTableEntry;
import org.apache.shardingsphere.data.pipeline.common.datasource.PipelineDataSourceManager;
import org.apache.shardingsphere.data.pipeline.core.preparer.datasource.AbstractDataSourcePreparer;
Expand All @@ -28,7 +27,6 @@

import java.sql.Connection;
import java.sql.SQLException;
import java.util.stream.Collectors;

/**
* Data source preparer for openGauss.
Expand All @@ -55,7 +53,7 @@ public void prepareTargetTables(final PrepareTargetTablesParameter param) throws
for (CreateTableEntry each : param.getCreateTableConfig().getCreateTableEntries()) {
String createTargetTableSQL = getCreateTargetTableSQL(each, dataSourceManager, param.getSqlParserEngine());
try (Connection targetConnection = getCachedDataSource(dataSourceManager, each.getTargetDataSourceConfig()).getConnection()) {
for (String sql : Splitter.on(";").trimResults().splitToList(createTargetTableSQL).stream().filter(cs -> !Strings.isNullOrEmpty(cs)).collect(Collectors.toList())) {
for (String sql : Splitter.on(";").trimResults().omitEmptyStrings().splitToList(createTargetTableSQL)) {
executeTargetTableSQL(targetConnection, addIfNotExistsForCreateTableSQL(sql));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@
package org.apache.shardingsphere.data.pipeline.postgresql.prepare.datasource;

import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import org.apache.shardingsphere.data.pipeline.common.config.CreateTableConfiguration.CreateTableEntry;
import org.apache.shardingsphere.data.pipeline.common.datasource.PipelineDataSourceManager;
import org.apache.shardingsphere.data.pipeline.core.preparer.datasource.AbstractDataSourcePreparer;
import org.apache.shardingsphere.data.pipeline.core.preparer.datasource.PrepareTargetTablesParameter;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.stream.Collectors;

/**
* Data source preparer for PostgreSQL.
Expand All @@ -39,7 +37,7 @@ public void prepareTargetTables(final PrepareTargetTablesParameter param) throws
for (CreateTableEntry each : param.getCreateTableConfig().getCreateTableEntries()) {
String createTargetTableSQL = getCreateTargetTableSQL(each, dataSourceManager, param.getSqlParserEngine());
try (Connection targetConnection = getCachedDataSource(dataSourceManager, each.getTargetDataSourceConfig()).getConnection()) {
for (String sql : Splitter.on(";").trimResults().splitToList(createTargetTableSQL).stream().filter(cs -> !Strings.isNullOrEmpty(cs)).collect(Collectors.toList())) {
for (String sql : Splitter.on(";").trimResults().omitEmptyStrings().splitToList(createTargetTableSQL)) {
executeTargetTableSQL(targetConnection, sql);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable;

import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import lombok.SneakyThrows;
import org.apache.shardingsphere.distsql.statement.ral.queryable.ConvertYamlConfigurationStatement;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
import org.apache.shardingsphere.parser.rule.builder.DefaultSQLParserRuleConfigurationBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
Expand All @@ -37,7 +37,6 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class ConvertYamlConfigurationExecutorTest {

Expand Down Expand Up @@ -97,11 +96,11 @@ private void assertRowData(final Collection<LocalDataQueryResultRow> data, final
}

private void assertParseSQL(final String actual) {
Splitter.on(";").trimResults().splitToList(actual).forEach(each -> {
if (!Strings.isNullOrEmpty(each)) {
assertNotNull(sqlParserRule.getSQLParserEngine(TypedSPILoader.getService(DatabaseType.class, "MySQL")).parse(each, false));
}
});
Splitter.on(";").trimResults().omitEmptyStrings().splitToList(actual).forEach(this::assertNotNull);
}

private void assertNotNull(final String sql) {
Assertions.assertNotNull(sqlParserRule.getSQLParserEngine(TypedSPILoader.getService(DatabaseType.class, "MySQL")).parse(sql, false));
}

@SneakyThrows(IOException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private void executeInitSQLs(final SingleE2EContainerComposer containerComposer,
if (null == containerComposer.getAssertion().getInitialSQL().getSql()) {
return;
}
for (String each : Splitter.on(";").trimResults().splitToList(containerComposer.getAssertion().getInitialSQL().getSql())) {
for (String each : Splitter.on(";").trimResults().omitEmptyStrings().splitToList(containerComposer.getAssertion().getInitialSQL().getSql())) {
try (PreparedStatement preparedStatement = connection.prepareStatement(each)) {
preparedStatement.executeUpdate();
}
Expand All @@ -159,7 +159,7 @@ private void executeDestroySQLs(final SingleE2EContainerComposer containerCompos
if (null == containerComposer.getAssertion().getDestroySQL().getSql()) {
return;
}
for (String each : Splitter.on(";").trimResults().splitToList(containerComposer.getAssertion().getDestroySQL().getSql())) {
for (String each : Splitter.on(";").trimResults().omitEmptyStrings().splitToList(containerComposer.getAssertion().getDestroySQL().getSql())) {
try (PreparedStatement preparedStatement = connection.prepareStatement(each)) {
preparedStatement.executeUpdate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private void executeInitSQLs(final SingleE2EContainerComposer containerComposer,
if (null == containerComposer.getAssertion().getInitialSQL().getSql()) {
return;
}
for (String each : Splitter.on(";").trimResults().splitToList(containerComposer.getAssertion().getInitialSQL().getSql())) {
for (String each : Splitter.on(";").trimResults().omitEmptyStrings().splitToList(containerComposer.getAssertion().getInitialSQL().getSql())) {
try (PreparedStatement preparedStatement = connection.prepareStatement(each)) {
preparedStatement.executeUpdate();
}
Expand All @@ -105,7 +105,7 @@ private void executeDestroySQLs(final SingleE2EContainerComposer containerCompos
if (null == containerComposer.getAssertion().getDestroySQL().getSql()) {
return;
}
for (String each : Splitter.on(";").trimResults().splitToList(containerComposer.getAssertion().getDestroySQL().getSql())) {
for (String each : Splitter.on(";").trimResults().omitEmptyStrings().splitToList(containerComposer.getAssertion().getDestroySQL().getSql())) {
try (PreparedStatement preparedStatement = connection.prepareStatement(each)) {
preparedStatement.executeUpdate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private void executeInitSQLs(final SingleE2EContainerComposer containerComposer,
if (null == containerComposer.getAssertion().getInitialSQL() || null == containerComposer.getAssertion().getInitialSQL().getSql()) {
return;
}
for (String each : Splitter.on(";").trimResults().splitToList(containerComposer.getAssertion().getInitialSQL().getSql())) {
for (String each : Splitter.on(";").trimResults().omitEmptyStrings().splitToList(containerComposer.getAssertion().getInitialSQL().getSql())) {
try (PreparedStatement preparedStatement = connection.prepareStatement(each)) {
preparedStatement.executeUpdate();
Awaitility.await().pollDelay(2L, TimeUnit.SECONDS).until(() -> true);
Expand All @@ -111,7 +111,7 @@ private void executeDestroySQLs(final SingleE2EContainerComposer containerCompos
if (null == containerComposer.getAssertion().getDestroySQL().getSql()) {
return;
}
for (String each : Splitter.on(";").trimResults().splitToList(containerComposer.getAssertion().getDestroySQL().getSql())) {
for (String each : Splitter.on(";").trimResults().omitEmptyStrings().splitToList(containerComposer.getAssertion().getDestroySQL().getSql())) {
try (PreparedStatement preparedStatement = connection.prepareStatement(each)) {
preparedStatement.executeUpdate();
Awaitility.await().pollDelay(2L, TimeUnit.SECONDS).until(() -> true);
Expand Down

0 comments on commit 1f28db4

Please sign in to comment.