faucet-core 1.13.1

Shared types, traits, and utilities 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
//! Arrow-native (vectorized) forms of the built-in record transforms (#636).
//!
//! The `Value` transforms in [`crate::transform`] iterate records as
//! `serde_json::Value` — a boxed map + boxed string per field per row. On the
//! columnar fast path (#375) that means a `parquet → select → parquet` chain
//! disqualifies the fast path and re-materializes every record. This module
//! supplies the same transforms as **whole-column** operations on an Arrow
//! [`RecordBatch`], so the chain stays columnar end-to-end.
//!
//! Only the transforms that are genuinely a column operation live here —
//! `select`/`drop` (projection), `rename_field` (schema rename), `set`
//! (constant column), `redact` (constant overwrite). They touch the record's
//! *shape*, never parse or re-type a value, so each is byte-identical to
//! running the `Value` form on the same batch — which is exactly what the
//! parity tests assert. Value-inspecting transforms (`cast`, `value_case`,
//! `hash`, …) and the opaque ones (`flatten`, `explode`, …) return `None` and
//! keep the chain on the `Value` path until they gain their own kernels.

use crate::stage::PageFnBatchBox;
use crate::transform::RecordTransform;

// Everything below the always-present `batch_form` signature is used only when a
// vectorizable transform feature is on. Under bare `arrow` (how the rest and
// bigquery crates pull core) `batch_form` is just `_ => None`, so these imports
// and helpers would otherwise be unused. Each gate matches its real usage.
#[cfg(any(
    feature = "transform-select",
    feature = "transform-drop",
    feature = "transform-rename-field",
    feature = "transform-set",
    feature = "transform-redact"
))]
use crate::FaucetError;
#[cfg(any(feature = "transform-set", feature = "transform-redact"))]
use arrow::array::ArrayRef;
#[cfg(any(
    feature = "transform-select",
    feature = "transform-drop",
    feature = "transform-rename-field",
    feature = "transform-set",
    feature = "transform-redact"
))]
use arrow::array::RecordBatch;
#[cfg(any(feature = "transform-set", feature = "transform-redact"))]
use arrow::datatypes::DataType;
#[cfg(any(
    feature = "transform-rename-field",
    feature = "transform-set",
    feature = "transform-redact"
))]
use arrow::datatypes::Field;
#[cfg(any(
    feature = "transform-select",
    feature = "transform-drop",
    feature = "transform-rename-field",
    feature = "transform-set",
    feature = "transform-redact"
))]
use arrow::datatypes::Schema;
#[cfg(any(
    feature = "transform-select",
    feature = "transform-drop",
    feature = "transform-rename-field",
    feature = "transform-set",
    feature = "transform-redact"
))]
use std::sync::Arc;

/// The Arrow `RecordBatch → RecordBatch` form of `t`, or `None` when `t` has no
/// vectorized kernel yet (the chain then stays on the `Value` path).
pub fn batch_form(t: &RecordTransform) -> Option<PageFnBatchBox> {
    match t {
        #[cfg(feature = "transform-select")]
        RecordTransform::Select { fields } => {
            let fields = fields.clone();
            Some(Arc::new(move |b| select(b, &fields)))
        }
        #[cfg(feature = "transform-drop")]
        RecordTransform::Drop { fields } => {
            let fields = fields.clone();
            Some(Arc::new(move |b| drop(b, &fields)))
        }
        #[cfg(feature = "transform-rename-field")]
        RecordTransform::RenameField { fields } => {
            // Sort into a stable Vec, exactly as `compile_stage` does — a
            // HashMap's order is randomized, and interacting renames
            // (chains/swaps) must resolve identically to the Value path.
            let mut fields: Vec<(String, String)> =
                fields.iter().map(|(f, t)| (f.clone(), t.clone())).collect();
            fields.sort();
            Some(Arc::new(move |b| rename_field(b, &fields)))
        }
        // `set`/`redact` only vectorize when every constant is an
        // exactly-representable scalar. A float, a u64 above i64::MAX, or a
        // container has no simple constant Arrow column that round-trips
        // byte-identically to the `Value` path (a u64 would go lossy through
        // f64; an object would become JSON text instead of staying an object),
        // so those fall back to the `Value` path where they are handled
        // correctly. Common cases — a status string, a flag, an int, a null —
        // stay columnar.
        #[cfg(feature = "transform-set")]
        RecordTransform::Set { values } if values.values().all(is_columnar_safe_scalar) => {
            let values = values.clone();
            Some(Arc::new(move |b| set(b, &values)))
        }
        #[cfg(feature = "transform-redact")]
        RecordTransform::Redact { fields, mask } if is_columnar_safe_scalar(mask) => {
            let fields = fields.clone();
            let mask = mask.clone();
            Some(Arc::new(move |b| redact(b, &fields, &mask)))
        }
        _ => None,
    }
}

/// Whether `v` is a scalar that [`constant_column`] can represent as an Arrow
/// column that round-trips byte-identically to the `Value` path: a bool, an
/// integer that fits `i64`, a string, or null. A float, a `u64` above
/// `i64::MAX`, or any container is **not** safe (see the gate in
/// [`batch_form`]).
#[cfg(any(feature = "transform-set", feature = "transform-redact"))]
fn is_columnar_safe_scalar(v: &serde_json::Value) -> bool {
    use serde_json::Value;
    match v {
        Value::Bool(_) | Value::Null | Value::String(_) => true,
        Value::Number(n) => n.is_i64(),
        _ => false,
    }
}

/// Column index of `name` in `schema`, if present.
#[cfg(any(
    feature = "transform-select",
    feature = "transform-drop",
    feature = "transform-rename-field",
    feature = "transform-set",
    feature = "transform-redact"
))]
fn index_of(schema: &Schema, name: &str) -> Option<usize> {
    schema.fields().iter().position(|f| f.name() == name)
}

/// `select`: keep the named columns in `fields` order, skipping any not in the
/// batch — matching [`crate::transform`]'s `select_fields`.
#[cfg(feature = "transform-select")]
fn select(batch: RecordBatch, fields: &[String]) -> Result<RecordBatch, FaucetError> {
    let schema = batch.schema();
    let indices: Vec<usize> = fields.iter().filter_map(|f| index_of(&schema, f)).collect();
    batch
        .project(&indices)
        .map_err(|e| FaucetError::Transform(format!("columnar select: {e}")))
}

/// `drop`: remove the named columns, keeping the rest in their original order —
/// matching `drop_fields`.
#[cfg(feature = "transform-drop")]
fn drop(batch: RecordBatch, fields: &[String]) -> Result<RecordBatch, FaucetError> {
    let schema = batch.schema();
    let remove: std::collections::HashSet<&str> = fields.iter().map(String::as_str).collect();
    let keep: Vec<usize> = schema
        .fields()
        .iter()
        .enumerate()
        .filter(|(_, f)| !remove.contains(f.name().as_str()))
        .map(|(i, _)| i)
        .collect();
    batch
        .project(&keep)
        .map_err(|e| FaucetError::Transform(format!("columnar drop: {e}")))
}

/// `rename_field`: rename columns against a snapshot of the original schema, so
/// chains (`{a:b, b:c}`) and swaps (`{a:b, b:a}`) are order-independent and the
/// same collision rules as `rename_field` in [`crate::transform`] apply.
#[cfg(feature = "transform-rename-field")]
fn rename_field(
    batch: RecordBatch,
    fields: &[(String, String)],
) -> Result<RecordBatch, FaucetError> {
    let schema = batch.schema();
    // Only renames whose source column exists and that actually change the name.
    let renames: Vec<(&str, &str)> = fields
        .iter()
        .filter(|(from, to)| from != to && index_of(&schema, from).is_some())
        .map(|(from, to)| (from.as_str(), to.as_str()))
        .collect();
    let sources: std::collections::HashSet<&str> = renames.iter().map(|(f, _)| *f).collect();

    let mut seen_targets: std::collections::HashSet<&str> = std::collections::HashSet::new();
    for (from, to) in &renames {
        if !seen_targets.insert(to) {
            return Err(FaucetError::Transform(format!(
                "rename_field: two fields rename to the same target key '{to}'"
            )));
        }
        if index_of(&schema, to).is_some() && !sources.contains(to) {
            return Err(FaucetError::Transform(format!(
                "rename_field: target key '{to}' already exists on the record \
                 (renaming from '{from}')"
            )));
        }
    }
    let map: std::collections::HashMap<&str, &str> = renames.iter().copied().collect();
    let new_fields: Vec<Arc<Field>> = schema
        .fields()
        .iter()
        .map(|f| {
            let name = map.get(f.name().as_str()).copied().unwrap_or(f.name());
            Arc::new(Field::new(name, f.data_type().clone(), f.is_nullable()))
        })
        .collect();
    let new_schema = Arc::new(Schema::new(new_fields));
    RecordBatch::try_new(new_schema, batch.columns().to_vec())
        .map_err(|e| FaucetError::Transform(format!("columnar rename_field: {e}")))
}

/// `set`: add or overwrite each `values` key as a constant column (the same
/// literal in every row) — matching `set_fields`. A key that already exists is
/// overwritten in place, preserving column position; a new key is appended.
#[cfg(feature = "transform-set")]
fn set(
    batch: RecordBatch,
    values: &serde_json::Map<String, serde_json::Value>,
) -> Result<RecordBatch, FaucetError> {
    let mut schema_fields: Vec<Arc<Field>> = batch.schema().fields().iter().cloned().collect();
    let mut columns: Vec<ArrayRef> = batch.columns().to_vec();
    let rows = batch.num_rows();
    for (k, v) in values {
        let (field, array) = constant_column(k, v, rows)?;
        match index_of(&batch.schema(), k) {
            Some(i) => {
                schema_fields[i] = Arc::new(field);
                columns[i] = array;
            }
            None => {
                schema_fields.push(Arc::new(field));
                columns.push(array);
            }
        }
    }
    RecordBatch::try_new(Arc::new(Schema::new(schema_fields)), columns)
        .map_err(|e| FaucetError::Transform(format!("columnar set: {e}")))
}

/// `redact`: overwrite each named column with the constant `mask` value, only
/// for columns present in the batch — matching `redact_fields`.
#[cfg(feature = "transform-redact")]
fn redact(
    batch: RecordBatch,
    fields: &[String],
    mask: &serde_json::Value,
) -> Result<RecordBatch, FaucetError> {
    let mut schema_fields: Vec<Arc<Field>> = batch.schema().fields().iter().cloned().collect();
    let mut columns: Vec<ArrayRef> = batch.columns().to_vec();
    let rows = batch.num_rows();
    for f in fields {
        if let Some(i) = index_of(&batch.schema(), f) {
            let (field, array) = constant_column(f, mask, rows)?;
            schema_fields[i] = Arc::new(field);
            columns[i] = array;
        }
    }
    RecordBatch::try_new(Arc::new(Schema::new(schema_fields)), columns)
        .map_err(|e| FaucetError::Transform(format!("columnar redact: {e}")))
}

#[cfg(any(feature = "transform-set", feature = "transform-redact"))]
/// Build a length-`rows` column holding the constant JSON scalar `v`, typed so
/// that `record_batch_to_values` reproduces `v` exactly. Non-scalar or null
/// values are rendered as their JSON text in a Utf8 column — the same shape the
/// `Value` path would round-trip through arrow-json, and never a silent type
/// change of a neighbouring column.
fn constant_column(
    name: &str,
    v: &serde_json::Value,
    rows: usize,
) -> Result<(Field, ArrayRef), FaucetError> {
    use arrow::array::{BooleanArray, Int64Array, StringArray};
    use serde_json::Value;
    // Only the exactly-representable scalar kinds reach here — `batch_form`
    // gates `set`/`redact` on `is_columnar_safe_scalar`, so a float, a wide
    // integer, or a container has already fallen back to the `Value` path.
    let (dt, arr): (DataType, ArrayRef) = match v {
        Value::Bool(b) => (
            DataType::Boolean,
            Arc::new(BooleanArray::from(vec![*b; rows])),
        ),
        Value::Number(n) if n.is_i64() => (
            DataType::Int64,
            Arc::new(Int64Array::from(vec![n.as_i64().unwrap(); rows])),
        ),
        Value::String(s) => (
            DataType::Utf8,
            Arc::new(StringArray::from(vec![s.clone(); rows])),
        ),
        // Null → an all-null Utf8 column, which round-trips to `key: null`.
        Value::Null => (
            DataType::Utf8,
            Arc::new(StringArray::from(vec![None::<String>; rows])),
        ),
        // Unreachable: gated out by `is_columnar_safe_scalar`. Fail loudly
        // rather than silently corrupt if that gate is ever weakened.
        other => {
            return Err(FaucetError::Transform(format!(
                "columnar set/redact: value {other} is not a columnar-safe scalar                  (should have fallen back to the Value path)"
            )));
        }
    };
    Ok((Field::new(name, dt, true), arr))
}

#[cfg(test)]
mod tests {
    use super::*;
    // Only the feature-gated kernel tests below use these; a bare `arrow` build
    // compiles none of them.
    #[cfg(any(
        feature = "transform-select",
        feature = "transform-drop",
        feature = "transform-rename-field",
        feature = "transform-set",
        feature = "transform-redact"
    ))]
    use crate::columnar::{record_batch_to_values, values_to_record_batch_inferred};
    #[cfg(any(
        feature = "transform-select",
        feature = "transform-drop",
        feature = "transform-rename-field",
        feature = "transform-set",
        feature = "transform-redact"
    ))]
    use crate::stage::{CompiledStage, TransformStage, apply_stages_to_page, compile_stage};
    use crate::transform::RecordTransform;
    #[cfg(any(
        feature = "transform-select",
        feature = "transform-drop",
        feature = "transform-rename-field",
        feature = "transform-set",
        feature = "transform-redact"
    ))]
    use serde_json::{Value, json};

    /// The whole point of this module: the columnar kernel must produce exactly
    /// what running the `Value` stage on the same batch produces. Both start
    /// from `records`, so any divergence is the kernel's.
    #[cfg(any(
        feature = "transform-select",
        feature = "transform-drop",
        feature = "transform-rename-field",
        feature = "transform-set",
        feature = "transform-redact"
    ))]
    fn assert_parity(records: Vec<Value>, t: RecordTransform) {
        let batch = values_to_record_batch_inferred(&records).expect("to batch");

        // Columnar path: kernel over the batch, then materialize.
        let kernel = batch_form(&t).expect("t has a batch form");
        let columnar = record_batch_to_values(&kernel(batch.clone()).expect("kernel"))
            .expect("kernel result to values");

        // Value path: materialize the SAME batch, then run the Value stage.
        let via_value_input = record_batch_to_values(&batch).expect("batch to values");
        let compiled: Vec<CompiledStage> =
            vec![compile_stage(&TransformStage::Map(t)).expect("compile")];
        let value = apply_stages_to_page(via_value_input, &compiled).expect("value stage");

        assert_eq!(
            columnar, value,
            "columnar kernel diverged from the Value stage"
        );
    }

    #[cfg(any(
        feature = "transform-select",
        feature = "transform-drop",
        feature = "transform-rename-field",
        feature = "transform-set",
        feature = "transform-redact"
    ))]
    fn corpus() -> Vec<Value> {
        vec![
            json!({ "id": 1, "name": "ada", "email": "a@x.io", "score": 1.5, "ok": true }),
            json!({ "id": 2, "name": "grace", "email": null, "score": 2.5, "ok": false }),
        ]
    }

    #[cfg(feature = "transform-select")]
    #[test]
    fn select_matches_the_value_path_and_keeps_field_order() {
        assert_parity(
            corpus(),
            RecordTransform::Select {
                // Reordered vs the batch, and one column that does not exist.
                fields: vec!["name".into(), "id".into(), "missing".into()],
            },
        );
        // The projection really is in `fields` order, not schema order.
        let batch = values_to_record_batch_inferred(&corpus()).unwrap();
        let kernel = batch_form(&RecordTransform::Select {
            fields: vec!["name".into(), "id".into()],
        })
        .unwrap();
        let out = kernel(batch).unwrap();
        let out_schema = out.schema();
        let names: Vec<&str> = out_schema
            .fields()
            .iter()
            .map(|f| f.name().as_str())
            .collect();
        assert_eq!(names, vec!["name", "id"]);
    }

    #[cfg(feature = "transform-drop")]
    #[test]
    fn drop_matches_the_value_path() {
        assert_parity(
            corpus(),
            RecordTransform::Drop {
                fields: vec!["email".into(), "score".into(), "missing".into()],
            },
        );
    }

    #[cfg(feature = "transform-set")]
    #[test]
    fn set_matches_the_value_path_for_new_and_overwritten_keys() {
        let mut values = serde_json::Map::new();
        values.insert("stage".into(), json!("prod")); // new column
        values.insert("ok".into(), json!(false)); // overwrite existing
        values.insert("n".into(), json!(42)); // new int column
        assert_parity(corpus(), RecordTransform::Set { values });
    }

    #[cfg(feature = "transform-redact")]
    #[test]
    fn redact_matches_the_value_path() {
        assert_parity(
            corpus(),
            RecordTransform::Redact {
                fields: vec!["email".into(), "name".into(), "missing".into()],
                mask: json!("***"),
            },
        );
    }

    #[cfg(feature = "transform-rename-field")]
    #[test]
    fn rename_field_matches_the_value_path() {
        let mut fields = std::collections::HashMap::new();
        fields.insert("name".to_string(), "full_name".to_string());
        fields.insert("email".to_string(), "contact".to_string());
        assert_parity(corpus(), RecordTransform::RenameField { fields });
    }

    /// A swap (`{a:b, b:a}`) must be order-independent and match the Value
    /// path's snapshot semantics — the case a naive sequential rename corrupts.
    #[cfg(feature = "transform-rename-field")]
    #[test]
    fn rename_field_swap_matches_the_value_path() {
        let mut fields = std::collections::HashMap::new();
        fields.insert("id".to_string(), "name".to_string());
        fields.insert("name".to_string(), "id".to_string());
        assert_parity(corpus(), RecordTransform::RenameField { fields });
    }

    /// Two fields renaming to the *same* target must error, matching the Value
    /// path — otherwise one silently clobbers the other.
    #[cfg(feature = "transform-rename-field")]
    #[test]
    fn rename_field_two_to_one_target_errors_like_the_value_path() {
        let batch = values_to_record_batch_inferred(&corpus()).unwrap();
        let mut fields = std::collections::HashMap::new();
        fields.insert("id".to_string(), "merged".to_string());
        fields.insert("name".to_string(), "merged".to_string());
        let err = batch_form(&RecordTransform::RenameField { fields }).unwrap()(batch)
            .expect_err("two renames to one target must error");
        assert!(err.to_string().contains("same target key"), "{err}");
    }

    /// `set` vectorizes every exactly-representable scalar kind — int, string,
    /// bool, null — byte-identically to the Value path.
    #[cfg(feature = "transform-set")]
    #[test]
    fn set_covers_every_columnar_safe_scalar_kind() {
        let mut values = serde_json::Map::new();
        values.insert("n".into(), json!(7)); // Int64
        values.insert("label".into(), json!("prod")); // Utf8
        values.insert("flag".into(), json!(true)); // Boolean
        values.insert("cleared".into(), Value::Null); // null → nullable Utf8
        assert_parity(corpus(), RecordTransform::Set { values });
    }

    /// The correctness gate: a `set` whose value is NOT an exactly-representable
    /// scalar (a float, a u64 above i64::MAX, or a container) must NOT vectorize
    /// — it falls back to the Value path, which handles it losslessly. Without
    /// this gate the columnar kernel silently made u64::MAX a lossy float and a
    /// nested object a JSON string.
    #[cfg(feature = "transform-set")]
    #[test]
    fn set_with_an_unsafe_constant_falls_back_to_the_value_path() {
        for v in [
            json!(1.5),
            json!(u64::MAX),
            json!({ "a": [1, 2] }),
            json!([1, 2]),
        ] {
            let mut values = serde_json::Map::new();
            values.insert("x".into(), v.clone());
            assert!(
                batch_form(&RecordTransform::Set { values }).is_none(),
                "set of {v} must fall back to Value, not vectorize losslessly"
            );
        }
    }

    /// `redact` likewise only vectorizes a safe scalar mask.
    #[cfg(feature = "transform-redact")]
    #[test]
    fn redact_with_an_unsafe_mask_falls_back_to_the_value_path() {
        assert!(
            batch_form(&RecordTransform::Redact {
                fields: vec!["email".into()],
                mask: json!({ "hidden": true }),
            })
            .is_none()
        );
    }

    /// A rename onto an occupied, non-renamed key must error identically to the
    /// Value path rather than silently clobbering a column.
    #[cfg(feature = "transform-rename-field")]
    #[test]
    fn rename_field_collision_errors_like_the_value_path() {
        let batch = values_to_record_batch_inferred(&corpus()).unwrap();
        let mut fields = std::collections::HashMap::new();
        fields.insert("id".to_string(), "name".to_string()); // `name` already exists, not renamed away
        let err = batch_form(&RecordTransform::RenameField { fields }).unwrap()(batch)
            .expect_err("collision must error");
        assert!(err.to_string().contains("already exists"), "{err}");
    }

    /// Value-inspecting and opaque transforms have no kernel yet, so they must
    /// return `None` and hold the chain on the `Value` path — a wrong `Some`
    /// here would silently run an unimplemented columnar transform.
    #[test]
    fn non_vectorizable_transforms_have_no_batch_form() {
        assert!(
            batch_form(&RecordTransform::Flatten {
                separator: "_".into()
            })
            .is_none()
        );
        #[cfg(feature = "transform-cast")]
        assert!(
            batch_form(&RecordTransform::Cast {
                fields: std::collections::HashMap::new(),
                on_error: Default::default(),
            })
            .is_none(),
            "cast inspects values; it must not claim a kernel until it has one"
        );
    }
}