Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -80,6 +80,8 @@ class BigQueryJdbcTypeMappings {
ImmutableMap.ofEntries(
entry(Types.BIGINT, Long.class),
entry(Types.INTEGER, Integer.class),
entry(Types.SMALLINT, Short.class),
entry(Types.TINYINT, Byte.class),
entry(Types.BOOLEAN, Boolean.class),
entry(Types.DOUBLE, Double.class),
entry(Types.FLOAT, Float.class),
Expand All @@ -94,42 +96,37 @@ class BigQueryJdbcTypeMappings {
entry(Types.VARBINARY, byte[].class),
entry(Types.STRUCT, Struct.class),
entry(Types.BIT, Boolean.class),
entry(Types.ARRAY, Array.class));
entry(Types.ARRAY, Array.class),
entry(Types.NULL, String.class));

static StandardSQLTypeName classToType(Class type)
throws BigQueryJdbcSqlFeatureNotSupportedException {
if (Boolean.class.isAssignableFrom(type)) {
return StandardSQLTypeName.BOOL;
} else if (String.class.isAssignableFrom(type)) {
return StandardSQLTypeName.STRING;
} else if (String.class.isAssignableFrom(type)) {
return StandardSQLTypeName.GEOGRAPHY;
} else if (String.class.isAssignableFrom(type)) {
return StandardSQLTypeName.DATETIME;
} else if (Integer.class.isAssignableFrom(type)) {
return StandardSQLTypeName.INT64;
} else if (Long.class.isAssignableFrom(type)) {
return StandardSQLTypeName.INT64;
} else if (Short.class.isAssignableFrom(type)) {
return StandardSQLTypeName.INT64;
} else if (Double.class.isAssignableFrom(type)) {
return StandardSQLTypeName.FLOAT64;
} else if (Float.class.isAssignableFrom(type)) {
return StandardSQLTypeName.FLOAT64;
} else if (BigDecimal.class.isAssignableFrom(type)) {
return StandardSQLTypeName.NUMERIC;
} else if (BigDecimal.class.isAssignableFrom(type)) {
return StandardSQLTypeName.BIGNUMERIC;
} else if (Date.class.isAssignableFrom(type)) {
return StandardSQLTypeName.DATE;
} else if (Timestamp.class.isAssignableFrom(type)) {
return StandardSQLTypeName.TIMESTAMP;
} else if (Time.class.isAssignableFrom(type)) {
return StandardSQLTypeName.TIME;
} else if (String.class.isAssignableFrom(type)) {
return StandardSQLTypeName.JSON;
} else if (JsonObject.class.isAssignableFrom(type)) {
return StandardSQLTypeName.JSON;
} else if (Byte.class.isAssignableFrom(type)) {
return StandardSQLTypeName.BYTES;
return StandardSQLTypeName.INT64;
} else if (Array.class.isAssignableFrom(type)) {
return StandardSQLTypeName.ARRAY;
} else if (Struct.class.isAssignableFrom(type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.cloud.bigquery.exception.BigQueryJdbcException;
import com.google.cloud.bigquery.exception.BigQueryJdbcSqlFeatureNotSupportedException;
import java.math.BigInteger;
import java.sql.SQLException;
import java.util.ArrayList;

Expand Down Expand Up @@ -59,6 +60,16 @@ QueryJobConfiguration.Builder configureParameters(

Object parameterValue = getParameter(i);
StandardSQLTypeName sqlType = getSqlType(i);
if (parameterValue != null) {
if (sqlType == StandardSQLTypeName.INT64
&& (parameterValue instanceof Short
|| parameterValue instanceof Byte
|| parameterValue instanceof BigInteger)) {
parameterValue = ((Number) parameterValue).longValue();
} else if (sqlType == StandardSQLTypeName.FLOAT64 && parameterValue instanceof Float) {
parameterValue = ((Number) parameterValue).doubleValue();
}
}
LOG.finest(
"Parameter %s of type %s at index %s added to QueryJobConfiguration",
parameterValue, sqlType, i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.exception.BigQueryJdbcException;
import com.google.cloud.bigquery.exception.BigQueryJdbcRuntimeException;
import com.google.cloud.bigquery.exception.BigQueryJdbcSqlFeatureNotSupportedException;
import com.google.cloud.bigquery.storage.v1.BatchCommitWriteStreamsRequest;
import com.google.cloud.bigquery.storage.v1.BatchCommitWriteStreamsResponse;
import com.google.cloud.bigquery.storage.v1.BigQueryWriteClient;
Expand Down Expand Up @@ -55,6 +56,7 @@
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
Expand Down Expand Up @@ -114,8 +116,10 @@ public void clearParameters() {
}

@Override
public void setNull(int parameterIndex, int sqlType) {
// TODO(neenu): implement null case
public void setNull(int parameterIndex, int sqlType) throws SQLException {
checkClosed();
Class<?> javaType = BigQueryJdbcTypeMappings.getJavaType(sqlType);
this.parameterHandler.setParameter(parameterIndex, null, javaType);
}

@Override
Expand All @@ -131,8 +135,9 @@ public void setByte(int parameterIndex, byte x) throws SQLException {
}

@Override
public void setShort(int parameterIndex, short x) {
// TODO(neenu): implement Bytes conversion.
public void setShort(int parameterIndex, short x) throws SQLException {
checkClosed();
this.parameterHandler.setParameter(parameterIndex, x, Short.class);
}

@Override
Expand Down Expand Up @@ -172,8 +177,9 @@ public void setString(int parameterIndex, String x) throws SQLException {
}

@Override
public void setBytes(int parameterIndex, byte[] x) {
// TODO(neenu): implement Bytes conversion.
public void setBytes(int parameterIndex, byte[] x) throws SQLException {
checkClosed();
this.parameterHandler.setParameter(parameterIndex, x, byte[].class);
}

@Override
Expand Down Expand Up @@ -218,11 +224,24 @@ public void setBinaryStream(int parameterIndex, InputStream x, int length) {
}

@Override
public void setObject(int parameterIndex, Object x, int targetSqlType) {}
public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
checkClosed();
if (x == null) {
setNull(parameterIndex, targetSqlType);
return;
}
Class<?> javaType = BigQueryJdbcTypeMappings.getJavaType(targetSqlType);
this.parameterHandler.setParameter(parameterIndex, x, javaType);
}

@Override
public void setObject(int parameterIndex, Object x) {
// TODO :NOT IMPLEMENTED
public void setObject(int parameterIndex, Object x) throws SQLException {
checkClosed();
if (x == null) {
setNull(parameterIndex, Types.NULL);
return;
}
this.parameterHandler.setParameter(parameterIndex, x, x.getClass());
}

@Override
Expand Down Expand Up @@ -424,28 +443,30 @@ Boolean useWriteAPI() {
}

@Override
public void setCharacterStream(int parameterIndex, Reader reader, int length) {
// TODO :NOT IMPLEMENTED
public void setCharacterStream(int parameterIndex, Reader reader, int length)
throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setCharacterStream is not supported.");
}

@Override
public void setRef(int parameterIndex, Ref x) {
// TODO :NOT IMPLEMENTED
public void setRef(int parameterIndex, Ref x) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setRef is not supported.");
}

@Override
public void setBlob(int parameterIndex, Blob x) {
// TODO :NOT IMPLEMENTED
public void setBlob(int parameterIndex, Blob x) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setBlob is not supported.");
}

@Override
public void setClob(int parameterIndex, Clob x) {
// TODO :NOT IMPLEMENTED
public void setClob(int parameterIndex, Clob x) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setClob is not supported.");
}

@Override
public void setArray(int parameterIndex, Array x) {
// TODO(neenu) :IMPLEMENT ARRAY
public void setArray(int parameterIndex, Array x) throws SQLException {
checkClosed();
this.parameterHandler.setParameter(parameterIndex, x, Array.class);
}

@Override
Expand All @@ -470,13 +491,13 @@ public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) {
}

@Override
public void setNull(int parameterIndex, int sqlType, String typeName) {
// TODO :NOT IMPLEMENTED
public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException {
setNull(parameterIndex, sqlType);
}

@Override
public void setURL(int parameterIndex, URL x) {
// TODO :NOT IMPLEMENTED
public void setURL(int parameterIndex, URL x) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setURL is not supported.");
}

@Override
Expand All @@ -486,97 +507,107 @@ public ParameterMetaData getParameterMetaData() {
}

@Override
public void setRowId(int parameterIndex, RowId x) {
// TODO :NOT IMPLEMENTED
public void setRowId(int parameterIndex, RowId x) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setRowId is not supported.");
}

@Override
public void setNString(int parameterIndex, String value) {
// TODO :NOT IMPLEMENTED
public void setNString(int parameterIndex, String value) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setNString is not supported.");
}

@Override
public void setNCharacterStream(int parameterIndex, Reader value, long length) {
// TODO :NOT IMPLEMENTED
public void setNCharacterStream(int parameterIndex, Reader value, long length)
throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setNCharacterStream is not supported.");
}

@Override
public void setNClob(int parameterIndex, NClob value) {
// TODO :NOT IMPLEMENTED
public void setNClob(int parameterIndex, NClob value) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setNClob is not supported.");
}

@Override
public void setClob(int parameterIndex, Reader reader, long length) {
// TODO :NOT IMPLEMENTED
public void setClob(int parameterIndex, Reader reader, long length) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setClob is not supported.");
}

@Override
public void setBlob(int parameterIndex, InputStream inputStream, long length) {
// TODO :NOT IMPLEMENTED
public void setBlob(int parameterIndex, InputStream inputStream, long length)
throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setBlob is not supported.");
}

@Override
public void setNClob(int parameterIndex, Reader reader, long length) {
// TODO :NOT IMPLEMENTED
public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setNClob is not supported.");
}

@Override
public void setSQLXML(int parameterIndex, SQLXML xmlObject) {
// TODO :NOT IMPLEMENTED
public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setSQLXML is not supported.");
}

/**
* Note: BigQuery handles numeric scale and precision dynamically for NUMERIC (DECIMAL) and
* BIGNUMERIC data types. The scaleOrLength parameter is ignored and delegates directly to {@link
* #setObject(int, Object, int)}.
*/
@Override
public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength) {
// TODO(neenu) : IMPLEMENT?
public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength)
throws SQLException {
checkClosed();
setObject(parameterIndex, x, targetSqlType);
}

@Override
public void setAsciiStream(int parameterIndex, InputStream x, long length) {
// TODO :NOT IMPLEMENTED
public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setAsciiStream is not supported.");
}

@Override
public void setBinaryStream(int parameterIndex, InputStream x, long length) {
// TODO :NOT IMPLEMENTED
public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setBinaryStream is not supported.");
}

@Override
public void setCharacterStream(int parameterIndex, Reader reader, long length) {
// TODO :NOT IMPLEMENTED
public void setCharacterStream(int parameterIndex, Reader reader, long length)
throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setCharacterStream is not supported.");
}

@Override
public void setAsciiStream(int parameterIndex, InputStream x) {
// TODO :NOT IMPLEMENTED
public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setAsciiStream is not supported.");
}

@Override
public void setBinaryStream(int parameterIndex, InputStream x) {
// TODO :NOT IMPLEMENTED
public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setBinaryStream is not supported.");
}

@Override
public void setCharacterStream(int parameterIndex, Reader reader) {
// TODO :NOT IMPLEMENTED
public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setCharacterStream is not supported.");
}

@Override
public void setNCharacterStream(int parameterIndex, Reader value) {
// TODO :NOT IMPLEMENTED
public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setNCharacterStream is not supported.");
}

@Override
public void setClob(int parameterIndex, Reader reader) {
// TODO :NOT IMPLEMENTED
public void setClob(int parameterIndex, Reader reader) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setClob is not supported.");
}

@Override
public void setBlob(int parameterIndex, InputStream inputStream) {
// TODO :NOT IMPLEMENTED
public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setBlob is not supported.");
}

@Override
public void setNClob(int parameterIndex, Reader reader) {
// TODO :NOT IMPLEMENTED
public void setNClob(int parameterIndex, Reader reader) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException("setNClob is not supported.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.cloud.bigquery.jdbc.BigQueryParameterHandler.BigQueryStatementParameterType;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -139,4 +140,20 @@ public void testGetSetParameterByIndex() throws Exception {
assertEquals(String.class, paramHandler.getType(2));
assertEquals(StandardSQLTypeName.STRING, paramHandler.getSqlType(2));
}

@Test
public void testConfigureParametersWidenNumericTypes() throws Exception {
BigQueryParameterHandler paramHandler = new BigQueryParameterHandler(3);
paramHandler.setParameter(1, (short) 5, Short.class);
paramHandler.setParameter(2, (byte) 10, Byte.class);
paramHandler.setParameter(3, 3.14f, Float.class);

QueryJobConfiguration.Builder builder = QueryJobConfiguration.newBuilder("SELECT 1");
paramHandler.configureParameters(builder);

QueryJobConfiguration config = builder.build();
assertEquals(3, config.getPositionalParameters().size());
assertEquals("5", config.getPositionalParameters().get(0).getValue());
assertEquals("10", config.getPositionalParameters().get(1).getValue());
}
}
Loading
Loading