use super::*;
use std::cell::Cell;
thread_local! {
static LEGACY_MAP_COLUMNS: Cell<bool> = const { Cell::new(false) };
}
#[doc(hidden)]
pub fn with_legacy_graph_group_keys<T>(f: impl FnOnce() -> T) -> T {
struct Restore(bool);
impl Drop for Restore {
fn drop(&mut self) {
LEGACY_MAP_COLUMNS.with(|legacy| legacy.set(self.0));
}
}
let _restore = Restore(LEGACY_MAP_COLUMNS.with(|legacy| legacy.replace(true)));
f()
}
pub(super) fn slot_names(
planned: &PlannedReturns,
scope: &Scope,
projection: &ProjectionClause,
names: &[String],
with_clause: bool,
) -> BTreeMap<String, GraphEntityKind> {
let mut slots = BTreeMap::new();
if !with_clause
|| LEGACY_MAP_COLUMNS.with(Cell::get)
|| planned.names() != names
|| projection.items.len() != names.len()
{
return slots;
}
let native = |variable: &str| match scope.0.get(variable) {
Some(BindingType::Graph { kind, column: None }) => Some(*kind),
_ => None,
};
match planned {
PlannedReturns::AggregateProjection {
group_keys,
items,
projections,
} if !projection.distinct
&& !items
.iter()
.any(|item| matches!(item.target, AggregateTarget::Ranked { .. })) =>
{
for (projection, name) in projections.iter().zip(names) {
let ProjectionExpression::Column(column) = &projection.expression else {
continue;
};
let Some(ProjectionExpression::Variable { variable }) = group_keys
.iter()
.find(|key| &key.name == column)
.map(|key| &key.expression)
else {
continue;
};
if let Some(kind) = native(variable) {
slots.insert(name.clone(), kind);
}
}
}
PlannedReturns::Projections(items) if projection.distinct => {
for (item, name) in items.iter().zip(names) {
if let ProjectionExpression::Variable { variable } = &item.expression
&& let Some(kind) = native(variable)
{
slots.insert(name.clone(), kind);
}
}
}
_ => {}
}
slots
}
pub(super) fn into_logical(
planned: PlannedReturns,
slots: &BTreeMap<String, GraphEntityKind>,
input: LogicalPlan,
) -> LogicalPlan {
match planned {
PlannedReturns::Projections(group_keys) => LogicalPlan::Aggregate {
group_keys,
items: Vec::new(),
graph_keys: GraphGroupKeys::DistinctSlots,
input: Box::new(input),
},
PlannedReturns::AggregateProjection {
mut group_keys,
items,
projections,
} => {
let mut remaining = Vec::with_capacity(projections.len());
for projection in projections {
let key = match (&projection.expression, slots.contains_key(&projection.name)) {
(ProjectionExpression::Column(column), true) => {
group_keys.iter_mut().find(|key| &key.name == column)
}
_ => None,
};
match key {
Some(key) => key.name = projection.name,
None => remaining.push(projection),
}
}
collapse_identity(group_keys, items, remaining, input)
}
PlannedReturns::Aggregations { group_keys, items } => LogicalPlan::Aggregate {
group_keys,
items,
graph_keys: GraphGroupKeys::Slots,
input: Box::new(input),
},
}
}
fn collapse_identity(
mut group_keys: Vec<Projection>,
mut items: Vec<Aggregation>,
projections: Vec<Projection>,
input: LogicalPlan,
) -> LogicalPlan {
let internal = group_keys
.iter()
.map(|key| &key.name)
.chain(items.iter().map(|item| &item.name))
.filter(|name| name.starts_with('\0'))
.count();
let mut renames = BTreeMap::new();
let identity = projections.len() == internal
&& projections.iter().all(|projection| {
matches!(&projection.expression, ProjectionExpression::Column(column)
if column.starts_with('\0')
&& renames.insert(column.clone(), projection.name.clone()).is_none())
});
if !identity {
return LogicalPlan::Project {
items: projections,
input: Box::new(LogicalPlan::Aggregate {
group_keys,
items,
graph_keys: GraphGroupKeys::Slots,
input: Box::new(input),
}),
};
}
for name in group_keys
.iter_mut()
.map(|key| &mut key.name)
.chain(items.iter_mut().map(|item| &mut item.name))
{
if let Some(renamed) = renames.remove(name.as_str()) {
*name = renamed;
}
}
LogicalPlan::Aggregate {
group_keys,
items,
graph_keys: GraphGroupKeys::Slots,
input: Box::new(input),
}
}