Skip to content

Commit

Permalink
Revise pr#30503 for code style (apache#30573)
Browse files Browse the repository at this point in the history
* Revise pr#30503 for code style

* Revise pr#30503 for code style

* Remove useless schema in SQLFederationResultSet

* fix spotless error
  • Loading branch information
strongduanmu authored Mar 21, 2024
1 parent 9a1d5f9 commit dbed08d
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
import org.apache.shardingsphere.infra.binder.context.segment.select.projection.Projection;
import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.driver.jdbc.type.util.ResultSetUtils;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.sqlfederation.spi.SQLFederationColumnTypeConverter;
import org.apache.shardingsphere.sqlfederation.resultset.converter.SQLFederationColumnTypeConverter;

import java.io.InputStream;
import java.io.Reader;
Expand Down Expand Up @@ -71,7 +72,7 @@ public final class SQLFederationResultSet extends AbstractUnsupportedOperationRe

private final SQLFederationResultSetMetaData resultSetMetaData;

private final SQLFederationColumnTypeConverter sqlFederationColumnTypeConverter;
private final SQLFederationColumnTypeConverter columnTypeConverter;

private Object[] currentRows;

Expand All @@ -82,11 +83,12 @@ public final class SQLFederationResultSet extends AbstractUnsupportedOperationRe
public SQLFederationResultSet(final Enumerator<Object> enumerator, final ShardingSphereSchema schema, final Schema sqlFederationSchema,
final SelectStatementContext selectStatementContext, final RelDataType resultColumnType) {
this.enumerator = enumerator;
this.sqlFederationColumnTypeConverter = DatabaseTypedSPILoader.getService(SQLFederationColumnTypeConverter.class, selectStatementContext.getDatabaseType());
DatabaseType databaseType = selectStatementContext.getDatabaseType().getTrunkDatabaseType().orElse(selectStatementContext.getDatabaseType());
columnTypeConverter = DatabaseTypedSPILoader.getService(SQLFederationColumnTypeConverter.class, databaseType);
columnLabelAndIndexes = new CaseInsensitiveMap<>(selectStatementContext.getProjectionsContext().getExpandProjections().size(), 1F);
Map<Integer, String> indexAndColumnLabels = new CaseInsensitiveMap<>(selectStatementContext.getProjectionsContext().getExpandProjections().size(), 1F);
handleColumnLabelAndIndex(columnLabelAndIndexes, indexAndColumnLabels, selectStatementContext);
resultSetMetaData = new SQLFederationResultSetMetaData(schema, sqlFederationSchema, selectStatementContext, resultColumnType, indexAndColumnLabels);
resultSetMetaData = new SQLFederationResultSetMetaData(sqlFederationSchema, selectStatementContext, resultColumnType, indexAndColumnLabels, columnTypeConverter);
}

private void handleColumnLabelAndIndex(final Map<String, Integer> columnLabelAndIndexes, final Map<Integer, String> indexAndColumnLabels, final SelectStatementContext selectStatementContext) {
Expand Down Expand Up @@ -469,7 +471,7 @@ private Object getValue(final int columnIndex, final Class<?> type) throws SQLEx
ShardingSpherePreconditions.checkState(!INVALID_FEDERATION_TYPES.contains(type), () -> new SQLFeatureNotSupportedException(String.format("Get value from `%s`", type.getName())));
Object result = currentRows[columnIndex - 1];
wasNull = null == result;
return sqlFederationColumnTypeConverter.convertColumnValue(result).orElse(result);
return columnTypeConverter.convertColumnValue(result);
}

private Object getCalendarValue(final int columnIndex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ColumnProjection;
import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.sqlfederation.spi.SQLFederationColumnTypeConverter;
import org.apache.shardingsphere.sqlfederation.resultset.converter.SQLFederationColumnTypeConverter;

import java.sql.ResultSetMetaData;
import java.util.List;
Expand All @@ -41,8 +39,6 @@
*/
public final class SQLFederationResultSetMetaData extends WrapperAdapter implements ResultSetMetaData {

private final ShardingSphereSchema schema;

private final Schema sqlFederationSchema;

private final RelDataTypeFactory relDataTypeFactory;
Expand All @@ -53,17 +49,16 @@ public final class SQLFederationResultSetMetaData extends WrapperAdapter impleme

private final Map<Integer, String> indexAndColumnLabels;

private final SQLFederationColumnTypeConverter sqlFederationColumnTypeConverter;
private final SQLFederationColumnTypeConverter columnTypeConverter;

public SQLFederationResultSetMetaData(final ShardingSphereSchema schema, final Schema sqlFederationSchema,
final SelectStatementContext selectStatementContext, final RelDataType resultColumnType, final Map<Integer, String> indexAndColumnLabels) {
this.schema = schema;
public SQLFederationResultSetMetaData(final Schema sqlFederationSchema, final SelectStatementContext selectStatementContext, final RelDataType resultColumnType,
final Map<Integer, String> indexAndColumnLabels, final SQLFederationColumnTypeConverter columnTypeConverter) {
this.sqlFederationSchema = sqlFederationSchema;
this.relDataTypeFactory = new JavaTypeFactoryImpl();
this.selectStatementContext = selectStatementContext;
this.resultColumnType = resultColumnType;
this.indexAndColumnLabels = indexAndColumnLabels;
this.sqlFederationColumnTypeConverter = DatabaseTypedSPILoader.getService(SQLFederationColumnTypeConverter.class, selectStatementContext.getDatabaseType());
this.columnTypeConverter = columnTypeConverter;
}

@Override
Expand Down Expand Up @@ -154,14 +149,13 @@ public String getCatalogName(final int column) {
@Override
public int getColumnType(final int column) {
int jdbcType = resultColumnType.getFieldList().get(column - 1).getType().getSqlTypeName().getJdbcOrdinal();
return sqlFederationColumnTypeConverter.convertColumnType(jdbcType).orElse(jdbcType);
return columnTypeConverter.convertColumnType(jdbcType);
}

@Override
public String getColumnTypeName(final int column) {
SqlTypeName originalSqlTypeName = resultColumnType.getFieldList().get(column - 1).getType().getSqlTypeName();
SqlTypeName convertSqlTypeName =
SqlTypeName.getNameForJdbcType(sqlFederationColumnTypeConverter.convertColumnType(originalSqlTypeName.getJdbcOrdinal()).orElse(originalSqlTypeName.getJdbcOrdinal()));
SqlTypeName convertSqlTypeName = SqlTypeName.getNameForJdbcType(columnTypeConverter.convertColumnType(originalSqlTypeName.getJdbcOrdinal()));
return null == convertSqlTypeName ? originalSqlTypeName.getName() : convertSqlTypeName.getName();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,32 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sqlfederation.spi;
package org.apache.shardingsphere.sqlfederation.resultset.converter;

import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPI;

import java.util.Optional;

/**
* SQL federation column type convert.
* SQL federation column type converter.
*/
public interface SQLFederationColumnTypeConverter extends DatabaseTypedSPI {

/**
* Transforming the column results of a federated query.
* Convert column value.
*
* @param columnValue column value
* @return convert column value result
* @return converted column value
*/
Optional<Object> convertColumnValue(Object columnValue);
default Object convertColumnValue(Object columnValue) {
return columnValue;
}

/**
* Converting the column types of a federated query.
* Convert column type.
*
* @param columnType column type
* @return convert column type result
* @return converted column type
*/
Optional<Integer> convertColumnType(Integer columnType);
default int convertColumnType(int columnType) {
return columnType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,30 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sqlfederation.resultset.converter;
package org.apache.shardingsphere.sqlfederation.resultset.converter.impl;

import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.shardingsphere.sqlfederation.spi.SQLFederationColumnTypeConverter;

import java.util.Optional;
import org.apache.shardingsphere.sqlfederation.resultset.converter.SQLFederationColumnTypeConverter;

/**
* MySQL column type converter.
*/
public final class MySQLColumnTypeConverter implements SQLFederationColumnTypeConverter {

@Override
public Optional<Object> convertColumnValue(final Object columnValue) {
public Object convertColumnValue(final Object columnValue) {
if (columnValue instanceof Boolean) {
return Optional.of((Boolean) columnValue ? 1 : 0);
return (Boolean) columnValue ? 1 : 0;
}
return Optional.empty();
return columnValue;
}

@Override
public Optional<Integer> convertColumnType(final Integer columnType) {
public int convertColumnType(final int columnType) {
if (SqlTypeName.BOOLEAN.getJdbcOrdinal() == columnType) {
return Optional.of(SqlTypeName.BIGINT.getJdbcOrdinal());
return SqlTypeName.BIGINT.getJdbcOrdinal();
}
return Optional.empty();
return columnType;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,15 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sqlfederation.resultset.converter;
package org.apache.shardingsphere.sqlfederation.resultset.converter.impl;

import org.apache.shardingsphere.sqlfederation.spi.SQLFederationColumnTypeConverter;

import java.util.Optional;
import org.apache.shardingsphere.sqlfederation.resultset.converter.SQLFederationColumnTypeConverter;

/**
* OpenGauss column type converter.
*/
public final class OpenGaussColumnTypeConverter implements SQLFederationColumnTypeConverter {

@Override
public Optional<Object> convertColumnValue(final Object columnValue) {
return Optional.empty();
}

@Override
public Optional<Integer> convertColumnType(final Integer columnType) {
return Optional.empty();
}

@Override
public String getDatabaseType() {
return "openGauss";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,15 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sqlfederation.resultset.converter;
package org.apache.shardingsphere.sqlfederation.resultset.converter.impl;

import org.apache.shardingsphere.sqlfederation.spi.SQLFederationColumnTypeConverter;

import java.util.Optional;
import org.apache.shardingsphere.sqlfederation.resultset.converter.SQLFederationColumnTypeConverter;

/**
* Oracle column type converter.
*/
public final class OracleColumnTypeConverter implements SQLFederationColumnTypeConverter {

@Override
public Optional<Object> convertColumnValue(final Object columnValue) {
return Optional.empty();
}

@Override
public Optional<Integer> convertColumnType(final Integer columnType) {
return Optional.empty();
}

@Override
public String getDatabaseType() {
return "Oracle";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,15 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sqlfederation.resultset.converter;
package org.apache.shardingsphere.sqlfederation.resultset.converter.impl;

import org.apache.shardingsphere.sqlfederation.spi.SQLFederationColumnTypeConverter;

import java.util.Optional;
import org.apache.shardingsphere.sqlfederation.resultset.converter.SQLFederationColumnTypeConverter;

/**
* PostgreSQL column type converter.
*/
public final class PostgreSQLColumnTypeConverter implements SQLFederationColumnTypeConverter {

@Override
public Optional<Object> convertColumnValue(final Object columnValue) {
return Optional.empty();
}

@Override
public Optional<Integer> convertColumnType(final Integer columnType) {
return Optional.empty();
}

@Override
public String getDatabaseType() {
return "PostgreSQL";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# limitations under the License.
#

org.apache.shardingsphere.sqlfederation.resultset.converter.MySQLColumnTypeConverter
org.apache.shardingsphere.sqlfederation.resultset.converter.OracleColumnTypeConverter
org.apache.shardingsphere.sqlfederation.resultset.converter.OpenGaussColumnTypeConverter
org.apache.shardingsphere.sqlfederation.resultset.converter.PostgreSQLColumnTypeConverter
org.apache.shardingsphere.sqlfederation.resultset.converter.impl.MySQLColumnTypeConverter
org.apache.shardingsphere.sqlfederation.resultset.converter.impl.OracleColumnTypeConverter
org.apache.shardingsphere.sqlfederation.resultset.converter.impl.OpenGaussColumnTypeConverter
org.apache.shardingsphere.sqlfederation.resultset.converter.impl.PostgreSQLColumnTypeConverter
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,10 @@

package org.apache.shardingsphere.sqlfederation.resultset.fixture.converter;

import org.apache.shardingsphere.sqlfederation.spi.SQLFederationColumnTypeConverter;

import java.util.Optional;
import org.apache.shardingsphere.sqlfederation.resultset.converter.SQLFederationColumnTypeConverter;

public final class SQLFederationColumnTypeConverterFixture implements SQLFederationColumnTypeConverter {

@Override
public Optional<Object> convertColumnValue(final Object columnValue) {
return Optional.empty();
}

@Override
public Optional<Integer> convertColumnType(final Integer columnType) {
return Optional.empty();
}

@Override
public String getDatabaseType() {
return "FIXTURE";
Expand Down

0 comments on commit dbed08d

Please sign in to comment.