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

Support for extending data types based on the Trunk database. #31010

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.database.datatype.DataTypeLoader;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;

import java.sql.Connection;
import java.sql.PreparedStatement;
Expand Down Expand Up @@ -68,7 +69,7 @@ public final class H2MetaDataLoader implements DialectMetaDataLoader {
public Collection<SchemaMetaData> load(final MetaDataLoaderMaterial material) throws SQLException {
Collection<TableMetaData> tableMetaDataList = new LinkedList<>();
try (Connection connection = material.getDataSource().getConnection()) {
Map<String, Collection<ColumnMetaData>> columnMetaDataMap = loadColumnMetaDataMap(connection, material.getActualTableNames());
Map<String, Collection<ColumnMetaData>> columnMetaDataMap = loadColumnMetaDataMap(connection, material.getActualTableNames(), material.getStorageType());
Map<String, Collection<IndexMetaData>> indexMetaDataMap = columnMetaDataMap.isEmpty() ? Collections.emptyMap() : loadIndexMetaData(connection, columnMetaDataMap.keySet());
for (Entry<String, Collection<ColumnMetaData>> entry : columnMetaDataMap.entrySet()) {
Collection<IndexMetaData> indexMetaDataList = indexMetaDataMap.getOrDefault(entry.getKey(), Collections.emptyList());
Expand All @@ -78,10 +79,10 @@ public Collection<SchemaMetaData> load(final MetaDataLoaderMaterial material) th
return Collections.singleton(new SchemaMetaData(material.getDefaultSchemaName(), tableMetaDataList));
}

private Map<String, Collection<ColumnMetaData>> loadColumnMetaDataMap(final Connection connection, final Collection<String> tables) throws SQLException {
private Map<String, Collection<ColumnMetaData>> loadColumnMetaDataMap(final Connection connection, final Collection<String> tables, final DatabaseType databaseType) throws SQLException {
Map<String, Collection<ColumnMetaData>> result = new HashMap<>();
try (PreparedStatement preparedStatement = connection.prepareStatement(getTableMetaDataSQL(tables))) {
Map<String, Integer> dataTypes = new DataTypeLoader().load(connection.getMetaData(), getType());
Map<String, Integer> dataTypes = new DataTypeLoader().load(connection.getMetaData(), databaseType);
Map<String, Collection<String>> tablePrimaryKeys = loadTablePrimaryKeys(connection, tables);
Map<String, Map<String, Boolean>> tableGenerated = loadTableGenerated(connection, tables);
preparedStatement.setString(1, connection.getCatalog());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.database.h2.type.H2DatabaseType;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -67,7 +66,8 @@ void assertLoadWithoutTables() throws SQLException {
"SELECT C.TABLE_NAME TABLE_NAME, C.COLUMN_NAME COLUMN_NAME, COALESCE(I.IS_GENERATED, FALSE) IS_GENERATED FROM INFORMATION_SCHEMA.COLUMNS C RIGHT JOIN"
+ " INFORMATION_SCHEMA.INDEXES I ON C.TABLE_NAME=I.TABLE_NAME WHERE C.TABLE_CATALOG=? AND C.TABLE_SCHEMA=?")
.executeQuery()).thenReturn(generatedInfo);
assertTableMetaDataMap(getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.emptyList(), dataSource, new H2DatabaseType(), "sharding_db")));
assertTableMetaDataMap(getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.emptyList(), dataSource, TypedSPILoader.getService(DatabaseType.class, "H2"),
"sharding_db")));
}

@Test
Expand All @@ -91,7 +91,8 @@ void assertLoadWithTables() throws SQLException {
"SELECT C.TABLE_NAME TABLE_NAME, C.COLUMN_NAME COLUMN_NAME, COALESCE(I.IS_GENERATED, FALSE) IS_GENERATED FROM INFORMATION_SCHEMA.COLUMNS C"
+ " RIGHT JOIN INFORMATION_SCHEMA.INDEXES I ON C.TABLE_NAME=I.TABLE_NAME WHERE C.TABLE_CATALOG=? AND C.TABLE_SCHEMA=? AND C.TABLE_NAME IN ('tbl')")
.executeQuery()).thenReturn(generatedInfo);
assertTableMetaDataMap(getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.singletonList("tbl"), dataSource, new H2DatabaseType(), "sharding_db")));
assertTableMetaDataMap(getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.singletonList("tbl"), dataSource, TypedSPILoader.getService(DatabaseType.class, "H2"),
"sharding_db")));
}

private DataSource mockDataSource() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.database.datatype.DataTypeLoader;
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.TableType;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;

import javax.sql.DataSource;
import java.sql.Connection;
Expand Down Expand Up @@ -68,7 +69,7 @@ public final class MySQLMetaDataLoader implements DialectMetaDataLoader {
@Override
public Collection<SchemaMetaData> load(final MetaDataLoaderMaterial material) throws SQLException {
Collection<TableMetaData> tableMetaDataList = new LinkedList<>();
Map<String, Collection<ColumnMetaData>> columnMetaDataMap = loadColumnMetaDataMap(material.getDataSource(), material.getActualTableNames());
Map<String, Collection<ColumnMetaData>> columnMetaDataMap = loadColumnMetaDataMap(material.getDataSource(), material.getActualTableNames(), material.getStorageType());
Collection<String> viewNames = loadViewNames(material.getDataSource(), columnMetaDataMap.keySet());
Map<String, Collection<IndexMetaData>> indexMetaDataMap = columnMetaDataMap.isEmpty() ? Collections.emptyMap() : loadIndexMetaData(material.getDataSource(), columnMetaDataMap.keySet());
Map<String, Collection<ConstraintMetaData>> constraintMetaDataMap =
Expand Down Expand Up @@ -126,12 +127,12 @@ private String getConstraintMetaDataSQL(final Collection<String> tableNames) {
return String.format(CONSTRAINT_META_DATA_SQL, tableNames.stream().map(each -> String.format("'%s'", each)).collect(Collectors.joining(",")));
}

private Map<String, Collection<ColumnMetaData>> loadColumnMetaDataMap(final DataSource dataSource, final Collection<String> tables) throws SQLException {
private Map<String, Collection<ColumnMetaData>> loadColumnMetaDataMap(final DataSource dataSource, final Collection<String> tables, final DatabaseType databaseType) throws SQLException {
Map<String, Collection<ColumnMetaData>> result = new HashMap<>();
try (
Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(getTableMetaDataSQL(tables))) {
Map<String, Integer> dataTypes = new DataTypeLoader().load(connection.getMetaData(), getType());
Map<String, Integer> dataTypes = new DataTypeLoader().load(connection.getMetaData(), databaseType);
String databaseName = "".equals(connection.getCatalog()) ? GlobalDataSourceRegistry.getInstance().getCachedDatabaseTables().get(tables.iterator().next()) : connection.getCatalog();
preparedStatement.setString(1, databaseName);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -57,7 +56,7 @@ void assertLoadWithoutTables() throws SQLException {
ResultSet indexResultSet = mockIndexMetaDataResultSet();
when(dataSource.getConnection().prepareStatement("SELECT TABLE_NAME, INDEX_NAME, NON_UNIQUE, COLUMN_NAME FROM information_schema.statistics "
+ "WHERE TABLE_SCHEMA=? and TABLE_NAME IN ('tbl') ORDER BY NON_UNIQUE, INDEX_NAME, SEQ_IN_INDEX").executeQuery()).thenReturn(indexResultSet);
assertTableMetaDataMap(dialectMetaDataLoader.load(new MetaDataLoaderMaterial(Collections.emptyList(), dataSource, new MySQLDatabaseType(), "sharding_db")));
assertTableMetaDataMap(dialectMetaDataLoader.load(new MetaDataLoaderMaterial(Collections.emptyList(), dataSource, TypedSPILoader.getService(DatabaseType.class, "Mysql"), "sharding_db")));
}

@Test
Expand All @@ -71,7 +70,8 @@ void assertLoadWithTables() throws SQLException {
when(dataSource.getConnection().prepareStatement("SELECT TABLE_NAME, INDEX_NAME, NON_UNIQUE, COLUMN_NAME FROM information_schema.statistics WHERE TABLE_SCHEMA=? and TABLE_NAME IN ('tbl') "
+ "ORDER BY NON_UNIQUE, INDEX_NAME, SEQ_IN_INDEX")
.executeQuery()).thenReturn(indexResultSet);
assertTableMetaDataMap(dialectMetaDataLoader.load(new MetaDataLoaderMaterial(Collections.singletonList("tbl"), dataSource, new MySQLDatabaseType(), "sharding_db")));
assertTableMetaDataMap(dialectMetaDataLoader.load(new MetaDataLoaderMaterial(Collections.singletonList("tbl"), dataSource, TypedSPILoader.getService(DatabaseType.class, "Mysql"),
"sharding_db")));
}

private DataSource mockDataSource() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Collection<SchemaMetaData> load(final MetaDataLoaderMaterial material) th
try (Connection connection = material.getDataSource().getConnection()) {
Collection<String> schemaNames = SchemaMetaDataLoader.loadSchemaNames(connection, TypedSPILoader.getService(DatabaseType.class, "openGauss"));
Map<String, Multimap<String, IndexMetaData>> schemaIndexMetaDataMap = loadIndexMetaDataMap(connection, schemaNames);
Map<String, Multimap<String, ColumnMetaData>> schemaColumnMetaDataMap = loadColumnMetaDataMap(connection, material.getActualTableNames(), schemaNames);
Map<String, Multimap<String, ColumnMetaData>> schemaColumnMetaDataMap = loadColumnMetaDataMap(connection, material.getActualTableNames(), schemaNames, material.getStorageType());
Collection<SchemaMetaData> result = new LinkedList<>();
for (String each : schemaNames) {
Multimap<String, IndexMetaData> tableIndexMetaDataMap = schemaIndexMetaDataMap.getOrDefault(each, LinkedHashMultimap.create());
Expand Down Expand Up @@ -123,10 +123,10 @@ private String getAdvanceIndexMetaDataSQL(final Collection<String> schemaNames)
}

private Map<String, Multimap<String, ColumnMetaData>> loadColumnMetaDataMap(final Connection connection, final Collection<String> tables,
final Collection<String> schemaNames) throws SQLException {
final Collection<String> schemaNames, final DatabaseType databaseType) throws SQLException {
Map<String, Multimap<String, ColumnMetaData>> result = new LinkedHashMap<>();
try (PreparedStatement preparedStatement = connection.prepareStatement(getColumnMetaDataSQL(schemaNames, tables)); ResultSet resultSet = preparedStatement.executeQuery()) {
Map<String, Integer> dataTypes = new DataTypeLoader().load(connection.getMetaData(), getType());
Map<String, Integer> dataTypes = new DataTypeLoader().load(connection.getMetaData(), databaseType);
Collection<String> primaryKeys = loadPrimaryKeys(connection, schemaNames);
while (resultSet.next()) {
String tableName = resultSet.getString("table_name");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.database.opengauss.type.OpenGaussDatabaseType;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -78,7 +77,8 @@ void assertLoadWithoutTables() throws SQLException {
when(dataSource.getConnection().prepareStatement(BASIC_INDEX_META_DATA_SQL).executeQuery()).thenReturn(indexResultSet);
ResultSet advanceIndexResultSet = mockAdvanceIndexMetaDataResultSet();
when(dataSource.getConnection().prepareStatement(ADVANCE_INDEX_META_DATA_SQL).executeQuery()).thenReturn(advanceIndexResultSet);
assertTableMetaDataMap(getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.emptyList(), dataSource, new OpenGaussDatabaseType(), "sharding_db")));
assertTableMetaDataMap(getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.emptyList(), dataSource,
TypedSPILoader.getService(DatabaseType.class, "openGauss"), "sharding_db")));
}

private ResultSet mockSchemaMetaDataResultSet() throws SQLException {
Expand All @@ -101,7 +101,8 @@ void assertLoadWithTables() throws SQLException {
when(dataSource.getConnection().prepareStatement(BASIC_INDEX_META_DATA_SQL).executeQuery()).thenReturn(indexResultSet);
ResultSet advanceIndexResultSet = mockAdvanceIndexMetaDataResultSet();
when(dataSource.getConnection().prepareStatement(ADVANCE_INDEX_META_DATA_SQL).executeQuery()).thenReturn(advanceIndexResultSet);
assertTableMetaDataMap(getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.singletonList("tbl"), dataSource, new OpenGaussDatabaseType(), "sharding_db")));
assertTableMetaDataMap(getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.singletonList("tbl"), dataSource,
TypedSPILoader.getService(DatabaseType.class, "openGauss"), "sharding_db")));
}

private DataSource mockDataSource() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,18 @@ public final class OracleMetaDataLoader implements DialectMetaDataLoader {
public Collection<SchemaMetaData> load(final MetaDataLoaderMaterial material) throws SQLException {
Collection<TableMetaData> tableMetaDataList = new LinkedList<>();
try (Connection connection = new MetaDataLoaderConnection(TypedSPILoader.getService(DatabaseType.class, "Oracle"), material.getDataSource().getConnection())) {
tableMetaDataList.addAll(getTableMetaDataList(connection, connection.getSchema(), material.getActualTableNames()));
tableMetaDataList.addAll(getTableMetaDataList(connection, connection.getSchema(), material.getActualTableNames(), material.getStorageType()));
}
return Collections.singletonList(new SchemaMetaData(material.getDefaultSchemaName(), tableMetaDataList));
}

private Collection<TableMetaData> getTableMetaDataList(final Connection connection, final String schema, final Collection<String> tableNames) throws SQLException {
private Collection<TableMetaData> getTableMetaDataList(final Connection connection, final String schema, final Collection<String> tableNames, final DatabaseType databaseType) throws SQLException {
Collection<String> viewNames = new LinkedList<>();
Map<String, Collection<ColumnMetaData>> columnMetaDataMap = new HashMap<>(tableNames.size(), 1F);
Map<String, Collection<IndexMetaData>> indexMetaDataMap = new HashMap<>(tableNames.size(), 1F);
for (List<String> each : Lists.partition(new ArrayList<>(tableNames), MAX_EXPRESSION_SIZE)) {
viewNames.addAll(loadViewNames(connection, each, schema));
columnMetaDataMap.putAll(loadColumnMetaDataMap(connection, each, schema));
columnMetaDataMap.putAll(loadColumnMetaDataMap(connection, each, schema, databaseType));
indexMetaDataMap.putAll(loadIndexMetaData(connection, each, schema));
}
Collection<TableMetaData> result = new LinkedList<>();
Expand Down Expand Up @@ -123,10 +123,11 @@ private String getViewMetaDataSQL(final Collection<String> tableNames) {
return String.format(VIEW_META_DATA_SQL, tableNames.stream().map(each -> String.format("'%s'", each)).collect(Collectors.joining(",")));
}

private Map<String, Collection<ColumnMetaData>> loadColumnMetaDataMap(final Connection connection, final Collection<String> tables, final String schema) throws SQLException {
private Map<String, Collection<ColumnMetaData>> loadColumnMetaDataMap(final Connection connection, final Collection<String> tables, final String schema,
final DatabaseType databaseType) throws SQLException {
Map<String, Collection<ColumnMetaData>> result = new HashMap<>(tables.size(), 1F);
try (PreparedStatement preparedStatement = connection.prepareStatement(getTableMetaDataSQL(tables, connection.getMetaData()))) {
Map<String, Integer> dataTypes = new DataTypeLoader().load(connection.getMetaData(), getType());
Map<String, Integer> dataTypes = new DataTypeLoader().load(connection.getMetaData(), databaseType);
Map<String, Collection<String>> tablePrimaryKeys = loadTablePrimaryKeys(connection, tables);
preparedStatement.setString(1, schema);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
Expand Down