-
Notifications
You must be signed in to change notification settings - Fork 623
[client-v2] Fixed converting integers thru decimal #2814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
67f3c40
10a54b7
f1c7d4a
95face3
860ed34
a8f9de9
c7d0e8e
e1a0be6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.clickhouse.client.api.data_formats.internal; | ||
|
|
||
| import org.testng.Assert; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| public class NumberConverterTests { | ||
|
|
||
| @Test | ||
| public void testToBigDecimalSupportsGenericNumberValues() { | ||
| Assert.assertEquals(NumberConverter.toBigDecimal(new CustomNumber("1234567890.123456789")), | ||
| new BigDecimal("1234567890.123456789")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testToBigDecimalSupportsStringValues() { | ||
| Assert.assertEquals(NumberConverter.toBigDecimal("98765.4321"), new BigDecimal("98765.4321")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testToBigDecimalPreservesFractionalFloatBoundaries() { | ||
| Assert.assertEquals(NumberConverter.toBigDecimal(0.0001f).compareTo(new BigDecimal("0.0001")), 0); | ||
| Assert.assertEquals(NumberConverter.toBigDecimal(0.0256f).compareTo(new BigDecimal("0.0256")), 0); | ||
| Assert.assertEquals(NumberConverter.toBigDecimal(6.5536f).compareTo(new BigDecimal("6.5536")), 0); | ||
| Assert.assertEquals(NumberConverter.toBigDecimal(838.8608f).compareTo(new BigDecimal("838.8608")), 0); | ||
| } | ||
|
|
||
| private static final class CustomNumber extends Number { | ||
| private final BigDecimal value; | ||
|
|
||
| private CustomNumber(String value) { | ||
| this.value = new BigDecimal(value); | ||
| } | ||
|
|
||
| @Override | ||
| public int intValue() { | ||
| return value.intValue(); | ||
| } | ||
|
|
||
| @Override | ||
| public long longValue() { | ||
| return value.longValue(); | ||
| } | ||
|
|
||
| @Override | ||
| public float floatValue() { | ||
| return value.floatValue(); | ||
| } | ||
|
|
||
| @Override | ||
| public double doubleValue() { | ||
| return value.doubleValue(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return value.toPlainString(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package com.clickhouse.client.api.data_formats.internal; | ||
|
|
||
| import com.clickhouse.client.api.data_formats.RowBinaryWithNamesAndTypesFormatReader; | ||
| import com.clickhouse.client.api.query.QuerySettings; | ||
| import com.clickhouse.data.ClickHouseColumn; | ||
| import com.clickhouse.data.format.BinaryStreamUtils; | ||
| import org.testng.Assert; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.math.BigDecimal; | ||
| import java.util.TimeZone; | ||
|
|
||
| public class SerializerUtilsPrimitiveSerializationTests { | ||
|
|
||
| @Test | ||
| public void testSerializeIntegerTargetFromStringValue() throws IOException { | ||
| RowBinaryWithNamesAndTypesFormatReader reader = | ||
| serializeSingleValue("Int32", "123456"); | ||
|
|
||
| reader.next(); | ||
|
|
||
| Assert.assertEquals(reader.getInteger("value"), Integer.valueOf(123456)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSerializeDecimalTargetFromStringValue() throws IOException { | ||
| RowBinaryWithNamesAndTypesFormatReader reader = | ||
| serializeSingleValue("Decimal64(3)", "123456.789"); | ||
|
|
||
| reader.next(); | ||
|
|
||
| Assert.assertEquals(reader.getBigDecimal("value"), new BigDecimal("123456.789")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSerializeDecimalTargetFromGenericNumberValue() throws IOException { | ||
| RowBinaryWithNamesAndTypesFormatReader reader = | ||
| serializeSingleValue("Decimal64(3)", new CustomNumber("987654.321")); | ||
|
|
||
| reader.next(); | ||
|
|
||
| Assert.assertEquals(reader.getBigDecimal("value"), new BigDecimal("987654.321")); | ||
| } | ||
|
|
||
| private RowBinaryWithNamesAndTypesFormatReader serializeSingleValue(String type, Object value) | ||
| throws IOException { | ||
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| BinaryStreamUtils.writeVarInt(out, 1); | ||
|
Check warning on line 51 in client-v2/src/test/java/com/clickhouse/client/api/data_formats/internal/SerializerUtilsPrimitiveSerializationTests.java
|
||
| BinaryStreamUtils.writeString(out, "value"); | ||
|
Check warning on line 52 in client-v2/src/test/java/com/clickhouse/client/api/data_formats/internal/SerializerUtilsPrimitiveSerializationTests.java
|
||
| BinaryStreamUtils.writeString(out, type); | ||
|
Check warning on line 53 in client-v2/src/test/java/com/clickhouse/client/api/data_formats/internal/SerializerUtilsPrimitiveSerializationTests.java
|
||
| SerializerUtils.serializeData(out, value, ClickHouseColumn.of("value", type)); | ||
|
|
||
| return new RowBinaryWithNamesAndTypesFormatReader( | ||
| new ByteArrayInputStream(out.toByteArray()), | ||
| new QuerySettings().setUseTimeZone(TimeZone.getTimeZone("UTC").toZoneId().getId()), | ||
| new BinaryStreamReader.CachingByteBufferAllocator()); | ||
| } | ||
|
|
||
| private static final class CustomNumber extends Number { | ||
| private final BigDecimal value; | ||
|
|
||
| private CustomNumber(String value) { | ||
| this.value = new BigDecimal(value); | ||
| } | ||
|
|
||
| @Override | ||
| public int intValue() { | ||
| return value.intValue(); | ||
| } | ||
|
|
||
| @Override | ||
| public long longValue() { | ||
| return value.longValue(); | ||
| } | ||
|
|
||
| @Override | ||
| public float floatValue() { | ||
| return value.floatValue(); | ||
| } | ||
|
|
||
| @Override | ||
| public double doubleValue() { | ||
| return value.doubleValue(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return value.toPlainString(); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.