Skip to content

Commit d1c90cd

Browse files
authored
Merge pull request #1790 from ClickHouse/fixing-endian
Adding a test to check, and implementing the fix
2 parents 411318e + 3db84ee commit d1c90cd

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,15 +441,15 @@ public static long readUnsignedIntLE(InputStream input) throws IOException {
441441
}
442442

443443
public static BigInteger readUnsignedInt64LE(InputStream input) throws IOException {
444-
return new BigInteger(1, readNBytes(input, 8));
444+
return readBigIntegerLE(input, 8, true);
445445
}
446446

447447
public static BigInteger readUnsignedInt128LE(InputStream input) throws IOException {
448-
return new BigInteger(1, readNBytes(input, 16));
448+
return readBigIntegerLE(input, 16, true);
449449
}
450450

451451
public static BigInteger readUnsignedInt256LE(InputStream input) throws IOException {
452-
return new BigInteger(1, readNBytes(input, 32));
452+
return readBigIntegerLE(input, 32, true);
453453
}
454454

455455
public static ZonedDateTime readDate(InputStream input, TimeZone tz) throws IOException {

client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void setUp() {
102102
.compressClientRequest(false)
103103
.compressServerResponse(useServerCompression)
104104
.useHttpCompression(useHttpCompression)
105-
.useNewImplementation(System.getProperty("client.tests.useNewImplementation", "false").equals("true"))
105+
.useNewImplementation(true)
106106
.build();
107107

108108
delayForProfiler(0);
@@ -159,6 +159,22 @@ public void testReadRecords() throws Exception {
159159
}
160160
}
161161

162+
@Test(groups = {"integration"})
163+
public void testBigUnsignedInt() throws Exception {
164+
final BigInteger expected128 = BigInteger.valueOf(2).pow(128).subtract(BigInteger.ONE).subtract(BigInteger.ONE);
165+
final BigInteger expected256 = BigInteger.valueOf(2).pow(256).subtract(BigInteger.ONE).subtract(BigInteger.ONE);
166+
167+
String sqlQuery = "SELECT toUInt128('" + expected128 + "') as i128, toUInt256('" + expected256 + "') as i256";
168+
System.out.println(sqlQuery);
169+
Records records = client.queryRecords(sqlQuery).get(3, TimeUnit.SECONDS);
170+
171+
GenericRecord firstRecord = records.iterator().next();
172+
173+
System.out.println(firstRecord.getBigInteger("i128"));
174+
Assert.assertEquals(firstRecord.getBigInteger("i128"), expected128);
175+
Assert.assertEquals(firstRecord.getBigInteger("i256"), expected256);
176+
}
177+
162178
@Test(groups = {"integration"})
163179
public void testReadRecordsGetFirstRecord() throws Exception {
164180
prepareDataSet(DATASET_TABLE, DATASET_COLUMNS, DATASET_VALUE_GENERATORS, 10);
@@ -601,30 +617,31 @@ public void testIntegerDataTypes() {
601617
columns.add("max_uint" + bits + " UInt" + bits);
602618

603619
final BigInteger minInt = BigInteger.valueOf(-1).multiply(BigInteger.valueOf(2).pow(bits - 1));
604-
final BigInteger maxInt = BigInteger.valueOf(2).pow(bits - 1).subtract(BigInteger.ONE);
605-
final BigInteger maxUInt = BigInteger.valueOf(2).pow(bits).subtract(BigInteger.ONE);
620+
final BigInteger nearMaxInt = BigInteger.valueOf(2).pow(bits - 1).subtract(BigInteger.ONE).subtract(BigInteger.ONE);//LE vs BigEndian test
621+
final BigInteger nearMaxUInt = BigInteger.valueOf(2).pow(bits).subtract(BigInteger.ONE).subtract(BigInteger.ONE);//LE vs BE
606622

607623
valueGenerators.add(() -> String.valueOf(minInt));
608624
valueGenerators.add(() -> String.valueOf(0));
609-
valueGenerators.add(() -> String.valueOf(maxInt));
610-
valueGenerators.add(() -> String.valueOf(maxUInt));
625+
valueGenerators.add(() -> String.valueOf(nearMaxInt));
626+
valueGenerators.add(() -> String.valueOf(nearMaxUInt));
611627

612628
final int index = i - 3;
613629
verifiers.add(createNumberVerifier("min_int" + bits, index * 4 + 1, bits, true,
614630
minInt));
615631
verifiers.add(createNumberVerifier("min_uint" + bits, index * 4 + 2, bits, false,
616632
BigInteger.ZERO));
617633
verifiers.add(createNumberVerifier("max_int" + bits, index * 4 + 3, bits, true,
618-
maxInt));
634+
nearMaxInt));
619635
verifiers.add(createNumberVerifier("max_uint" + bits, index * 4 + 4, bits, false,
620-
maxUInt));
636+
nearMaxUInt));
621637
}
622638

623639
// valueGenerators.forEach(r -> System.out.println(r.get()));
624640

625641
testDataTypes(columns, valueGenerators, verifiers);
626642
}
627643

644+
628645
@Test(groups = {"integration"})
629646
public void testFloatDataTypes() {
630647
final List<String> columns = Arrays.asList(

0 commit comments

Comments
 (0)