use super::{OwnedScalarExpr, Result, ScalarExpr, ScalarMemory};
use crate::{
ast::{BinaryOp, FunctionBinding, FunctionOrderSyntax},
plan::scalar::resources::{Control, Lowering},
};
use uqa_core::memory::{BudgetedVec, Produced, ProductionControl};
impl OwnedScalarExpr {
pub fn case(
base: Option<Self>,
branches: impl ExactSizeIterator<Item = (Self, Self)>,
fallback: Option<Self>,
control: &ProductionControl<'_>,
) -> Result<Self> {
control.check()?;
let mut lowering = owned_lowering(control);
let base = base
.map(|base| lowering.boxed(|lowering| Self::attach_child(lowering, base)))
.transpose()?;
let when = lowering.map(branches, |lowering, (condition, value)| {
Ok((
Self::attach_child(lowering, condition)?,
Self::attach_child(lowering, value)?,
))
})?;
let else_branch = fallback
.map(|fallback| lowering.boxed(|lowering| Self::attach_child(lowering, fallback)))
.transpose()?;
finish(
ScalarExpr::Case {
base,
when,
else_branch,
},
lowering,
control,
)
}
pub fn replace_composite_type(
mut self,
bound_type: Option<Produced<crate::ColumnType>>,
control: &ProductionControl<'_>,
) -> Result<Self> {
self.assert_owner(control);
control.check()?;
let ScalarExpr::CompositeRow { items, binding, .. } = &self.expression else {
panic!("composite type replacement requires a named row constructor");
};
let count = items.len();
let mut lowering = owned_lowering(control);
let binding =
lowering.composite_binding(crate::plan::scalar::source::Source::Borrowed(binding))?;
let (bound_type, memory) = match bound_type {
Some(bound_type) => {
let (bound_type, memory) = bound_type.into_parts();
(Some(bound_type), memory)
}
None => (None, control.empty_reservation()),
};
let memory = control.combine(control.empty_reservation(), memory);
if let Some(owner) = &mut lowering.control {
owner
.memory
.absorb(memory.expect("admitted composite type"));
}
let items = lowering.map(0..count, |lowering, index| {
Self::attach_child(lowering, self.take_child(index))
})?;
let output = finish(
ScalarExpr::CompositeRow {
items,
binding,
bound_type,
},
lowering,
control,
)?;
drop(self);
Ok(output)
}
pub fn array(
children: impl ExactSizeIterator<Item = Self>,
control: &ProductionControl<'_>,
) -> Result<Self> {
control.check()?;
let mut lowering = owned_lowering(control);
let items = lowering.map(children, Self::attach_child)?;
finish(ScalarExpr::Array(items), lowering, control)
}
pub fn row(
children: impl ExactSizeIterator<Item = Self>,
control: &ProductionControl<'_>,
) -> Result<Self> {
control.check()?;
let mut lowering = owned_lowering(control);
let items = lowering.map(children, Self::attach_child)?;
finish(ScalarExpr::Row(items), lowering, control)
}
pub fn binary(
op: BinaryOp,
left: Self,
right: Self,
control: &ProductionControl<'_>,
) -> Result<Self> {
control.check()?;
let mut lowering = owned_lowering(control);
let lhs = lowering.boxed(|lowering| Self::attach_child(lowering, left))?;
let rhs = lowering.boxed(|lowering| Self::attach_child(lowering, right))?;
finish(ScalarExpr::Binary { op, lhs, rhs }, lowering, control)
}
pub fn call(
name: Produced<String>,
binding: Option<Produced<FunctionBinding>>,
arguments: impl ExactSizeIterator<Item = Self>,
control: &ProductionControl<'_>,
) -> Result<Self> {
control.check()?;
let mut lowering = owned_lowering(control);
let (name, name_memory) = name.into_parts();
let (binding, binding_memory) = match binding {
Some(binding) => {
let (binding, memory) = binding.into_parts();
(Some(binding), memory)
}
None => (None, control.empty_reservation()),
};
let metadata = control.combine(name_memory, binding_memory);
if let Some(owner) = &mut lowering.control {
owner
.memory
.absorb(metadata.expect("admitted call metadata"));
}
let args = lowering.map(arguments, Self::attach_child)?;
finish(
ScalarExpr::Func {
name,
binding,
args,
distinct: false,
order_by: Vec::new(),
order_syntax: FunctionOrderSyntax::Ordinary,
filter: None,
},
lowering,
control,
)
}
}
pub(super) fn owned_lowering<'a>(control: &ProductionControl<'a>) -> Lowering<'a> {
Lowering {
control: control.budget().map(|_| Control::from_production(*control)),
owned_children: control.budget().map(BudgetedVec::new),
}
}
pub(super) fn finish(
expression: ScalarExpr,
lowering: Lowering<'_>,
control: &ProductionControl<'_>,
) -> Result<OwnedScalarExpr> {
let output = OwnedScalarExpr {
expression,
memory: lowering.control.map(|control| {
ScalarMemory::from_parts(
control.memory,
lowering
.owned_children
.expect("mutable generated expression"),
)
}),
};
control.check()?;
Ok(output)
}