Skip to content

Commit 2e52743

Browse files
committed
Update tests of DecimalType/DecimalValue
1 parent cca97f3 commit 2e52743

File tree

2 files changed

+174
-35
lines changed

2 files changed

+174
-35
lines changed

table/src/test/java/tech/ydb/table/integration/ValuesReadTest.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import tech.ydb.table.result.ValueReader;
1818
import tech.ydb.table.rpc.grpc.GrpcTableRpc;
1919
import tech.ydb.table.transaction.TxControl;
20+
import tech.ydb.table.values.DecimalType;
21+
import tech.ydb.table.values.DecimalValue;
2022
import tech.ydb.table.values.NullType;
2123
import tech.ydb.table.values.NullValue;
2224
import tech.ydb.table.values.PrimitiveType;
@@ -157,4 +159,68 @@ public void timestampReadTest() {
157159
Assert.assertEquals("Invalid value \"2106-01-01T00:00:00.000000Z\" for type Timestamp", issues[1].getMessage());
158160

159161
}
162+
163+
@Test
164+
public void decimalReadTest() {
165+
DataQueryResult result = CTX.supplyResult(
166+
s -> s.executeDataQuery("SELECT "
167+
+ "Decimal('9', 1, 0) AS d1, "
168+
+ "Decimal('-9', 1, 0) AS d2, "
169+
+ "Decimal('99999999999999999999999999999999999', 35, 0) AS d3, "
170+
+ "Decimal('-99999999999999999999999999999999999', 35, 0) AS d4, "
171+
+ "Decimal('9999999999999999999999999.9999999999', 35, 10) AS d5, "
172+
+ "Decimal('-9999999999999999999999999.9999999999', 35, 10) AS d6, "
173+
+ "Decimal('9.6', 1, 0) AS d7, "
174+
+ "Decimal('-9.6', 1, 0) AS d8, "
175+
+ "Decimal('99999999999999999999999999999999999.6', 35, 0) AS d9, "
176+
+ "Decimal('-99999999999999999999999999999999999.6', 35, 0) AS d10, "
177+
+ "Decimal('9999999999999999999999999.99999999996', 35, 10) AS d11, "
178+
+ "Decimal('-9999999999999999999999999.99999999996', 35, 10) AS d12;",
179+
TxControl.serializableRw()
180+
)
181+
).join().getValue();
182+
183+
Assert.assertEquals(1, result.getResultSetCount());
184+
185+
ResultSetReader rs = result.getResultSet(0);
186+
Assert.assertTrue(rs.next());
187+
188+
DecimalValue d1 = rs.getColumn("d1").getDecimal();
189+
DecimalValue d2 = rs.getColumn("d2").getDecimal();
190+
DecimalValue d3 = rs.getColumn("d3").getDecimal();
191+
DecimalValue d4 = rs.getColumn("d4").getDecimal();
192+
DecimalValue d5 = rs.getColumn("d5").getDecimal();
193+
DecimalValue d6 = rs.getColumn("d6").getDecimal();
194+
DecimalValue d7 = rs.getColumn("d7").getDecimal();
195+
DecimalValue d8 = rs.getColumn("d8").getDecimal();
196+
DecimalValue d9 = rs.getColumn("d9").getDecimal();
197+
DecimalValue d10 = rs.getColumn("d10").getDecimal();
198+
DecimalValue d11 = rs.getColumn("d11").getDecimal();
199+
DecimalValue d12 = rs.getColumn("d12").getDecimal();
200+
201+
Assert.assertEquals(DecimalType.of(1).newValue(9), d1);
202+
Assert.assertEquals(DecimalType.of(1).newValue(-9), d2);
203+
Assert.assertEquals(DecimalType.of(35).newValue("99999999999999999999999999999999999"), d3);
204+
Assert.assertEquals(DecimalType.of(35).newValue("-99999999999999999999999999999999999"), d4);
205+
Assert.assertEquals(DecimalType.of(35, 10).newValue("9999999999999999999999999.9999999999"), d5);
206+
Assert.assertEquals(DecimalType.of(35, 10).newValue("-9999999999999999999999999.9999999999"), d6);
207+
208+
Assert.assertEquals(DecimalType.of(1).getInf(), d7);
209+
Assert.assertEquals(DecimalType.of(1).getNegInf(), d8);
210+
Assert.assertEquals(DecimalType.of(35).getInf(), d9);
211+
Assert.assertEquals(DecimalType.of(35).getNegInf(), d10);
212+
Assert.assertEquals(DecimalType.of(35, 10).getInf(), d11);
213+
Assert.assertEquals(DecimalType.of(35, 10).getNegInf(), d12);
214+
215+
// All infinity values have the same high & low parts
216+
Assert.assertEquals(d7.getHigh(), d9.getHigh());
217+
Assert.assertEquals(d7.getHigh(), d11.getHigh());
218+
Assert.assertEquals(d7.getLow(), d9.getLow());
219+
Assert.assertEquals(d7.getLow(), d11.getLow());
220+
221+
Assert.assertEquals(d8.getHigh(), d10.getHigh());
222+
Assert.assertEquals(d8.getHigh(), d12.getHigh());
223+
Assert.assertEquals(d8.getLow(), d10.getLow());
224+
Assert.assertEquals(d8.getLow(), d12.getLow());
225+
}
160226
}

table/src/test/java/tech/ydb/table/values/DecimalValueTest.java

Lines changed: 108 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,35 @@ public void bigDecimalConv() {
6767

6868
@Test
6969
public void contract() {
70-
DecimalType type = DecimalType.of(13, 2);
70+
DecimalType type = DecimalType.of(20, 2);
7171
DecimalValue value = DecimalValue.fromBits(type, 0x0001, 0x0002);
7272

73-
Assert.assertEquals(DecimalType.of(13, 2), value.getType());
73+
Assert.assertEquals(DecimalType.of(20, 2), value.getType());
7474
Assert.assertEquals(0x0001, value.getHigh());
7575
Assert.assertEquals(0x0002, value.getLow());
7676

7777
// equals
7878
Assert.assertEquals(value, DecimalValue.fromBits(type, 0x0001, 0x0002));
79-
Assert.assertEquals(value, DecimalValue.fromBits(DecimalType.of(13, 2), 0x0001, 0x0002));
79+
Assert.assertEquals(value, DecimalValue.fromBits(DecimalType.of(20, 2), 0x0001, 0x0002));
8080

8181
Assert.assertNotEquals(value, DecimalValue.fromBits(type, 0x0001, 0x0003));
8282
Assert.assertNotEquals(value, DecimalValue.fromBits(type, 0x0002, 0x0002));
83-
Assert.assertNotEquals(value, DecimalValue.fromBits(DecimalType.of(12, 2), 0x0001, 0x0002));
84-
Assert.assertNotEquals(value, DecimalValue.fromBits(DecimalType.of(13, 1), 0x0001, 0x0002));
83+
Assert.assertNotEquals(value, DecimalValue.fromBits(DecimalType.of(21, 2), 0x0001, 0x0002));
84+
Assert.assertNotEquals(value, DecimalValue.fromBits(DecimalType.of(20, 1), 0x0001, 0x0002));
8585

8686
// hashCode
8787
Assert.assertEquals(value.hashCode(), DecimalValue.fromBits(type, 0x0001, 0x0002).hashCode());
88-
Assert.assertEquals(value.hashCode(), DecimalValue.fromBits(DecimalType.of(13, 2), 0x0001, 0x0002).hashCode());
88+
Assert.assertEquals(value.hashCode(), DecimalValue.fromBits(DecimalType.of(20, 2), 0x0001, 0x0002).hashCode());
8989

9090
Assert.assertNotEquals(value.hashCode(), DecimalValue.fromBits(type, 0x0001, 0x0003).hashCode());
9191
Assert.assertNotEquals(value.hashCode(), DecimalValue.fromBits(type, 0x0002, 0x0002).hashCode());
92-
Assert.assertNotEquals(value.hashCode(), DecimalValue.fromBits(DecimalType.of(12, 2), 0x0001, 0x0002).hashCode());
93-
Assert.assertNotEquals(value.hashCode(), DecimalValue.fromBits(DecimalType.of(13, 1), 0x0001, 0x0002).hashCode());
92+
Assert.assertNotEquals(value.hashCode(), DecimalValue.fromBits(DecimalType.of(21, 2), 0x0001, 0x0002).hashCode());
93+
Assert.assertNotEquals(value.hashCode(), DecimalValue.fromBits(DecimalType.of(20, 1), 0x0001, 0x0002).hashCode());
9494
}
9595

9696
@Test
9797
public void protobuf() {
98-
DecimalType type = DecimalType.of(13, 2);
98+
DecimalType type = DecimalType.of(20, 2);
9999
DecimalValue value = DecimalValue.fromBits(type, 0x0001, 0x0002);
100100

101101
ValueProtos.Value valuePb = value.toPb();
@@ -128,11 +128,83 @@ public void inf() {
128128
DecimalValue value = type.newValue(inf);
129129
Assert.assertTrue(value.isInf());
130130
Assert.assertFalse(value.isNegative());
131-
Assert.assertEquals(DecimalValue.INF, value);
131+
Assert.assertEquals(type.getInf(), value);
132132
inf = inf.add(k);
133133
}
134134
}
135135

136+
private void assertIsValid(DecimalValue v) {
137+
Assert.assertFalse("Non expected Nan for " + v, v.isNan());
138+
Assert.assertFalse("Non expected Inf for " + v, v.isInf());
139+
Assert.assertFalse("Non expected -Inf for " + v, v.isNegativeInf());
140+
}
141+
142+
private void assertIsNan(DecimalValue v) {
143+
Assert.assertTrue("Expected Nan for " + v, v.isNan());
144+
Assert.assertFalse("Non expected Inf for " + v, v.isInf());
145+
Assert.assertFalse("Non expected -Inf for " + v, v.isNegativeInf());
146+
147+
Assert.assertEquals(DecimalValue.NAN_LOW, v.getLow());
148+
Assert.assertEquals(DecimalValue.NAN_HIGH, v.getHigh());
149+
}
150+
151+
private void assertIsInf(DecimalValue v) {
152+
Assert.assertFalse("Non expected Nan for " + v, v.isNan());
153+
Assert.assertTrue("Expected Inf for " + v, v.isInf());
154+
Assert.assertFalse("Non expected -Inf for " + v, v.isNegativeInf());
155+
156+
Assert.assertEquals(DecimalValue.INF_LOW, v.getLow());
157+
Assert.assertEquals(DecimalValue.INF_HIGH, v.getHigh());
158+
}
159+
160+
private void assertIsNegInf(DecimalValue v) {
161+
Assert.assertFalse("Non expected Nan for " + v, v.isNan());
162+
Assert.assertFalse("Non expected Inf for " + v, v.isInf());
163+
Assert.assertTrue("Expected -Inf for " + v, v.isNegativeInf());
164+
165+
Assert.assertEquals(DecimalValue.NEG_INF_LOW, v.getLow());
166+
Assert.assertEquals(DecimalValue.NEG_INF_HIGH, v.getHigh());
167+
}
168+
169+
@Test
170+
public void allTypeInfiniteAndNan() {
171+
BigInteger inf = BigInteger.ONE;
172+
BigInteger nan = new BigInteger("100000000000000000000000000000000001");
173+
int[] scales = new int[] { 1, 9, 35 };
174+
for (int precision = 1; precision <= DecimalType.MAX_PRECISION; precision++) {
175+
inf = inf.multiply(BigInteger.TEN);
176+
177+
DecimalType type = DecimalType.of(precision);
178+
179+
assertIsInf(type.newValue(inf));
180+
assertIsNegInf(type.newValue(inf.negate()));
181+
assertIsNan(type.newValue(nan));
182+
183+
assertIsValid(type.newValue(inf.subtract(BigInteger.ONE)));
184+
assertIsValid(type.newValue(inf.negate().add(BigInteger.ONE)));
185+
186+
for (int scale : scales) {
187+
if (scale > precision) {
188+
continue;
189+
}
190+
191+
DecimalType scaled = DecimalType.of(precision, scale);
192+
BigDecimal scaledInf = new BigDecimal(inf, scale);
193+
BigDecimal scaledNan = new BigDecimal(nan, scale);
194+
195+
System.out.println("Nan for " + scaled + " -> " + scaledNan);
196+
197+
assertIsInf(scaled.newValue(scaledInf));
198+
assertIsNegInf(scaled.newValue(scaledInf.negate()));
199+
200+
assertIsValid(scaled.newValue(scaledInf.subtract(BigDecimal.valueOf(1, scale))));
201+
assertIsValid(scaled.newValue(scaledInf.negate().add(BigDecimal.valueOf(1, scale))));
202+
203+
assertIsNan(scaled.newValue(scaledNan));
204+
}
205+
}
206+
}
207+
136208
@Test
137209
public void infDefaulttype() {
138210
DecimalType type = DecimalType.getDefault();
@@ -143,7 +215,7 @@ public void infDefaulttype() {
143215
DecimalValue value = type.newValue(inf);
144216
Assert.assertTrue(value.isInf());
145217
Assert.assertFalse(value.isNegative());
146-
Assert.assertNotEquals(DecimalValue.INF, value);
218+
Assert.assertEquals(type.getInf(), value);
147219
inf = inf.add(k);
148220
}
149221
}
@@ -158,7 +230,7 @@ public void negativeInf() {
158230
DecimalValue value = type.newValue(inf);
159231
Assert.assertTrue(value.isNegativeInf());
160232
Assert.assertTrue(value.isNegative());
161-
Assert.assertEquals(DecimalValue.NEG_INF, value);
233+
Assert.assertEquals(type.getNegInf(), value);
162234
inf = inf.subtract(k);
163235
}
164236
}
@@ -173,7 +245,7 @@ public void negativeInfDefaultType() {
173245
DecimalValue value = type.newValue(inf);
174246
Assert.assertTrue(value.isNegativeInf());
175247
Assert.assertTrue(value.isNegative());
176-
Assert.assertNotEquals(DecimalValue.NEG_INF, value);
248+
Assert.assertEquals(type.getNegInf(), value);
177249
inf = inf.subtract(k);
178250
}
179251
}
@@ -258,7 +330,7 @@ public void ofString() {
258330

259331
@Test
260332
public void ofUnsigned() {
261-
DecimalType t = DecimalType.getDefault();
333+
DecimalType t = DecimalType.of(31, 9);
262334
String zeros = "." + String.join("", Collections.nCopies(t.getScale(), "0"));
263335

264336
Assert.assertTrue(t.newValueUnsigned(0).isZero());
@@ -278,7 +350,7 @@ public void ofUnsigned() {
278350

279351
@Test
280352
public void ofLong() {
281-
DecimalType t = DecimalType.getDefault();
353+
DecimalType t = DecimalType.of(31, 9);
282354
String zeros = "." + String.join("", Collections.nCopies(t.getScale(), "0"));
283355

284356
Assert.assertTrue(t.newValue(0).isZero());
@@ -575,25 +647,25 @@ public void toUnscaledBigInteger() {
575647

576648
// (2) positive numbers: 1, 12, 123, ...
577649
String s = "";
578-
for (int i = 1; i < DecimalType.MAX_PRECISION; i++) {
650+
for (int i = 1; i < t.getPrecision(); i++) {
579651
s += Integer.toString(i % 10);
580652
BigInteger value = new BigInteger(s);
581653
Assert.assertEquals(value, t.newValueUnscaled(value).toUnscaledBigInteger());
582654
}
583655

584656
// (3) negative numbers: -1, -12, -123, ...
585657
s = "-";
586-
for (int i = 1; i < DecimalType.MAX_PRECISION; i++) {
658+
for (int i = 1; i < t.getPrecision(); i++) {
587659
s += Integer.toString(i % 10);
588660
BigInteger value = new BigInteger(s);
589661
Assert.assertEquals(value, t.newValueUnscaled(value).toUnscaledBigInteger());
590662
}
591663

592664
// (4) -inf, +inf, nan
593665
BigInteger inf = BigInteger.TEN.pow(DecimalType.MAX_PRECISION);
594-
Assert.assertEquals(DecimalValue.INF.toUnscaledBigInteger(), inf);
595-
Assert.assertEquals(DecimalValue.NEG_INF.toUnscaledBigInteger(), inf.negate());
596-
Assert.assertEquals(DecimalValue.NAN.toUnscaledBigInteger(), inf.add(BigInteger.ONE));
666+
Assert.assertEquals(t.getInf().toUnscaledBigInteger(), inf);
667+
Assert.assertEquals(t.getNegInf().toUnscaledBigInteger(), inf.negate());
668+
Assert.assertEquals(t.getNaN().toUnscaledBigInteger(), inf.add(BigInteger.ONE));
597669
}
598670

599671
@Test
@@ -627,18 +699,19 @@ public void toBigInteger() {
627699

628700
// (4) -inf, +inf, nan
629701
BigInteger inf = BigInteger.TEN.pow(DecimalType.MAX_PRECISION);
630-
Assert.assertEquals(DecimalValue.INF.toBigInteger(), inf);
631-
Assert.assertEquals(DecimalValue.NEG_INF.toBigInteger(), inf.negate());
632-
Assert.assertEquals(DecimalValue.NAN.toBigInteger(), inf.add(BigInteger.ONE));
702+
Assert.assertEquals(t.getInf().toBigInteger(), inf);
703+
Assert.assertEquals(t.getNegInf().toBigInteger(), inf.negate());
704+
Assert.assertEquals(t.getNaN().toBigInteger(), inf.add(BigInteger.ONE));
633705
}
634706

635707
@Test
636708
public void toBigDecimal() {
637709
// (1) special values
638-
BigDecimal inf = BigDecimal.TEN.pow(DecimalType.MAX_PRECISION);
639-
Assert.assertEquals(DecimalValue.INF.toBigDecimal(), inf);
640-
Assert.assertEquals(DecimalValue.NEG_INF.toBigDecimal(), inf.negate());
641-
Assert.assertEquals(DecimalValue.NAN.toBigDecimal(), inf.add(BigDecimal.ONE));
710+
BigDecimal inf = BigDecimal.ONE.scaleByPowerOfTen(DecimalType.MAX_PRECISION);
711+
DecimalType type = DecimalType.getDefault();
712+
Assert.assertEquals(type.getInf().toBigDecimal(), inf.setScale(type.getScale()));
713+
Assert.assertEquals(type.getNegInf().toBigDecimal(), inf.negate().setScale(type.getScale()));
714+
Assert.assertEquals(type.getNaN().toBigDecimal(), inf.add(BigDecimal.ONE).setScale(type.getScale()));
642715

643716
// (2) positive numbers
644717
Assert.assertEquals(newDecimal(1234567890L, 0).toBigDecimal(), BigDecimal.valueOf(1234567890L, 0));
@@ -682,30 +755,30 @@ public void toUnscaledString() {
682755

683756
// (2) positive numbers: 1, 12, 123, ...
684757
String s = "";
685-
for (int i = 1; i < DecimalType.MAX_PRECISION; i++) {
758+
for (int i = 1; i < t.getPrecision(); i++) {
686759
s += Integer.toString(i % 10);
687760
Assert.assertEquals(s, t.newValueUnscaled(new BigInteger(s)).toUnscaledString());
688761
}
689762

690763
// (3) negative numbers: -1, -12, -123, ...
691764
s = "-";
692-
for (int i = 1; i < DecimalType.MAX_PRECISION; i++) {
765+
for (int i = 1; i < t.getPrecision(); i++) {
693766
s += Integer.toString(i % 10);
694767
Assert.assertEquals(s, t.newValueUnscaled(new BigInteger(s)).toUnscaledString());
695768
}
696769

697770
// (4) -inf, +inf, nan
698-
Assert.assertEquals("100000000000000000000000000000000000", DecimalValue.INF.toUnscaledString()); // 10^35
699-
Assert.assertEquals("-100000000000000000000000000000000000", DecimalValue.NEG_INF.toUnscaledString()); // -10^35
700-
Assert.assertEquals("100000000000000000000000000000000001", DecimalValue.NAN.toUnscaledString()); // 10^35 + 1
771+
Assert.assertEquals("100000000000000000000000000000000000", t.getInf().toUnscaledString()); // 10^35
772+
Assert.assertEquals("-100000000000000000000000000000000000", t.getNegInf().toUnscaledString()); // -10^35
773+
Assert.assertEquals("100000000000000000000000000000000001", t.getNaN().toUnscaledString()); // 10^35 + 1
701774
}
702775

703776
@Test
704777
public void toStringTest() {
705778
// (1) special values
706-
Assert.assertEquals("inf", DecimalValue.INF.toString());
707-
Assert.assertEquals("-inf", DecimalValue.NEG_INF.toString());
708-
Assert.assertEquals("nan", DecimalValue.NAN.toString());
779+
Assert.assertEquals("inf", DecimalType.getDefault().getInf().toString());
780+
Assert.assertEquals("-inf", DecimalType.getDefault().getNegInf().toString());
781+
Assert.assertEquals("nan", DecimalType.getDefault().getNaN().toString());
709782

710783
// (2) positive numbers
711784
Assert.assertEquals("1234567890", newDecimal(1234567890L, 0).toString());

0 commit comments

Comments
 (0)