Skip to content

Commit beae9f4

Browse files
committed
fixed maps and decimals
1 parent 9c3a6ed commit beae9f4

File tree

3 files changed

+63
-24
lines changed

3 files changed

+63
-24
lines changed

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/BinaryStreamReader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,10 @@ private ClickHouseColumn readDynamicData() throws IOException {
10291029
} else if (tag == ClickHouseDataType.Array.getBinTag()) {
10301030
ClickHouseColumn elementColumn = readDynamicData();
10311031
return ClickHouseColumn.of("v", "Array(" + elementColumn.getOriginalTypeName() + ")");
1032+
} else if (tag == ClickHouseDataType.Map.getBinTag()) {
1033+
ClickHouseColumn keyInfo = readDynamicData();
1034+
ClickHouseColumn valueInfo = readDynamicData();
1035+
return ClickHouseColumn.of("v", "Map(" + keyInfo.getOriginalTypeName() + "," + valueInfo.getOriginalTypeName() + ")");
10321036
} else {
10331037
type = ClickHouseDataType.binTag2Type.get(tag);
10341038
if (type == null) {

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,22 @@ public static ClickHouseColumn valueToColumnForDynamicType(Object value) {
174174
} else if (value instanceof BigDecimal) {
175175
BigDecimal d = (BigDecimal) value;
176176
String decType;
177-
if (d.scale() <= ClickHouseDataType.Decimal32.getMaxScale()) {
178-
decType = "Decimal32";
179-
} else if (d.scale() <= ClickHouseDataType.Decimal64.getMaxScale()) {
180-
decType = "Decimal64";
181-
} else if (d.scale() <= ClickHouseDataType.Decimal128.getMaxScale()) {
177+
int scale;
178+
if (d.precision() > ClickHouseDataType.Decimal128.getMaxScale()) {
179+
decType = "Decimal256";
180+
scale = ClickHouseDataType.Decimal128.getMaxScale();
181+
} else if (d.precision() > ClickHouseDataType.Decimal64.getMaxScale()) {
182182
decType = "Decimal128";
183+
scale = ClickHouseDataType.Decimal128.getMaxScale();
184+
} else if (d.precision() > ClickHouseDataType.Decimal32.getMaxScale()) {
185+
decType = "Decimal64";
186+
scale = ClickHouseDataType.Decimal128.getMaxScale();
183187
} else {
184-
decType = "Decimal256";
188+
decType = "Decimal32";
189+
scale = ClickHouseDataType.Decimal128.getMaxScale();
185190
}
186-
column = ClickHouseColumn.of("v", decType + "(" + d.precision() +"," + d.scale() + ")");
191+
192+
column = ClickHouseColumn.of("v", decType + "(" + scale + ")");
187193
} else if (value instanceof Map<?,?>) {
188194
Map<?, ?> map = (Map<?, ?>) value;
189195
// TODO: handle empty map?
@@ -279,11 +285,9 @@ public static void writeDynamicTypeTag(OutputStream stream, ClickHouseColumn typ
279285
case Decimal128:
280286
case Decimal256:
281287
stream.write(binTag);
282-
stream.write(dt.getMaxPrecision());
283-
stream.write(dt.getMaxScale());
284-
//<tag><uint8_precision><uint8_scale>
288+
BinaryStreamUtils.writeUnsignedInt8(stream, dt.getMaxPrecision());
289+
BinaryStreamUtils.writeUnsignedInt8(stream, dt.getMaxScale());
285290
break;
286-
287291
case IntervalNanosecond:
288292
case IntervalMillisecond:
289293
case IntervalSecond:
@@ -313,8 +317,10 @@ public static void writeDynamicTypeTag(OutputStream stream, ClickHouseColumn typ
313317
writeDynamicTypeTag(stream, arrayElemColumn);
314318
break;
315319
case Map:
316-
stream.write(binTag);
317-
///0x0F<var_uint_size><key_encoding_1><value_encoding_1>...<key_encoding_N><value_encoding_N>
320+
stream.write(binTag);
321+
// 0x27<key_type_encoding><value_type_encoding>
322+
writeDynamicTypeTag(stream, typeColumn.getKeyInfo());
323+
writeDynamicTypeTag(stream, typeColumn.getValueInfo());
318324
break;
319325
case Tuple:
320326
// Tuple(T1, ..., TN)

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

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,6 @@ public void testVariantWithTuple() throws Exception {
388388
});
389389
}
390390

391-
392-
393391
@Test(groups = {"integration"})
394392
public void testDynamicWithPrimitives() throws Exception {
395393

@@ -416,11 +414,11 @@ public void testDynamicWithPrimitives() throws Exception {
416414
case DateTime:
417415
case DateTime32:
418416
case DateTime64:
419-
case Decimal:
420-
case Decimal32:
421-
case Decimal64:
422-
case Decimal128:
423-
case Decimal256:
417+
// case Decimal:
418+
// case Decimal32:
419+
// case Decimal64:
420+
// case Decimal128:
421+
// case Decimal256:
424422
// requires fix
425423
continue;
426424
case Array:
@@ -486,10 +484,6 @@ public void testDynamicWithPrimitives() throws Exception {
486484
case MultiPolygon:
487485
strValue = row.getGeoMultiPolygon("field").toString();
488486
break;
489-
case Decimal32:
490-
double v = row.getDouble("field");
491-
System.out.println(v);
492-
break;
493487
}
494488
System.out.println("field: " + strValue + " value " + value);
495489
if (value.getClass().isPrimitive()) {
@@ -538,6 +532,41 @@ public void testDynamicWithArrays() throws Exception {
538532
});
539533
}
540534

535+
@Test(groups = {"integration"})
536+
public void testDynamicWithMaps() throws Exception {
537+
Map<String, Byte> map1 = new HashMap<>();
538+
map1.put("key1", (byte) 1);
539+
map1.put("key2", (byte) 2);
540+
map1.put("key3", (byte) 3);
541+
542+
testDynamicWith("maps",
543+
new Object[]{
544+
map1
545+
},
546+
new String[]{
547+
"{key1=1, key2=2, key3=3}",
548+
});
549+
550+
551+
Map<Integer, String> map2 = new HashMap<>();
552+
map2.put(1, "a");
553+
map2.put(2, "b");
554+
555+
Map<String, String> map3 = new HashMap<>();
556+
map3.put("1", "a");
557+
map3.put("2", "b");
558+
559+
testDynamicWith("maps",
560+
new Object[]{
561+
map2,
562+
map3
563+
},
564+
new String[]{
565+
"{1=a, 2=b}",
566+
"{1=a, 2=b}",
567+
});
568+
}
569+
541570
@Data
542571
@AllArgsConstructor
543572
public static class DTOForDynamicPrimitivesTests {

0 commit comments

Comments
 (0)