Skip to content

Commit

Permalink
Fix sql federation unknown type exception caused by calcite wrong res…
Browse files Browse the repository at this point in the history
…ult type with bigint (#32730)
  • Loading branch information
strongduanmu authored Aug 30, 2024
1 parent a97f022 commit f3a9752
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

package org.apache.shardingsphere.sqlfederation.resultset;

import org.apache.calcite.avatica.SqlType;
import org.apache.calcite.jdbc.JavaTypeFactoryImpl;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rel.type.RelDataTypeFactoryImpl.JavaType;
import org.apache.calcite.schema.Schema;
import org.apache.calcite.schema.Table;
import org.apache.calcite.sql.type.SqlTypeName;
Expand All @@ -29,6 +31,7 @@
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.sqlfederation.resultset.converter.SQLFederationColumnTypeConverter;

import java.math.BigInteger;
import java.sql.ResultSetMetaData;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -148,7 +151,12 @@ public String getCatalogName(final int column) {

@Override
public int getColumnType(final int column) {
int jdbcType = resultColumnType.getFieldList().get(column - 1).getType().getSqlTypeName().getJdbcOrdinal();
RelDataType relDataType = resultColumnType.getFieldList().get(column - 1).getType();
// TODO remove this logic when calcite supports BigInteger type
if (relDataType instanceof JavaType && BigInteger.class.isAssignableFrom(((JavaType) relDataType).getJavaClass())) {
return SqlType.BIGINT.id;
}
int jdbcType = relDataType.getSqlTypeName().getJdbcOrdinal();
return columnTypeConverter.convertColumnType(jdbcType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.exception.kernel.data.UnsupportedDataTypeConversionException;
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.ShardingSphereColumn;
import org.apache.shardingsphere.sqlfederation.optimizer.metadata.util.SQLFederationDataTypeUtils;
Expand Down Expand Up @@ -80,7 +81,7 @@ public static Object[] convertToTargetType(final Map<Integer, Class<?>> columnTy
private static Object convertValue(final Object[] rows, final Map<Integer, Class<?>> columnTypes, final int index) {
try {
return ResultSetUtils.convertValue(rows[index], columnTypes.get(index));
} catch (final SQLFeatureNotSupportedException ex) {
} catch (final SQLFeatureNotSupportedException | UnsupportedDataTypeConversionException ex) {
return rows[index];
}
}
Expand Down

0 comments on commit f3a9752

Please sign in to comment.