faucet-cli 1.10.0

Config-driven CLI runner for faucet-stream pipelines (YAML / JSON, Meltano-style)
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
//! Discover a partition bound by running a source once (#479).
//!
//! An id range is frequently open-ended: you know where to start but not where
//! the data ends. Rather than invent a probe protocol, the bound comes from an
//! ordinary **source config** — so `SELECT MAX(id)`, a `?sort=-id&limit=1`
//! request, or a count endpoint all work through the existing connector
//! registry, auth catalog, and secrets. Discovery stays as source-agnostic as
//! the substitution it feeds.
//!
//! ## The probe/plan race
//!
//! Rows inserted between the probe and the last chunk's execution sit above the
//! discovered maximum and would never be read. `to_unbounded: true` closes that
//! by dropping the final chunk's upper bound, so it is **defaulted on** whenever
//! `to` is discovered — the same reason `plan_pk_shards` marks its last shard
//! `hi_unbounded`. An explicit `to_unbounded: false` still wins, for a range the
//! user knows is closed.
//!
//! ## Injection
//!
//! A probed value comes from outside faucet, so it is parsed into a typed `i64` /
//! `u64` here and re-rendered from that. The raw string a source returned is
//! never substituted into a config — which is what keeps the "every token value
//! is faucet-generated" argument true once discovery is in play.

use super::spec::{BoundProbe, CountBound, IntBound, PartitionSpec};
use crate::auth_catalog::AuthCatalog;
use crate::error::{CliError, CliResult};
use serde_json::Value;

/// Resolve every discoverable bound in `spec`, returning a spec whose bounds are
/// literals. A spec with no probes is returned unchanged and costs nothing.
pub async fn resolve_bounds(spec: &PartitionSpec, auth: &AuthCatalog) -> CliResult<PartitionSpec> {
    Ok(match spec {
        PartitionSpec::Integer {
            from,
            to,
            chunk_size,
            bounds,
            to_unbounded,
        } => match to {
            IntBound::Literal(_) => spec.clone(),
            IntBound::Discovered(p) => {
                let raw = probe_value(p, auth).await?;
                let v = as_i64(&raw, &p.value_path)?;
                if v < *from {
                    return Err(CliError::Config(format!(
                        "partition: the discovered upper bound ({v}) is below `from` ({from}) — \
                         the probe `{}` returned an empty or unexpected result",
                        p.value_path
                    )));
                }
                PartitionSpec::Integer {
                    from: *from,
                    to: IntBound::Literal(v),
                    chunk_size: *chunk_size,
                    bounds: *bounds,
                    // Default the open-ended tail ON for a probed bound: the
                    // probe is stale the instant it returns, so rows appended
                    // between it and the last chunk would otherwise be missed.
                    // An explicit setting still wins.
                    to_unbounded: Some(to_unbounded.unwrap_or(true)),
                }
            }
        },
        PartitionSpec::Offset { total, chunk_size } => match total {
            CountBound::Literal(_) => spec.clone(),
            CountBound::Discovered(p) => {
                let raw = probe_value(p, auth).await?;
                let v = as_u64(&raw, &p.value_path)?;
                PartitionSpec::Offset {
                    total: CountBound::Literal(v),
                    chunk_size: *chunk_size,
                }
            }
        },
        PartitionSpec::Timestamp { .. } => spec.clone(),
    })
}

/// Whether `spec` needs a probe — lets `faucet validate` report that it cannot
/// fully plan offline without pretending it can.
pub fn needs_probe(spec: &PartitionSpec) -> bool {
    matches!(
        spec,
        PartitionSpec::Integer {
            to: IntBound::Discovered(_),
            ..
        } | PartitionSpec::Offset {
            total: CountBound::Discovered(_),
            ..
        }
    )
}

/// Whether a probed integer bound should default `to_unbounded` on.
pub fn probe_implies_unbounded(spec: &PartitionSpec) -> bool {
    matches!(
        spec,
        PartitionSpec::Integer {
            to: IntBound::Discovered(_),
            ..
        }
    )
}

/// Run the probe source and pull `value_path` out of its first record.
async fn probe_value(p: &BoundProbe, auth: &AuthCatalog) -> CliResult<Value> {
    let source = crate::registry::build_source(
        &p.from_source.kind,
        p.from_source.config.clone(),
        auth,
        None,
    )
    .await
    .map_err(|e| {
        CliError::Config(format!(
            "partition bound probe: building source failed: {e}"
        ))
    })?;

    let records = source.fetch_all().await.map_err(|e| {
        CliError::Config(format!(
            "partition bound probe: fetching the bound failed: {e}"
        ))
    })?;

    // Empty is a distinct, actionable outcome — not "assume zero". Zero would
    // silently plan nothing and the run would read no data at all.
    let first = records.first().ok_or_else(|| {
        CliError::Config(format!(
            "partition bound probe: source '{}' returned no records, so the bound could not \
             be determined. A `MAX(id)` over an empty table returns NULL rather than a row — \
             give the range an explicit `to` if the source can legitimately be empty",
            p.from_source.kind
        ))
    })?;

    // Reuse core's JSONPath helper so the path grammar matches `records_path`
    // and every other JSONPath surface in the project.
    let found = faucet_core::util::extract_records(first, Some(&p.value_path)).map_err(|e| {
        CliError::Config(format!(
            "partition bound probe: value_path '{}' is not valid JSONPath: {e}",
            p.value_path
        ))
    })?;
    match found.as_slice() {
        [] => Err(CliError::Config(format!(
            "partition bound probe: value_path '{}' matched nothing in the probe's first \
             record ({})",
            p.value_path,
            crate::secrets::registry::redact(&first.to_string())
        ))),
        [one] => Ok(one.clone()),
        many => Err(CliError::Config(format!(
            "partition bound probe: value_path '{}' matched {} values; it must select exactly \
             one",
            p.value_path,
            many.len()
        ))),
    }
}

/// Parse a probed value as a signed bound. A JSON number or a numeric string are
/// both accepted (a SQL driver may hand back either); anything else is an error
/// rather than a silent zero.
fn as_i64(v: &Value, path: &str) -> CliResult<i64> {
    if v.is_null() {
        return Err(CliError::Config(format!(
            "partition bound probe: '{path}' is null — `MAX(id)` over an empty table returns \
             NULL. Give the range an explicit `to`, or ensure the probe cannot match zero rows"
        )));
    }
    v.as_i64()
        .or_else(|| v.as_str().and_then(|s| s.trim().parse::<i64>().ok()))
        .ok_or_else(|| {
            CliError::Config(format!(
                "partition bound probe: '{path}' is not an integer (got {v})"
            ))
        })
}

fn as_u64(v: &Value, path: &str) -> CliResult<u64> {
    if v.is_null() {
        return Err(CliError::Config(format!(
            "partition bound probe: '{path}' is null — give the range an explicit `total`"
        )));
    }
    v.as_u64()
        .or_else(|| v.as_str().and_then(|s| s.trim().parse::<u64>().ok()))
        .ok_or_else(|| {
            CliError::Config(format!(
                "partition bound probe: '{path}' is not a non-negative integer (got {v})"
            ))
        })
}

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

    fn probe() -> BoundProbe {
        BoundProbe {
            from_source: crate::config::ConnectorSpec {
                kind: "csv".into(),
                config: json!({"path": "./x.csv"}),
                transforms: None,
                inherit_transforms: true,
                status: None,
                tags: Vec::new(),
                complete_for: None,
            },
            value_path: "$.max_id".into(),
        }
    }

    #[test]
    fn needs_probe_only_when_a_bound_is_discovered() {
        let literal = PartitionSpec::Integer {
            from: 0,
            to: IntBound::Literal(10),
            chunk_size: 5,
            bounds: crate::chunking::Bounds::Inclusive,
            to_unbounded: None,
        };
        assert!(!needs_probe(&literal));

        let discovered = PartitionSpec::Integer {
            from: 0,
            to: IntBound::Discovered(probe()),
            chunk_size: 5,
            bounds: crate::chunking::Bounds::Inclusive,
            to_unbounded: None,
        };
        assert!(needs_probe(&discovered));
        assert!(probe_implies_unbounded(&discovered));
        assert!(!probe_implies_unbounded(&literal));

        // A timestamp range is never probed.
        assert!(!needs_probe(&PartitionSpec::Timestamp {
            from: "a".into(),
            to: "b".into(),
            chunk_size: "1d".into(),
            timezone: None,
        }));
    }

    #[test]
    fn parses_numbers_and_numeric_strings() {
        assert_eq!(as_i64(&json!(42), "$.x").unwrap(), 42);
        // A SQL driver may hand back a big integer as text.
        assert_eq!(as_i64(&json!("  42 "), "$.x").unwrap(), 42);
        assert_eq!(as_u64(&json!(7), "$.x").unwrap(), 7);
        assert_eq!(as_u64(&json!("7"), "$.x").unwrap(), 7);
    }

    #[test]
    fn null_is_refused_with_the_empty_table_explanation() {
        // `MAX(id)` over an empty table returns NULL. Treating that as 0 would
        // plan a single degenerate chunk and read nothing.
        let err = as_i64(&json!(null), "$.max_id").unwrap_err().to_string();
        assert!(err.contains("null"), "{err}");
        assert!(err.contains("empty table"), "explains why: {err}");
        assert!(as_u64(&json!(null), "$.total").is_err());
    }

    #[test]
    fn non_numeric_is_refused_rather_than_defaulted() {
        for v in [json!("abc"), json!(true), json!({"a": 1}), json!([1])] {
            assert!(as_i64(&v, "$.x").is_err(), "{v} must not parse");
            assert!(as_u64(&v, "$.x").is_err(), "{v} must not parse");
        }
        // Negative is a valid i64 bound but never a valid count.
        assert_eq!(as_i64(&json!(-5), "$.x").unwrap(), -5);
        assert!(as_u64(&json!(-5), "$.x").is_err());
    }

    #[tokio::test]
    async fn a_probe_below_from_is_refused() {
        // Guards the "probe returned something unexpected" case: planning
        // [0, -1] would otherwise be an opaque empty-range error.
        let dir = tempfile::tempdir().unwrap();
        let csv = dir.path().join("p.csv");
        std::fs::write(&csv, "max_id\n-3\n").unwrap();
        let mut p = probe();
        p.from_source.config = json!({ "path": csv.to_str().unwrap() });

        let spec = PartitionSpec::Integer {
            from: 0,
            to: IntBound::Discovered(p),
            chunk_size: 5,
            bounds: crate::chunking::Bounds::Inclusive,
            to_unbounded: None,
        };
        let err = resolve_bounds(&spec, &AuthCatalog::default())
            .await
            .unwrap_err()
            .to_string();
        assert!(err.contains("below `from`"), "{err}");
    }

    #[tokio::test]
    async fn a_probed_bound_defaults_the_open_tail_on() {
        // The probe is stale the instant it returns, so rows appended between it
        // and the last chunk would be missed without an open final chunk.
        let dir = tempfile::tempdir().unwrap();
        let csv = dir.path().join("p.csv");
        std::fs::write(&csv, "max_id\n99\n").unwrap();
        let mut p = probe();
        p.from_source.config = json!({ "path": csv.to_str().unwrap() });

        let mk = |explicit: Option<bool>| PartitionSpec::Integer {
            from: 0,
            to: IntBound::Discovered(p.clone()),
            chunk_size: 50,
            bounds: crate::chunking::Bounds::Inclusive,
            to_unbounded: explicit,
        };
        let unset = resolve_bounds(&mk(None), &AuthCatalog::default())
            .await
            .unwrap();
        match unset {
            PartitionSpec::Integer { to_unbounded, .. } => {
                assert_eq!(
                    to_unbounded,
                    Some(true),
                    "unset must default ON when probed"
                )
            }
            o => panic!("{o:?}"),
        }
        // An explicit `false` still wins — the user knows the range is closed.
        let forced = resolve_bounds(&mk(Some(false)), &AuthCatalog::default())
            .await
            .unwrap();
        match forced {
            PartitionSpec::Integer { to_unbounded, .. } => assert_eq!(to_unbounded, Some(false)),
            o => panic!("{o:?}"),
        }
    }

    #[tokio::test]
    async fn a_literal_bound_never_defaults_the_open_tail_on() {
        let spec = PartitionSpec::Integer {
            from: 0,
            to: IntBound::Literal(10),
            chunk_size: 5,
            bounds: crate::chunking::Bounds::Inclusive,
            to_unbounded: None,
        };
        let out = resolve_bounds(&spec, &AuthCatalog::default())
            .await
            .unwrap();
        match out {
            PartitionSpec::Integer { to_unbounded, .. } => assert_eq!(to_unbounded, None),
            o => panic!("{o:?}"),
        }
    }

    #[tokio::test]
    async fn a_literal_spec_is_returned_unchanged_without_any_probing() {
        let spec = PartitionSpec::Offset {
            total: CountBound::Literal(10),
            chunk_size: 5,
        };
        let out = resolve_bounds(&spec, &AuthCatalog::default())
            .await
            .unwrap();
        assert_eq!(out, spec);
    }

    #[tokio::test]
    async fn a_discovered_bound_is_resolved_from_the_probe_source() {
        let dir = tempfile::tempdir().unwrap();
        let csv = dir.path().join("p.csv");
        std::fs::write(&csv, "max_id\n99\n").unwrap();
        let mut p = probe();
        p.from_source.config = json!({ "path": csv.to_str().unwrap() });

        let spec = PartitionSpec::Integer {
            from: 0,
            to: IntBound::Discovered(p),
            chunk_size: 50,
            bounds: crate::chunking::Bounds::Inclusive,
            to_unbounded: Some(true),
        };
        let out = resolve_bounds(&spec, &AuthCatalog::default())
            .await
            .unwrap();
        match out {
            PartitionSpec::Integer { to, .. } => assert_eq!(to, IntBound::Literal(99)),
            other => panic!("expected an integer spec, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn an_empty_probe_result_is_actionable_not_zero() {
        let dir = tempfile::tempdir().unwrap();
        let csv = dir.path().join("p.csv");
        std::fs::write(&csv, "max_id\n").unwrap(); // header only
        let mut p = probe();
        p.from_source.config = json!({ "path": csv.to_str().unwrap() });

        let spec = PartitionSpec::Integer {
            from: 0,
            to: IntBound::Discovered(p),
            chunk_size: 5,
            bounds: crate::chunking::Bounds::Inclusive,
            to_unbounded: None,
        };
        let err = resolve_bounds(&spec, &AuthCatalog::default())
            .await
            .unwrap_err()
            .to_string();
        assert!(err.contains("no records"), "{err}");
        assert!(err.contains("explicit `to`"), "suggests the fix: {err}");
    }
}