@@ -77,6 +77,36 @@ private static void assertEqualsKinda(Object actual, Object expected) {
7777 assertEquals (String .valueOf (actual ), String .valueOf (expected ));
7878 }
7979
80+ private void writeTest (String tableName , String tableCreate , Field [][] rows ) throws Exception {
81+ initTable (tableName , tableCreate , new CommandSettings ());
82+ TableSchema schema = client .getTableSchema (tableName );
83+
84+ ClickHouseFormat format = ClickHouseFormat .RowBinaryWithDefaults ;
85+ try (InsertResponse response = client .insert (tableName , out -> {
86+ RowBinaryFormatWriter w = new RowBinaryFormatWriter (out , schema , format );
87+ for (Field [] row : rows ) {
88+ for (Field field : row ) {
89+ w .setValue (schema .nameToColumnIndex (field .name ), field .value );
90+ }
91+ w .commitRow ();
92+ }
93+ }, format , settings ).get ()) {
94+ System .out .println ("Rows written: " + response .getWrittenRows ());
95+ }
96+
97+ List <GenericRecord > records = client .queryAll ("SELECT * FROM \" " + tableName + "\" ORDER BY id" );
98+
99+ int id = 1 ;
100+ for (GenericRecord record : records ) {
101+ Map <String , Object > row = record .getValues ();
102+ //Validate data
103+ for (Field field : rows [id - 1 ]) {
104+ assertEqualsKinda (row .get (field .name ), field .getValue ());
105+ }
106+ id ++;
107+ }
108+ }
109+
80110
81111
82112 @ Test (groups = { "integration" })
@@ -104,181 +134,67 @@ public void writeNumbersTest() throws Exception {
104134 " decimal128 Decimal(36, 8), decimal128_nullable Nullable(Decimal(36, 8)), decimal128_default Decimal(36, 8) DEFAULT 3, " +
105135 " decimal256 Decimal(74, 10), decimal256_nullable Nullable(Decimal(74, 10)), decimal256_default Decimal(74, 10) DEFAULT 3" +
106136 " ) Engine = MergeTree ORDER BY id" ;
107- initTable (tableName , tableCreate , new CommandSettings ());
108137
109138 // Insert random (valid) values
110139 long seed = System .currentTimeMillis ();
111140 Random rand = new Random (seed );
112141 System .out .println ("Random seed: " + seed );
113142
114- Object [][] rows = new Object [][] {
115- {1 ,
116- rand .nextInt (256 ) - 128 , null , null , //Int8
117- rand .nextInt (65536 ) - 32768 , null , null , //Int16
118- rand .nextInt (), null , null , //Int32
119- rand .nextLong (), null , null , //Int64
120- new BigInteger (127 , rand ), null , null , //Int128
121- new BigInteger (255 , rand ), null , null , //Int256
122- rand .nextInt (256 ), null , null , //UInt8
123- rand .nextInt (65536 ), null , null , //UInt16
124- rand .nextInt () & 0xFFFFFFFFL , null , null , //UInt32
125- BigInteger .valueOf (rand .nextLong (Long .MAX_VALUE )), null , null , //UInt64
126- new BigInteger (128 , rand ), null , null , //UInt128
127- new BigInteger (256 , rand ), null , null , //UInt256
128- rand .nextFloat (), null , null , //Float32
129- rand .nextDouble (), null , null , //Float64
130- new BigDecimal (new BigInteger (7 , rand ) + "." + rand .nextInt (10 ,100 )), null , null , //Decimal(4)
131- new BigDecimal (new BigInteger (5 , rand ) + "." + rand .nextInt (1000 , 10000 )), null , null , //Decimal32
132- new BigDecimal (new BigInteger (18 , rand ) + "." + rand .nextInt (100000 , 1000000 )), null , null , //Decimal64
133- new BigDecimal (new BigInteger (20 , rand ) + "." + rand .nextLong (10000000 , 100000000 )), null , null , //Decimal128
134- new BigDecimal (new BigInteger (57 , rand ) + "." + rand .nextLong (1000000000 , 10000000000L )), null , null //Decimal256
135- },
136- {2 ,
137- rand .nextInt (256 ) - 128 , null , null , //Int8
138- rand .nextInt (65536 ) - 32768 , null , null , //Int16
139- rand .nextInt (), null , null , //Int32
140- rand .nextLong (), null , null , //Int64
141- new BigInteger (127 , rand ), null , null , //Int128
142- new BigInteger (255 , rand ), null , null , //Int256
143- rand .nextInt (256 ), null , null , //UInt8
144- rand .nextInt (65536 ), null , null , //UInt16
145- rand .nextInt () & 0xFFFFFFFFL , null , null , //UInt32
146- BigInteger .valueOf (rand .nextLong (Long .MAX_VALUE )), null , null , //UInt64
147- new BigInteger (128 , rand ), null , null , //UInt128
148- new BigInteger (256 , rand ), null , null , //UInt256
149- rand .nextFloat (), null , null , //Float32
150- rand .nextDouble (), null , null , //Float64
151- new BigDecimal (new BigInteger (7 , rand ) + "." + rand .nextInt (10 ,100 )), null , null , //Decimal(4)
152- new BigDecimal (new BigInteger (5 , rand ) + "." + rand .nextInt (1000 , 10000 )), null , null , //Decimal32
153- new BigDecimal (new BigInteger (18 , rand ) + "." + rand .nextInt (100000 , 1000000 )), null , null , //Decimal64
154- new BigDecimal (new BigInteger (20 , rand ) + "." + rand .nextLong (10000000 , 100000000 )), null , null , //Decimal128
155- new BigDecimal (new BigInteger (57 , rand ) + "." + rand .nextLong (1000000000 , 10000000000L )), null , null //Decimal256
156- },
143+ Field [][] rows = new Field [][] {{
144+ new Field ("id" , 1 ),
145+ new Field ("int8" , rand .nextInt (256 ) - 128 ), new Field ("int8_nullable" ), new Field ("int8_default" ).set (3 ), //Int8
146+ new Field ("int16" , rand .nextInt (65536 ) - 32768 ), new Field ("int16_nullable" ), new Field ("int16_default" ).set (3 ), //Int16
147+ new Field ("int32" , rand .nextInt ()), new Field ("int32_nullable" ), new Field ("int32_default" ).set (3 ), //Int32
148+ new Field ("int64" , rand .nextLong ()), new Field ("int64_nullable" ), new Field ("int64_default" ).set (3 ), //Int64
149+ new Field ("int128" , new BigInteger (127 , rand )), new Field ("int128_nullable" ), new Field ("int128_default" ).set (3 ), //Int128
150+ new Field ("int256" , new BigInteger (255 , rand )), new Field ("int256_nullable" ), new Field ("int256_default" ).set (3 ), //Int256
151+ new Field ("uint8" , rand .nextInt (256 )), new Field ("uint8_nullable" ), new Field ("uint8_default" ).set (3 ), //UInt8
152+ new Field ("uint16" , rand .nextInt (65536 )), new Field ("uint16_nullable" ), new Field ("uint16_default" ).set (3 ), //UInt16
153+ new Field ("uint32" , rand .nextInt () & 0xFFFFFFFFL ), new Field ("uint32_nullable" ), new Field ("uint32_default" ).set (3 ), //UInt32
154+ new Field ("uint64" , BigInteger .valueOf (rand .nextLong (Long .MAX_VALUE ))), new Field ("uint64_nullable" ), new Field ("uint64_default" ).set (3 ), //UInt64
155+ new Field ("uint128" , new BigInteger (128 , rand )), new Field ("uint128_nullable" ), new Field ("uint128_default" ).set (3 ), //UInt128
156+ new Field ("uint256" , new BigInteger (256 , rand )), new Field ("uint256_nullable" ), new Field ("uint256_default" ).set (3 ), //UInt256
157+ new Field ("float32" , rand .nextFloat ()), new Field ("float32_nullable" ), new Field ("float32_default" ).set ("3.0" ), //Float32
158+ new Field ("float64" , rand .nextDouble ()), new Field ("float64_nullable" ), new Field ("float64_default" ).set ("3.0" ), //Float64
159+ 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)
160+ 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
161+ 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
162+ 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
163+ 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
164+ }
157165 };
158166
159- TableSchema schema = client .getTableSchema (tableName );
167+ writeTest (tableName , tableCreate , rows );
168+ }
160169
161- ClickHouseFormat format = ClickHouseFormat .RowBinaryWithDefaults ;
162- try (InsertResponse response = client .insert (tableName , out -> {
163- RowBinaryFormatWriter w = new RowBinaryFormatWriter (out , schema , format );
164- for (Object [] row : rows ) {
165- for (int i = 0 ; i < row .length ; i ++) {
166- w .setValue (i + 1 , row [i ]);
167- }
168- w .commitRow ();
169- }
170- }, format , settings ).get ()) {
171- System .out .println ("Rows written: " + response .getWrittenRows ());
170+
171+ private static class Field {
172+ String name ;
173+ Object value ;
174+ Object defaultValue ;
175+
176+ Field (String name ) {
177+ this .name = name ;
178+ this .value = null ;
179+ this .defaultValue = null ;
172180 }
173181
174- List <GenericRecord > records = client .queryAll ("SELECT * FROM \" " + tableName + "\" " );
182+ Field (String name , Object value ) {
183+ this .name = name ;
184+ this .value = value ;
185+ }
175186
176- int id = 1 ;
177- for (GenericRecord record : records ) {
178- Map <String , Object > r = record .getValues ();
179- assertEquals (r .get ("id" ), id );
180- assertEqualsKinda (r .get ("int8" ), String .valueOf (rows [id - 1 ][1 ]));
181- assertEqualsKinda (r .get ("int8_nullable" ), rows [id - 1 ][2 ]);
182- assertEqualsKinda (r .get ("int8_default" ), 3 );
183- assertEqualsKinda (r .get ("int16" ), rows [id - 1 ][4 ]);
184- assertEqualsKinda (r .get ("int16_nullable" ), rows [id - 1 ][5 ]);
185- assertEqualsKinda (r .get ("int16_default" ), 3 );
186- assertEqualsKinda (r .get ("int32" ), rows [id - 1 ][7 ]);
187- assertEqualsKinda (r .get ("int32_nullable" ), rows [id - 1 ][8 ]);
188- assertEqualsKinda (r .get ("int32_default" ), 3 );
189- assertEqualsKinda (r .get ("int64" ), rows [id - 1 ][10 ]);
190- assertEqualsKinda (r .get ("int64_nullable" ), rows [id - 1 ][11 ]);
191- assertEqualsKinda (r .get ("int64_default" ), 3 );
192- assertEqualsKinda (r .get ("int128" ), rows [id - 1 ][13 ]);
193- assertEqualsKinda (r .get ("int128_nullable" ), rows [id - 1 ][14 ]);
194- assertEqualsKinda (r .get ("int128_default" ), 3 );
195- assertEqualsKinda (r .get ("int256" ), rows [id - 1 ][16 ]);
196- assertEqualsKinda (r .get ("int256_nullable" ), rows [id - 1 ][17 ]);
197- assertEqualsKinda (r .get ("int256_default" ), 3 );
198- assertEqualsKinda (r .get ("uint8" ), rows [id - 1 ][19 ]);
199- assertEqualsKinda (r .get ("uint8_nullable" ), rows [id - 1 ][20 ]);
200- assertEqualsKinda (r .get ("uint8_default" ), 3 );
201- assertEqualsKinda (r .get ("uint16" ), rows [id - 1 ][22 ]);
202- assertEqualsKinda (r .get ("uint16_nullable" ), rows [id - 1 ][23 ]);
203- assertEqualsKinda (r .get ("uint16_default" ), 3 );
204- assertEqualsKinda (r .get ("uint32" ), rows [id - 1 ][25 ]);
205- assertEqualsKinda (r .get ("uint32_nullable" ), rows [id - 1 ][26 ]);
206- assertEqualsKinda (r .get ("uint32_default" ), 3 );
207- assertEqualsKinda (r .get ("uint64" ), rows [id - 1 ][28 ]);
208- assertEqualsKinda (r .get ("uint64_nullable" ), rows [id - 1 ][29 ]);
209- assertEqualsKinda (r .get ("uint64_default" ), 3 );
210- assertEqualsKinda (r .get ("uint128" ), rows [id - 1 ][31 ]);
211- assertEqualsKinda (r .get ("uint128_nullable" ), rows [id - 1 ][32 ]);
212- assertEqualsKinda (r .get ("uint128_default" ), 3 );
213- assertEqualsKinda (r .get ("uint256" ), rows [id - 1 ][34 ]);
214- assertEqualsKinda (r .get ("uint256_nullable" ), rows [id - 1 ][35 ]);
215- assertEqualsKinda (r .get ("uint256_default" ), 3 );
216- assertEqualsKinda (r .get ("float32" ), rows [id - 1 ][37 ]);
217- assertEqualsKinda (r .get ("float32_nullable" ), rows [id - 1 ][38 ]);
218- assertEqualsKinda (r .get ("float32_default" ), 3.0 );
219- assertEqualsKinda (r .get ("float64" ), rows [id - 1 ][40 ]);
220- assertEqualsKinda (r .get ("float64_nullable" ), rows [id - 1 ][41 ]);
221- assertEqualsKinda (r .get ("float64_default" ), 3.0 );
222- assertEqualsKinda (r .get ("decimal" ), rows [id - 1 ][43 ]);
223- assertEqualsKinda (r .get ("decimal_nullable" ), rows [id - 1 ][44 ]);
224- assertEqualsKinda (r .get ("decimal_default" ), "3.00" );
225- assertEqualsKinda (r .get ("decimal32" ), rows [id - 1 ][46 ]);
226- assertEqualsKinda (r .get ("decimal32_nullable" ), rows [id - 1 ][47 ]);
227- assertEqualsKinda (r .get ("decimal32_default" ), "3.0000" );
228- assertEqualsKinda (r .get ("decimal64" ), rows [id - 1 ][49 ]);
229- assertEqualsKinda (r .get ("decimal64_nullable" ), rows [id - 1 ][50 ]);
230- assertEqualsKinda (r .get ("decimal64_default" ), "3.000000" );
231- assertEqualsKinda (r .get ("decimal128" ), rows [id - 1 ][52 ]);
232- assertEqualsKinda (r .get ("decimal128_nullable" ), rows [id - 1 ][53 ]);
233- assertEqualsKinda (r .get ("decimal128_default" ), "3.00000000" );
234- assertEqualsKinda (r .get ("decimal256" ), rows [id - 1 ][55 ]);
235- assertEqualsKinda (r .get ("decimal256_nullable" ), rows [id - 1 ][56 ]);
236- assertEqualsKinda (r .get ("decimal256_default" ), "3.0000000000" );
237- id ++;
187+ public Field set (Object defaultValue ) {//For default value for comparison purposes
188+ this .defaultValue = defaultValue ;
189+ return this ;
238190 }
239- }
240191
241- // @Test
242- // public void testAdvancedWriter() throws Exception {
243- // String tableName = "very_long_table_name_with_uuid_" + UUID.randomUUID().toString().replace('-', '_');
244- // String tableCreate = "CREATE TABLE \"" + tableName + "\" " +
245- // " (name String, " +
246- // " v1 Float32, " +
247- // " v2 Float32, " +
248- // " attrs Nullable(String), " +
249- // " corrected_time DateTime('UTC') DEFAULT now()," +
250- // " special_attr Nullable(Int8) DEFAULT -1)" +
251- // " Engine = MergeTree ORDER by ()";
252- //
253- // initTable(tableName, tableCreate);
254- //
255- // ZonedDateTime correctedTime = Instant.now().atZone(ZoneId.of("UTC"));
256- // Object[][] rows = new Object[][] {
257- // {"foo1", 0.3f, 0.6f, "a=1,b=2,c=5", correctedTime, 10},
258- // {"foo2", 0.6f, 0.1f, "a=1,b=2,c=5", correctedTime, null},
259- // {"foo3", 0.7f, 0.4f, "a=1,b=2,c=5", null, null},
260- // {"foo4", 0.8f, 0.5f, null, null, null},
261- // };
262- //
263- // TableSchema schema = client.getTableSchema(tableName);
264- //
265- // ClickHouseFormat format = ClickHouseFormat.RowBinaryWithDefaults;
266- // try (InsertResponse response = client.insert(tableName, out -> {
267- // RowBinaryFormatWriter w = new RowBinaryFormatWriter(out, schema, format);
268- // for (Object[] row : rows) {
269- // for (int i = 0; i < row.length; i++) {
270- // w.setValue(i + 1, row[i]);
271- // }
272- // w.commitRow();
273- // }
274- // }, format, new InsertSettings()).get()) {
275- // System.out.println("Rows written: " + response.getWrittenRows());
276- // }
277- //
278- // List<GenericRecord> records = client.queryAll("SELECT * FROM \"" + tableName + "\"" );
279- //
280- // for (GenericRecord record : records) {
281- // System.out.println("> " + record.getString(1) + ", " + record.getFloat(2) + ", " + record.getFloat(3));
282- // }
283- // }
192+ public Object getValue () {
193+ if (value == null && defaultValue != null ) {
194+ return defaultValue ;
195+ }
196+
197+ return value ;
198+ }
199+ }
284200}
0 commit comments