55import com .clickhouse .client .ClickHouseProtocol ;
66import com .clickhouse .client .api .Client ;
77import com .clickhouse .client .api .command .CommandSettings ;
8+ import com .clickhouse .client .api .data_formats .internal .BinaryStreamReader ;
89import com .clickhouse .client .api .enums .Protocol ;
910import com .clickhouse .client .api .insert .InsertResponse ;
1011import com .clickhouse .client .api .insert .InsertSettings ;
12+ import com .clickhouse .client .api .metadata .TableSchema ;
1113import com .clickhouse .client .api .query .GenericRecord ;
1214import com .clickhouse .data .ClickHouseDataType ;
1315import lombok .AllArgsConstructor ;
1416import lombok .Data ;
17+ import lombok .NoArgsConstructor ;
1518import org .testng .Assert ;
1619import org .testng .annotations .AfterMethod ;
1720import org .testng .annotations .BeforeMethod ;
2528import java .util .Arrays ;
2629import java .util .Collection ;
2730import java .util .List ;
31+ import java .util .concurrent .atomic .AtomicInteger ;
32+ import java .util .function .BiConsumer ;
33+ import java .util .function .Consumer ;
2834
2935public class DataTypeTests extends BaseIntegrationTest {
3036
@@ -59,39 +65,83 @@ public void setUp() throws IOException {
5965 .build ();
6066 }
6167
62- @ AfterMethod (groups = { "integration" })
68+ @ AfterMethod (groups = {"integration" })
6369 public void tearDown () {
6470 client .close ();
6571 }
6672
73+ private <T > void writeReadVerify (String table , String tableDef , Class <T > dtoClass , List <T > data ,
74+ BiConsumer <List <T >, T > rowVerifier ) throws Exception {
75+ client .execute ("DROP TABLE IF EXISTS " + table ).get ();
76+ client .execute (tableDef );
77+
78+ final TableSchema tableSchema = client .getTableSchema (table );
79+ client .register (dtoClass , tableSchema );
80+ client .insert (table , data );
81+ final AtomicInteger rowCount = new AtomicInteger (0 );
82+ client .queryAll ("SELECT * FROM " + table , dtoClass , tableSchema ).forEach (dto -> {
83+ rowVerifier .accept (data , dto );
84+ rowCount .incrementAndGet ();
85+ });
86+
87+ Assert .assertEquals (rowCount .get (), data .size ());
88+ }
6789
6890 @ Test (groups = {"integration" })
6991 public void testNestedDataTypes () throws Exception {
7092 final String table = "test_nested_types" ;
71- String tblCreateSQL = NestedTypesDTO .tblCreateSQL (table );
72- client .execute ("DROP TABLE IF EXISTS " + table ).get ();
73- client .execute (tblCreateSQL );
93+ writeReadVerify (table ,
94+ NestedTypesDTO .tblCreateSQL (table ),
95+ NestedTypesDTO .class ,
96+ Arrays .asList (new NestedTypesDTO (0 , new Object []{(short ) 127 , "test 1" }, new double []{0.3d , 0.4d })),
97+ (data , dto ) -> {
98+ NestedTypesDTO dataDto = data .get (dto .getRowId ());
99+ Assert .assertEquals (dto .getTuple1 (), dataDto .getTuple1 ());
100+ Assert .assertEquals (dto .getPoint1 (), dataDto .getPoint1 ());
101+ });
102+ }
74103
75- client .register (NestedTypesDTO .class , client .getTableSchema (table ));
104+ @ Test (groups = {"integration" })
105+ public void testArrays () throws Exception {
106+ final String table = "test_arrays" ;
107+ writeReadVerify (table ,
108+ DTOForArraysTests .tblCreateSQL (table ),
109+ DTOForArraysTests .class ,
110+ Arrays .asList (new DTOForArraysTests (
111+ 0 , Arrays .asList ("db" , "fast" ), new int []{1 , 2 , 3 }, new String []{"a" , "b" , "c" })),
112+ (data , dto ) -> {
113+ DTOForArraysTests dataDto = data .get (dto .getRowId ());
114+ System .out .println (dto .getWords ());
115+ Assert .assertEquals (dto .getWords (), dataDto .getWords ());
116+ System .out .println (Arrays .asList (dto .getLetters ()));
117+ Assert .assertEquals (dto .getLetters (), dataDto .getLetters ());
118+ System .out .println (Arrays .asList (dto .getNumbers ()));
119+ Assert .assertEquals (dto .getNumbers (), dataDto .getNumbers ());
120+ });
121+ }
76122
77- List <NestedTypesDTO > data =
78- Arrays .asList (new NestedTypesDTO (0 , new Object [] {(short )127 , "test 1" }, new Double [] {0.3d , 0.4d } ));
79- client .insert (table , data );
123+ @ Data
124+ @ AllArgsConstructor
125+ @ NoArgsConstructor
126+ public static class DTOForArraysTests {
127+ private int rowId ;
80128
81- List <GenericRecord > rows = client .queryAll ("SELECT * FROM " + table );
82- for (GenericRecord row : rows ) {
83- NestedTypesDTO dto = data .get (row .getInteger ("rowId" ));
84- Assert .assertEquals (row .getTuple ("tuple1" ), dto .getTuple1 ());
85- Assert .assertEquals (row .getGeoPoint ("point1" ).getValue (), dto .getPoint1 ());
86- }
129+ private List <String > words ;
130+
131+ private int [] numbers ;
87132
133+ private String [] letters ;
134+
135+ public static String tblCreateSQL (String table ) {
136+ return tableDefinition (table , "rowId Int16" , "words Array(String)" , "numbers Array(Int32)" ,
137+ "letters Array(String)" );
138+ }
88139 }
89140
90141 @ Test (groups = {"integration" })
91142 public void testVariantWithSimpleDataTypes () throws Exception {
92143 final String table = "test_variant_primitives" ;
93144 final DataTypesTestingPOJO sample = new DataTypesTestingPOJO ();
94- System .out .println ("sample: " + sample );
95145
96146 dataTypesLoop :
97147 for (ClickHouseDataType dataType : ClickHouseDataType .values ()) {
@@ -168,7 +218,7 @@ public void testVariantWithSimpleDataTypes() throws Exception {
168218 case DateTime :
169219 case DateTime32 :
170220 strValue = row .getLocalDateTime ("field" ).truncatedTo (ChronoUnit .SECONDS ).toString ();
171- value = ((LocalDateTime )value ).truncatedTo (ChronoUnit .SECONDS ).toString ();
221+ value = ((LocalDateTime ) value ).truncatedTo (ChronoUnit .SECONDS ).toString ();
172222 break ;
173223 case Point :
174224 strValue = row .getGeoPoint ("field" ).toString ();
@@ -183,7 +233,7 @@ public void testVariantWithSimpleDataTypes() throws Exception {
183233 strValue = row .getGeoMultiPolygon ("field" ).toString ();
184234 break ;
185235 }
186- System .out .println ("field: " + strValue + " value " + value );
236+ System .out .println ("field: " + strValue + " value " + value );
187237 if (value .getClass ().isPrimitive ()) {
188238 Assert .assertEquals (strValue , String .valueOf (value ));
189239 } else {
@@ -222,17 +272,41 @@ public void testVariantWithDecimals() throws Exception {
222272 });
223273 }
224274
225- @ Test (groups = {"integration" }, enabled = false )
275+ @ Test (groups = {"integration" })
226276 public void testVariantWithArrays () throws Exception {
227- // TODO: writing array would need custom serialization logic
228277 testVariantWith ("arrays" , new String []{"field Variant(String, Array(String))" },
229278 new Object []{
230279 "a,b" ,
231- new String []{"a" , "b" }
280+ new String []{"a" , "b" },
281+ Arrays .asList ("c" , "d" )
232282 },
233283 new String []{
234284 "a,b" ,
235- "a,b" ,
285+ "[a, b]" ,
286+ "[c, d]"
287+ });
288+ testVariantWith ("arrays" , new String []{"field Variant(Array(String), Array(Int32))" },
289+ new Object []{
290+ new int []{1 , 2 },
291+ new String []{"a" , "b" },
292+ Arrays .asList ("c" , "d" )
293+ },
294+ new String []{
295+ "[1, 2]" ,
296+ "[a, b]" ,
297+ "[c, d]" ,
298+ });
299+
300+ testVariantWith ("arrays" , new String []{"field Variant(Array(Array(String)), Array(Array(Int32)))" },
301+ new Object []{
302+ new int [][]{ new int [] {1 , 2 }, new int [] { 3 , 4 }},
303+ new String [][]{new String []{"a" , "b" }, new String []{"c" , "d" }},
304+ // Arrays.asList(Arrays.asList("e", "f"), Arrays.asList("j", "h"))
305+ },
306+ new String []{
307+ "[[1, 2], [3, 4]]" ,
308+ "[[a, b], [c, d]]" ,
309+ // "[c, d]",
236310 });
237311 }
238312
@@ -268,7 +342,7 @@ public void testVariantWithTuple() throws Exception {
268342 testVariantWith ("arrays" , new String []{"field Variant(String, Tuple(Int32, Float32))" },
269343 new Object []{
270344 "10,0.34" ,
271- new Object [] { 10 , 0.34f }
345+ new Object []{ 10 , 0.34f }
272346 },
273347 new String []{
274348 "10,0.34" ,
0 commit comments