-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathsink.rs
More file actions
192 lines (169 loc) · 6.14 KB
/
Copy pathsink.rs
File metadata and controls
192 lines (169 loc) · 6.14 KB
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
use std::sync::Arc;
use arroyo_datastream::logical::{LogicalEdge, LogicalEdgeType, LogicalNode, OperatorName};
use arroyo_rpc::{
UPDATING_META_FIELD,
api_types::connections::ConnectionType,
df::{ArroyoSchema, ArroyoSchemaRef},
};
use datafusion::common::{DFSchemaRef, Result, TableReference, plan_err};
use datafusion::logical_expr::{Expr, Extension, LogicalPlan, UserDefinedLogicalNodeCore};
use prost::Message;
use crate::{
builder::{NamedNode, Planner},
multifield_partial_ord,
tables::Table,
};
use super::{
ArroyoExtension, NodeWithIncomingEdges, debezium::ToDebeziumExtension,
remote_table::RemoteTableExtension,
};
pub(crate) const SINK_NODE_NAME: &str = "SinkExtension";
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct SinkExtension {
pub(crate) name: TableReference,
pub(crate) table: Table,
pub(crate) schema: DFSchemaRef,
inputs: Arc<Vec<LogicalPlan>>,
}
multifield_partial_ord!(SinkExtension, name, inputs);
impl SinkExtension {
pub fn new(
name: TableReference,
table: Table,
mut schema: DFSchemaRef,
mut input: Arc<LogicalPlan>,
) -> Result<Self> {
let input_is_updating = input
.schema()
.has_column_with_unqualified_name(UPDATING_META_FIELD);
match &table {
Table::ConnectorTable(connector_table) => {
if connector_table.connection_type == ConnectionType::Source {
return plan_err!(
"attempted to insert into table '{}', but it is a source",
connector_table.name
);
}
match (input_is_updating, connector_table.is_updating()) {
(_, true) => {
let to_debezium_extension =
ToDebeziumExtension::try_new(input.as_ref().clone())?;
input = Arc::new(LogicalPlan::Extension(Extension {
node: Arc::new(to_debezium_extension),
}));
schema = input.schema().clone();
}
(true, false) => {
return plan_err!(
"input is updating, but sink is not configured as an updating sink (hint: use `format = 'debezium_json'`)"
);
}
(false, false) => {}
}
}
Table::LookupTable(..) => return plan_err!("cannot use a lookup table as a sink"),
Table::MemoryTable { .. } => return plan_err!("memory tables not supported"),
Table::TableFromQuery { .. } => {}
Table::PreviewSink { .. } => {
if input_is_updating {
let to_debezium_extension =
ToDebeziumExtension::try_new(input.as_ref().clone())?;
input = Arc::new(LogicalPlan::Extension(Extension {
node: Arc::new(to_debezium_extension),
}));
schema = input.schema().clone();
}
}
}
Self::add_remote_if_necessary(&schema, &mut input);
let inputs = Arc::new(vec![(*input).clone()]);
Ok(Self {
name,
table,
schema,
inputs,
})
}
// The input to a sink needs to be a non-transparent logical plan extension.
// If it isn't, wrap the input in a RemoteTableExtension.
pub fn add_remote_if_necessary(schema: &DFSchemaRef, input: &mut Arc<LogicalPlan>) {
if let LogicalPlan::Extension(node) = input.as_ref() {
let arroyo_extension: &dyn ArroyoExtension = (&node.node).try_into().unwrap();
if !arroyo_extension.transparent() {
return;
}
}
let remote_table_extension = RemoteTableExtension {
input: input.as_ref().clone(),
name: TableReference::bare("sink projection"),
schema: schema.clone(),
materialize: false,
};
*input = Arc::new(LogicalPlan::Extension(Extension {
node: Arc::new(remote_table_extension),
}));
}
}
impl UserDefinedLogicalNodeCore for SinkExtension {
fn name(&self) -> &str {
SINK_NODE_NAME
}
fn inputs(&self) -> Vec<&LogicalPlan> {
self.inputs.iter().collect()
}
fn schema(&self) -> &DFSchemaRef {
&self.schema
}
fn expressions(&self) -> Vec<Expr> {
vec![]
}
fn fmt_for_explain(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "SinkExtension({:?}): {}", self.name, self.schema)
}
fn with_exprs_and_inputs(&self, _exprs: Vec<Expr>, inputs: Vec<LogicalPlan>) -> Result<Self> {
Ok(Self {
name: self.name.clone(),
table: self.table.clone(),
schema: self.schema.clone(),
inputs: Arc::new(inputs),
})
}
}
impl ArroyoExtension for SinkExtension {
fn node_name(&self) -> Option<NamedNode> {
match &self.table {
Table::PreviewSink { .. } => None,
_ => Some(NamedNode::Sink(self.name.clone())),
}
}
fn plan_node(
&self,
_planner: &Planner,
index: usize,
input_schemas: Vec<ArroyoSchemaRef>,
) -> Result<NodeWithIncomingEdges> {
let operator_config = (self
.table
.connector_op()
.map_err(|e| e.context("connector op"))?)
.encode_to_vec();
let node = LogicalNode::single(
index as u32,
format!("sink_{}_{}", self.name, index),
OperatorName::ConnectorSink,
operator_config,
self.table.connector_op()?.description.clone(),
1,
);
let edges = input_schemas
.into_iter()
.map(|input_schema| {
LogicalEdge::project_all(LogicalEdgeType::Forward, (*input_schema).clone())
})
.collect();
Ok(NodeWithIncomingEdges { node, edges })
}
fn output_schema(&self) -> ArroyoSchema {
ArroyoSchema::from_fields(vec![])
}
}