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
use std::collections::BTreeMap;
use serde::{
Deserialize,
Serialize,
};
use crate::core::{
operations::{
environment::FirehoseOperatorEnvironment,
signature::FirehoseOperatorSignature,
},
schema::{
BuildPlan,
ColumnSchema,
FirehoseTableSchema,
},
};
/// A builder for constructing a call to an operator in a `BuildPlan`.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct OperationPlan {
/// The ID of the operator to be called.
pub operator_id: String,
/// A map from formal parameter names to column names for inputs.
pub inputs: BTreeMap<String, String>,
/// A map from formal parameter names to column names for outputs.
pub outputs: BTreeMap<String, String>,
/// An optional configuration for the call, serialized as JSON.
pub config: Option<serde_json::Value>,
}
impl OperationPlan {
/// Creates a new `CallBuilder` for the specified operator ID.
///
/// # Arguments
///
/// * `operator_id` - The ID of the operator to be called.
pub fn for_operation_id(operator_id: &str) -> Self {
Self {
operator_id: operator_id.to_string(),
inputs: BTreeMap::new(),
outputs: BTreeMap::new(),
config: None,
}
}
/// Adds an input parameter to the call builder.
///
/// # Arguments
///
/// * `pname` - The name of the input parameter.
/// * `cname` - The column name in the input table.
///
/// # Returns
///
/// The same `CallBuilder` instance with the input parameter added.
pub fn with_input(
mut self,
pname: &str,
cname: &str,
) -> Self {
if self.inputs.contains_key(pname) {
panic!("Input parameter '{pname}' already exists.");
}
self.inputs.insert(pname.to_string(), cname.to_string());
self
}
/// Adds an output parameter to the call builder.
///
/// # Arguments
///
/// * `pname` - The name of the output parameter.
/// * `cname` - The column name in the output table.
///
/// # Returns
///
/// The same `CallBuilder` instance with the output parameter added.
pub fn with_output(
mut self,
pname: &str,
cname: &str,
) -> Self {
if self.outputs.contains_key(pname) {
panic!("Output parameter '{pname}' already exists.");
}
self.outputs.insert(pname.to_string(), cname.to_string());
self
}
/// Adds a configuration to the call builder.
///
/// The configuration is serialized to JSON and stored in the call.
///
/// # Arguments
///
/// * `config` - The configuration to be added, which must implement
/// `Serialize`.
///
/// # Returns
///
/// The same `CallBuilder` instance with the configuration added.
pub fn with_config<T>(
mut self,
config: T,
) -> Self
where
T: Serialize,
{
self.config = Some(serde_json::to_value(config).expect("Failed to serialize config"));
self
}
/// Binds the context to a specific operator signature.
///
/// This does not validate the plan and signature against any schema.
///
/// # Arguments
///
/// * `signature` - The operator signature to bind to the context.
///
/// # Returns
///
/// An `anyhow::Result` of (`BuildPlan`, { column: `ColumnSchema`}).
pub fn plan_for_signature(
self,
signature: &FirehoseOperatorSignature,
) -> anyhow::Result<(BuildPlan, BTreeMap<String, ColumnSchema>)> {
let mut plan = BuildPlan::for_operator(self.operator_id);
plan.inputs = self.inputs.clone();
plan.outputs = self.outputs.clone();
if let Some(description) = &signature.description {
plan = plan.with_description(description);
}
if let Some(config) = &self.config {
plan = plan.with_config(config);
}
let output_cols = signature.output_column_schemas_for_plan(&plan)?;
Ok((plan, output_cols))
}
/// Applies the operation planner to a table schema and environment.
///
/// # Arguments
///
/// * `schema` - The mutable reference to the table schema to which the
/// operation will be applied.
/// * `env` - The environment that can create the operator based on the
/// build plan.
///
/// # Returns
///
/// A result containing a `BuildPlan` if successful, or an error message if
/// the operation fails.
pub fn apply_to_schema(
self,
schema: &mut FirehoseTableSchema,
env: &dyn FirehoseOperatorEnvironment,
) -> anyhow::Result<BuildPlan> {
env.apply_plan_to_schema(schema, self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_operation_plan_creation() {
let plan = OperationPlan::for_operation_id("example_operator")
.with_input("input1", "col1")
.with_output("output1", "col2");
assert_eq!(plan.operator_id, "example_operator");
assert_eq!(plan.inputs.get("input1"), Some(&"col1".to_string()));
assert_eq!(plan.outputs.get("output1"), Some(&"col2".to_string()));
}
#[should_panic(expected = "Input parameter 'input1' already exists.")]
#[test]
fn test_duplicate_input_panic() {
let _ = OperationPlan::for_operation_id("example_operator")
.with_input("input1", "col1")
.with_input("input1", "col2"); // This should panic
}
#[should_panic(expected = "Output parameter 'output1' already exists.")]
#[test]
fn test_duplicate_output_panic() {
let _ = OperationPlan::for_operation_id("example_operator")
.with_output("output1", "col1")
.with_output("output1", "col2"); // This should panic
}
pub struct BadConfig;
impl Serialize for BadConfig {
fn serialize<S>(
&self,
_serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
Err(serde::ser::Error::custom("Serialization failed"))
}
}
#[should_panic(expected = "Failed to serialize config")]
#[test]
fn test_with_config_serialization_failure() {
let _ = OperationPlan::for_operation_id("example_operator").with_config(BadConfig {}); // This should panic because `()` cannot be serialized to JSON
}
}