narwhal-drivers 2.0.0

Bundled database drivers for narwhal (PostgreSQL, MySQL, SQLite, DuckDB, ClickHouse) + driver registry
Documentation
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
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
//! `ClickHouse` driver using the native HTTP interface.
//!
//! `ClickHouse` exposes an HTTP API on port 8123 by default. Queries are
//! sent as `POST` requests with the SQL in the body and results come back
//! in the `TabSeparatedWithNamesAndTypes` format which embeds column
//! names and native type strings in the first two rows.
//!
//! # Architecture
//!
//! * **Transport** — [`reqwest`] async HTTP client. One client is shared
//!   across all queries on a connection; the client is cloned (which is
//!   cheap — it internally uses an `Arc` connection pool).
//! * **Streaming** — [`Connection::stream`] uses
//!   `reqwest::Response::bytes_stream()` to feed a small line buffer
//!   that walks byte chunks as they arrive, emits the two TSV header
//!   lines once available, then forwards each completed row through
//!   an [`mpsc`] channel. Backpressure is provided by the channel's
//!   bounded buffer (capacity 64). Data cells are parsed at the byte
//!   level (not routed through `&str`) because `ClickHouse`'s `String`
//!   type is byte-oriented — cells may contain arbitrary bytes that
//!   are not valid UTF-8. TSV escape sequences (`\b \f \n \r \t \0 \\ \'`)
//!   are decoded, and invalid-UTF-8 payloads are preserved as
//!   [`Value::Bytes`] instead of being silently replaced with `U+FFFD`.
//! * **Cancellation** — Each outgoing request is tagged with a
//!   `query_id` (UUID v4) that is tracked in an `Arc<Mutex<HashSet>>`
//!   on the connection. [`Connection::cancel_handle`] returns a handle
//!   whose `cancel()` method reads the active query IDs and issues a
//!   `KILL QUERY WHERE query_id IN (...)` request. Cancellation is
//!   best-effort: server errors during KILL are ignored and an empty
//!   active-queries set is a no-op.
//! * **Parameter binding** — `ClickHouse`'s HTTP API does not support
//!   server-side prepared statements. Parameters are rendered as SQL
//!   literals via the internal `types::value_to_sql_literal` and interpolated into
//!   the query string. String escaping uses single-quote doubling to
//!   prevent injection.
//!
//! # Limitations
//!
//! * `ClickHouse` does not support true ACID transactions, savepoints, or
//!   foreign keys. The corresponding [`narwhal_core::Connection`] methods return
//!   [`Error::Unsupported`].
//! * `rows_affected` is not reliably available from the HTTP response
//!   (it lives in the `X-ClickHouse-Summary` header, but the format is
//!   version-dependent). For now, `rows_affected` is always `None` for
//!   DML and `0` for row-returning statements.

#![forbid(unsafe_code)]

mod types;

use std::collections::HashSet;
use std::sync::Arc;
use std::time::Instant;

use narwhal_core::{
    CancelHandle, Capabilities, Column, ColumnHeader, Connection, ConnectionConfig,
    ConnectionParams, DatabaseDriver, Error, IsolationLevel, QueryResult, Result, Row as CoreRow,
    RowStream, Schema, SslMode, Table, TableKind, TableSchema, Value,
};
use parking_lot::Mutex;
use tokio::sync::mpsc;
use tracing::{debug, info};
use url::Url;

use self::types::{parse_tsv_body, parse_tsv_value, value_to_sql_literal};

// Driver

/// `ClickHouse` driver factory.
#[derive(Debug, Default)]
pub struct ClickhouseDriver;

impl ClickhouseDriver {
    pub const NAME: &'static str = "clickhouse";

    pub const fn new() -> Self {
        Self
    }

    fn capabilities() -> Capabilities {
        Capabilities::default()
            .with_transactions(false)
            .with_cancellation(true)
            .with_multiple_schemas(true)
            .with_prepared_statements(false)
            .with_savepoints(false)
            .with_rows_affected(false)
            // Native HTTP TSV response is consumed chunk-by-chunk via
            // `stream_tsv_chunks`, so rows are surfaced progressively.
            .with_streaming(true)
            .with_row_level_dml(false)
    }
}

impl DatabaseDriver for ClickhouseDriver {
    fn name(&self) -> &'static str {
        Self::NAME
    }

    fn display_name(&self) -> &'static str {
        "ClickHouse"
    }

    fn validate(&self, config: &ConnectionConfig) -> Vec<String> {
        let mut problems = Vec::new();
        if config.params.host.is_none() {
            problems.push("host is required".into());
        }
        problems
    }

    async fn connect(
        &self,
        config: &ConnectionConfig,
        password: Option<&str>,
    ) -> Result<Box<dyn narwhal_core::DynConnection>> {
        let base_url = build_base_url(&config.params)?;
        let user = config
            .params
            .username
            .as_deref()
            .unwrap_or("default")
            .to_owned();
        let database = config
            .params
            .database
            .as_deref()
            .unwrap_or("default")
            .to_owned();
        let pw = password.map(String::from).unwrap_or_default();

        debug!(target: "narwhal::clickhouse", %base_url, %user, %database, "connecting");

        // Five-minute default request timeout. ClickHouse analytical
        // queries can run for a long time; this is a per-request limit,
        // not a session limit. TODO: surface as a config option once
        // narwhal-config grows a `request_timeout_seconds` field.
        const REQUEST_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(300);
        let mut client_builder = reqwest::Client::builder().timeout(REQUEST_TIMEOUT);

        // When using HTTPS, add the CA root certificate if provided.
        if config.params.ssl_mode != SslMode::Disable {
            let has_root_cert = if let Some(path) = &config.params.ssl_root_cert {
                let bytes = std::fs::read(path).map_err(|e| {
                    Error::Config(format!(
                        "failed to read ssl_root_cert '{}': {e}",
                        path.display()
                    ))
                })?;
                let cert = reqwest::Certificate::from_pem(&bytes)
                    .map_err(|e| Error::Config(format!("failed to parse ssl_root_cert: {e}")))?;
                client_builder = client_builder.add_root_certificate(cert);
                true
            } else {
                false
            };

            // Determine certificate and hostname validation based on
            // ssl_mode and whether an explicit CA was provided:
            //
            // SECURITY (was CRITICAL): Prefer/Require without an explicit
            // CA previously set accept_invalid_certs=true, which enables
            // MITM attacks. Now we always validate the chain against the
            // system CA store (or ssl_root_cert if provided). Only hostname
            // checking is relaxed for non-VerifyFull modes.
            //
            // | ssl_mode   | chain verify | hostname verify |
            // |------------|-------------|-----------------|
            // | Prefer     | yes         | no              |
            // | Require    | yes         | no              |
            // | VerifyCa   | yes         | no              |
            // | VerifyFull | yes         | yes             |
            let accept_invalid_certs = false;
            let accept_invalid_hostnames = !matches!(config.params.ssl_mode, SslMode::VerifyFull);

            if has_root_cert && matches!(config.params.ssl_mode, SslMode::Prefer | SslMode::Require)
            {
                tracing::info!(
                    target: "narwhal::clickhouse",
                    "ssl_root_cert is set with ssl_mode='{}'; \
                     CA certificate will be validated but hostname will not — \
                     use ssl_mode='verify-full' to also enforce hostname checking",
                    match config.params.ssl_mode {
                        SslMode::Prefer => "prefer",
                        SslMode::Require => "require",
                        _ => "unknown",
                    }
                );
            }

            // When no explicit CA is provided and TLS is requested, the
            // system native CA store is used for chain verification.
            // Previously this path skipped verification entirely (MITM risk).
            if !has_root_cert {
                tracing::debug!(
                    target: "narwhal::clickhouse",
                    ssl_mode = ?config.params.ssl_mode,
                    "no ssl_root_cert provided; using system CA store for chain verification"
                );
            }

            client_builder = client_builder
                .danger_accept_invalid_certs(accept_invalid_certs)
                .danger_accept_invalid_hostnames(accept_invalid_hostnames);
        }

        // M2.1: ssl_cert and ssl_key must both be provided or both omitted.
        // A half-configured mTLS setup (cert without key or vice versa)
        // would silently skip client authentication or fail at runtime
        // with a confusing error.
        if config.params.ssl_cert.is_some() != config.params.ssl_key.is_some() {
            return Err(Error::Config(
                "ssl_cert and ssl_key must both be provided or both omitted".into(),
            ));
        }

        // Client identity (mTLS) is not directly supported by reqwest's
        // high-level API in the same way as mysql_async / rustls.
        // If ssl_cert and ssl_key are both provided, we read them and
        // configure identity via reqwest::Identity.
        if let (Some(cert_path), Some(key_path)) = (&config.params.ssl_cert, &config.params.ssl_key)
        {
            let cert_bytes = std::fs::read(cert_path).map_err(|e| {
                Error::Config(format!(
                    "failed to read ssl_cert '{}': {e}",
                    cert_path.display()
                ))
            })?;
            let key_bytes = std::fs::read(key_path).map_err(|e| {
                Error::Config(format!(
                    "failed to read ssl_key '{}': {e}",
                    key_path.display()
                ))
            })?;
            // Combine cert + key into a single PEM blob for reqwest::Identity.
            let mut pem = Vec::with_capacity(cert_bytes.len() + key_bytes.len());
            pem.extend_from_slice(&cert_bytes);
            pem.extend_from_slice(&key_bytes);
            let identity = reqwest::Identity::from_pem(&pem)
                .map_err(|e| Error::Config(format!("failed to parse client identity PEM: {e}")))?;
            client_builder = client_builder.identity(identity);
        }

        let client = client_builder
            .build()
            .map_err(|e| Error::connection_with("failed to build HTTP client", e))?;

        // Ping to verify connectivity.
        let mut url = base_url.clone();
        url.query_pairs_mut().append_pair("query", "SELECT 1");

        let response = client
            .post(url.as_str())
            .basic_auth(&user, if pw.is_empty() { None } else { Some(&pw) })
            .send()
            .await
            .map_err(|e| Error::connection_with("ping failed", e))?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response
                .text()
                .await
                .unwrap_or_else(|e| format!("<failed to read response body: {e}>"));
            return Err(Error::Connection(format!(
                "ClickHouse returned {status}: {body}"
            )));
        }

        info!(target: "narwhal::clickhouse", %base_url, "connected");

        Ok(Box::new(ClickhouseConnection {
            inner: Arc::new(SharedState {
                client,
                base_url,
                user,
                password: pw,
                database,
                active_queries: Arc::new(Mutex::new(HashSet::new())),
            }),
        }))
    }
}

// Connection

/// Shared state behind a plain `Arc` so the spawned streaming task
/// can clone the `Arc` and issue HTTP requests independently.
/// `reqwest::Client` is already `Send + Sync` with an internal
/// connection pool, so no mutex is needed to parallelise requests.
///
/// `active_queries` uses [`parking_lot::Mutex`] — every callsite
/// follows the same `lock().{insert,remove,iter}` pattern that drops
/// the guard before reaching any `.await` point, so the async-aware
/// `tokio::sync::Mutex` is unnecessary overhead (L2).
struct SharedState {
    client: reqwest::Client,
    base_url: Url,
    user: String,
    password: String,
    database: String,
    active_queries: Arc<Mutex<HashSet<String>>>,
}

impl SharedState {
    /// Build an authenticated POST request with SQL in the body.
    ///
    /// Centralises the auth + body pattern so every call site stays
    /// consistent and a future auth change touches one place.
    fn build_request(&self, url: &Url, body: String) -> reqwest::RequestBuilder {
        self.client
            .post(url.as_str())
            .basic_auth(
                &self.user,
                if self.password.is_empty() {
                    None
                } else {
                    Some(self.password.as_str())
                },
            )
            .body(body)
    }
}

pub struct ClickhouseConnection {
    inner: Arc<SharedState>,
}

/// Best-effort heuristic: does `sql` likely return a result set?
///
/// `ClickHouse`'s HTTP API always returns a response body (even for DDL),
/// but we need to decide whether to parse it as rows or treat it as a
/// simple acknowledgement. The heuristic matches the same pattern used
/// by the `DuckDB` driver.
fn statement_returns_rows(sql: &str) -> bool {
    let lead = sql
        .trim_start()
        .split(|c: char| c.is_whitespace() || c == '(')
        .next()
        .unwrap_or("")
        .to_ascii_uppercase();
    matches!(
        lead.as_str(),
        "SELECT" | "WITH" | "SHOW" | "DESCRIBE" | "EXPLAIN" | "EXISTS"
    )
}

/// Build the base URL from connection parameters.
///
/// Uses `https://` when `ssl_mode` is not `Disable`, otherwise `http://`.
/// Default: `http://localhost:8123/`.
fn build_base_url(params: &ConnectionParams) -> Result<Url> {
    let host = params
        .host
        .as_deref()
        .ok_or_else(|| Error::Config("host is required".into()))?;
    let port = params.port.unwrap_or(8123);
    let scheme = if params.ssl_mode == SslMode::Disable {
        "http"
    } else {
        "https"
    };
    // C2: bracket IPv6 addresses so the URL is valid.
    // Pre-bracketed hosts (e.g. `[::1]`) are left unchanged.
    let host_part = if host.contains(':') && !host.starts_with('[') {
        format!("[{host}]")
    } else {
        host.to_string()
    };
    Url::parse(&format!("{scheme}://{host_part}:{port}/"))
        .map_err(|e| Error::Config(format!("invalid URL: {e}")))
}

/// Double-quote an identifier for `ClickHouse` (e.g. `"my table"`).
fn quote_ident(name: &str) -> String {
    format!("\"{}\"", name.replace('"', "\"\""))
}

impl ClickhouseConnection {
    /// Send a query to `ClickHouse` via HTTP and return the full response
    /// body as a string.
    ///
    /// If `query_id` is `Some`, the query ID is registered in the
    /// active-queries set for the duration of the request so that
    /// cancellation can target it.
    /// Send a query to `ClickHouse` via HTTP and return the full response
    /// body as raw bytes.
    ///
    /// If `query_id` is `Some`, the query ID is registered in the
    /// active-queries set for the duration of the request so that
    /// cancellation can target it.
    ///
    /// Uses `response.bytes()` instead of `response.text()` because
    /// `ClickHouse`'s `String` type is byte-oriented — cells may contain
    /// arbitrary bytes that are not valid UTF-8.
    /// Send a query and return the raw response body.
    ///
    /// L28: returns `bytes::Bytes` (cheap clone, refcounted buffer)
    /// instead of `Vec<u8>` so the response is not copied a second time
    /// just to satisfy the `Vec` API. Callers that need an owned `Vec`
    /// can still `.to_vec()` at the boundary, but no current caller does
    /// — `parse_tsv_body` takes `&[u8]`.
    async fn http_query(&self, sql: &str, query_id: Option<&str>) -> Result<bytes::Bytes> {
        let state = &self.inner;
        let mut url = state.base_url.clone();
        url.query_pairs_mut()
            .append_pair("database", &state.database);

        if let Some(qid) = query_id {
            url.query_pairs_mut().append_pair("query_id", qid);
        }

        debug!(target: "narwhal::clickhouse", %sql, "sending HTTP query");

        // Register query ID before sending.
        if let Some(qid) = query_id {
            state.active_queries.lock().insert(qid.to_owned());
        }

        // SQL goes in the request body, not the URL query string. URLs
        // are capped around 8 KiB on most front-end proxies and even on
        // bare ClickHouse, long analytical queries blow that limit.
        let response = match state.build_request(&url, sql.to_owned()).send().await {
            Ok(r) => r,
            Err(e) => {
                if let Some(qid) = query_id {
                    state.active_queries.lock().remove(qid);
                }
                return Err(Error::query_with("HTTP request failed", e));
            }
        };

        let status = response.status();
        if !status.is_success() {
            // Error bodies are ClickHouse error messages — always UTF-8.
            let body = response
                .text()
                .await
                .unwrap_or_else(|e| format!("<failed to read response body: {e}>"));
            // Remove query ID on failure.
            if let Some(qid) = query_id {
                state.active_queries.lock().remove(qid);
            }
            return Err(Error::Query(format!(
                "ClickHouse returned {status}: {body}"
            )));
        }

        // Remove query ID on success.
        if let Some(qid) = query_id {
            state.active_queries.lock().remove(qid);
        }

        response
            .bytes()
            .await
            .map_err(|e| Error::query_with("failed to read response body", e))
    }

    /// Send a query with `TabSeparatedWithNamesAndTypes` format and
    /// return a parsed [`QueryResult`].
    async fn query_tsv(&self, sql: &str, params: &[Value]) -> Result<QueryResult> {
        let started = Instant::now();
        let query_id = Self::new_query_id();

        let formatted_sql = if params.is_empty() {
            sql.to_owned()
        } else {
            substitute_params(sql, params)
        };

        // Append the format directive.
        let full_sql = format!("{formatted_sql}\nFORMAT TabSeparatedWithNamesAndTypes");

        let body = self.http_query(&full_sql, Some(&query_id)).await?;
        let (headers, type_strings, rows) = parse_tsv_body(&body);

        let column_headers: Vec<ColumnHeader> = headers
            .into_iter()
            .zip(type_strings)
            .map(|(name, data_type)| ColumnHeader { name, data_type })
            .collect();

        let core_rows: Vec<CoreRow> = rows.into_iter().map(CoreRow).collect();

        Ok(QueryResult {
            columns: column_headers,
            rows: core_rows,
            rows_affected: None,
            elapsed_ms: started.elapsed().as_millis() as u64,
        })
    }

    /// Execute a non-row-returning statement (DDL/DML).
    async fn execute_raw(&self, sql: &str, params: &[Value]) -> Result<QueryResult> {
        let started = Instant::now();
        let query_id = Self::new_query_id();

        let formatted_sql = if params.is_empty() {
            sql.to_owned()
        } else {
            substitute_params(sql, params)
        };

        self.http_query(&formatted_sql, Some(&query_id)).await?;

        Ok(QueryResult {
            columns: Vec::new(),
            rows: Vec::new(),
            rows_affected: None,
            elapsed_ms: started.elapsed().as_millis() as u64,
        })
    }

    /// Generate a new query ID for use with cancellation.
    fn new_query_id() -> String {
        uuid::Uuid::new_v4().to_string()
    }
}

/// Substitute `?` and `$N` placeholders with rendered SQL literals.
///
/// Left-to-right walk that respects single- and double-quoted string
/// literals so a `?` or `$1` *inside* a literal is left untouched.
/// Each `?` consumes the next parameter; each `$N` (1-based) selects
/// `params[N-1]` directly so the two styles can be mixed.
fn substitute_params(sql: &str, params: &[Value]) -> String {
    let mut out = String::with_capacity(sql.len());
    let mut quote: Option<char> = None;
    let mut placeholder_idx = 0usize;
    let mut chars = sql.chars().peekable();

    while let Some(ch) = chars.next() {
        if let Some(q) = quote {
            out.push(ch);
            if ch == q {
                if chars.peek() == Some(&q) {
                    // Doubled quote = escaped literal quote.
                    out.push(q);
                    chars.next();
                } else {
                    quote = None;
                }
            }
            continue;
        }
        match ch {
            '\'' | '"' => {
                quote = Some(ch);
                out.push(ch);
            }
            '?' => {
                if let Some(p) = params.get(placeholder_idx) {
                    out.push_str(&value_to_sql_literal(p));
                    placeholder_idx += 1;
                } else {
                    out.push('?');
                }
            }
            '$' => {
                // Try to parse $N; if not a positive integer, copy the
                // `$` literally so dollar-amount strings outside a
                // literal (rare) survive.
                let mut digits = String::new();
                while let Some(&next) = chars.peek() {
                    if next.is_ascii_digit() {
                        digits.push(next);
                        chars.next();
                    } else {
                        break;
                    }
                }
                if digits.is_empty() {
                    out.push('$');
                    continue;
                }
                match digits.parse::<usize>() {
                    Ok(n) if n >= 1 => {
                        if let Some(p) = params.get(n - 1) {
                            out.push_str(&value_to_sql_literal(p));
                        } else {
                            out.push('$');
                            out.push_str(&digits);
                        }
                    }
                    _ => {
                        out.push('$');
                        out.push_str(&digits);
                    }
                }
            }
            other => out.push(other),
        }
    }
    out
}

/// Escape a string for use inside single-quoted SQL literals. Used for
/// internal queries against `system.tables` etc. where we splice schema
/// or table names into the SQL by hand instead of going through the
/// regular parameter binding path.
///
/// `ClickHouse` honours backslash escapes inside string literals, so a
/// lone backslash would be interpreted as an escape leader (potentially
/// swallowing a subsequent closing quote). Both backslash-to-doubled-backslash
/// and single-quote-to-doubled-quote are applied to prevent injection
/// and misinterpretation.
fn escape_sql_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for ch in s.chars() {
        match ch {
            '\\' => out.push_str("\\\\"),
            '\'' => out.push_str("''"),
            other => out.push(other),
        }
    }
    out
}

#[doc(hidden)]
pub mod __test_only {
    //! Private helpers exposed for integration tests only. Not part of
    //! the public API; do not depend on this module outside the crate's
    //! own `tests/` directory.
    use narwhal_core::Value;

    pub fn replace_question_marks(sql: &str, params: &[Value]) -> String {
        super::replace_question_marks(sql, params)
    }

    pub fn substitute_params(sql: &str, params: &[Value]) -> String {
        super::substitute_params(sql, params)
    }
}

/// Replace `?` placeholders left-to-right with parameter literals.
///
/// Char-based walk so multi-byte UTF-8 sequences in identifiers and
/// string literals survive intact. Single- and double-quoted regions
/// are tracked with doubled-quote escape detection (`''`, `""`).
fn replace_question_marks(sql: &str, params: &[Value]) -> String {
    let mut out = String::with_capacity(sql.len());
    let mut param_iter = params.iter();
    let mut quote: Option<char> = None;
    let mut chars = sql.chars().peekable();

    while let Some(ch) = chars.next() {
        if let Some(q) = quote {
            out.push(ch);
            if ch == q {
                if chars.peek() == Some(&q) {
                    out.push(q);
                    chars.next();
                } else {
                    quote = None;
                }
            }
            continue;
        }
        match ch {
            '\'' | '"' => {
                quote = Some(ch);
                out.push(ch);
            }
            '?' => {
                if let Some(p) = param_iter.next() {
                    out.push_str(&value_to_sql_literal(p));
                } else {
                    out.push('?');
                }
            }
            other => out.push(other),
        }
    }
    out
}

impl Connection for ClickhouseConnection {
    async fn execute(&mut self, sql: &str, params: &[Value]) -> Result<QueryResult> {
        if statement_returns_rows(sql) {
            self.query_tsv(sql, params).await
        } else {
            self.execute_raw(sql, params).await
        }
    }

    async fn stream(
        &mut self,
        sql: &str,
        params: &[Value],
    ) -> Result<Box<dyn narwhal_core::DynRowStream>> {
        let state = &self.inner;
        let formatted_sql = if params.is_empty() {
            sql.to_owned()
        } else {
            substitute_params(sql, params)
        };

        let query_id = Self::new_query_id();

        // Register query ID for cancellation tracking. The QueryGuard
        // ensures the ID is removed when the scope exits, even on error
        // or task abort (M7 RAII pattern).
        state.active_queries.lock().insert(query_id.clone());
        let _guard = QueryGuard {
            active: Arc::clone(&state.active_queries),
            qid: query_id.clone(),
        };

        if !statement_returns_rows(&formatted_sql) {
            // Non-row-returning: execute and return an empty stream.
            // SQL goes in the body (not the URL) to avoid the ~8 KiB
            // URL length limit on large analytical DML statements.
            let mut url = state.base_url.clone();
            {
                let mut pairs = url.query_pairs_mut();
                pairs.append_pair("database", &state.database);
                pairs.append_pair("query_id", &query_id);
            }

            let response = state
                .build_request(&url, formatted_sql)
                .send()
                .await
                .map_err(|e| Error::query_with("HTTP request failed", e))?;

            if !response.status().is_success() {
                let status = response.status();
                let body = response
                    .text()
                    .await
                    .unwrap_or_else(|e| format!("<failed to read response body: {e}>"));
                return Err(Error::Query(format!(
                    "ClickHouse returned {status}: {body}"
                )));
            }

            // _guard is dropped here, cleaning up the query ID.

            // Drop the sender immediately so the receiver yields
            // `Ok(None)` on first poll — a clean empty stream. No task
            // was spawned in this branch, so the stream owns nothing
            // that needs cancellation. (L10: don't keep a `_tx` named
            // binding alive longer than necessary.)
            let (tx, rx) = mpsc::channel::<Result<CoreRow>>(1);
            drop(tx);
            return Ok(Box::new(ClickhouseRowStream {
                columns: Vec::new(),
                rx,
                task: None,
            }));
        }

        // Row-returning: use TSV format and stream the body.
        let full_sql = format!("{formatted_sql}\nFORMAT TabSeparatedWithNamesAndTypes");

        let mut url = state.base_url.clone();
        {
            let mut pairs = url.query_pairs_mut();
            pairs.append_pair("database", &state.database);
            pairs.append_pair("query_id", &query_id);
        }

        let response = state
            .build_request(&url, full_sql)
            .send()
            .await
            .map_err(|e| Error::query_with("HTTP request failed", e))?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response
                .text()
                .await
                .unwrap_or_else(|e| format!("<failed to read response body: {e}>"));
            return Err(Error::Query(format!(
                "ClickHouse returned {status}: {body}"
            )));
        }

        // Chunked streaming: use bytes_stream() to receive byte chunks
        // as they arrive and feed a small line buffer. Rows are emitted
        // through the mpsc channel as soon as their line is complete.
        //
        // The query ID is deregistered by the QueryGuard when the stream
        // task completes or when the stream is dropped/aborted.
        let (header_tx, header_rx) = tokio::sync::oneshot::channel::<Result<Vec<ColumnHeader>>>();
        let (row_tx, row_rx) = mpsc::channel::<Result<CoreRow>>(64);

        // Move the guard into the spawned task so it is dropped when
        // the streaming work finishes (success or error), cleaning up
        // the query ID.
        let guard = _guard;
        let task = tokio::spawn(async move {
            stream_tsv_chunks(response.bytes_stream(), header_tx, row_tx).await;
            // guard is dropped here → query ID removed from active set.
            drop(guard);
        });

        let columns = match header_rx.await {
            Ok(Ok(cols)) => cols,
            Ok(Err(e)) => {
                // stream_tsv_chunks sent an error through header_tx. The
                // spawned task has already returned, so abort() is a
                // no-op but keeps the cleanup path uniform.
                task.abort();
                return Err(e);
            }
            Err(_) => {
                task.abort();
                return Err(Error::Other("clickhouse stream cancelled".into()));
            }
        };

        Ok(Box::new(ClickhouseRowStream {
            columns,
            rx: row_rx,
            task: Some(task),
        }))
    }

    async fn begin(&mut self) -> Result<()> {
        Err(Error::unsupported("transactions (ClickHouse)"))
    }

    async fn begin_with(&mut self, _isolation: IsolationLevel) -> Result<()> {
        Err(Error::unsupported("transactions (ClickHouse)"))
    }

    async fn commit(&mut self) -> Result<()> {
        Err(Error::unsupported("transactions (ClickHouse)"))
    }

    async fn rollback(&mut self) -> Result<()> {
        Err(Error::unsupported("transactions (ClickHouse)"))
    }

    async fn list_schemas(&mut self) -> Result<Vec<Schema>> {
        const SQL: &str = "SHOW DATABASES";
        let result = self.query_tsv(SQL, &[]).await?;

        // Filter system databases that are not interesting for browsing.
        // ClickHouse exposes `INFORMATION_SCHEMA` and `information_schema`
        // as two case variants of the same schema.
        let hidden = ["system", "INFORMATION_SCHEMA", "information_schema"];
        let mut out = Vec::with_capacity(result.rows.len());
        for row in result.rows {
            if let Some(Value::String(name)) = row.0.into_iter().next() {
                if !hidden.contains(&name.as_str()) {
                    out.push(Schema { name });
                }
            }
        }
        Ok(out)
    }

    async fn list_tables(&mut self, schema: &str) -> Result<Vec<Table>> {
        // schema is interpolated into a SQL literal; escape any `'`s
        // even though sidebar-driven calls won't contain them.
        let sql = format!(
            "SELECT name, engine FROM system.tables WHERE database = '{}' ORDER BY name",
            escape_sql_string(schema)
        );
        let result = self.query_tsv(&sql, &[]).await?;
        let mut out = Vec::with_capacity(result.rows.len());
        for row in result.rows {
            let mut iter = row.0.into_iter();
            let name = match iter.next() {
                Some(Value::String(s)) => s,
                _ => continue,
            };
            let engine = match iter.next() {
                Some(Value::String(s)) => s.to_ascii_lowercase(),
                _ => String::new(),
            };
            let kind = if engine == "view" {
                TableKind::View
            } else if engine == "materializedview" {
                TableKind::MaterializedView
            } else {
                TableKind::Table
            };
            out.push(Table {
                schema: schema.to_owned(),
                name,
                kind,
            });
        }
        Ok(out)
    }

    async fn list_all_tables(&mut self) -> Result<Vec<(Schema, Vec<Table>)>> {
        const SQL: &str = "
            SELECT database, name, engine
              FROM system.tables
             WHERE database NOT IN ('system', 'INFORMATION_SCHEMA', 'information_schema')
             ORDER BY database, name";
        let result = self.query_tsv(SQL, &[]).await?;

        let mut map: std::collections::BTreeMap<String, Vec<Table>> =
            std::collections::BTreeMap::new();
        for row in result.rows {
            let mut iter = row.0.into_iter();
            let schema = match iter.next() {
                Some(Value::String(s)) => s,
                _ => continue,
            };
            let name = match iter.next() {
                Some(Value::String(s)) => s,
                _ => continue,
            };
            let engine = match iter.next() {
                Some(Value::String(s)) => s.to_ascii_lowercase(),
                _ => String::new(),
            };
            let kind = if engine == "view" {
                TableKind::View
            } else if engine == "materializedview" {
                TableKind::MaterializedView
            } else {
                TableKind::Table
            };
            map.entry(schema.clone())
                .or_default()
                .push(Table { schema, name, kind });
        }

        // Preserve the order of schemas from list_schemas.
        let schemas = self.list_schemas().await?;
        let mut out = Vec::with_capacity(schemas.len());
        for schema in schemas {
            let tables = map.remove(&schema.name).unwrap_or_default();
            out.push((schema, tables));
        }
        // Append any schemas that appeared in system.tables but not in
        // list_schemas (shouldn't happen, but defensive).
        for (name, tables) in map {
            out.push((Schema { name }, tables));
        }
        Ok(out)
    }

    async fn describe_table(&mut self, schema: &str, name: &str) -> Result<TableSchema> {
        let escaped_schema = quote_ident(schema);
        let escaped_name = quote_ident(name);
        let sql = format!("DESCRIBE TABLE {escaped_schema}.{escaped_name}");
        let result = self.query_tsv(&sql, &[]).await?;

        if result.rows.is_empty() {
            return Err(Error::Schema(format!("table {schema}.{name} not found")));
        }

        // Look up the table kind (Table/View/MaterializedView) from
        // system.tables.engine — M11.
        let kind = self.lookup_table_kind(schema, name).await?;

        // ClickHouse DESCRIBE TABLE returns:
        // name, type, default_type, default_expression, comment, codec_expression, ttl_expression
        let columns: Vec<Column> = result
            .rows
            .into_iter()
            .filter_map(|row| {
                let mut iter = row.0.into_iter();
                let col_name = match iter.next() {
                    Some(Value::String(s)) => s,
                    _ => return None,
                };
                let data_type = match iter.next() {
                    Some(Value::String(s)) => s,
                    _ => String::new(),
                };
                let _default_kind = match iter.next() {
                    Some(Value::String(s)) => s,
                    _ => String::new(),
                };
                let default_expr = match iter.next() {
                    Some(Value::String(s)) if !s.is_empty() => Some(s),
                    _ => None,
                };
                let default = default_expr;

                // ClickHouse doesn't have a traditional NOT NULL / PRIMARY KEY
                // in DESCRIBE TABLE. Nullable types are expressed in the type
                // string itself. Primary key info is available from system.tables.
                let nullable = data_type.trim().starts_with("Nullable(");

                Some(Column {
                    name: col_name,
                    data_type,
                    nullable,
                    primary_key: false,
                    default,
                })
            })
            .collect();

        // Try to look up primary key from system.tables. Log a miss
        // (often a permission issue) instead of silently dropping it.
        let primary_key_columns = match self.lookup_primary_key(schema, name).await {
            Ok(v) => v,
            Err(error) => {
                tracing::warn!(
                    target: "narwhal::clickhouse",
                    schema, table = name, error = %error,
                    "primary-key lookup failed; continuing without"
                );
                Vec::new()
            }
        };
        let pk_set: std::collections::HashSet<String> = primary_key_columns.into_iter().collect();

        let columns: Vec<Column> = columns
            .into_iter()
            .map(|mut c| {
                c.primary_key = pk_set.contains(&c.name);
                c
            })
            .collect();

        // ClickHouse has no foreign keys. Skip indexes in MVP.
        Ok(TableSchema {
            table: Table {
                schema: schema.to_owned(),
                name: name.to_owned(),
                kind,
            },
            columns,
            indexes: Vec::new(),
            foreign_keys: Vec::new(),
            unique_constraints: Vec::new(),
        })
    }

    async fn fetch_ddl(&mut self, schema: &str, name: &str) -> Result<String> {
        let escaped_schema = quote_ident(schema);
        let escaped_name = quote_ident(name);
        let sql = format!("SHOW CREATE TABLE {escaped_schema}.{escaped_name}");
        let result = self.query_tsv(&sql, &[]).await?;
        // SHOW CREATE TABLE returns: name, create_table_query, ...
        // The DDL is in column index 1 (or sometimes the last column).
        match result.rows.into_iter().next() {
            Some(row) => {
                // ClickHouse SHOW CREATE TABLE returns a single column with the statement.
                // Try the last column first (engine-dependent), fall back to index 1.
                let ddl = row.0.last().cloned().or_else(|| row.0.into_iter().nth(1));
                match ddl {
                    Some(Value::String(s)) => Ok(s),
                    _ => Err(Error::Schema(format!(
                        "DDL not found for table {schema}.{name}"
                    ))),
                }
            }
            None => Err(Error::Schema(format!(
                "DDL not found for table {schema}.{name}"
            ))),
        }
    }

    async fn ping(&mut self) -> Result<()> {
        self.http_query("SELECT 1", None).await.map(|_| ())
        // L28: `.map(|_| ())` discards the `Bytes` immediately; the
        // refcount drop is the only cleanup needed.
    }

    /// `ClickHouse` exposes session read-only as the `readonly` setting
    /// with three levels: `0` (writes allowed), `1` (only SELECT/SHOW,
    /// settings frozen), `2` (only SELECT/SHOW, but `SET` is still
    /// allowed so callers can tweak query-time parameters).
    /// Narwhal uses level `2` — it is the safe choice that still lets
    /// the agent/UI emit `SET max_execution_time = ...` style guards
    /// before the SELECT.
    async fn set_read_only(&mut self, read_only: bool) -> Result<()> {
        let sql = if read_only {
            "SET readonly = 2"
        } else {
            "SET readonly = 0"
        };
        self.http_query(sql, None).await.map(|_| ())
    }

    fn cancel_handle(&self) -> Option<Box<dyn narwhal_core::DynCancelHandle>> {
        Some(Box::new(ClickhouseCancel {
            state: Arc::clone(&self.inner),
        }))
    }

    fn capabilities(&self) -> Capabilities {
        ClickhouseDriver::capabilities()
    }

    async fn close(self: Box<Self>) -> Result<()> {
        // Nothing to close for HTTP — the reqwest client drops cleanly.
        Ok(())
    }
}

impl ClickhouseConnection {
    /// Look up the table kind (Table/View/MaterializedView) from
    /// `system.tables.engine`.
    async fn lookup_table_kind(&self, schema: &str, name: &str) -> Result<TableKind> {
        let sql = format!(
            "SELECT engine FROM system.tables WHERE database = '{}' AND name = '{}'",
            escape_sql_string(schema),
            escape_sql_string(name)
        );
        let result = self.query_tsv(&sql, &[]).await?;
        match result.rows.into_iter().next() {
            Some(row) => match row.0.into_iter().next() {
                Some(Value::String(engine)) => {
                    let engine = engine.to_ascii_lowercase();
                    Ok(if engine == "view" {
                        TableKind::View
                    } else if engine == "materializedview" {
                        TableKind::MaterializedView
                    } else {
                        TableKind::Table
                    })
                }
                _ => Ok(TableKind::Table),
            },
            None => Ok(TableKind::Table),
        }
    }

    /// Look up the primary key columns for a table from `system.tables`.
    async fn lookup_primary_key(&mut self, schema: &str, name: &str) -> Result<Vec<String>> {
        // Both identifiers reach SQL as quoted literals; escape `'` to
        // close the injection vector even though normal callers pass
        // sanitised metadata names.
        let sql = format!(
            "SELECT primary_key FROM system.tables WHERE database = '{}' AND name = '{}'",
            escape_sql_string(schema),
            escape_sql_string(name)
        );
        let result = self.query_tsv(&sql, &[]).await?;
        match result.rows.into_iter().next() {
            Some(row) => match row.0.into_iter().next() {
                Some(Value::String(pk)) if !pk.is_empty() => {
                    Ok(pk.split(',').map(|s| s.trim().to_owned()).collect())
                }
                _ => Ok(Vec::new()),
            },
            None => Ok(Vec::new()),
        }
    }
}

// Query ID RAII Guard

/// RAII guard that ensures a query ID is removed from the active-queries
/// set when the guard is dropped, regardless of how the scope exits.
///
/// This prevents query ID leaks on error paths and task aborts.
/// The drop implementation uses `tokio::spawn` to perform the async
/// removal because the `Drop` trait cannot be async. This is acceptable
/// because the removal is a brief lock + hash-set remove operation.
struct QueryGuard {
    active: Arc<Mutex<HashSet<String>>>,
    qid: String,
}

impl Drop for QueryGuard {
    fn drop(&mut self) {
        let active = self.active.clone();
        let qid = std::mem::take(&mut self.qid);
        // Spawn a tiny async task to remove the query ID. The lock is
        // held only for the brief remove() call.
        tokio::spawn(async move {
            active.lock().remove(&qid);
        });
    }
}

// Cancellation

/// Cancellation handle for `ClickHouse` connections.
///
/// Reads the active query IDs from the shared state and issues a
/// `KILL QUERY WHERE query_id IN (...)` request. Best-effort:
/// server errors during KILL are silently ignored.
struct ClickhouseCancel {
    state: Arc<SharedState>,
}

impl CancelHandle for ClickhouseCancel {
    async fn cancel(&self) -> Result<()> {
        let query_ids: Vec<String> = self.state.active_queries.lock().iter().cloned().collect();

        if query_ids.is_empty() {
            // No active queries — nothing to cancel.
            return Ok(());
        }

        // Build a KILL QUERY statement targeting all active IDs.
        let ids: Vec<String> = query_ids.iter().map(|id| format!("'{id}'")).collect();
        let kill_sql = format!("KILL QUERY WHERE query_id IN ({})", ids.join(", "));

        debug!(target: "narwhal::clickhouse", %kill_sql, "cancelling queries");

        // Fire-and-forget: we don't care if the KILL succeeds or fails.
        let state = &self.state;
        let mut url = state.base_url.clone();
        url.query_pairs_mut()
            .append_pair("database", &state.database);

        let result = state.build_request(&url, kill_sql).send().await;

        match result {
            Ok(response) => {
                if !response.status().is_success() {
                    debug!(
                        target: "narwhal::clickhouse",
                        status = %response.status(),
                        "KILL QUERY returned non-success (best-effort, ignoring)"
                    );
                }
            }
            Err(e) => {
                debug!(
                    target: "narwhal::clickhouse",
                    error = %e,
                    "KILL QUERY request failed (best-effort, ignoring)"
                );
            }
        }

        Ok(())
    }
}

// Row stream

struct ClickhouseRowStream {
    columns: Vec<ColumnHeader>,
    rx: mpsc::Receiver<Result<CoreRow>>,
    /// Handle to the spawned task that drives [`stream_tsv_chunks`].
    /// `close()` aborts it so a slow server cannot keep dribbling bytes
    /// into a buffer no one is reading after the consumer walks away.
    /// `None` for the empty-stream case (no task was spawned).
    task: Option<tokio::task::JoinHandle<()>>,
}

impl RowStream for ClickhouseRowStream {
    fn columns(&self) -> &[ColumnHeader] {
        &self.columns
    }

    async fn next_row(&mut self) -> Result<Option<CoreRow>> {
        match self.rx.recv().await {
            Some(Ok(row)) => Ok(Some(row)),
            Some(Err(error)) => Err(error),
            None => Ok(None),
        }
    }

    async fn close(mut self: Box<Self>) -> Result<()> {
        // Closing the receiver alone is not enough: if the task is
        // currently parked on bytes_stream.next().await waiting for the
        // next chunk from the server, it stays parked until that chunk
        // arrives and only then notices the dropped row_tx. On a big
        // result set that can keep the HTTP connection and a chunk of
        // RAM tied up for seconds. abort() unparks it immediately.
        if let Some(handle) = self.task.take() {
            handle.abort();
        }
        Ok(())
    }
}

impl Drop for ClickhouseRowStream {
    fn drop(&mut self) {
        // Caller dropped without calling close(): same reasoning.
        if let Some(handle) = self.task.take() {
            handle.abort();
        }
    }
}

// Chunked TSV stream decoder (testable in isolation)

/// Drive a `bytes_stream`-style async byte source through the TSV
/// line-buffered decoder and emit headers + rows via channels.
///
/// This is the core logic extracted from `Connection::stream` so it
/// can be unit-tested without a real HTTP server.
async fn stream_tsv_chunks<S>(
    stream: S,
    header_tx: tokio::sync::oneshot::Sender<Result<Vec<ColumnHeader>>>,
    row_tx: mpsc::Sender<Result<CoreRow>>,
) where
    S: futures_util::Stream<Item = std::result::Result<bytes::Bytes, reqwest::Error>> + Unpin,
{
    use futures_util::StreamExt;

    let mut stream = stream;
    let mut buf: Vec<u8> = Vec::new();

    // Collect the first two newline-terminated lines (column names,
    // then type strings) before switching to row mode.
    //
    // Header lines are always UTF-8 (ASCII identifiers and type names
    // on the wire). We use `from_utf8_lossy` defensively — if the
    // server sends non-UTF-8 headers that is a server bug, and the
    // replacement character makes it visible.
    let mut header_lines: Vec<String> = Vec::new();

    while header_lines.len() < 2 {
        match stream.next().await {
            Some(Ok(chunk)) => {
                buf.extend_from_slice(&chunk);
                while header_lines.len() < 2 {
                    let Some(pos) = buf.iter().position(|&b| b == b'\n') else {
                        break;
                    };
                    let line_bytes: Vec<u8> = buf.drain(..=pos).collect();
                    // Warn if header bytes are not valid UTF-8 — this
                    // would indicate a server-side bug since column names
                    // and type strings are always ASCII identifiers.
                    if std::str::from_utf8(&line_bytes).is_err() {
                        tracing::warn!(
                            target: "narwhal::clickhouse",
                            "header line contained invalid UTF-8; lossy conversion applied"
                        );
                    }
                    let line = String::from_utf8_lossy(&line_bytes);
                    let line = line.trim_end_matches('\n').trim_end_matches('\r');
                    header_lines.push(line.to_owned());
                }
            }
            Some(Err(e)) => {
                let _ = header_tx.send(Err(Error::query_with("stream error", e)));
                return;
            }
            None => {
                // Stream ended before both header lines arrived — this
                // indicates a network interruption or server-side
                // cancellation, not a legitimate empty result.
                let _ = header_tx.send(Err(Error::Query(
                    "clickhouse stream ended before headers were complete".into(),
                )));
                return;
            }
        }
    }

    // The header phase guarantees exactly two lines in header_lines
    // (the inner loop stops as soon as the second \n is seen and any
    // trailing data row bytes stay in `buf` for row mode to consume).
    // Indexing is safe: the outer `while header_lines.len() < 2` exit
    // condition implies len == 2 here.
    let header_line = header_lines[0].as_str();
    let type_line = header_lines[1].as_str();

    let headers: Vec<String> = header_line.split('\t').map(String::from).collect();
    let type_strings: Vec<String> = type_line.split('\t').map(String::from).collect();

    let column_headers: Vec<ColumnHeader> = headers
        .iter()
        .zip(type_strings.iter())
        .map(|(name, data_type)| ColumnHeader {
            name: name.clone(),
            data_type: data_type.clone(),
        })
        .collect();

    if header_tx.send(Ok(column_headers)).is_err() {
        return;
    }

    // Row mode — byte-level field splitting. Data cells may contain
    // arbitrary bytes (ClickHouse String type is byte-oriented), so we
    // never route through &str on the data path.
    loop {
        while let Some(pos) = buf.iter().position(|&b| b == b'\n') {
            let mut line_bytes: Vec<u8> = buf.drain(..=pos).collect();
            // Strip trailing \n and optional \r.
            if line_bytes.last() == Some(&b'\n') {
                line_bytes.pop();
            }
            if line_bytes.last() == Some(&b'\r') {
                line_bytes.pop();
            }
            if line_bytes.is_empty() {
                continue;
            }
            let fields: Vec<&[u8]> = line_bytes.split(|&b| b == b'\t').collect();
            let mut row = Vec::with_capacity(headers.len());
            for (i, field) in fields.iter().enumerate() {
                let ch_type = type_strings.get(i).map_or("String", String::as_str);
                row.push(parse_tsv_value(field, ch_type));
            }
            while row.len() < headers.len() {
                row.push(Value::Null);
            }
            if row_tx.send(Ok(CoreRow(row))).await.is_err() {
                return;
            }
        }

        match stream.next().await {
            Some(Ok(chunk)) => {
                buf.extend_from_slice(&chunk);
            }
            Some(Err(e)) => {
                let _ = row_tx.send(Err(Error::query_with("stream error", e))).await;
                return;
            }
            None => {
                // End of stream. ClickHouse always terminates rows with
                // \n in TSV, so a non-empty buffer here means the
                // response was truncated mid-row — surface it instead of
                // silently delivering a half-parsed row as a real result.
                if !buf.is_empty() {
                    if buf.last() == Some(&b'\r') {
                        buf.pop();
                    }
                    if !buf.is_empty() {
                        let _ = row_tx
                            .send(Err(Error::Query(
                                "clickhouse stream truncated mid-row (no trailing newline)".into(),
                            )))
                            .await;
                    }
                }
                return;
            }
        }
    }
}

#[cfg(test)]
mod stream_tests {
    use super::*;
    use bytes::Bytes;
    use futures_util::stream;

    /// Feed a known TSV payload through the chunked decoder split
    /// across multiple byte chunks to verify line-buffered splitting.
    #[tokio::test]
    async fn chunked_tsv_decodes_rows() {
        // Simulate a ClickHouse TSV response split across 3 chunks
        // that don't align on line boundaries.
        let payload: &[u8] = b"id\tname\nUInt32\tString\n1\talice\n2\tbob\n";

        // Split the payload across chunk boundaries that don't
        // align with line boundaries.
        let chunks: Vec<std::result::Result<Bytes, reqwest::Error>> = vec![
            Ok(Bytes::copy_from_slice(&payload[..8])),
            Ok(Bytes::copy_from_slice(&payload[8..20])),
            Ok(Bytes::copy_from_slice(&payload[20..])),
        ];

        let byte_stream = stream::iter(chunks);
        let (header_tx, header_rx) = tokio::sync::oneshot::channel::<Result<Vec<ColumnHeader>>>();
        let (row_tx, mut row_rx) = mpsc::channel::<Result<CoreRow>>(64);

        // Call directly instead of spawning — simpler for testing.
        stream_tsv_chunks(byte_stream, header_tx, row_tx).await;

        let columns = header_rx.await.expect("header rx").expect("headers");
        assert_eq!(columns.len(), 2);
        assert_eq!(columns[0].name, "id");
        assert_eq!(columns[0].data_type, "UInt32");
        assert_eq!(columns[1].name, "name");
        assert_eq!(columns[1].data_type, "String");

        let mut rows = Vec::new();
        while let Some(result) = row_rx.recv().await {
            let row = result.expect("row");
            rows.push(row);
        }

        assert_eq!(rows.len(), 2);
        assert!(matches!(rows[0].0.first(), Some(Value::Int(1))));
        assert!(matches!(rows[0].0.get(1), Some(Value::String(_))));
        assert!(matches!(rows[1].0.first(), Some(Value::Int(2))));
        assert!(matches!(rows[1].0.get(1), Some(Value::String(_))));
    }

    /// Verify that a binary String cell (non-UTF-8 bytes) arrives as
    /// `Value::Bytes` through the streaming path.
    #[tokio::test]
    async fn chunked_tsv_preserves_binary_string() {
        // Build a TSV payload where the String column contains
        // 0xFF 0xFE 0x00 0x01 — not valid UTF-8.
        let mut payload: Vec<u8> = b"col\nString\n".to_vec();
        payload.extend_from_slice(&[0xFF, 0xFE, 0x00, 0x01]);
        payload.push(b'\n');

        let chunks: Vec<std::result::Result<Bytes, reqwest::Error>> =
            vec![Ok(Bytes::copy_from_slice(&payload))];

        let byte_stream = stream::iter(chunks);
        let (header_tx, header_rx) = tokio::sync::oneshot::channel::<Result<Vec<ColumnHeader>>>();
        let (row_tx, mut row_rx) = mpsc::channel::<Result<CoreRow>>(64);

        stream_tsv_chunks(byte_stream, header_tx, row_tx).await;

        let columns = header_rx.await.expect("header rx").expect("headers");
        assert_eq!(columns.len(), 1);
        assert_eq!(columns[0].name, "col");
        assert_eq!(columns[0].data_type, "String");

        let row = row_rx.recv().await.expect("row rx").expect("row");
        match row.0.first() {
            Some(Value::Bytes(b)) => assert_eq!(b, &vec![0xFF, 0xFE, 0x00, 0x01]),
            other => panic!("expected Value::Bytes, got {other:?}"),
        }
    }

    /// A response truncated mid-row (no trailing newline after the last
    /// row's bytes) must surface as `Error::Query`, not as a half-parsed
    /// row silently delivered to the consumer.
    #[tokio::test]
    async fn chunked_tsv_truncated_mid_row_errors() {
        // Headers complete (two \n-terminated lines), then a partial
        // row with no trailing \n.
        let payload: &[u8] = b"id\tname\nUInt32\tString\n1\tali";

        let chunks: Vec<std::result::Result<Bytes, reqwest::Error>> =
            vec![Ok(Bytes::copy_from_slice(payload))];

        let byte_stream = stream::iter(chunks);
        let (header_tx, header_rx) = tokio::sync::oneshot::channel::<Result<Vec<ColumnHeader>>>();
        let (row_tx, mut row_rx) = mpsc::channel::<Result<CoreRow>>(64);

        stream_tsv_chunks(byte_stream, header_tx, row_tx).await;

        // Headers came through fine — truncation is mid-row.
        let _columns = header_rx.await.expect("header rx").expect("headers");

        // The first (and only) item from the row channel must be Err.
        match row_rx.recv().await {
            Some(Err(Error::Query(msg))) => {
                assert!(
                    msg.contains("truncated"),
                    "expected truncation error, got: {msg}"
                );
            }
            other => panic!("expected Err(Error::Query(truncated)), got {other:?}"),
        }
    }
}

#[cfg(test)]
mod cancel_tests {
    use super::*;

    /// Verify that query IDs are correctly inserted into and removed
    /// from the active-queries set.
    #[tokio::test]
    async fn tracks_active_query_id() {
        let active: Arc<Mutex<HashSet<String>>> = Arc::new(Mutex::new(HashSet::new()));

        // Simulate inserting a query ID.
        active.lock().insert("test-qid-1".to_owned());
        assert!(active.lock().contains("test-qid-1"));

        // Simulate removing it.
        active.lock().remove("test-qid-1");
        assert!(!active.lock().contains("test-qid-1"));

        // Set should be empty.
        assert!(active.lock().is_empty());
    }

    /// Verify that `cancel()` is idempotent — a second call on the same
    /// handle still sees the same query IDs (they are cloned, not drained).
    #[tokio::test]
    async fn cancel_reads_cloned_not_drained() {
        let active: Arc<Mutex<HashSet<String>>> = Arc::new(Mutex::new(HashSet::new()));
        active.lock().insert("qid-1".to_owned());
        active.lock().insert("qid-2".to_owned());

        // First cancel should read the IDs without draining.
        let client = reqwest::Client::new();
        let base_url = Url::parse("http://127.0.0.1:1/").expect("url");
        let state = Arc::new(SharedState {
            client,
            base_url,
            user: "default".to_owned(),
            password: String::new(),
            database: "default".to_owned(),
            active_queries: active.clone(),
        });
        let cancel = ClickhouseCancel { state };

        // Cancel will try to POST KILL QUERY — this will fail since no
        // server, but the important thing is we don't drain the set.
        let _ = cancel.cancel().await;

        // The active_queries set should still contain the query IDs.
        let remaining = active.lock();
        assert!(
            remaining.contains("qid-1"),
            "qid-1 should still be present after cancel"
        );
        assert!(
            remaining.contains("qid-2"),
            "qid-2 should still be present after cancel"
        );
    }

    /// Verify that the RAII query guard removes the query ID on drop.
    #[tokio::test]
    async fn stream_error_path_clears_active_query() {
        let active: Arc<Mutex<HashSet<String>>> = Arc::new(Mutex::new(HashSet::new()));
        let qid = "test-qid-guard".to_owned();
        active.lock().insert(qid.clone());
        assert!(active.lock().contains(&qid));

        // Simulate the RAII guard being dropped.
        {
            let guard_active = active.clone();
            let guard_qid = qid.clone();
            let _guard = QueryGuard {
                active: guard_active,
                qid: guard_qid,
            };
            // Guard is dropped here — spawns an async task to remove.
        }

        // Yield to allow the spawned removal task to complete.
        tokio::task::yield_now().await;

        assert!(
            !active.lock().contains(&qid),
            "query ID should be removed after guard drops"
        );
    }

    /// Verify that calling cancel with no active queries returns Ok(())
    /// and doesn't attempt an HTTP request (no server to contact).
    #[tokio::test]
    async fn cancel_with_no_active_queries_is_noop() {
        // Build a minimal SharedState. The URL points at an unreachable
        // host — if cancel tries to issue an HTTP request, the test
        // would fail or hang, proving the early-return guard works.
        let active: Arc<Mutex<HashSet<String>>> = Arc::new(Mutex::new(HashSet::new()));
        let client = reqwest::Client::new();
        let base_url = Url::parse("http://127.0.0.1:1/").expect("url");

        let state = Arc::new(SharedState {
            client,
            base_url,
            user: "default".to_owned(),
            password: String::new(),
            database: "default".to_owned(),
            active_queries: active.clone(),
        });

        let cancel = ClickhouseCancel { state };
        let result = cancel.cancel().await;
        assert!(result.is_ok());

        // Active set should still be empty.
        assert!(active.lock().is_empty());
    }
}

#[cfg(test)]
mod build_base_url_tests {
    use super::*;
    use narwhal_core::SslMode;

    fn params(host: &str, port: Option<u16>, ssl_mode: SslMode) -> ConnectionParams {
        ConnectionParams::with(|p| {
            p.host = Some(host.to_owned());
            p.port = port;
            p.ssl_mode = ssl_mode;
        })
    }

    #[test]
    fn ipv6_loopback() {
        let url = build_base_url(&params("::1", None, SslMode::Disable)).unwrap();
        assert_eq!(url.as_str(), "http://[::1]:8123/");
    }

    #[test]
    fn ipv6_full_address() {
        let url = build_base_url(&params("2001:db8::1", Some(8443), SslMode::Prefer)).unwrap();
        assert_eq!(url.as_str(), "https://[2001:db8::1]:8443/");
    }

    #[test]
    fn pre_bracketed_ipv6() {
        // Host that already has brackets should not be double-bracketed.
        let url = build_base_url(&params("[::1]", None, SslMode::Disable)).unwrap();
        assert_eq!(url.as_str(), "http://[::1]:8123/");
    }

    #[test]
    fn ipv4_unchanged() {
        let url = build_base_url(&params("192.168.1.1", Some(8123), SslMode::Disable)).unwrap();
        assert_eq!(url.as_str(), "http://192.168.1.1:8123/");
    }

    #[test]
    fn dns_hostname_unchanged() {
        let url = build_base_url(&params("clickhouse.prod", None, SslMode::Require)).unwrap();
        assert_eq!(url.as_str(), "https://clickhouse.prod:8123/");
    }

    #[test]
    fn missing_host_returns_config_error() {
        let params = ConnectionParams::default();
        let err = build_base_url(&params).unwrap_err();
        assert!(matches!(err, Error::Config(msg) if msg.contains("host is required")));
    }
}

#[cfg(test)]
mod mtls_tests {
    use super::*;
    use narwhal_core::SslMode;

    fn make_config(ssl_cert: Option<&str>, ssl_key: Option<&str>) -> ConnectionConfig {
        let params = ConnectionParams::with(|p| {
            p.host = Some("localhost".to_owned());
            p.port = Some(8123);
            p.ssl_mode = SslMode::Prefer;
            p.ssl_cert = ssl_cert.map(std::path::PathBuf::from);
            p.ssl_key = ssl_key.map(std::path::PathBuf::from);
        });
        ConnectionConfig {
            id: uuid::Uuid::new_v4(),
            name: "test".into(),
            driver: "clickhouse".into(),
            params,
        }
    }

    #[tokio::test]
    async fn mtls_half_config_rejected() {
        let driver = ClickhouseDriver::new();

        // ssl_cert without ssl_key
        let config = make_config(Some("/tmp/cert.pem"), None);
        let result = driver.connect(&config, None).await;
        assert!(result.is_err(), "expected error when only ssl_cert is set");
        let err = result.err().unwrap();
        assert!(
            matches!(err, Error::Config(ref msg) if msg.contains("ssl_cert and ssl_key must both be provided or both omitted")),
            "expected Config error about ssl_cert/ssl_key, got: {err:?}"
        );

        // ssl_key without ssl_cert
        let config = make_config(None, Some("/tmp/key.pem"));
        let result = driver.connect(&config, None).await;
        assert!(result.is_err(), "expected error when only ssl_key is set");
        let err = result.err().unwrap();
        assert!(
            matches!(err, Error::Config(ref msg) if msg.contains("ssl_cert and ssl_key must both be provided or both omitted")),
            "expected Config error about ssl_cert/ssl_key, got: {err:?}"
        );
    }
}