use crate::dsl::graph::{GraphNodeKind, LogicalGraph, LowerState};
pub(crate) fn lower(mut graph: LogicalGraph, app_id: &str) -> crate::topology::Topology {
let reuse_changelog = graph
.nodes
.iter()
.filter_map(|n| match &n.kind {
GraphNodeKind::TableSource {
topic,
reuse_source_for_changelog: true,
..
} => Some((n.id, topic.clone())),
_ => None,
})
.collect();
let mut state = LowerState {
topology: crate::topology::Topology::new(),
app_id: app_id.to_string(),
handle_name: std::collections::HashMap::new(),
reuse_changelog,
};
let aliases = std::mem::take(&mut graph.aliases);
for node in &mut graph.nodes {
if let Some(&target) = aliases.get(&node.id) {
debug_assert!(
target < node.id,
"alias keeper id {target} must precede aliased id {} for id-order lowering",
node.id
);
let name = state.handle_name[&target].clone();
state.handle_name.insert(node.id, name);
node.lower.take();
continue;
}
if let Some(thunk) = node.lower.take() {
thunk(&mut state);
}
}
state.topology
}