Skip to content

Commit c8cd6c3

Browse files
authored
Merge pull request #414 from arenadata/add_decimal_rowbinary_stream
add tests and add bytes to decimal convert in binary input stream
2 parents af43112 + bed348f commit c8cd6c3

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

src/main/java/ru/yandex/clickhouse/util/ClickHouseRowBinaryInputStream.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.EOFException;
1010
import java.io.IOException;
1111
import java.io.InputStream;
12+
import java.math.BigDecimal;
1213
import java.math.BigInteger;
1314
import java.nio.ByteBuffer;
1415
import java.nio.ByteOrder;
@@ -386,6 +387,29 @@ public UUID readUUID() throws IOException {
386387
return new UUID(bb.getLong(), bb.getLong());
387388
}
388389

390+
public BigDecimal readDecimal32(int scale) throws IOException {
391+
int i = in.readInt();
392+
BigDecimal ten = BigDecimal.valueOf(10);
393+
BigDecimal s = ten.pow(scale);
394+
return new BigDecimal(i).divide(s);
395+
}
396+
397+
public BigDecimal readDecimal64(int scale) throws IOException {
398+
long i = in.readLong();
399+
BigDecimal ten = BigDecimal.valueOf(10);
400+
BigDecimal s = ten.pow(scale);
401+
return new BigDecimal(i).divide(s);
402+
}
403+
404+
public BigDecimal readDecimal128(int scale) throws IOException {
405+
byte[] r = new byte[16];
406+
for (int i = 16; i > 0; i--) {
407+
r[i - 1] = in.readByte();
408+
}
409+
BigDecimal res = new BigDecimal(new BigInteger(r), scale);
410+
return res;
411+
}
412+
389413
@Override
390414
public void close() throws IOException {
391415
in.close();

src/test/java/ru/yandex/clickhouse/util/ClickHouseRowBinaryInputStreamTest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import ru.yandex.clickhouse.settings.ClickHouseProperties;
66

77
import java.io.ByteArrayInputStream;
8-
import java.io.IOException;
9-
import java.nio.charset.Charset;
8+
import java.math.BigDecimal;
109
import java.util.Date;
1110
import java.util.TimeZone;
1211

@@ -80,6 +79,26 @@ public void testUInt64AsLong() throws Exception {
8079
assertEquals(input.readUInt64AsLong(), UnsignedLong.valueOf("18446744073709551615").longValue());
8180
}
8281

82+
@Test
83+
public void testDecimal128() throws Exception {
84+
ClickHouseRowBinaryInputStream input = prepareStream(new byte[]{-10, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
85+
assertEquals(input.readDecimal128(3), new BigDecimal("10.230"));
86+
ClickHouseRowBinaryInputStream input2 = prepareStream(new byte[]{-2, 127, -58, -92, 126, -115, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0});
87+
assertEquals(input2.readDecimal128(2), new BigDecimal("9999999999999.98"));
88+
}
89+
90+
@Test
91+
public void testDecimal64() throws Exception {
92+
ClickHouseRowBinaryInputStream input = prepareStream(new byte[]{-10, 39, 0, 0, 0, 0, 0, 0});
93+
assertEquals(input.readDecimal64(3), new BigDecimal("10.23"));
94+
}
95+
96+
@Test
97+
public void testDecimal32() throws Exception {
98+
ClickHouseRowBinaryInputStream input = prepareStream(new byte[]{-10, 39, 0, 0});
99+
assertEquals(input.readDecimal32(3), new BigDecimal("10.23"));
100+
}
101+
83102
@Test
84103
public void testFixedString() throws Exception {
85104
ClickHouseRowBinaryInputStream input = prepareStream(new byte[]{48, 49, 48, 49, 55, 49, 50, 50, 48, 48});

src/test/java/ru/yandex/clickhouse/util/ClickHouseRowBinaryStreamTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import ru.yandex.clickhouse.settings.ClickHouseProperties;
77

88
import java.io.ByteArrayOutputStream;
9+
import java.math.BigDecimal;
910
import java.util.Date;
1011
import java.util.TimeZone;
1112
import java.util.UUID;
@@ -76,6 +77,47 @@ public void write(ClickHouseRowBinaryStream stream) throws Exception {
7677
);
7778
}
7879

80+
@Test
81+
public void testDecimal32() throws Exception {
82+
check(
83+
new StreamWriter() {
84+
@Override
85+
public void write(ClickHouseRowBinaryStream stream) throws Exception {
86+
stream.writeDecimal32(new BigDecimal(10.23), 3);
87+
stream.writeDecimal32(new BigDecimal(-99999.9998), 4);
88+
}
89+
},
90+
new byte[]{-10, 39, 0, 0, 2, 54, 101, -60}
91+
);
92+
}
93+
94+
@Test
95+
public void testDecimal64() throws Exception {
96+
check(
97+
new StreamWriter() {
98+
@Override
99+
public void write(ClickHouseRowBinaryStream stream) throws Exception {
100+
stream.writeDecimal64(new BigDecimal(10.23), 3);
101+
stream.writeDecimal64(new BigDecimal(-9999999999.99999998), 8);
102+
}
103+
},
104+
new byte[]{-10, 39, 0, 0, 0, 0, 0, 0, 0, 0, -100, 88, 76, 73, 31, -14}
105+
);
106+
}
107+
108+
@Test
109+
public void testDecimal128() throws Exception {
110+
check(
111+
new StreamWriter() {
112+
@Override
113+
public void write(ClickHouseRowBinaryStream stream) throws Exception {
114+
stream.writeDecimal128(new BigDecimal(10.23), 3);
115+
}
116+
},
117+
new byte[]{-10, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
118+
);
119+
}
120+
79121
@Test
80122
public void testOne() throws Exception {
81123

0 commit comments

Comments
 (0)