-
Notifications
You must be signed in to change notification settings - Fork 1.2k
refactor(bigquery-jdbc): migrate write path and metadata to TypeRegistry #14061
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
| } | ||
|
|
@@ -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); | ||
| } | ||
|
|
@@ -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); | ||
| } | ||
|
|
@@ -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); | ||
| } | ||
|
|
@@ -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; | ||
| } | ||
|
|
@@ -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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this explicit check? I assume |
||
| int javaSqlType = BigQueryTypeRegistry.toJdbcType(sqlType); | ||
| if (javaSqlType != targetSqlType) { | ||
| throw new BigQueryJdbcSqlFeatureNotSupportedException( | ||
| String.format("Unsupported sql type:%s ", targetSqlType)); | ||
|
|
@@ -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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please avoid using one-line |
||
| 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) { | ||
|
Neenu1995 marked this conversation as resolved.
Outdated
|
||
| TypeDescriptor<?> descriptor = DESCRIPTORS_BY_JDBC_TYPE.get(jdbcType); | ||
| if (descriptor != null) { | ||
|
|
@@ -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); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.