bunsen-firehose 0.21.3

bunsen dataloader / processing pipeline
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
use std::collections::BTreeMap;

use anyhow::{
    Context,
    bail,
};
use serde::{
    Deserialize,
    Serialize,
};

use crate::core::schema::{
    BuildPlan,
    ColumnSchema,
    DataTypeDescription,
};

/// Defines a single parameter specification
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ParameterSpec {
    /// The name of the parameter.
    pub name: String,

    /// An optional description of the parameter.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    pub description: Option<String>,

    /// The data type of the parameter.
    pub data_type: DataTypeDescription,
}

impl ParameterSpec {
    /// Creates a new `ParameterSpec` with the given name and data type.
    ///
    /// The type `T` is used to infer the data type of the parameter.
    ///
    /// # Parameters
    ///
    /// - `name`: The name of the parameter.
    /// - `T`: The type of the parameter, which is used to determine the data
    ///   type description.
    ///
    /// # Returns
    ///
    /// A new `ParameterSpec` instance with the specified name, data type, and
    /// required arity.
    pub fn new<T>(name: &str) -> Self {
        Self {
            name: name.to_string(),
            description: None,
            data_type: DataTypeDescription::new::<T>(),
        }
    }

    /// Extends the parameter specification with a description.
    pub fn with_description(
        self,
        description: impl Into<String>,
    ) -> Self {
        Self {
            name: self.name,
            description: Some(description.into()),
            data_type: self.data_type,
        }
    }
}

/// Defines the call signature of a firehose operator.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FirehoseOperatorSignature {
    /// The identifier for the operator.
    pub operator_id: Option<String>,

    /// Optional description.
    pub description: Option<String>,

    /// A list of input parameters for the operator.
    pub inputs: Vec<ParameterSpec>,

    /// A list of output parameters for the operator.
    pub outputs: Vec<ParameterSpec>,
}

impl Default for FirehoseOperatorSignature {
    fn default() -> Self {
        Self::new()
    }
}

impl FirehoseOperatorSignature {
    /// Returns a reference to the input parameter named `name`, if it exists.
    pub fn get_input(
        &self,
        name: &str,
    ) -> Option<&ParameterSpec> {
        self.inputs.iter().find(|spec| spec.name == name)
    }

    /// Returns a reference to the output parameter named `name`, if it exists.
    pub fn get_output(
        &self,
        name: &str,
    ) -> Option<&ParameterSpec> {
        self.outputs.iter().find(|spec| spec.name == name)
    }

    /// Creates a new `OperatorSpec` with no inputs or outputs.
    pub const fn new() -> Self {
        Self {
            operator_id: None,
            description: None,
            inputs: Vec::new(),
            outputs: Vec::new(),
        }
    }

    /// Creates a new `FirehoseOperatorSignature` with the specified operator
    /// ID.
    pub fn from_operator_id(operator_id: &str) -> Self {
        Self {
            operator_id: Some(operator_id.into()),
            description: None,
            inputs: Vec::new(),
            outputs: Vec::new(),
        }
    }

    /// Extends the operator specification with an operator ID.
    pub fn with_operator_id(
        self,
        operator_id: &str,
    ) -> Self {
        Self {
            operator_id: Some(operator_id.into()),
            description: self.description,
            inputs: self.inputs,
            outputs: self.outputs,
        }
    }

    /// Extends the operator specification with a description.
    pub fn with_description(
        self,
        description: &str,
    ) -> Self {
        Self {
            operator_id: self.operator_id,
            description: Some(description.to_string()),
            inputs: self.inputs,
            outputs: self.outputs,
        }
    }

    /// Internal helper to add a parameter specification to the list of inputs
    /// or outputs.
    ///
    /// # Parameters
    ///
    /// * `spec`: The parameter specification to add.
    /// * `ptype`: A string indicating the type of parameter ("input" or
    ///   "output").
    /// * `specs`: The current list of parameter specifications (either inputs
    ///   or outputs).
    ///
    /// # Returns
    ///
    /// An `anyhow::Result<Vec<ParameterSpec>>` containing a new vector of
    /// parameter specifications with the added parameter.
    fn with_parameter(
        spec: ParameterSpec,
        ptype: &str,
        specs: &[ParameterSpec],
    ) -> anyhow::Result<Vec<ParameterSpec>> {
        if let Some(that) = specs.iter().find(|prev| prev.name == spec.name) {
            bail!(
                "Duplicate {ptype} parameter '{}':\na. {:?}\nb. {:?}",
                spec.name,
                that,
                spec
            )
        } else {
            let mut new_specs = specs.to_vec();
            new_specs.push(spec);
            Ok(new_specs)
        }
    }

    /// Extends the operator specification with an input parameter.
    ///
    /// # Parameters
    ///
    /// * `spec`: The input parameter specification to add.
    ///
    /// # Returns
    ///
    /// An `Result<Self, String>` where:
    /// * `Ok(Self)`: A new `FirehoseOperatorSignature` with the input parameter
    ///   added.
    /// * `Err(String)`: An error message if the input parameter name already
    ///   exists in the signature.
    pub fn with_input_result(
        self,
        spec: ParameterSpec,
    ) -> anyhow::Result<Self> {
        Ok(Self {
            operator_id: self.operator_id,
            description: self.description,
            inputs: Self::with_parameter(spec, "input", &self.inputs)?,
            outputs: self.outputs.clone(),
        })
    }

    /// Extends the operator specification with an input parameter.
    ///
    /// # Parameters
    ///
    /// * `spec`: The input parameter specification to add.
    ///
    /// # Returns
    ///
    /// A new `FirehoseOperatorSignature` with the input parameter added.
    ///
    /// # Panics
    ///
    /// If the input parameter name already exists in the signature,
    pub fn with_input(
        self,
        spec: ParameterSpec,
    ) -> Self {
        match self.with_input_result(spec) {
            Ok(signature) => signature,
            Err(e) => panic!("{e}"),
        }
    }

    /// Extends the operator specification with an output parameter.
    ///
    /// # Parameters
    ///
    /// * `spec`: The output parameter specification to add.
    ///
    /// # Returns
    ///
    /// An `Result<Self, String>` where:
    /// * `Ok(Self)`: A new `FirehoseOperatorSignature` with the output
    ///   parameter added.
    /// * `Err(String)`: An error message if the output parameter name already
    ///   exists in the signature.
    pub fn with_output_result(
        self,
        spec: ParameterSpec,
    ) -> anyhow::Result<Self> {
        Ok(Self {
            operator_id: self.operator_id,
            description: self.description,
            inputs: self.inputs,
            outputs: Self::with_parameter(spec, "output", &self.outputs)?,
        })
    }

    /// Extends the operator specification with an output parameter.
    ///
    /// # Parameters
    ///
    /// * `spec`: The output parameter specification to add.
    ///
    /// # Returns
    ///
    /// A new `FirehoseOperatorSignature` with the output parameter added.
    ///
    /// # Panics
    ///
    /// If the output parameter name already exists in the signature,
    /// it will panic with an error message.
    pub fn with_output(
        self,
        spec: ParameterSpec,
    ) -> Self {
        match self.with_output_result(spec) {
            Ok(signature) => signature,
            Err(e) => panic!("{e}"),
        }
    }

    /// Generates a map of output column schemas for the given build plan.
    pub fn output_column_schemas_for_plan(
        &self,
        build_plan: &BuildPlan,
    ) -> anyhow::Result<BTreeMap<String, ColumnSchema>> {
        let mut result = BTreeMap::new();

        for output_param in &self.outputs {
            let param_name = &output_param.name;

            let column_name = build_plan.outputs.get(param_name).with_context(|| {
                format!("Output parameter '{param_name}' not found in build plan")
            })?;

            let data_type = output_param.data_type.clone();

            let mut col_schema = ColumnSchema::new_with_type(column_name, data_type);
            if output_param.description.is_some() {
                col_schema.description = output_param.description.clone();
            }

            result.insert(column_name.clone(), col_schema);
        }

        Ok(result)
    }

    /// Validates both inputs and outputs
    pub fn validate(
        &self,
        input_types: &BTreeMap<String, DataTypeDescription>,
        output_types: &BTreeMap<String, DataTypeDescription>,
    ) -> anyhow::Result<()> {
        self.validate_parameters("input", &self.inputs, input_types)?;
        self.validate_parameters("output", &self.outputs, output_types)?;
        Ok(())
    }

    /// Utility function to validate parameters against their specifications.
    ///
    /// # Arguments
    ///
    /// * `param_type`: A string indicating the type of parameters being
    ///   validated ("input" or "output").
    /// * `specs`: A slice of `ParameterSpec` that defines the expected
    ///   parameters.
    /// * `provided`: A map of provided parameters, where keys are parameter
    ///   names and values are their data types.
    ///
    /// # Returns
    ///
    /// An `anyhow::Result<()>` indicating success or failure.
    fn validate_parameters(
        &self,
        param_type: &str,
        specs: &[ParameterSpec],
        provided: &BTreeMap<String, DataTypeDescription>,
    ) -> anyhow::Result<()> {
        // Check for required parameters
        let required_params = specs;

        for spec in required_params {
            if !provided.contains_key(&spec.name) {
                bail!(
                    "Missing required {} parameter '{}' of type {:?}",
                    param_type,
                    spec.name,
                    spec.data_type
                );
            }

            if provided[&spec.name].type_name != spec.data_type.type_name {
                bail!(
                    "{} parameter '{}' expected type {:?}, but got {:?}",
                    param_type,
                    spec.name,
                    spec.data_type,
                    provided[&spec.name]
                );
            }
        }

        // Check for unknown parameters
        let expected_names: BTreeMap<String, &DataTypeDescription> = specs
            .iter()
            .map(|spec| (spec.name.clone(), &spec.data_type))
            .collect();

        for (name, data_type) in provided {
            match expected_names.get(name) {
                Some(expected_type) => {
                    if data_type.type_name != expected_type.type_name {
                        bail!(
                            "{param_type} parameter '{name}' expected type {expected_type:?}, but got {data_type:?}"
                        );
                    }
                }
                None => {
                    bail!(
                        "Unexpected {} parameter '{}'. Expected parameters: [{}]",
                        param_type,
                        name,
                        specs
                            .iter()
                            .map(|s| s.name.as_str())
                            .collect::<Vec<_>>()
                            .join(", ")
                    );
                }
            }
        }

        Ok(())
    }
}

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

    #[test]
    fn test_parameter_spec_new() {
        let spec: ParameterSpec = ParameterSpec::new::<i32>("count");
        assert_eq!(spec.name, "count");
        assert_eq!(spec.data_type.type_name, "i32");
    }

    #[test]
    fn test_parameter_spec_with_description() {
        let spec = ParameterSpec::new::<String>("name").with_description("The name of the user");
        assert_eq!(spec.description, Some("The name of the user".to_string()));
    }

    #[test]
    fn test_firehose_operator_signature_new() {
        let signature = FirehoseOperatorSignature::default()
            .with_operator_id("foo::bar::baz")
            .with_description("This is a test operator")
            .with_input(ParameterSpec::new::<i32>("count"))
            .with_output(ParameterSpec::new::<String>("result"));

        assert_eq!(
            signature.get_input("count"),
            Some(&ParameterSpec::new::<i32>("count"))
        );
        assert_eq!(signature.get_input("none"), None);
        assert_eq!(
            signature.get_output("result"),
            Some(&ParameterSpec::new::<String>("result"))
        );
        assert_eq!(signature.get_output("none"), None);

        assert_eq!(signature.operator_id, Some("foo::bar::baz".to_string()));
        assert_eq!(
            signature.description,
            Some("This is a test operator".to_string())
        );
        assert_eq!(signature.inputs, vec![ParameterSpec::new::<i32>("count")]);
        assert_eq!(
            signature.outputs,
            vec![ParameterSpec::new::<String>("result")]
        );
    }

    #[should_panic(expected = "Duplicate input parameter 'count':")]
    #[test]
    fn test_duplicate_input_parameter() {
        FirehoseOperatorSignature::default()
            .with_operator_id("foo::bar::baz")
            .with_input(ParameterSpec::new::<i32>("count"))
            .with_input(ParameterSpec::new::<String>("count")); // Duplicate name
    }

    #[should_panic(expected = "Duplicate output parameter 'count':")]
    #[test]
    fn test_duplicate_output_parameter() {
        FirehoseOperatorSignature::default()
            .with_operator_id("foo::bar::baz")
            .with_output(ParameterSpec::new::<i32>("count"))
            .with_output(ParameterSpec::new::<String>("count")); // Duplicate nam
    }
}