3838import java .util .UUID ;
3939import java .util .concurrent .TimeUnit ;
4040
41+ import static com .clickhouse .data .ClickHouseDataType .Decimal ;
4142import static org .testng .Assert .assertEquals ;
4243import static org .testng .Assert .assertNull ;
4344import static org .testng .Assert .assertTrue ;
@@ -133,6 +134,14 @@ private static void assertEqualsKinda(Object actual, Object expected) {
133134 return ;
134135 }
135136
137+ if (actual instanceof BigDecimal ) {
138+ actual = ((BigDecimal ) actual ).stripTrailingZeros ();
139+ }
140+
141+ if (expected instanceof BigDecimal ) {
142+ expected = ((BigDecimal ) expected ).stripTrailingZeros ();
143+ }
144+
136145 assertEquals (String .valueOf (actual ), String .valueOf (expected ));
137146 }
138147
@@ -249,11 +258,6 @@ public void writeNumbersTest() throws Exception {
249258 " float32 Float32, float32_nullable Nullable(Float32), float32_default Float32 DEFAULT 3, " +
250259 " float64 Float64, float64_nullable Nullable(Float64), float64_default Float64 DEFAULT 3, " +
251260// " bfloat16 BFloat16, bfloat16_nullable Nullable(BFloat16), bfloat16_default BFloat16 DEFAULT 3, " +
252- " decimal Decimal(4, 2), decimal_nullable Nullable(Decimal(4, 2)), decimal_default Decimal(4, 2) DEFAULT 3, " +
253- " decimal32 Decimal(8, 4), decimal32_nullable Nullable(Decimal(8, 4)), decimal32_default Decimal(8, 4) DEFAULT 3, " +
254- " decimal64 Decimal(18, 6), decimal64_nullable Nullable(Decimal(18, 6)), decimal64_default Decimal(18, 6) DEFAULT 3, " +
255- " decimal128 Decimal(36, 8), decimal128_nullable Nullable(Decimal(36, 8)), decimal128_default Decimal(36, 8) DEFAULT 3, " +
256- " decimal256 Decimal(74, 10), decimal256_nullable Nullable(Decimal(74, 10)), decimal256_default Decimal(74, 10) DEFAULT 3" +
257261 " ) Engine = MergeTree ORDER BY id" ;
258262
259263 // Insert random (valid) values
@@ -272,17 +276,12 @@ public void writeNumbersTest() throws Exception {
272276 new Field ("uint8" , rand .nextInt (256 )), new Field ("uint8_nullable" ), new Field ("uint8_default" ).set (3 ), //UInt8
273277 new Field ("uint16" , rand .nextInt (65536 )), new Field ("uint16_nullable" ), new Field ("uint16_default" ).set (3 ), //UInt16
274278 new Field ("uint32" , rand .nextInt () & 0xFFFFFFFFL ), new Field ("uint32_nullable" ), new Field ("uint32_default" ).set (3 ), //UInt32
275- new Field ("uint64" , BigInteger .valueOf (rand .nextLong (Long . MAX_VALUE ))), new Field ("uint64_nullable" ), new Field ("uint64_default" ).set (3 ), //UInt64
279+ new Field ("uint64" , BigInteger .valueOf (rand .nextLong ())), new Field ("uint64_nullable" ), new Field ("uint64_default" ).set (3 ), //UInt64
276280 new Field ("uint128" , new BigInteger (128 , rand )), new Field ("uint128_nullable" ), new Field ("uint128_default" ).set (3 ), //UInt128
277281 new Field ("uint256" , new BigInteger (256 , rand )), new Field ("uint256_nullable" ), new Field ("uint256_default" ).set (3 ), //UInt256
278282 new Field ("float32" , rand .nextFloat ()), new Field ("float32_nullable" ), new Field ("float32_default" ).set ("3.0" ), //Float32
279283 new Field ("float64" , rand .nextDouble ()), new Field ("float64_nullable" ), new Field ("float64_default" ).set ("3.0" ), //Float64
280284// new Field("bfloat16", rand.nextDouble()), new Field("bfloat16_nullable"), new Field("bfloat16_default").set("3.0"), //BFloat16
281- new Field ("decimal" , new BigDecimal (new BigInteger (5 , rand ) + "." + rand .nextInt (10 ,100 ))), new Field ("decimal_nullable" ), new Field ("decimal_default" ).set ("3.00" ), //Decimal(4)
282- new Field ("decimal32" , new BigDecimal (new BigInteger (7 , rand ) + "." + rand .nextInt (1000 , 10000 ))), new Field ("decimal32_nullable" ), new Field ("decimal32_default" ).set ("3.0000" ), //Decimal32
283- new Field ("decimal64" , new BigDecimal (new BigInteger (18 , rand ) + "." + rand .nextInt (100000 , 1000000 ))), new Field ("decimal64_nullable" ), new Field ("decimal64_default" ).set ("3.000000" ), //Decimal64
284- new Field ("decimal128" , new BigDecimal (new BigInteger (20 , rand ) + "." + rand .nextLong (10000000 , 100000000 ))), new Field ("decimal128_nullable" ), new Field ("decimal128_default" ).set ("3.00000000" ), //Decimal128
285- new Field ("decimal256" , new BigDecimal (new BigInteger (57 , rand ) + "." + rand .nextLong (1000000000 , 10000000000L ))), new Field ("decimal256_nullable" ), new Field ("decimal256_default" ).set ("3.0000000000" ) //Decimal256
286285 }, {
287286 new Field ("id" , 2 ), //Row ID
288287 new Field ("int8" , rand .nextInt (256 ) - 128 ), new Field ("int8_nullable" ), new Field ("int8_default" ).set (3 ), //Int8
@@ -300,11 +299,42 @@ public void writeNumbersTest() throws Exception {
300299 new Field ("float32" , rand .nextFloat ()), new Field ("float32_nullable" ), new Field ("float32_default" ).set ("3.0" ), //Float32
301300 new Field ("float64" , rand .nextDouble ()), new Field ("float64_nullable" ), new Field ("float64_default" ).set ("3.0" ), //Float64
302301// new Field("bfloat16", rand.nextDouble()), new Field("bfloat16_nullable"), new Field("bfloat16_default").set("3.0"), //BFloat16
303- new Field ("decimal" , new BigDecimal (new BigInteger (5 , rand ) + "." + rand .nextInt (10 ,100 ))), new Field ("decimal_nullable" ), new Field ("decimal_default" ).set ("3.00" ), //Decimal(4)
304- new Field ("decimal32" , new BigDecimal (new BigInteger (7 , rand ) + "." + rand .nextInt (1000 , 10000 ))), new Field ("decimal32_nullable" ), new Field ("decimal32_default" ).set ("3.0000" ), //Decimal32
305- new Field ("decimal64" , new BigDecimal (new BigInteger (18 , rand ) + "." + rand .nextInt (100000 , 1000000 ))), new Field ("decimal64_nullable" ), new Field ("decimal64_default" ).set ("3.000000" ), //Decimal64
306- new Field ("decimal128" , new BigDecimal (new BigInteger (20 , rand ) + "." + rand .nextLong (10000000 , 100000000 ))), new Field ("decimal128_nullable" ), new Field ("decimal128_default" ).set ("3.00000000" ), //Decimal128
307- new Field ("decimal256" , new BigDecimal (new BigInteger (57 , rand ) + "." + rand .nextLong (1000000000 , 10000000000L ))), new Field ("decimal256_nullable" ), new Field ("decimal256_default" ).set ("3.0000000000" ) //Decimal256
302+ }};
303+
304+ writeTest (tableName , tableCreate , rows );
305+ }
306+
307+
308+ @ Test (groups = { "integration" })
309+ public void writeDecimalsTest () throws Exception {
310+ String tableName = "rowBinaryFormatWriterTest_writeNumbersTest_" + UUID .randomUUID ().toString ().replace ('-' , '_' );
311+ String tableCreate = "CREATE TABLE \" " + tableName + "\" " +
312+ " (id Int32, " +
313+ " decimal Decimal(4, 2), decimal_nullable Nullable(Decimal(4, 2)), decimal_default Decimal(4, 2) DEFAULT 3, " +
314+ " decimal32 Decimal(8, 4), decimal32_nullable Nullable(Decimal(8, 4)), decimal32_default Decimal(8, 4) DEFAULT 3, " +
315+ " decimal64 Decimal(18, 6), decimal64_nullable Nullable(Decimal(18, 6)), decimal64_default Decimal(18, 6) DEFAULT 3, " +
316+ " decimal128 Decimal(36, 8), decimal128_nullable Nullable(Decimal(36, 8)), decimal128_default Decimal(36, 8) DEFAULT 3, " +
317+ " decimal256 Decimal(74, 10), decimal256_nullable Nullable(Decimal(74, 10)), decimal256_default Decimal(74, 10) DEFAULT 3" +
318+ " ) Engine = MergeTree ORDER BY id" ;
319+
320+ // Insert random (valid) values
321+ long seed = System .currentTimeMillis ();
322+ Random rand = new Random (seed );
323+ System .out .println ("Random seed: " + seed );
324+
325+ BigDecimal decimal = new BigDecimal (new BigInteger (5 , rand ) + "." + rand .nextInt (10 ,100 ));
326+ BigDecimal decimal32 = new BigDecimal (new BigInteger (7 , rand ) + "." + rand .nextInt (1000 , 10000 ));
327+ BigDecimal decimal64 = new BigDecimal (new BigInteger (18 , rand ) + "." + rand .nextInt (100000 , 1000000 ));
328+ BigDecimal decimal128 = new BigDecimal (new BigInteger (20 , rand ) + "." + rand .nextInt (100000 , 1000000 ));
329+ BigDecimal decimal256 = new BigDecimal (new BigInteger (57 , rand ) + "." + rand .nextInt (100000 , 1000000 ));
330+
331+ Field [][] rows = new Field [][] {{
332+ new Field ("id" , 1 ),
333+ new Field ("decimal" , decimal ).set (decimal ), new Field ("decimal_nullable" ), new Field ("decimal_default" ).set ("3" ), //Decimal(4)
334+ new Field ("decimal32" , decimal32 ).set (decimal32 ), new Field ("decimal32_nullable" ), new Field ("decimal32_default" ).set ("3" ), //Decimal32
335+ new Field ("decimal64" , decimal64 ).set (decimal64 ), new Field ("decimal64_nullable" ), new Field ("decimal64_default" ).set ("3" ), //Decimal64
336+ new Field ("decimal128" , decimal128 ).set (decimal128 ), new Field ("decimal128_nullable" ), new Field ("decimal128_default" ).set ("3" ), //Decimal128
337+ new Field ("decimal256" , decimal256 ).set (decimal256 ), new Field ("decimal256_nullable" ), new Field ("decimal256_default" ).set ("3" ) //Decimal256
308338 }};
309339
310340 writeTest (tableName , tableCreate , rows );
0 commit comments