Skip to content

Commit

Permalink
Add column type assert in BaseDQLE2EIT (#29390)
Browse files Browse the repository at this point in the history
* Add column type assert in BaseDQLE2EIT

* skip proxy adapter column type assert

* skip proxy adapter column type assert
  • Loading branch information
strongduanmu authored Dec 13, 2023
1 parent f895ac9 commit 7ec53c2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,21 @@

import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.apache.commons.collections4.map.CaseInsensitiveMap;
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.database.core.metadata.database.enums.QuoteCharacter;
import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.RouteUnitAware;
import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.SQLToken;
import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.Substitutable;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter;
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

/**
* Substitutable column name token.
Expand All @@ -61,14 +60,6 @@ public SubstitutableColumnNameToken(final int startIndex, final int stopIndex, f
this.projections = projections;
}

public SubstitutableColumnNameToken(final int startIndex, final int stopIndex, final Collection<Projection> projections, final boolean lastColumn) {
super(startIndex);
this.stopIndex = stopIndex;
this.lastColumn = lastColumn;
this.quoteCharacter = QuoteCharacter.NONE;
this.projections = projections;
}

public SubstitutableColumnNameToken(final int startIndex, final int stopIndex, final Collection<Projection> projections, final QuoteCharacter quoteCharacter) {
super(startIndex);
this.stopIndex = stopIndex;
Expand Down Expand Up @@ -100,7 +91,7 @@ private Map<String, String> getLogicAndActualTables(final RouteUnit routeUnit) {
if (null == routeUnit) {
return Collections.emptyMap();
}
Map<String, String> result = new LinkedHashMap<>();
Map<String, String> result = new CaseInsensitiveMap<>();
for (RouteMapper each : routeUnit.getTableMappers()) {
result.put(each.getLogicName().toLowerCase(), each.getActualName());
}
Expand All @@ -112,21 +103,24 @@ private String getColumnExpression(final Projection projection, final Map<String
if (projection instanceof ColumnProjection) {
appendColumnProjection((ColumnProjection) projection, logicActualTableNames, builder);
} else {
// TODO use alias quoteCharacter to avoid oracle rewrite error
builder.append(quoteCharacter.wrap(projection.getColumnLabel()));
}
return builder.toString();
}

private static void appendColumnProjection(final ColumnProjection columnProjection, final Map<String, String> logicActualTableNames, final StringBuilder builder) {
private void appendColumnProjection(final ColumnProjection columnProjection, final Map<String, String> logicActualTableNames, final StringBuilder builder) {
if (columnProjection.getOwner().isPresent()) {
Optional<IdentifierValue> owner = columnProjection.getOwner();
String lowerCaseOwner = owner.get().getValue().toLowerCase();
builder.append(owner.get().getQuoteCharacter().wrap(logicActualTableNames.getOrDefault(lowerCaseOwner, owner.get().getValue()))).append('.');
IdentifierValue owner = columnProjection.getOwner().get();
String actualTableOwner = logicActualTableNames.getOrDefault(owner.getValue(), owner.getValue());
builder.append(getValueWithQuoteCharacters(new IdentifierValue(actualTableOwner, owner.getQuoteCharacter()))).append('.');
}
builder.append(columnProjection.getName().getValueWithQuoteCharacters());
if (columnProjection.getAlias().isPresent()) {
builder.append(" AS ").append(columnProjection.getAlias().get().getValueWithQuoteCharacters());
}
}

private String getValueWithQuoteCharacters(final IdentifierValue identifierValue) {
return QuoteCharacter.NONE == identifierValue.getQuoteCharacter() ? identifierValue.getValue() : quoteCharacter.wrap(identifierValue.getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void assertExecuteQueryWithResultSetTypeAndConcurrency(final AssertionTestParame
if (isUseXMLAsExpectedDataset()) {
assertExecuteQueryWithXMLExpected(testParam, containerComposer, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
} else {
assertExecuteQueryWithExpectedDataSource(containerComposer, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
assertExecuteQueryWithExpectedDataSource(testParam, containerComposer, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
}
}

Expand All @@ -81,7 +81,7 @@ void assertExecuteQueryWithResultSetTypeAndConcurrencyAndHoldability(final Asser
if (isUseXMLAsExpectedDataset()) {
assertExecuteQueryWithXMLExpected(testParam, containerComposer, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
} else {
assertExecuteQueryWithExpectedDataSource(containerComposer, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
assertExecuteQueryWithExpectedDataSource(testParam, containerComposer, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}
}

Expand All @@ -102,7 +102,7 @@ void assertExecuteWithResultSetTypeAndConcurrency(final AssertionTestParameter t
if (isUseXMLAsExpectedDataset()) {
assertExecuteWithXMLExpected(testParam, containerComposer, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
} else {
assertExecuteWithExpectedDataSource(containerComposer, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
assertExecuteWithExpectedDataSource(testParam, containerComposer, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
}
}

Expand All @@ -123,7 +123,7 @@ void assertExecuteWithResultSetTypeAndConcurrencyAndHoldability(final AssertionT
if (isUseXMLAsExpectedDataset()) {
assertExecuteWithXMLExpected(testParam, containerComposer, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
} else {
assertExecuteWithExpectedDataSource(containerComposer, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
assertExecuteWithExpectedDataSource(testParam, containerComposer, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}
}

Expand All @@ -146,34 +146,34 @@ private void assertExecuteQueryWithXMLExpected(final AssertionTestParameter test
}
}

private void assertExecuteQueryWithExpectedDataSource(final SingleE2EContainerComposer containerComposer, final int... resultSetTypes) throws SQLException {
private void assertExecuteQueryWithExpectedDataSource(final AssertionTestParameter testParam, final SingleE2EContainerComposer containerComposer, final int... resultSetTypes) throws SQLException {
try (
Connection actualConnection = containerComposer.getTargetDataSource().getConnection();
Connection expectedConnection = getExpectedDataSource().getConnection()) {
if (SQLExecuteType.Literal == containerComposer.getSqlExecuteType()) {
assertExecuteQueryForStatementWithResultSetTypes(containerComposer, actualConnection, expectedConnection, resultSetTypes);
assertExecuteQueryForStatementWithResultSetTypes(containerComposer, actualConnection, expectedConnection, testParam, resultSetTypes);
} else {
assertExecuteQueryForPreparedStatementWithResultSetTypes(containerComposer, actualConnection, expectedConnection, resultSetTypes);
assertExecuteQueryForPreparedStatementWithResultSetTypes(containerComposer, actualConnection, expectedConnection, testParam, resultSetTypes);
}
}
}

private void assertExecuteQueryForStatementWithResultSetTypes(final SingleE2EContainerComposer containerComposer,
final Connection actualConnection, final Connection expectedConnection,
final int... resultSetTypes) throws SQLException {
final AssertionTestParameter testParam, final int... resultSetTypes) throws SQLException {
try (
Statement actualStatement = 2 == resultSetTypes.length ? actualConnection.createStatement(resultSetTypes[0], resultSetTypes[1])
: actualConnection.createStatement(resultSetTypes[0], resultSetTypes[1], resultSetTypes[2]);
ResultSet actualResultSet = actualStatement.executeQuery(containerComposer.getSQL());
Statement expectedStatement = 2 == resultSetTypes.length ? expectedConnection.createStatement(resultSetTypes[0], resultSetTypes[1])
: expectedConnection.createStatement(resultSetTypes[0], resultSetTypes[1], resultSetTypes[2]);
ResultSet expectedResultSet = expectedStatement.executeQuery(containerComposer.getSQL())) {
assertResultSet(actualResultSet, expectedResultSet);
assertResultSet(actualResultSet, expectedResultSet, testParam);
}
}

private void assertExecuteQueryForPreparedStatementWithResultSetTypes(final SingleE2EContainerComposer containerComposer, final Connection actualConnection, final Connection expectedConnection,
final int... resultSetTypes) throws SQLException {
final AssertionTestParameter testParam, final int... resultSetTypes) throws SQLException {
try (
PreparedStatement actualPreparedStatement = 2 == resultSetTypes.length ? actualConnection.prepareStatement(containerComposer.getSQL(), resultSetTypes[0], resultSetTypes[1])
: actualConnection.prepareStatement(containerComposer.getSQL(), resultSetTypes[0], resultSetTypes[1], resultSetTypes[2]);
Expand All @@ -186,7 +186,7 @@ private void assertExecuteQueryForPreparedStatementWithResultSetTypes(final Sing
try (
ResultSet actualResultSet = actualPreparedStatement.executeQuery();
ResultSet expectedResultSet = expectedPreparedStatement.executeQuery()) {
assertResultSet(actualResultSet, expectedResultSet);
assertResultSet(actualResultSet, expectedResultSet, testParam);
}
}
}
Expand All @@ -207,20 +207,20 @@ private void assertExecuteWithXMLExpected(final AssertionTestParameter testParam
}
}

private void assertExecuteWithExpectedDataSource(final SingleE2EContainerComposer containerComposer, final int... resultSetTypes) throws SQLException {
private void assertExecuteWithExpectedDataSource(final AssertionTestParameter testParam, final SingleE2EContainerComposer containerComposer, final int... resultSetTypes) throws SQLException {
try (
Connection actualConnection = containerComposer.getTargetDataSource().getConnection();
Connection expectedConnection = getExpectedDataSource().getConnection()) {
if (SQLExecuteType.Literal == containerComposer.getSqlExecuteType()) {
assertExecuteForStatementWithResultSetTypes(containerComposer, actualConnection, expectedConnection, resultSetTypes);
assertExecuteForStatementWithResultSetTypes(containerComposer, actualConnection, expectedConnection, testParam, resultSetTypes);
} else {
assertExecuteForPreparedStatementWithResultSetTypes(containerComposer, actualConnection, expectedConnection, resultSetTypes);
assertExecuteForPreparedStatementWithResultSetTypes(containerComposer, actualConnection, expectedConnection, testParam, resultSetTypes);
}
}
}

private void assertExecuteForStatementWithResultSetTypes(final SingleE2EContainerComposer containerComposer,
final Connection actualConnection, final Connection expectedConnection, final int... resultSetTypes) throws SQLException {
private void assertExecuteForStatementWithResultSetTypes(final SingleE2EContainerComposer containerComposer, final Connection actualConnection, final Connection expectedConnection,
final AssertionTestParameter testParam, final int... resultSetTypes) throws SQLException {
try (
Statement actualStatement = 2 == resultSetTypes.length ? actualConnection.createStatement(resultSetTypes[0], resultSetTypes[1])
: actualConnection.createStatement(resultSetTypes[0], resultSetTypes[1], resultSetTypes[2]);
Expand All @@ -230,13 +230,13 @@ private void assertExecuteForStatementWithResultSetTypes(final SingleE2EContaine
try (
ResultSet actualResultSet = actualStatement.getResultSet();
ResultSet expectedResultSet = expectedStatement.getResultSet()) {
assertResultSet(actualResultSet, expectedResultSet);
assertResultSet(actualResultSet, expectedResultSet, testParam);
}
}
}

private void assertExecuteForPreparedStatementWithResultSetTypes(final SingleE2EContainerComposer containerComposer, final Connection actualConnection, final Connection expectedConnection,
final int... resultSetTypes) throws SQLException {
final AssertionTestParameter testParam, final int... resultSetTypes) throws SQLException {
try (
PreparedStatement actualPreparedStatement = 2 == resultSetTypes.length ? actualConnection.prepareStatement(containerComposer.getSQL(), resultSetTypes[0], resultSetTypes[1])
: actualConnection.prepareStatement(containerComposer.getSQL(), resultSetTypes[0], resultSetTypes[1], resultSetTypes[2]);
Expand All @@ -250,7 +250,7 @@ private void assertExecuteForPreparedStatementWithResultSetTypes(final SingleE2E
try (
ResultSet actualResultSet = actualPreparedStatement.getResultSet();
ResultSet expectedResultSet = expectedPreparedStatement.getResultSet()) {
assertResultSet(actualResultSet, expectedResultSet);
assertResultSet(actualResultSet, expectedResultSet, testParam);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ private void fillDataOnlyOnce(final AssertionTestParameter testParam, final Sing
}
}

protected final void assertResultSet(final ResultSet actualResultSet, final ResultSet expectedResultSet) throws SQLException {
assertMetaData(actualResultSet.getMetaData(), expectedResultSet.getMetaData());
protected final void assertResultSet(final ResultSet actualResultSet, final ResultSet expectedResultSet, final AssertionTestParameter testParam) throws SQLException {
assertMetaData(actualResultSet.getMetaData(), expectedResultSet.getMetaData(), testParam);
assertRows(actualResultSet, expectedResultSet);
}

Expand All @@ -117,11 +117,15 @@ private Collection<String> getNotAssertionColumns(final SingleE2EContainerCompos
return result;
}

private void assertMetaData(final ResultSetMetaData actualResultSetMetaData, final ResultSetMetaData expectedResultSetMetaData) throws SQLException {
private void assertMetaData(final ResultSetMetaData actualResultSetMetaData, final ResultSetMetaData expectedResultSetMetaData, final AssertionTestParameter testParam) throws SQLException {
assertThat(actualResultSetMetaData.getColumnCount(), is(expectedResultSetMetaData.getColumnCount()));
for (int i = 0; i < actualResultSetMetaData.getColumnCount(); i++) {
assertThat(actualResultSetMetaData.getColumnLabel(i + 1), is(expectedResultSetMetaData.getColumnLabel(i + 1)));
assertThat(actualResultSetMetaData.getColumnName(i + 1), is(expectedResultSetMetaData.getColumnName(i + 1)));
if ("jdbc".equals(testParam.getAdapter()) && "Cluster".equals(testParam.getMode())) {
// FIXME correct columnType with proxy adapter
assertThat(actualResultSetMetaData.getColumnType(i + 1), is(expectedResultSetMetaData.getColumnType(i + 1)));
}
}
}

Expand Down
Loading

0 comments on commit 7ec53c2

Please sign in to comment.