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 @@ -158,11 +158,13 @@ protected int getSqlType(Class<?> javaClass) { // and purpose(e.g. for read or w
sqlType = Types.BIGINT;
} else if (javaClass == float.class || javaClass == Float.class) {
sqlType = Types.FLOAT;
} else if (javaClass == double.class || javaClass == Double.class) {
sqlType = Types.DOUBLE;
} else if (javaClass == BigInteger.class || javaClass == BigDecimal.class) {
sqlType = Types.DECIMAL;
} else if (javaClass == Date.class || javaClass == LocalDate.class) {
} else if (javaClass == double.class || javaClass == Double.class) {
sqlType = Types.DOUBLE;
} else if (javaClass == BigInteger.class) {
sqlType = Types.NUMERIC;
} else if (javaClass == BigDecimal.class) {
sqlType = Types.DECIMAL;
} else if (javaClass == Date.class || javaClass == LocalDate.class) {
sqlType = Types.DATE;
} else if (javaClass == Time.class || javaClass == LocalTime.class) {
sqlType = Types.TIME;
Expand Down Expand Up @@ -234,6 +236,8 @@ public int toSqlType(ClickHouseColumn column, Map<String, Class<?>> typeMap) {
case UInt128:
case Int256:
case UInt256:
sqlType = Types.NUMERIC;
break;
case Decimal:
case Decimal32:
case Decimal64:
Expand Down Expand Up @@ -526,6 +530,13 @@ public Class<?> toJavaClass(ClickHouseColumn column, Map<String, Class<?>> typeM

ClickHouseDataType type = column.getDataType();
switch (type) {
case UInt64:
case Int128:
case UInt128:
case Int256:
case UInt256:
clazz = BigInteger.class;
break;
case DateTime:
case DateTime32:
case DateTime64:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.clickhouse.jdbc;

import com.clickhouse.data.ClickHouseColumn;
import com.clickhouse.data.ClickHouseDataType;
import org.testng.annotations.Test;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Types;

import static org.testng.Assert.assertEquals;

public class JdbcTypeMappingTest {

private final JdbcTypeMapping mapping = JdbcTypeMapping.getDefaultMapping();

@Test(groups = {"unit"})
public void testInt128Mapping() {
ClickHouseColumn col = ClickHouseColumn.of("col", ClickHouseDataType.Int128, false, false);
assertEquals(mapping.toSqlType(col, null), Types.NUMERIC);
assertEquals(mapping.toJavaClass(col, null), BigInteger.class);
}

@Test(groups = {"unit"})
public void testInt256Mapping() {
ClickHouseColumn col = ClickHouseColumn.of("col", ClickHouseDataType.Int256, false, false);
assertEquals(mapping.toSqlType(col, null), Types.NUMERIC);
assertEquals(mapping.toJavaClass(col, null), BigInteger.class);
}

@Test(groups = {"unit"})
public void testUInt64Mapping() {
ClickHouseColumn col = ClickHouseColumn.of("col", ClickHouseDataType.UInt64, false, false);
assertEquals(mapping.toSqlType(col, null), Types.NUMERIC);
assertEquals(mapping.toJavaClass(col, null), BigInteger.class);
}

@Test(groups = {"unit"})
public void testUInt128Mapping() {
ClickHouseColumn col = ClickHouseColumn.of("col", ClickHouseDataType.UInt128, false, false);
assertEquals(mapping.toSqlType(col, null), Types.NUMERIC);
assertEquals(mapping.toJavaClass(col, null), BigInteger.class);
}

@Test(groups = {"unit"})
public void testUInt256Mapping() {
ClickHouseColumn col = ClickHouseColumn.of("col", ClickHouseDataType.UInt256, false, false);
assertEquals(mapping.toSqlType(col, null), Types.NUMERIC);
assertEquals(mapping.toJavaClass(col, null), BigInteger.class);
}

@Test(groups = {"unit"})
public void testBigIntegerToSqlType() {
// Test that BigInteger class is mapped to Types.NUMERIC (not DECIMAL)
assertEquals(mapping.getSqlType(BigInteger.class), Types.NUMERIC);
}

@Test(groups = {"unit"})
public void testBigDecimalToSqlType() {
// Test that BigDecimal class is still mapped to Types.DECIMAL
assertEquals(mapping.getSqlType(BigDecimal.class), Types.DECIMAL);
}
}

36 changes: 11 additions & 25 deletions jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.clickhouse.jdbc.types.Array;
import com.google.common.collect.ImmutableMap;

import java.math.BigInteger;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
Expand Down Expand Up @@ -44,9 +43,9 @@ public class JdbcUtils {

public static final Map<String, SQLType> CLICKHOUSE_TYPE_NAME_TO_SQL_TYPE_MAP = Collections.unmodifiableMap(generateTypeMap().entrySet()
.stream().collect(
HashMap::new,
(map, entry) -> map.put(entry.getKey().name(), entry.getValue()),
HashMap::putAll
HashMap::new,
(map, entry) -> map.put(entry.getKey().name(), entry.getValue()),
HashMap::putAll
));

private static Map<ClickHouseDataType, SQLType> generateTypeMap() {
Expand All @@ -55,14 +54,14 @@ private static Map<ClickHouseDataType, SQLType> generateTypeMap() {
map.put(ClickHouseDataType.Int16, JDBCType.SMALLINT);
map.put(ClickHouseDataType.Int32, JDBCType.INTEGER);
map.put(ClickHouseDataType.Int64, JDBCType.BIGINT);
map.put(ClickHouseDataType.Int128, JDBCType.OTHER);
map.put(ClickHouseDataType.Int256, JDBCType.OTHER);
map.put(ClickHouseDataType.Int128, JDBCType.NUMERIC);
map.put(ClickHouseDataType.Int256, JDBCType.NUMERIC);
map.put(ClickHouseDataType.UInt8, JDBCType.SMALLINT);
map.put(ClickHouseDataType.UInt16, JDBCType.INTEGER);
map.put(ClickHouseDataType.UInt32, JDBCType.BIGINT);
map.put(ClickHouseDataType.UInt64, JDBCType.OTHER);
map.put(ClickHouseDataType.UInt128, JDBCType.OTHER);
map.put(ClickHouseDataType.UInt256, JDBCType.OTHER);
map.put(ClickHouseDataType.UInt64, JDBCType.NUMERIC);
map.put(ClickHouseDataType.UInt128, JDBCType.NUMERIC);
map.put(ClickHouseDataType.UInt256, JDBCType.NUMERIC);
map.put(ClickHouseDataType.Float32, JDBCType.FLOAT);
map.put(ClickHouseDataType.Float64, JDBCType.DOUBLE);
map.put(ClickHouseDataType.BFloat16, JDBCType.FLOAT);
Expand Down Expand Up @@ -130,7 +129,7 @@ private static Map<SQLType, Class<?>> generateClassMap() {
map.put(JDBCType.CHAR, String.class);
map.put(JDBCType.VARCHAR, String.class);
map.put(JDBCType.LONGVARCHAR, String.class);
map.put(JDBCType.NUMERIC, java.math.BigDecimal.class);
map.put(JDBCType.NUMERIC, java.math.BigInteger.class);
map.put(JDBCType.DECIMAL, java.math.BigDecimal.class);
map.put(JDBCType.BIT, Boolean.class);
map.put(JDBCType.BOOLEAN, Boolean.class);
Expand Down Expand Up @@ -173,21 +172,6 @@ private static Map<ClickHouseDataType, Class<?>> getDataTypeClassMap() {
for (Map.Entry<ClickHouseDataType, SQLType> e : CLICKHOUSE_TO_SQL_TYPE_MAP.entrySet()) {
if (e.getValue().equals(JDBCType.OTHER)) {
switch (e.getKey()) {
case UInt64:
map.put(e.getKey(), BigInteger.class);
break;
case UInt128:
map.put(e.getKey(), BigInteger.class);
break;
case UInt256:
map.put(e.getKey(), BigInteger.class);
break;
case Int128:
map.put(e.getKey(), BigInteger.class);
break;
case Int256:
map.put(e.getKey(), BigInteger.class);
break;
case Point:
map.put(e.getKey(), double[].class);
break;
Expand Down Expand Up @@ -361,6 +345,8 @@ static Object convertObject(Object value, Class<?> type, ClickHouseColumn column
return Double.parseDouble(value.toString());
} else if (type == java.math.BigDecimal.class) {
return new java.math.BigDecimal(value.toString());
} else if (type == java.math.BigInteger.class) {
return new java.math.BigInteger(value.toString());
} else if (type == Duration.class && value instanceof LocalDateTime) {
return DataTypeUtils.localDateTimeToDuration((LocalDateTime) value);
} else if (value instanceof TemporalAccessor) {
Expand Down
Loading
Loading