1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
use crate::{
block::Block,
connection::{
Connection,
ConnectionOptions,
},
io::{
BlockReader,
BlockWriter,
},
protocol::{
ClientCode,
CompressionMethod,
ServerCode,
},
query::{
ClientInfo,
Profile,
Progress,
Query,
ServerInfo,
},
Error,
Result,
};
use std::time::Duration;
use tracing::debug;
#[cfg(feature = "tls")]
use crate::ssl::SSLOptions;
/// Endpoint configuration (host + port)
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Endpoint {
/// Server host
pub host: String,
/// Server port
pub port: u16,
}
impl Endpoint {
/// Create a new endpoint
pub fn new(host: impl Into<String>, port: u16) -> Self {
Self { host: host.into(), port }
}
}
/// Client options
#[derive(Clone, Debug)]
pub struct ClientOptions {
/// Server host (used if endpoints is empty)
pub host: String,
/// Server port (used if endpoints is empty)
pub port: u16,
/// Multiple endpoints for failover (if empty, uses host+port)
pub endpoints: Vec<Endpoint>,
/// Database name
pub database: String,
/// Username
pub user: String,
/// Password
pub password: String,
/// Compression method
pub compression: Option<CompressionMethod>,
/// Maximum compression chunk size (default: 65535)
pub max_compression_chunk_size: usize,
/// Client information
pub client_info: ClientInfo,
/// Connection timeout and TCP options
pub connection_options: ConnectionOptions,
/// SSL/TLS options (requires 'tls' feature)
#[cfg(feature = "tls")]
pub ssl_options: Option<SSLOptions>,
/// Number of send retries (default: 1, no retry)
pub send_retries: u32,
/// Timeout between retry attempts (default: 5 seconds)
pub retry_timeout: Duration,
/// Send ping before each query (default: false)
pub ping_before_query: bool,
/// Rethrow server exceptions (default: true)
pub rethrow_exceptions: bool,
}
impl Default for ClientOptions {
fn default() -> Self {
Self {
host: "localhost".to_string(),
port: 9000,
endpoints: Vec::new(),
database: "default".to_string(),
user: "default".to_string(),
password: String::new(),
compression: Some(CompressionMethod::Lz4),
max_compression_chunk_size: 65535,
client_info: ClientInfo::default(),
connection_options: ConnectionOptions::default(),
#[cfg(feature = "tls")]
ssl_options: None,
send_retries: 1,
retry_timeout: Duration::from_secs(5),
ping_before_query: false,
rethrow_exceptions: true,
}
}
}
impl ClientOptions {
/// Create new client options with host and port
pub fn new(host: impl Into<String>, port: u16) -> Self {
Self { host: host.into(), port, ..Default::default() }
}
/// Set multiple endpoints for failover
pub fn endpoints(mut self, endpoints: Vec<Endpoint>) -> Self {
self.endpoints = endpoints;
self
}
/// Add an endpoint for failover
pub fn add_endpoint(mut self, host: impl Into<String>, port: u16) -> Self {
self.endpoints.push(Endpoint::new(host, port));
self
}
/// Set the database
pub fn database(mut self, database: impl Into<String>) -> Self {
self.database = database.into();
self
}
/// Set the username
pub fn user(mut self, user: impl Into<String>) -> Self {
self.user = user.into();
self
}
/// Set the password
pub fn password(mut self, password: impl Into<String>) -> Self {
self.password = password.into();
self
}
/// Set compression method
pub fn compression(mut self, method: Option<CompressionMethod>) -> Self {
self.compression = method;
self
}
/// Set maximum compression chunk size
pub fn max_compression_chunk_size(mut self, size: usize) -> Self {
self.max_compression_chunk_size = size;
self
}
/// Set connection options (timeouts, TCP settings)
pub fn connection_options(mut self, options: ConnectionOptions) -> Self {
self.connection_options = options;
self
}
/// Set number of send retries
pub fn send_retries(mut self, retries: u32) -> Self {
self.send_retries = retries;
self
}
/// Set retry timeout
pub fn retry_timeout(mut self, timeout: Duration) -> Self {
self.retry_timeout = timeout;
self
}
/// Enable/disable ping before query
pub fn ping_before_query(mut self, enabled: bool) -> Self {
self.ping_before_query = enabled;
self
}
/// Enable/disable exception rethrowing
pub fn rethrow_exceptions(mut self, enabled: bool) -> Self {
self.rethrow_exceptions = enabled;
self
}
/// Set SSL/TLS options (requires 'tls' feature)
#[cfg(feature = "tls")]
pub fn ssl_options(mut self, options: SSLOptions) -> Self {
self.ssl_options = Some(options);
self
}
/// Get all endpoints (including host+port if endpoints is empty)
pub(crate) fn get_endpoints(&self) -> Vec<Endpoint> {
if self.endpoints.is_empty() {
vec![Endpoint::new(&self.host, self.port)]
} else {
self.endpoints.clone()
}
}
}
/// Async ClickHouse client using the native TCP protocol.
///
/// Create a client by calling [`Client::connect`] with [`ClientOptions`].
/// The client holds a single TCP connection and is not `Clone`; for
/// concurrent access, create multiple client instances.
pub struct Client {
conn: Connection,
server_info: ServerInfo,
block_reader: BlockReader,
block_writer: BlockWriter,
options: ClientOptions,
}
impl Client {
/// Connect to ClickHouse server with retry and endpoint failover
pub async fn connect(options: ClientOptions) -> Result<Self> {
let endpoints = options.get_endpoints();
let mut last_error = None;
// Try each endpoint with retries
for endpoint in &endpoints {
for attempt in 0..options.send_retries {
match Self::try_connect(
&endpoint.host,
endpoint.port,
&options,
)
.await
{
Ok(client) => return Ok(client),
Err(e) => {
last_error = Some(e);
// Wait before retry (except for last attempt)
if attempt + 1 < options.send_retries {
tokio::time::sleep(options.retry_timeout).await;
}
}
}
}
}
// All endpoints and retries failed
Err(last_error.unwrap_or_else(|| {
Error::Connection("No endpoints available".to_string())
}))
}
/// Try to connect to a specific endpoint
async fn try_connect(
host: &str,
port: u16,
options: &ClientOptions,
) -> Result<Self> {
// Connect with or without TLS based on options
let mut conn = {
#[cfg(feature = "tls")]
{
if let Some(ref ssl_opts) = options.ssl_options {
// Build SSL client config
let ssl_config = ssl_opts.build_client_config()?;
// Use server name from SSL options if provided, otherwise
// use host
let server_name = ssl_opts
.server_name
.as_deref()
.or(if ssl_opts.use_sni { Some(host) } else { None });
Connection::connect_with_tls(
host,
port,
&options.connection_options,
ssl_config,
server_name,
)
.await?
} else {
Connection::connect_with_options(
host,
port,
&options.connection_options,
)
.await?
}
}
#[cfg(not(feature = "tls"))]
{
Connection::connect_with_options(
host,
port,
&options.connection_options,
)
.await?
}
};
// Send hello
Self::send_hello(&mut conn, options).await?;
// Receive hello
let server_info = Self::receive_hello(&mut conn).await?;
// Send addendum (quota key) if server supports it
// DBMS_MIN_PROTOCOL_VERSION_WITH_ADDENDUM = 54458
if server_info.revision >= 54458 {
debug!("Sending quota key addendum (empty string)...");
conn.write_string("").await?;
conn.flush().await?;
debug!("Addendum sent");
}
// Create block reader/writer with compression
let mut block_reader = BlockReader::new(server_info.revision);
let mut block_writer = BlockWriter::new(server_info.revision);
// Enable compression on both reader and writer
if let Some(compression) = options.compression {
block_reader = block_reader.with_compression(compression);
block_writer = block_writer.with_compression(compression);
}
Ok(Self {
conn,
server_info,
block_reader,
block_writer,
options: options.clone(),
})
}
/// Send hello packet
async fn send_hello(
conn: &mut Connection,
options: &ClientOptions,
) -> Result<()> {
debug!("Sending client hello...");
// Write client hello code
conn.write_varint(ClientCode::Hello as u64).await?;
debug!("Sent hello code");
// Write client name and version
conn.write_string(&options.client_info.client_name).await?;
debug!("Sent client name: {}", options.client_info.client_name);
conn.write_varint(options.client_info.client_version_major).await?;
conn.write_varint(options.client_info.client_version_minor).await?;
conn.write_varint(options.client_info.client_revision).await?;
debug!(
"Sent version: {}.{}.{}",
options.client_info.client_version_major,
options.client_info.client_version_minor,
options.client_info.client_revision
);
// Write database, user, password
conn.write_string(&options.database).await?;
conn.write_string(&options.user).await?;
conn.write_string(&options.password).await?;
debug!("Sent credentials");
conn.flush().await?;
debug!("Flushed");
Ok(())
}
/// Receive hello packet from server
async fn receive_hello(conn: &mut Connection) -> Result<ServerInfo> {
debug!("Reading server hello...");
let packet_type = conn.read_varint().await?;
debug!("Got packet type: {}", packet_type);
if packet_type != ServerCode::Hello as u64 {
if packet_type == ServerCode::Exception as u64 {
debug!("Server sent exception during handshake!");
let exception = Self::read_exception_from_conn(conn).await?;
debug!(
"Exception: code={}, name={}, msg={}",
exception.code, exception.name, exception.display_text
);
return Err(Error::Protocol(format!(
"ClickHouse exception during handshake: {} (code {}): {}",
exception.name, exception.code, exception.display_text
)));
}
debug!("Unexpected packet type: {}", packet_type);
return Err(Error::Protocol(format!(
"Expected Hello packet, got {}",
packet_type
)));
}
// Read server info
debug!("Reading server info...");
let name = conn.read_string().await?;
debug!("Server name: {}", name);
let version_major = conn.read_varint().await?;
let version_minor = conn.read_varint().await?;
let revision = conn.read_varint().await?;
debug!(
"Server version: {}.{}, revision: {}",
version_major, version_minor, revision
);
let timezone = if revision >= 54058 {
debug!("Reading timezone...");
conn.read_string().await?
} else {
String::new()
};
let display_name = if revision >= 54372 {
debug!("Reading display name...");
conn.read_string().await?
} else {
String::new()
};
let version_patch = if revision >= 54401 {
debug!("Reading version patch...");
conn.read_varint().await?
} else {
0
};
debug!("Server hello complete!");
Ok(ServerInfo {
name,
version_major,
version_minor,
version_patch,
revision,
timezone,
display_name,
})
}
/// Execute a DDL/DML query without returning data
///
/// Use this for queries that don't return result sets:
/// - CREATE/DROP TABLE, DATABASE
/// - ALTER TABLE
/// - TRUNCATE
/// - Other DDL/DML operations
///
/// For SELECT queries, use `query()` instead.
/// For query tracing, use `execute_with_id()`.
///
/// # Example
/// ```no_run
/// # use clickhouse_native_client::{Client, ClientOptions};
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let mut client = Client::connect(ClientOptions::default()).await?;
/// client.execute("CREATE TABLE test (id UInt32) ENGINE = Memory").await?;
/// client.execute("DROP TABLE test").await?;
/// # Ok(())
/// # }
/// ```
pub async fn execute(&mut self, query: impl Into<Query>) -> Result<()> {
self.execute_with_id(query, "").await
}
/// Execute a DDL/DML query with a specific query ID
///
/// The query ID is useful for query tracing and debugging.
///
/// # Example
/// ```no_run
/// # use clickhouse_native_client::{Client, ClientOptions};
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let mut client = Client::connect(ClientOptions::default()).await?;
/// client.execute_with_id("CREATE TABLE test (id UInt32) ENGINE = Memory", "create-123").await?;
/// # Ok(())
/// # }
/// ```
pub async fn execute_with_id(
&mut self,
query: impl Into<Query>,
query_id: &str,
) -> Result<()> {
let mut query = query.into();
if !query_id.is_empty() {
query = Query::new(query.text()).with_query_id(query_id);
}
self.send_query(&query).await?;
// Read responses until EndOfStream, but don't collect blocks
loop {
let packet_type = self.conn.read_varint().await?;
match packet_type {
code if code == ServerCode::Data as u64 => {
// Skip data blocks (shouldn't happen for DDL, but handle
// gracefully)
if self.server_info.revision >= 50264 {
let _temp_table = self.conn.read_string().await?;
}
let _block =
self.block_reader.read_block(&mut self.conn).await?;
}
code if code == ServerCode::Progress as u64 => {
let progress = self.read_progress().await?;
// Invoke progress callback if present
if let Some(callback) = query.get_on_progress() {
callback(&progress);
}
}
code if code == ServerCode::EndOfStream as u64 => {
break;
}
code if code == ServerCode::Exception as u64 => {
let exception = self.read_exception().await?;
// Invoke exception callback if present
if let Some(callback) = query.get_on_exception() {
callback(&exception);
}
return Err(Error::Protocol(format!(
"ClickHouse exception: {} (code {}): {}",
exception.name, exception.code, exception.display_text
)));
}
code if code == ServerCode::ProfileInfo as u64 => {
// Read profile info
let rows = self.conn.read_varint().await?;
let blocks = self.conn.read_varint().await?;
let bytes = self.conn.read_varint().await?;
let applied_limit = self.conn.read_u8().await?;
let rows_before_limit = self.conn.read_varint().await?;
let calculated = self.conn.read_u8().await?;
let profile = Profile {
rows,
blocks,
bytes,
applied_limit: applied_limit != 0,
rows_before_limit,
calculated_rows_before_limit: calculated != 0,
};
// Invoke profile callback if present
if let Some(callback) = query.get_on_profile() {
callback(&profile);
}
}
code if code == ServerCode::Log as u64 => {
let _log_tag = self.conn.read_string().await?;
// Log blocks are sent uncompressed
let uncompressed_reader =
BlockReader::new(self.server_info.revision);
let block =
uncompressed_reader.read_block(&mut self.conn).await?;
// Invoke server log callback if present
if let Some(callback) = query.get_on_server_log() {
callback(&block);
}
}
code if code == ServerCode::ProfileEvents as u64 => {
let _table_name = self.conn.read_string().await?;
// ProfileEvents blocks are sent uncompressed
let uncompressed_reader =
BlockReader::new(self.server_info.revision);
let block =
uncompressed_reader.read_block(&mut self.conn).await?;
// Invoke profile events callback if present
if let Some(callback) = query.get_on_profile_events() {
callback(&block);
}
}
code if code == ServerCode::TableColumns as u64 => {
let _table_name = self.conn.read_string().await?;
let _columns_metadata = self.conn.read_string().await?;
}
_ => {
return Err(Error::Protocol(format!(
"Unexpected packet type during execute: {}",
packet_type
)));
}
}
}
Ok(())
}
/// Execute a query and return results
///
/// For INSERT operations, use `insert()` instead.
/// For DDL/DML without results, use `execute()` instead.
/// For query tracing, use `query_with_id()`.
pub async fn query(
&mut self,
query: impl Into<Query>,
) -> Result<QueryResult> {
self.query_with_id(query, "").await
}
/// Execute a query with a specific query ID and return results
///
/// The query ID is useful for query tracing and debugging.
///
/// # Example
/// ```no_run
/// # use clickhouse_native_client::{Client, ClientOptions};
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let mut client = Client::connect(ClientOptions::default()).await?;
/// let result = client.query_with_id("SELECT 1", "select-123").await?;
/// # Ok(())
/// # }
/// ```
pub async fn query_with_id(
&mut self,
query: impl Into<Query>,
query_id: &str,
) -> Result<QueryResult> {
let mut query = query.into();
if !query_id.is_empty() {
query = Query::new(query.text()).with_query_id(query_id);
}
// Send query
self.send_query(&query).await?;
// Receive results
let mut blocks = Vec::new();
let mut progress_info = Progress::default();
loop {
let packet_type = self.conn.read_varint().await?;
debug!("Query response packet: {}", packet_type);
match packet_type {
code if code == ServerCode::Data as u64 => {
debug!("Received data packet");
// Skip temp table name if protocol supports it (matches
// C++ ReceiveData)
if self.server_info.revision >= 50264 {
// DBMS_MIN_REVISION_WITH_TEMPORARY_TABLES
let _temp_table = self.conn.read_string().await?;
}
let block =
self.block_reader.read_block(&mut self.conn).await?;
// Invoke data callback if present
if let Some(callback) = query.get_on_data_cancelable() {
let should_continue = callback(&block);
if !should_continue {
debug!("Query cancelled by data callback");
break;
}
} else if let Some(callback) = query.get_on_data() {
callback(&block);
}
if !block.is_empty() {
blocks.push(block);
}
}
code if code == ServerCode::Progress as u64 => {
debug!("Received progress packet");
let delta = self.read_progress().await?;
progress_info.rows += delta.rows;
progress_info.bytes += delta.bytes;
progress_info.total_rows = delta.total_rows;
progress_info.written_rows += delta.written_rows;
progress_info.written_bytes += delta.written_bytes;
// Invoke progress callback if present
if let Some(callback) = query.get_on_progress() {
callback(&progress_info);
}
}
code if code == ServerCode::EndOfStream as u64 => {
debug!("Received end of stream");
break;
}
code if code == ServerCode::ProfileInfo as u64 => {
debug!("Received profile info packet");
// Read ProfileInfo fields directly
let rows = self.conn.read_varint().await?;
let blocks = self.conn.read_varint().await?;
let bytes = self.conn.read_varint().await?;
let applied_limit = self.conn.read_u8().await? != 0;
let rows_before_limit = self.conn.read_varint().await?;
let calculated_rows_before_limit =
self.conn.read_u8().await? != 0;
let profile = crate::query::Profile {
rows,
blocks,
bytes,
rows_before_limit,
applied_limit,
calculated_rows_before_limit,
};
// Invoke profile callback if present
if let Some(callback) = query.get_on_profile() {
callback(&profile);
}
}
code if code == ServerCode::Log as u64 => {
debug!("Received log packet");
// Skip string first (log tag)
let _log_tag = self.conn.read_string().await?;
// Read the log block (sent uncompressed)
let uncompressed_reader =
BlockReader::new(self.server_info.revision);
let block =
uncompressed_reader.read_block(&mut self.conn).await?;
// Invoke server log callback if present
if let Some(callback) = query.get_on_server_log() {
callback(&block);
}
}
code if code == ServerCode::ProfileEvents as u64 => {
debug!("Received profile events packet");
// Skip string first (matches C++ implementation)
let _table_name = self.conn.read_string().await?;
// Read ProfileEvents block (sent uncompressed)
let uncompressed_reader =
BlockReader::new(self.server_info.revision);
let block =
uncompressed_reader.read_block(&mut self.conn).await?;
// Invoke profile events callback if present
if let Some(callback) = query.get_on_profile_events() {
callback(&block);
}
}
code if code == ServerCode::TableColumns as u64 => {
debug!("Received table columns packet (ignoring)");
// Skip external table name
let _table_name = self.conn.read_string().await?;
// Skip columns metadata string
let _columns_metadata = self.conn.read_string().await?;
}
code if code == ServerCode::Exception as u64 => {
debug!("Server returned exception during query, reading details...");
let exception = self.read_exception().await?;
debug!(
"Exception: code={}, name={}, msg={}",
exception.code, exception.name, exception.display_text
);
// Invoke exception callback if present
if let Some(callback) = query.get_on_exception() {
callback(&exception);
}
return Err(Error::Protocol(format!(
"ClickHouse exception: {} ({}): {}",
exception.name, exception.code, exception.display_text
)));
}
other => {
debug!("Unexpected packet type: {}", other);
return Err(Error::Protocol(format!(
"Unexpected packet type: {}",
other
)));
}
}
}
Ok(QueryResult { blocks, progress: progress_info })
}
/// Execute a SELECT query with external tables for JOIN operations
///
/// External tables allow passing temporary in-memory data to queries for
/// JOINs without creating actual tables in ClickHouse.
///
/// # Example
/// ```no_run
/// # use clickhouse_native_client::{Client, ClientOptions, Block, ExternalTable};
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let mut client = Client::connect(ClientOptions::default()).await?;
/// // Create a block with temporary data
/// let mut block = Block::new();
/// // ... populate block with data ...
///
/// // Create external table
/// let ext_table = ExternalTable::new("temp_table", block);
///
/// // Use in query with JOIN
/// let query = "SELECT * FROM my_table JOIN temp_table ON my_table.id = temp_table.id";
/// let result = client.query_with_external_data(query, &[ext_table]).await?;
/// # Ok(())
/// # }
/// ```
pub async fn query_with_external_data(
&mut self,
query: impl Into<Query>,
external_tables: &[crate::ExternalTable],
) -> Result<QueryResult> {
self.query_with_external_data_and_id(query, "", external_tables).await
}
/// Execute a SELECT query with external tables and a specific query ID
///
/// Combines external table support with query ID tracing.
///
/// # Example
/// ```no_run
/// # use clickhouse_native_client::{Client, ClientOptions, Block, ExternalTable};
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let mut client = Client::connect(ClientOptions::default()).await?;
/// # let mut block = Block::new();
/// let ext_table = ExternalTable::new("temp_table", block);
/// let result = client.query_with_external_data_and_id(
/// "SELECT * FROM my_table JOIN temp_table ON my_table.id = temp_table.id",
/// "query-123",
/// &[ext_table]
/// ).await?;
/// # Ok(())
/// # }
/// ```
pub async fn query_with_external_data_and_id(
&mut self,
query: impl Into<Query>,
query_id: &str,
external_tables: &[crate::ExternalTable],
) -> Result<QueryResult> {
let mut query = query.into();
if !query_id.is_empty() {
query = Query::new(query.text()).with_query_id(query_id);
}
// Send query WITHOUT finalization (we'll finalize after external
// tables)
self.send_query_internal(&query, false).await?;
// Send external tables data (before finalization)
self.send_external_tables(external_tables).await?;
// Now finalize the query with empty block
self.finalize_query().await?;
// Receive results (same as regular query)
let mut blocks = Vec::new();
let mut progress_info = Progress::default();
loop {
let packet_type = self.conn.read_varint().await?;
debug!("Query response packet: {}", packet_type);
match packet_type {
code if code == ServerCode::Data as u64 => {
debug!("Received data packet");
// Skip temp table name if protocol supports it
if self.server_info.revision >= 50264 {
let _temp_table = self.conn.read_string().await?;
}
let block =
self.block_reader.read_block(&mut self.conn).await?;
// Invoke data callback if present
if let Some(callback) = query.get_on_data_cancelable() {
let should_continue = callback(&block);
if !should_continue {
debug!("Query cancelled by data callback");
break;
}
} else if let Some(callback) = query.get_on_data() {
callback(&block);
}
if !block.is_empty() {
blocks.push(block);
}
}
code if code == ServerCode::Progress as u64 => {
debug!("Received progress packet");
let delta = self.read_progress().await?;
progress_info.rows += delta.rows;
progress_info.bytes += delta.bytes;
progress_info.total_rows = delta.total_rows;
progress_info.written_rows += delta.written_rows;
progress_info.written_bytes += delta.written_bytes;
// Invoke progress callback if present
if let Some(callback) = query.get_on_progress() {
callback(&progress_info);
}
}
code if code == ServerCode::EndOfStream as u64 => {
debug!("Received end of stream");
break;
}
code if code == ServerCode::ProfileInfo as u64 => {
debug!("Received profile info packet");
let rows = self.conn.read_varint().await?;
let blocks = self.conn.read_varint().await?;
let bytes = self.conn.read_varint().await?;
let applied_limit = self.conn.read_u8().await?;
let rows_before_limit = self.conn.read_varint().await?;
let calculated = self.conn.read_u8().await?;
let profile = Profile {
rows,
blocks,
bytes,
applied_limit: applied_limit != 0,
rows_before_limit,
calculated_rows_before_limit: calculated != 0,
};
// Invoke profile callback if present
if let Some(callback) = query.get_on_profile() {
callback(&profile);
}
}
code if code == ServerCode::Log as u64 => {
debug!("Received log packet");
let _log_tag = self.conn.read_string().await?;
// Log blocks are sent uncompressed
let uncompressed_reader =
BlockReader::new(self.server_info.revision);
let block =
uncompressed_reader.read_block(&mut self.conn).await?;
// Invoke server log callback if present
if let Some(callback) = query.get_on_server_log() {
callback(&block);
}
}
code if code == ServerCode::ProfileEvents as u64 => {
debug!("Received profile events packet");
let _table_name = self.conn.read_string().await?;
// ProfileEvents blocks are sent uncompressed
let uncompressed_reader =
BlockReader::new(self.server_info.revision);
let block =
uncompressed_reader.read_block(&mut self.conn).await?;
// Invoke profile events callback if present
if let Some(callback) = query.get_on_profile_events() {
callback(&block);
}
}
code if code == ServerCode::TableColumns as u64 => {
debug!("Received table columns packet (ignoring)");
// Skip external table name
let _table_name = self.conn.read_string().await?;
// Skip columns metadata string
let _columns_metadata = self.conn.read_string().await?;
}
code if code == ServerCode::Exception as u64 => {
let exception = self.read_exception().await?;
debug!(
"Received exception: {} - {}",
exception.name, exception.display_text
);
// Invoke exception callback if present
if let Some(callback) = query.get_on_exception() {
callback(&exception);
}
return Err(Error::Protocol(format!(
"ClickHouse exception: {} (code {}): {}",
exception.name, exception.code, exception.display_text
)));
}
other => {
return Err(Error::Protocol(format!(
"Unexpected packet type during query: {}",
other
)));
}
}
}
Ok(QueryResult { blocks, progress: progress_info })
}
/// Send a query packet (always finalized)
async fn send_query(&mut self, query: &Query) -> Result<()> {
self.send_query_internal(query, true).await
}
/// Send a query packet (internal with finalization control)
async fn send_query_internal(
&mut self,
query: &Query,
finalize: bool,
) -> Result<()> {
debug!("Sending query: {}", query.text());
// Write query code
self.conn.write_varint(ClientCode::Query as u64).await?;
// Write query ID
self.conn.write_string(query.id()).await?;
debug!("Sent query ID");
// Client info
let revision = self.server_info.revision;
if revision >= 54032 {
debug!("Writing client info...");
let info = &self.options.client_info;
// Write client info fields in the correct order
self.conn.write_u8(1).await?; // query_kind = 1 (initial query)
self.conn.write_string(&info.initial_user).await?;
self.conn.write_string(&info.initial_query_id).await?;
self.conn.write_string("127.0.0.1:0").await?; // initial_address (client address:port)
if revision >= 54449 {
self.conn.write_i64(0).await?; // initial_query_start_time
}
self.conn.write_u8(info.interface_type).await?; // interface type (1 = TCP)
self.conn.write_string(&info.os_user).await?;
self.conn.write_string(&info.client_hostname).await?;
self.conn.write_string(&info.client_name).await?;
self.conn.write_varint(info.client_version_major).await?;
self.conn.write_varint(info.client_version_minor).await?;
self.conn.write_varint(info.client_revision).await?;
if revision >= 54060 {
self.conn.write_string(&info.quota_key).await?;
}
if revision >= 54448 {
self.conn.write_varint(0).await?; // distributed_depth
}
if revision >= 54401 {
self.conn.write_varint(info.client_version_patch).await?;
}
if revision >= 54442 {
// OpenTelemetry tracing context
if let Some(ctx) = query.tracing_context() {
self.conn.write_u8(1).await?; // have OpenTelemetry
// Write trace_id (128-bit)
self.conn.write_u128(ctx.trace_id).await?;
// Write span_id (64-bit)
self.conn.write_u64(ctx.span_id).await?;
// Write tracestate
self.conn.write_string(&ctx.tracestate).await?;
// Write trace_flags
self.conn.write_u8(ctx.trace_flags).await?;
} else {
self.conn.write_u8(0).await?; // no OpenTelemetry
}
}
if revision >= 54453 {
self.conn.write_varint(0).await?; // collaborate_with_initiator
self.conn.write_varint(0).await?; // count_participating_replicas
self.conn.write_varint(0).await?; // number_of_current_replica
}
debug!("Client info sent");
}
// Settings
if revision >= 54429 {
debug!("Writing settings...");
for (key, field) in query.settings() {
self.conn.write_string(key).await?;
self.conn.write_varint(field.flags).await?;
self.conn.write_string(&field.value).await?;
}
}
// Empty string to mark end of settings
self.conn.write_string("").await?;
debug!("Settings sent");
// Interserver secret (for servers >= 54441)
if revision >= 54441 {
self.conn.write_string("").await?; // empty interserver secret
}
// Query stage, compression, text
debug!("Writing query stage and text...");
self.conn.write_varint(2).await?; // Stage = Complete
// Enable compression if we have it configured
let compression_enabled =
if self.options.compression.is_some() { 1u64 } else { 0u64 };
self.conn.write_varint(compression_enabled).await?;
self.conn.write_string(query.text()).await?;
// Query parameters (for servers >= 54459)
if revision >= 54459 {
for (key, value) in query.parameters() {
self.conn.write_string(key).await?;
self.conn.write_varint(2).await?; // Custom type
self.conn.write_quoted_string(value).await?;
}
// Empty string to mark end of parameters
self.conn.write_string("").await?;
}
// Conditionally finalize based on parameter
if finalize {
self.finalize_query().await?;
}
Ok(())
}
/// Finalize query by sending empty block marker
///
/// Must be called after send_query_internal() to complete the query
/// protocol. For most queries, use send_query() which handles this
/// automatically. Only split for special cases like external tables.
async fn finalize_query(&mut self) -> Result<()> {
// Send empty block to finalize query (as per C++ client)
// This block must respect the compression setting we told the server
debug!("Sending empty block to finalize...");
self.conn.write_varint(ClientCode::Data as u64).await?;
let empty_block = Block::new();
// Create writer that matches the compression setting
let writer = if let Some(compression) = self.options.compression {
BlockWriter::new(self.server_info.revision)
.with_compression(compression)
} else {
BlockWriter::new(self.server_info.revision)
};
writer.write_block(&mut self.conn, &empty_block).await?;
self.conn.flush().await?;
debug!("Query finalized");
Ok(())
}
/// Send external tables data
///
/// External tables are sent as Data packets after the initial query
/// packet. Each table is sent with its name and block data.
/// Empty blocks are skipped to keep the connection in a consistent state.
async fn send_external_tables(
&mut self,
external_tables: &[crate::ExternalTable],
) -> Result<()> {
for table in external_tables {
// Skip empty blocks to keep connection consistent
if table.data.row_count() == 0 {
continue;
}
debug!("Sending external table: {}", table.name);
// Send Data packet type
self.conn.write_varint(ClientCode::Data as u64).await?;
// Send table name (this serves as the temp table name for this
// Data packet)
self.conn.write_string(&table.name).await?;
// Send block data WITHOUT temp table name prefix (we already wrote
// it above)
self.block_writer
.write_block_with_temp_table(
&mut self.conn,
&table.data,
false,
)
.await?;
}
self.conn.flush().await?;
Ok(())
}
/// Read progress info
async fn read_progress(&mut self) -> Result<Progress> {
let rows = self.conn.read_varint().await?;
let bytes = self.conn.read_varint().await?;
let total_rows = self.conn.read_varint().await?;
let (written_rows, written_bytes) = if self.server_info.revision
>= 54405
{
(self.conn.read_varint().await?, self.conn.read_varint().await?)
} else {
(0, 0)
};
Ok(Progress { rows, bytes, total_rows, written_rows, written_bytes })
}
/// Read exception from connection (static helper for use in contexts
/// without self)
fn read_exception_from_conn(
conn: &mut Connection,
) -> std::pin::Pin<
Box<
dyn std::future::Future<Output = Result<crate::query::Exception>>
+ '_,
>,
> {
use crate::query::Exception;
Box::pin(async move {
debug!("Reading exception code...");
let code = conn.read_i32().await?;
debug!("Exception code: {}", code);
debug!("Reading exception name...");
let name = conn.read_string().await?;
debug!("Exception name: {}", name);
debug!("Reading exception display_text...");
let display_text = conn.read_string().await?;
debug!("Exception display_text length: {}", display_text.len());
debug!("Reading exception stack_trace...");
let stack_trace = conn.read_string().await?;
debug!("Exception stack_trace length: {}", stack_trace.len());
// Check for nested exception
let has_nested = conn.read_u8().await?;
let nested = if has_nested != 0 {
Some(Box::new(Self::read_exception_from_conn(conn).await?))
} else {
None
};
Ok(Exception { code, name, display_text, stack_trace, nested })
})
}
/// Read exception from server
fn read_exception<'a>(
&'a mut self,
) -> std::pin::Pin<
Box<
dyn std::future::Future<Output = Result<crate::query::Exception>>
+ 'a,
>,
> {
Box::pin(async move {
Self::read_exception_from_conn(&mut self.conn).await
})
}
/// Insert data into a table
///
/// This method constructs an INSERT query from the block's column names
/// and sends the data. Example: `client.insert("my_database.my_table",
/// block).await?`
///
/// For query tracing, use `insert_with_id()` to specify a query ID.
pub async fn insert(
&mut self,
table_name: &str,
block: Block,
) -> Result<()> {
self.insert_with_id(table_name, "", block).await
}
/// Insert data into a table with a specific query ID
///
/// The query ID is useful for:
/// - Query tracing and debugging
/// - Correlating queries with logs
/// - OpenTelemetry integration
///
/// # Example
/// ```no_run
/// # use clickhouse_native_client::{Client, ClientOptions, Block};
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let mut client = Client::connect(ClientOptions::default()).await?;
/// # let block = Block::new();
/// client.insert_with_id("my_table", "trace-id-12345", block).await?;
/// # Ok(())
/// # }
/// ```
pub async fn insert_with_id(
&mut self,
table_name: &str,
query_id: &str,
block: Block,
) -> Result<()> {
// Build query with column names from block (matches C++
// implementation)
let col_names: Vec<String> = (0..block.column_count())
.filter_map(|i| block.column_name(i))
.map(|n| format!("`{}`", n.replace("`", "``"))) // Escape backticks
.collect();
if col_names.is_empty() {
return Err(Error::Protocol("Block has no columns".to_string()));
}
let query_text = format!(
"INSERT INTO {} ({}) VALUES",
table_name,
col_names.join(", ")
);
debug!("Sending INSERT query: {}", query_text);
let query = Query::new(query_text).with_query_id(query_id);
// Send query
self.send_query(&query).await?;
// Wait for server to respond with Data packet (matches C++ Insert
// flow)
debug!("Waiting for server Data packet...");
loop {
let packet_type = self.conn.read_varint().await?;
debug!("INSERT wait response packet type: {}", packet_type);
match packet_type {
code if code == ServerCode::Data as u64 => {
debug!("Received Data packet, ready to send data");
// CRITICAL: Must consume the Data packet's payload to keep
// stream aligned! Skip temp table name
if self.server_info.revision >= 50264 {
let _temp_table = self.conn.read_string().await?;
}
// Read the block (likely empty, but must consume it)
let _block =
self.block_reader.read_block(&mut self.conn).await?;
debug!("Consumed Data packet payload, stream aligned");
break;
}
code if code == ServerCode::Progress as u64 => {
debug!("Received Progress packet");
let _ = self.read_progress().await?;
}
code if code == ServerCode::TableColumns as u64 => {
debug!("Received TableColumns packet");
// Skip external table name
let _table_name = self.conn.read_string().await?;
// Skip columns metadata string
let _columns_metadata = self.conn.read_string().await?;
}
code if code == ServerCode::Exception as u64 => {
debug!("Server returned exception before accepting data");
let exception = self.read_exception().await?;
return Err(Error::Protocol(format!(
"ClickHouse exception: {} (code {}): {}",
exception.name, exception.code, exception.display_text
)));
}
other => {
return Err(Error::Protocol(format!(
"Unexpected packet type while waiting for Data: {}",
other
)));
}
}
}
// Now send our data block
debug!("Sending data block with {} rows", block.row_count());
self.conn.write_varint(ClientCode::Data as u64).await?;
self.block_writer.write_block(&mut self.conn, &block).await?;
// Send empty block to signal end
debug!("Sending empty block to signal end");
let empty_block = Block::new();
self.conn.write_varint(ClientCode::Data as u64).await?;
self.block_writer.write_block(&mut self.conn, &empty_block).await?;
// Wait for EndOfStream (matches C++ flow)
debug!("Waiting for EndOfStream...");
loop {
let packet_type = self.conn.read_varint().await?;
debug!("INSERT final response packet type: {}", packet_type);
match packet_type {
code if code == ServerCode::EndOfStream as u64 => {
debug!("Received EndOfStream, insert complete");
break;
}
code if code == ServerCode::Data as u64 => {
debug!(
"Received Data packet in INSERT response (skipping)"
);
// Skip temp table name if protocol supports it
if self.server_info.revision >= 50264 {
let _temp_table = self.conn.read_string().await?;
}
// Read and discard the block
let _block =
self.block_reader.read_block(&mut self.conn).await?;
}
code if code == ServerCode::Progress as u64 => {
debug!("Received Progress packet");
let _ = self.read_progress().await?;
}
code if code == ServerCode::ProfileEvents as u64 => {
debug!("Received ProfileEvents packet (skipping)");
let _table_name = self.conn.read_string().await?;
let uncompressed_reader =
BlockReader::new(self.server_info.revision);
let _block =
uncompressed_reader.read_block(&mut self.conn).await?;
}
code if code == ServerCode::TableColumns as u64 => {
debug!("Received TableColumns packet (skipping)");
let _table_name = self.conn.read_string().await?;
let _columns_metadata = self.conn.read_string().await?;
}
code if code == ServerCode::Exception as u64 => {
debug!("Server returned exception after sending data");
let exception = self.read_exception().await?;
return Err(Error::Protocol(format!(
"ClickHouse exception: {} (code {}): {}",
exception.name, exception.code, exception.display_text
)));
}
_ => {
debug!("WARNING: Ignoring unexpected packet type: {} - stream may be misaligned", packet_type);
}
}
}
Ok(())
}
/// Ping the server
pub async fn ping(&mut self) -> Result<()> {
debug!("Sending ping...");
self.conn.write_varint(ClientCode::Ping as u64).await?;
self.conn.flush().await?;
debug!("Ping sent, waiting for pong...");
let packet_type = self.conn.read_varint().await?;
debug!("Got response packet type: {}", packet_type);
if packet_type == ServerCode::Pong as u64 {
debug!("Pong received!");
Ok(())
} else {
debug!("Unexpected packet: {}", packet_type);
Err(Error::Protocol(format!("Expected Pong, got {}", packet_type)))
}
}
/// Cancel the current query
///
/// Sends a cancel packet to the server to stop any currently running
/// query. Note: This is most useful when called with a cancelable
/// callback, or when you need to cancel a long-running query from
/// outside the query execution flow.
pub async fn cancel(&mut self) -> Result<()> {
debug!("Sending cancel...");
self.conn.write_varint(ClientCode::Cancel as u64).await?;
self.conn.flush().await?;
debug!("Cancel sent");
Ok(())
}
/// Get server info
///
/// Returns information about the connected ClickHouse server including
/// name, version, revision, timezone, and display name.
///
/// # Example
/// ```no_run
/// # use clickhouse_native_client::{Client, ClientOptions};
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = Client::connect(ClientOptions::default()).await?;
/// let info = client.server_info();
/// println!("Server: {} v{}.{}.{}",
/// info.name,
/// info.version_major,
/// info.version_minor,
/// info.version_patch
/// );
/// # Ok(())
/// # }
/// ```
pub fn server_info(&self) -> &ServerInfo {
&self.server_info
}
/// Get server version as a tuple (major, minor, patch)
///
/// # Example
/// ```no_run
/// # use clickhouse_native_client::{Client, ClientOptions};
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = Client::connect(ClientOptions::default()).await?;
/// let (major, minor, patch) = client.server_version();
/// println!("Server version: {}.{}.{}", major, minor, patch);
/// # Ok(())
/// # }
/// ```
pub fn server_version(&self) -> (u64, u64, u64) {
(
self.server_info.version_major,
self.server_info.version_minor,
self.server_info.version_patch,
)
}
/// Get server revision number
///
/// The revision number is used for protocol feature negotiation.
///
/// # Example
/// ```no_run
/// # use clickhouse_native_client::{Client, ClientOptions};
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = Client::connect(ClientOptions::default()).await?;
/// let revision = client.server_revision();
/// println!("Server revision: {}", revision);
/// # Ok(())
/// # }
/// ```
pub fn server_revision(&self) -> u64 {
self.server_info.revision
}
}
/// Result of a `SELECT` query, containing data blocks and progress
/// information.
pub struct QueryResult {
/// Result blocks
pub blocks: Vec<Block>,
/// Progress information
pub progress: Progress,
}
impl QueryResult {
/// Get all blocks
pub fn blocks(&self) -> &[Block] {
&self.blocks
}
/// Get progress info
pub fn progress(&self) -> &Progress {
&self.progress
}
/// Get total number of rows across all blocks
pub fn total_rows(&self) -> usize {
self.blocks.iter().map(|b| b.row_count()).sum()
}
}
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
use super::*;
#[test]
fn test_client_options_default() {
let opts = ClientOptions::default();
assert_eq!(opts.host, "localhost");
assert_eq!(opts.port, 9000);
assert_eq!(opts.database, "default");
}
#[test]
fn test_client_options_builder() {
let opts = ClientOptions::new("127.0.0.1", 9000)
.database("test_db")
.user("test_user")
.password("test_pass");
assert_eq!(opts.host, "127.0.0.1");
assert_eq!(opts.database, "test_db");
assert_eq!(opts.user, "test_user");
assert_eq!(opts.password, "test_pass");
}
#[test]
fn test_query_result() {
let result =
QueryResult { blocks: vec![], progress: Progress::default() };
assert_eq!(result.total_rows(), 0);
}
}