use super::{
LogicalExpr, LogicalForeach, LogicalIfOp, LogicalLet, LogicalOp, LogicalReturn, LogicalSequence,
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Exits {
pub falls_through: bool,
pub returns: bool,
pub breaks: bool,
pub continues: bool,
pub throws: bool,
}
impl Exits {
pub const FALLS_THROUGH: Exits = Exits {
falls_through: true,
returns: false,
breaks: false,
continues: false,
throws: false,
};
pub fn union(self, other: Exits) -> Exits {
Exits {
falls_through: self.falls_through || other.falls_through,
returns: self.returns || other.returns,
breaks: self.breaks || other.breaks,
continues: self.continues || other.continues,
throws: self.throws || other.throws,
}
}
fn absorbing(self, returns: bool, loops: bool) -> Exits {
let caught = (returns && self.returns) || (loops && (self.breaks || self.continues));
Exits {
falls_through: self.falls_through || caught,
returns: self.returns && !returns,
breaks: self.breaks && !loops,
continues: self.continues && !loops,
throws: self.throws,
}
}
pub fn escapes_a_loop(self) -> bool {
self.breaks || self.continues
}
}
impl LogicalOp {
pub fn exits(&self) -> Exits {
match self {
LogicalOp::Return(LogicalReturn {
what,
fetch,
}) => {
let value = what.exits().absorbing(true, false);
let fetched =
fetch.iter().fold(Exits::default(), |acc, e| acc.union(expr_exits(e)));
Exits {
falls_through: false,
returns: true,
..value.union(fetched)
}
}
LogicalOp::Throw(throw) => Exits {
throws: true,
..expr_exits(&throw.value)
}
.without_falling_through(),
LogicalOp::Break(_) => Exits {
breaks: true,
..Exits::default()
},
LogicalOp::Continue(_) => Exits {
continues: true,
..Exits::default()
},
LogicalOp::Sequence(LogicalSequence {
stmts,
}) => {
let mut exits = Exits::FALLS_THROUGH;
for stmt in stmts {
let stmt = stmt.exits();
exits = Exits {
falls_through: stmt.falls_through,
..exits.union(stmt)
};
if !stmt.falls_through {
break;
}
}
exits
}
LogicalOp::If(ifs) => if_exits(ifs),
LogicalOp::Foreach(LogicalForeach {
range,
body,
..
}) => expr_exits(range).union(body.exits().absorbing(false, true)).with_falling_through(),
LogicalOp::Let(LogicalLet {
value,
..
}) => value.exits().absorbing(true, false),
other @ (LogicalOp::Source(_)
| LogicalOp::Values(_)
| LogicalOp::Match(_)
| LogicalOp::Subquery(_)
| LogicalOp::Write(_)
| LogicalOp::Ddl(_)
| LogicalOp::UnionAll(_)
| LogicalOp::Join(_)
| LogicalOp::Filter(_)
| LogicalOp::PermissionFilter(_)
| LogicalOp::Split(_)
| LogicalOp::Aggregate(_)
| LogicalOp::Distinct(_)
| LogicalOp::Window(_)
| LogicalOp::Sort(_)
| LogicalOp::Limit(_)
| LogicalOp::Project(_)
| LogicalOp::Fetch(_)
| LogicalOp::Timeout(_)
| LogicalOp::VersionScope(_)
| LogicalOp::Only(_)
| LogicalOp::Explain(_)
| LogicalOp::Eval(_)
| LogicalOp::Info(_)
| LogicalOp::Sleep(_)) => {
let mut exits = Exits::FALLS_THROUGH;
other.for_each_scalar(&mut |scalar| exits = exits.union(expr_exits(scalar)));
for child in other.children() {
exits = exits.union(child.exits().absorbing(true, false));
}
exits
}
}
}
}
impl LogicalSequence {
pub fn reachable_prefix(&self) -> &[LogicalOp] {
let reached = self
.stmts
.iter()
.position(|stmt| !stmt.exits().falls_through)
.map_or(self.stmts.len(), |at| at + 1);
&self.stmts[..reached]
}
}
impl Exits {
fn with_falling_through(self) -> Exits {
Exits {
falls_through: true,
..self
}
}
fn without_falling_through(self) -> Exits {
Exits {
falls_through: false,
..self
}
}
}
fn if_exits(ifs: &LogicalIfOp) -> Exits {
let mut exits = ifs
.conditions
.iter()
.fold(Exits::FALLS_THROUGH, |acc, condition| acc.union(expr_exits(condition)));
let arms = ifs.arms.iter().fold(None, |acc: Option<Exits>, arm| {
Some(acc.map_or_else(|| arm.exits(), |acc| acc.union(arm.exits())))
});
let arms = arms.unwrap_or(Exits::FALLS_THROUGH);
exits = Exits {
falls_through: arms.falls_through || ifs.otherwise().is_none(),
..exits.union(arms)
};
exits
}
fn expr_exits(expr: &LogicalExpr) -> Exits {
let mut exits = Exits::FALLS_THROUGH;
let _ = expr.visit(&mut |node| {
if matches!(node, LogicalExpr::Break) {
exits.breaks = true;
} else if matches!(node, LogicalExpr::Continue) {
exits.continues = true;
} else if matches!(node, LogicalExpr::Throw(_)) {
exits.throws = true;
} else if let LogicalExpr::Subquery(op) = node {
exits = exits.union(op.exits().absorbing(true, false).with_falling_through());
}
std::ops::ControlFlow::<()>::Continue(())
});
exits
}
#[cfg(test)]
mod tests {
use super::*;
use crate::expr::logical::lower;
use crate::parser::TopLevelExpr;
fn exits(sql: &str) -> Exits {
let ast = crate::parser::parse(sql).expect("test statement should parse");
let top = ast.expressions.into_iter().next().expect("one statement");
let TopLevelExpr::Expr(expr) = top else {
panic!("expected a bare expression");
};
lower(expr).expect("statement should lower").exits()
}
fn of(
falls_through: bool,
returns: bool,
breaks: bool,
continues: bool,
throws: bool,
) -> Exits {
Exits {
falls_through,
returns,
breaks,
continues,
throws,
}
}
#[test]
fn each_node_kind_has_its_summary() {
assert_eq!(exits("SELECT * FROM t"), Exits::FALLS_THROUGH);
assert_eq!(exits("1 + 1"), Exits::FALLS_THROUGH);
assert_eq!(exits("RETURN 1"), of(false, true, false, false, false));
assert_eq!(exits("THROW 'x'"), of(false, false, false, false, true));
assert_eq!(exits("BREAK"), of(false, false, true, false, false));
assert_eq!(exits("CONTINUE"), of(false, false, false, true, false));
assert_eq!(exits("{ 1; RETURN 2; THROW 3 }"), of(false, true, false, false, false));
assert_eq!(exits("{ 1; 2 }"), Exits::FALLS_THROUGH);
assert_eq!(exits("IF $a { BREAK }"), of(true, false, true, false, false));
assert_eq!(exits("IF $a { BREAK } ELSE { RETURN 1 }"), of(false, true, true, false, false));
assert_eq!(exits("IF $a { BREAK } ELSE { 1 }"), of(true, false, true, false, false));
assert_eq!(exits("FOR $x IN [1] { IF $x { BREAK }; CONTINUE }"), Exits::FALLS_THROUGH);
assert_eq!(
exits("FOR $x IN [1] { IF $x { THROW 'x' }; RETURN 1 }"),
of(true, true, false, false, true)
);
assert_eq!(exits("LET $v = { RETURN 1 }"), Exits::FALLS_THROUGH);
assert_eq!(exits("LET $v = IF $a { BREAK }"), of(true, false, true, false, false));
}
#[test]
fn a_block_reaches_its_statements_up_to_the_first_that_exits() {
fn reached(sql: &str) -> usize {
let ast = crate::parser::parse(sql).expect("test statement should parse");
let top = ast.expressions.into_iter().next().expect("one statement");
let TopLevelExpr::Expr(expr) = top else {
panic!("expected a bare expression");
};
let LogicalOp::Sequence(sequence) = lower(expr).expect("block should lower") else {
panic!("expected a block");
};
sequence.reachable_prefix().len()
}
assert_eq!(reached("{ 1; 2 }"), 2);
assert_eq!(reached("{ 1; RETURN 2; THROW 3 }"), 2);
assert_eq!(reached("{ RETURN 1; LET $auth = 2 }"), 1);
assert_eq!(reached("{ THROW 'a'; LET $session = 1 }"), 1);
assert_eq!(reached("{ IF $a { RETURN 1 }; 2 }"), 2);
}
#[test]
fn a_signal_nested_in_an_expression_leaves_the_statement() {
assert_eq!(exits("CREATE t SET a = { BREAK }"), of(true, false, true, false, false));
assert_eq!(exits("CREATE t SET a = { RETURN 1 }"), Exits::FALLS_THROUGH);
assert!(exits("BREAK").escapes_a_loop());
assert!(!exits("FOR $x IN [1] { BREAK }").escapes_a_loop());
}
}