Skip to content

Commit af28fca

Browse files
committed
implemented interval and partially decimal
1 parent f18ce46 commit af28fca

File tree

5 files changed

+109
-7
lines changed

5 files changed

+109
-7
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ private static Set<Class<?>> setOf(Class<?>... args) {
230230

231231
public static final byte CUSTOM_TYPE_BIN_TAG = 0x2C;
232232

233+
public static final byte TUPLE_WITHOUT_NAMES_BIN_TAG = 0x1F;
234+
235+
public static final byte TUPLE_WITH_NAMES_BIN_TAG = 0x20;
236+
233237
public enum IntervalKindBinTag {
234238
Nanosecond(IntervalNanosecond, 0x00),
235239
Microsecond(IntervalMicrosecond, 0x01),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ protected void setSchema(TableSchema schema) {
252252
case Enum8:
253253
case Enum16:
254254
case Variant:
255+
case Dynamic:
255256
this.convertions[i] = NumberConverter.NUMBER_CONVERTERS;
256257
break;
257258
default:

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,13 +1005,37 @@ private ClickHouseDataType readDynamicData() throws IOException {
10051005
byte intervalKind = readByte();
10061006
type = ClickHouseDataType.intervalKind2Type.get(intervalKind);
10071007
if (type == null) {
1008-
throw new ClientException("Unsupported interval kind: " + intervalKind);
1008+
throw new ClientException("Unsupported interval kind: " + intervalKind);
10091009
}
1010-
1010+
} else if (tag == ClickHouseDataType.CUSTOM_TYPE_BIN_TAG) {
1011+
String typeName = readString(input);
1012+
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;
1018+
} else if (tag == ClickHouseDataType.Decimal32.getBinTag()) {
1019+
byte precision = readByte();
1020+
byte scale = readByte();
1021+
System.out.println("precision: " + precision + " scale: " + scale);
1022+
return ClickHouseDataType.Decimal;
1023+
} 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;
1028+
} 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;
1033+
} 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;
10111038
} else {
1012-
if (tag == ClickHouseDataType.String.getBinTag()) {
1013-
System.out.println("String");
1014-
}
10151039
type = ClickHouseDataType.binTag2Type.get(tag);
10161040
if (type == null) {
10171041
throw new ClientException("Unsupported data type with tag " + tag);

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.time.LocalDateTime;
3434
import java.time.ZoneId;
3535
import java.time.ZonedDateTime;
36+
import java.util.ArrayList;
3637
import java.util.Arrays;
3738
import java.util.Collections;
3839
import java.util.HashMap;
@@ -124,6 +125,11 @@ private static Map<Class<?>, ClickHouseColumn> getPredefinedTypeColumnsMap() {
124125
map.put(String.class, ClickHouseColumn.of("v", "String"));
125126
map.put(LocalDateTime.class, ClickHouseColumn.of("v", "DateTime"));
126127

128+
map.put(ClickHouseGeoPointValue.class, ClickHouseColumn.of("v", "Point"));
129+
map.put(ClickHouseGeoRingValue.class, ClickHouseColumn.of("v", "Ring"));
130+
map.put(ClickHouseGeoPolygonValue.class, ClickHouseColumn.of("v", "Polygon"));
131+
map.put(ClickHouseGeoMultiPolygonValue.class, ClickHouseColumn.of("v", "MultiPolygon"));
132+
127133
map.put(boolean[].class, ClickHouseColumn.of("v", "Array(Bool)"));
128134
map.put(boolean[][].class, ClickHouseColumn.of("v", "Array(Array(Bool))"));
129135
map.put(boolean[][][].class, ClickHouseColumn.of("v", "Array(Array(Array(Bool)))"));
@@ -239,6 +245,8 @@ public static void writeDynamicTypeTag(OutputStream stream, ClickHouseColumn typ
239245
case Decimal128:
240246
case Decimal256:
241247
stream.write(binTag);
248+
stream.write(dt.getMaxPrecision());
249+
stream.write(dt.getMaxScale());
242250
//<tag><uint8_precision><uint8_scale>
243251
break;
244252

@@ -253,7 +261,11 @@ public static void writeDynamicTypeTag(OutputStream stream, ClickHouseColumn typ
253261
case IntervalQuarter:
254262
case IntervalYear:
255263
stream.write(binTag);
256-
stream.write(ClickHouseDataType.IntervalKindBinTag.Day.getTag());
264+
Byte kindTag = ClickHouseDataType.intervalType2Kind.get(dt);
265+
if (kindTag == null) {
266+
throw new ClientException("BUG! No Interval Mapping to a kind tag");
267+
}
268+
stream.write(kindTag);
257269
break;
258270
case DateTime32:
259271
stream.write(binTag);
@@ -283,6 +295,7 @@ public static void writeDynamicTypeTag(OutputStream stream, ClickHouseColumn typ
283295
case Ring:
284296
case MultiPolygon:
285297
stream.write(ClickHouseDataType.CUSTOM_TYPE_BIN_TAG);
298+
BinaryStreamUtils.writeString(stream, dt.name());
286299
break;
287300
case Variant:
288301
stream.write(binTag);
@@ -469,6 +482,18 @@ private static void serializePrimitiveData(OutputStream stream, Object value, Cl
469482
case JSON:
470483
serializeJSON(stream, value);
471484
break;
485+
case IntervalNanosecond:
486+
case IntervalMillisecond:
487+
case IntervalSecond:
488+
case IntervalMinute:
489+
case IntervalHour:
490+
case IntervalDay:
491+
case IntervalWeek:
492+
case IntervalMonth:
493+
case IntervalQuarter:
494+
case IntervalYear:
495+
BinaryStreamUtils.writeUnsignedInt64(stream, convertToLong(value));
496+
break;
472497
default:
473498
throw new UnsupportedOperationException("Unsupported data type: " + column.getDataType());
474499
}

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,28 @@ public class DataTypesTestingPOJO {
9494
private ClickHouseBitmap groupBitmapUint32;
9595
private ClickHouseBitmap groupBitmapUint64;
9696

97+
private int intervalYear;
98+
99+
private byte intervalQuarter;
100+
101+
private byte intervalMonth;
102+
103+
private byte intervalWeek;
104+
105+
private short intervalDay;
106+
107+
private byte intervalHour;
108+
109+
private byte intervalMinute;
110+
111+
private byte intervalSecond;
112+
113+
private long intervalMillisecond;
114+
115+
private long intervalMicrosecond;
116+
117+
private BigInteger intervalNanosecond;
118+
97119
public DataTypesTestingPOJO() {
98120
final Random random = new Random();
99121
byteValue = (byte) random.nextInt();
@@ -219,6 +241,21 @@ public DataTypesTestingPOJO() {
219241
groupBitmapUint32 = ClickHouseBitmap.wrap(random.ints(5, Integer.MAX_VALUE - 100, Integer.MAX_VALUE).toArray());
220242
groupBitmapUint64 = ClickHouseBitmap.wrap(random.longs(5, Long.MAX_VALUE - 100, Long.MAX_VALUE).toArray());
221243

244+
intervalYear = random.nextInt(2000, 4000);
245+
intervalQuarter = (byte) random.nextInt(4);
246+
intervalMonth = (byte) random.nextInt(12);
247+
intervalWeek = (byte) random.nextInt(52);
248+
intervalDay = (byte) random.nextInt(30);
249+
intervalHour = (byte) random.nextInt(24);
250+
intervalMinute = (byte) random.nextInt(60);
251+
intervalSecond = (byte) random.nextInt(60);
252+
intervalMillisecond = random.nextLong(10000);
253+
intervalMicrosecond = random.nextLong(10000);
254+
255+
upper = BigInteger.valueOf(random.nextLong()).shiftLeft(64);
256+
lower = BigInteger.valueOf(random.nextLong()).and(BigInteger.valueOf(Long.MAX_VALUE));
257+
258+
intervalNanosecond = upper.or(lower);
222259
}
223260

224261
public boolean getBool() {
@@ -277,7 +314,18 @@ public static String generateTableCreateSQL(String tableName) {
277314
"nested Nested (innerInt Int32, innerString String, " +
278315
"innerNullableInt Nullable(Int32)), " +
279316
"groupBitmapUint32 AggregateFunction(groupBitmap, UInt32), " +
280-
"groupBitmapUint64 AggregateFunction(groupBitmap, UInt64) " +
317+
"groupBitmapUint64 AggregateFunction(groupBitmap, UInt64), " +
318+
"intervalYear IntervalYear, " +
319+
"intervalQuarter IntervalQuarter, " +
320+
"intervalMonth IntervalMonth, " +
321+
"intervalWeek IntervalWeek, " +
322+
"intervalDay IntervalDay, " +
323+
"intervalHour IntervalHour, " +
324+
"intervalMinute IntervalMinute, " +
325+
"intervalSecond IntervalSecond, " +
326+
"intervalMillisecond IntervalMillisecond, " +
327+
"intervalMicrosecond IntervalMicrosecond, " +
328+
"intervalNanosecond IntervalNanosecond " +
281329
") ENGINE = MergeTree ORDER BY ()";
282330
}
283331
}

0 commit comments

Comments
 (0)