use super::{resources::Result, ScalarExpr};
use crate::schema::retention::CatalogRetentionError;
use uqa_core::{
memory::{BudgetedVec, MemoryReservation, Produced, ProductionControl},
Value,
};
mod call;
mod children;
mod composite;
mod composites;
mod construction;
mod constructors;
mod edit;
mod generated;
mod iteration;
mod literals;
mod metadata;
mod query_ids;
mod references;
mod replacement;
mod selection;
pub(in crate::plan) mod windows;
pub use iteration::OwnedScalarChildren;
pub use replacement::ScalarNodeTransform;
#[derive(Debug)]
pub struct OwnedScalarExpr {
pub(super) expression: ScalarExpr,
pub(super) memory: Option<ScalarMemory>,
}
#[derive(Debug)]
pub(crate) struct ScalarMemory {
pub(super) children: Vec<Option<Self>>,
pub(super) direct: MemoryReservation,
}
impl ScalarMemory {
pub(super) fn from_parts(
mut direct: MemoryReservation,
children: BudgetedVec<Option<Self>>,
) -> Self {
let (children, memory) = children.into_parts();
direct.absorb(memory);
Self { children, direct }
}
fn into_parts(self) -> (MemoryReservation, BudgetedVec<Option<Self>>) {
let Self {
children,
mut direct,
} = self;
let memory = direct.split(children.capacity() * size_of::<Option<Self>>());
(direct, BudgetedVec::from_parts(children, memory))
}
fn child_buffer(&mut self) -> ScalarChildren<'_> {
let children = std::mem::take(&mut self.children);
let memory = self
.direct
.split(children.capacity() * size_of::<Option<Self>>());
ScalarChildren {
parent: self,
buffer: Some(BudgetedVec::from_parts(children, memory)),
}
}
pub(super) fn reserve_children(&mut self, count: usize) -> Result<()> {
self.child_buffer()
.buffer
.as_mut()
.expect("child buffer")
.reserve(count)?;
Ok(())
}
pub(super) fn push_child(&mut self, child: Option<Self>) -> Result<()> {
self.child_buffer()
.buffer
.as_mut()
.expect("child buffer")
.push(child)?;
Ok(())
}
pub(crate) fn reserved_bytes(&self) -> usize {
self.direct.bytes()
+ self
.children
.iter()
.flatten()
.map(Self::reserved_bytes)
.sum::<usize>()
}
pub(crate) fn into_reservation(self) -> MemoryReservation {
let (mut direct, mut children) = self.into_parts();
while let Some(child) = children.pop() {
direct.absorb(child.expect("owned scalar child").into_reservation());
}
drop(children);
direct
}
}
struct ScalarChildren<'a> {
parent: &'a mut ScalarMemory,
buffer: Option<BudgetedVec<Option<ScalarMemory>>>,
}
impl Drop for ScalarChildren<'_> {
fn drop(&mut self) {
let (children, memory) = self.buffer.take().expect("child buffer").into_parts();
self.parent.children = children;
self.parent.direct.absorb(memory);
}
}
impl OwnedScalarExpr {
pub(crate) fn into_rewrite_parts(self) -> (ScalarExpr, Option<ScalarMemory>) {
(self.expression, self.memory)
}
pub(crate) fn from_rewrite_parts(expression: ScalarExpr, memory: Option<ScalarMemory>) -> Self {
Self { expression, memory }
}
pub fn uncontrolled(expression: ScalarExpr) -> Self {
Self {
expression,
memory: None,
}
}
pub fn literal(value: Produced<Value>, control: &ProductionControl<'_>) -> Result<Self> {
let (value, memory) = value.into_parts();
let output = Self {
expression: ScalarExpr::Literal(value),
memory: memory.map(|direct| ScalarMemory {
children: Vec::new(),
direct,
}),
};
output.assert_owner(control);
control.check()?;
Ok(output)
}
pub fn reserved_bytes(&self) -> usize {
self.memory.as_ref().map_or(0, ScalarMemory::reserved_bytes)
}
pub fn rewrite<E>(
self,
control: &ProductionControl<'_>,
rewrite: impl FnOnce(Self) -> std::result::Result<Self, E>,
) -> std::result::Result<Self, E>
where
E: From<CatalogRetentionError>,
{
self.assert_owner(control);
control.check().map_err(CatalogRetentionError::from)?;
let output = rewrite(self)?;
output.assert_owner(control);
control.check().map_err(CatalogRetentionError::from)?;
Ok(output)
}
pub fn child_count(&self) -> usize {
children::count(&self.expression)
}
pub fn select_child(mut self, index: usize) -> Self {
let child = self.take_child(index);
drop(self);
child
}
fn take_child(&mut self, index: usize) -> Self {
Self {
expression: std::mem::replace(
children::get_mut(&mut self.expression, index).expect("scalar child index"),
ScalarExpr::Literal(Value::Null),
),
memory: self
.memory
.as_mut()
.map(|memory| memory.children[index].take().expect("owned scalar child")),
}
}
pub fn map_children<E>(
mut self,
control: &ProductionControl<'_>,
mut map: impl FnMut(usize, Self) -> std::result::Result<Self, E>,
) -> std::result::Result<Self, E>
where
E: From<CatalogRetentionError>,
{
self.assert_owner(control);
control.check().map_err(CatalogRetentionError::from)?;
for ordinal in 0..self.child_count() {
control.check().map_err(CatalogRetentionError::from)?;
let child = self.take_child(ordinal);
let replacement = map(ordinal, child)?;
replacement.assert_owner(control);
control.check().map_err(CatalogRetentionError::from)?;
let Self { expression, memory } = replacement;
*children::get_mut(&mut self.expression, ordinal).expect("scalar child index") =
expression;
if let Some(parent) = &mut self.memory {
parent.children[ordinal] = memory;
}
}
control.check().map_err(CatalogRetentionError::from)?;
Ok(self)
}
pub fn map_child<E>(
mut self,
index: usize,
control: &ProductionControl<'_>,
map: impl FnOnce(Self) -> std::result::Result<Self, E>,
) -> std::result::Result<Self, E>
where
E: From<CatalogRetentionError>,
{
self.assert_owner(control);
control.check().map_err(CatalogRetentionError::from)?;
let replacement = map(self.take_child(index))?;
replacement.assert_owner(control);
control.check().map_err(CatalogRetentionError::from)?;
let Self { expression, memory } = replacement;
*children::get_mut(&mut self.expression, index).expect("scalar child index") = expression;
if let Some(parent) = &mut self.memory {
parent.children[index] = memory;
}
Ok(self)
}
pub fn finish(self, control: &ProductionControl<'_>) -> Result<Produced<ScalarExpr>> {
self.assert_owner(control);
control.check()?;
let Self { expression, memory } = self;
let memory = memory.map(ScalarMemory::into_reservation);
Ok(control.finish(expression, memory)?)
}
fn assert_owner(&self, control: &ProductionControl<'_>) {
match (&self.memory, control.budget()) {
(Some(memory), Some(budget)) => assert!(
memory.direct.budget().shares_allowance(budget),
"different scalar production allowance"
),
(None, None) => {}
_ => panic!("controlled and ordinary scalar ownership must not be mixed"),
}
}
}
impl std::ops::Deref for OwnedScalarExpr {
type Target = ScalarExpr;
fn deref(&self) -> &Self::Target {
&self.expression
}
}
#[cfg(test)]
mod tests;