faucet-source-clickhouse 1.0.0

ClickHouse query source connector for the faucet-stream ecosystem
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
//! The ClickHouse [`Source`] implementation — HTTP client, query execution,
//! streaming JSONEachRow decode, and incremental-replication bookkeeping.

use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::pin::Pin;
use std::sync::Mutex;

use async_trait::async_trait;
use faucet_common_clickhouse::{
    apply_auth, build_client, parse_json_each_row, query_params, sql_literal,
};
use faucet_core::check::{CheckContext, CheckReport, Probe};
use faucet_core::replication::{filter_incremental, max_replication_value, max_value};
use faucet_core::util::{DEFAULT_ERROR_BODY_MAX_LEN, check_http_response};
use faucet_core::{FaucetError, Source, Stream, StreamPage};
use futures::StreamExt;
use serde_json::Value;

use crate::config::{ClickHouseReplication, ClickHouseSourceConfig};

/// ClickHouse query source (HTTP interface, `JSONEachRow`).
pub struct ClickHouseSource {
    config: ClickHouseSourceConfig,
    client: reqwest::Client,
    /// Resolved once in [`ClickHouseSource::new`] so the hot path never re-parses.
    base_url: String,
    /// Bookmark loaded via [`Source::apply_start_bookmark`]; overrides the
    /// configured `initial_value` for incremental runs.
    start_bookmark: Mutex<Option<Value>>,
}

/// Incremental-replication context resolved for one run.
#[derive(Debug, Clone, PartialEq)]
struct IncrementalCtx {
    column: String,
    start: Value,
}

impl ClickHouseSource {
    /// Validate the config and build the reusable HTTP client.
    pub fn new(config: ClickHouseSourceConfig) -> Result<Self, FaucetError> {
        config.validate()?;
        let base_url = config.connection.base_url()?;
        let client = build_client(&config.connection)?;
        Ok(Self {
            config,
            client,
            base_url,
            start_bookmark: Mutex::new(None),
        })
    }

    fn current_start(&self) -> Option<Value> {
        self.start_bookmark
            .lock()
            .expect("start_bookmark mutex poisoned")
            .clone()
    }

    /// Build the POST request that runs `query` and returns `JSONEachRow`.
    fn request(&self, query: String) -> reqwest::RequestBuilder {
        let params = query_params(
            &self.config.connection.database,
            &[("default_format", "JSONEachRow")],
        );
        let req = self.client.post(&self.base_url).query(&params).body(query);
        apply_auth(req, &self.config.connection)
    }

    /// Run the query and collect all decoded rows plus (for incremental) the new
    /// bookmark. Used by the non-streaming convenience methods.
    async fn collect_all(
        &self,
        context: &HashMap<String, Value>,
    ) -> Result<(Vec<Value>, Option<Value>), FaucetError> {
        let start = self.current_start();
        let (query, incr) = build_effective_query(&self.config, context, start.as_ref());

        let resp = self.request(query).send().await?;
        let resp = check_http_response(resp, DEFAULT_ERROR_BODY_MAX_LEN).await?;
        let body = resp.text().await.map_err(|e| {
            FaucetError::Source(format!("ClickHouse: reading response failed: {e}"))
        })?;
        let records = parse_json_each_row(&body)?;

        let mut running_max: Option<Value> = None;
        let records = apply_incremental(records, incr.as_ref(), &mut running_max);
        let bookmark = if incr.is_some() { running_max } else { None };
        Ok((records, bookmark))
    }
}

/// Build the final query string and (for incremental runs) the client-side
/// filter context. Pure (no client) so it is unit-testable.
///
/// Substitution order: parent-context `{key}` tokens (as injection-safe SQL
/// literals) → the incremental bookmark bound where the user wrote `@bookmark`.
fn build_effective_query(
    config: &ClickHouseSourceConfig,
    context: &HashMap<String, Value>,
    start_bookmark: Option<&Value>,
) -> (String, Option<IncrementalCtx>) {
    let mut query = if context.is_empty() {
        config.query.clone()
    } else {
        substitute_context_sql(&config.query, context)
    };

    let incremental = match &config.replication {
        ClickHouseReplication::Full => None,
        ClickHouseReplication::Incremental {
            column,
            initial_value,
        } => {
            let start = start_bookmark
                .cloned()
                .unwrap_or_else(|| initial_value.clone());
            // Server-side pushdown: substitute the cursor as an injection-safe
            // SQL literal where the user wrote `@bookmark`. If absent, only the
            // client-side filter applies.
            if query.contains("@bookmark") {
                query = query.replace("@bookmark", &sql_literal(&start));
            }
            Some(IncrementalCtx {
                column: column.clone(),
                start,
            })
        }
    };

    (query, incremental)
}

/// Replace each `{key}` token with the injection-safe SQL literal of the
/// corresponding context value. Tokens with no matching context entry are left
/// verbatim.
fn substitute_context_sql(query: &str, context: &HashMap<String, Value>) -> String {
    let mut out = query.to_string();
    for (key, value) in context {
        out = out.replace(&format!("{{{key}}}"), &sql_literal(value));
    }
    out
}

/// Filter a page for incremental replication and advance `running_max`.
/// For full replication the page passes through unchanged.
fn apply_incremental(
    page: Vec<Value>,
    incr: Option<&IncrementalCtx>,
    running_max: &mut Option<Value>,
) -> Vec<Value> {
    match incr {
        None => page,
        Some(ctx) => {
            let kept = filter_incremental(page, &ctx.column, &ctx.start);
            if let Some(m) = max_replication_value(&kept, &ctx.column) {
                let m = m.clone();
                *running_max = Some(match running_max.take() {
                    Some(prev) => max_value(prev, m),
                    None => m,
                });
            }
            kept
        }
    }
}

/// Drain every complete `\n`-terminated line from `buf`, returning each line's
/// bytes (without the trailing newline). Splitting on the `0x0A` byte is safe
/// for UTF-8 because a newline never appears inside a multi-byte sequence, so a
/// line split across two network chunks is reassembled correctly. Pure and
/// unit-testable.
fn split_complete_lines(buf: &mut Vec<u8>) -> Vec<Vec<u8>> {
    let mut lines = Vec::new();
    while let Some(pos) = buf.iter().position(|&b| b == b'\n') {
        let mut line: Vec<u8> = buf.drain(..=pos).collect();
        line.pop(); // strip the trailing '\n'
        lines.push(line);
    }
    lines
}

/// Parse one raw JSONEachRow line into a JSON value. Blank lines yield `None`.
/// Invalid UTF-8 or JSON surfaces as a typed [`FaucetError::Source`].
fn parse_line(line: &[u8]) -> Result<Option<Value>, FaucetError> {
    let text = std::str::from_utf8(line)
        .map_err(|e| FaucetError::Source(format!("ClickHouse: non-UTF-8 response line: {e}")))?
        .trim();
    if text.is_empty() {
        return Ok(None);
    }
    let value: Value = serde_json::from_str(text).map_err(|e| {
        FaucetError::Source(format!("ClickHouse: failed to parse JSONEachRow line: {e}"))
    })?;
    Ok(Some(value))
}

/// Derive a default state-store key from the connection host + a query
/// fingerprint, stable across runs.
fn default_state_key(config: &ClickHouseSourceConfig) -> String {
    let host = config
        .connection
        .base_url()
        .ok()
        .and_then(|u| url::Url::parse(&u).ok())
        .and_then(|u| u.host_str().map(str::to_string))
        .unwrap_or_else(|| "clickhouse".to_string());

    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    config.query.hash(&mut hasher);
    let fingerprint = hasher.finish();
    let host: String = host
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '.') {
                c
            } else {
                '_'
            }
        })
        .collect();
    format!("clickhouse:{host}:{fingerprint:016x}")
}

#[async_trait]
impl Source for ClickHouseSource {
    async fn fetch_with_context(
        &self,
        context: &HashMap<String, Value>,
    ) -> Result<Vec<Value>, FaucetError> {
        Ok(self.collect_all(context).await?.0)
    }

    async fn fetch_with_context_incremental(
        &self,
        context: &HashMap<String, Value>,
    ) -> Result<(Vec<Value>, Option<Value>), FaucetError> {
        self.collect_all(context).await
    }

    /// Stream rows straight off the HTTP response body without buffering the
    /// whole result set: bytes are accumulated, split into complete
    /// `JSONEachRow` lines, and yielded in [`ClickHouseSourceConfig::batch_size`]
    /// pages. The final page carries the incremental bookmark (when replicating
    /// incrementally) so the pipeline persists only after everything before it
    /// is written.
    fn stream_pages<'a>(
        &'a self,
        context: &'a HashMap<String, Value>,
        _batch_size: usize,
    ) -> Pin<Box<dyn Stream<Item = Result<StreamPage, FaucetError>> + Send + 'a>> {
        let batch_size = self.config.batch_size;
        let chunk = if batch_size == 0 {
            usize::MAX
        } else {
            batch_size
        };
        let cap = if batch_size == 0 { 1024 } else { batch_size };
        let start = self.current_start();
        let (query, incr) = build_effective_query(&self.config, context, start.as_ref());

        Box::pin(async_stream::try_stream! {
            let resp = self.request(query).send().await?;
            let resp = check_http_response(resp, DEFAULT_ERROR_BODY_MAX_LEN).await?;
            let mut body = resp.bytes_stream();

            let mut buf: Vec<u8> = Vec::new();
            let mut page: Vec<Value> = Vec::with_capacity(cap);
            let mut running_max: Option<Value> = None;
            let mut total = 0usize;

            while let Some(chunk_result) = body.next().await {
                let bytes = chunk_result.map_err(FaucetError::Http)?;
                buf.extend_from_slice(&bytes);
                for line in split_complete_lines(&mut buf) {
                    if let Some(value) = parse_line(&line)? {
                        page.push(value);
                        if page.len() >= chunk {
                            let ready = std::mem::replace(&mut page, Vec::with_capacity(cap));
                            let kept = apply_incremental(ready, incr.as_ref(), &mut running_max);
                            total += kept.len();
                            if !kept.is_empty() {
                                yield StreamPage { records: kept, bookmark: None };
                            }
                        }
                    }
                }
            }
            // Trailing line without a terminating newline (ClickHouse always
            // newline-terminates, but be robust).
            if let Some(value) = parse_line(&buf)? {
                page.push(value);
            }

            // Final page carries the bookmark.
            let kept = apply_incremental(page, incr.as_ref(), &mut running_max);
            total += kept.len();
            let bookmark = if incr.is_some() { running_max.clone() } else { None };
            if !kept.is_empty() || bookmark.is_some() {
                yield StreamPage { records: kept, bookmark };
            }

            tracing::info!(rows = total, batch_size, query = %self.config.query, "ClickHouse source stream complete");
        })
    }

    fn config_schema(&self) -> Value {
        serde_json::to_value(faucet_core::schema_for!(ClickHouseSourceConfig))
            .expect("schema serialization")
    }

    fn connector_name(&self) -> &'static str {
        "clickhouse"
    }

    fn dataset_uri(&self) -> String {
        format!(
            "{}?query={}",
            faucet_core::redact_uri_credentials(&self.base_url),
            self.config.query
        )
    }

    fn state_key(&self) -> Option<String> {
        match &self.config.replication {
            ClickHouseReplication::Full => None,
            ClickHouseReplication::Incremental { .. } => Some(
                self.config
                    .state_key
                    .clone()
                    .unwrap_or_else(|| default_state_key(&self.config)),
            ),
        }
    }

    async fn apply_start_bookmark(&self, bookmark: Value) -> Result<(), FaucetError> {
        *self
            .start_bookmark
            .lock()
            .expect("start_bookmark mutex poisoned") = Some(bookmark);
        Ok(())
    }

    /// Non-mutating preflight probe (`connect`): runs `SELECT 1` over the HTTP
    /// interface.
    async fn check(&self, ctx: &CheckContext) -> Result<CheckReport, FaucetError> {
        let started = std::time::Instant::now();
        let hint = "check url / host / database / credentials / that the server is reachable";
        let req = self.request("SELECT 1".to_string());
        let probe = match tokio::time::timeout(ctx.timeout, req.send()).await {
            Ok(Ok(resp)) => match check_http_response(resp, DEFAULT_ERROR_BODY_MAX_LEN).await {
                Ok(_) => Probe::pass("connect", started.elapsed()),
                Err(e) => Probe::fail_hint("connect", started.elapsed(), e.to_string(), hint),
            },
            Ok(Err(e)) => Probe::fail_hint("connect", started.elapsed(), e.to_string(), hint),
            Err(_) => Probe::fail_hint("connect", started.elapsed(), "timed out", hint),
        };
        Ok(CheckReport::single(probe))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn full_cfg() -> ClickHouseSourceConfig {
        ClickHouseSourceConfig::new("http://db.example.com:8123", "SELECT * FROM t")
    }

    #[test]
    fn build_full_returns_query_unchanged() {
        let (q, incr) = build_effective_query(&full_cfg(), &HashMap::new(), None);
        assert_eq!(q, "SELECT * FROM t");
        assert!(incr.is_none());
    }

    #[test]
    fn build_incremental_substitutes_bookmark_literal() {
        let cfg = ClickHouseSourceConfig::new(
            "http://h:8123",
            "SELECT * FROM t WHERE updated_at > @bookmark",
        )
        .incremental("updated_at", json!("1970-01-01"));
        let (q, incr) = build_effective_query(&cfg, &HashMap::new(), None);
        assert_eq!(q, "SELECT * FROM t WHERE updated_at > '1970-01-01'");
        assert_eq!(
            incr,
            Some(IncrementalCtx {
                column: "updated_at".into(),
                start: json!("1970-01-01"),
            })
        );
    }

    #[test]
    fn build_incremental_prefers_stored_bookmark_over_initial() {
        let cfg =
            ClickHouseSourceConfig::new("http://h:8123", "SELECT * FROM t WHERE c > @bookmark")
                .incremental("c", json!(0));
        let stored = json!(500);
        let (q, incr) = build_effective_query(&cfg, &HashMap::new(), Some(&stored));
        assert_eq!(q, "SELECT * FROM t WHERE c > 500");
        assert_eq!(incr.unwrap().start, json!(500));
    }

    #[test]
    fn build_incremental_without_token_still_returns_filter_ctx() {
        let cfg = ClickHouseSourceConfig::new("http://h:8123", "SELECT * FROM t")
            .incremental("c", json!(0));
        let (q, incr) = build_effective_query(&cfg, &HashMap::new(), None);
        assert_eq!(q, "SELECT * FROM t");
        assert!(incr.is_some(), "client-side filter must still run");
    }

    #[test]
    fn context_substitution_uses_injection_safe_literals() {
        let mut ctx = HashMap::new();
        ctx.insert("tenant".to_string(), json!("ac'me"));
        ctx.insert("id".to_string(), json!(7));
        let cfg = ClickHouseSourceConfig::new(
            "http://h:8123",
            "SELECT * FROM t WHERE tenant = {tenant} AND id = {id}",
        );
        let (q, _incr) = build_effective_query(&cfg, &ctx, None);
        assert!(q.contains("tenant = 'ac\\'me'"), "got: {q}");
        assert!(q.contains("id = 7"), "got: {q}");
    }

    #[test]
    fn apply_incremental_filters_and_tracks_max() {
        let ctx = IncrementalCtx {
            column: "c".into(),
            start: json!(10),
        };
        let mut running = None;
        let page = vec![json!({"c": 5}), json!({"c": 15}), json!({"c": 20})];
        let kept = apply_incremental(page, Some(&ctx), &mut running);
        assert_eq!(kept.len(), 2);
        assert_eq!(running, Some(json!(20)));
    }

    #[test]
    fn apply_incremental_full_passes_through() {
        let mut running = None;
        let page = vec![json!({"c": 1}), json!({"c": 2})];
        let kept = apply_incremental(page, None, &mut running);
        assert_eq!(kept.len(), 2);
        assert_eq!(running, None);
    }

    #[test]
    fn split_complete_lines_drains_full_lines_only() {
        let mut buf = b"{\"a\":1}\n{\"a\":2}\n{\"a\":3".to_vec();
        let lines = split_complete_lines(&mut buf);
        assert_eq!(lines.len(), 2, "the unterminated tail stays buffered");
        assert_eq!(lines[0], b"{\"a\":1}");
        assert_eq!(buf, b"{\"a\":3", "partial line remains in the buffer");
    }

    #[test]
    fn split_complete_lines_reassembles_across_chunks() {
        // A line split across two network chunks is reassembled once the
        // second chunk (carrying the newline) arrives.
        let mut buf = b"{\"a\":".to_vec();
        assert!(split_complete_lines(&mut buf).is_empty());
        buf.extend_from_slice(b"1}\n");
        let lines = split_complete_lines(&mut buf);
        assert_eq!(lines, vec![b"{\"a\":1}".to_vec()]);
    }

    #[test]
    fn parse_line_handles_blank_and_valid_and_invalid() {
        assert_eq!(parse_line(b"").unwrap(), None);
        assert_eq!(parse_line(b"   ").unwrap(), None);
        assert_eq!(parse_line(b"{\"a\":1}").unwrap(), Some(json!({"a": 1})));
        assert!(parse_line(b"not-json").is_err());
    }

    #[test]
    fn parse_line_rejects_invalid_utf8() {
        assert!(parse_line(&[0xff, 0xfe]).is_err());
    }

    #[test]
    fn state_key_only_for_incremental_and_is_stable() {
        let source = ClickHouseSource::new(full_cfg()).unwrap();
        assert_eq!(source.state_key(), None, "full replication has no bookmark");

        let cfg = ClickHouseSourceConfig::new("http://db.example.com:8123", "SELECT * FROM t")
            .incremental("c", json!(0));
        let source = ClickHouseSource::new(cfg).unwrap();
        let k1 = source.state_key().unwrap();
        let k2 = source.state_key().unwrap();
        assert_eq!(k1, k2);
        assert!(k1.starts_with("clickhouse:db.example.com:"), "got: {k1}");
        faucet_core::state::validate_state_key(&k1).expect("derived key must be valid");
    }

    #[test]
    fn explicit_state_key_overrides_default() {
        let mut cfg = ClickHouseSourceConfig::new("http://h:8123", "SELECT * FROM t")
            .incremental("c", json!(0));
        cfg.state_key = Some("custom-key".into());
        let source = ClickHouseSource::new(cfg).unwrap();
        assert_eq!(source.state_key().as_deref(), Some("custom-key"));
    }

    #[tokio::test]
    async fn apply_start_bookmark_overrides_initial() {
        let cfg =
            ClickHouseSourceConfig::new("http://h:8123", "SELECT * FROM t WHERE c > @bookmark")
                .incremental("c", json!(0));
        let source = ClickHouseSource::new(cfg).unwrap();
        source.apply_start_bookmark(json!(999)).await.unwrap();
        let (q, incr) = build_effective_query(
            &source.config,
            &HashMap::new(),
            source.current_start().as_ref(),
        );
        assert_eq!(q, "SELECT * FROM t WHERE c > 999");
        assert_eq!(incr.unwrap().start, json!(999));
    }

    #[test]
    fn dataset_uri_has_no_credentials() {
        let source = ClickHouseSource::new(full_cfg()).unwrap();
        assert_eq!(
            source.dataset_uri(),
            "http://db.example.com:8123?query=SELECT * FROM t"
        );
    }

    #[test]
    fn connector_name_is_clickhouse() {
        let source = ClickHouseSource::new(full_cfg()).unwrap();
        assert_eq!(source.connector_name(), "clickhouse");
    }

    #[test]
    fn config_schema_is_object() {
        let source = ClickHouseSource::new(full_cfg()).unwrap();
        assert_eq!(source.config_schema()["type"], "object");
    }

    #[test]
    fn new_rejects_invalid_config() {
        let cfg = ClickHouseSourceConfig::new("http://h:8123", "SELECT 1")
            .with_batch_size(faucet_core::MAX_BATCH_SIZE + 1);
        assert!(ClickHouseSource::new(cfg).is_err());
    }

    #[tokio::test]
    async fn check_fails_against_unreachable_server() {
        // Connection-refused on a closed local port exercises the check() I/O
        // path offline (no external dependency).
        let source = ClickHouseSource::new(ClickHouseSourceConfig::new(
            "http://127.0.0.1:1",
            "SELECT 1",
        ))
        .unwrap();
        let ctx = CheckContext {
            timeout: std::time::Duration::from_secs(2),
        };
        let report = source.check(&ctx).await.unwrap();
        assert!(
            matches!(
                report.probes[0].status,
                faucet_core::check::ProbeStatus::Fail { .. }
            ),
            "unreachable server must fail the connect probe"
        );
    }
}