Skip to content

Commit d2b6d59

Browse files
committed
almost all types are working
1 parent af28fca commit d2b6d59

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

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

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ public <T> T readValue(ClickHouseColumn column, Class<?> typeHint) throws IOExce
9797
int estimatedLen = column.getEstimatedLength();
9898
int precision = column.getPrecision();
9999
int scale = column.getScale();
100+
if (dataType == ClickHouseDataType.Decimal || dataType == ClickHouseDataType.Decimal32 ||
101+
dataType == ClickHouseDataType.Decimal64 || dataType == ClickHouseDataType.Decimal128 ||
102+
dataType == ClickHouseDataType.Decimal256) {
103+
precision = readByte();
104+
scale = readByte();
105+
System.out.println("p: " + precision + " " +scale);
106+
}
100107
TimeZone timezone = column.getTimeZoneOrDefault(timeZone);
101108

102109
try {
@@ -138,7 +145,7 @@ public <T> T readValue(ClickHouseColumn column, Class<?> typeHint) throws IOExce
138145
case UInt256:
139146
return (T) readBigIntegerLE(INT256_SIZE, true);
140147
case Decimal:
141-
return (T) readDecimal(column.getPrecision(), column.getScale());
148+
return (T) readDecimal(precision, scale);
142149
case Decimal32:
143150
return (T) readDecimal(ClickHouseDataType.Decimal32.getMaxPrecision(), scale);
144151
case Decimal64:
@@ -462,7 +469,6 @@ public BigDecimal readDecimal(int precision, int scale) throws IOException {
462469
} else {
463470
v = new BigDecimal(readBigIntegerLE(INT256_SIZE, false), scale);
464471
}
465-
466472
return v;
467473
}
468474

@@ -1010,31 +1016,14 @@ private ClickHouseDataType readDynamicData() throws IOException {
10101016
} else if (tag == ClickHouseDataType.CUSTOM_TYPE_BIN_TAG) {
10111017
String typeName = readString(input);
10121018
return ClickHouseDataType.valueOf(typeName);
1013-
} else if (tag == ClickHouseDataType.Decimal.getBinTag()) {
1014-
byte precision = readByte();
1015-
byte scale = readByte();
1016-
System.out.println("precision: " + precision + " scale: " + scale);
1017-
return ClickHouseDataType.Decimal;
10181019
} else if (tag == ClickHouseDataType.Decimal32.getBinTag()) {
1019-
byte precision = readByte();
1020-
byte scale = readByte();
1021-
System.out.println("precision: " + precision + " scale: " + scale);
10221020
return ClickHouseDataType.Decimal;
10231021
} else if (tag == ClickHouseDataType.Decimal64.getBinTag()) {
1024-
byte precision = readByte();
1025-
byte scale = readByte();
1026-
System.out.println("precision: " + precision + " scale: " + scale);
1027-
return ClickHouseDataType.Decimal64;
1022+
return ClickHouseDataType.Decimal;
10281023
} else if (tag == ClickHouseDataType.Decimal128.getBinTag()) {
1029-
byte precision = readByte();
1030-
byte scale = readByte();
1031-
System.out.println("precision: " + precision + " scale: " + scale);
1032-
return ClickHouseDataType.Decimal128;
1024+
return ClickHouseDataType.Decimal;
10331025
} else if (tag == ClickHouseDataType.Decimal256.getBinTag()) {
1034-
byte precision = readByte();
1035-
byte scale = readByte();
1036-
System.out.println("precision: " + precision + " scale: " + scale);
1037-
return ClickHouseDataType.Decimal256;
1026+
return ClickHouseDataType.Decimal;
10381027
} else {
10391028
type = ClickHouseDataType.binTag2Type.get(tag);
10401029
if (type == null) {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,17 @@ public static ClickHouseColumn valueToColumnForDynamicType(Object value) {
168168
column = ClickHouseColumn.of("v", "DateTime(" + dt.getZone().getId() + ")");
169169
} else if (value instanceof BigDecimal) {
170170
BigDecimal d = (BigDecimal) value;
171-
column = ClickHouseColumn.of("v", "Decimal256(" + d.precision() + ", " + d.scale() + ")");
171+
String decType;
172+
if (d.scale() <= ClickHouseDataType.Decimal32.getMaxScale()) {
173+
decType = "Decimal32";
174+
} else if (d.scale() <= ClickHouseDataType.Decimal64.getMaxScale()) {
175+
decType = "Decimal64";
176+
} else if (d.scale() <= ClickHouseDataType.Decimal128.getMaxScale()) {
177+
decType = "Decimal128";
178+
} else {
179+
decType = "Decimal256";
180+
}
181+
column = ClickHouseColumn.of("v", decType + "(" + d.precision() +"," + d.scale() + ")");
172182
} else if (value instanceof Map<?,?>) {
173183
Map<?, ?> map = (Map<?, ?>) value;
174184
// TODO: handle empty map?

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,10 @@ public void testDynamicWithPrimitives() throws Exception {
466466
case MultiPolygon:
467467
strValue = row.getGeoMultiPolygon("field").toString();
468468
break;
469+
case Decimal32:
470+
double v = row.getDouble("field");
471+
System.out.println(v);
472+
break;
469473
}
470474
System.out.println("field: " + strValue + " value " + value);
471475
if (value.getClass().isPrimitive()) {

0 commit comments

Comments
 (0)