Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -234,6 +234,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.clickhouse.jdbc;

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

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);
}

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

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,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 +55,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);
Comment thread
chernser marked this conversation as resolved.
map.put(ClickHouseDataType.Float32, JDBCType.FLOAT);
map.put(ClickHouseDataType.Float64, JDBCType.DOUBLE);
map.put(ClickHouseDataType.BFloat16, JDBCType.FLOAT);
Expand Down Expand Up @@ -130,7 +130,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);
Comment thread
cursor[bot] marked this conversation as resolved.
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 +173,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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.JDBCType;
import java.sql.SQLException;
import java.time.Instant;
import java.time.ZoneId;
Expand Down Expand Up @@ -150,4 +152,22 @@ public void testConvertToUnhexExpression() throws Exception {
decodedChecksum.update(decodedBytes);
assertEquals(decodedChecksum.getValue(), expectedChecksum, "Checksum of decoded bytes should match original");
}

@Test(groups = {"unit"})
public void testClickHouseToSqlType() {
assertEquals(JdbcUtils.convertToSqlType(ClickHouseDataType.Int128), JDBCType.NUMERIC);
assertEquals(JdbcUtils.convertToSqlType(ClickHouseDataType.Int256), JDBCType.NUMERIC);
assertEquals(JdbcUtils.convertToSqlType(ClickHouseDataType.UInt64), JDBCType.NUMERIC);
assertEquals(JdbcUtils.convertToSqlType(ClickHouseDataType.UInt128), JDBCType.NUMERIC);
assertEquals(JdbcUtils.convertToSqlType(ClickHouseDataType.UInt256), JDBCType.NUMERIC);
}

@Test(groups = {"unit"})
public void testClickHouseToJavaClass() {
assertEquals(JdbcUtils.convertToJavaClass(ClickHouseDataType.Int128), BigInteger.class);
assertEquals(JdbcUtils.convertToJavaClass(ClickHouseDataType.Int256), BigInteger.class);
assertEquals(JdbcUtils.convertToJavaClass(ClickHouseDataType.UInt64), BigInteger.class);
assertEquals(JdbcUtils.convertToJavaClass(ClickHouseDataType.UInt128), BigInteger.class);
assertEquals(JdbcUtils.convertToJavaClass(ClickHouseDataType.UInt256), BigInteger.class);
}
}