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 Index0utOfBoundsException in GeneratedKeyContextEngine caused by wrong generated flag in metadata #32935

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.ShardingTable;
import org.junit.jupiter.api.Test;

import java.sql.Types;
Expand All @@ -51,15 +52,19 @@ void assertReviseWithKeyGenerateStrategy() {
Map<String, SchemaMetaData> actual = new MetaDataReviseEngine(Collections.singleton(mockShardingRule())).revise(Collections.singletonMap("sharding_db",
new SchemaMetaData("sharding_db", Collections.singleton(createTableMetaData()))), material);
Iterator<ColumnMetaData> columns = actual.get("sharding_db").getTables().iterator().next().getColumns().iterator();
assertTrue(columns.next().isGenerated());
assertFalse(columns.next().isGenerated());
assertFalse(columns.next().isGenerated());
assertTrue(columns.next().isGenerated());
}

private ShardingRule mockShardingRule() {
ShardingRule result = mock(ShardingRule.class);
DataNodeRuleAttribute ruleAttribute = mock(DataNodeRuleAttribute.class);
when(ruleAttribute.findLogicTableByActualTable("t_order")).thenReturn(Optional.of("t_order"));
ShardingTable shardingTable = mock(ShardingTable.class);
when(shardingTable.getGenerateKeyColumn()).thenReturn(Optional.of("product_id"));
when(result.getShardingTable("t_order")).thenReturn(shardingTable);
when(result.findShardingTableByActualTable("t_order")).thenReturn(Optional.of(shardingTable));
when(result.getAttributes()).thenReturn(new RuleAttributes(ruleAttribute));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public Collection<ColumnMetaData> revise(final String tableName, final Collectio
Optional<? extends ColumnExistedReviser> existedReviser = reviseEntry.getColumnExistedReviser(rule, tableName);
Optional<? extends ColumnNameReviser> nameReviser = reviseEntry.getColumnNameReviser(rule, tableName);
Optional<? extends ColumnGeneratedReviser> generatedReviser = reviseEntry.getColumnGeneratedReviser(rule, tableName);
Collection<ColumnMetaData> result = new LinkedHashSet<>();
Collection<ColumnMetaData> result = new LinkedHashSet<>(originalMetaDataList.size(), 1F);
for (ColumnMetaData each : originalMetaDataList) {
if (existedReviser.isPresent() && !existedReviser.get().isExisted(each.getName())) {
continue;
}
String name = nameReviser.isPresent() ? nameReviser.get().revise(each.getName()) : each.getName();
boolean generated = generatedReviser.map(optional -> optional.revise(each)).orElseGet(each::isGenerated);
boolean generated = generatedReviser.map(optional -> optional.revise(each)).orElse(false);
result.add(new ColumnMetaData(name, each.getDataType(), each.isPrimaryKey(), generated, each.isCaseSensitive(), each.isVisible(), each.isUnsigned(), each.isNullable()));
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Properties;

Expand All @@ -56,7 +57,19 @@ void assertReviseWithMetaDataReviseEntry() {
SchemaMetaData actual = new SchemaMetaDataReviseEngine(
Collections.singleton(new FixtureGlobalRule()), new ConfigurationProperties(new Properties()), mock(DatabaseType.class), mock(DataSource.class)).revise(schemaMetaData);
assertThat(actual.getName(), is(schemaMetaData.getName()));
assertThat(actual.getTables(), is(schemaMetaData.getTables()));
assertThat(actual.getTables().size(), is(schemaMetaData.getTables().size()));
TableMetaData actualTableMetaData = actual.getTables().iterator().next();
TableMetaData expectedTableMetaData = schemaMetaData.getTables().iterator().next();
assertThat(actualTableMetaData.getName(), is(expectedTableMetaData.getName()));
assertThat(actualTableMetaData.getType(), is(expectedTableMetaData.getType()));
assertThat(actualTableMetaData.getIndexes(), is(expectedTableMetaData.getIndexes()));
assertThat(actualTableMetaData.getConstraints(), is(expectedTableMetaData.getConstraints()));
assertThat(actualTableMetaData.getColumns().size(), is(expectedTableMetaData.getColumns().size()));
Iterator<ColumnMetaData> actualColumnIterator = actualTableMetaData.getColumns().iterator();
Iterator<ColumnMetaData> expectedColumnIterator = expectedTableMetaData.getColumns().iterator();
while (actualColumnIterator.hasNext() && expectedColumnIterator.hasNext()) {
assertColumnMetaData(actualColumnIterator.next(), expectedColumnIterator.next());
}
}

private TableMetaData createTableMetaData() {
Expand All @@ -68,4 +81,14 @@ private TableMetaData createTableMetaData() {
return new TableMetaData("table_name", columns, Collections.singletonList(indexMetaData), Collections.singleton(constraintMetaData));
}

private void assertColumnMetaData(final ColumnMetaData actualColumnMetaData, final ColumnMetaData expectedColumnMetaData) {
assertThat(actualColumnMetaData.getName(), is(expectedColumnMetaData.getName()));
assertThat(actualColumnMetaData.getDataType(), is(expectedColumnMetaData.getDataType()));
assertThat(actualColumnMetaData.isPrimaryKey(), is(expectedColumnMetaData.isPrimaryKey()));
assertThat(actualColumnMetaData.isGenerated(), is(false));
assertThat(actualColumnMetaData.isCaseSensitive(), is(expectedColumnMetaData.isCaseSensitive()));
assertThat(actualColumnMetaData.isVisible(), is(expectedColumnMetaData.isVisible()));
assertThat(actualColumnMetaData.isUnsigned(), is(expectedColumnMetaData.isUnsigned()));
assertThat(actualColumnMetaData.isNullable(), is(expectedColumnMetaData.isNullable()));
}
}
Loading