use super::{Arc, ColumnType, RowSchema, SchemaLayoutError, SchemaLayoutResult, SchemaMap};
use crate::ast::InternalColumnRef;
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub(super) struct RelationPresence(Option<Arc<SchemaMap<Box<str>, InternalColumnRef>>>);
impl RelationPresence {
fn insert(&mut self, qualifier: &str, marker: InternalColumnRef) {
let bindings = self.0.get_or_insert_with(|| Arc::new(SchemaMap::default()));
Arc::make_mut(bindings).insert(qualifier.into(), marker);
}
pub(super) fn retaining(&self, keep: impl Fn(InternalColumnRef) -> bool) -> Self {
let Some(bindings) = &self.0 else {
return Self::default();
};
if bindings.values().all(|marker| keep(*marker)) {
return self.clone();
}
let retained: SchemaMap<_, _> = bindings
.iter()
.filter(|(_, marker)| keep(**marker))
.map(|(qualifier, marker)| (qualifier.clone(), *marker))
.collect();
Self((!retained.is_empty()).then(|| Arc::new(retained)))
}
pub(super) fn joined(left: &RowSchema, right: &RowSchema) -> Self {
let mut result = Self::default();
for (source, other) in [(left, right), (right, left)] {
for (qualifier, marker) in source.whole_row_presences() {
if !other.has_qualifier(qualifier) {
result.insert(qualifier, marker);
}
}
}
result
}
pub(super) fn with_outer(input: &RowSchema, outer: &RowSchema) -> Self {
let mut result = input.index.cold.row_presence.clone();
for (qualifier, marker) in outer.whole_row_presences() {
if !input.has_qualifier(qualifier) {
result.insert(qualifier, marker);
}
}
result
}
}
impl RowSchema {
pub(super) fn relayout_presence_slot(
&self,
target: &Self,
column: InternalColumnRef,
) -> Option<usize> {
target
.whole_row_presences()
.find_map(|(qualifier, marker)| {
(marker == column)
.then(|| self.whole_row_presence(qualifier))
.flatten()
.and_then(|marker| self.internal_slot(marker))
})
}
pub fn whole_row_presence(&self, qualifier: &str) -> Option<InternalColumnRef> {
self.index
.cold
.row_presence
.0
.as_ref()?
.get(qualifier)
.copied()
}
pub fn whole_row_presences(&self) -> impl Iterator<Item = (&str, InternalColumnRef)> {
self.index.cold.row_presence.0.iter().flat_map(|bindings| {
bindings
.iter()
.map(|(qualifier, marker)| (qualifier.as_ref(), *marker))
})
}
pub fn with_nullable_relations(&self, marker: InternalColumnRef) -> Option<Self> {
let mut qualifiers = self
.relation_qualifiers()
.filter(|qualifier| self.whole_row_presence(qualifier).is_none())
.peekable();
qualifiers.peek()?;
let mut result = Self::append_internal_typed(self, &[(marker, Some(ColumnType::Boolean))]);
let presence = &mut Arc::make_mut(&mut result.index).cold.row_presence;
for qualifier in qualifiers {
presence.insert(qualifier, marker);
}
Some(result)
}
pub fn with_whole_row_presences(
mut self,
bindings: impl IntoIterator<Item = (String, InternalColumnRef)>,
) -> SchemaLayoutResult<Self> {
let mut presence = RelationPresence::default();
for (qualifier, marker) in bindings {
if self.internal_slot(marker).is_none()
|| self.internal_type(marker) != Some(&ColumnType::Boolean)
{
return Err(SchemaLayoutError(
"whole-row presence requires a Boolean internal attribute".into(),
));
}
if presence
.0
.as_ref()
.is_some_and(|bindings| bindings.contains_key(qualifier.as_str()))
{
return Err(SchemaLayoutError(format!(
"duplicate whole-row presence qualifier `{qualifier}`"
)));
}
presence.insert(&qualifier, marker);
}
Arc::make_mut(&mut self.index).cold.row_presence = presence;
Ok(self)
}
}
#[cfg(test)]
mod tests;