Skip to content

Commit 268f1b7

Browse files
authored
Merge pull request #2780 from HanchiMed/issue-2779
[MHA] fix issue-2779
2 parents df9692f + 3cd682e commit 268f1b7

File tree

5 files changed

+294
-30
lines changed

5 files changed

+294
-30
lines changed

clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/JdbcTypeMapping.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,13 @@ protected int getSqlType(Class<?> javaClass) { // and purpose(e.g. for read or w
158158
sqlType = Types.BIGINT;
159159
} else if (javaClass == float.class || javaClass == Float.class) {
160160
sqlType = Types.FLOAT;
161-
} else if (javaClass == double.class || javaClass == Double.class) {
162-
sqlType = Types.DOUBLE;
163-
} else if (javaClass == BigInteger.class || javaClass == BigDecimal.class) {
164-
sqlType = Types.DECIMAL;
165-
} else if (javaClass == Date.class || javaClass == LocalDate.class) {
161+
} else if (javaClass == double.class || javaClass == Double.class) {
162+
sqlType = Types.DOUBLE;
163+
} else if (javaClass == BigInteger.class) {
164+
sqlType = Types.NUMERIC;
165+
} else if (javaClass == BigDecimal.class) {
166+
sqlType = Types.DECIMAL;
167+
} else if (javaClass == Date.class || javaClass == LocalDate.class) {
166168
sqlType = Types.DATE;
167169
} else if (javaClass == Time.class || javaClass == LocalTime.class) {
168170
sqlType = Types.TIME;
@@ -234,6 +236,8 @@ public int toSqlType(ClickHouseColumn column, Map<String, Class<?>> typeMap) {
234236
case UInt128:
235237
case Int256:
236238
case UInt256:
239+
sqlType = Types.NUMERIC;
240+
break;
237241
case Decimal:
238242
case Decimal32:
239243
case Decimal64:
@@ -526,6 +530,13 @@ public Class<?> toJavaClass(ClickHouseColumn column, Map<String, Class<?>> typeM
526530

527531
ClickHouseDataType type = column.getDataType();
528532
switch (type) {
533+
case UInt64:
534+
case Int128:
535+
case UInt128:
536+
case Int256:
537+
case UInt256:
538+
clazz = BigInteger.class;
539+
break;
529540
case DateTime:
530541
case DateTime32:
531542
case DateTime64:
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.clickhouse.jdbc;
2+
3+
import com.clickhouse.data.ClickHouseColumn;
4+
import com.clickhouse.data.ClickHouseDataType;
5+
import org.testng.annotations.Test;
6+
7+
import java.math.BigDecimal;
8+
import java.math.BigInteger;
9+
import java.sql.Types;
10+
11+
import static org.testng.Assert.assertEquals;
12+
13+
public class JdbcTypeMappingTest {
14+
15+
private final JdbcTypeMapping mapping = JdbcTypeMapping.getDefaultMapping();
16+
17+
@Test(groups = {"unit"})
18+
public void testInt128Mapping() {
19+
ClickHouseColumn col = ClickHouseColumn.of("col", ClickHouseDataType.Int128, false, false);
20+
assertEquals(mapping.toSqlType(col, null), Types.NUMERIC);
21+
assertEquals(mapping.toJavaClass(col, null), BigInteger.class);
22+
}
23+
24+
@Test(groups = {"unit"})
25+
public void testInt256Mapping() {
26+
ClickHouseColumn col = ClickHouseColumn.of("col", ClickHouseDataType.Int256, false, false);
27+
assertEquals(mapping.toSqlType(col, null), Types.NUMERIC);
28+
assertEquals(mapping.toJavaClass(col, null), BigInteger.class);
29+
}
30+
31+
@Test(groups = {"unit"})
32+
public void testUInt64Mapping() {
33+
ClickHouseColumn col = ClickHouseColumn.of("col", ClickHouseDataType.UInt64, false, false);
34+
assertEquals(mapping.toSqlType(col, null), Types.NUMERIC);
35+
assertEquals(mapping.toJavaClass(col, null), BigInteger.class);
36+
}
37+
38+
@Test(groups = {"unit"})
39+
public void testUInt128Mapping() {
40+
ClickHouseColumn col = ClickHouseColumn.of("col", ClickHouseDataType.UInt128, false, false);
41+
assertEquals(mapping.toSqlType(col, null), Types.NUMERIC);
42+
assertEquals(mapping.toJavaClass(col, null), BigInteger.class);
43+
}
44+
45+
@Test(groups = {"unit"})
46+
public void testUInt256Mapping() {
47+
ClickHouseColumn col = ClickHouseColumn.of("col", ClickHouseDataType.UInt256, false, false);
48+
assertEquals(mapping.toSqlType(col, null), Types.NUMERIC);
49+
assertEquals(mapping.toJavaClass(col, null), BigInteger.class);
50+
}
51+
52+
@Test(groups = {"unit"})
53+
public void testBigIntegerToSqlType() {
54+
// Test that BigInteger class is mapped to Types.NUMERIC (not DECIMAL)
55+
assertEquals(mapping.getSqlType(BigInteger.class), Types.NUMERIC);
56+
}
57+
58+
@Test(groups = {"unit"})
59+
public void testBigDecimalToSqlType() {
60+
// Test that BigDecimal class is still mapped to Types.DECIMAL
61+
assertEquals(mapping.getSqlType(BigDecimal.class), Types.DECIMAL);
62+
}
63+
}
64+

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.clickhouse.jdbc.types.Array;
1010
import com.google.common.collect.ImmutableMap;
1111

12-
import java.math.BigInteger;
1312
import java.net.Inet4Address;
1413
import java.net.Inet6Address;
1514
import java.net.InetAddress;
@@ -44,9 +43,9 @@ public class JdbcUtils {
4443

4544
public static final Map<String, SQLType> CLICKHOUSE_TYPE_NAME_TO_SQL_TYPE_MAP = Collections.unmodifiableMap(generateTypeMap().entrySet()
4645
.stream().collect(
47-
HashMap::new,
48-
(map, entry) -> map.put(entry.getKey().name(), entry.getValue()),
49-
HashMap::putAll
46+
HashMap::new,
47+
(map, entry) -> map.put(entry.getKey().name(), entry.getValue()),
48+
HashMap::putAll
5049
));
5150

5251
private static Map<ClickHouseDataType, SQLType> generateTypeMap() {
@@ -55,14 +54,14 @@ private static Map<ClickHouseDataType, SQLType> generateTypeMap() {
5554
map.put(ClickHouseDataType.Int16, JDBCType.SMALLINT);
5655
map.put(ClickHouseDataType.Int32, JDBCType.INTEGER);
5756
map.put(ClickHouseDataType.Int64, JDBCType.BIGINT);
58-
map.put(ClickHouseDataType.Int128, JDBCType.OTHER);
59-
map.put(ClickHouseDataType.Int256, JDBCType.OTHER);
57+
map.put(ClickHouseDataType.Int128, JDBCType.NUMERIC);
58+
map.put(ClickHouseDataType.Int256, JDBCType.NUMERIC);
6059
map.put(ClickHouseDataType.UInt8, JDBCType.SMALLINT);
6160
map.put(ClickHouseDataType.UInt16, JDBCType.INTEGER);
6261
map.put(ClickHouseDataType.UInt32, JDBCType.BIGINT);
63-
map.put(ClickHouseDataType.UInt64, JDBCType.OTHER);
64-
map.put(ClickHouseDataType.UInt128, JDBCType.OTHER);
65-
map.put(ClickHouseDataType.UInt256, JDBCType.OTHER);
62+
map.put(ClickHouseDataType.UInt64, JDBCType.NUMERIC);
63+
map.put(ClickHouseDataType.UInt128, JDBCType.NUMERIC);
64+
map.put(ClickHouseDataType.UInt256, JDBCType.NUMERIC);
6665
map.put(ClickHouseDataType.Float32, JDBCType.FLOAT);
6766
map.put(ClickHouseDataType.Float64, JDBCType.DOUBLE);
6867
map.put(ClickHouseDataType.BFloat16, JDBCType.FLOAT);
@@ -130,7 +129,7 @@ private static Map<SQLType, Class<?>> generateClassMap() {
130129
map.put(JDBCType.CHAR, String.class);
131130
map.put(JDBCType.VARCHAR, String.class);
132131
map.put(JDBCType.LONGVARCHAR, String.class);
133-
map.put(JDBCType.NUMERIC, java.math.BigDecimal.class);
132+
map.put(JDBCType.NUMERIC, java.math.BigInteger.class);
134133
map.put(JDBCType.DECIMAL, java.math.BigDecimal.class);
135134
map.put(JDBCType.BIT, Boolean.class);
136135
map.put(JDBCType.BOOLEAN, Boolean.class);
@@ -173,21 +172,6 @@ private static Map<ClickHouseDataType, Class<?>> getDataTypeClassMap() {
173172
for (Map.Entry<ClickHouseDataType, SQLType> e : CLICKHOUSE_TO_SQL_TYPE_MAP.entrySet()) {
174173
if (e.getValue().equals(JDBCType.OTHER)) {
175174
switch (e.getKey()) {
176-
case UInt64:
177-
map.put(e.getKey(), BigInteger.class);
178-
break;
179-
case UInt128:
180-
map.put(e.getKey(), BigInteger.class);
181-
break;
182-
case UInt256:
183-
map.put(e.getKey(), BigInteger.class);
184-
break;
185-
case Int128:
186-
map.put(e.getKey(), BigInteger.class);
187-
break;
188-
case Int256:
189-
map.put(e.getKey(), BigInteger.class);
190-
break;
191175
case Point:
192176
map.put(e.getKey(), double[].class);
193177
break;
@@ -361,6 +345,8 @@ static Object convertObject(Object value, Class<?> type, ClickHouseColumn column
361345
return Double.parseDouble(value.toString());
362346
} else if (type == java.math.BigDecimal.class) {
363347
return new java.math.BigDecimal(value.toString());
348+
} else if (type == java.math.BigInteger.class) {
349+
return new java.math.BigInteger(value.toString());
364350
} else if (type == Duration.class && value instanceof LocalDateTime) {
365351
return DataTypeUtils.localDateTimeToDuration((LocalDateTime) value);
366352
} else if (value instanceof TemporalAccessor) {

0 commit comments

Comments
 (0)