Skip to content

Commit 146a6be

Browse files
committed
Add Decimal128
1 parent ca7ec2a commit 146a6be

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

driver-core/src/main/com/mongodb/client/model/expressions/Expressions.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public static NumberExpression of(@NonNull final BigDecimal of) {
8080
Assertions.notNull("BigDecimal", of);
8181
return new MqlExpression<>((codecRegistry) -> new AstPlaceholder(new BsonDecimal128(new Decimal128(of))));
8282
}
83+
public static NumberExpression of(final Decimal128 of) {
84+
Assertions.notNull("Decimal128", of);
85+
return new MqlExpression<>((codecRegistry) -> new AstPlaceholder(new BsonDecimal128(of)));
86+
}
8387
public static DateExpression of(@NonNull final Instant of) {
8488
Assertions.notNull("Instant", of);
8589
return new MqlExpression<>((codecRegistry) -> new AstPlaceholder(new BsonDateTime(of.toEpochMilli())));

driver-core/src/test/functional/com/mongodb/client/model/expressions/ArithmeticExpressionsFunctionalTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public void literalsTest() {
3636
assertExpression(1L, of(1L));
3737
assertExpression(1.0, of(1.0));
3838
assertExpression(BigDecimal.valueOf(1.0), of(BigDecimal.valueOf(1.0)));
39+
assertExpression(Decimal128.parse("1.0"), of(Decimal128.parse("1.0")));
3940
assertThrows(IllegalArgumentException.class, () -> of((BigDecimal) null));
41+
assertThrows(IllegalArgumentException.class, () -> of((Decimal128) null));
4042

4143
// expression equality differs from bson equality
4244
assertExpression(true, of(1L).eq(of(1.0)));
@@ -91,13 +93,29 @@ public void divideTest() {
9193
0.5,
9294
of(1).divide(of(2)),
9395
"{'$divide': [1, 2]}");
94-
// divide always returns a Number, so the method is not on IntegerExpression
96+
97+
// however, there are differences in evaluation between numbers
98+
// represented using Decimal128 and double:
99+
assertExpression(
100+
2.5242187499999997,
101+
of(3.231).divide(of(1.28)));
102+
assertExpression(
103+
Decimal128.parse("2.52421875"),
104+
of(Decimal128.parse("3.231")).divide(of(Decimal128.parse("1.28"))));
105+
assertExpression(
106+
Decimal128.parse("2.52421875"),
107+
of(Decimal128.parse("3.231")).divide(of(1.28)));
108+
assertExpression(
109+
Decimal128.parse("2.52421875"),
110+
of(3.231).divide(of(Decimal128.parse("1.28"))));
95111

96112
// convenience
97113
assertExpression(0.5, of(1.0).divide(2.0));
98114
assertExpression(0.5, of(1).divide(2.0));
99115
assertExpression(0.5, of(1).divide(2L));
100116
assertExpression(0.5, of(1).divide(2));
117+
118+
// divide always returns a Number, so the method is not on IntegerExpression
101119
}
102120

103121
@Test

0 commit comments

Comments
 (0)