Skip to content

Commit 0ac059f

Browse files
committed
fixed converting datatype name to SQLType int value
1 parent 935e491 commit 0ac059f

File tree

10 files changed

+288
-22
lines changed

10 files changed

+288
-22
lines changed

clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseColumn.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ private static ClickHouseColumn update(ClickHouseColumn column) {
129129
case Bool:
130130
column.template = ClickHouseBoolValue.ofNull();
131131
break;
132+
case Enum:
132133
case Enum8:
133134
case Enum16:
134135
column.template = ClickHouseEnumValue
@@ -746,7 +747,7 @@ public boolean isArray() {
746747
}
747748

748749
public boolean isEnum() {
749-
return dataType == ClickHouseDataType.Enum8 || dataType == ClickHouseDataType.Enum16;
750+
return dataType == ClickHouseDataType.Enum8 || dataType == ClickHouseDataType.Enum16 || dataType == ClickHouseDataType.Enum;
750751
}
751752

752753
public boolean isFixedLength() {

clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseDataType.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public enum ClickHouseDataType {
5555
DateTime(LocalDateTime.class, true, false, false, 0, 29, 0, 0, 9, false, 0x11, "TIMESTAMP"),
5656
DateTime32(LocalDateTime.class, true, false, false, 4, 19, 0, 0, 0, false, 0x12),
5757
DateTime64(LocalDateTime.class, true, false, false, 8, 29, 3, 0, 9, false, 0x14), // we always write timezone as argument
58+
Enum(String.class, true, true, false, 0, 0, 0, 0, 0, false),
5859
Enum8(String.class, true, true, false, 1, 0, 0, 0, 0, false, 0x17, "ENUM"),
5960
Enum16(String.class, true, true, false, 2, 0, 0, 0, 0, false, 0x18),
6061
FixedString(String.class, true, true, false, 0, 0, 0, 0, 0, false, 0x16, "BINARY"),
@@ -90,6 +91,8 @@ public enum ClickHouseDataType {
9091
Decimal64(BigDecimal.class, true, false, true, 8, 18, 18, 0, 18, false, 0x1A),
9192
Decimal128(BigDecimal.class, true, false, true, 16, 38, 38, 0, 38, false, 0x1B),
9293
Decimal256(BigDecimal.class, true, false, true, 32, 76, 20, 0, 76, false, 0x1C),
94+
95+
BFloat16(Float.class, false, true, true, 2, 3, 0, 0, 16, false, 0x31),
9396
Float32(Float.class, false, true, true, 4, 12, 0, 0, 38, false, 0x0D, "FLOAT", "REAL", "SINGLE"),
9497
Float64(Double.class, false, true, true, 8, 22, 0, 0, 308, false, 0x0E, "DOUBLE", "DOUBLE PRECISION"),
9598
IPv4(Inet4Address.class, false, true, false, 4, 10, 0, 0, 0, false, 0x28, "INET4"),
@@ -99,6 +102,9 @@ public enum ClickHouseDataType {
99102
Polygon(Object.class, false, true, true, 0, 0, 0, 0, 0, true, 0x2C), // same as Array(Ring)
100103
MultiPolygon(Object.class, false, true, true, 0, 0, 0, 0, 0, true, 0x2C), // same as Array(Polygon)
101104
Ring(Object.class, false, true, true, 0, 0, 0, 0, 0, true, 0x2C), // same as Array(Point)
105+
LineString( Object.class, false, true, true, 0, 0, 0, 0, 0, true, 0x2C), // same as Array(Point)
106+
MultiLineString(Object.class, false, true, true, 0, 0, 0, 0, 0, true, 0x2C), // same as Array(Ring)
107+
102108
JSON(Object.class, false, false, false, 0, 0, 0, 0, 0, true, 0x30),
103109
@Deprecated
104110
Object(Object.class, true, true, false, 0, 0, 0, 0, 0, true),
@@ -113,6 +119,8 @@ public enum ClickHouseDataType {
113119
Nested(Object.class, true, true, false, 0, 0, 0, 0, 0, true, 0x2F),
114120
Tuple(List.class, true, true, false, 0, 0, 0, 0, 0, true, 0x1F),
115121
Nothing(Object.class, false, true, false, 0, 0, 0, 0, 0, true, 0x00),
122+
LowCardinality(Object.class, true, false, false, 0, 0, 0, 0, 0, true, 0x26),
123+
Nullable( Object.class, true, false, false, 0, 0, 0, 0, 0, true, 0x23),
116124
SimpleAggregateFunction(String.class, true, true, false, 0, 0, 0, 0, 0, false, 0x2E),
117125
// implementation-defined intermediate state
118126
AggregateFunction(String.class, true, true, false, 0, 0, 0, 0, 0, true),
@@ -364,7 +372,7 @@ public byte getTag() {
364372
* @return true if the type name is an alias; false otherwise
365373
*/
366374
public static boolean isAlias(String typeName) {
367-
return typeName != null && !typeName.isEmpty() && allAliases.contains(typeName.trim().toUpperCase());
375+
return typeName != null && !typeName.isEmpty() && allAliases.contains(typeName.trim());
368376
}
369377

370378
/**

clickhouse-data/src/test/java/com/clickhouse/data/ClickHouseColumnTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,9 @@ public void testEnum(String typeName) {
253253
Assert.assertEquals(column.getEnumConstants().name(10), "Query'Finish");
254254
Assert.assertEquals(column.getEnumConstants().value("Query'Start"), 1);
255255
Assert.assertEquals(column.getEnumConstants().value("Query'Finish"), 10);
256-
Assert.assertTrue(column.isFixedLength(), "Should have fixed length in byte");
256+
if (column.getDataType() != ClickHouseDataType.Enum) { // virtual type
257+
Assert.assertTrue(column.isFixedLength(), "Should have fixed length in byte");
258+
}
257259
Assert.assertEquals(column.getEstimatedLength(), column.getDataType().getByteLength());
258260
}
259261

@@ -417,7 +419,8 @@ public boolean isWidenUnsignedTypes() {
417419
for (ClickHouseDataType type : ClickHouseDataType.values()) {
418420
// skip advanced types
419421
if (type.isNested() || type == ClickHouseDataType.AggregateFunction
420-
|| type == ClickHouseDataType.SimpleAggregateFunction) {
422+
|| type == ClickHouseDataType.SimpleAggregateFunction || type == ClickHouseDataType.Enum
423+
|| type == ClickHouseDataType.Nullable || type == ClickHouseDataType.BFloat16) {
421424
continue;
422425
}
423426

clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseDatabaseMetaDataTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ public void testGetTypeInfo() throws SQLException {
9696
props.setProperty("decompress", "0");
9797
try (ClickHouseConnection conn = newConnection(props); ResultSet rs = conn.getMetaData().getTypeInfo()) {
9898
while (rs.next()) {
99-
Assert.assertNotNull(rs.getString(1));
99+
rs.getString(1);
100+
System.out.println(rs.getString(1));
101+
rs.getInt(2);
100102
}
101103
}
102104
}

client-v2/src/test/java/com/clickhouse/client/datatypes/DataTypeTests.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ public void testVariantWithSimpleDataTypes() throws Exception {
156156
b.append(table).append(" ( rowId Int64, field Variant(String, ").append(dataType.name());
157157

158158
switch (dataType) {
159+
case BFloat16:
160+
// TODO: add support
161+
continue dataTypesLoop;
159162
case String:
160163
case FixedString:
161164
case Nothing:
@@ -179,6 +182,11 @@ public void testVariantWithSimpleDataTypes() throws Exception {
179182
case AggregateFunction:
180183
case Enum8:
181184
case Enum16:
185+
case Enum:
186+
case Nullable: // virtual type
187+
case LowCardinality: // virtual type
188+
case LineString: // same as Ring
189+
case MultiLineString: // same as MultiPolygon
182190
// tested separately
183191
continue dataTypesLoop;
184192

@@ -414,6 +422,9 @@ public void testDynamicWithPrimitives() throws Exception {
414422
int rowId = 0;
415423
for (ClickHouseDataType dataType : ClickHouseDataType.values()) {
416424
switch (dataType) {
425+
case BFloat16:
426+
// TODO: add support
427+
continue;
417428
case Array:
418429
case Map:
419430
case AggregateFunction:
@@ -428,6 +439,11 @@ public void testDynamicWithPrimitives() throws Exception {
428439
case Tuple:
429440
case Variant:
430441
case Decimal: // virtual type
442+
case Nullable: // virtual type
443+
case LowCardinality: // virtual type
444+
case Enum: // virtual type
445+
case LineString: // same as Ring
446+
case MultiLineString: // same as MultiPolygon
431447
// no tests or tested in other tests
432448
continue;
433449
default:
@@ -447,7 +463,7 @@ public void testDynamicWithPrimitives() throws Exception {
447463
}
448464
}
449465

450-
Assert.assertNotNull(value);
466+
Assert.assertNotNull(value, "Value for " + dataType.name() + " should not be null.");
451467

452468
List<DTOForDynamicPrimitivesTests> data = new ArrayList<>();
453469
data.add(new DTOForDynamicPrimitivesTests(rowId++, value));

jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected ResultSetImpl(ResultSetImpl resultSet) {
4848
this.parentStatement = resultSet.parentStatement;
4949
this.response = resultSet.response;
5050
this.reader = resultSet.reader;
51-
this.metaData = resultSet.metaData;
51+
this.metaData = new com.clickhouse.jdbc.metadata.ResultSetMetaData(this);
5252
this.closed = false;
5353
this.wasNull = false;
5454
this.defaultCalendar = parentStatement.connection.defaultCalendar;

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
44
import com.clickhouse.data.ClickHouseDataType;
55
import com.clickhouse.jdbc.types.Array;
6+
import org.jspecify.annotations.Nullable;
67

78
import java.sql.Date;
89
import java.sql.JDBCType;
@@ -15,16 +16,27 @@
1516
import java.time.ZonedDateTime;
1617
import java.time.temporal.TemporalAccessor;
1718
import java.util.ArrayList;
19+
import java.util.Collections;
1820
import java.util.HashMap;
1921
import java.util.List;
2022
import java.util.Map;
23+
import java.util.Optional;
24+
import java.util.Set;
2125
import java.util.TreeMap;
2226
import java.util.regex.Matcher;
2327
import java.util.regex.Pattern;
2428

2529
public class JdbcUtils {
2630
//Define a map to store the mapping between ClickHouse data types and SQL data types
2731
private static final Map<ClickHouseDataType, SQLType> CLICKHOUSE_TO_SQL_TYPE_MAP = generateTypeMap();
32+
33+
public static final Map<String, SQLType> CLICKHOUSE_TYPE_NAME_TO_SQL_TYPE_MAP = Collections.unmodifiableMap(generateTypeMap().entrySet()
34+
.stream().collect(
35+
HashMap::new,
36+
(map, entry) -> map.put(entry.getKey().name(), entry.getValue()),
37+
HashMap::putAll
38+
));
39+
2840
private static Map<ClickHouseDataType, SQLType> generateTypeMap() {
2941
Map<ClickHouseDataType, SQLType> map = new TreeMap<>(); // TreeMap is used to sort the keys in natural order so FixedString will be before String :-) (type match should be more accurate)
3042
map.put(ClickHouseDataType.Int8, JDBCType.TINYINT);
@@ -44,6 +56,7 @@ private static Map<ClickHouseDataType, SQLType> generateTypeMap() {
4456
map.put(ClickHouseDataType.Decimal128, JDBCType.DECIMAL);
4557
map.put(ClickHouseDataType.String, JDBCType.VARCHAR);
4658
map.put(ClickHouseDataType.FixedString, JDBCType.VARCHAR);
59+
map.put(ClickHouseDataType.Enum, JDBCType.VARCHAR);
4760
map.put(ClickHouseDataType.Enum8, JDBCType.VARCHAR);
4861
map.put(ClickHouseDataType.Enum16, JDBCType.VARCHAR);
4962
map.put(ClickHouseDataType.Date, JDBCType.DATE);
@@ -54,6 +67,12 @@ private static Map<ClickHouseDataType, SQLType> generateTypeMap() {
5467
map.put(ClickHouseDataType.Array, JDBCType.ARRAY);
5568
map.put(ClickHouseDataType.Nested, JDBCType.ARRAY);
5669
map.put(ClickHouseDataType.Map, JDBCType.JAVA_OBJECT);
70+
map.put(ClickHouseDataType.Point, JDBCType.OTHER);
71+
map.put(ClickHouseDataType.Ring, JDBCType.OTHER);
72+
map.put(ClickHouseDataType.Polygon, JDBCType.OTHER);
73+
map.put(ClickHouseDataType.LineString, JDBCType.OTHER);
74+
map.put(ClickHouseDataType.MultiPolygon, JDBCType.OTHER);
75+
map.put(ClickHouseDataType.MultiLineString, JDBCType.OTHER);
5776
return map;
5877
}
5978

@@ -240,5 +259,4 @@ public static Object convert(Object value, Class<?> type) throws SQLException {
240259

241260
throw new SQLException("Unsupported conversion from " + value.getClass().getName() + " to " + type.getName(), ExceptionUtils.SQL_STATE_DATA_EXCEPTION);
242261
}
243-
244262
}

0 commit comments

Comments
 (0)