faucet-source-redshift 1.3.1

Amazon Redshift 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
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
//! Amazon Redshift query source implementation.
//!
//! Redshift is PostgreSQL wire-compatible, so the source streams rows through
//! `sqlx`'s Postgres cursor, re-framing them into `batch_size`-sized pages. It
//! supports full and incremental replication: an incremental read wraps the
//! query in a `>=` cursor filter ordered by the cursor and drops the rows it
//! already emitted at the bookmark value (`CursorTracker`).

use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Mutex;

use async_trait::async_trait;
use faucet_common_redshift::data_api::{self, DataColumn, Effect};
use faucet_common_redshift::{DataApiClient, RedshiftPool};
use faucet_core::replication::cursor::{CursorBookmark, CursorTracker};
use faucet_core::util::{quote_ident, substitute_context_bind_params};
use faucet_core::{FaucetError, Source, Stream, StreamPage};
use serde_json::Value;

use crate::config::{BOOKMARK_TOKEN, RedshiftReplication, RedshiftSourceConfig};
use crate::convert::{bind_params, row_to_json};

/// A source that executes a SQL query against Amazon Redshift and returns rows
/// as JSON objects.
pub struct RedshiftSource {
    config: RedshiftSourceConfig,
    backend: Backend,
    /// Bookmark applied via [`Source::apply_start_bookmark`] (incremental only).
    start_bookmark: Mutex<Option<CursorBookmark>>,
    /// Backend-call context (#880): the query and every page of rows run
    /// through it.
    calls: faucet_core::CallSlot,
}

/// How statements reach Redshift.
enum Backend {
    /// The PostgreSQL wire protocol (`password`, `iam`).
    Wire(RedshiftPool),
    /// The Redshift Data API over HTTPS (`redshift_data_api`).
    DataApi(Box<DataApiClient>),
}

/// A backend ready to run a query: the wire pool with fresh credentials, or
/// the Data API client.
enum Conn<'a> {
    Wire(sqlx::PgPool),
    DataApi(&'a DataApiClient),
}

/// Where the rows of a running query come from.
enum Cursor<'a> {
    Wire(futures::stream::BoxStream<'a, Result<sqlx::postgres::PgRow, sqlx::Error>>),
    DataApi {
        api: &'a DataApiClient,
        id: String,
        token: Option<String>,
        columns: Vec<DataColumn>,
        done: bool,
    },
}

/// Incremental-read context (column + starting position).
struct IncrementalCtx {
    column: String,
    start: CursorBookmark,
}

impl RedshiftSource {
    /// Create a new source. Validates config and builds a lazily-connected pool
    /// (no I/O — connectivity is verified on first query or via
    /// [`Source::check`]).
    pub fn new(config: RedshiftSourceConfig) -> Result<Self, FaucetError> {
        config.validate()?;
        let backend = match DataApiClient::from_connection(&config.connection, FaucetError::Source)?
        {
            Some(api) => Backend::DataApi(Box::new(api)),
            None => Backend::Wire(faucet_common_redshift::connect_lazy(
                &config.connection,
                config.max_connections,
            )?),
        };
        Ok(Self {
            config,
            backend,
            start_bookmark: Mutex::new(None),
            calls: faucet_core::CallSlot::new(faucet_core::CallContext::new().without_timeout()),
        })
    }

    /// The effective incremental start bookmark (persisted bookmark, else the
    /// configured `initial_value`), or `None` for full replication.
    fn incremental_ctx(&self) -> Option<IncrementalCtx> {
        match &self.config.replication {
            RedshiftReplication::Full => None,
            RedshiftReplication::Incremental {
                column,
                initial_value,
            } => {
                let start = self
                    .start_bookmark
                    .lock()
                    .expect("start_bookmark mutex poisoned")
                    .clone()
                    .unwrap_or_else(|| CursorBookmark::initial(initial_value.clone()));
                Some(IncrementalCtx {
                    column: column.clone(),
                    start,
                })
            }
        }
    }

    /// Build the effective SQL and ordered positional bind values for a parent
    /// context + optional incremental cursor, whose bookmark binds as
    /// `CAST($n AS <cast>)` (the cursor column's type).
    ///
    /// Bind order is: static [`config.params`](RedshiftSourceConfig::params),
    /// then parent-context `{key}` values, then the `${bookmark}` value (when
    /// the query contains that token), then the bookmark of the outer
    /// `column >= …` filter.
    fn resolve_query(
        &self,
        context: &HashMap<String, Value>,
        incr: Option<&IncrementalCtx>,
        cast: &str,
    ) -> (String, Vec<Value>) {
        let mut binds = self.config.params.clone();
        let mut sql = self.config.query.clone();

        if !context.is_empty() {
            let (rewritten, ctx_values) =
                substitute_context_bind_params(&sql, context, binds.len() + 1, |i| format!("${i}"));
            sql = rewritten;
            binds.extend(ctx_values);
        }

        let Some(ctx) = incr else {
            return (sql, binds);
        };
        let cast = bookmark_cast(cast, &ctx.start.value);
        if sql.contains(BOOKMARK_TOKEN) {
            let marker = format!("CAST(${} AS {cast})", binds.len() + 1);
            sql = sql.replace(BOOKMARK_TOKEN, &marker);
            binds.push(ctx.start.value.clone());
        }
        binds.push(ctx.start.value.clone());
        let c = quote_ident(&ctx.column);
        let inner = sql.trim_end_matches(|ch: char| ch.is_whitespace() || ch == ';');
        (
            format!(
                "SELECT * FROM ({inner}\n) AS q WHERE q.{c} >= CAST(${n} AS {cast}) ORDER BY q.{c}",
                n = binds.len()
            ),
            binds,
        )
    }

    /// The cursor column's type, as the server describes the query's output.
    async fn cursor_cast(
        &self,
        context: &HashMap<String, Value>,
        column: &str,
    ) -> Result<String, FaucetError> {
        let described = self.describe(context).await.map_err(|e| match e {
            FaucetError::Source(m) => FaucetError::Source(format!(
                "redshift: could not describe the incremental query: {m}"
            )),
            other => other,
        })?;
        let name = described
            .iter()
            .find(|(c, _, _)| c == column)
            .map(|(_, t, _)| t.clone())
            .ok_or_else(|| {
                FaucetError::Config(format!(
                    "redshift: incremental replication `column` {column:?} is not a column of \
                     the query's output"
                ))
            })?;
        cast_type(&name)
    }

    /// The query's output columns as `(name, type name, nullable)`: a
    /// describe on the wire, a `LIMIT 0` run of the query through the Data
    /// API (which has no describe).
    async fn describe(
        &self,
        context: &HashMap<String, Value>,
    ) -> Result<Vec<(String, String, Option<bool>)>, FaucetError> {
        use sqlx::Executor;
        let (probe, binds) = self.resolve_query(context, None, "");
        let probe = probe.replace(BOOKMARK_TOKEN, "NULL");
        let calls = self.calls.get();
        match &self.backend {
            Backend::Wire(pool) => {
                let pool = pool.get(&calls).await?;
                let described = faucet_common_redshift::calls::once(&calls, "describe", async {
                    (&pool)
                        .describe(&probe)
                        .await
                        .map_err(|e| FaucetError::Source(e.to_string()))
                })
                .await?;
                Ok(described
                    .columns()
                    .iter()
                    .enumerate()
                    .map(|(i, c)| {
                        (
                            sqlx::Column::name(c).to_string(),
                            sqlx::TypeInfo::name(sqlx::Column::type_info(c)).to_string(),
                            described.nullable(i),
                        )
                    })
                    .collect())
            }
            Backend::DataApi(api) => {
                let inner = probe.trim_end_matches(|ch: char| ch.is_whitespace() || ch == ';');
                let sql = format!("SELECT * FROM ({inner}\n) AS q LIMIT 0");
                let (columns, _) = api
                    .query(&calls, &data_api::named_parameters(&sql, &binds))
                    .await?;
                Ok(columns
                    .into_iter()
                    .map(|c| (c.name, c.type_name, c.nullable))
                    .collect())
            }
        }
    }

    /// Start `sql` and return where its rows come from.
    async fn open<'a>(
        calls: &faucet_core::io::CallContext,
        conn: &'a Conn<'a>,
        sql: &'a str,
        binds: &'a [Value],
    ) -> Result<Cursor<'a>, FaucetError> {
        use futures::StreamExt;
        match conn {
            Conn::DataApi(api) => {
                let done = api
                    .execute(calls, Effect::Read, &data_api::named_parameters(sql, binds))
                    .await?;
                Ok(Cursor::DataApi {
                    api,
                    id: done.id,
                    token: None,
                    columns: Vec::new(),
                    done: !done.has_result_set,
                })
            }
            Conn::Wire(pool) => {
                let query = bind_params(sqlx::query(sql), binds)?;
                calls.record("query");
                Ok(Cursor::Wire(query.fetch(pool).boxed()))
            }
        }
    }

    /// Up to `max` more rows of `cursor` as JSON objects, and whether it is
    /// exhausted. A Data API page is read whole.
    async fn next_chunk(
        calls: &faucet_core::io::CallContext,
        cursor: &mut Cursor<'_>,
        max: usize,
    ) -> Result<(Vec<Value>, bool), FaucetError> {
        match cursor {
            Cursor::Wire(rows) => {
                let reads = calls.clone().with_roundtrip_metering(false);
                let fetched =
                    faucet_common_redshift::calls::next_rows(&reads, "fetch", rows, max, |e| {
                        FaucetError::Source(format!("redshift query failed: {e}"))
                    })
                    .await?;
                let exhausted = fetched.len() < max;
                let rows = fetched.iter().map(row_to_json).collect::<Result<_, _>>()?;
                Ok((rows, exhausted))
            }
            Cursor::DataApi {
                api,
                id,
                token,
                columns,
                done,
            } => {
                if *done {
                    return Ok((Vec::new(), true));
                }
                let page = api.result_page(calls, id, token.as_deref()).await?;
                if columns.is_empty() {
                    *columns = page.columns;
                }
                let rows = page
                    .rows
                    .iter()
                    .map(|f| data_api::row_to_json(columns, f).map(Value::Object))
                    .collect::<Result<_, _>>()?;
                *token = page.next_token;
                *done = token.is_none();
                Ok((rows, *done))
            }
        }
    }
}

/// The cast a bookmark binds through. The server describes a column's type
/// without its precision and scale, and a bare `NUMERIC` / `DECIMAL` on
/// Redshift is `DECIMAL(18,0)`, which would round a fractional bookmark and
/// skip the rows just above it. A decimal cursor is cast at the widest
/// precision and the bookmark's own scale instead, which is exact (the
/// bookmark is rendered at the column's scale).
fn bookmark_cast(cast: &str, bookmark: &Value) -> String {
    let base = cast.trim().to_ascii_uppercase();
    if !matches!(base.as_str(), "NUMERIC" | "DECIMAL") {
        return cast.to_string();
    }
    let text = match bookmark {
        Value::String(s) => s.trim().to_string(),
        Value::Number(n) => n.to_string(),
        _ => String::new(),
    };
    let mantissa = text.split(['e', 'E']).next().unwrap_or("");
    let scale = mantissa
        .split_once('.')
        .map_or(0, |(_, frac)| {
            frac.chars().filter(char::is_ascii_digit).count()
        })
        .min(37);
    format!("NUMERIC(38,{scale})")
}

/// A server-reported type name usable in `CAST(… AS <name>)`.
fn cast_type(name: &str) -> Result<String, FaucetError> {
    let ok = !name.is_empty()
        && name
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | ' ' | '"' | '[' | ']'));
    if ok {
        Ok(name.to_string())
    } else {
        Err(FaucetError::Config(format!(
            "redshift: the incremental cursor column has type {name:?}, which cannot be \
             compared to a bookmark; cast it in the query"
        )))
    }
}

/// Derive a stable state key from the host, database, and query.
fn default_state_key(config: &RedshiftSourceConfig) -> String {
    // Fixed FNV-1a: the key is durable and `DefaultHasher` is not stable across Rust releases.
    let material = format!(
        "{}\u{1f}{}\u{1f}{}\u{1f}{}",
        config.connection.endpoint_label(),
        config.connection.port,
        config.connection.database,
        config.query
    );
    format!(
        "redshift:{:016x}",
        faucet_core::shard::shard_hash(&material)
    )
}

/// The logical type of a described column as this source renders it (#955).
fn declared_type(type_name: &str) -> faucet_core::LogicalType {
    use faucet_core::LogicalType as L;
    match type_name.to_ascii_uppercase().as_str() {
        "TIMETZ" | "INTERVAL" | "OID" => L::String,
        _ => L::from_sql_type(type_name, None, None).unwrap_or(L::String),
    }
}

#[async_trait]
impl Source for RedshiftSource {
    fn connector_name(&self) -> &'static str {
        "redshift"
    }

    fn capabilities(&self) -> faucet_core::Capabilities {
        faucet_core::Capabilities::of_source(self)
            .to_builder()
            .declared_columns(true)
            .build()
    }

    /// The query's columns from a describe of it (#955). The server reports
    /// no decimal precision, so a `NUMERIC` is declared unbounded.
    async fn declared_columns(
        &self,
        context: &HashMap<String, Value>,
    ) -> Result<Option<faucet_core::DeclaredColumns>, FaucetError> {
        let Ok(described) = self.describe(context).await else {
            return Ok(None);
        };
        Ok(Some(faucet_core::DeclaredColumns::new(
            described.into_iter().map(|(name, ty, nullable)| {
                faucet_core::DeclaredColumn::new(name, declared_type(&ty), nullable != Some(false))
            }),
        )))
    }

    fn set_call_context(&self, ctx: faucet_core::io::CallContext) {
        self.calls.install(ctx);
    }

    fn set_roundtrip_recorder(
        &self,
        recorder: std::sync::Arc<faucet_core::observability::RoundtripRecorder>,
    ) {
        self.calls.install_recorder(recorder);
    }

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

    fn dataset_uri(&self) -> String {
        let conn = &self.config.connection;
        if conn.uses_data_api() {
            return format!(
                "redshift://{}/{}?query={}",
                conn.endpoint_label(),
                conn.database,
                self.config.query
            );
        }
        format!(
            "redshift://{}:{}/{}?query={}",
            self.config.connection.host,
            self.config.connection.port,
            self.config.connection.database,
            self.config.query
        )
    }

    fn state_key(&self) -> Option<String> {
        match &self.config.replication {
            RedshiftReplication::Full => None,
            RedshiftReplication::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> {
        let start = CursorBookmark::from_state(bookmark)?;
        *self
            .start_bookmark
            .lock()
            .expect("start_bookmark mutex poisoned") = Some(start);
        Ok(())
    }

    async fn fetch_with_context(
        &self,
        context: &HashMap<String, Value>,
    ) -> Result<Vec<Value>, FaucetError> {
        Ok(self.fetch_with_context_incremental(context).await?.0)
    }

    async fn fetch_with_context_incremental(
        &self,
        context: &HashMap<String, Value>,
    ) -> Result<(Vec<Value>, Option<Value>), FaucetError> {
        use futures::StreamExt;
        let mut records = Vec::new();
        let mut bookmark = None;
        let mut s = self.stream_pages(context, self.config.batch_size);
        while let Some(page) = s.next().await {
            let page = page?;
            records.extend(page.records);
            bookmark = page.bookmark.or(bookmark);
        }
        Ok((records, bookmark))
    }

    /// Stream rows from the `sqlx` cursor without buffering the full result set.
    /// Each emitted [`StreamPage`] holds up to
    /// [`RedshiftSourceConfig::batch_size`] rows.
    ///
    /// For incremental replication rows arrive in cursor order, the rows
    /// already emitted at the bookmark value are dropped, and every page
    /// carries the bookmark after its last row (the final page carries one even
    /// when empty).
    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;

        Box::pin(async_stream::try_stream! {
            let incr = self.incremental_ctx();
            let cast = match &incr {
                Some(ic) => self.cursor_cast(context, &ic.column).await?,
                None => String::new(),
            };
            let (query_str, binds) = self.resolve_query(context, incr.as_ref(), &cast);
            let mut tracker = incr
                .as_ref()
                .map(|ic| CursorTracker::new(ic.column.clone(), ic.start.clone()));
            let calls = self.calls.get();
            let conn = match &self.backend {
                Backend::Wire(p) => Conn::Wire(p.get(&calls).await?),
                Backend::DataApi(api) => Conn::DataApi(api),
            };
            let mut cursor = Self::open(&calls, &conn, &query_str, &binds).await?;
            let chunk = if batch_size == 0 { usize::MAX } else { batch_size };
            let cap = if batch_size == 0 { 1024 } else { batch_size };
            let mut buffer: Vec<Value> = Vec::with_capacity(cap);
            let mut total = 0usize;

            loop {
            let (fetched, exhausted) = Self::next_chunk(&calls, &mut cursor, chunk).await?;
            for obj in fetched {
                if tracker.as_mut().is_none_or(|t| t.admit(&obj)) {
                    buffer.push(obj);
                }
                if buffer.len() >= chunk {
                    let page = std::mem::replace(&mut buffer, Vec::with_capacity(cap));
                    total += page.len();
                    let bookmark = tracker.as_ref().map(|t| t.bookmark().to_value());
                    yield StreamPage { records: page, bookmark };
                }
            }
            if exhausted {
                break;
            }
            }

            if !buffer.is_empty() || tracker.is_some() {
                total += buffer.len();
                let bookmark = tracker.as_ref().map(|t| t.bookmark().to_value());
                yield StreamPage { records: buffer, bookmark };
            }
            if let Some(missing) = tracker.as_ref().map(CursorTracker::missing).filter(|n| *n > 0) {
                tracing::warn!(
                    missing,
                    "Redshift incremental read: rows without a cursor value were emitted \
                     without moving the bookmark"
                );
            }

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

    /// Preflight probe for `faucet doctor`: acquire a connection and run
    /// `SELECT 1` (non-scanning — never executes the configured query).
    async fn check(
        &self,
        ctx: &faucet_core::check::CheckContext,
    ) -> Result<faucet_core::check::CheckReport, FaucetError> {
        use faucet_core::check::{CheckReport, Probe};

        let started = std::time::Instant::now();
        let calls = self.calls.get();
        let ping = async {
            match &self.backend {
                Backend::Wire(pool) => {
                    let pool = pool.get(&calls).await?;
                    faucet_common_redshift::calls::once(&calls, "query", async {
                        sqlx::query("SELECT 1")
                            .execute(&pool)
                            .await
                            .map(|_| ())
                            .map_err(|e| FaucetError::Source(e.to_string()))
                    })
                    .await
                }
                Backend::DataApi(api) => api
                    .query(&calls, &data_api::Statement::new("SELECT 1"))
                    .await
                    .map(|_| ()),
            }
        };
        let probe = match tokio::time::timeout(ctx.timeout, ping).await {
            Ok(Ok(_)) => Probe::pass("auth", started.elapsed()),
            Ok(Err(e)) => Probe::fail_hint(
                "auth",
                started.elapsed(),
                e.to_string(),
                "check host/port/database/user/credentials and that the cluster is reachable",
            ),
            Err(_) => Probe::fail_hint(
                "auth",
                started.elapsed(),
                "timed out",
                "check host/port/database/user/credentials and that the cluster is reachable",
            ),
        };
        Ok(CheckReport::single(probe))
    }
}

#[cfg(test)]
mod tests;