Skip to content

Commit

Permalink
Merge pull request #1790 from ClickHouse/fixing-endian
Browse files Browse the repository at this point in the history
Adding a test to check, and implementing the fix
  • Loading branch information
Paultagoras authored Aug 26, 2024
2 parents 411318e + 3db84ee commit d1c90cd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,15 @@ public static long readUnsignedIntLE(InputStream input) throws IOException {
}

public static BigInteger readUnsignedInt64LE(InputStream input) throws IOException {
return new BigInteger(1, readNBytes(input, 8));
return readBigIntegerLE(input, 8, true);
}

public static BigInteger readUnsignedInt128LE(InputStream input) throws IOException {
return new BigInteger(1, readNBytes(input, 16));
return readBigIntegerLE(input, 16, true);
}

public static BigInteger readUnsignedInt256LE(InputStream input) throws IOException {
return new BigInteger(1, readNBytes(input, 32));
return readBigIntegerLE(input, 32, true);
}

public static ZonedDateTime readDate(InputStream input, TimeZone tz) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void setUp() {
.compressClientRequest(false)
.compressServerResponse(useServerCompression)
.useHttpCompression(useHttpCompression)
.useNewImplementation(System.getProperty("client.tests.useNewImplementation", "false").equals("true"))
.useNewImplementation(true)
.build();

delayForProfiler(0);
Expand Down Expand Up @@ -159,6 +159,22 @@ public void testReadRecords() throws Exception {
}
}

@Test(groups = {"integration"})
public void testBigUnsignedInt() throws Exception {
final BigInteger expected128 = BigInteger.valueOf(2).pow(128).subtract(BigInteger.ONE).subtract(BigInteger.ONE);
final BigInteger expected256 = BigInteger.valueOf(2).pow(256).subtract(BigInteger.ONE).subtract(BigInteger.ONE);

String sqlQuery = "SELECT toUInt128('" + expected128 + "') as i128, toUInt256('" + expected256 + "') as i256";
System.out.println(sqlQuery);
Records records = client.queryRecords(sqlQuery).get(3, TimeUnit.SECONDS);

GenericRecord firstRecord = records.iterator().next();

System.out.println(firstRecord.getBigInteger("i128"));
Assert.assertEquals(firstRecord.getBigInteger("i128"), expected128);
Assert.assertEquals(firstRecord.getBigInteger("i256"), expected256);
}

@Test(groups = {"integration"})
public void testReadRecordsGetFirstRecord() throws Exception {
prepareDataSet(DATASET_TABLE, DATASET_COLUMNS, DATASET_VALUE_GENERATORS, 10);
Expand Down Expand Up @@ -601,30 +617,31 @@ public void testIntegerDataTypes() {
columns.add("max_uint" + bits + " UInt" + bits);

final BigInteger minInt = BigInteger.valueOf(-1).multiply(BigInteger.valueOf(2).pow(bits - 1));
final BigInteger maxInt = BigInteger.valueOf(2).pow(bits - 1).subtract(BigInteger.ONE);
final BigInteger maxUInt = BigInteger.valueOf(2).pow(bits).subtract(BigInteger.ONE);
final BigInteger nearMaxInt = BigInteger.valueOf(2).pow(bits - 1).subtract(BigInteger.ONE).subtract(BigInteger.ONE);//LE vs BigEndian test
final BigInteger nearMaxUInt = BigInteger.valueOf(2).pow(bits).subtract(BigInteger.ONE).subtract(BigInteger.ONE);//LE vs BE

valueGenerators.add(() -> String.valueOf(minInt));
valueGenerators.add(() -> String.valueOf(0));
valueGenerators.add(() -> String.valueOf(maxInt));
valueGenerators.add(() -> String.valueOf(maxUInt));
valueGenerators.add(() -> String.valueOf(nearMaxInt));
valueGenerators.add(() -> String.valueOf(nearMaxUInt));

final int index = i - 3;
verifiers.add(createNumberVerifier("min_int" + bits, index * 4 + 1, bits, true,
minInt));
verifiers.add(createNumberVerifier("min_uint" + bits, index * 4 + 2, bits, false,
BigInteger.ZERO));
verifiers.add(createNumberVerifier("max_int" + bits, index * 4 + 3, bits, true,
maxInt));
nearMaxInt));
verifiers.add(createNumberVerifier("max_uint" + bits, index * 4 + 4, bits, false,
maxUInt));
nearMaxUInt));
}

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

testDataTypes(columns, valueGenerators, verifiers);
}


@Test(groups = {"integration"})
public void testFloatDataTypes() {
final List<String> columns = Arrays.asList(
Expand Down

0 comments on commit d1c90cd

Please sign in to comment.