use super::{OwnedScalarExpr, ScalarExpr};
use crate::SQLError;
use uqa_core::memory::ProductionControl;
impl OwnedScalarExpr {
pub(crate) fn remap_query_occurrences(
mut self,
control: &ProductionControl<'_>,
map: &mut dyn FnMut(usize) -> Result<usize, SQLError>,
) -> Result<Self, SQLError> {
self.assert_owner(control);
let mut result: Result<(), SQLError> = Ok(());
crate::plan::rewrite_scalar_expression(&mut self.expression, &mut |node| {
if result.is_err() {
return;
}
result = (|| {
control.check()?;
match node {
ScalarExpr::ScalarSubquery(id)
| ScalarExpr::Exists { subquery: id, .. }
| ScalarExpr::InSubquery { subquery: id, .. } => *id = map(*id)?,
_ => {}
}
Ok(())
})();
});
result?;
Ok(self)
}
pub fn remap_subquery_ids(
mut self,
control: &ProductionControl<'_>,
map: &mut dyn FnMut(usize) -> usize,
) -> Result<Self, SQLError> {
self.assert_owner(control);
let mut checked = Ok(());
self.expression.visit_mut(&mut |node| {
if checked.is_ok() {
checked = control.check();
match node {
ScalarExpr::ScalarSubquery(id)
| ScalarExpr::Exists { subquery: id, .. }
| ScalarExpr::InSubquery { subquery: id, .. } => *id = map(*id),
_ => {}
}
}
});
checked?;
Ok(self)
}
}