Skip to content

Commit

Permalink
Add default rounding mode as suggested in #864
Browse files Browse the repository at this point in the history
  • Loading branch information
zhicwu committed Mar 14, 2022
1 parent 37d261b commit 772d304
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
Expand All @@ -27,6 +28,7 @@
import java.util.UUID;
import java.util.Map.Entry;

import com.clickhouse.client.config.ClickHouseDefaults;
import com.clickhouse.client.data.ClickHouseArrayValue;
import com.clickhouse.client.data.ClickHouseBigDecimalValue;
import com.clickhouse.client.data.ClickHouseBigIntegerValue;
Expand Down Expand Up @@ -83,6 +85,8 @@ public final class ClickHouseValues {
public static final String EMPTY_STRING_EXPR = "''";

public static final BigDecimal NANOS = new BigDecimal(BigInteger.TEN.pow(9));
public static final RoundingMode ROUNDING_MODE = (RoundingMode) ClickHouseDefaults.ROUNDING_MODE
.getEffectiveDefaultValue();

public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static final DateTimeFormatter TIME_FORMATTER = new DateTimeFormatterBuilder().appendPattern("HH:mm:ss")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.clickhouse.client.config;

import java.io.Serializable;
import java.math.RoundingMode;

import com.clickhouse.client.ClickHouseChecker;
import com.clickhouse.client.ClickHouseFormat;
Expand Down Expand Up @@ -69,6 +70,10 @@ public enum ClickHouseDefaults implements ClickHouseOption {
* Max requests.
*/
MAX_REQUESTS("max_requests", 0, "Maximum size of shared thread pool, 0 means no limit."),
/**
* Rounding mode for type conversion.
*/
ROUNDING_MODE("rounding_mode", RoundingMode.DOWN, "Default rounding mode for BigDecimal."),
/**
* Thread keep alive timeout in milliseconds.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.time.LocalDate;
Expand Down Expand Up @@ -124,7 +123,7 @@ public BigDecimal asBigDecimal() {
public BigDecimal asBigDecimal(int scale) {
BigDecimal v = getValue();
if (v != null && v.scale() != scale) {
v = v.setScale(scale, RoundingMode.DOWN);
v = v.setScale(scale, ClickHouseValues.ROUNDING_MODE);
}

return v;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
Expand Down Expand Up @@ -171,7 +170,7 @@ public BigDecimal asBigDecimal(int scale) {
v = new BigDecimal(BigInteger.valueOf(value.atZone(tz.toZoneId()).toEpochSecond()), scale);
if (scale != 0 && nanoSeconds != 0) {
v = v.add(BigDecimal.valueOf(nanoSeconds).divide(ClickHouseValues.NANOS).setScale(scale,
RoundingMode.HALF_UP));
ClickHouseValues.ROUNDING_MODE));
}
}
return v;
Expand Down Expand Up @@ -290,7 +289,7 @@ public ClickHouseDateTimeValue update(BigDecimal value) {
resetToNullOrEmpty();
} else {
if (value.scale() != scale) {
value = value.setScale(scale, RoundingMode.HALF_UP);
value = value.setScale(scale, ClickHouseValues.ROUNDING_MODE);
}
set(ClickHouseValues.convertToDateTime(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

Expand Down Expand Up @@ -31,8 +30,7 @@ public static ClickHouseDoubleValue ofNull() {
* @return same object as {@code ref} or a new instance if it's null
*/
public static ClickHouseDoubleValue ofNull(ClickHouseValue ref) {
return ref instanceof ClickHouseDoubleValue ?
((ClickHouseDoubleValue) ref).set(true, 0D)
return ref instanceof ClickHouseDoubleValue ? ((ClickHouseDoubleValue) ref).set(true, 0D)
: new ClickHouseDoubleValue(true, 0D);
}

Expand All @@ -55,8 +53,7 @@ public static ClickHouseDoubleValue of(double value) {
* @return same object as {@code ref} or a new instance if it's null
*/
public static ClickHouseDoubleValue of(ClickHouseValue ref, double value) {
return ref instanceof ClickHouseDoubleValue ?
((ClickHouseDoubleValue) ref).set(false, value)
return ref instanceof ClickHouseDoubleValue ? ((ClickHouseDoubleValue) ref).set(false, value)
: new ClickHouseDoubleValue(false, value);
}

Expand Down Expand Up @@ -142,7 +139,7 @@ public BigDecimal asBigDecimal(int scale) {
if (diff > 0) {
dec = dec.divide(BigDecimal.TEN.pow(diff + 1));
} else if (diff < 0) {
dec = dec.setScale(scale, RoundingMode.DOWN);
dec = dec.setScale(scale, ClickHouseValues.ROUNDING_MODE);
}
}
return dec;
Expand All @@ -160,7 +157,8 @@ public String asString(int length, Charset charset) {
}
String str = String.valueOf(value);
if (length > 0) {
ClickHouseChecker.notWithDifferentLength(str.getBytes(charset == null ? StandardCharsets.UTF_8 : charset), length);
ClickHouseChecker.notWithDifferentLength(str.getBytes(charset == null ? StandardCharsets.UTF_8 : charset),
length);
}
return str;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import com.clickhouse.client.ClickHouseChecker;
Expand Down Expand Up @@ -158,7 +157,7 @@ public BigDecimal asBigDecimal(int scale) {
if (diff > 0) {
dec = dec.divide(BigDecimal.TEN.pow(diff + 1));
} else if (diff < 0) {
dec = dec.setScale(scale, RoundingMode.DOWN);
dec = dec.setScale(scale, ClickHouseValues.ROUNDING_MODE);
}
}
return dec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
Expand Down Expand Up @@ -167,7 +166,7 @@ public BigDecimal asBigDecimal(int scale) {
v = new BigDecimal(BigInteger.valueOf(value.getEpochSecond()), scale);
if (scale != 0 && nanoSeconds != 0) {
v = v.add(BigDecimal.valueOf(nanoSeconds).divide(ClickHouseValues.NANOS).setScale(scale,
RoundingMode.HALF_UP));
ClickHouseValues.ROUNDING_MODE));
}
}
return v;
Expand Down Expand Up @@ -292,7 +291,7 @@ public ClickHouseInstantValue update(BigDecimal value) {
resetToNullOrEmpty();
} else {
if (value.scale() != scale) {
value = value.setScale(scale, RoundingMode.HALF_UP);
value = value.setScale(scale, ClickHouseValues.ROUNDING_MODE);
}
set(ClickHouseValues.convertToInstant(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
Expand Down Expand Up @@ -145,7 +144,7 @@ public BigDecimal asBigDecimal(int scale) {
v = new BigDecimal(BigInteger.valueOf(value.toEpochSecond()), scale);
if (scale != 0 && nanoSeconds != 0) {
v = v.add(BigDecimal.valueOf(nanoSeconds).divide(ClickHouseValues.NANOS).setScale(scale,
RoundingMode.HALF_UP));
ClickHouseValues.ROUNDING_MODE));
}
}
return v;
Expand Down Expand Up @@ -275,7 +274,7 @@ public ClickHouseOffsetDateTimeValue update(BigDecimal value) {
resetToNullOrEmpty();
} else {
if (value.scale() != scale) {
value = value.setScale(scale, RoundingMode.HALF_UP);
value = value.setScale(scale, ClickHouseValues.ROUNDING_MODE);
}
set(ClickHouseValues.convertToDateTime(value, tz).toOffsetDateTime());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,6 @@ public void testValueWithScale() throws Exception {
Arrays.asList(dateTime) // Tuple
);
Assert.assertEquals(ClickHouseDateTimeValue.of(dateTime, 3, ClickHouseValues.UTC_TIMEZONE).asBigDecimal(4),
BigDecimal.valueOf(0.1235D));
BigDecimal.valueOf(0.1234D));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void testValueWithScale() throws Exception {
Arrays.asList(dateTime) // Tuple
);
Assert.assertEquals(ClickHouseOffsetDateTimeValue.of(dateTime.toLocalDateTime(), 3, null).asBigDecimal(4),
BigDecimal.valueOf(0.1235D));
BigDecimal.valueOf(0.1234D));
}

@Test(groups = { "unit" })
Expand Down

0 comments on commit 772d304

Please sign in to comment.