From f3a97521b62a36367b7e67e118ef822610ca10c4 Mon Sep 17 00:00:00 2001 From: Zhengqiang Duan Date: Fri, 30 Aug 2024 15:06:23 +0800 Subject: [PATCH] Fix sql federation unknown type exception caused by calcite wrong result type with bigint (#32730) --- .../resultset/SQLFederationResultSetMetaData.java | 10 +++++++++- .../sqlfederation/executor/utils/EnumeratorUtils.java | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/SQLFederationResultSetMetaData.java b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/SQLFederationResultSetMetaData.java index 0330f32d4dd9c..2a3a7af94bee2 100644 --- a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/SQLFederationResultSetMetaData.java +++ b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/SQLFederationResultSetMetaData.java @@ -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; @@ -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; @@ -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); } diff --git a/kernel/sql-federation/executor/src/main/java/org/apache/shardingsphere/sqlfederation/executor/utils/EnumeratorUtils.java b/kernel/sql-federation/executor/src/main/java/org/apache/shardingsphere/sqlfederation/executor/utils/EnumeratorUtils.java index 09741eff43619..dd60ff2c7cf35 100644 --- a/kernel/sql-federation/executor/src/main/java/org/apache/shardingsphere/sqlfederation/executor/utils/EnumeratorUtils.java +++ b/kernel/sql-federation/executor/src/main/java/org/apache/shardingsphere/sqlfederation/executor/utils/EnumeratorUtils.java @@ -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; @@ -80,7 +81,7 @@ public static Object[] convertToTargetType(final Map> columnTy private static Object convertValue(final Object[] rows, final Map> 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]; } }