Skip to content

Commit 20ad646

Browse files
committed
improved tests with number conversion and overflow check
1 parent 8f4b5fa commit 20ad646

File tree

4 files changed

+117
-94
lines changed

4 files changed

+117
-94
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ protected void setSchema(TableSchema schema) {
247247
case UInt256:
248248
case Float32:
249249
case Float64:
250+
case Decimal:
251+
case Decimal32:
252+
case Decimal64:
253+
case Decimal128:
254+
case Decimal256:
250255
case Bool:
251256
converters.put(NumberType.Byte, SerializerUtils.NumberConverter::toByte);
252257
converters.put(NumberType.Short, SerializerUtils.NumberConverter::toShort);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.clickhouse.client.api;
2+
3+
import org.testng.annotations.Test;
4+
5+
import java.time.LocalDateTime;
6+
7+
import static org.testng.AssertJUnit.assertEquals;
8+
9+
public class DataTypeTests {
10+
11+
12+
@Test
13+
public void testDateTimeFormatter() {
14+
LocalDateTime dateTime = LocalDateTime.of(2021, 12, 31, 23, 59, 59);
15+
String formattedDateTime = dateTime.format(DataTypeUtils.DATETIME_FORMATTER);
16+
assertEquals("2021-12-31 23:59:59", formattedDateTime);
17+
}
18+
19+
@Test
20+
public void testDateFormatter() {
21+
LocalDateTime date = LocalDateTime.of(2021, 12, 31, 10, 20);
22+
String formattedDate = date.toLocalDate().format(DataTypeUtils.DATE_FORMATTER);
23+
assertEquals("2021-12-31", formattedDate);
24+
}
25+
26+
@Test
27+
public void testDateTimeWithNanosFormatter() {
28+
LocalDateTime dateTime = LocalDateTime.of(2021, 12, 31, 23, 59, 59, 123456789);
29+
String formattedDateTimeWithNanos = dateTime.format(DataTypeUtils.DATETIME_WITH_NANOS_FORMATTER);
30+
assertEquals("2021-12-31 23:59:59.123456789", formattedDateTimeWithNanos);
31+
}
32+
}

client-v2/src/test/java/com/clickhouse/client/api/data_formats/ClickHouseBinaryFormatReaderTest.java

Lines changed: 72 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,23 @@
1212
import java.io.InputStream;
1313
import java.math.BigDecimal;
1414
import java.math.BigInteger;
15+
import java.util.Arrays;
1516
import java.util.TimeZone;
1617
import java.util.function.BiConsumer;
18+
import java.util.function.Consumer;
1719

1820
public class ClickHouseBinaryFormatReaderTest {
21+
1922
@Test
2023
public void testReadingNumbers() throws IOException {
2124
ByteArrayOutputStream out = new ByteArrayOutputStream();
2225

23-
String[] names = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
24-
String[] types = new String[]{"Int8", "Int16", "Int32", "Int64", "UInt8", "UInt16", "UInt32", "UInt64", "Float32", "Float64"};
26+
String[] names = new String[]{ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
27+
"q", "r"};
28+
String[] types = new String[]{"Int8", "Int16", "Int32", "Int64", "UInt8", "UInt16", "UInt32", "UInt64",
29+
"Int128", "Int256", "UInt128", "UInt256", "Float32", "Float64",
30+
"Decimal32(3)", "Decimal64(3)", "Decimal128(4)", "Decimal256(4)"};
31+
2532

2633
BinaryStreamUtils.writeVarInt(out, names.length);
2734
for (String name : names) {
@@ -31,17 +38,25 @@ public void testReadingNumbers() throws IOException {
3138
BinaryStreamUtils.writeString(out, type);
3239
}
3340

34-
BinaryStreamUtils.writeInt8(out, 1);
35-
BinaryStreamUtils.writeInt16(out, 2);
36-
BinaryStreamUtils.writeInt32(out, 3);
37-
BinaryStreamUtils.writeInt64(out, 4);
38-
BinaryStreamUtils.writeUnsignedInt8(out, 5);
39-
BinaryStreamUtils.writeUnsignedInt16(out, 6);
40-
BinaryStreamUtils.writeUnsignedInt32(out, 7);
41-
BinaryStreamUtils.writeUnsignedInt64(out, 8);
42-
BinaryStreamUtils.writeFloat32(out, 9.0f);
43-
BinaryStreamUtils.writeFloat64(out, 10.0);
44-
41+
final int testValue = 120;
42+
BinaryStreamUtils.writeInt8(out, testValue);
43+
BinaryStreamUtils.writeInt16(out, testValue);
44+
BinaryStreamUtils.writeInt32(out, testValue);
45+
BinaryStreamUtils.writeInt64(out, testValue);
46+
BinaryStreamUtils.writeUnsignedInt8(out, testValue);
47+
BinaryStreamUtils.writeUnsignedInt16(out, testValue);
48+
BinaryStreamUtils.writeUnsignedInt32(out, testValue);
49+
BinaryStreamUtils.writeUnsignedInt64(out, testValue);
50+
BinaryStreamUtils.writeInt128( out, new BigInteger(String.valueOf(testValue )));
51+
BinaryStreamUtils.writeInt256( out, new BigInteger(String.valueOf(testValue )));
52+
BinaryStreamUtils.writeUnsignedInt128(out, new BigInteger(String.valueOf(testValue )));
53+
BinaryStreamUtils.writeUnsignedInt256(out, new BigInteger(String.valueOf(testValue )));
54+
BinaryStreamUtils.writeFloat32(out, testValue);
55+
BinaryStreamUtils.writeFloat64(out, testValue);
56+
BinaryStreamUtils.writeDecimal32(out, BigDecimal.valueOf(testValue), 3);
57+
BinaryStreamUtils.writeDecimal64(out, BigDecimal.valueOf(testValue), 3);
58+
BinaryStreamUtils.writeDecimal128(out, BigDecimal.valueOf(testValue), 4);
59+
BinaryStreamUtils.writeDecimal256(out, BigDecimal.valueOf(testValue), 4);
4560

4661
InputStream in = new ByteArrayInputStream(out.toByteArray());
4762
QuerySettings querySettings = new QuerySettings().setUseTimeZone(TimeZone.getTimeZone("UTC").toZoneId().getId());
@@ -53,46 +68,28 @@ public void testReadingNumbers() throws IOException {
5368
for (int i = 0; i < names.length; i++) {
5469
String name = names[i];
5570
Assert.assertEquals(reader.getBoolean(name), Boolean.TRUE);
71+
Assert.assertEquals(reader.getByte(name), (byte)testValue);
72+
Assert.assertEquals(reader.getShort(name), (short)testValue);
73+
Assert.assertEquals(reader.getInteger(name), (int)testValue);
74+
Assert.assertEquals(reader.getLong(name), (long)testValue);
5675

57-
if (types[i].equalsIgnoreCase("int8")) {
58-
Assert.assertEquals(reader.getByte(name), (i + 1));
59-
}
60-
if (types[i].equalsIgnoreCase("int8") || types[i].equalsIgnoreCase("int16")) {
61-
Assert.assertEquals(reader.getShort(name), (i + 1));
62-
}
63-
64-
if (types[i].equalsIgnoreCase("int8") || types[i].equalsIgnoreCase("int16") || types[i].equalsIgnoreCase("int32")) {
65-
Assert.assertEquals(reader.getInteger(name), (i + 1));
66-
}
67-
68-
if (types[i].equalsIgnoreCase("int8") || types[i].equalsIgnoreCase("int16") || types[i].equalsIgnoreCase("int32") || types[i].equalsIgnoreCase("int64")) {
69-
Assert.assertEquals(reader.getLong(name), (i + 1));
70-
}
71-
72-
if (types[i].equalsIgnoreCase("int8") || types[i].equalsIgnoreCase("int16") || types[i].equalsIgnoreCase("int32") || types[i].equalsIgnoreCase("int64") || types[i].equalsIgnoreCase("uint8")) {
73-
Assert.assertEquals(reader.getFloat(name), (i + 1));
74-
}
76+
Assert.assertEquals(reader.getFloat(name), (float) testValue);
77+
Assert.assertEquals(reader.getDouble(name), (double) testValue);
78+
Assert.assertEquals(reader.getBigInteger(name), BigInteger.valueOf((testValue)));
7579

76-
if (types[i].equalsIgnoreCase("int8") || types[i].equalsIgnoreCase("int16") || types[i].equalsIgnoreCase("int32") || types[i].equalsIgnoreCase("int64") || types[i].equalsIgnoreCase("uint8") || types[i].equalsIgnoreCase("uint16")) {
77-
Assert.assertEquals(reader.getDouble(name), (i + 1));
78-
}
79-
80-
if (types[i].equalsIgnoreCase("int8") || types[i].equalsIgnoreCase("int16") || types[i].equalsIgnoreCase("int32") || types[i].equalsIgnoreCase("int64") || types[i].equalsIgnoreCase("uint8") || types[i].equalsIgnoreCase("uint16") || types[i].equalsIgnoreCase("uint32")) {
81-
Assert.assertEquals(reader.getBigInteger(name), BigInteger.valueOf((i + 1)));
82-
}
83-
84-
if (types[i].equalsIgnoreCase("int8") || types[i].equalsIgnoreCase("int16") || types[i].equalsIgnoreCase("int32") || types[i].equalsIgnoreCase("int64") || types[i].equalsIgnoreCase("uint8") || types[i].equalsIgnoreCase("uint16") || types[i].equalsIgnoreCase("uint32") || types[i].equalsIgnoreCase("uint64")) {
85-
Assert.assertTrue(reader.getBigDecimal(name).compareTo(BigDecimal.valueOf((i+ 1.0f))) == 0);
86-
}
80+
Assert.assertTrue(reader.getBigDecimal(name).compareTo(BigDecimal.valueOf((testValue))) == 0);
8781
}
8882
}
8983

9084
@Test
9185
public void testReadingNumbersWithOverflow() throws IOException {
9286
ByteArrayOutputStream out = new ByteArrayOutputStream();
9387

94-
String[] names = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
95-
String[] types = new String[]{"Int8", "Int16", "Int32", "Int64", "UInt8", "UInt16", "UInt32", "UInt64", "Float32", "Float64"};
88+
String[] names = new String[]{ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
89+
"q", "r"};
90+
String[] types = new String[]{"Int8", "Int16", "Int32", "Int64", "UInt8", "UInt16", "UInt32", "UInt64",
91+
"Int128", "Int256", "UInt128", "UInt256", "Float32", "Float64",
92+
"Decimal32(3)", "Decimal64(3)", "Decimal128(4)", "Decimal256(4)"};
9693

9794
BinaryStreamUtils.writeVarInt(out, names.length);
9895
for (String name : names) {
@@ -102,16 +99,24 @@ public void testReadingNumbersWithOverflow() throws IOException {
10299
BinaryStreamUtils.writeString(out, type);
103100
}
104101

105-
BinaryStreamUtils.writeInt8(out, 127); // Max value for Int8
106-
BinaryStreamUtils.writeInt16(out, 2000);
107-
BinaryStreamUtils.writeInt32(out, 300000);
108-
BinaryStreamUtils.writeInt64(out, 4000000000L);
109-
BinaryStreamUtils.writeUnsignedInt8(out, 255);
110-
BinaryStreamUtils.writeUnsignedInt16(out, 60000);
111-
BinaryStreamUtils.writeUnsignedInt32(out, 4000000000L);
112-
BinaryStreamUtils.writeUnsignedInt64(out, new BigInteger("18000044073709551615"));
113-
BinaryStreamUtils.writeFloat32(out, 9.0f);
114-
BinaryStreamUtils.writeFloat64(out, 10.0);
102+
BinaryStreamUtils.writeInt8(out, 127); // a
103+
BinaryStreamUtils.writeInt16(out, 2000); // b
104+
BinaryStreamUtils.writeInt32(out, 300000); // c
105+
BinaryStreamUtils.writeInt64(out, 4000000000L); // d
106+
BinaryStreamUtils.writeUnsignedInt8(out, 255); // e
107+
BinaryStreamUtils.writeUnsignedInt16(out, 60000); // f
108+
BinaryStreamUtils.writeUnsignedInt32(out, 4000000000L); // g
109+
BinaryStreamUtils.writeUnsignedInt64(out, new BigInteger("18000044073709551615")); // h
110+
BinaryStreamUtils.writeInt128(out, new BigInteger("18000044073709551615")); // i
111+
BinaryStreamUtils.writeInt256(out, new BigInteger("18000044073709551615")); // j
112+
BinaryStreamUtils.writeUnsignedInt128(out, new BigInteger("18000044073709551615")); // k
113+
BinaryStreamUtils.writeUnsignedInt256(out, new BigInteger("18000044073709551615")); // l
114+
BinaryStreamUtils.writeFloat32(out, 900000.123f); // m
115+
BinaryStreamUtils.writeFloat64(out, 1000000.333); // n
116+
BinaryStreamUtils.writeDecimal32(out, BigDecimal.valueOf(100000), 3); // o
117+
BinaryStreamUtils.writeDecimal64(out, BigDecimal.valueOf(10000000.10000), 3); // p
118+
BinaryStreamUtils.writeDecimal128(out, BigDecimal.valueOf(1000000000.1000000), 4); // q
119+
BinaryStreamUtils.writeDecimal256(out, BigDecimal.valueOf(1000000000.1000000), 4); // r
115120

116121

117122
InputStream in = new ByteArrayInputStream(out.toByteArray());
@@ -121,25 +126,18 @@ public void testReadingNumbersWithOverflow() throws IOException {
121126

122127
reader.next();
123128

124-
for (int i = 0; i < names.length; i++) {
125-
String name = names[i];
126-
String type = types[i];
127-
128-
if (type.equalsIgnoreCase("int16")) {
129-
Assert.expectThrows(ArithmeticException.class, () -> reader.getByte(name)) ;
130-
} else if (type.equalsIgnoreCase("int32")) {
131-
Assert.expectThrows(ArithmeticException.class, () -> reader.getShort(name)) ;
132-
} else if (type.equalsIgnoreCase("int64")) {
133-
Assert.expectThrows(ArithmeticException.class, () -> reader.getInteger(name)) ;
134-
} else if (type.equalsIgnoreCase("uint8")) {
135-
Assert.expectThrows(ArithmeticException.class, () -> reader.getByte(name)) ;
136-
} else if (type.equalsIgnoreCase("uint16")) {
137-
Assert.expectThrows(ArithmeticException.class, () -> reader.getShort(name)) ;
138-
} else if (type.equalsIgnoreCase("uint32")) {
139-
Assert.expectThrows(ArithmeticException.class, () -> reader.getInteger(name)) ;
140-
} else if (type.equalsIgnoreCase("uint64")) {
141-
Assert.expectThrows(ArithmeticException.class, () -> reader.getLong(name)) ;
142-
}
143-
}
129+
Consumer<String> byteConsumer = name -> Assert.expectThrows(ArithmeticException.class, () -> reader.getByte(name));
130+
Consumer<String> shortConsumer = name -> Assert.expectThrows(ArithmeticException.class, () -> reader.getShort(name));
131+
Consumer<String> integerConsumer = name -> Assert.expectThrows(ArithmeticException.class, () -> reader.getInteger(name));
132+
Consumer<String> longConsumer = name -> Assert.expectThrows(ArithmeticException.class, () -> reader.getLong(name));
133+
Consumer<String> floatConsumer = name -> Assert.expectThrows(ArithmeticException.class, () -> reader.getFloat(name));
134+
Consumer<String> doubleConsumer = name -> Assert.expectThrows(ArithmeticException.class, () -> reader.getDouble(name));
135+
136+
Arrays.stream("b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r".split(",")).forEach(byteConsumer);
137+
Arrays.stream("c,d,f,g,h,i,j,k,l,m,n,o,p,q,r".split(",")).forEach(shortConsumer);
138+
Arrays.stream("d,g,h,i,j,k,l".split(",")).forEach(integerConsumer);
139+
Arrays.stream("h,i,j,k,l".split(",")).forEach(longConsumer);
140+
Arrays.stream("h,i,j,k,l,n,p,q,r".split(",")).forEach(floatConsumer);
141+
Arrays.stream("h,i,j,k,l,p,q,r".split(",")).forEach(doubleConsumer);
144142
}
145143
}
Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
11
package com.clickhouse.client.internal;
22

3-
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
4-
import com.clickhouse.client.api.query.POJOSetter;
5-
import com.clickhouse.client.query.QuerySamplePOJO;
6-
import com.clickhouse.client.query.SimplePOJO;
7-
import com.clickhouse.data.ClickHouseColumn;
3+
import com.clickhouse.client.api.internal.SerializerUtils;
4+
import org.testng.annotations.Test;
85

9-
import java.io.IOException;
10-
import java.time.LocalDateTime;
11-
import java.time.ZonedDateTime;
6+
import static org.testng.AssertJUnit.assertEquals;
127

138
public class SerializerUtilsTests {
149

15-
16-
public static class SamplePOJOInt256Setter implements POJOSetter {
17-
18-
@Override
19-
public void setValue(Object obj, BinaryStreamReader reader, ClickHouseColumn column) throws IOException {
20-
((QuerySamplePOJO)obj).setDateTime(((ZonedDateTime)reader.readValue(column)).toLocalDateTime());
21-
}
22-
23-
public void readValue(Object obj, BinaryStreamReader reader, ClickHouseColumn column) throws IOException {
24-
// ((SamplePOJO)obj).setDateTime(((ZonedDateTime)reader.readValue(column)).toLocalDateTime());
25-
((SimplePOJO)obj).setId(reader.readIntLE());
26-
}
10+
@Test
11+
public void testConvertToInteger() {
12+
int expected = 1640995199; // Unix timestamp for the given date
13+
assertEquals(expected, SerializerUtils.convertToInteger("1640995199").intValue());
14+
assertEquals(0, SerializerUtils.convertToInteger(false).intValue());
2715
}
2816
}

0 commit comments

Comments
 (0)