Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -507,7 +507,7 @@ public void registerOutParameter(int parameterIndex, int sqlType) throws SQLExce
this.parameterHandler.setParameter(
parameterIndex,
null,
BigQueryJdbcTypeMappings.getJavaType(sqlType),
BigQueryTypeRegistry.toJavaClass(sqlType),
BigQueryParameterHandler.BigQueryStatementParameterType.OUT,
-1);
}
Expand All @@ -519,7 +519,7 @@ public void registerOutParameter(String parameterName, int sqlType) throws SQLEx
this.parameterHandler.setParameter(
parameterName,
null,
BigQueryJdbcTypeMappings.getJavaType(sqlType),
BigQueryTypeRegistry.toJavaClass(sqlType),
BigQueryParameterHandler.BigQueryStatementParameterType.OUT,
-1);
}
Expand All @@ -540,7 +540,7 @@ public void registerOutParameter(int parameterIndex, int sqlType, int scale) thr
this.parameterHandler.setParameter(
parameterIndex,
null,
BigQueryJdbcTypeMappings.getJavaType(sqlType),
BigQueryTypeRegistry.toJavaClass(sqlType),
BigQueryParameterHandler.BigQueryStatementParameterType.OUT,
scale);
}
Expand Down Expand Up @@ -572,7 +572,7 @@ public void registerOutParameter(String parameterName, int sqlType, int scale)
this.parameterHandler.setParameter(
parameterName,
null,
BigQueryJdbcTypeMappings.getJavaType(sqlType),
BigQueryTypeRegistry.toJavaClass(sqlType),
BigQueryParameterHandler.BigQueryStatementParameterType.OUT,
scale);
}
Expand Down Expand Up @@ -778,7 +778,7 @@ public void setNString(String parameterName, String value) throws SQLException {
@Override
public void setNull(String parameterName, int sqlType) throws SQLException {
checkClosed();
Class<?> javaType = BigQueryJdbcTypeMappings.getJavaType(sqlType);
Class<?> javaType = BigQueryTypeRegistry.toJavaClass(sqlType);
if (javaType == null) {
javaType = String.class;
}
Comment thread
Neenu1995 marked this conversation as resolved.
Outdated
Expand Down Expand Up @@ -812,8 +812,8 @@ public void setObject(String parameterName, Object value, int targetSqlType) thr
this.parameterHandler.setParameter(
parameterName, value, value.getClass(), BigQueryStatementParameterType.IN, 0);
StandardSQLTypeName sqlType = this.parameterHandler.getSqlType(parameterName);
if (BigQueryJdbcTypeMappings.standardSQLToJavaSqlTypesMapping.containsKey(sqlType)) {
int javaSqlType = BigQueryJdbcTypeMappings.standardSQLToJavaSqlTypesMapping.get(sqlType);
if (sqlType != null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this explicit check? I assume toJdbcType throws exception for null

int javaSqlType = BigQueryTypeRegistry.toJdbcType(sqlType);
if (javaSqlType != targetSqlType) {
throw new BigQueryJdbcSqlFeatureNotSupportedException(
String.format("Unsupported sql type:%s ", targetSqlType));
Expand All @@ -835,8 +835,8 @@ public void setObject(String parameterName, Object value, int targetSqlType, int
this.parameterHandler.setParameter(
parameterName, value, value.getClass(), BigQueryStatementParameterType.IN, scaleOrLength);
StandardSQLTypeName sqlType = this.parameterHandler.getSqlType(parameterName);
if (BigQueryJdbcTypeMappings.standardSQLToJavaSqlTypesMapping.containsKey(sqlType)) {
int javaSqlType = BigQueryJdbcTypeMappings.standardSQLToJavaSqlTypesMapping.get(sqlType);
if (sqlType != null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

int javaSqlType = BigQueryTypeRegistry.toJdbcType(sqlType);
if (javaSqlType != targetSqlType) {
throw new BigQueryJdbcSqlFeatureNotSupportedException(
String.format("Unsupported sql type:%s ", targetSqlType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import com.google.cloud.bigquery.TableDefinition;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.exception.BigQueryJdbcException;
import com.google.cloud.bigquery.jdbc.BigQueryJdbcTypeMappings.ColumnTypeInfo;
import com.google.cloud.bigquery.jdbc.BigQueryTypeRegistry.ColumnTypeInfo;
import com.google.cloud.bigquery.jdbc.utils.BigQueryJdbcVersionUtility;
import io.opentelemetry.context.Context;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -3976,7 +3976,7 @@ private ColumnTypeInfo getColumnTypeInfoForSqlType(StandardSQLTypeName bqType) {
LOG.warning("Null BigQuery type encountered. Mapping to STRING.");
return new ColumnTypeInfo(Types.NVARCHAR, "STRING", null, null, null);
}
ColumnTypeInfo info = BigQueryJdbcTypeMappings.STANDARD_TYPE_INFO.get(bqType);
ColumnTypeInfo info = BigQueryTypeRegistry.getColumnTypeInfo(bqType);
if (info == null) {
LOG.warning("Unknown BigQuery type encountered: " + bqType.name() + ". Mapping to STRING.");
return new ColumnTypeInfo(Types.NVARCHAR, "STRING", null, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void setParameter(int parameterIndex, Object value, Class type)
parameter.setIndex(parameterIndex);
parameter.setValue(value);
parameter.setType(type);
parameter.setSqlType(BigQueryJdbcTypeMappings.classToType(type));
parameter.setSqlType(BigQueryTypeRegistry.toBigQueryType(type));
parameter.setParamName("");
parameter.setParamType(BigQueryStatementParameterType.UNSPECIFIED);
parameter.setScale(-1);
Expand Down Expand Up @@ -208,7 +208,7 @@ void setParameter(
}
parameter.setValue(value);
parameter.setType(type);
parameter.setSqlType(BigQueryJdbcTypeMappings.classToType(type));
parameter.setSqlType(BigQueryTypeRegistry.toBigQueryType(type));
parameter.setParamName(paramName);
parameter.setParamType(paramType);
parameter.setScale(scale);
Expand Down Expand Up @@ -243,7 +243,7 @@ void setParameter(
parameter.setIndex(parameterIndex);
parameter.setValue(value);
parameter.setType(type);
parameter.setSqlType(BigQueryJdbcTypeMappings.classToType(type));
parameter.setSqlType(BigQueryTypeRegistry.toBigQueryType(type));
parameter.setParamName("");
parameter.setParamType(paramType);
parameter.setScale(scale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public int getPrecision(int param) throws SQLException {
if (sqlType == null) {
return 0;
}
BigQueryJdbcTypeMappings.ColumnTypeInfo typeInfo =
BigQueryJdbcTypeMappings.STANDARD_TYPE_INFO.get(sqlType);
BigQueryTypeRegistry.ColumnTypeInfo typeInfo = BigQueryTypeRegistry.getColumnTypeInfo(sqlType);
if (typeInfo != null && typeInfo.columnSize != null) {
return typeInfo.columnSize;
}
Expand All @@ -84,8 +83,7 @@ public int getScale(int param) throws SQLException {
if (sqlType == null) {
return 0;
}
BigQueryJdbcTypeMappings.ColumnTypeInfo typeInfo =
BigQueryJdbcTypeMappings.STANDARD_TYPE_INFO.get(sqlType);
BigQueryTypeRegistry.ColumnTypeInfo typeInfo = BigQueryTypeRegistry.getColumnTypeInfo(sqlType);
if (typeInfo != null && typeInfo.decimalDigits != null) {
return typeInfo.decimalDigits;
}
Expand All @@ -99,7 +97,7 @@ public int getParameterType(int param) throws SQLException {
if (sqlType == null) {
return Types.OTHER;
}
Integer jdbcType = BigQueryJdbcTypeMappings.standardSQLToJavaSqlTypesMapping.get(sqlType);
Integer jdbcType = BigQueryTypeRegistry.toJdbcType(sqlType);
if (jdbcType != null) {
return jdbcType;
Comment thread
Neenu1995 marked this conversation as resolved.
Outdated
}
Comment thread
Neenu1995 marked this conversation as resolved.
Outdated
Expand All @@ -118,7 +116,7 @@ public String getParameterClassName(int param) throws SQLException {
checkValidIndex(param);
StandardSQLTypeName sqlType = getStandardSQLTypeName(param);
if (sqlType != null) {
Class<?> clazz = BigQueryJdbcTypeMappings.standardSQLToJavaTypeMapping.get(sqlType);
Class<?> clazz = BigQueryTypeRegistry.toJavaClass(BigQueryTypeRegistry.toJdbcType(sqlType));
Comment thread
Neenu1995 marked this conversation as resolved.
Outdated
if (clazz != null) {
return clazz.getName();
}
Expand Down Expand Up @@ -162,12 +160,7 @@ private StandardSQLTypeName getStandardSQLTypeName(int param) {
if (javaType == null) {
return null;
}
try {
return BigQueryJdbcTypeMappings.classToType(javaType);
} catch (SQLException ignored) {
// fall back to default
return null;
}
return BigQueryTypeRegistry.toBigQueryType(javaType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void clearParameters() {
@Override
public void setNull(int parameterIndex, int sqlType) throws SQLException {
checkClosed();
Class<?> javaType = BigQueryJdbcTypeMappings.getJavaType(sqlType);
Class<?> javaType = BigQueryTypeRegistry.toJavaClass(sqlType);
this.parameterHandler.setParameter(parameterIndex, null, javaType);
}

Expand Down Expand Up @@ -248,7 +248,7 @@ public void setObject(int parameterIndex, Object value, int targetSqlType) throw
if (setTemporalObject(parameterIndex, value)) {
return;
}
Class<?> javaType = BigQueryJdbcTypeMappings.getJavaType(targetSqlType);
Class<?> javaType = BigQueryTypeRegistry.toJavaClass(targetSqlType);
this.parameterHandler.setParameter(parameterIndex, value, javaType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ public int getPrecision(int column) {
return precision.intValue();
}
StandardSQLTypeName type = getStandardSQLTypeName(column);
BigQueryJdbcTypeMappings.ColumnTypeInfo typeInfo =
BigQueryJdbcTypeMappings.STANDARD_TYPE_INFO.get(type);
BigQueryTypeRegistry.ColumnTypeInfo typeInfo = BigQueryTypeRegistry.getColumnTypeInfo(type);
if (typeInfo != null && typeInfo.columnSize != null) {
return typeInfo.columnSize;
}
Expand All @@ -154,8 +153,7 @@ public int getScale(int column) {
return scale.intValue();
}
StandardSQLTypeName type = getStandardSQLTypeName(column);
BigQueryJdbcTypeMappings.ColumnTypeInfo typeInfo =
BigQueryJdbcTypeMappings.STANDARD_TYPE_INFO.get(type);
BigQueryTypeRegistry.ColumnTypeInfo typeInfo = BigQueryTypeRegistry.getColumnTypeInfo(type);
if (typeInfo != null && typeInfo.decimalDigits != null) {
return typeInfo.decimalDigits;
}
Expand Down Expand Up @@ -189,8 +187,7 @@ private StandardSQLTypeName getStandardSQLTypeName(int column) {

@Override
public int getColumnType(int column) {
return BigQueryJdbcTypeMappings.standardSQLToJavaSqlTypesMapping.get(
getStandardSQLTypeName(column));
return BigQueryTypeRegistry.toJdbcType(getStandardSQLTypeName(column));
}

@Override
Expand Down Expand Up @@ -219,8 +216,8 @@ public String getColumnClassName(int column) {
if (field.getMode() == Mode.REPEATED) {
return java.sql.Array.class.getName();
}
return BigQueryJdbcTypeMappings.standardSQLToJavaTypeMapping
.get(field.getType().getStandardType())
return BigQueryTypeRegistry.toJavaClass(
BigQueryTypeRegistry.toJdbcType(getStandardSQLTypeName(column)))
.getName();
Comment thread
Neenu1995 marked this conversation as resolved.
Outdated
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.cloud.bigquery.exception.BigQueryJdbcException;
import com.google.common.collect.ImmutableMap;
import java.math.BigDecimal;
import java.sql.Array;
import java.sql.Date;
Expand Down Expand Up @@ -361,6 +362,16 @@ public static StandardSQLTypeName toBigQueryType(Class<?> clazz) {
}

/** Returns the default Java target class for a given JDBC type constant. */
/** Returns the JDBC Type constant for a given BigQuery type. */
public static int toJdbcType(StandardSQLTypeName bqType) {
if (bqType == null) return java.sql.Types.OTHER;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please avoid using one-line if-return, it is more difficult to read compared to

if (){
  return;
}

int ordinal = bqType.ordinal();
if (ordinal >= DESCRIPTORS_BY_ORDINAL.length || DESCRIPTORS_BY_ORDINAL[ordinal] == null) {
return java.sql.Types.OTHER;
}
return DESCRIPTORS_BY_ORDINAL[ordinal].getJdbcType();
}

public static Class<?> toJavaClass(int jdbcType) {
Comment thread
Neenu1995 marked this conversation as resolved.
Outdated
TypeDescriptor<?> descriptor = DESCRIPTORS_BY_JDBC_TYPE.get(jdbcType);
if (descriptor != null) {
Expand Down Expand Up @@ -431,4 +442,68 @@ private static TypeDescriptor<?> getDescriptorForClass(Class<?> clazz) {
}
return null;
}

static class ColumnTypeInfo {
final int jdbcType;
final String typeName;
final Integer columnSize;
final Integer decimalDigits;
final Integer numPrecRadix;

ColumnTypeInfo(
int jdbcType,
String typeName,
Integer columnSize,
Integer decimalDigits,
Integer numPrecRadix) {
this.jdbcType = jdbcType;
this.typeName = typeName;
this.columnSize = columnSize;
this.decimalDigits = decimalDigits;
this.numPrecRadix = numPrecRadix;
}
}

private static final Map<StandardSQLTypeName, ColumnTypeInfo> STANDARD_TYPE_INFO =
ImmutableMap.<StandardSQLTypeName, ColumnTypeInfo>builder()
.put(StandardSQLTypeName.INT64, new ColumnTypeInfo(Types.BIGINT, "INT64", 19, 0, 10))
.put(StandardSQLTypeName.BOOL, new ColumnTypeInfo(Types.BOOLEAN, "BOOL", 1, null, null))
.put(
StandardSQLTypeName.FLOAT64,
new ColumnTypeInfo(Types.DOUBLE, "FLOAT64", 15, null, 10))
.put(StandardSQLTypeName.NUMERIC, new ColumnTypeInfo(Types.NUMERIC, "NUMERIC", 38, 9, 10))
.put(
StandardSQLTypeName.BIGNUMERIC,
new ColumnTypeInfo(Types.NUMERIC, "BIGNUMERIC", 77, 38, 10))
.put(
StandardSQLTypeName.STRING,
new ColumnTypeInfo(Types.NVARCHAR, "STRING", null, null, null))
.put(
StandardSQLTypeName.TIMESTAMP,
new ColumnTypeInfo(Types.TIMESTAMP, "TIMESTAMP", 26, 6, null))
.put(
StandardSQLTypeName.DATETIME,
new ColumnTypeInfo(Types.TIMESTAMP, "DATETIME", 26, 6, null))
.put(StandardSQLTypeName.DATE, new ColumnTypeInfo(Types.DATE, "DATE", 10, 0, null))
.put(StandardSQLTypeName.TIME, new ColumnTypeInfo(Types.TIME, "TIME", 15, 6, null))
.put(
StandardSQLTypeName.GEOGRAPHY,
new ColumnTypeInfo(Types.OTHER, "GEOGRAPHY", null, null, null))
.put(StandardSQLTypeName.JSON, new ColumnTypeInfo(Types.OTHER, "JSON", null, null, null))
.put(
StandardSQLTypeName.INTERVAL,
new ColumnTypeInfo(Types.OTHER, "INTERVAL", null, null, null))
.put(
StandardSQLTypeName.RANGE, new ColumnTypeInfo(Types.OTHER, "RANGE", null, null, null))
.put(
StandardSQLTypeName.BYTES,
new ColumnTypeInfo(Types.VARBINARY, "BYTES", null, null, null))
.put(
StandardSQLTypeName.STRUCT,
new ColumnTypeInfo(Types.STRUCT, "STRUCT", null, null, null))
.build();

public static ColumnTypeInfo getColumnTypeInfo(StandardSQLTypeName bqType) {
return STANDARD_TYPE_INFO.get(bqType);
}
}
Loading