1414import com .clickhouse .client .api .ClientException ;
1515import com .clickhouse .client .api .DataTypeUtils ;
1616import com .clickhouse .client .api .ServerException ;
17- import com .clickhouse .client .api .enums .Protocol ;
1817import com .clickhouse .client .api .data_formats .ClickHouseBinaryFormatReader ;
1918import com .clickhouse .client .api .data_formats .NativeFormatReader ;
2019import com .clickhouse .client .api .data_formats .RowBinaryFormatReader ;
2120import com .clickhouse .client .api .data_formats .RowBinaryWithNamesAndTypesFormatReader ;
2221import com .clickhouse .client .api .data_formats .RowBinaryWithNamesFormatReader ;
22+ import com .clickhouse .client .api .enums .Protocol ;
2323import com .clickhouse .client .api .insert .InsertSettings ;
2424import com .clickhouse .client .api .metadata .TableSchema ;
2525import com .clickhouse .client .api .metrics .ClientMetrics ;
5252import java .math .BigInteger ;
5353import java .time .LocalDate ;
5454import java .time .LocalDateTime ;
55- import java .time .OffsetDateTime ;
5655import java .time .ZoneId ;
5756import java .time .ZonedDateTime ;
58- import java .time .temporal .ChronoUnit ;
5957import java .util .ArrayList ;
6058import java .util .Arrays ;
6159import java .util .HashMap ;
60+ import java .util .HashSet ;
6261import java .util .Iterator ;
6362import java .util .List ;
6463import java .util .Map ;
65- import java .util .NoSuchElementException ;
66- import java .util .Properties ;
6764import java .util .Random ;
65+ import java .util .Set ;
6866import java .util .UUID ;
6967import java .util .concurrent .ExecutionException ;
7068import java .util .concurrent .Future ;
7371import java .util .function .Function ;
7472import java .util .function .Supplier ;
7573import java .util .stream .BaseStream ;
74+ import java .util .stream .Collectors ;
7675
7776public class QueryTests extends BaseIntegrationTest {
7877
@@ -146,19 +145,49 @@ public void testSimpleQueryWithTSV() {
146145
147146 @ Test (groups = {"integration" })
148147 public void testReadRecords () throws Exception {
149- prepareDataSet (DATASET_TABLE , DATASET_COLUMNS , DATASET_VALUE_GENERATORS , 10 );
150-
148+ List < Map < String , Object >> dataset = prepareDataSet (DATASET_TABLE , DATASET_COLUMNS , DATASET_VALUE_GENERATORS , 10 );
149+
151150 Records records = client .queryRecords ("SELECT * FROM " + DATASET_TABLE ).get (3 , TimeUnit .SECONDS );
152151 Assert .assertEquals (records .getResultRows (), 10 , "Unexpected number of rows" );
152+
153+ Iterator <Map <String , Object >> dataIterator = dataset .iterator ();
153154 for (GenericRecord record : records ) {
154- System .out .println (record .getLong (1 )); // UInt32 column col1
155- System .out .println (record .getInteger (2 )); // Int32 column col2
156- System .out .println (record .getString (3 )); // string column col3
157- System .out .println (record .getLong (4 )); // Int64 column col4
158- System .out .println (record .getString (5 )); // string column col5
155+ Map <String ,Object > dsRecords = dataIterator .next ();
156+ Assert .assertEquals (record .getLong ("col1" ), dsRecords .get ("col1" ));
157+ Assert .assertEquals (record .getInteger ("col2" ), dsRecords .get ("col2" ));
158+ Assert .assertEquals (record .getString ("col3" ), dsRecords .get ("col3" ));
159+ Assert .assertEquals (record .getLong ("col4" ), dsRecords .get ("col4" ));
160+ Assert .assertEquals (record .getString ("col5" ), dsRecords .get ("col5" ));
159161 }
160162 }
161163
164+ @ Test (groups = {"integration" })
165+ public void testReadRecordsWithStreamAPI () throws Exception {
166+ final int tables = 10 ;
167+
168+ Set <String > expectedTableNames = new HashSet <>();
169+ for (int i = 0 ; i < tables ; i ++) {
170+ final String tableName = "a_" + i ;
171+ expectedTableNames .add (tableName );
172+ client .execute ("DROP TABLE IF EXISTS default." + tableName );
173+ client .execute ("CREATE TABLE " + tableName +" (x UInt32) ENGINE = Memory" );
174+ }
175+
176+ Records records = client .queryRecords ("SHOW TABLES" ).get (3 , TimeUnit .SECONDS );
177+
178+ HashSet <String > tableNames = new HashSet <>();
179+ records .forEach (r -> {
180+ tableNames .add (r .getString (1 ));
181+ });
182+ Assert .assertTrue (tableNames .containsAll (expectedTableNames ));
183+
184+ Assert .expectThrows (IllegalStateException .class , () -> {
185+ records .forEach (r -> {
186+ System .out .println (r );
187+ });
188+ });
189+ }
190+
162191 @ Test (groups = {"integration" })
163192 public void testReadRecordsGetFirstRecord () throws Exception {
164193 prepareDataSet (DATASET_TABLE , DATASET_COLUMNS , DATASET_VALUE_GENERATORS , 10 );
@@ -184,12 +213,13 @@ public void testQueryAll() throws Exception {
184213 List <GenericRecord > records = client .queryAll ("SELECT * FROM " + DATASET_TABLE + " LIMIT " + dataset .size ());
185214 Assert .assertFalse (records .isEmpty ());
186215
187- int i = 0 ;
188216 for (String colDefinition : DATASET_COLUMNS ) {
189-
217+ // result values
190218 String colName = colDefinition .split (" " )[0 ];
191219 List <Object > colValues = records .stream ().map (r -> r .getObject (colName )).toList ();
192220 Assert .assertEquals (colValues .size (), dataset .size ());
221+
222+ // dataset values
193223 List <Object > dataValue = dataset .stream ().map (d -> d .get (colName )).toList ();
194224 Assert .assertEquals (colValues , dataValue , "Failed for column " + colName );
195225 }
@@ -201,6 +231,24 @@ public void testQueryAllNoResult() throws Exception {
201231 Assert .assertTrue (records .isEmpty ());
202232 }
203233
234+ @ Test
235+ public void testQueryAllTableNames () {
236+ final int tables = 10 ;
237+ Set <String > expectedTableNames = new HashSet <>();
238+ for (int i = 0 ; i < tables ; i ++) {
239+ final String tableName = "a_" + i ;
240+ expectedTableNames .add (tableName );
241+ client .execute ("DROP TABLE IF EXISTS default." + tableName );
242+ client .execute ("CREATE TABLE " + tableName +" (x UInt32) ENGINE = Memory" );
243+ }
244+
245+ List <GenericRecord > records = client .queryAll ("SHOW TABLES" );
246+ Assert .assertTrue (records .size () >= tables );
247+
248+ Set <String > tableNames = records .stream ().map (r -> r .getString (1 )).collect (Collectors .toSet ());
249+ Assert .assertTrue (tableNames .containsAll (expectedTableNames ));
250+ }
251+
204252 @ Test (groups = {"integration" })
205253 public void testQueryJSON () throws ExecutionException , InterruptedException {
206254 Map <String , Object > datasetRecord = prepareSimpleDataSet ();
@@ -952,7 +1000,6 @@ public void testDataTypes(List<String> columns, List<Supplier<String>> valueGene
9521000 colIndex ++;
9531001 try {
9541002 verifier .accept (reader );
955- System .out .println ("Verified " + colIndex );
9561003 } catch (Exception e ) {
9571004 Assert .fail ("Failed to verify " + columns .get (colIndex ), e );
9581005 }
@@ -977,8 +1024,6 @@ public void testQueryMetrics() throws Exception {
9771024
9781025 // Stats should be available after the query is done
9791026 OperationMetrics metrics = response .getMetrics ();
980- System .out .println ("Server read rows: " + metrics .getMetric (ServerMetrics .NUM_ROWS_READ ).getLong ());
981- System .out .println ("Client stats: " + metrics .getMetric (ClientMetrics .OP_DURATION ).getLong ());
9821027
9831028 Assert .assertEquals (metrics .getMetric (ServerMetrics .NUM_ROWS_READ ).getLong (), 10 ); // 10 rows in the table
9841029 Assert .assertEquals (metrics .getMetric (ServerMetrics .RESULT_ROWS ).getLong (), 3 );
@@ -995,8 +1040,6 @@ public void testQueryMetrics() throws Exception {
9951040 response = client .query (insertStmtBuilder .toString (), settings ).get ();
9961041
9971042 metrics = response .getMetrics ();
998- System .out .println ("Server read rows: " + metrics .getMetric (ServerMetrics .NUM_ROWS_READ ).getLong ());
999- System .out .println ("Client stats: " + metrics .getMetric (ClientMetrics .OP_DURATION ).getLong ());
10001043
10011044 Assert .assertEquals (metrics .getMetric (ServerMetrics .NUM_ROWS_READ ).getLong (), rowsToInsert ); // 10 rows in the table
10021045 Assert .assertEquals (metrics .getMetric (ServerMetrics .RESULT_ROWS ).getLong (), rowsToInsert );
@@ -1064,7 +1107,6 @@ private List<Map<String, Object>> prepareDataSet(String table, List<String> colu
10641107 insertStmtBuilder .append ("), " );
10651108 data .add (values );
10661109 }
1067- System .out .println ("Insert statement: " + insertStmtBuilder );
10681110 request = client .write (getServer (ClickHouseProtocol .HTTP ))
10691111 .query (insertStmtBuilder .toString ());
10701112 try (ClickHouseResponse response = request .executeAndWait ()) {}
@@ -1209,9 +1251,7 @@ public void testServerTimeZoneFromHeader() {
12091251 reader .next ();
12101252
12111253 LocalDateTime serverTime = reader .getLocalDateTime (1 );
1212- System .out .println ("Server time: " + serverTime );
12131254 LocalDateTime serverUtcTime = reader .getLocalDateTime (2 );
1214- System .out .println ("Server UTC time: " + serverUtcTime );
12151255
12161256 ZonedDateTime serverTimeZ = serverTime .atZone (ZoneId .of (requestTimeZone ));
12171257 ZonedDateTime serverUtcTimeZ = serverUtcTime .atZone (ZoneId .of ("UTC" ));
@@ -1242,11 +1282,8 @@ public void testClientUseOwnTimeZone() {
12421282 reader .next ();
12431283
12441284 LocalDateTime serverTime = reader .getLocalDateTime (1 ); // in "America/Los_Angeles"
1245- System .out .println ("Server time: " + serverTime );
12461285 LocalDateTime serverUtcTime = reader .getLocalDateTime (2 );
1247- System .out .println ("Server UTC time: " + serverUtcTime );
12481286 ZonedDateTime serverLisbonTime = reader .getZonedDateTime (3 ); // in "Europe/Lisbon"
1249- System .out .println ("Server Lisbon time: " + serverLisbonTime );
12501287
12511288 ZonedDateTime serverTimeZ = serverTime .atZone (ZoneId .of ("America/Los_Angeles" ));
12521289 ZonedDateTime serverUtcTimeZ = serverUtcTime .atZone (ZoneId .of ("UTC" ));
0 commit comments