use super::{OwnedScalarExpr, Result, ScalarExpr, ScalarMemory};
use crate::plan::scalar::resources::{Control, Lowering};
use uqa_core::{
memory::{BudgetedVec, ProductionControl},
Value,
};
impl OwnedScalarExpr {
pub fn typed_null(ty: &str, control: &ProductionControl<'_>) -> Result<Self> {
let (ty, memory) = control.copy_text(ty)?.into_parts();
let output = Self {
expression: ScalarExpr::TypedLiteral {
value: Value::Null,
ty,
bound_type: None,
collation_oid: None,
composite_source: None,
parameter_index: None,
},
memory: memory.map(|direct| ScalarMemory {
children: Vec::new(),
direct,
}),
};
control.check()?;
Ok(output)
}
pub fn boolean(
children: impl ExactSizeIterator<Item = Self>,
conjunction: bool,
control: &ProductionControl<'_>,
) -> Result<Self> {
control.check()?;
let mut lowering = Lowering {
control: control.budget().map(|_| Control::from_production(*control)),
owned_children: control.budget().map(BudgetedVec::new),
};
let items = lowering.map(children, Self::attach_child)?;
let expression = if conjunction {
ScalarExpr::And(items)
} else {
ScalarExpr::Or(items)
};
let output = Self {
expression,
memory: lowering.control.map(|control| {
ScalarMemory::from_parts(
control.memory,
lowering
.owned_children
.expect("mutable scalar construction"),
)
}),
};
control.check()?;
Ok(output)
}
pub(in crate::plan::scalar) fn attach_child(
lowering: &mut Lowering<'_>,
child: Self,
) -> Result<ScalarExpr> {
let control = lowering
.control
.as_ref()
.map_or_else(ProductionControl::uncontrolled, |control| {
control.production
});
child.assert_owner(&control);
control.check()?;
if let Some(children) = &mut lowering.owned_children {
children.reserve(1)?;
}
let Self { expression, memory } = child;
if let Some(children) = &mut lowering.owned_children {
children
.push(memory)
.expect("scalar child slot was admitted");
}
Ok(expression)
}
}